-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.cpp
More file actions
277 lines (236 loc) · 8.27 KB
/
controller.cpp
File metadata and controls
277 lines (236 loc) · 8.27 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "controller.h"
bool AsusDLL::failed(const std::wstring reason = _ts(L"Failed"), bool shown = true) {
if (shown) _dErr(reason);
return false;
}
bool AsusDLL::success(const std::wstring reason = _ts(L"Success"), bool shown = false)
{
if (shown) _dInfo(reason);
return true;
}
static bool service_running(std::wstring token)
{
SC_HANDLE scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
if (scm == NULL)
return false;
LPCWSTR lpServiceName = (LPCWSTR)token.data();
SC_HANDLE hService = OpenService(scm, lpServiceName, GENERIC_READ);
if (hService == NULL)
{
CloseServiceHandle(scm);
return false;
}
SERVICE_STATUS status;
LPSERVICE_STATUS pstatus = &status;
if (QueryServiceStatus(hService, pstatus) == 0)
{
CloseServiceHandle(hService);
CloseServiceHandle(scm);
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(scm);
return (status.dwCurrentState == SERVICE_RUNNING) ? (true) : (false);
}
static bool service_checker() {
if (service_running(L"ASUSSystemAnalysis")) return TRUE;
_dErr(_ts(L"[Service Control] Service not running yet!!"));
bool success = false;
if (IsUserAnAdmin()) {
// If user is admin, try to start the service using OpenSCManager
SC_HANDLE scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
if (scm != NULL) {
SC_HANDLE hService = OpenService(scm, L"ASUSSystemAnalysis", SERVICE_CHANGE_CONFIG | SERVICE_START);
if (hService != NULL) {
bool stService = StartService(hService, 0, NULL);
if (!stService) {
ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
_dInfo(L"[Service Control] Changed to Manual");
stService = StartService(hService, 0, NULL);
if (stService) {
MessageBox(NULL,
_ts(L"ASUSSystemAnalysis Service *was* not running.\nSince you started the program with Administrator rights, the service was started by the program.\nFrom now on, it will automatically be started when you use this program.").c_str(),
_ts(L"Service notice!").c_str(), MB_ICONINFORMATION | MB_OK);
}
}
success = stService;
CloseServiceHandle(hService);
}
CloseServiceHandle(scm);
}
if (!success) {
MessageBox(NULL, _ts(L"ASUSSystemAnalysis Service is not running!\nPlease start the service manually!").c_str(), _ts(L"Error").c_str(), MB_ICONERROR | MB_OK);
}
}
else {
MessageBox(NULL, _ts(L"ASUSSystemAnalysis Service is not running!\nPlease start the service first, or run this program again with Administrator rights.").c_str(), _ts(L"Error").c_str(), MB_ICONERROR | MB_OK);
}
return success;
}
AsusDLL::AsusDLL() {
_dInfo(L"Constructor called");
if (asus_dll == NULL) {
failed(_ts(L"DLL not loaded yet!!"));
MessageBox(NULL, _ts(L"Cannot initiate DLL!\nPlease recheck if AsusWinIO64.dll is in the same folder with this executable!").c_str(), _ts(L"Error").c_str(), MB_ICONERROR | MB_OK);
error_occured = true;
return;
}
if (!service_checker()) {
_dErr(L"[Service Control] Service failed to start!");
error_occured = true;
return;
}
typedef void (*Cons)();
Cons initialize = (Cons)GetProcAddress(asus_dll, "InitializeWinIo");
initialize();
init_status = true;
_dInfo(L"Initialized!");
}
AsusDLL::~AsusDLL() noexcept(false) {
_dInfo(L"Destructor called");
if (!init_status) {
_dErr(L"Already killed!");
return;
}
AsusDLL::set_fan_test_mode(0x00);
typedef void (*Dest)();
Dest shutdown = (Dest)GetProcAddress(asus_dll, "ShutdownWinIo");
shutdown();
FreeLibrary(asus_dll);
init_status = false;
_dInfo(L"Killed!");
}
int AsusDLL::get_fan_count()
{
if (!init_status) {
_dErr(L"Get fan count failed");
return -1;
}
if (fan_count != -1) return fan_count;
typedef int (*FanCount)();
FanCount fan_count = (FanCount)GetProcAddress(asus_dll, "HealthyTable_FanCounts");
int fnc = (int)fan_count();
if (fnc < 1) {
MessageBox(NULL, _ts(L"Cannot find any fan!").c_str(), _ts(L"Error").c_str(), MB_ICONERROR | MB_OK);
error_occured = true;
}
return fnc;
}
bool AsusDLL::set_fan_test_mode(char mode)
{
if (!init_status) return failed();
if (current_fan_test_mode == mode) return success();
if (mode == 0x00) current_fan_percent = 0;
int fan_cnt = AsusDLL::get_fan_count();
typedef void (*FanTestMode)(char value);
typedef void (*FanIdx)(byte fanIndex);
for (byte fanIdx = 0; fanIdx < fan_cnt; ++fanIdx) {
FanIdx set_fan_idx = (FanIdx)GetProcAddress(asus_dll, "HealthyTable_SetFanIndex");
set_fan_idx(fanIdx);
FanTestMode set_fan_test_mode = (FanTestMode)GetProcAddress(asus_dll, "HealthyTable_SetFanTestMode");
set_fan_test_mode(mode);
}
current_fan_test_mode = mode;
return success(_ts(L"[ASUS Service] Fan test mode changed to ") + _ts(mode), true);
}
bool AsusDLL::set_fan_pwm_duty(byte value)
{
if (!init_status) return failed();
typedef void (*FanPWM)(byte value);
FanPWM set_fan_pwm = (FanPWM)GetProcAddress(asus_dll, "HealthyTable_SetFanPwmDuty");
set_fan_pwm(value);
return success(L"", false);
}
bool AsusDLL::set_fan_speed_idx(byte value, byte fanIdx = 0)
{
if (!init_status) return failed(_ts(L"Set fan speed #") + _ts(fanIdx) + _ts(L" failed"));
if (current_fan_test_mode == 0x00) {
return success(_ts(L"Fan test mode is not enabled, skip setting fan speed #") + _ts(fanIdx), false);
}
typedef void (*FanIdx)(byte fanIndex);
FanIdx set_fan_idx = (FanIdx)GetProcAddress(asus_dll, "HealthyTable_SetFanIndex");
set_fan_idx(fanIdx);
AsusDLL::set_fan_pwm_duty(value);
return success(_ts(L"Set fan speed #") + _ts(fanIdx) + _ts(L" to ") + _ts(value), false);
}
bool AsusDLL::set_fan_speed(float percent)
{
if (!init_status) return failed();
// wont change if the delta is too small
if (abs(AsusDLL::current_fan_percent - percent) <= 1) return success();
// set to 0 if the percent is too small
if (percent < 10) percent = 0;
AsusDLL::current_fan_percent = percent;
byte value = max((byte)1, (byte)(AsusDLL::current_fan_percent / 100.0f * 255));
int fan_cnt = AsusDLL::get_fan_count();
for (byte fanIdx = 0; fanIdx < fan_cnt; ++fanIdx) {
if (!AsusDLL::set_fan_speed_idx(value, fanIdx)) return failed();
}
return success(_ts(L"Set fan speed to ") + _ts(value), true);
}
int AsusDLL::get_fan_speed_idx(byte fanIdx = 0)
{
if (!init_status) {
_dErr(_ts(L"Get fan speed #") + _ts(fanIdx) + _ts(L" failed"));
return -1;
}
typedef void (*FanIdx)(byte fanIndex);
FanIdx set_fan_idx = (FanIdx)GetProcAddress(asus_dll, "HealthyTable_SetFanIndex");
set_fan_idx(fanIdx);
typedef int (*FanSpd)();
FanSpd fan_speed = (FanSpd)GetProcAddress(asus_dll, "HealthyTable_FanRPM");
return (int)fan_speed();
}
static bool update_needed(ULONGLONG& last_update, SYSTEMTIME& st) {
GetSystemTime(&st);
inipp::Ini<wchar_t> settings;
read_settings(settings);
int update_interval = 2000;
inipp::extract(settings.sections[L"General"][L"UpdateInterval"], update_interval);
settings.clear();
return (convert_to_ull(st) - last_update > update_interval);
}
std::vector<int> AsusDLL::get_fan_speed()
{
SYSTEMTIME st;
if (!update_needed(last_update_fan_speed, st)) return fan_speed_list;
int fan_cnt = AsusDLL::get_fan_count();
fan_speed_list.clear();
for (byte fanIdx = 0; fanIdx < fan_cnt; ++fanIdx) {
int val = AsusDLL::get_fan_speed_idx(fanIdx);
if (val == -1) break;
fan_speed_list.push_back(val);
}
last_update_fan_speed = convert_to_ull(st);
return fan_speed_list;
}
ULONG AsusDLL::get_thermal()
{
SYSTEMTIME st;
if (!update_needed(last_update_thermal, st)) return current_combined_thermal;
if (!init_status) {
_dErr(L"Get thermal failed");
return -1;
}
typedef int (*TherCPU)();
TherCPU thermal_cpu = (TherCPU)GetProcAddress(asus_dll, "Thermal_Read_Cpu_Temperature");
typedef int (*TherGPU)();
TherGPU thermal_gpu = (TherGPU)GetProcAddress(asus_dll, "Thermal_Read_GpuTS1L_Temperature");
current_cpu_thermal = thermal_cpu();
current_gpu_thermal = thermal_gpu();
current_combined_thermal = max(current_cpu_thermal, current_gpu_thermal);
last_update_thermal = convert_to_ull(st);
return current_combined_thermal;
}
PWRStatus AsusDLL::get_power_mode()
{
if (!init_status) {
_dErr(L"Get power mode failed");
return Error;
}
typedef byte(*PowerMode)();
PowerMode power_mode = (PowerMode)GetProcAddress(asus_dll, "Power_Read_ACDCMode");
return (PWRStatus)power_mode();
}