-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStatTracker.cpp
More file actions
165 lines (139 loc) · 4.5 KB
/
Copy pathStatTracker.cpp
File metadata and controls
165 lines (139 loc) · 4.5 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
namespace StatTracker
{
static ULONGLONG uStartTick = 0;
static UInt32 uSavedTotalSeconds;
static ULONGLONG uLastSavedTick = 0;
constexpr UInt32 OUR_TIMER = 0x1041;
int ReadTime()
{
return GetPrivateProfileIntA("Stats", "uTime", 0, IniPath);
}
void WriteTime(UInt32 uiTimeSeconds)
{
char value[32];
snprintf(value, sizeof(value), "%d", uiTimeSeconds);
WritePrivateProfileStringA("Stats", "uTime", value, IniPath);
}
void IncreaseSessionCounter()
{
int uiSessions = GetPrivateProfileIntA("Stats", "uSessions", 0, IniPath);
++uiSessions;
char value[0x10];
snprintf(value, sizeof(value), "%d", uiSessions);
WritePrivateProfileStringA("Stats", "uSessions", value, IniPath);
}
void SetupTimer(HWND hMainWindow)
{
uSavedTotalSeconds = ReadTime();
uStartTick = GetTickCount64();
uLastSavedTick = uStartTick;
SetTimer(hMainWindow, OUR_TIMER, 1000, nullptr);
}
static bool bShowTotalTime = false;
void UpdateStatusBarText(HWND hStatusBar)
{
if (!config.iSessionTimeMode)
{
return;
}
bool bSessionTimeByDefault = config.iSessionTimeMode == 1;
ULONGLONG now = GetTickCount64();
ULONGLONG sessionTime = (now - uStartTick) / 1000;
ULONGLONG totalTime = uSavedTotalSeconds + sessionTime;
ULONGLONG displayTime = (bShowTotalTime ^ bSessionTimeByDefault) ? sessionTime : totalTime;
char buf[0x10];
snprintf(buf, sizeof(buf), "%02llu:%02llu:%02llu",
displayTime / 3600,
(displayTime % 3600) / 60,
displayTime % 60);
SendMessageA(hStatusBar, SB_SETTEXTA, 4, (LPARAM)buf);
}
WNDPROC OldStatusProc;
LRESULT CALLBACK StatusBarProc(
HWND hStatusBar,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_MOUSEMOVE:
{
POINT pt;
pt.x = (SHORT)LOWORD(lParam);
pt.y = (SHORT)HIWORD(lParam);
RECT rc;
SendMessageA(hStatusBar, SB_GETRECT, 4, (LPARAM)&rc);
bool bHoveringTime = PtInRect(&rc, pt);
if (bHoveringTime != bShowTotalTime)
{
bShowTotalTime = bHoveringTime;
UpdateStatusBarText(hStatusBar);
}
TRACKMOUSEEVENT tme = {};
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hStatusBar;
TrackMouseEvent(&tme);
break;
}
case WM_MOUSELEAVE:
bShowTotalTime = false;
UpdateStatusBarText(hStatusBar);
break;
}
return CallWindowProcA(OldStatusProc, hStatusBar, msg, wParam, lParam);
}
void SetupStatusBar(HWND hStatusBar)
{
if (!config.iSessionTimeMode)
{
return;
}
RECT rc;
GetClientRect(hStatusBar, &rc);
int kParts[] = { 150, 225, 500, rc.right - 100, -1 };
SendMessageA(hStatusBar, SB_SETPARTS, 5, (LPARAM)kParts);
if (!OldStatusProc)
{
OldStatusProc = (WNDPROC)SetWindowLongPtrA(hStatusBar, GWLP_WNDPROC, (LONG_PTR)StatusBarProc);
}
}
void Callback(HWND hMainWindow, UINT Msg, WPARAM wParam, LPARAM lParam)
{
HWND hStatusBar = MainWindow::GetStatusBar();
switch (Msg)
{
case WM_CREATE:
IncreaseSessionCounter();
SetupTimer(hMainWindow);
SetupStatusBar(hStatusBar);
break;
case WM_SIZE:
SetupStatusBar(hStatusBar);
break;
case WM_TIMER:
if (wParam == OUR_TIMER)
{
ULONGLONG now = GetTickCount64();
ULONGLONG sessionTime = (now - uStartTick) / 1000;
ULONGLONG totalTime = uSavedTotalSeconds + sessionTime;
// Save every minute.
if (now - uLastSavedTick >= 60'000)
{
uLastSavedTick += 60'000;
WriteTime(static_cast<UInt32>(totalTime));
}
UpdateStatusBarText(hStatusBar);
}
break;
case WM_CLOSE:
{
ULONGLONG now = GetTickCount64();
ULONGLONG sessionTime = (now - uStartTick) / 1000;
WriteTime(static_cast<UInt32>(uSavedTotalSeconds + sessionTime));
break;
}
}
}
}