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
671AsusTUFLaptopController::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
1797AsusTUFLaptopController::~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
0 commit comments