-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaswmihumanapi.cpp
264 lines (223 loc) · 11.4 KB
/
aswmihumanapi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/******************************************************************************
* File: aswmihumanapi.cpp
* Description: AsWMI human api for asus tuf gaming laptop.
* Created: 21 sep 2020
* Copyright: (C) 2020 Valeriy Zbiranyk
* Authors: programing - Valeriy Zbiranyk
* reverse engineering - Valeriy Zbiranyk, Mykola Vorobiov
* Email: culikov.valeriy@gmail.com, flingseal@gmail.com
******************************************************************************/
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <regex>
#include <windows.h>
#include "aswmihumanapi.h"
const int redBits = 0xff0000;
const int greenBits = 0x00ff00;
const int blueBits = 0x0000ff;
typedef int (*AsWMI_NB_DeviceControl_2arg)(int, int, int);
typedef int (*AsWMI_NB_DeviceControl)(int, int);
enum DevControlFunckType
{
ASWMI_NB_DEVCONTROL_2ARG = 0x100056,
ASWMI_NB_DEVCONTROL = 0x100057
};
std::string getCurrentDirectory()
{
char buffer[MAX_PATH];
GetModuleFileNameA(NULL, buffer, MAX_PATH);
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
return std::string(buffer).substr(0, pos);
}
int executeAsWMI(DevControlFunckType devControlFunckType,
int RGDiodeAndEType,
int BDiodeAndTemp,
int backlightPowerSettings)
{
std::string libraryName = "ACPIWMI.dll";
std::string libraryPath = getCurrentDirectory() + '\\' + libraryName;
std::ifstream libraryFile(libraryPath.c_str());
if(!libraryFile.good())
{
fprintf(stderr, "Can`t find file \'%s\'", libraryName.c_str());
return -1;
}
libraryFile.close();
HMODULE ACPIWMI = LoadLibraryA(libraryName.c_str());
FARPROC AsWMI_Open = GetProcAddress(ACPIWMI, "AsWMI_Open");
FARPROC AsWMI_Close = GetProcAddress(ACPIWMI, "AsWMI_Close");
AsWMI_NB_DeviceControl_2arg AsWMI_DeviceControlBacklightEffectTypeTempoColor = (AsWMI_NB_DeviceControl_2arg)GetProcAddress(ACPIWMI, "AsWMI_NB_DeviceControl_2arg");
AsWMI_NB_DeviceControl AsWMI_DeviceControlBacklightPowerSettings = (AsWMI_NB_DeviceControl)GetProcAddress(ACPIWMI, "AsWMI_NB_DeviceControl");
AsWMI_Open();
switch(devControlFunckType)
{
case ASWMI_NB_DEVCONTROL:
AsWMI_DeviceControlBacklightPowerSettings(ASWMI_NB_DEVCONTROL, backlightPowerSettings);
break;
case ASWMI_NB_DEVCONTROL_2ARG:
AsWMI_DeviceControlBacklightEffectTypeTempoColor(ASWMI_NB_DEVCONTROL_2ARG, RGDiodeAndEType, BDiodeAndTemp);
break;
default:
fprintf(stderr, "Wrong AsWMI_NB_DeviceControl function type!");
return -1;
break;
}
AsWMI_Close();
return 0;
}
unsigned __int8 correctTempo(TUFLaptopRGBBacklightControl::EffectType effectType,
TUFLaptopRGBBacklightControl::Tempo tempo)
{
if(effectType == TUFLaptopRGBBacklightControl::STATIC || effectType == TUFLaptopRGBBacklightControl::STROBING)
{
tempo = TUFLaptopRGBBacklightControl::NONE;
}
if((effectType == TUFLaptopRGBBacklightControl::COLOR_CYCLE || effectType == TUFLaptopRGBBacklightControl::BREATHING)
&& tempo == TUFLaptopRGBBacklightControl::NONE)
{
tempo = TUFLaptopRGBBacklightControl::SLOW;
}
return tempo;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklight(EffectType effectType,
Tempo tempo,
unsigned __int8 brightnessRedDiode,
unsigned __int8 brightnessGreenDiode,
unsigned __int8 brightnessBueDiode)
{
unsigned __int8 correctedTempo = correctTempo(effectType, tempo);
int RGDiodeAndEType = (0x00010000 * brightnessRedDiode) + (0x01000000 * brightnessGreenDiode) + (0x00000100 * effectType) + 0xb3;
int BDiodeAndTemp = brightnessBueDiode + (0x0100 * correctedTempo);
return executeAsWMI(ASWMI_NB_DEVCONTROL_2ARG,
RGDiodeAndEType,
BDiodeAndTemp, 0);
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklight(EffectType effectType,
Tempo tempo,
unsigned int colorHex)
{
unsigned __int8 brightnessRedDiode = (colorHex & redBits) >> 16;
unsigned __int8 brightnessGreenDiode = (colorHex & greenBits) >> 8;
unsigned __int8 brightnessBueDiode = colorHex & blueBits;
return setLaptopRGBBacklight(effectType, tempo, brightnessRedDiode, brightnessGreenDiode, brightnessBueDiode);
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklight(EffectType effectType,
Tempo tempo,
std::string HTMLColor)
{
std::regex colorReg("#[a-f0-9A-F]{6}");
std::smatch parts;
if(!std::regex_search(HTMLColor, colorReg))
{
fprintf(stderr, "Wrong HTML color string format! Use \'#FF00FF\' for example, instead of %s", HTMLColor.c_str());
return -1;
}
HTMLColor.erase(std::remove(HTMLColor.begin(), HTMLColor.end(), '#'), HTMLColor.end());
unsigned int colorHex = std::stoi(HTMLColor, 0, 16);
return setLaptopRGBBacklight(effectType, tempo, colorHex);
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightPowerSettings(unsigned __int8 backlightPowerSettings)
{
int backlightSettings = (0x10000 * backlightPowerSettings) + 0xbd;
return executeAsWMI(ASWMI_NB_DEVCONTROL, 0, 0, backlightSettings);
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightPowerSettings(bool onBootUp,
bool onAwake,
bool onSleep,
bool onShutDown)
{
unsigned __int8 backlightPowerSettings = (onBootUp * BOOT_UP) + (onAwake * AWAKE) + (onSleep * SLEEP) + (onShutDown * SHUT_DOWN);
return setLaptopRGBBacklightPowerSettings(backlightPowerSettings);
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
unsigned int colorHex,
unsigned char backlightPowerSettings)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, colorHex);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(backlightPowerSettings);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
std::string HTMLColor,
unsigned char backlightPowerSettings)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, HTMLColor);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(backlightPowerSettings);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
unsigned char brightnessRedDiode,
unsigned char brightnessGreenDiode,
unsigned char brightnessBueDiode,
unsigned char backlightPowerSettings)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, brightnessRedDiode, brightnessGreenDiode, brightnessBueDiode);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(backlightPowerSettings);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
unsigned int colorHex,
bool onBootUp,
bool onAwake,
bool onSleep,
bool onShutDown)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, colorHex);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(onBootUp, onAwake, onSleep, onShutDown);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
std::string HTMLColor,
bool onBootUp,
bool onAwake,
bool onSleep,
bool onShutDown)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, HTMLColor);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(onBootUp, onAwake, onSleep, onShutDown);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}
int TUFLaptopRGBBacklightControl::setLaptopRGBBacklightSettings(EffectType effectType,
Tempo tempo,
unsigned char brightnessRedDiode,
unsigned char brightnessGreenDiode,
unsigned char brightnessBueDiode,
bool onBootUp,
bool onAwake,
bool onSleep,
bool onShutDown)
{
int backlightColorSuccess = setLaptopRGBBacklight(effectType, tempo, brightnessRedDiode, brightnessGreenDiode, brightnessBueDiode);
int backlightPowerSuccess = setLaptopRGBBacklightPowerSettings(onBootUp, onAwake, onSleep, onShutDown);
if((backlightColorSuccess == -1) || (backlightPowerSuccess == -1))
{
return -1;
}
return 0;
}