-
Notifications
You must be signed in to change notification settings - Fork 2
/
FanControl++.cpp
641 lines (541 loc) · 20.5 KB
/
FanControl++.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
// FanControl++.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "FanControl++.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
AsusDLL asus_control;
inipp::Ini<wchar_t> settings;
NOTIFYICONDATAW nid = {};
bool toggle_change_fan_speed = true;
int current_mode = ID_POPUP_BALANCED;
bool toggle_adaptive_mode = false;
int current_ac_mode = ID_POPUP_BALANCED;
int current_dc_mode = ID_POPUP_ECO;
PWRStatus current_power_mode = PWRStatus::Error;
PWRStatus last_power_mode = PWRStatus::Error;
bool toggle_smooth_temp = true;
int update_interval = 2000;
bool startup = 0;
// dirty trick
bool thread_term = false;
bool is_about_opened = false;
HWND current_settings_hwnd = 0;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
HWND InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam, LONG_PTR lpdata);
INT_PTR CALLBACK Settings(HWND, UINT, WPARAM, LPARAM);
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex{};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FANCONTROL));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
{
// check for single instance
HANDLE hMutex = CreateMutexW(NULL, FALSE, L"FanControl++-{10547806-8CBC-4BFC-86D2-4A26475BFB95}");
if (GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED) {
MessageBox(NULL, L"FanControl++ is already running!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return NULL;
}
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowExW(
WS_EX_CLIENTEDGE,
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd) {
MessageBox(NULL, L"Window creation failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
_dInfo(L"Window initialized!");
return hWnd;
}
static BOOL SysRegisterRestart(bool is_register) {
HRESULT status;
if (is_register)
{
status = RegisterApplicationRestart(L"-minimized", RESTART_NO_CRASH);
}
else
{
status = UnregisterApplicationRestart();
}
return SUCCEEDED(status);
}
static BOOL ChangeStartupBehavior(BOOL enable) {
HKEY hKey;
LONG lResult;
lResult = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);
if (lResult != ERROR_SUCCESS) {
_dErr(_ts(L"[Startup] Cannot open registry key!"));
MessageBox(NULL, L"Cannot open registry key!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
if (enable) {
wchar_t szPath[MAX_PATH];
GetModuleFileNameW(NULL, szPath, MAX_PATH);
std::wstring tmp(szPath);
tmp = L"\"" + tmp + L"\" -minimized";
wcscpy_s(szPath, tmp.c_str());
lResult = RegSetValueExW(hKey, L"FanControl++", 0, REG_SZ, (LPBYTE)(szPath), sizeof(szPath));
if (lResult != ERROR_SUCCESS) {
_dErr(_ts(L"[Startup] Cannot set registry key!"));
MessageBox(NULL, L"Cannot set registry key!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
RegCloseKey(hKey);
return FALSE;
}
SysRegisterRestart(true);
}
else {
lResult = RegDeleteValueW(hKey, L"FanControl++");
if (lResult != ERROR_SUCCESS && lResult != ERROR_FILE_NOT_FOUND) {
_dErr(_ts(L"[Startup] Cannot delete registry key!"));
MessageBox(NULL, L"Cannot delete registry key!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
RegCloseKey(hKey);
return FALSE;
}
SysRegisterRestart(false);
}
_dInfo(_ts(L"[Startup] Startup behavior changed to ") + _ts(enable));
RegCloseKey(hKey);
return TRUE;
}
static BOOL Cleanup(HWND& hWnd, NOTIFYICONDATAW& nid) {
return Shell_NotifyIconW(NIM_DELETE, &nid);
}
static BOOL MainThread(HWND hWnd) noexcept(false) {
ULONGLONG next_time = GetTickCount64();
while (!thread_term) {
if (asus_control.error_occured) {
_dErr(_ts(L"[Thread] Error occured!"));
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
int temp = asus_control.get_thermal();
update_average_temperature(temp, toggle_smooth_temp);
if (toggle_adaptive_mode) {
current_power_mode = asus_control.get_power_mode();
if (current_power_mode != last_power_mode) {
last_power_mode = current_power_mode;
if (current_power_mode == PWRStatus::AC) current_mode = current_ac_mode;
else if (current_power_mode == PWRStatus::DC) current_mode = current_dc_mode;
settings.sections[L"General"][L"CurrentMode"] = _ts(current_mode);
write_settings(settings);
}
}
if (toggle_change_fan_speed) {
float perc = calc_fan_percent(current_mode);
if (perc < 0) _dErr(_ts(L"[Thread] Invalid fan percent!"));
else asus_control.set_fan_speed(perc);
}
_dInfo(_ts(L"[Thread] Temp: ") + _ts(temp) + _ts(L" | Percent: ") + _ts(trunc(asus_control.current_fan_percent)) + _ts(L" | Mode: ") + _ts(current_mode - ID_POPUP_ECO));
UpdateTray(hWnd, nid, asus_control, current_mode);
if (thread_term) break;
if (current_settings_hwnd) {
std::wstring tool_tip = _ts(L"CPU: ") + _ts(asus_control.current_cpu_thermal) + _ts(L"°C | GPU: ") + _ts(asus_control.current_gpu_thermal) + _ts(L"°C\n")
+ _ts(L"Fan: ") + _ts(asus_control.get_fan_speed()) + _ts(L" RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
SetWindowTextW(GetDlgItem(current_settings_hwnd, IDC_STATIC_INFO), tool_tip.c_str());
}
next_time += update_interval;
LONGLONG sleep_time = next_time - GetTickCount64();
if (sleep_time > 0) {
Sleep(sleep_time); // sleep until next update
}
}
return Cleanup(hWnd, nid);
}
static void ShowAboutMessage(HWND hWnd) {
if (is_about_opened) return;
is_about_opened = true;
TASKDIALOGCONFIG tdc = { 0 };
LPCWSTR str_title = L"About";
WCHAR str_content[512];
std::wstring tmp_str;
std::wstring version = CURRENT_VERSION;
#ifdef _DEBUG
version += L" (Debug), ";
#else
version += L" (Release), ";
#endif
version += (sizeof(void*) == 8 ? L"x64" : L"x86");
version += L" Unicode.";
tmp_str = L"A lightweight fan controller for ASUS laptops.\r\n"
+ _ts(L"Version ") + version + L"\r\n\r\n"
+ _ts(L"(c) 2024 Phạm Nhật Quang (Legend0fHell).\r\nAll Rights Reserved.\r\n\r\n")
+ _ts(L"<a href=\"https://github.com/Legend0fHell\">github.com/Legend0fHell</a>\r\n");
wcscpy_s(str_content, tmp_str.c_str());
tdc.cbSize = sizeof(tdc);
tdc.dwFlags = TDF_ENABLE_HYPERLINKS | TDF_ALLOW_DIALOG_CANCELLATION | TDF_EXPAND_FOOTER_AREA | TDF_SIZE_TO_CONTENT;
tdc.hwndParent = hWnd;
tdc.hInstance = hInst;
tdc.dwCommonButtons = TDCBF_CLOSE_BUTTON;
tdc.pszFooterIcon = TD_INFORMATION_ICON;
tdc.pszWindowTitle = str_title;
tdc.pszMainInstruction = (LPWSTR)IDS_APP_TITLE;
tdc.pszContent = str_content;
tdc.pfCallback = &About;
tdc.lpCallbackData = MAKELONG(0, TRUE); // on top
tdc.pszMainIcon = MAKEINTRESOURCE(IDI_FANCONTROL);
tdc.pszFooter = L"FanControl++ is free software, and licensed under the <a href=\"https://www.gnu.org/licenses/gpl-3.0.en.html\">GNU General Public License 3</a>.\r\n"\
L"Included AsusWinIO64.dll is licensed by (c) ASUSTek COMPUTER INC. and used in compliance with it's EULA.\r\n";
TaskDialogIndirect(&tdc, NULL, NULL, NULL);
is_about_opened = false;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
POINT lpClickPoint;
if (asus_control.error_occured) {
DestroyWindow(hWnd);
return -1;
}
if (message == WM_TASKBAR_CREATE) { // Taskbar has been recreated (Explorer crashed?)
// Display tray icon
if (!InitTray(hInst, hWnd, nid)) {
MessageBox(NULL, L"Systray Icon Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
DestroyWindow(hWnd);
return -1;
}
}
switch (message)
{
case WM_DESTROY:
Cleanup(hWnd, nid); // Remove Tray Item
PostQuitMessage(0); // Quit
break;
case WM_USER_SHELLICON: // sys tray icon Messages
switch (LOWORD(lParam))
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN: // Click on sys tray icon
{
HMENU hMenu, hSubMenu;
GetCursorPos(&lpClickPoint);
// Load menu resource
hMenu = LoadMenuW(hInst, MAKEINTRESOURCE(IDC_FANCONTROL));
if (!hMenu)
return -1; // !0, message not successful?
// Select the first submenu
hSubMenu = GetSubMenu(hMenu, 0);
if (!hSubMenu) {
DestroyMenu(hMenu); // Be sure to Destroy Menu Before Returning
return -1;
}
// Set Enabled State
CheckMenuItem(hSubMenu, ID_POPUP_ENABLE, MF_BYCOMMAND | (toggle_change_fan_speed ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(hSubMenu, ID_POPUP_STARTUP, MF_BYCOMMAND | (startup ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(hSubMenu, ID_POPUP_ECO, MF_BYCOMMAND | (current_mode == ID_POPUP_ECO ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(hSubMenu, ID_POPUP_BALANCED, MF_BYCOMMAND | (current_mode == ID_POPUP_BALANCED ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(hSubMenu, ID_POPUP_TURBO, MF_BYCOMMAND | (current_mode == ID_POPUP_TURBO ? MF_CHECKED : MF_UNCHECKED));
// Display menu
SetForegroundWindow(hWnd);
TrackPopupMenu(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN, lpClickPoint.x, lpClickPoint.y, 0, hWnd, NULL);
SendMessage(hWnd, WM_NULL, 0, 0);
// Kill off objects we're done with
DestroyMenu(hMenu);
}
break;
}
break;
case WM_CLOSE:
Cleanup(hWnd, nid); // Remove Tray Item
DestroyWindow(hWnd); // Destroy Window
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_POPUP_EXIT:
Cleanup(hWnd, nid); // Remove Tray Item
DestroyWindow(hWnd); // Destroy Window
break;
case ID_POPUP_ABOUT: // Open about box
ShowAboutMessage(hWnd);
break;
case ID_POPUP_SETTINGS:
if (current_settings_hwnd == 0) DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGSBOX), hWnd, Settings);
else {
SetForegroundWindow(current_settings_hwnd); // focus on the window
}
break;
case ID_POPUP_ENABLE: // Toggle Enable
toggle_change_fan_speed = !toggle_change_fan_speed;
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
write_settings(settings);
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
break;
case ID_POPUP_STARTUP: // Toggle Startup
startup = !startup;
if (!ChangeStartupBehavior(startup)) startup = !startup; // revert if failed
settings.sections[L"General"][L"Startup"] = _ts(startup);
write_settings(settings);
break;
case ID_POPUP_ECO:
case ID_POPUP_BALANCED:
case ID_POPUP_TURBO:
current_mode = LOWORD(wParam);
settings.sections[L"General"][L"CurrentMode"] = _ts(current_mode);
write_settings(settings);
UpdateTray(hWnd, nid, asus_control, current_mode);
break;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0; // Return 0 = Message successfully proccessed
}
// Message handler for about box.
HRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam, LONG_PTR lpdata)
{
if (message == TDN_HYPERLINK_CLICKED) {
ShellExecuteW(NULL, L"open", (LPCWSTR)lParam, NULL, NULL, SW_SHOWNORMAL);
}
return S_OK;
}
// Message handler for settings box.
INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
current_settings_hwnd = hDlg;
read_settings(settings);
std::wstring tmp_eco, tmp_balanced, tmp_turbo;
inipp::extract(settings.sections[L"Curves"][L"FanTurbo"], tmp_turbo);
inipp::extract(settings.sections[L"Curves"][L"FanBalanced"], tmp_balanced);
inipp::extract(settings.sections[L"Curves"][L"FanEco"], tmp_eco);
SetDlgItemInt(hDlg, IDC_EDIT_INTERVAL, update_interval, FALSE);
SetDlgItemTextW(hDlg, IDC_EDIT_ECO, tmp_eco.c_str());
SetDlgItemTextW(hDlg, IDC_EDIT_BALANCED, tmp_balanced.c_str());
SetDlgItemTextW(hDlg, IDC_EDIT_TURBO, tmp_turbo.c_str());
CheckDlgButton(hDlg, IDC_CHECK_ENABLE, toggle_change_fan_speed);
CheckDlgButton(hDlg, IDC_CHECK_STARTUP, startup);
CheckDlgButton(hDlg, IDC_CHECK_SMOOTHTEMP, toggle_smooth_temp);
CheckDlgButton(hDlg, IDC_CHECK_ADAPTIVEMODE, toggle_adaptive_mode);
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Eco");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Balanced");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Turbo");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_MODE), CB_SETCURSEL, static_cast<WPARAM>(current_mode - ID_POPUP_ECO), 0);
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_AC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Eco");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_AC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Balanced");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_AC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Turbo");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_AC_MODE), CB_SETCURSEL, static_cast<WPARAM>(current_ac_mode - ID_POPUP_ECO), 0);
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_DC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Eco");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_DC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Balanced");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_DC_MODE), CB_INSERTSTRING, -1, (LPARAM)L"Turbo");
SendMessageW(GetDlgItem(hDlg, IDC_COMBO_DC_MODE), CB_SETCURSEL, static_cast<WPARAM>(current_dc_mode - ID_POPUP_ECO), 0);
return (INT_PTR)TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON_OK:
case IDC_BUTTON_APPLY:
{
// read settings
read_settings(settings);
// update interval
int tmp_update_interval = GetDlgItemInt(hDlg, IDC_EDIT_INTERVAL, NULL, FALSE);
if (tmp_update_interval < 500 || tmp_update_interval > 30000) {
MessageBox(NULL, L"Update interval not in the range of [500;30000]!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
break;
}
update_interval = tmp_update_interval;
settings.sections[L"General"][L"UpdateInterval"] = _ts(update_interval);
// update current mode
current_mode = SendMessageW(GetDlgItem(hDlg, IDC_COMBO_MODE), CB_GETCURSEL, 0, 0) + ID_POPUP_ECO;
settings.sections[L"General"][L"CurrentMode"] = _ts(current_mode);
toggle_smooth_temp = IsDlgButtonChecked(hDlg, IDC_CHECK_SMOOTHTEMP);
settings.sections[L"General"][L"SmoothTempEnable"] = _ts(toggle_smooth_temp);
// update adaptive mode
toggle_adaptive_mode = IsDlgButtonChecked(hDlg, IDC_CHECK_ADAPTIVEMODE);
settings.sections[L"General"][L"AdaptiveModeEnable"] = _ts(toggle_adaptive_mode);
current_ac_mode = SendMessageW(GetDlgItem(hDlg, IDC_COMBO_AC_MODE), CB_GETCURSEL, 0, 0) + ID_POPUP_ECO;
settings.sections[L"General"][L"AdaptiveModeAC"] = _ts(current_ac_mode);
current_dc_mode = SendMessageW(GetDlgItem(hDlg, IDC_COMBO_DC_MODE), CB_GETCURSEL, 0, 0) + ID_POPUP_ECO;
settings.sections[L"General"][L"AdaptiveModeDC"] = _ts(current_dc_mode);
// update fan curves
wchar_t tmp_eco[256], tmp_balanced[256], tmp_turbo[256];
GetDlgItemTextW(hDlg, IDC_EDIT_ECO, tmp_eco, 256);
GetDlgItemTextW(hDlg, IDC_EDIT_BALANCED, tmp_balanced, 256);
GetDlgItemTextW(hDlg, IDC_EDIT_TURBO, tmp_turbo, 256);
if (validator(tmp_eco) == false || validator(tmp_balanced) == false || validator(tmp_turbo) == false) {
MessageBox(NULL, L"Invalid fan curve!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
break;
}
settings.sections[L"Curves"][L"FanEco"] = _ts(tmp_eco);
settings.sections[L"Curves"][L"FanBalanced"] = _ts(tmp_balanced);
settings.sections[L"Curves"][L"FanTurbo"] = _ts(tmp_turbo);
bool tmp_enable = IsDlgButtonChecked(hDlg, IDC_CHECK_ENABLE);
bool tmp_startup = IsDlgButtonChecked(hDlg, IDC_CHECK_STARTUP);
// close dialog
if(LOWORD(wParam) == IDC_BUTTON_OK) EndDialog(hDlg, LOWORD(wParam));
if (tmp_startup != startup) {
startup = tmp_startup;
if (!ChangeStartupBehavior(startup)) startup = !startup; // revert if failed
settings.sections[L"General"][L"Startup"] = _ts(startup);
}
if (tmp_enable != toggle_change_fan_speed) {
toggle_change_fan_speed = tmp_enable;
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
}
// write settings
write_settings(settings);
MessageBox(NULL, L"Settings applied successfully!", L"Success!", MB_ICONINFORMATION | MB_OK);
// update tray icon
UpdateTray(GetParent(hDlg), nid, asus_control, current_mode);
if (LOWORD(wParam) == IDC_BUTTON_OK) {
current_settings_hwnd = 0;
return (INT_PTR)TRUE;
}
else {
break;
}
}
case IDC_BUTTON_CANCEL:
case IDCANCEL:
{
EndDialog(hDlg, LOWORD(wParam));
current_settings_hwnd = 0;
return (INT_PTR)TRUE;
}
}
break;
}
return (INT_PTR)FALSE;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// before doing anything, set the working directory to the executable's directory
wchar_t path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
std::wstring tmp(path);
tmp = tmp.substr(0, tmp.find_last_of(L"\\/"));
SetCurrentDirectoryW(tmp.c_str());
_dInfo(tmp);
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_FANCONTROL, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
HWND hWnd = InitInstance(hInstance, nCmdShow);
if (hWnd == NULL)
{
return FALSE;
}
// Load settings
read_settings(settings);
inipp::extract(settings.sections[L"General"][L"UpdateInterval"], update_interval);
int tmp_re = 0;
inipp::extract(settings.sections[L"General"][L"SmoothTempEnable"], tmp_re);
toggle_smooth_temp = tmp_re;
// Load settings (adaptive mode)
inipp::extract(settings.sections[L"General"][L"AdaptiveModeEnable"], tmp_re);
toggle_adaptive_mode = tmp_re;
inipp::extract(settings.sections[L"General"][L"AdaptiveModeAC"], current_ac_mode);
inipp::extract(settings.sections[L"General"][L"AdaptiveModeDC"], current_dc_mode);
if (toggle_adaptive_mode) {
current_power_mode = asus_control.get_power_mode();
if (current_power_mode == PWRStatus::AC) current_mode = current_ac_mode;
else if (current_power_mode == PWRStatus::DC) current_mode = current_dc_mode;
settings.sections[L"General"][L"CurrentMode"] = _ts(current_mode);
write_settings(settings);
}
else {
inipp::extract(settings.sections[L"General"][L"CurrentMode"], current_mode);
}
// Load settings (startup)
inipp::extract(settings.sections[L"General"][L"Startup"], tmp_re);
startup = tmp_re;
ChangeStartupBehavior(startup);
// Load settings (enable program)
inipp::extract(settings.sections[L"General"][L"Enable"], tmp_re);
toggle_change_fan_speed = tmp_re;
asus_control.set_fan_test_mode(tmp_re ? 0x01 : 0x00);
// Display tray icon
if (!InitTray(hInst, hWnd, nid)) {
MessageBox(NULL, L"Systray Icon Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
DestroyWindow(hWnd);
return 0;
}
if (asus_control.error_occured) {
DestroyWindow(hWnd);
return 0;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FANCONTROL));
MSG msg;
std::thread main_thread(MainThread, hWnd);
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Cleanup(hWnd, nid);
// kinda dirty trick, investigate later
thread_term = true;
main_thread.join();
return (int)msg.wParam;
}