-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenTime.cpp
More file actions
128 lines (109 loc) · 3.47 KB
/
ScreenTime.cpp
File metadata and controls
128 lines (109 loc) · 3.47 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
#include "ScreenTime.h"
#include <iostream>
#include <fstream>
#include <thread>
#include <filesystem>
ScreenTimeAPI::ScreenTimeAPI(const std::string& filename)
: outputFilename(filename), lastApp("Unknown"),
lastSwitchTime(std::chrono::steady_clock::now())
{
}
std::string ScreenTimeAPI::getActiveWindowTitle()
{
#ifdef _WIN32
HWND hwnd = GetForegroundWindow();
if (hwnd)
{
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (processHandle)
{
char processName[MAX_PATH];
if (GetModuleBaseNameA(processHandle, NULL, processName, sizeof(processName)))
{
CloseHandle(processHandle);
return std::string(processName);
}
CloseHandle(processHandle);
}
}
#endif
return "Unknown";
}
std::string ScreenTimeAPI::getCurrentDayMonthYear()
{
std::time_t now = std::time(nullptr);
std::tm* localTime = std::localtime(&now);
char buffer[16];
std::strftime(buffer, sizeof(buffer), "%d-%m-%Y", localTime);
return std::string(buffer);
}
void ScreenTimeAPI::writeToJson()
{
Json::Value root;
std::ifstream inputFile(outputFilename, std::ios::in);
if (inputFile.is_open())
{
Json::CharReaderBuilder reader;
JSONCPP_STRING errs;
if (!Json::parseFromStream(reader, inputFile, &root, &errs))
{
std::cerr << "Error parsing existing JSON: " << errs << std::endl;
}
inputFile.close();
}
std::string currentDayMonthYear = getCurrentDayMonthYear();
for (const auto& [app, time] : appTimes)
{
root[currentDayMonthYear][app] = root[currentDayMonthYear][app].asInt() + time;
}
std::ofstream outputFile(outputFilename, std::ios::out | std::ios::trunc);
if (outputFile.is_open())
{
Json::StreamWriterBuilder writer;
outputFile << Json::writeString(writer, root);
outputFile.close();
}
else
{
std::cerr << "Unable to open file for writing." << std::endl;
}
}
void ScreenTimeAPI::trackScreenTime()
{
const auto saveInterval = std::chrono::seconds(5);
auto lastSaveTime = std::chrono::steady_clock::now();
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string currentApp = getActiveWindowTitle();
auto now = std::chrono::steady_clock::now();
if (currentApp.empty() || currentApp == "Unknown")
{
continue;
}
if (currentApp != lastApp)
{
int duration = std::chrono::duration_cast<std::chrono::seconds>(now - lastSwitchTime).count();
if (!lastApp.empty() && lastApp != "Unknown")
{
appTimes[lastApp] += duration;
}
lastApp = currentApp;
lastSwitchTime = now;
}
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSaveTime) >= saveInterval)
{
int currentDuration = std::chrono::duration_cast<std::chrono::seconds>(now - lastSwitchTime).count();
if (!currentApp.empty() && currentApp != "Unknown")
{
appTimes[currentApp] += currentDuration;
}
writeToJson();
lastSaveTime = now;
appTimes.clear();
lastSwitchTime = now;
}
}
}