Skip to content

Commit 8173275

Browse files
k1-801CalcProgrammer1
authored andcommitted
Removed acpiwmi, updated TUF controller to be self-contained
1 parent a7885ba commit 8173275

File tree

7 files changed

+275
-333
lines changed

7 files changed

+275
-333
lines changed

Controllers/AsusTUFLaptopController/AsusTUFLaptopController.cpp

Lines changed: 236 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,238 @@
11
#ifdef _WIN32
22

33
#include "AsusTUFLaptopController.h"
4-
#include "acpiwmi.h"
4+
5+
#include <Objbase.h>
6+
#include <setupapi.h>
7+
#include <comdef.h>
8+
#include <Wbemidl.h>
9+
10+
#include <cstdint>
11+
#include <cstring>
12+
#include <cstdio>
13+
14+
static bool coInitialized = 0;
15+
16+
static GUID CLSID_GUID_DEVCLASS_SYSTEM = { 0x4D36E97D, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } };
17+
18+
int AsusTUFLaptopController::checkWMIType()
19+
{
20+
int n;
21+
int v6;
22+
int result = 0;
23+
struct _SP_DEVINFO_DATA DeviceInfoData;
24+
const int bufsize = 260;
25+
wchar_t PropertyBuffer[bufsize];
26+
27+
HDEVINFO devinfo = SetupDiGetClassDevsW(&CLSID_GUID_DEVCLASS_SYSTEM, 0, 0, 2u);
28+
if ( devinfo == HDEVINFO(-1) )
29+
{
30+
return 0;
31+
}
32+
n = 0;
33+
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
34+
35+
while ( SetupDiEnumDeviceInfo(devinfo, n, &DeviceInfoData) ) // Invalid buffer
36+
{
37+
if ( SetupDiGetDeviceRegistryPropertyW(devinfo,
38+
&DeviceInfoData,
39+
SPDRP_ENUMERATOR_NAME,
40+
NULL,
41+
PBYTE(PropertyBuffer),
42+
sizeof(PropertyBuffer),
43+
0) )
44+
{
45+
// If we found property "ACPI"
46+
if(!wcscmp(PropertyBuffer, L"ACPI"))
47+
{
48+
memset(PropertyBuffer, 0, sizeof(PropertyBuffer));
49+
if ( SetupDiGetDeviceInstanceIdW(devinfo, &DeviceInfoData, PropertyBuffer, bufsize, 0) )
50+
{
51+
_wcsupr_s(PropertyBuffer, bufsize);
52+
if(wcsstr(PropertyBuffer, L"ACPI\\ATK0100"))
53+
{
54+
result = 1;
55+
break;
56+
}
57+
if(!wcscmp(PropertyBuffer, L"ACPI\\PNP0C14\\ATK"))
58+
{
59+
result = 2;
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
++n;
66+
}
67+
SetupDiDestroyDeviceInfoList(devinfo);
68+
return result;
69+
}
570

671
AsusTUFLaptopController::AsusTUFLaptopController()
772
{
8-
/*---------------------------------------------------*\
9-
| Call AsWMI_Open at least once during initialization.|
10-
| Since it's part of the firmware it's guaranteed to |
11-
| always be there. This is necessary to issue further |
12-
| commands. |
13-
\*---------------------------------------------------*/
14-
AsWMI_Open();
73+
hDevice = CreateFileW(L"\\\\.\\ATKACPI", 0xC0000000, 3u, 0, 3u, 0, 0);
74+
}
75+
76+
AsusTUFLaptopController* AsusTUFLaptopController::checkAndCreate()
77+
{
78+
// This might cause issues when coInitialize() is used in multiple places
79+
HRESULT init = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
80+
if ( init < 0 && init != 0x80010106 )
81+
return 0;
82+
coInitialized = 1;
83+
84+
int type = checkWMIType();
85+
if ( type == 2 )
86+
{
87+
AsusTUFLaptopController* controller = new AsusTUFLaptopController();
88+
if(controller->hDevice != HANDLE(-1))
89+
{
90+
return controller;
91+
}
92+
delete controller;
93+
}
94+
return nullptr;
1595
}
1696

1797
AsusTUFLaptopController::~AsusTUFLaptopController()
1898
{
99+
if ( hDevice && hDevice != HANDLE(-1) )
100+
{
101+
CloseHandle(hDevice);
102+
hDevice = 0;
103+
}
104+
// This might cause issues when coInitialize() is used in multiple places
105+
if ( coInitialized )
106+
{
107+
CoUninitialize();
108+
coInitialized = 0;
109+
}
110+
111+
}
112+
113+
bool AsusTUFLaptopController::deviceIoControlWrapper(const void *dataIn, int commandIndex, int dataSizeIn, void *dataOut, int *dataSizeOut)
114+
{
115+
size_t BytesReturned;
116+
const int bufsize = 1024;
117+
char outBuffer[bufsize];
118+
119+
LPDWORD inBuffer = LPDWORD(malloc(dataSizeIn + 8));
120+
inBuffer[0] = commandIndex;
121+
inBuffer[1] = dataSizeIn;
122+
memmove(inBuffer + 2, dataIn, dataSizeIn);
123+
memset(outBuffer, 0, bufsize);
124+
BytesReturned = 0;
125+
bool result = DeviceIoControl(
126+
hDevice,
127+
0x22240Cu,
128+
inBuffer,
129+
dataSizeIn + 8,
130+
outBuffer,
131+
bufsize,
132+
LPDWORD(&BytesReturned),
133+
0);
134+
if ( result )
135+
{
136+
if ( *dataSizeOut < BytesReturned )
137+
{
138+
BytesReturned = *dataSizeOut;
139+
}
140+
memmove(dataOut, outBuffer, BytesReturned);
141+
}
142+
free(inBuffer);
143+
return result;
144+
}
145+
146+
bool AsusTUFLaptopController::deviceControl(int a1, int a2)
147+
{
148+
if ( hDevice && hDevice != HANDLE(-1) )
149+
{
150+
int data[2];
151+
data[0] = a1;
152+
data[1] = a2;
153+
int result;
154+
int outBufSize = 4;
155+
if ( deviceIoControlWrapper(&data, 1398162756, 8, &result, &outBufSize) )
156+
{
157+
if(outBufSize < 4)
158+
{
159+
result = 0;
160+
}
161+
if ( result == 1 )
162+
{
163+
return 1;
164+
}
165+
}
166+
}
167+
return 0;
168+
}
169+
170+
bool AsusTUFLaptopController::deviceControl(int a1, int a2, int a3)
171+
{
172+
unsigned int data[3];
173+
data[0] = a1;
174+
data[1] = a2;
175+
data[2] = a3;
176+
int outBuf;
177+
int outBufSize = 4;
178+
179+
if ( hDevice && hDevice != HANDLE(-1) )
180+
{
181+
if ( deviceIoControlWrapper(data, 0x53564544, 12, &outBuf, &outBufSize) )
182+
{
183+
if(outBufSize < 4)
184+
{
185+
outBuf = 0;
186+
}
187+
if ( outBuf == 1 )
188+
{
189+
return 1;
190+
}
191+
}
192+
}
193+
return 0;
194+
}
195+
196+
bool AsusTUFLaptopController::getStatus(int a1, int *out)
197+
{
198+
int status;
199+
int statusSize = 4;
200+
201+
if ( !hDevice || hDevice == HANDLE(-1) || (!deviceIoControlWrapper(&a1, 1398035268, 4, &status, &statusSize)) )
202+
{
203+
return 0;
204+
}
205+
if(statusSize < 4)
206+
{
207+
status = 0;
208+
}
209+
*out = status;
210+
return 1;
211+
}
212+
213+
bool AsusTUFLaptopController::getStatusExtended(int a1, int a2, int *status1, int *status2, int* status3)
214+
{
215+
int commandData[2];
216+
commandData[0] = a1;
217+
commandData[1] = a2;
218+
int statusBuffer[3];
219+
int statusSize = 12;
19220

221+
if ( hDevice && hDevice != HANDLE(-1)
222+
&& deviceIoControlWrapper(commandData, 1398035268, 8, statusBuffer, &statusSize) )
223+
{
224+
*status1 = statusBuffer[0];
225+
*status2 = statusBuffer[1];
226+
*status3 = statusBuffer[2];
227+
return 1;
228+
}
229+
else
230+
{
231+
return 0;
232+
}
20233
}
21234

22-
void AsusTUFLaptopController::SetMode(unsigned char red,
235+
void AsusTUFLaptopController::setMode(unsigned char red,
23236
unsigned char green,
24237
unsigned char blue,
25238
unsigned char mode,
@@ -63,34 +276,34 @@ void AsusTUFLaptopController::SetMode(unsigned char red,
63276
unsigned int high = save_val | (mode<<8) | (red<<16) | (green<<24);
64277
unsigned int low = blue | (speed_val<<8);
65278

66-
AsWMI_NB_DeviceControl_2arg(ASUS_WMI_DEVID_TUF_RGB_MODE, high, low);
279+
deviceControl(ASUS_WMI_DEVID_TUF_RGB_MODE, high, low);
67280

68281
}
69282

70-
unsigned char AsusTUFLaptopController::GetBrightness()
283+
unsigned char AsusTUFLaptopController::getBrightness()
71284
{
72285
int backlight_state = 0;
73-
AsWMI_NB_GetDeviceStatus(ASUS_WMI_DEVID_KBD_BACKLIGHT, &backlight_state);
286+
getStatus(ASUS_WMI_DEVID_KBD_BACKLIGHT, &backlight_state);
74287

75288
/*----------------------------------------------*\
76289
| Only lowest two bits indicate brightness level |
77290
\*----------------------------------------------*/
78291
return backlight_state & 0x7F;
79292
}
80293

81-
void AsusTUFLaptopController::SetBrightness(unsigned char brightness)
294+
void AsusTUFLaptopController::setBrightness(unsigned char brightness)
82295
{
83296
/*-----------------------------------------------------*\
84297
| Only calls in this format persistently set brightness |
85298
\*-----------------------------------------------------*/
86299
int ctrl_param = 0x80 | (brightness & 0x7F);
87-
AsWMI_NB_DeviceControl(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param);
300+
deviceControl(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param);
88301
}
89302

90303
/*-----------------------------------------------------------*\
91304
| These settings will not persist a reboot unless save is set |
92305
\*-----------------------------------------------------------*/
93-
void AsusTUFLaptopController::SetPowerState(bool boot,
306+
void AsusTUFLaptopController::setPowerState(bool boot,
94307
bool awake,
95308
bool sleep,
96309
bool shutdown,
@@ -105,7 +318,13 @@ void AsusTUFLaptopController::SetPowerState(bool boot,
105318

106319
if (save) state = state | ASUS_WMI_KEYBOARD_POWER_SAVE;
107320

108-
AsWMI_NB_DeviceControl(ASUS_WMI_DEVID_TUF_RGB_STATE, state);
321+
deviceControl(ASUS_WMI_DEVID_TUF_RGB_STATE, state);
109322
}
110323

111-
#endif // _WIN32
324+
void AsusTUFLaptopController::setFanMode(int mode)
325+
{
326+
deviceControl(ASUS_WMI_DEVID_FAN_BOOST_MODE, mode);
327+
}
328+
329+
330+
#endif

Controllers/AsusTUFLaptopController/AsusTUFLaptopController.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
#ifdef _WIN32
2-
31
#ifndef ASUSTUFLAPTOPCONTROLLER_H
42
#define ASUSTUFLAPTOPCONTROLLER_H
53

4+
#ifdef _WIN32
5+
6+
#include <windows.h>
7+
68
#define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
79
#define ASUS_WMI_DEVID_TUF_RGB_MODE 0x00100056
810
#define ASUS_WMI_DEVID_TUF_RGB_STATE 0x00100057
911

12+
#define ASUS_WMI_DEVID_FAN_BOOST_MODE 0x00110018
13+
#define ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY 0x00120075
14+
1015
#define ASUS_WMI_KEYBOARD_SPEED_SLOW 0xE1
1116
#define ASUS_WMI_KEYBOARD_SPEED_NORMAL 0xEB
1217
#define ASUS_WMI_KEYBOARD_SPEED_FAST 0xF5
@@ -32,29 +37,46 @@
3237

3338
#define ASUS_WMI_KEYBOARD_POWER_SAVE 0x01<<8
3439

40+
#define ASUS_WMI_FAN_SPEED_NORMAL 0
41+
#define ASUS_WMI_FAN_SPEED_TURBO 1
42+
#define ASUS_WMI_FAN_SPEED_SILENT 2
43+
3544
class AsusTUFLaptopController
3645
{
37-
public:
46+
private:
47+
HANDLE hDevice;
48+
static int checkWMIType();
3849
AsusTUFLaptopController();
50+
51+
bool deviceIoControlWrapper(const void *dataIn, int commandIndex, int dataSizeIn, void *dataOut, int *dataSizeOut);
52+
bool deviceControl(int a1, int a2);
53+
bool deviceControl(int a1, int a2, int a3);
54+
bool getStatus(int a1, int *out);
55+
bool getStatusExtended(int a1, int a2, int *status1, int *status2, int* status3);
56+
57+
public:
58+
static AsusTUFLaptopController * checkAndCreate();
3959
~AsusTUFLaptopController();
4060

41-
void SetMode(unsigned char red,
61+
void setMode(unsigned char red,
4262
unsigned char green,
4363
unsigned char blue,
4464
unsigned char mode,
4565
unsigned char speed,
4666
bool save);
4767

48-
unsigned char GetBrightness();
49-
void SetBrightness(unsigned char brightness);
68+
unsigned char getBrightness();
69+
void setBrightness(unsigned char brightness);
5070

51-
void SetPowerState(bool boot,
71+
void setPowerState(bool boot,
5272
bool awake,
5373
bool sleep,
5474
bool shutdown,
5575
bool save);
76+
77+
void setFanMode(int mode);
5678
};
5779

58-
#endif // ASUSTUFLAPTOPCONTROLLER_H
80+
#endif
5981

60-
#endif // _WIN32
82+
#endif // ASUSTUFLAPTOPCONTROLLER_H

0 commit comments

Comments
 (0)