Removed acpiwmi, updated TUF controller to be self-contained

This commit is contained in:
Dmitry K 2023-07-05 23:34:25 +00:00 committed by Adam Honse
parent a7885bac57
commit 817327586d
7 changed files with 275 additions and 333 deletions

View file

@ -1,25 +1,238 @@
#ifdef _WIN32
#include "AsusTUFLaptopController.h"
#include "acpiwmi.h"
#include <Objbase.h>
#include <setupapi.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <cstdint>
#include <cstring>
#include <cstdio>
static bool coInitialized = 0;
static GUID CLSID_GUID_DEVCLASS_SYSTEM = { 0x4D36E97D, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } };
int AsusTUFLaptopController::checkWMIType()
{
int n;
int v6;
int result = 0;
struct _SP_DEVINFO_DATA DeviceInfoData;
const int bufsize = 260;
wchar_t PropertyBuffer[bufsize];
HDEVINFO devinfo = SetupDiGetClassDevsW(&CLSID_GUID_DEVCLASS_SYSTEM, 0, 0, 2u);
if ( devinfo == HDEVINFO(-1) )
{
return 0;
}
n = 0;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
while ( SetupDiEnumDeviceInfo(devinfo, n, &DeviceInfoData) ) // Invalid buffer
{
if ( SetupDiGetDeviceRegistryPropertyW(devinfo,
&DeviceInfoData,
SPDRP_ENUMERATOR_NAME,
NULL,
PBYTE(PropertyBuffer),
sizeof(PropertyBuffer),
0) )
{
// If we found property "ACPI"
if(!wcscmp(PropertyBuffer, L"ACPI"))
{
memset(PropertyBuffer, 0, sizeof(PropertyBuffer));
if ( SetupDiGetDeviceInstanceIdW(devinfo, &DeviceInfoData, PropertyBuffer, bufsize, 0) )
{
_wcsupr_s(PropertyBuffer, bufsize);
if(wcsstr(PropertyBuffer, L"ACPI\\ATK0100"))
{
result = 1;
break;
}
if(!wcscmp(PropertyBuffer, L"ACPI\\PNP0C14\\ATK"))
{
result = 2;
break;
}
}
}
}
++n;
}
SetupDiDestroyDeviceInfoList(devinfo);
return result;
}
AsusTUFLaptopController::AsusTUFLaptopController()
{
/*---------------------------------------------------*\
| Call AsWMI_Open at least once during initialization.|
| Since it's part of the firmware it's guaranteed to |
| always be there. This is necessary to issue further |
| commands. |
\*---------------------------------------------------*/
AsWMI_Open();
hDevice = CreateFileW(L"\\\\.\\ATKACPI", 0xC0000000, 3u, 0, 3u, 0, 0);
}
AsusTUFLaptopController* AsusTUFLaptopController::checkAndCreate()
{
// This might cause issues when coInitialize() is used in multiple places
HRESULT init = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if ( init < 0 && init != 0x80010106 )
return 0;
coInitialized = 1;
int type = checkWMIType();
if ( type == 2 )
{
AsusTUFLaptopController* controller = new AsusTUFLaptopController();
if(controller->hDevice != HANDLE(-1))
{
return controller;
}
delete controller;
}
return nullptr;
}
AsusTUFLaptopController::~AsusTUFLaptopController()
{
if ( hDevice && hDevice != HANDLE(-1) )
{
CloseHandle(hDevice);
hDevice = 0;
}
// This might cause issues when coInitialize() is used in multiple places
if ( coInitialized )
{
CoUninitialize();
coInitialized = 0;
}
}
void AsusTUFLaptopController::SetMode(unsigned char red,
bool AsusTUFLaptopController::deviceIoControlWrapper(const void *dataIn, int commandIndex, int dataSizeIn, void *dataOut, int *dataSizeOut)
{
size_t BytesReturned;
const int bufsize = 1024;
char outBuffer[bufsize];
LPDWORD inBuffer = LPDWORD(malloc(dataSizeIn + 8));
inBuffer[0] = commandIndex;
inBuffer[1] = dataSizeIn;
memmove(inBuffer + 2, dataIn, dataSizeIn);
memset(outBuffer, 0, bufsize);
BytesReturned = 0;
bool result = DeviceIoControl(
hDevice,
0x22240Cu,
inBuffer,
dataSizeIn + 8,
outBuffer,
bufsize,
LPDWORD(&BytesReturned),
0);
if ( result )
{
if ( *dataSizeOut < BytesReturned )
{
BytesReturned = *dataSizeOut;
}
memmove(dataOut, outBuffer, BytesReturned);
}
free(inBuffer);
return result;
}
bool AsusTUFLaptopController::deviceControl(int a1, int a2)
{
if ( hDevice && hDevice != HANDLE(-1) )
{
int data[2];
data[0] = a1;
data[1] = a2;
int result;
int outBufSize = 4;
if ( deviceIoControlWrapper(&data, 1398162756, 8, &result, &outBufSize) )
{
if(outBufSize < 4)
{
result = 0;
}
if ( result == 1 )
{
return 1;
}
}
}
return 0;
}
bool AsusTUFLaptopController::deviceControl(int a1, int a2, int a3)
{
unsigned int data[3];
data[0] = a1;
data[1] = a2;
data[2] = a3;
int outBuf;
int outBufSize = 4;
if ( hDevice && hDevice != HANDLE(-1) )
{
if ( deviceIoControlWrapper(data, 0x53564544, 12, &outBuf, &outBufSize) )
{
if(outBufSize < 4)
{
outBuf = 0;
}
if ( outBuf == 1 )
{
return 1;
}
}
}
return 0;
}
bool AsusTUFLaptopController::getStatus(int a1, int *out)
{
int status;
int statusSize = 4;
if ( !hDevice || hDevice == HANDLE(-1) || (!deviceIoControlWrapper(&a1, 1398035268, 4, &status, &statusSize)) )
{
return 0;
}
if(statusSize < 4)
{
status = 0;
}
*out = status;
return 1;
}
bool AsusTUFLaptopController::getStatusExtended(int a1, int a2, int *status1, int *status2, int* status3)
{
int commandData[2];
commandData[0] = a1;
commandData[1] = a2;
int statusBuffer[3];
int statusSize = 12;
if ( hDevice && hDevice != HANDLE(-1)
&& deviceIoControlWrapper(commandData, 1398035268, 8, statusBuffer, &statusSize) )
{
*status1 = statusBuffer[0];
*status2 = statusBuffer[1];
*status3 = statusBuffer[2];
return 1;
}
else
{
return 0;
}
}
void AsusTUFLaptopController::setMode(unsigned char red,
unsigned char green,
unsigned char blue,
unsigned char mode,
@ -63,14 +276,14 @@ void AsusTUFLaptopController::SetMode(unsigned char red,
unsigned int high = save_val | (mode<<8) | (red<<16) | (green<<24);
unsigned int low = blue | (speed_val<<8);
AsWMI_NB_DeviceControl_2arg(ASUS_WMI_DEVID_TUF_RGB_MODE, high, low);
deviceControl(ASUS_WMI_DEVID_TUF_RGB_MODE, high, low);
}
unsigned char AsusTUFLaptopController::GetBrightness()
unsigned char AsusTUFLaptopController::getBrightness()
{
int backlight_state = 0;
AsWMI_NB_GetDeviceStatus(ASUS_WMI_DEVID_KBD_BACKLIGHT, &backlight_state);
getStatus(ASUS_WMI_DEVID_KBD_BACKLIGHT, &backlight_state);
/*----------------------------------------------*\
| Only lowest two bits indicate brightness level |
@ -78,19 +291,19 @@ unsigned char AsusTUFLaptopController::GetBrightness()
return backlight_state & 0x7F;
}
void AsusTUFLaptopController::SetBrightness(unsigned char brightness)
void AsusTUFLaptopController::setBrightness(unsigned char brightness)
{
/*-----------------------------------------------------*\
| Only calls in this format persistently set brightness |
\*-----------------------------------------------------*/
int ctrl_param = 0x80 | (brightness & 0x7F);
AsWMI_NB_DeviceControl(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param);
deviceControl(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param);
}
/*-----------------------------------------------------------*\
| These settings will not persist a reboot unless save is set |
\*-----------------------------------------------------------*/
void AsusTUFLaptopController::SetPowerState(bool boot,
void AsusTUFLaptopController::setPowerState(bool boot,
bool awake,
bool sleep,
bool shutdown,
@ -105,7 +318,13 @@ void AsusTUFLaptopController::SetPowerState(bool boot,
if (save) state = state | ASUS_WMI_KEYBOARD_POWER_SAVE;
AsWMI_NB_DeviceControl(ASUS_WMI_DEVID_TUF_RGB_STATE, state);
deviceControl(ASUS_WMI_DEVID_TUF_RGB_STATE, state);
}
#endif // _WIN32
void AsusTUFLaptopController::setFanMode(int mode)
{
deviceControl(ASUS_WMI_DEVID_FAN_BOOST_MODE, mode);
}
#endif

View file

@ -1,12 +1,17 @@
#ifdef _WIN32
#ifndef ASUSTUFLAPTOPCONTROLLER_H
#define ASUSTUFLAPTOPCONTROLLER_H
#ifdef _WIN32
#include <windows.h>
#define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
#define ASUS_WMI_DEVID_TUF_RGB_MODE 0x00100056
#define ASUS_WMI_DEVID_TUF_RGB_STATE 0x00100057
#define ASUS_WMI_DEVID_FAN_BOOST_MODE 0x00110018
#define ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY 0x00120075
#define ASUS_WMI_KEYBOARD_SPEED_SLOW 0xE1
#define ASUS_WMI_KEYBOARD_SPEED_NORMAL 0xEB
#define ASUS_WMI_KEYBOARD_SPEED_FAST 0xF5
@ -32,29 +37,46 @@
#define ASUS_WMI_KEYBOARD_POWER_SAVE 0x01<<8
#define ASUS_WMI_FAN_SPEED_NORMAL 0
#define ASUS_WMI_FAN_SPEED_TURBO 1
#define ASUS_WMI_FAN_SPEED_SILENT 2
class AsusTUFLaptopController
{
public:
private:
HANDLE hDevice;
static int checkWMIType();
AsusTUFLaptopController();
bool deviceIoControlWrapper(const void *dataIn, int commandIndex, int dataSizeIn, void *dataOut, int *dataSizeOut);
bool deviceControl(int a1, int a2);
bool deviceControl(int a1, int a2, int a3);
bool getStatus(int a1, int *out);
bool getStatusExtended(int a1, int a2, int *status1, int *status2, int* status3);
public:
static AsusTUFLaptopController * checkAndCreate();
~AsusTUFLaptopController();
void SetMode(unsigned char red,
void setMode(unsigned char red,
unsigned char green,
unsigned char blue,
unsigned char mode,
unsigned char speed,
bool save);
unsigned char GetBrightness();
void SetBrightness(unsigned char brightness);
unsigned char getBrightness();
void setBrightness(unsigned char brightness);
void SetPowerState(bool boot,
void setPowerState(bool boot,
bool awake,
bool sleep,
bool shutdown,
bool save);
void setFanMode(int mode);
};
#endif // ASUSTUFLAPTOPCONTROLLER_H
#endif
#endif // _WIN32
#endif // ASUSTUFLAPTOPCONTROLLER_H

View file

@ -3,7 +3,6 @@
#include "AsusTUFLaptopController.h"
#include "RGBController_AsusTUFLaptopWMI.h"
#include "acpiwmi.h"
#include "Detector.h"
#include "wmi.h"
#include <string>
@ -36,16 +35,15 @@ static void DetectAsusTUFLaptopWMIControllers()
return;
}
if(AsWMI_Open())
AsusTUFLaptopController* controller = AsusTUFLaptopController::checkAndCreate();
if(controller)
{
AsusTUFLaptopController* asus_wmi_controller = new AsusTUFLaptopController();
RGBController* new_controller = new RGBController_AsusTUFLaptopWMI(asus_wmi_controller);
RGBController* new_controller = new RGBController_AsusTUFLaptopWMI(controller);
ResourceManager::get()->RegisterRGBController(new_controller);
// Success! No more if's
}
} /* DetectFaustusControllers() */
} /* DetectAsusTUFLaptopWMIControllers() */
REGISTER_DETECTOR("TUF Laptop WMI", DetectAsusTUFLaptopWMIControllers);
REGISTER_DETECTOR("ASUS TUF Laptop", DetectAsusTUFLaptopWMIControllers);
#endif

View file

@ -133,7 +133,7 @@ void RGBController_AsusTUFLaptopWMI::ControllerSetMode(bool save)
speed = (unsigned char)modes[(unsigned int)active_mode].speed;
}
controller->SetMode(red, green, blue, mode, speed, save);
controller->setMode(red, green, blue, mode, speed, save);
}
void RGBController_AsusTUFLaptopWMI::DeviceUpdateLEDs()
@ -155,7 +155,7 @@ void RGBController_AsusTUFLaptopWMI::DeviceUpdateMode()
{
if (modes[(unsigned int)active_mode].flags & MODE_FLAG_HAS_BRIGHTNESS)
{
controller->SetBrightness((unsigned char)modes[(unsigned int)active_mode].brightness);
controller->setBrightness((unsigned char)modes[(unsigned int)active_mode].brightness);
}
ControllerSetMode(false);
}
@ -164,7 +164,7 @@ void RGBController_AsusTUFLaptopWMI::ReadConfiguration()
{
if (modes[(unsigned int)active_mode].flags & MODE_FLAG_HAS_BRIGHTNESS)
{
modes[(unsigned int)active_mode].brightness = controller->GetBrightness();
modes[(unsigned int)active_mode].brightness = controller->getBrightness();
}
}

View file

@ -1625,7 +1625,6 @@ win32:SOURCES +=
i2c_smbus/i2c_smbus_piix4.cpp \
serial_port/find_usb_serial_port_win.cpp \
wmi/wmi.cpp \
wmi/acpiwmi.cpp \
AutoStart/AutoStart-Windows.cpp \
Controllers/AsusTUFLaptopController/AsusTUFLaptopController.cpp \
Controllers/AsusTUFLaptopController/AsusTUFLaptopWMIDetect.cpp \
@ -1650,7 +1649,6 @@ win32:HEADERS +=
i2c_smbus/i2c_smbus_nvapi.h \
i2c_smbus/i2c_smbus_piix4.h \
wmi/wmi.h \
wmi/acpiwmi.h \
AutoStart/AutoStart-Windows.h \
Controllers/AsusTUFLaptopController/AsusTUFLaptopController.h \
Controllers/AsusTUFLaptopController/RGBController_AsusTUFLaptopWMI.h \

View file

@ -1,278 +0,0 @@
#ifdef _WIN32
#include <windows.h>
#include <Objbase.h>
#include <setupapi.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <cstdint>
#include <cstring>
#include <cstdio>
static bool IsWOW64 = false;
static HANDLE hDevice = 0;
static bool WMI_Notebook = 0;
static bool coInitialized = 0;
static GUID CLSID_GUID_DEVCLASS_SYSTEM = { 0x4D36E97D, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } };
static int CheckWMIType()
{
int n;
signed int v3;
int v4;
signed int v5;
int v6;
int result;
struct _SP_DEVINFO_DATA DeviceInfoData;
wchar_t PropertyBuffer[260];
HDEVINFO devinfo = SetupDiGetClassDevsW(&CLSID_GUID_DEVCLASS_SYSTEM, 0, 0, 2u);
if ( devinfo == HDEVINFO(-1) )
{
return 0;
}
n = 0;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
v3 = 1;
result = 0;
while ( SetupDiEnumDeviceInfo(devinfo, n, &DeviceInfoData) ) // Invalid buffer
{
if ( !SetupDiGetDeviceRegistryPropertyW(devinfo, &DeviceInfoData, 0x16u, 0, PBYTE(PropertyBuffer), 0x208u, 0) )
{
v5 = 0;
v4 = wcscmp(PropertyBuffer, L"ACPI");
if ( v4 )
{
v4 = -(v4 < 0) | 1;
v5 = 0;
}
else
{
v5 = 1;
}
}
if ( v5 )
{
memset(PropertyBuffer, 0, 0x208u);
if ( SetupDiGetDeviceInstanceIdW(devinfo, &DeviceInfoData, PropertyBuffer, 0x104u, 0) )
{
_wcsupr_s(PropertyBuffer, 0x104u);
if ( wcsstr(PropertyBuffer, L"ACPI\\ATK0100") )
{
result = 1;
break;
}
v6 = wcscmp(PropertyBuffer, L"ACPI\\PNP0C14\\ATK");
if ( v6 )
v6 = -(v6 < 0) | 1;
if ( !v6 )
{
result = 2;
v3 = 0;
}
}
}
++n;
if ( !v3 )
break;
}
SetupDiDestroyDeviceInfoList(devinfo);
return result;
}
static bool OpenNotebook()
{
HMODULE kernelHandler; // eax
void (__stdcall *IsWow64Process)(HANDLE, int *); // esi
int wow64; // [esp+4h] [ebp-4h]
wow64 = 0;
kernelHandler = GetModuleHandleW(L"kernel32");
IsWow64Process = (void (__stdcall *)(HANDLE, int *))GetProcAddress(kernelHandler, "IsWow64Process");
if ( IsWow64Process )
{
int iswow;
IsWow64Process(GetCurrentProcess(), &iswow);
IsWOW64 = iswow;
}
hDevice = CreateFileW(L"\\\\.\\ATKACPI", 0xC0000000, 3u, 0, 3u, 0, 0);
return (hDevice != HANDLE(-1));
}
bool AsWMI_Open()
{
HRESULT init = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if ( init < 0 && init != -2147417850 )
return 0;
coInitialized = 1;
int type = CheckWMIType();
if ( type == 2 )
{
OpenNotebook();
WMI_Notebook = 1;
return 1;
}
return 0;
}
void AsWMI_Close()
{
if ( WMI_Notebook )
{
if ( hDevice && hDevice != (HANDLE)-1 )
{
CloseHandle(hDevice);
hDevice = 0;
}
}
if ( coInitialized )
{
CoUninitialize();
coInitialized = 0;
}
}
static bool DeviceIoControlWrapper(const void *dataIn, int commandIndex, int dataSizeIn, void *dataOut, int *dataSizeOut)
{
size_t BytesReturned;
char OutBuffer[1024];
LPDWORD inBuffer = (LPDWORD)(malloc(dataSizeIn + 8));
inBuffer[0] = commandIndex;
inBuffer[1] = dataSizeIn;
memmove(inBuffer + 2, dataIn, dataSizeIn);
memset(OutBuffer, 0, 0x400u);
BytesReturned = 0;
bool result = DeviceIoControl(
hDevice,
0x22240Cu,
inBuffer,
dataSizeIn + 8,
OutBuffer,
0x400u,
LPDWORD(&BytesReturned),
0);
if ( result )
{
if ( *dataSizeOut < BytesReturned )
BytesReturned = *dataSizeOut;
memmove(dataOut, OutBuffer, BytesReturned);
}
free(inBuffer);
return result;
}
bool AsWMI_NB_DeviceControl(int a1, int a2)
{
unsigned int data[2];
if ( WMI_Notebook )
{
if ( hDevice )
{
if ( hDevice != (HANDLE)-1 )
{
data[0] = a1;
data[1] = a2;
int result;
int outBufSize = 4;
if ( DeviceIoControlWrapper(&data, 1398162756, 8, &result, &outBufSize) )
{
if(outBufSize < 4)
{
result = 0;
}
if ( result == 1 )
{
return 1;
}
}
}
}
}
return 0;
}
bool AsWMI_NB_DeviceControl_2arg(int a1, int a2, int a3)
{
unsigned int data[3];
data[0] = a1;
data[1] = a2;
data[2] = a3;
int outBuf;
int outBufSize = 4;
if ( WMI_Notebook )
{
if ( hDevice )
{
if ( hDevice != (HANDLE)-1 )
{
if ( DeviceIoControlWrapper(data, 0x53564544, 12, &outBuf, &outBufSize) )
{
if(outBufSize < 4)
{
outBuf = 0;
}
if ( outBuf == 1 )
{
return 1;
}
}
}
}
}
return 0;
}
char AsWMI_NB_GetDeviceStatus(int a1, int *out)
{
int status;
int statusSize = 4;
if ( !WMI_Notebook )
{
return 0;
}
if ( !hDevice || hDevice == HANDLE(-1) || (!DeviceIoControlWrapper(&a1, 1398035268, 4, &status, &statusSize)) )
{
return 0;
}
if(statusSize < 4)
{
status = 0;
}
*out = status;
return 1;
}
bool AsWMI_NB_GetDeviceStatus_MoreBYTE(int a1, int a2, int *status1, int *status2, int* status3)
{
if ( !WMI_Notebook )
{
return 0;
}
int commandData[2];
commandData[0] = a1;
commandData[1] = a2;
int statusBuffer[3];
int statusSize = 12;
if ( hDevice
&& hDevice != HANDLE(-1)
&& DeviceIoControlWrapper(commandData, 1398035268, 8, statusBuffer, &statusSize) )
{
*status1 = statusBuffer[0];
*status2 = statusBuffer[1];
*status3 = statusBuffer[2];
return 1;
}
else
{
return 0;
}
}
#endif

View file

@ -1,17 +0,0 @@
#ifndef ACPIWMI_H
#define ACPIWMI_H
#ifdef _WIN32
bool AsWMI_CheckSupport();
bool AsWMI_Open();
void AsWMI_Close();
bool AsWMI_NB_RegisterEvent();
bool AsWMI_NB_DeviceControl(int a1, int a2);
bool AsWMI_NB_DeviceControl_2arg(int a1, int a2, int a3);
char AsWMI_NB_GetDeviceStatus(int a1, int *out);
bool AsWMI_NB_GetDeviceStatus_MoreBYTE(int a1, int a2, int *status1, int *status2, int* status3);
#endif
#endif // ACPIWMI_H