-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
363 lines (301 loc) · 11.2 KB
/
main.cpp
File metadata and controls
363 lines (301 loc) · 11.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
#include "globals.h"
#include "process_monitor.h"
#include "file_watcher.h"
#include "wakatime_client.h"
#include "tray_icon.h"
#include "unity_focus_detector.h"
#include "windows_dark_mode.h"
WakaTimeClient *g_wakatimeClient = nullptr;
FileWatcher *g_fileWatcher = nullptr;
ProcessMonitor *g_processMonitor = nullptr;
TrayIcon *g_trayIcon = nullptr;
UnityFocusDetector *g_unityFocusDetector = nullptr;
std::atomic<bool> g_shouldExit(false);
// Globals 네임스페이스 구현
namespace Globals
{
void Cleanup()
{
g_wakatimeClient = nullptr;
g_fileWatcher = nullptr;
g_processMonitor = nullptr;
g_trayIcon = nullptr;
g_unityFocusDetector = nullptr;
}
}
// 파일 변경 이벤트 처리 - TrayIcon도 업데이트
void OnFileChanged(const FileChangeEvent &event)
{
std::cout << "[HEARTBEAT] " << event.fileName << " (" << event.projectName << ")" << std::endl;
// WakaTime API에 heartbeat 전송
if (g_wakatimeClient)
{
if (g_wakatimeClient->IsInitialized())
{
g_wakatimeClient->SendHeartbeatFromEvent(event);
std::cout << "[HEARTBEAT] ✅ Sent to WakaTime API" << std::endl;
}
else
{
std::cout << "[HEARTBEAT] ⚠️ Skipped - WakaTime client not initialized" << std::endl;
}
}
// 트레이 아이콘 업데이트
if (g_trayIcon)
{
g_trayIcon->IncrementHeartbeats();
g_trayIcon->SetCurrentProject(event.projectName);
}
}
// 트레이 아이콘 콜백 함수들
void OnTrayExit()
{
std::cout << "[Main] Exit requested from tray" << std::endl;
Globals::RequestExit();
}
void OnTrayShowStatus()
{
if (g_trayIcon)
{
g_trayIcon->RefreshStatusMenu();
g_trayIcon->ShowInfoNotification("Status menu updated!");
}
}
void OnTrayToggleMonitoring(const bool enabled)
{
std::cout << "[Main] Monitoring " << (enabled ? "enabled" : "disabled") << std::endl;
if (g_trayIcon)
{
g_trayIcon->SetMonitoringState(enabled);
g_trayIcon->ShowInfoNotification(
enabled ? "Monitoring resumed" : "Monitoring paused"
);
}
}
void OnTrayOpenDashboard()
{
std::cout << "[Main] Opening WakaTime dashboard" << std::endl;
ShellExecuteW(nullptr, L"open", L"https://wakatime.com/dashboard",
nullptr, nullptr, SW_SHOWNORMAL);
}
void OnTrayShowSettings()
{
std::cout << "[Main] Settings requested" << std::endl;
if (g_trayIcon && g_wakatimeClient)
{
std::ostringstream settings;
settings << "API Key: " << g_wakatimeClient->GetMaskedApiKey() << "\n"
<< "Watching " << g_fileWatcher->GetWatchedProjectCount() << " Unity projects";
g_trayIcon->ShowInfoNotification(settings.str());
}
}
void OnApiKeyChanged(const std::string &newApiKey)
{
std::cout << "[Main] API Key changed, updating WakaTime client..." << std::endl;
if (g_wakatimeClient)
{
const bool success = g_wakatimeClient->ReInitialize(newApiKey);
if (g_trayIcon)
{
if (success)
{
g_trayIcon->ShowInfoNotification("✅ API Key saved and client reinitialized!");
g_trayIcon->RefreshStatusMenu();
std::cout << "[Main] ✅ WakaTime client successfully reinitialized" << std::endl;
}
else
{
g_trayIcon->ShowErrorNotification("❌ Failed to initialize with new API key");
std::cerr << "[Main] ❌ WakaTime client reinitialization failed" << std::endl;
}
}
}
}
void OnUnityFocusHeartbeat()
{
if (!g_fileWatcher || !g_wakatimeClient) return;
if (!g_wakatimeClient->IsInitialized()) return;
const auto& watchedProjects = g_fileWatcher->GetWatchedProjects();
if (watchedProjects.empty()) return;
const auto& project = watchedProjects[0];
g_wakatimeClient->SendHeartbeat(
project.projectPath,
project.projectName,
project.unityVersion,
false
);
if (g_trayIcon)
{
g_trayIcon->IncrementHeartbeats();
g_trayIcon->SetCurrentProject(project.projectName);
}
}
void HandleNewUnityInstances(const std::vector<UnityInstance> &newInstances)
{
for (const auto &instance: newInstances)
{
std::cout << "[Main] New Unity instance detected: " << instance.projectName
<< " (Unity " << instance.editorVersion << ")" << std::endl;
if (g_fileWatcher && g_fileWatcher->StartWatching(instance.projectPath, instance.projectName, instance.editorVersion))
{
std::cout << "[Main] Started watching: " << instance.projectName << std::endl;
if (g_trayIcon)
{
g_trayIcon->SetCurrentProject(instance.projectName);
g_trayIcon->ShowInfoNotification("New Unity project: " + instance.projectName +
" (Unity " + instance.editorVersion + ")");
}
}
else
{
std::cout << "[Main] Failed to start watching: " << instance.projectName << std::endl;
}
}
}
void HandleClosedUnityInstances(const std::vector<UnityInstance> &closedInstances)
{
for (const auto &instance: closedInstances)
{
std::cout << "[Main] Unity instance closed: " << instance.projectName << std::endl;
if (g_fileWatcher)
{
g_fileWatcher->StopWatching(instance.projectPath);
}
if (g_trayIcon)
{
g_trayIcon->ShowInfoNotification("Unity project closed: " + instance.projectName);
// 다른 활성 프로젝트로 전환
if (g_fileWatcher)
{
if (const auto& remainingProjects = g_fileWatcher->GetWatchedProjects(); !remainingProjects.empty())
{
const auto& projectName = remainingProjects[0].projectName;
g_trayIcon->SetCurrentProject(projectName);
std::cout << "[Main] Switched to remaining project: " << projectName << std::endl;
} else
{
g_trayIcon->SetCurrentProject(""); // 모든 프로젝트 종료
std::cout << "[Main] No Unity projects are being watched" << std::endl;
}
}
}
}
}
void InitialUnityProjectScan()
{
if (!g_processMonitor || !g_fileWatcher) return;
std::cout << "[Main] Performing initial Unity project scan..." << std::endl;
const auto& instances = g_processMonitor->ScanUnityProcesses();
if (instances.empty())
{
std::cout << "[Main] No Unity processes found during initial scan" << std::endl;
return;
}
for (const auto &instance: instances)
{
if (g_fileWatcher->StartWatching(instance.projectPath, instance.projectName, instance.editorVersion))
{
std::cout << "[Main] ✅ Started watching: " << instance.projectName
<< " (Unity " << instance.editorVersion << ")" << std::endl;
if (g_trayIcon)
{
g_trayIcon->SetCurrentProject(instance.projectName);
}
}
}
std::cout << "[Main] Initial scan complete. Watching " << instances.size() << " Unity projects" << std::endl;
}
int main()
{
std::cout << "[Main] Unity WakaTime Monitor Starting..." << std::endl;
const bool darkModeAvailable = WindowsDarkMode::EnableForApp();
std::cout << "[Main] Dark mode menu opt-in: "
<< (darkModeAvailable ? "enabled" : "not available") << std::endl;
TrayIcon trayIcon;
g_trayIcon = &trayIcon;
if (!trayIcon.Initialize("Unity WakaTime"))
{
std::cerr << "[Main] Failed to initialize tray icon!" << std::endl;
return 1;
}
// 트레이 콜백 설정
trayIcon.SetExitCallback(OnTrayExit);
trayIcon.SetShowStatusCallback(OnTrayShowStatus);
trayIcon.SetToggleMonitoringCallback(OnTrayToggleMonitoring);
trayIcon.SetOpenDashboardCallback(OnTrayOpenDashboard);
trayIcon.SetShowSettingsCallback(OnTrayShowSettings);
trayIcon.SetApiKeyChangeCallback(OnApiKeyChanged);
trayIcon.ShowInfoNotification("Unity WakaTime started !");
WakaTimeClient wakatimeClient;
g_wakatimeClient = &wakatimeClient;
if (!wakatimeClient.Initialize())
{
std::cerr << "[Main] Failed to initialize WakaTime client!" << std::endl;
trayIcon.ShowErrorNotification("WakaTime client not initialized. Click 'Setup API Key' in menu.");
}
ProcessMonitor processMonitor;
FileWatcher fileWatcher;
g_processMonitor = &processMonitor;
g_fileWatcher = &fileWatcher;
fileWatcher.SetChangeCallback(OnFileChanged);
UnityFocusDetector unityFocusDetector;
g_unityFocusDetector = &unityFocusDetector;
unityFocusDetector.SetFocusCallback(OnUnityFocusHeartbeat);
unityFocusDetector.SetUnfocusCallback([]() {});
unityFocusDetector.SetPeriodicHeartbeatCallback(OnUnityFocusHeartbeat);
InitialUnityProjectScan();
trayIcon.SetMonitoringState(true);
std::cout << "\n[Main] Unity WakaTime is now running in background!" << std::endl;
auto lastScan = std::chrono::steady_clock::now();
const auto scanInterval = std::chrono::seconds(10);
while (!Globals::ShouldExit())
{
if (g_fileWatcher)
{
g_fileWatcher->DrainPendingEvents();
}
if (g_unityFocusDetector) {
g_unityFocusDetector->CheckFocused();
g_unityFocusDetector->SendPeriodicHeartbeat();
}
int msgCount = trayIcon.ProcessMessages();
if (msgCount > 5) std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 많은 메시지 → 빠른 처리
else if (msgCount > 0) std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 일부 메시지 → 보통 처리
else std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // 메시지 없음 → 여유 있게
if (auto now = std::chrono::steady_clock::now(); now - lastScan >= scanInterval)
{
// 새로운 Unity 인스턴스 감지
if (auto newInstances = processMonitor.GetNewInstances(); !newInstances.empty())
{
HandleNewUnityInstances(newInstances);
}
// 종료된 Unity 인스턴스 감지
if (auto closedInstances = processMonitor.GetClosedInstances(); !closedInstances.empty())
{
HandleClosedUnityInstances(closedInstances);
}
lastScan = now;
}
}
std::cout << "\n[Main] Shutting down Unity WakaTime..." << std::endl;
trayIcon.ShowInfoNotification("Unity WakaTime shutting down...");
// 남은 heartbeat 전송
if (g_wakatimeClient)
{
if (g_fileWatcher)
{
g_fileWatcher->DrainPendingEvents(2048);
}
std::cout << "[Main] Flushing remaining heartbeats..." << std::endl;
wakatimeClient.FlushQueue();
}
// 모든 파일 감시 중지
if (g_fileWatcher)
{
std::cout << "[Main] Stopping all file watchers..." << std::endl;
g_fileWatcher->StopAllWatching();
}
Globals::Cleanup();
std::cout << "[Main] Unity WakaTime stopped gracefully." << std::endl;
return 0;
}