4 #include <WinSock2.h>
\r
8 #define LAY_IMPLEMENTATION
\r
16 #include <stdbool.h>
\r
19 #include <windows.h>
\r
20 #include <shlwapi.h>
\r
22 bool recording = false;
\r
23 HANDLE process = NULL;
\r
25 HINSTANCE hInstance;
\r
26 HICON icon_white, icon_green, icon_red;
\r
28 std::vector<std::string> gameExes;
\r
31 NOTIFYICONDATAA niData = { 0 };
\r
32 const UINT ICON_MSG = WM_APP+1;
\r
34 ShowNotificationIcon()
\r
36 Shell_NotifyIconA(NIM_ADD, &niData);
\r
37 Shell_NotifyIconA(NIM_SETVERSION, &niData);
\r
41 HideNotificationIcon()
\r
43 Shell_NotifyIconA(NIM_DELETE, &niData);
\r
46 void changeIcon(HWND hwnd, HICON icon)
\r
48 niData.hIcon = icon;
\r
49 if (! IsWindowVisible(hwnd))
\r
50 Shell_NotifyIconA(NIM_MODIFY, &niData);
\r
51 SendMessage(hwnd, WM_SETICON, 0, (LPARAM)icon);
\r
52 SendMessage(hwnd, WM_SETICON, 1, (LPARAM)icon);
\r
60 ws::sendRequest("StartRecord");
\r
61 changeIcon(hwnd, icon_green);
\r
67 ws::sendRequest("StopRecord");
\r
68 changeIcon(hwnd, icon_red);
\r
72 checkProcessRunning(HANDLE handle)
\r
76 if (handle != NULL && GetExitCodeProcess(handle, &exit_code)) {
\r
77 return exit_code == STILL_ACTIVE;
\r
84 getHwndProcess(HWND hwnd)
\r
86 DWORD processId, threadId = GetWindowThreadProcessId(hwnd, &processId);
\r
87 return OpenProcess(PROCESS_QUERY_INFORMATION, false, processId);
\r
90 // HWND getProcessHwnd(HANDLE handle) {}
\r
93 checkFullscreenWindow()
\r
95 HWND desktopHwnd = GetDesktopWindow();
\r
96 HWND fgHwnd = GetForegroundWindow();
\r
98 if (fgHwnd != desktopHwnd && fgHwnd != GetShellWindow()) {
\r
99 RECT windowRect, desktopRect;
\r
100 // Get Window and Desktop size
\r
101 GetWindowRect(fgHwnd, &windowRect);
\r
102 GetWindowRect(desktopHwnd, &desktopRect);
\r
104 bool fullscreen = windowRect.bottom == desktopRect.bottom &&
\r
105 windowRect.top == desktopRect.top &&
\r
106 windowRect.left == desktopRect.left &&
\r
107 windowRect.right == desktopRect.right;
\r
116 checkForegroundProcess(std::string exeName)
\r
118 HWND fgHwnd = GetForegroundWindow();
\r
119 HANDLE fgHandle = getHwndProcess(fgHwnd);
\r
121 char filename[MAX_PATH];
\r
122 int len = GetModuleFileNameExA(fgHandle, NULL, filename, MAX_PATH);
\r
124 if (strcmp(filename, exeName.c_str()) == 0)
\r
127 PathStripPathA(filename);
\r
129 return strcmp(filename, exeName.c_str()) == 0;
\r
132 void ReadGameExes()
\r
134 std::ifstream ifs("games.txt");
\r
140 std::getline(ifs, str);
\r
142 gameExes.push_back(str);
\r
148 void WriteGameExes()
\r
150 std::ofstream ofs("games.txt", std::ios::trunc);
\r
152 for (const auto &exe : gameExes)
\r
154 ofs.write(exe.c_str(), exe.size());
\r
155 ofs.write("\n", 1);
\r
163 - Disconnect while recording
\r
169 WinMain(HINSTANCE hInstance,
\r
170 HINSTANCE hPrevInstance,
\r
173 //int main(int argc, char **argv)
\r
175 hInstance = GetModuleHandle(0);
\r
177 icon_white = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_WHITE));
\r
178 icon_green = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_GREEN));
\r
179 icon_red = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_ICON_RED));
\r
181 win::Window window("Title", "MyWindowClass", hInstance);
\r
182 hwnd = window.hwnd;
\r
184 niData.cbSize = sizeof(niData);
\r
185 niData.uID = 12345;
\r
186 niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
\r
187 niData.hIcon = icon_white;
\r
188 niData.hWnd = window.hwnd;
\r
189 niData.uCallbackMessage = ICON_MSG;
\r
190 niData.uVersion = NOTIFYICON_VERSION_4;
\r
192 window.handlers[WM_SIZE].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
193 if (wParam == SIZE_MINIMIZED) {
\r
194 ShowNotificationIcon();
\r
195 ShowWindow(hwnd, false);
\r
198 window.handlers[WM_DESTROY].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
199 HideNotificationIcon();
\r
201 window.handlers[ICON_MSG].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
202 if (LOWORD(lParam) == NIN_SELECT) {
\r
203 HideNotificationIcon();
\r
204 ShowWindow(hwnd, true);
\r
205 SetForegroundWindow(hwnd);
\r
206 SetActiveWindow(hwnd);
\r
210 window.handlers[WM_GETMINMAXINFO].push_back([](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
211 MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
\r
212 mmInfo->ptMinTrackSize.x = 400;
\r
213 mmInfo->ptMinTrackSize.y = 200;
\r
216 lay_context *ctx = &window.ctx;
\r
217 lay_id root = window.lId;
\r
219 lay_id row1 = win::createLayId(&window.ctx, window.lId, 0, 25, LAY_ROW, LAY_LEFT);
\r
220 lay_set_margins_ltrb(ctx, row1, 5, 5, 5, 5);
\r
221 lay_id row2 = win::createLayId(&window.ctx, window.lId, 0, 0, LAY_ROW, LAY_FILL);
\r
222 lay_set_margins_ltrb(ctx, row2, 5, 5, 5, 5);
\r
224 win::CheckBox cbWindowTitle(&window, "Window Title", row1, 100, 25, 0, 0);
\r
225 win::CheckBox cbFullscreenWindow(&window, "Any Fullscreen Application", row1, 200, 25, 0, 0);
\r
227 win::Button btnConnect(&window, "Connect", row1, 100, 25, 0, 0);
\r
228 btnConnect.onClick([&]() {
\r
229 if (! ws::isConnected)
\r
230 ws::connect("ws://127.0.0.1:4455");
\r
233 win::ListBox lstActiveProcesses(&window, row2, 0, 0, 0, LAY_FILL);
\r
235 lay_id col1 = win::createLayId(&window.ctx, row2, 80, 0, LAY_COLUMN, LAY_VCENTER);
\r
236 lstActiveProcesses.addStyle(WS_VSCROLL);
\r
238 lay_set_margins_ltrb(ctx, col1, 5, 0, 5, 0);
\r
240 win::ListBox lstMonitoredProcesses(&window, row2, 0, 0, 0, LAY_FILL);
\r
241 lstMonitoredProcesses.addStyle(WS_VSCROLL);
\r
244 for (const auto &exe : gameExes)
\r
245 lstMonitoredProcesses.addString(exe);
\r
247 win::Button btnUpdateWindows(&window, "Update", col1, 85, 25, 0, 0);
\r
248 win::Button btnStartMonitoringName(&window, "Exe name >>", col1, 85, 25, 0, 0);
\r
249 win::Button btnStartMonitoringPath(&window, "Full path >>", col1, 85, 25, 0, 0);
\r
250 win::Button btnStopMonitoring(&window, "Remove", col1, 85, 25, 0, 0);
\r
251 btnUpdateWindows.onClick([&]() {
\r
252 lstActiveProcesses.clear();
\r
253 for (HWND hwnd = GetTopWindow(NULL); hwnd != nullptr;
\r
254 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)) {
\r
255 if (!IsWindowVisible(hwnd))
\r
259 GetWindowRect(hwnd, &rect);
\r
262 if (GetModuleFileNameExA(getHwndProcess(hwnd), 0, str, 1024) != 0 &&
\r
263 lstActiveProcesses.findString(str) == LB_ERR) {
\r
264 lstActiveProcesses.addString(str);
\r
268 btnStartMonitoringName.onClick([&]() {
\r
269 int sel = lstActiveProcesses.getSelectedIndex();
\r
270 if (sel < 0) return;
\r
272 std::string selStr = lstActiveProcesses.getText(sel);
\r
274 char *filename = new char[selStr.size()+1];
\r
275 std::memcpy(filename, selStr.c_str(), selStr.size());
\r
276 filename[selStr.size()] = '\0';
\r
277 PathStripPathA(filename);
\r
279 std::string filenameStr(filename);
\r
281 if (lstMonitoredProcesses.findString(filenameStr) == LB_ERR)
\r
283 lstMonitoredProcesses.addString(filenameStr);
\r
284 gameExes.push_back(filenameStr);
\r
290 btnStartMonitoringPath.onClick([&]() {
\r
291 int sel = lstActiveProcesses.getSelectedIndex();
\r
292 if (sel < 0) return;
\r
293 std::string selStr = lstActiveProcesses.getText(sel);
\r
294 if (lstMonitoredProcesses.findString(selStr) == LB_ERR)
\r
296 lstMonitoredProcesses.addString(selStr);
\r
297 gameExes.push_back(selStr);
\r
301 btnStopMonitoring.onClick([&]() {
\r
302 int sel = lstMonitoredProcesses.getSelectedIndex();
\r
303 if (sel < 0) return;
\r
304 lstMonitoredProcesses.remove(sel);
\r
305 gameExes.erase(gameExes.begin() + sel);
\r
310 window.setDefaultFont();
\r
312 ws::onConnect = [&]() {
\r
313 changeIcon(window.hwnd, icon_red);
\r
314 btnConnect.setActive(false);
\r
316 ws::onClose = [&]() {
\r
317 changeIcon(window.hwnd, icon_white);
\r
318 btnConnect.setActive(true);
\r
320 ws::onError = ws::onClose;
\r
323 window.setTimer(1000, [&btnConnect]() {
\r
324 if (! ws::isConnected)
\r
325 ws::connect("ws://127.0.0.1:4455");
\r
328 window.setTimer(100, []() {
\r
330 for (auto exe : gameExes) {
\r
331 if (checkForegroundProcess(exe)) {
\r
333 process = getHwndProcess(GetForegroundWindow());
\r
338 if (!checkProcessRunning(process)) {
\r
346 while (window.update()) {
\r