-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1220 lines (1101 loc) · 55.2 KB
/
Copy pathmain.cpp
File metadata and controls
1220 lines (1101 loc) · 55.2 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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "advapi32.lib")
#include <windows.h>
#include <shellapi.h>
#include <dwmapi.h>
#include <tlhelp32.h>
#include <shlobj.h>
#include <commctrl.h>
#include <string>
#include <vector>
#include <set>
#include <thread>
#include <atomic>
#include <mutex>
#include <memory>
#include <algorithm>
#include <fstream>
#include <ctime>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "dwmapi.lib")
// ==========================================
// DATA STRUCTURES & PRESETS
// ==========================================
struct AppProfile {
std::wstring exePath;
std::wstring exeName;
std::wstring displayDev;
int targetW;
int targetH;
int targetHz;
int delaySec;
bool restore;
bool enabled;
};
struct ResTemplate {
std::wstring name;
int w;
int h;
};
struct ModeEntry {
int w;
int h;
int hz;
};
const std::vector<ResTemplate> g_Presets = {
{ L"Custom (Manual Input)", 0, 0 },
{ L"3840 x 2160 (4K UHD 16:9)", 3840, 2160 },
{ L"2560 x 1440 (QHD 16:9)", 2560, 1440 },
{ L"1920 x 1080 (FHD 16:9)", 1920, 1080 },
{ L"1600 x 900 (HD+ 16:9)", 1600, 900 },
{ L"1440 x 900 (WXGA+ 16:10)", 1440, 900 },
{ L"1366 x 768 (HD 16:9)", 1366, 768 },
{ L"1280 x 1024 (SXGA 5:4)", 1280, 1024 },
{ L"1280 x 800 (WXGA 16:10)", 1280, 800 },
{ L"1280 x 720 (HD 16:9)", 1280, 720 },
{ L"1024 x 768 (XGA 4:3)", 1024, 768 },
{ L"800 x 600 (SVGA 4:3)", 800, 600 },
{ L"640 x 480 (VGA 4:3)", 640, 480 }
};
// The profile list is owned exclusively by the UI thread. The monitor thread
// never touches g_Profiles directly; instead the UI thread publishes an
// immutable snapshot that the worker reads. This eliminates the data race and
// the dangling-pointer bug that occurred when the vector reallocated.
std::vector<AppProfile> g_Profiles;
std::mutex g_SnapshotMutex;
std::shared_ptr<const std::vector<AppProfile>> g_ProfileSnapshot;
// NOTE: g_OriginalDevMode / g_ResChanged / g_ProcessFoundTime / g_ActiveIndex
// are touched only by the monitor thread, except for the explicit
// restore-on-exit path which is serialized via g_RestoreMutex.
DEVMODEW g_OriginalDevMode;
bool g_ResChanged = false;
std::atomic<bool> g_MonitorRunning(true);
std::wstring g_IniPath;
std::atomic<ULONGLONG> g_ProcessFoundTime(0);
std::atomic<int> g_ActiveIndex(-1); // index into the *current snapshot*, or -1
AppProfile g_ActiveProfileCopy; // value copy of the active profile (never dangles)
std::mutex g_RestoreMutex; // serializes restore between UI + monitor threads
bool g_IgnoreEditChange = false;
HINSTANCE g_hInst = NULL; // Stores instance handle for loading the baked-in icon
HANDLE g_hInstanceMutex = NULL; // Single-instance guard
HICON g_hAppIcon = NULL; // Owned icon handle, freed on exit
bool g_DarkMode = false; // UI theme preference
bool g_LoggingEnabled = false; // Persisted logging preference
// UI Handles
#ifndef TESTING
HWND g_hMain, g_hList, g_hExe, g_hDisplayCombo, g_hW, g_hH, g_hHz, g_hDelay;
HWND g_hRestore, g_hEnable, g_hStartup, g_hPresetCombo, g_hSupportedCombo;
HWND g_hListLabel, g_hExeLabel, g_hBtnBrowse, g_hDisplayLabel, g_hPresetLabel;
HWND g_hWLabel, g_hHLabel, g_hHzLabel, g_hDelayLabel, g_hSaveBtn, g_hDelBtn, g_hLaunchBtn, g_hTestBtn;
HWND g_hSupportedLabel, g_hLogChk, g_hDarkChk, g_hStatusLabel;
#endif
#define WM_TRAYICON (WM_APP + 1)
#ifndef TESTING
NOTIFYICONDATAW g_Nid = {};
#endif
std::vector<std::wstring> g_MonitorDevices;
std::wstring g_LogPath;
std::vector<ModeEntry> g_SupportedModesCache;
COLORREF g_StatusColor = RGB(40, 40, 40); // current inline-status text colour
// ==========================================
// LOGGING
// ==========================================
void LogMsg(const std::wstring& text) {
if (!g_LoggingEnabled || g_LogPath.empty()) return;
std::wofstream f(g_LogPath.c_str(), std::ios::app);
if (!f.is_open()) return;
time_t t = time(nullptr);
tm tmv;
localtime_s(&tmv, &t);
WCHAR stamp[32];
wcsftime(stamp, 32, L"%Y-%m-%d %H:%M:%S", &tmv);
f << L"[" << stamp << L"] " << text << L"\n";
}
// ==========================================
// UTILITY & REGISTRY FUNCTIONS
// ==========================================
std::wstring GetFileName(const std::wstring& path) {
size_t pos = path.find_last_of(L"\\/");
return (pos == std::wstring::npos) ? path : path.substr(pos + 1);
}
bool IsProcessRunning(const std::wstring& processName) {
if (processName.empty()) return false;
bool exists = false;
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) return false;
if (Process32FirstW(snapshot, &entry)) {
do {
if (_wcsicmp(entry.szExeFile, processName.c_str()) == 0) {
exists = true; break;
}
} while (Process32NextW(snapshot, &entry));
}
CloseHandle(snapshot);
return exists;
}
// Returns the set of running executable names (lower-cased) in a single pass.
// The monitor thread uses one snapshot per tick instead of one per profile,
// which keeps the detection cheap even with many profiles.
std::set<std::wstring> GetRunningProcessNames() {
std::set<std::wstring> names;
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) return names;
if (Process32FirstW(snapshot, &entry)) {
do {
std::wstring n = entry.szExeFile;
for (auto& c : n) c = (WCHAR)towlower(c);
names.insert(n);
} while (Process32NextW(snapshot, &entry));
}
CloseHandle(snapshot);
return names;
}
std::wstring ToLowerCopy(const std::wstring& s) {
std::wstring r = s;
for (auto& c : r) c = (WCHAR)towlower(c);
return r;
}
// Republish an immutable snapshot of the profile list for the monitor thread.
// Must be called from the UI thread whenever g_Profiles changes.
void PublishSnapshot() {
auto snap = std::make_shared<std::vector<AppProfile>>(g_Profiles);
std::lock_guard<std::mutex> lock(g_SnapshotMutex);
g_ProfileSnapshot = std::move(snap);
}
bool GetRunAtStartup() {
HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
WCHAR path[MAX_PATH];
DWORD size = sizeof(path);
bool exists = (RegQueryValueExW(hKey, L"AutoResChanger", NULL, NULL, (LPBYTE)path, &size) == ERROR_SUCCESS);
RegCloseKey(hKey);
return exists;
}
return false;
}
void SetRunAtStartup(bool enable) {
HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {
if (enable) {
WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
// Properly quote path and add the silent flag so it starts minimized in the background
std::wstring cmd = L"\"" + std::wstring(path) + L"\" -silent";
RegSetValueExW(hKey, L"AutoResChanger", 0, REG_SZ, (const BYTE*)cmd.c_str(), (DWORD)((cmd.length() + 1) * sizeof(WCHAR)));
} else {
RegDeleteValueW(hKey, L"AutoResChanger");
}
RegCloseKey(hKey);
}
}
// ==========================================
// CONFIGURATION MANAGEMENT
// ==========================================
void InitConfigPath() {
WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path))) {
std::wstring dir = std::wstring(path) + L"\\AutoResChanger";
CreateDirectoryW(dir.c_str(), NULL);
g_IniPath = dir + L"\\config.ini";
g_LogPath = dir + L"\\log.txt";
}
}
void LoadSettings() {
g_DarkMode = GetPrivateProfileIntW(L"Settings", L"DarkMode", 0, g_IniPath.c_str()) != 0;
g_LoggingEnabled = GetPrivateProfileIntW(L"Settings", L"Logging", 0, g_IniPath.c_str()) != 0;
}
void SaveSettings() {
WritePrivateProfileStringW(L"Settings", L"DarkMode", g_DarkMode ? L"1" : L"0", g_IniPath.c_str());
WritePrivateProfileStringW(L"Settings", L"Logging", g_LoggingEnabled ? L"1" : L"0", g_IniPath.c_str());
}
void LoadConfig() {
g_Profiles.clear();
int count = GetPrivateProfileIntW(L"Settings", L"Count", 0, g_IniPath.c_str());
for (int i = 0; i < count; ++i) {
std::wstring section = L"Profile_" + std::to_wstring(i);
WCHAR exe[MAX_PATH], dev[64];
GetPrivateProfileStringW(section.c_str(), L"Exe", L"", exe, MAX_PATH, g_IniPath.c_str());
GetPrivateProfileStringW(section.c_str(), L"Device", L"", dev, 64, g_IniPath.c_str());
if (wcslen(exe) > 0) {
AppProfile p;
p.exePath = exe;
p.exeName = GetFileName(p.exePath);
p.displayDev = dev;
p.targetW = GetPrivateProfileIntW(section.c_str(), L"W", 1920, g_IniPath.c_str());
p.targetH = GetPrivateProfileIntW(section.c_str(), L"H", 1080, g_IniPath.c_str());
p.targetHz = GetPrivateProfileIntW(section.c_str(), L"Hz", 0, g_IniPath.c_str());
p.delaySec = GetPrivateProfileIntW(section.c_str(), L"Delay", 0, g_IniPath.c_str());
p.restore = GetPrivateProfileIntW(section.c_str(), L"Restore", 1, g_IniPath.c_str()) != 0;
p.enabled = GetPrivateProfileIntW(section.c_str(), L"Enabled", 1, g_IniPath.c_str()) != 0;
g_Profiles.push_back(p);
}
}
}
void SaveConfig() {
if (g_IniPath.empty()) return;
// Write to a temporary file first, then atomically replace the real one so
// a crash or power loss mid-save cannot corrupt config.ini.
std::wstring tmpPath = g_IniPath + L".tmp";
DeleteFileW(tmpPath.c_str());
WritePrivateProfileStringW(L"Settings", L"Count", std::to_wstring(g_Profiles.size()).c_str(), tmpPath.c_str());
WritePrivateProfileStringW(L"Settings", L"DarkMode", g_DarkMode ? L"1" : L"0", tmpPath.c_str());
WritePrivateProfileStringW(L"Settings", L"Logging", g_LoggingEnabled ? L"1" : L"0", tmpPath.c_str());
for (size_t i = 0; i < g_Profiles.size(); ++i) {
std::wstring sec = L"Profile_" + std::to_wstring(i);
WritePrivateProfileStringW(sec.c_str(), L"Exe", g_Profiles[i].exePath.c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"Device", g_Profiles[i].displayDev.c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"W", std::to_wstring(g_Profiles[i].targetW).c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"H", std::to_wstring(g_Profiles[i].targetH).c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"Hz", std::to_wstring(g_Profiles[i].targetHz).c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"Delay", std::to_wstring(g_Profiles[i].delaySec).c_str(), tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"Restore", g_Profiles[i].restore ? L"1" : L"0", tmpPath.c_str());
WritePrivateProfileStringW(sec.c_str(), L"Enabled", g_Profiles[i].enabled ? L"1" : L"0", tmpPath.c_str());
}
// Flush the file to disk before swapping it in.
WritePrivateProfileStringW(NULL, NULL, NULL, tmpPath.c_str());
if (!MoveFileExW(tmpPath.c_str(), g_IniPath.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
// Fallback: if the atomic replace failed, try a plain replace.
DeleteFileW(g_IniPath.c_str());
MoveFileW(tmpPath.c_str(), g_IniPath.c_str());
}
}
// ==========================================
// DISPLAY MANAGEMENT
// ==========================================
#ifndef TESTING
void PopulateMonitors() {
g_MonitorDevices.clear();
DISPLAY_DEVICEW dd = { sizeof(dd) };
DWORD devNum = 0;
SendMessage(g_hDisplayCombo, CB_RESETCONTENT, 0, 0);
SendMessage(g_hDisplayCombo, CB_ADDSTRING, 0, (LPARAM)L"Primary Monitor (Default)");
g_MonitorDevices.push_back(L"");
while (EnumDisplayDevicesW(NULL, devNum, &dd, 0)) {
if (dd.StateFlags & DISPLAY_DEVICE_ACTIVE) {
std::wstring displayStr = std::wstring(dd.DeviceString) + L" (" + dd.DeviceName + L")";
SendMessage(g_hDisplayCombo, CB_ADDSTRING, 0, (LPARAM)displayStr.c_str());
g_MonitorDevices.push_back(dd.DeviceName);
}
devNum++;
}
}
#endif
DEVMODEW GetCurrentRes(const std::wstring& devName) {
DEVMODEW dm = { 0 }; dm.dmSize = sizeof(dm);
EnumDisplaySettingsExW(devName.empty() ? NULL : devName.c_str(), ENUM_CURRENT_SETTINGS, &dm, 0);
return dm;
}
// Enumerates every distinct (w,h,hz) mode the given display advertises. This
// powers the "Supported Modes" dropdown so the user never has to guess.
std::vector<ModeEntry> EnumModesForDevice(const std::wstring& devName) {
std::vector<ModeEntry> modes;
std::set<long long> seen; // key = w<<40 | h<<20 | hz-ish, packed loosely
DEVMODEW dm = { 0 }; dm.dmSize = sizeof(dm);
DWORD i = 0;
while (EnumDisplaySettingsExW(devName.empty() ? NULL : devName.c_str(), i, &dm, 0)) {
long long key = ((long long)dm.dmPelsWidth << 32) ^ ((long long)dm.dmPelsHeight << 16) ^ (long long)dm.dmDisplayFrequency;
if (seen.insert(key).second) {
modes.push_back({ (int)dm.dmPelsWidth, (int)dm.dmPelsHeight, (int)dm.dmDisplayFrequency });
}
i++;
}
// Sort by resolution desc, then refresh rate desc.
std::sort(modes.begin(), modes.end(), [](const ModeEntry& a, const ModeEntry& b) {
if (a.w != b.w) return a.w > b.w;
if (a.h != b.h) return a.h > b.h;
return a.hz > b.hz;
});
return modes;
}
// Rebuilds the Supported Modes dropdown for the currently selected display.
#ifndef TESTING
void RefreshSupportedModes() {
if (!g_hSupportedCombo) return;
SendMessage(g_hSupportedCombo, CB_RESETCONTENT, 0, 0);
int idx = (int)SendMessage(g_hDisplayCombo, CB_GETCURSEL, 0, 0);
std::wstring dev = (idx > 0 && idx < (int)g_MonitorDevices.size()) ? g_MonitorDevices[idx] : L"";
std::vector<ModeEntry> modes = EnumModesForDevice(dev);
SendMessage(g_hSupportedCombo, CB_ADDSTRING, 0, (LPARAM)L"Pick a listed mode...");
for (const auto& m : modes) {
std::wstring label = std::to_wstring(m.w) + L" x " + std::to_wstring(m.h);
if (m.hz > 0) label += L" @ " + std::to_wstring(m.hz) + L" Hz";
SendMessage(g_hSupportedCombo, CB_ADDSTRING, 0, (LPARAM)label.c_str());
}
g_SupportedModesCache = modes; // index 0 == placeholder, so cache index = combo index-1
SendMessage(g_hSupportedCombo, CB_SETCURSEL, 0, 0);
}
#endif
// Extracts the icon for a given executable and returns a small HICON (caller
// owns it) or NULL on failure.
HICON ExtractExeIcon(const std::wstring& path, int cx, int cy) {
if (path.empty()) return NULL;
SHFILEINFOW sfi = { 0 };
UINT flags = SHGFI_ICON | SHGFI_SMALLICON;
DWORD_PTR r = SHGetFileInfoW(path.c_str(), 0, &sfi, sizeof(sfi), flags);
if (!r || !sfi.hIcon) return NULL;
// Resize to the requested dimensions for crisper list rendering.
HICON resized = (HICON)CopyImage(sfi.hIcon, IMAGE_ICON, cx, cy, LR_COPYFROMRESOURCE);
DestroyIcon(sfi.hIcon);
return resized ? resized : NULL;
}
int ChangeRes(const std::wstring& devName, int w, int h, int hz) {
DEVMODEW dm = GetCurrentRes(devName);
dm.dmPelsWidth = w; dm.dmPelsHeight = h;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
if (hz > 0) { dm.dmDisplayFrequency = hz; dm.dmFields |= DM_DISPLAYFREQUENCY; }
return ChangeDisplaySettingsExW(devName.empty() ? NULL : devName.c_str(), &dm, NULL, CDS_FULLSCREEN, NULL);
}
void RestoreRes(const std::wstring& devName, DEVMODEW& dm) {
ChangeDisplaySettingsExW(devName.empty() ? NULL : devName.c_str(), &dm, NULL, CDS_FULLSCREEN, NULL);
}
// Restore the resolution if a profile change is currently active. Safe to call
// from either thread; the restore itself is serialized by g_RestoreMutex.
// Returns true if a restore was performed.
bool RestoreActiveIfChanged() {
std::lock_guard<std::mutex> lock(g_RestoreMutex);
if (g_ActiveIndex >= 0 && g_ResChanged) {
RestoreRes(g_ActiveProfileCopy.displayDev, g_OriginalDevMode);
g_ResChanged = false;
g_ActiveIndex = -1;
g_ProcessFoundTime = 0;
return true;
}
return false;
}
// Clears the active-state bookkeeping without touching the display. Used when
// the profile was set to not restore, or the change never succeeded.
void ClearActiveState() {
std::lock_guard<std::mutex> lock(g_RestoreMutex);
g_ResChanged = false;
g_ActiveIndex = -1;
g_ProcessFoundTime = 0;
}
// ==========================================
// BACKGROUND THREAD
// ==========================================
void MonitorLoop() {
// How long to wait between checks. We poll more often while idle so a game
// is detected quickly, but back off a little while watching a running
// process (nothing can change until it exits).
const DWORD kIdleIntervalMs = 1000;
const DWORD kWatchIntervalMs = 2000;
while (g_MonitorRunning) {
DWORD sleepMs = kIdleIntervalMs;
// Grab the latest immutable snapshot (cheap shared_ptr copy).
std::shared_ptr<const std::vector<AppProfile>> snap;
{
std::lock_guard<std::mutex> lock(g_SnapshotMutex);
snap = g_ProfileSnapshot;
}
if (g_ActiveIndex >= 0) {
sleepMs = kWatchIntervalMs;
// An active profile is being watched. If it was removed from the
// config, or its process exited, restore and stop watching.
bool stillConfigured = (snap && g_ActiveIndex < (int)snap->size());
bool processGone = !stillConfigured || !IsProcessRunning(g_ActiveProfileCopy.exeName);
if (processGone) {
if (g_ActiveProfileCopy.restore) {
if (RestoreActiveIfChanged())
LogMsg(L"Restored resolution (process exited): " + g_ActiveProfileCopy.exeName);
} else {
ClearActiveState();
}
}
} else if (snap && !snap->empty()) {
std::set<std::wstring> running = GetRunningProcessNames();
for (size_t i = 0; i < snap->size(); ++i) {
const AppProfile& p = (*snap)[i];
if (p.enabled && running.count(ToLowerCopy(p.exeName))) {
ULONGLONG now = GetTickCount64();
if (g_ProcessFoundTime == 0) {
g_ProcessFoundTime = now;
LogMsg(L"Detected process: " + p.exeName);
}
if ((now - g_ProcessFoundTime) >= (ULONGLONG)(p.delaySec * 1000)) {
DEVMODEW orig = GetCurrentRes(p.displayDev);
int result = ChangeRes(p.displayDev, p.targetW, p.targetH, p.targetHz);
// Publish the new active state under the restore lock so
// the UI thread's RestoreActiveIfChanged() sees a
// consistent (dev mode, active index, changed flag) set.
{
std::lock_guard<std::mutex> lock(g_RestoreMutex);
g_OriginalDevMode = orig;
g_ResChanged = (result == DISP_CHANGE_SUCCESSFUL);
g_ActiveProfileCopy = p; // value copy -> never dangles
g_ActiveIndex = (int)i;
}
if (result == DISP_CHANGE_SUCCESSFUL) {
LogMsg(L"Applied " + std::to_wstring(p.targetW) + L"x" + std::to_wstring(p.targetH) +
L"@" + std::to_wstring(p.targetHz) + L"Hz for " + p.exeName);
} else {
LogMsg(L"FAILED to apply mode for " + p.exeName + L" (code " + std::to_wstring(result) + L")");
}
break;
}
}
}
}
// Interruptible sleep: check the shutdown flag frequently so exit is fast.
DWORD waited = 0;
while (waited < sleepMs && g_MonitorRunning) {
Sleep(200);
waited += 200;
}
}
}
// ==========================================
// UI MANAGEMENT
// ==========================================
#ifndef TESTING
// Icons are owned per rendered list row and re-created on refresh.
std::vector<HICON> g_ListItemIcons;
void RefreshList(int selectIndex = -1) {
for (HICON ic : g_ListItemIcons) if (ic) DestroyIcon(ic);
g_ListItemIcons.clear();
SendMessage(g_hList, LB_RESETCONTENT, 0, 0);
for (const auto& p : g_Profiles) {
std::wstring display = p.exeName + L" (" + std::to_wstring(p.targetW) + L"x" + std::to_wstring(p.targetH) + L")";
int idx = (int)SendMessage(g_hList, LB_ADDSTRING, 0, (LPARAM)display.c_str());
HICON ic = ExtractExeIcon(p.exePath, 16, 16);
g_ListItemIcons.push_back(ic);
SendMessage(g_hList, LB_SETITEMDATA, idx, (LPARAM)ic);
}
if (selectIndex >= 0 && selectIndex < (int)g_Profiles.size()) {
SendMessage(g_hList, LB_SETCURSEL, selectIndex, 0);
}
}
void SelectProfile(int index) {
if (index >= 0 && index < (int)g_Profiles.size()) {
const auto& p = g_Profiles[index];
SetWindowTextW(g_hExe, p.exePath.c_str());
g_IgnoreEditChange = true;
SetWindowTextW(g_hW, std::to_wstring(p.targetW).c_str());
SetWindowTextW(g_hH, std::to_wstring(p.targetH).c_str());
g_IgnoreEditChange = false;
SetWindowTextW(g_hHz, std::to_wstring(p.targetHz).c_str());
SetWindowTextW(g_hDelay, std::to_wstring(p.delaySec).c_str());
SendMessage(g_hRestore, BM_SETCHECK, p.restore ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessage(g_hEnable, BM_SETCHECK, p.enabled ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessage(g_hDisplayCombo, CB_SETCURSEL, 0, 0);
for (size_t i = 0; i < g_MonitorDevices.size(); ++i) {
if (g_MonitorDevices[i] == p.displayDev) {
SendMessage(g_hDisplayCombo, CB_SETCURSEL, i, 0); break;
}
}
int presetIdx = 0;
for (size_t i = 1; i < g_Presets.size(); ++i) {
if (g_Presets[i].w == p.targetW && g_Presets[i].h == p.targetH) {
presetIdx = i; break;
}
}
SendMessage(g_hPresetCombo, CB_SETCURSEL, presetIdx, 0);
}
}
BOOL CALLBACK SetFontCallback(HWND hwndChild, LPARAM lParam) {
SendMessage(hwndChild, WM_SETFONT, lParam, TRUE);
return TRUE;
}
// ==========================================
// THEME (DARK MODE)
// ==========================================
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
HBRUSH g_hDarkBrush = NULL; // Owned background brush for dark mode
HBRUSH g_hEditBrush = NULL; // Owned edit-field background brush
// Applies the Windows immersive dark title bar. The rest of the dark styling is
// handled in WM_CTLCOLOR* using the brushes above.
void ApplyTheme(HWND hwnd) {
BOOL dark = g_DarkMode ? TRUE : FALSE;
// Older builds honour attribute 19, newer ones 20; try both.
if (FAILED(DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &dark, sizeof(dark)))) {
const DWORD alt = 19;
DwmSetWindowAttribute(hwnd, alt, &dark, sizeof(dark));
}
if (!g_hDarkBrush) g_hDarkBrush = CreateSolidBrush(RGB(32, 32, 32));
if (!g_hEditBrush) g_hEditBrush = CreateSolidBrush(RGB(45, 45, 45));
InvalidateRect(hwnd, NULL, TRUE);
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN);
}
// ==========================================
// TEST-MODE AUTO-REVERT
// ==========================================
// When the user tests a display mode we apply it, then arm a countdown. If the
// user does not confirm within the grace period we automatically restore the
// previous mode. This is the safety net that the README always promised but
// the original blocking MessageBox never delivered.
#define TIMER_TEST_REVERT 1
#define TIMER_CLEAR_STATUS 2
#define TEST_REVERT_SECONDS 15
DEVMODEW g_TestOriginal;
std::wstring g_TestDevice;
bool g_TestActive = false;
void CancelTestTimer(HWND hwnd) {
if (g_TestActive) {
KillTimer(hwnd, TIMER_TEST_REVERT);
g_TestActive = false;
}
}
// Colour codes for the inline status line.
enum class StatusKind { Info, Warn, Error, Ok };
// Sets the inline status text (replacing the old modal message boxes). The
// message stays visible for a few seconds, then auto-clears. This keeps the UI
// responsive and fully automatable.
void SetStatus(HWND hwnd, const std::wstring& text, StatusKind kind) {
if (!g_hStatusLabel) return;
SetWindowTextW(g_hStatusLabel, text.c_str());
COLORREF color = RGB(0, 120, 0);
switch (kind) {
case StatusKind::Warn: color = RGB(180, 100, 0); break;
case StatusKind::Error: color = RGB(190, 30, 30); break;
case StatusKind::Ok: color = RGB(0, 130, 0); break;
case StatusKind::Info: default: color = g_DarkMode ? RGB(210, 210, 210) : RGB(40, 40, 40); break;
}
// A static's text colour follows the WM_CTLCOLORSTATIC of its parent, so we
// store the desired colour and let the parent render it.
g_StatusColor = color;
InvalidateRect(g_hStatusLabel, NULL, TRUE);
// Restart the auto-clear timer.
KillTimer(hwnd, TIMER_CLEAR_STATUS);
SetTimer(hwnd, TIMER_CLEAR_STATUS, 6000, NULL);
}
// Returns true and writes the value when the field holds a positive integer.
bool ReadPositiveInt(HWND hEdit, int minVal, int maxVal, int& out) {
WCHAR buf[16] = { 0 };
GetWindowTextW(hEdit, buf, 16);
if (wcslen(buf) == 0) return false;
int v = _wtoi(buf);
if (v < minVal || v > maxVal) return false;
out = v;
return true;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// Left Panel
g_hListLabel = CreateWindowW(L"STATIC", L"Profiles:", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hList = CreateWindowW(L"LISTBOX", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | LBS_NOTIFY | WS_VSCROLL | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT, 0, 0, 0, 0, hwnd, (HMENU)100, NULL, NULL);
// Right Panel
g_hExeLabel = CreateWindowW(L"STATIC", L"Executable Path:", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hExe = CreateWindowW(L"EDIT", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 0, 0, 0, 0, hwnd, (HMENU)115, NULL, NULL);
g_hBtnBrowse = CreateWindowW(L"BUTTON", L"...", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, (HMENU)101, NULL, NULL);
g_hDisplayLabel = CreateWindowW(L"STATIC", L"Target Display (Monitor):", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hDisplayCombo = CreateWindowW(L"COMBOBOX", NULL, WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST | WS_VSCROLL, 0, 0, 0, 0, hwnd, (HMENU)110, NULL, NULL);
g_hSupportedLabel = CreateWindowW(L"STATIC", L"Supported Modes (live from monitor):", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hSupportedCombo = CreateWindowW(L"COMBOBOX", NULL, WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST | WS_VSCROLL, 0, 0, 0, 0, hwnd, (HMENU)111, NULL, NULL);
g_hPresetLabel = CreateWindowW(L"STATIC", L"Resolution Template / Presets:", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hPresetCombo = CreateWindowW(L"COMBOBOX", NULL, WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST | WS_VSCROLL, 0, 0, 0, 0, hwnd, (HMENU)107, NULL, NULL);
g_hWLabel = CreateWindowW(L"STATIC", L"Width:", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hW = CreateWindowW(L"EDIT", L"1920", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_NUMBER, 0, 0, 0, 0, hwnd, (HMENU)108, NULL, NULL);
g_hHLabel = CreateWindowW(L"STATIC", L"Height:", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hH = CreateWindowW(L"EDIT", L"1080", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_NUMBER, 0, 0, 0, 0, hwnd, (HMENU)109, NULL, NULL);
g_hHzLabel = CreateWindowW(L"STATIC", L"Hz (0=Def):", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hHz = CreateWindowW(L"EDIT", L"0", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_NUMBER, 0, 0, 0, 0, hwnd, (HMENU)116, NULL, NULL);
g_hDelayLabel = CreateWindowW(L"STATIC", L"Delay (sec):", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, NULL, NULL, NULL);
g_hDelay = CreateWindowW(L"EDIT", L"0", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_NUMBER, 0, 0, 0, 0, hwnd, (HMENU)117, NULL, NULL);
g_hRestore = CreateWindowW(L"BUTTON", L"Restore resolution on exit", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)118, NULL, NULL);
g_hEnable = CreateWindowW(L"BUTTON", L"Enable this profile", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)119, NULL, NULL);
SendMessage(g_hRestore, BM_SETCHECK, BST_CHECKED, 0);
SendMessage(g_hEnable, BM_SETCHECK, BST_CHECKED, 0);
g_hSaveBtn = CreateWindowW(L"BUTTON", L"Save Profile", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, (HMENU)102, NULL, NULL);
g_hDelBtn = CreateWindowW(L"BUTTON", L"Delete", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, (HMENU)103, NULL, NULL);
g_hLaunchBtn = CreateWindowW(L"BUTTON", L"Launch App", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, (HMENU)105, NULL, NULL);
g_hTestBtn = CreateWindowW(L"BUTTON", L"Test Display Settings", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, (HMENU)104, NULL, NULL);
// Inline, non-modal status line. Replaces blocking message boxes so
// the UI stays responsive and remains scriptable/automatable.
g_hStatusLabel = CreateWindowW(L"STATIC", L"", WS_VISIBLE | WS_CHILD | SS_LEFTNOWORDWRAP, 0, 0, 0, 0, hwnd, (HMENU)114, NULL, NULL);
g_hStartup = CreateWindowW(L"BUTTON", L"Start automatically with Windows (Minimized)", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)106, NULL, NULL);
if (GetRunAtStartup()) SendMessage(g_hStartup, BM_SETCHECK, BST_CHECKED, 0);
g_hLogChk = CreateWindowW(L"BUTTON", L"Enable diagnostic logging", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)112, NULL, NULL);
g_hDarkChk = CreateWindowW(L"BUTTON", L"Dark mode", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)113, NULL, NULL);
if (g_LoggingEnabled) SendMessage(g_hLogChk, BM_SETCHECK, BST_CHECKED, 0);
if (g_DarkMode) SendMessage(g_hDarkChk, BM_SETCHECK, BST_CHECKED, 0);
DragAcceptFiles(hwnd, TRUE);
for (const auto& preset : g_Presets) {
SendMessage(g_hPresetCombo, CB_ADDSTRING, 0, (LPARAM)preset.name.c_str());
}
SendMessage(g_hPresetCombo, CB_SETCURSEL, 0, 0);
RefreshSupportedModes();
HFONT hFont = CreateFontW(15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Segoe UI");
EnumChildWindows(hwnd, SetFontCallback, (LPARAM)hFont);
// Tray Icon Setup (loads the baked-in icon '1' at the correct small
// size so it renders crisply in the notification area).
g_hAppIcon = (HICON)LoadImageW(g_hInst, MAKEINTRESOURCEW(1), IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
if (!g_hAppIcon) g_hAppIcon = LoadIconW(NULL, IDI_APPLICATION);
g_Nid.cbSize = sizeof(NOTIFYICONDATAW);
g_Nid.hWnd = hwnd;
g_Nid.uID = 1;
g_Nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
g_Nid.uCallbackMessage = WM_TRAYICON;
g_Nid.hIcon = g_hAppIcon;
wcscpy_s(g_Nid.szTip, L"AutoRes Changer");
Shell_NotifyIconW(NIM_ADD, &g_Nid);
break;
}
case WM_SIZE: {
int w = LOWORD(lParam);
int h = HIWORD(lParam);
if (w == 0 || h == 0) break; // Minimized
int listWidth = w * 33 / 100;
if (listWidth < 180) listWidth = 180;
if (listWidth > 260) listWidth = 260;
int rightX = listWidth + 20;
int rightW = w - rightX - 10;
if (rightW < 220) rightW = 220;
MoveWindow(g_hListLabel, 10, 10, listWidth, 20, TRUE);
MoveWindow(g_hList, 10, 30, listWidth, h - 70, TRUE);
MoveWindow(g_hExeLabel, rightX, 10, rightW, 20, TRUE);
MoveWindow(g_hExe, rightX, 30, rightW - 35, 22, TRUE);
MoveWindow(g_hBtnBrowse, rightX + rightW - 30, 30, 30, 22, TRUE);
MoveWindow(g_hDisplayLabel, rightX, 58, rightW, 18, TRUE);
MoveWindow(g_hDisplayCombo, rightX, 76, rightW, 150, TRUE);
MoveWindow(g_hSupportedLabel, rightX, 104, rightW, 18, TRUE);
MoveWindow(g_hSupportedCombo, rightX, 122, rightW, 150, TRUE);
MoveWindow(g_hPresetLabel, rightX, 150, rightW, 18, TRUE);
MoveWindow(g_hPresetCombo, rightX, 168, rightW, 150, TRUE);
int colW = rightW / 4;
MoveWindow(g_hWLabel, rightX, 196, colW - 5, 18, TRUE);
MoveWindow(g_hW, rightX, 214, colW - 5, 22, TRUE);
MoveWindow(g_hHLabel, rightX + colW, 196, colW - 5, 18, TRUE);
MoveWindow(g_hH, rightX + colW, 214, colW - 5, 22, TRUE);
MoveWindow(g_hHzLabel, rightX + colW * 2, 196, colW - 5, 18, TRUE);
MoveWindow(g_hHz, rightX + colW * 2, 214, colW - 5, 22, TRUE);
MoveWindow(g_hDelayLabel, rightX + colW * 3, 196, colW - 5, 18, TRUE);
MoveWindow(g_hDelay, rightX + colW * 3, 214, colW - 5, 22, TRUE);
MoveWindow(g_hRestore, rightX, 244, rightW, 20, TRUE);
MoveWindow(g_hEnable, rightX, 264, rightW, 20, TRUE);
int btnW = rightW / 3;
MoveWindow(g_hSaveBtn, rightX, 292, btnW - 5, 30, TRUE);
MoveWindow(g_hDelBtn, rightX + btnW, 292, btnW - 5, 30, TRUE);
MoveWindow(g_hLaunchBtn, rightX + btnW * 2, 292, btnW - 5, 30, TRUE);
MoveWindow(g_hTestBtn, rightX, 326, rightW, 30, TRUE);
MoveWindow(g_hStatusLabel, rightX, 360, rightW, 18, TRUE);
// Bottom row of options.
int optW = rightW / 2;
MoveWindow(g_hLogChk, rightX, h - 58, optW - 5, 20, TRUE);
MoveWindow(g_hDarkChk, rightX + optW, h - 58, optW - 5, 20, TRUE);
MoveWindow(g_hStartup, rightX, h - 34, rightW, 20, TRUE);
break;
}
case WM_GETMINMAXINFO: {
MINMAXINFO* mmi = (MINMAXINFO*)lParam;
mmi->ptMinTrackSize.x = 580;
mmi->ptMinTrackSize.y = 500;
return 0;
}
case WM_CTLCOLORSTATIC:
case WM_CTLCOLORBTN: {
HDC hdc = (HDC)wParam;
HWND ctl = (HWND)lParam;
// The inline status label uses its own colour regardless of theme.
if (ctl == g_hStatusLabel) {
SetTextColor(hdc, g_StatusColor);
SetBkColor(hdc, g_DarkMode ? RGB(32, 32, 32) : GetSysColor(COLOR_WINDOW));
return (LRESULT)(g_DarkMode && g_hDarkBrush ? g_hDarkBrush : GetSysColorBrush(COLOR_WINDOW));
}
if (g_DarkMode && g_hDarkBrush) {
SetTextColor(hdc, RGB(230, 230, 230));
SetBkColor(hdc, RGB(32, 32, 32));
return (LRESULT)g_hDarkBrush;
}
break;
}
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX: {
if (g_DarkMode && g_hEditBrush) {
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(230, 230, 230));
SetBkColor(hdc, RGB(45, 45, 45));
return (LRESULT)g_hEditBrush;
}
break;
}
case WM_ERASEBKGND: {
if (g_DarkMode && g_hDarkBrush) {
RECT rc; GetClientRect(hwnd, &rc);
FillRect((HDC)wParam, &rc, g_hDarkBrush);
return 1;
}
break;
}
case WM_COMMAND: {
int wmId = LOWORD(wParam);
if (wmId == 100 && HIWORD(wParam) == LBN_SELCHANGE) {
SelectProfile(SendMessage(g_hList, LB_GETCURSEL, 0, 0));
} else if (wmId == 110 && HIWORD(wParam) == CBN_SELCHANGE) { // Display changed
RefreshSupportedModes();
} else if (wmId == 111 && HIWORD(wParam) == CBN_SELCHANGE) { // Supported mode picked
int sel = (int)SendMessage(g_hSupportedCombo, CB_GETCURSEL, 0, 0);
int mi = sel - 1; // combo index 0 is the placeholder
if (mi >= 0 && mi < (int)g_SupportedModesCache.size()) {
const ModeEntry& m = g_SupportedModesCache[mi];
g_IgnoreEditChange = true;
SetWindowTextW(g_hW, std::to_wstring(m.w).c_str());
SetWindowTextW(g_hH, std::to_wstring(m.h).c_str());
SetWindowTextW(g_hHz, std::to_wstring(m.hz > 0 ? m.hz : 0).c_str());
g_IgnoreEditChange = false;
SendMessage(g_hPresetCombo, CB_SETCURSEL, 0, 0);
}
} else if (wmId == 101) { // Browse
WCHAR szFile[MAX_PATH] = { 0 };
OPENFILENAMEW ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Executables\0*.exe\0All\0*.*\0";
ofn.lpstrDefExt = L"exe";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileNameW(&ofn)) SetWindowTextW(g_hExe, szFile);
} else if (wmId == 102) { // Save
WCHAR exe[MAX_PATH];
GetWindowTextW(g_hExe, exe, MAX_PATH);
if (wcslen(exe) == 0) {
SetStatus(hwnd, L"⚠ Select an executable path first.", StatusKind::Warn);
break;
}
int w = 0, h = 0;
if (!ReadPositiveInt(g_hW, 1, 32767, w) || !ReadPositiveInt(g_hH, 1, 32767, h)) {
SetStatus(hwnd, L"⚠ Width and Height must be 1-32767.", StatusKind::Warn);
break;
}
WCHAR hzbuf[16] = { 0 }, delbuf[16] = { 0 };
GetWindowTextW(g_hHz, hzbuf, 16);
GetWindowTextW(g_hDelay, delbuf, 16);
int hz = _wtoi(hzbuf); if (hz < 0) hz = 0;
int delSec = _wtoi(delbuf); if (delSec < 0) delSec = 0;
AppProfile p;
p.exePath = exe; p.exeName = GetFileName(p.exePath);
p.targetW = w; p.targetH = h; p.targetHz = hz; p.delaySec = delSec;
p.restore = SendMessage(g_hRestore, BM_GETCHECK, 0, 0) == BST_CHECKED;
p.enabled = SendMessage(g_hEnable, BM_GETCHECK, 0, 0) == BST_CHECKED;
int comboIdx = SendMessage(g_hDisplayCombo, CB_GETCURSEL, 0, 0);
p.displayDev = (comboIdx > 0 && comboIdx < (int)g_MonitorDevices.size()) ? g_MonitorDevices[comboIdx] : L"";
int targetIdx = -1;
bool found = false;
for (size_t i = 0; i < g_Profiles.size(); ++i) {
if (_wcsicmp(g_Profiles[i].exePath.c_str(), p.exePath.c_str()) == 0) {
g_Profiles[i] = p; found = true; targetIdx = (int)i; break;
}
}
if (!found) {
g_Profiles.push_back(p);
targetIdx = (int)g_Profiles.size() - 1;
}
SaveConfig();
PublishSnapshot();
RefreshList(targetIdx);
SetStatus(hwnd, L"✔ Saved profile: " + p.exeName, StatusKind::Ok);
} else if (wmId == 103) { // Delete
int idx = SendMessage(g_hList, LB_GETCURSEL, 0, 0);
if (idx != LB_ERR) {
g_Profiles.erase(g_Profiles.begin() + idx); SaveConfig(); PublishSnapshot();
RefreshList(); SetWindowTextW(g_hExe, L"");
SetStatus(hwnd, L"✔ Profile deleted.", StatusKind::Ok);
} else {
SetStatus(hwnd, L"⚠ Select a profile to delete.", StatusKind::Warn);
}
} else if (wmId == 104) { // Test / Confirm
// Two-phase, non-blocking test. If a test is already running,
// this click means "keep it". Otherwise we apply the mode and
// arm the auto-revert countdown.
if (g_TestActive) {
CancelTestTimer(hwnd);
SetWindowTextW(g_hTestBtn, L"Test Display Settings");
SetStatus(hwnd, L"✔ Kept the tested display mode.", StatusKind::Ok);
break;
}
int w = 0, h = 0, hz = 0;
if (!ReadPositiveInt(g_hW, 1, 32767, w) || !ReadPositiveInt(g_hH, 1, 32767, h)) {
SetStatus(hwnd, L"⚠ Enter a valid Width and Height before testing.", StatusKind::Warn);
break;
}
// Hz is optional: blank or 0 means "use the driver default".
{
WCHAR hzbuf[16] = { 0 };
GetWindowTextW(g_hHz, hzbuf, 16);
hz = _wtoi(hzbuf);
if (hz < 0) hz = 0;
}
int idx = SendMessage(g_hDisplayCombo, CB_GETCURSEL, 0, 0);
std::wstring dev = (idx > 0 && idx < (int)g_MonitorDevices.size()) ? g_MonitorDevices[idx] : L"";
DEVMODEW orig = GetCurrentRes(dev);
if (ChangeRes(dev, w, h, hz) == DISP_CHANGE_SUCCESSFUL) {
g_TestOriginal = orig;
g_TestDevice = dev;
g_TestActive = true;
SetWindowTextW(g_hTestBtn, L"Keep This Mode");
SetTimer(hwnd, TIMER_TEST_REVERT, (UINT)TEST_REVERT_SECONDS * 1000, NULL);
std::wstring msg = L"✔ Mode applied — auto-reverts in " +
std::to_wstring(TEST_REVERT_SECONDS) +
L"s. Click \"Keep This Mode\" to keep it.";
SetStatus(hwnd, msg, StatusKind::Info);
MessageBeep(MB_ICONINFORMATION);
} else {
SetStatus(hwnd, L"✖ Monitor does not support this mode.", StatusKind::Error);
MessageBeep(MB_ICONERROR);
}
} else if (wmId == 105) { // Launch App
WCHAR exe[MAX_PATH]; GetWindowTextW(g_hExe, exe, MAX_PATH);
if (wcslen(exe) > 0) ShellExecuteW(NULL, L"open", exe, NULL, NULL, SW_SHOWNORMAL);
} else if (wmId == 106) { // Start with Windows
SetRunAtStartup(SendMessage(g_hStartup, BM_GETCHECK, 0, 0) == BST_CHECKED);
} else if (wmId == 107 && HIWORD(wParam) == CBN_SELCHANGE) { // Preset changed
int idx = SendMessage(g_hPresetCombo, CB_GETCURSEL, 0, 0);
if (idx > 0 && idx < (int)g_Presets.size()) {
g_IgnoreEditChange = true;
SetWindowTextW(g_hW, std::to_wstring(g_Presets[idx].w).c_str());
SetWindowTextW(g_hH, std::to_wstring(g_Presets[idx].h).c_str());
g_IgnoreEditChange = false;
}
} else if ((wmId == 108 || wmId == 109) && HIWORD(wParam) == EN_CHANGE) {
if (!g_IgnoreEditChange) {
SendMessage(g_hPresetCombo, CB_SETCURSEL, 0, 0);
}
} else if (wmId == 112) { // Logging toggle
g_LoggingEnabled = (SendMessage(g_hLogChk, BM_GETCHECK, 0, 0) == BST_CHECKED);
SaveSettings();
if (g_LoggingEnabled) LogMsg(L"Diagnostic logging enabled.");
} else if (wmId == 113) { // Dark mode toggle
g_DarkMode = (SendMessage(g_hDarkChk, BM_GETCHECK, 0, 0) == BST_CHECKED);
SaveSettings();
ApplyTheme(hwnd);
} else if (wmId == 201) { // Tray: open window
ShowWindow(hwnd, SW_RESTORE); SetForegroundWindow(hwnd);
} else if (wmId == 202) { // Tray: open config folder
std::wstring dir = g_IniPath.substr(0, g_IniPath.find_last_of(L'\\'));
if (!dir.empty()) ShellExecuteW(NULL, L"open", dir.c_str(), NULL, NULL, SW_SHOWNORMAL);
} else if (wmId == 203) { // Tray: restore resolution now
if (RestoreActiveIfChanged()) LogMsg(L"User restored resolution from tray.");
} else if (wmId == 200) {
DestroyWindow(hwnd);
}
break;
}
case WM_TIMER: {
if (wParam == TIMER_TEST_REVERT && g_TestActive) {
RestoreRes(g_TestDevice, g_TestOriginal);
g_TestActive = false;
KillTimer(hwnd, TIMER_TEST_REVERT);
SetWindowTextW(g_hTestBtn, L"Test Display Settings");
SetStatus(hwnd, L"↩ Reverted to previous display mode (test not confirmed).", StatusKind::Info);
} else if (wParam == TIMER_CLEAR_STATUS) {
KillTimer(hwnd, TIMER_CLEAR_STATUS);
SetWindowTextW(g_hStatusLabel, L"");