Repository navigation
Expand file tree
/
Copy pathtimetrack.wh.cpp
More file actions
891 lines (765 loc) · 27.7 KB
/
Copy pathtimetrack.wh.cpp
File metadata and controls
891 lines (765 loc) · 27.7 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
// ==WindhawkMod==
// @id timetrack
// @name TimeTrack
// @description Digital wellbeing & daily screen time monitor for Windows
// @version 1.0.0
// @author Dev-StudioCode
// @github https://github.com/Dev-StudioCode
// @include explorer.exe
// @architecture x86-64
// @compilerOptions -lpsapi -lshlwapi -lshell32 -lole32
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
# TimeTrack
An ultra-lightweight, native digital wellbeing and active screen time monitor for Windows.
Runs seamlessly inside Windows Explorer via Windhawk with zero background telemetry, minimal memory usage, and no external runtime dependencies.
### Features
* **Active Screen Time:** Measures genuine foreground screen time per application.
* **Smart Inactivity Detection:** Automatically pauses tracking when you step away from keyboard/mouse or lock your screen (`Win + L`).
* **Per-App Duration & Focus Counts:** Records active duration and window switch counts for every app.
* **Modern App Resolution:** Properly resolves Windows 10/11 UWP apps hosted in `ApplicationFrameHost.exe`.
* **Native System Tray Integration:** Real-time tooltip status and right-click menu with top 5 applications.
* **Atomic Local Storage:** Persists daily metrics to clean JSON files locally on your computer with automatic midnight rollover.
* **Zero Overhead:** Native C++ design using negligible RAM (< 4 MB) and 0% CPU.
* **100% Private:** Operates strictly offline with zero network requests.
*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
/*
- idleThresholdSeconds: 120
$name: Inactivity timeout (seconds)
$description: Stop accumulating screen time when no keyboard or mouse activity is detected for this duration.
- saveIntervalSeconds: 30
$name: Auto-save interval (seconds)
$description: Frequency in seconds to persist daily stats to disk.
- showTrayIcon: true
$name: Show System Tray Icon
$description: Display the TimeTrack tray icon in the Windows taskbar notification area.
*/
// ==/WindhawkModSettings==
#include <psapi.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <strsafe.h>
#include <windows.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <fstream>
#include <mutex>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <windhawk_api.h>
// ---------------------------------------------------------------------------
// Constants & IDs
// ---------------------------------------------------------------------------
#define WM_TRAYICON (WM_USER + 101)
#define ID_TIMER_TICK 1001
#define ID_TRAY_TITLE 2001
#define ID_TRAY_TOTAL_TIME 2002
#define ID_TRAY_ACTIVE_APP 2003
#define ID_TRAY_SEPARATOR 2004
#define ID_TRAY_TOP_APP_START 2100
#define ID_TRAY_OPEN_FOLDER 2201
#define ID_TRAY_RESET_TODAY 2202
// ---------------------------------------------------------------------------
// Structures
// ---------------------------------------------------------------------------
struct ModSettings {
int idleThresholdSeconds = 120;
int saveIntervalSeconds = 30;
bool showTrayIcon = true;
};
struct AppUsageRecord {
std::wstring exeName;
uint32_t activeSeconds = 0;
uint32_t focusCount = 0;
};
struct DailyStats {
std::wstring dateString; // YYYY-MM-DD
uint32_t totalScreenTimeSeconds = 0;
std::unordered_map<std::wstring, AppUsageRecord> apps;
};
// ---------------------------------------------------------------------------
// Global State
// ---------------------------------------------------------------------------
static ModSettings g_settings;
static std::mutex g_settingsMutex;
static DailyStats g_dailyStats;
static std::mutex g_statsMutex;
static std::wstring g_currentActiveApp = L"";
static bool g_isUserIdle = false;
static WCHAR g_storageDirectory[MAX_PATH] = {0};
static WCHAR g_todayJsonPath[MAX_PATH] = {0};
static WCHAR g_historyDirectory[MAX_PATH] = {0};
static std::atomic<bool> g_stopRequested(false);
static HANDLE g_hWorkerThread = NULL;
static HWND g_hTrayWnd = NULL;
static NOTIFYICONDATAW g_nid = {0};
static bool g_trayIconAdded = false;
// ---------------------------------------------------------------------------
// Helper Functions: Date & Time Formatting
// ---------------------------------------------------------------------------
static std::wstring GetCurrentDateString() {
SYSTEMTIME st;
GetLocalTime(&st);
WCHAR buffer[32];
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%04d-%02d-%02d", st.wYear,
st.wMonth, st.wDay);
return std::wstring(buffer);
}
static std::wstring GetCurrentTimestampString() {
SYSTEMTIME st;
GetLocalTime(&st);
WCHAR buffer[64];
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%04d-%02d-%02d %02d:%02d:%02d",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute,
st.wSecond);
return std::wstring(buffer);
}
static std::wstring FormatDuration(uint32_t totalSeconds,
bool includeSeconds = false) {
uint32_t hours = totalSeconds / 3600;
uint32_t minutes = (totalSeconds % 3600) / 60;
uint32_t seconds = totalSeconds % 60;
WCHAR buffer[64];
if (hours > 0) {
if (includeSeconds) {
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%uh %um %us", hours,
minutes, seconds);
} else {
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%uh %um", hours, minutes);
}
} else if (minutes > 0) {
if (includeSeconds) {
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%um %us", minutes, seconds);
} else {
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%um", minutes);
}
} else {
StringCchPrintfW(buffer, ARRAYSIZE(buffer), L"%us", seconds);
}
return std::wstring(buffer);
}
// ---------------------------------------------------------------------------
// JSON Escape helper
// ---------------------------------------------------------------------------
static std::wstring EscapeJsonString(const std::wstring &input) {
std::wstring out;
out.reserve(input.size() + 8);
for (WCHAR ch : input) {
switch (ch) {
case L'\"':
out += L"\\\"";
break;
case L'\\':
out += L"\\\\";
break;
case L'\b':
out += L"\\b";
break;
case L'\f':
out += L"\\f";
break;
case L'\n':
out += L"\\n";
break;
case L'\r':
out += L"\\r";
break;
case L'\t':
out += L"\\t";
break;
default:
if (ch < 32) {
WCHAR hexBuf[8];
StringCchPrintfW(hexBuf, ARRAYSIZE(hexBuf), L"\\u%04x", ch);
out += hexBuf;
} else {
out += ch;
}
break;
}
}
return out;
}
// ---------------------------------------------------------------------------
// Storage Management
// ---------------------------------------------------------------------------
static void InitializeStoragePaths() {
WCHAR basePath[MAX_PATH] = {0};
// Try Windhawk's official storage path first
size_t chars = Wh_GetModStoragePath(basePath, ARRAYSIZE(basePath));
if (chars == 0 || basePath[0] == L'\0') {
// Fallback to %LocalAppData%\TimeTrack
WCHAR localAppData[MAX_PATH] = {0};
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0,
localAppData))) {
PathCombineW(basePath, localAppData, L"TimeTrack");
} else {
StringCchCopyW(basePath, ARRAYSIZE(basePath), L"C:\\TimeTrack");
}
}
StringCchCopyW(g_storageDirectory, ARRAYSIZE(g_storageDirectory), basePath);
CreateDirectoryW(g_storageDirectory, NULL);
PathCombineW(g_todayJsonPath, g_storageDirectory, L"timetrack_today.json");
PathCombineW(g_historyDirectory, g_storageDirectory, L"history");
CreateDirectoryW(g_historyDirectory, NULL);
}
// ---------------------------------------------------------------------------
// JSON Persistence: Save
// ---------------------------------------------------------------------------
static void SaveDailyStatsToFile(const std::wstring &targetPath) {
std::lock_guard<std::mutex> lock(g_statsMutex);
std::vector<AppUsageRecord> sortedApps;
sortedApps.reserve(g_dailyStats.apps.size());
for (const auto &pair : g_dailyStats.apps) {
sortedApps.push_back(pair.second);
}
std::sort(sortedApps.begin(), sortedApps.end(),
[](const AppUsageRecord &a, const AppUsageRecord &b) {
return a.activeSeconds > b.activeSeconds;
});
std::wostringstream ss;
ss << L"{\n";
ss << L" \"date\": \"" << EscapeJsonString(g_dailyStats.dateString)
<< L"\",\n";
ss << L" \"totalScreenTimeSeconds\": " << g_dailyStats.totalScreenTimeSeconds
<< L",\n";
ss << L" \"totalScreenTimeFormatted\": \""
<< EscapeJsonString(
FormatDuration(g_dailyStats.totalScreenTimeSeconds, true))
<< L"\",\n";
ss << L" \"lastUpdated\": \""
<< EscapeJsonString(GetCurrentTimestampString()) << L"\",\n";
ss << L" \"appCount\": " << sortedApps.size() << L",\n";
ss << L" \"apps\": [\n";
for (size_t i = 0; i < sortedApps.size(); ++i) {
const auto &app = sortedApps[i];
ss << L" {\n";
ss << L" \"exe\": \"" << EscapeJsonString(app.exeName) << L"\",\n";
ss << L" \"seconds\": " << app.activeSeconds << L",\n";
ss << L" \"formatted\": \""
<< EscapeJsonString(FormatDuration(app.activeSeconds)) << L"\",\n";
ss << L" \"focusCount\": " << app.focusCount << L"\n";
ss << L" }" << (i + 1 < sortedApps.size() ? L"," : L"") << L"\n";
}
ss << L" ]\n";
ss << L"}\n";
std::wstring jsonContent = ss.str();
// Write atomically using a temporary file
WCHAR tempPath[MAX_PATH];
StringCchPrintfW(tempPath, ARRAYSIZE(tempPath), L"%s.tmp",
targetPath.c_str());
HANDLE hFile = CreateFileW(tempPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
// UTF-8 conversion
int utf8Length = WideCharToMultiByte(CP_UTF8, 0, jsonContent.c_str(), -1,
NULL, 0, NULL, NULL);
if (utf8Length > 0) {
std::vector<char> utf8Buffer(utf8Length);
WideCharToMultiByte(CP_UTF8, 0, jsonContent.c_str(), -1,
utf8Buffer.data(), utf8Length, NULL, NULL);
DWORD bytesWritten = 0;
WriteFile(hFile, utf8Buffer.data(), (DWORD)utf8Length - 1, &bytesWritten,
NULL);
}
CloseHandle(hFile);
// Atomic replace
MoveFileExW(tempPath, targetPath.c_str(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
}
}
// ---------------------------------------------------------------------------
// JSON Persistence: Load
// ---------------------------------------------------------------------------
static void LoadTodayStatsFromFile() {
std::wstring todayStr = GetCurrentDateString();
HANDLE hFile = CreateFileW(g_todayJsonPath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::lock_guard<std::mutex> lock(g_statsMutex);
g_dailyStats.dateString = todayStr;
g_dailyStats.totalScreenTimeSeconds = 0;
g_dailyStats.apps.clear();
return;
}
DWORD fileSize = GetFileSize(hFile, NULL);
if (fileSize == 0 || fileSize == INVALID_FILE_SIZE) {
CloseHandle(hFile);
std::lock_guard<std::mutex> lock(g_statsMutex);
g_dailyStats.dateString = todayStr;
return;
}
std::vector<char> buffer(fileSize + 1, 0);
DWORD bytesRead = 0;
ReadFile(hFile, buffer.data(), fileSize, &bytesRead, NULL);
CloseHandle(hFile);
int wideLen = MultiByteToWideChar(CP_UTF8, 0, buffer.data(), -1, NULL, 0);
if (wideLen <= 0)
return;
std::vector<WCHAR> wideBuffer(wideLen);
MultiByteToWideChar(CP_UTF8, 0, buffer.data(), -1, wideBuffer.data(),
wideLen);
std::wstring json(wideBuffer.data());
// Basic, safe parser for our own schema
std::lock_guard<std::mutex> lock(g_statsMutex);
// Parse date
size_t datePos = json.find(L"\"date\": \"");
if (datePos != std::wstring::npos) {
size_t start = datePos + 9;
size_t end = json.find(L"\"", start);
if (end != std::wstring::npos) {
std::wstring fileDate = json.substr(start, end - start);
if (fileDate != todayStr) {
// Different day! Archive yesterday's file and start fresh
WCHAR archivePath[MAX_PATH];
PathCombineW(archivePath, g_historyDirectory,
(fileDate + L".json").c_str());
MoveFileExW(g_todayJsonPath, archivePath, MOVEFILE_REPLACE_EXISTING);
g_dailyStats.dateString = todayStr;
g_dailyStats.totalScreenTimeSeconds = 0;
g_dailyStats.apps.clear();
return;
}
}
}
g_dailyStats.dateString = todayStr;
// Parse totalScreenTimeSeconds
size_t secPos = json.find(L"\"totalScreenTimeSeconds\": ");
if (secPos != std::wstring::npos) {
size_t start = secPos + 26;
size_t end = json.find_first_of(L",\n\r", start);
if (end != std::wstring::npos) {
std::wstring secStr = json.substr(start, end - start);
g_dailyStats.totalScreenTimeSeconds = (uint32_t)_wtoi(secStr.c_str());
}
}
// Parse apps
size_t appsArrayPos = json.find(L"\"apps\": [");
if (appsArrayPos != std::wstring::npos) {
size_t curPos = appsArrayPos;
while (true) {
size_t exePos = json.find(L"\"exe\": \"", curPos);
if (exePos == std::wstring::npos)
break;
size_t exeStart = exePos + 8;
size_t exeEnd = json.find(L"\"", exeStart);
if (exeEnd == std::wstring::npos)
break;
std::wstring exeName = json.substr(exeStart, exeEnd - exeStart);
uint32_t seconds = 0;
size_t sPos = json.find(L"\"seconds\": ", exeEnd);
if (sPos != std::wstring::npos) {
size_t sStart = sPos + 11;
size_t sEnd = json.find_first_of(L",\n\r", sStart);
if (sEnd != std::wstring::npos) {
seconds = (uint32_t)_wtoi(json.substr(sStart, sEnd - sStart).c_str());
}
}
uint32_t focusCount = 1;
size_t fcPos = json.find(L"\"focusCount\": ", exeEnd);
if (fcPos != std::wstring::npos) {
size_t fcStart = fcPos + 14;
size_t fcEnd = json.find_first_of(L",\n\r}", fcStart);
if (fcEnd != std::wstring::npos) {
focusCount =
(uint32_t)_wtoi(json.substr(fcStart, fcEnd - fcStart).c_str());
}
}
AppUsageRecord record;
record.exeName = exeName;
record.activeSeconds = seconds;
record.focusCount = focusCount;
g_dailyStats.apps[exeName] = record;
curPos = exeEnd;
}
}
}
// ---------------------------------------------------------------------------
// Active Window & Process Identification
// ---------------------------------------------------------------------------
static std::wstring GetActiveProcessExecutableName() {
HWND hwnd = GetForegroundWindow();
if (!hwnd || !IsWindow(hwnd)) {
return L"";
}
// Skip shell/desktop if desired or tag it cleanly
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == 0) {
return L"";
}
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (!hProcess) {
return L"";
}
WCHAR fullPath[MAX_PATH];
DWORD pathSize = ARRAYSIZE(fullPath);
std::wstring exeName = L"";
if (QueryFullProcessImageNameW(hProcess, 0, fullPath, &pathSize)) {
PCWSTR fileName = PathFindFileNameW(fullPath);
if (fileName && *fileName) {
exeName = fileName;
}
}
CloseHandle(hProcess);
// If hosted inside UWP ApplicationFrameHost, resolve real child process
// (e.g., Calculator, Settings)
if (_wcsicmp(exeName.c_str(), L"ApplicationFrameHost.exe") == 0) {
HWND hChild =
FindWindowExW(hwnd, NULL, L"Windows.UI.Core.CoreWindow", NULL);
if (hChild) {
DWORD childPid = 0;
GetWindowThreadProcessId(hChild, &childPid);
if (childPid != 0 && childPid != pid) {
HANDLE hChildProc =
OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, childPid);
if (hChildProc) {
WCHAR childPath[MAX_PATH];
DWORD childSize = ARRAYSIZE(childPath);
if (QueryFullProcessImageNameW(hChildProc, 0, childPath,
&childSize)) {
PCWSTR childFileName = PathFindFileNameW(childPath);
if (childFileName && *childFileName) {
exeName = childFileName;
}
}
CloseHandle(hChildProc);
}
}
}
}
return exeName;
}
// ---------------------------------------------------------------------------
// User Inactivity Detection
// ---------------------------------------------------------------------------
static bool CheckIsUserIdle(int thresholdSeconds) {
// 1. Check if desktop is locked or on secure desktop (Win + L)
HDESK hDesk = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
if (!hDesk) {
return true; // Workstation locked or secure desktop active
}
CloseDesktop(hDesk);
// 2. Check mouse/keyboard inactivity
LASTINPUTINFO lii = {sizeof(LASTINPUTINFO)};
if (!GetLastInputInfo(&lii)) {
return false;
}
DWORD currentTick = GetTickCount();
DWORD idleMs = currentTick - lii.dwTime;
return idleMs >= (DWORD)(thresholdSeconds * 1000);
}
// ---------------------------------------------------------------------------
// Tracking Tick (Executed every second)
// ---------------------------------------------------------------------------
static void OnTrackingTick() {
ModSettings settings;
{
std::lock_guard<std::mutex> lock(g_settingsMutex);
settings = g_settings;
}
// 1. Day rollover check
std::wstring currentDate = GetCurrentDateString();
{
std::lock_guard<std::mutex> lock(g_statsMutex);
if (g_dailyStats.dateString.empty()) {
g_dailyStats.dateString = currentDate;
} else if (g_dailyStats.dateString != currentDate) {
// Archive previous day
WCHAR archivePath[MAX_PATH];
PathCombineW(archivePath, g_historyDirectory,
(g_dailyStats.dateString + L".json").c_str());
SaveDailyStatsToFile(archivePath);
// Reset for today
g_dailyStats.dateString = currentDate;
g_dailyStats.totalScreenTimeSeconds = 0;
g_dailyStats.apps.clear();
}
}
// 2. Idle check
bool idle = CheckIsUserIdle(settings.idleThresholdSeconds);
g_isUserIdle = idle;
if (idle) {
return; // User is away: do not count towards screen time
}
// 3. Active application tracking
std::wstring activeExe = GetActiveProcessExecutableName();
if (activeExe.empty()) {
return;
}
{
std::lock_guard<std::mutex> lock(g_statsMutex);
g_dailyStats.totalScreenTimeSeconds++;
auto &record = g_dailyStats.apps[activeExe];
if (record.exeName.empty()) {
record.exeName = activeExe;
record.activeSeconds = 0;
record.focusCount = 1;
}
if (activeExe != g_currentActiveApp) {
record.focusCount++;
g_currentActiveApp = activeExe;
}
record.activeSeconds++;
}
}
// ---------------------------------------------------------------------------
// Tray Notification & Menu
// ---------------------------------------------------------------------------
static void UpdateTrayTooltip() {
if (!g_trayIconAdded || !g_hTrayWnd)
return;
uint32_t totalSeconds = 0;
std::wstring activeApp = L"None";
{
std::lock_guard<std::mutex> lock(g_statsMutex);
totalSeconds = g_dailyStats.totalScreenTimeSeconds;
if (!g_currentActiveApp.empty()) {
activeApp = g_currentActiveApp;
}
}
std::wstring durationStr = FormatDuration(totalSeconds);
WCHAR tip[128];
if (g_isUserIdle) {
StringCchPrintfW(tip, ARRAYSIZE(tip), L"TimeTrack: %s (Idle)\nActive: %s",
durationStr.c_str(), activeApp.c_str());
} else {
StringCchPrintfW(tip, ARRAYSIZE(tip), L"TimeTrack: %s\nActive: %s",
durationStr.c_str(), activeApp.c_str());
}
StringCchCopyW(g_nid.szTip, ARRAYSIZE(g_nid.szTip), tip);
Shell_NotifyIconW(NIM_MODIFY, &g_nid);
}
static void ShowTrayContextMenu(HWND hWnd) {
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
if (!hMenu)
return;
uint32_t totalSeconds = 0;
std::wstring activeApp = L"None";
std::vector<AppUsageRecord> sortedApps;
{
std::lock_guard<std::mutex> lock(g_statsMutex);
totalSeconds = g_dailyStats.totalScreenTimeSeconds;
activeApp = g_currentActiveApp.empty() ? L"None" : g_currentActiveApp;
for (const auto &pair : g_dailyStats.apps) {
sortedApps.push_back(pair.second);
}
}
std::sort(sortedApps.begin(), sortedApps.end(),
[](const AppUsageRecord &a, const AppUsageRecord &b) {
return a.activeSeconds > b.activeSeconds;
});
// 1. Title Item (Bold Header)
AppendMenuW(hMenu, MF_STRING | MF_DISABLED, ID_TRAY_TITLE,
L"⏱️ TimeTrack - Daily Screen Time");
// 2. Total Screen Time
WCHAR totalBuf[128];
StringCchPrintfW(totalBuf, ARRAYSIZE(totalBuf), L" Today: %s",
FormatDuration(totalSeconds, true).c_str());
AppendMenuW(hMenu, MF_STRING | MF_DISABLED, ID_TRAY_TOTAL_TIME, totalBuf);
// 3. Active App
WCHAR activeBuf[128];
StringCchPrintfW(activeBuf, ARRAYSIZE(activeBuf), L" Active: %s",
activeApp.c_str());
AppendMenuW(hMenu, MF_STRING | MF_DISABLED, ID_TRAY_ACTIVE_APP, activeBuf);
AppendMenuW(hMenu, MF_SEPARATOR, ID_TRAY_SEPARATOR, NULL);
// 4. Top Apps List
if (!sortedApps.empty()) {
AppendMenuW(hMenu, MF_STRING | MF_DISABLED, 0, L"📊 Top Apps Today:");
size_t displayCount = (std::min)((size_t)5, sortedApps.size());
for (size_t i = 0; i < displayCount; ++i) {
WCHAR appBuf[128];
StringCchPrintfW(appBuf, ARRAYSIZE(appBuf), L" %zu. %s (%s)", i + 1,
sortedApps[i].exeName.c_str(),
FormatDuration(sortedApps[i].activeSeconds).c_str());
AppendMenuW(hMenu, MF_STRING | MF_DISABLED,
(UINT_PTR)(ID_TRAY_TOP_APP_START + i), appBuf);
}
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
}
// 5. Actions
AppendMenuW(hMenu, MF_STRING, ID_TRAY_OPEN_FOLDER, L"📁 Open Data Folder");
AppendMenuW(hMenu, MF_STRING, ID_TRAY_RESET_TODAY, L"🔄 Reset Today's Stats");
SetForegroundWindow(hWnd);
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_BOTTOMALIGN, pt.x, pt.y, 0, hWnd,
NULL);
PostMessageW(hWnd, WM_NULL, 0, 0);
DestroyMenu(hMenu);
}
// ---------------------------------------------------------------------------
// Tray Window Procedure
// ---------------------------------------------------------------------------
static LRESULT CALLBACK TrayWndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam) {
switch (uMsg) {
case WM_TIMER:
if (wParam == ID_TIMER_TICK) {
OnTrackingTick();
static int saveCounter = 0;
static int tooltipCounter = 0;
tooltipCounter++;
if (tooltipCounter >= 3) {
tooltipCounter = 0;
UpdateTrayTooltip();
}
saveCounter++;
int saveInterval = 30;
{
std::lock_guard<std::mutex> lock(g_settingsMutex);
saveInterval = g_settings.saveIntervalSeconds;
}
if (saveCounter >= saveInterval) {
saveCounter = 0;
SaveDailyStatsToFile(g_todayJsonPath);
}
}
return 0;
case WM_TRAYICON:
if (lParam == WM_RBUTTONUP || lParam == WM_LBUTTONUP) {
ShowTrayContextMenu(hWnd);
}
return 0;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_TRAY_OPEN_FOLDER:
ShellExecuteW(NULL, L"open", g_storageDirectory, NULL, NULL,
SW_SHOWNORMAL);
break;
case ID_TRAY_RESET_TODAY:
if (MessageBoxW(
hWnd,
L"Are you sure you want to reset today's screen time statistics?",
L"TimeTrack - Confirm Reset",
MB_YESNO | MB_ICONQUESTION) == IDYES) {
{
std::lock_guard<std::mutex> lock(g_statsMutex);
g_dailyStats.totalScreenTimeSeconds = 0;
g_dailyStats.apps.clear();
}
SaveDailyStatsToFile(g_todayJsonPath);
UpdateTrayTooltip();
}
break;
}
return 0;
case WM_DESTROY:
if (g_trayIconAdded) {
Shell_NotifyIconW(NIM_DELETE, &g_nid);
g_trayIconAdded = false;
}
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
// ---------------------------------------------------------------------------
// Worker Thread
// ---------------------------------------------------------------------------
static DWORD WINAPI TimeTrackWorkerThread(LPVOID) {
WNDCLASSEXW wc = {sizeof(WNDCLASSEXW)};
wc.lpfnWndProc = TrayWndProc;
wc.hInstance = GetModuleHandleW(NULL);
wc.lpszClassName = L"TimeTrackTrayWndClass";
RegisterClassExW(&wc);
g_hTrayWnd =
CreateWindowExW(0, wc.lpszClassName, L"TimeTrackTrayWnd", WS_POPUP, 0, 0,
0, 0, NULL, NULL, wc.hInstance, NULL);
if (!g_hTrayWnd) {
Wh_Log(L"Failed to create TimeTrack message window");
return 1;
}
// Add tray icon
bool showIcon = true;
{
std::lock_guard<std::mutex> lock(g_settingsMutex);
showIcon = g_settings.showTrayIcon;
}
if (showIcon) {
ZeroMemory(&g_nid, sizeof(g_nid));
g_nid.cbSize = sizeof(NOTIFYICONDATAW);
g_nid.hWnd = g_hTrayWnd;
g_nid.uID = 1;
g_nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
g_nid.uCallbackMessage = WM_TRAYICON;
g_nid.hIcon = LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
StringCchCopyW(g_nid.szTip, ARRAYSIZE(g_nid.szTip),
L"TimeTrack: Starting...");
if (Shell_NotifyIconW(NIM_ADD, &g_nid)) {
g_trayIconAdded = true;
}
}
// Set 1-second interval timer
SetTimer(g_hTrayWnd, ID_TIMER_TICK, 1000, NULL);
// Initial tooltip update
UpdateTrayTooltip();
// Message loop
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
KillTimer(g_hTrayWnd, ID_TIMER_TICK);
UnregisterClassW(wc.lpszClassName, wc.hInstance);
return 0;
}
// ---------------------------------------------------------------------------
// Settings Loader
// ---------------------------------------------------------------------------
static void LoadModSettings() {
std::lock_guard<std::mutex> lock(g_settingsMutex);
g_settings.idleThresholdSeconds = Wh_GetIntSetting(L"idleThresholdSeconds");
if (g_settings.idleThresholdSeconds <= 0) {
g_settings.idleThresholdSeconds = 120;
}
g_settings.saveIntervalSeconds = Wh_GetIntSetting(L"saveIntervalSeconds");
if (g_settings.saveIntervalSeconds <= 0) {
g_settings.saveIntervalSeconds = 30;
}
g_settings.showTrayIcon = Wh_GetIntSetting(L"showTrayIcon") != 0;
}
// ---------------------------------------------------------------------------
// Windhawk Lifecycle Callbacks
// ---------------------------------------------------------------------------
BOOL Wh_ModInit() {
Wh_Log(L"TimeTrack initializing...");
LoadModSettings();
InitializeStoragePaths();
LoadTodayStatsFromFile();
g_stopRequested = false;
g_hWorkerThread = CreateThread(NULL, 0, TimeTrackWorkerThread, NULL, 0, NULL);
if (!g_hWorkerThread) {
Wh_Log(L"Failed to launch TimeTrack background worker thread");
return FALSE;
}
Wh_Log(L"TimeTrack initialized successfully. Tracking active screen time.");
return TRUE;
}
void Wh_ModUninit() {
Wh_Log(L"TimeTrack uninitializing...");
g_stopRequested = true;
if (g_hTrayWnd) {
PostMessageW(g_hTrayWnd, WM_CLOSE, 0, 0);
}
if (g_hWorkerThread) {
WaitForSingleObject(g_hWorkerThread, 3000);
CloseHandle(g_hWorkerThread);
g_hWorkerThread = NULL;
}
// Final flush to disk
SaveDailyStatsToFile(g_todayJsonPath);
Wh_Log(L"TimeTrack uninitialized.");
}
BOOL Wh_ModSettingsChanged(BOOL *bReload) {
Wh_Log(L"TimeTrack settings changed.");
*bReload = TRUE;
return TRUE;
}