]> gitweb.ps.run Git - autorec/blob - src/main.cpp
a59041103694d92ca1d4623e5f220ff14ec7fdd5
[autorec] / src / main.cpp
1 #ifndef WIN32\r
2   #define WIN32\r
3 #endif\r
4 #include <WinSock2.h>\r
5 #include "win.h"\r
6 #include "ws.h"\r
7 \r
8 #define LAY_IMPLEMENTATION\r
9 #include "layout.h"\r
10 \r
11 #include <Psapi.h>\r
12 #include <stdbool.h>\r
13 #include <stdio.h>\r
14 #include <string.h>\r
15 #include <windows.h>\r
16 #include <shlwapi.h>\r
17 \r
18 void\r
19 startRecording()\r
20 {\r
21   ws::sendRequest("StartRecord");\r
22 }\r
23 \r
24 void\r
25 stopRecording()\r
26 {\r
27   ws::sendRequest("StopRecord");\r
28 }\r
29 \r
30 bool\r
31 checkProcessRunning(HANDLE handle)\r
32 {\r
33   DWORD exit_code;\r
34 \r
35   if (handle != NULL && GetExitCodeProcess(handle, &exit_code)) {\r
36     return exit_code == STILL_ACTIVE;\r
37   }\r
38 \r
39   return false;\r
40 }\r
41 \r
42 HANDLE\r
43 getHwndProcess(HWND hwnd)\r
44 {\r
45   DWORD processId, threadId = GetWindowThreadProcessId(hwnd, &processId);\r
46   return OpenProcess(PROCESS_QUERY_INFORMATION, false, processId);\r
47 }\r
48 \r
49 // HWND getProcessHwnd(HANDLE handle) {}\r
50 \r
51 bool\r
52 checkFullscreenWindow()\r
53 {\r
54   HWND desktopHwnd = GetDesktopWindow();\r
55   HWND fgHwnd = GetForegroundWindow();\r
56 \r
57   if (fgHwnd != desktopHwnd && fgHwnd != GetShellWindow()) {\r
58     RECT windowRect, desktopRect;\r
59     // Get Window and Desktop size\r
60     GetWindowRect(fgHwnd, &windowRect);\r
61     GetWindowRect(desktopHwnd, &desktopRect);\r
62 \r
63     bool fullscreen = windowRect.bottom == desktopRect.bottom &&\r
64                       windowRect.top == desktopRect.top &&\r
65                       windowRect.left == desktopRect.left &&\r
66                       windowRect.right == desktopRect.right;\r
67 \r
68     return fullscreen;\r
69   }\r
70 \r
71   return false;\r
72 }\r
73 \r
74 bool\r
75 checkForegroundProcess(std::string exeName)\r
76 {\r
77   HWND fgHwnd = GetForegroundWindow();\r
78   HANDLE fgHandle = getHwndProcess(fgHwnd);\r
79 \r
80   char filename[1024];\r
81   int len = GetModuleFileNameExA(fgHandle, NULL, filename, 1024);\r
82   PathStripPathA(filename);\r
83 \r
84   return strcmp(filename, exeName.c_str()) == 0;\r
85 }\r
86 \r
87 bool recording = false;\r
88 HANDLE process = NULL;\r
89 \r
90 int WINAPI\r
91 WinMain(HINSTANCE hInstance,\r
92         HINSTANCE hPrevInstance,\r
93         LPSTR lpCmdLine,\r
94         int nCmdShow)\r
95 {\r
96 \r
97   HWND window = win::Window("Title", "MyWindowClass", hInstance);\r
98 \r
99   lay_id row1 = lay_item(&win::_::ctx);\r
100   lay_insert(&win::_::ctx, win::_::root, row1);\r
101   lay_set_size_xy(&win::_::ctx, row1, 0, 25);\r
102   lay_set_behave(&win::_::ctx, row1, LAY_LEFT);\r
103   lay_set_contain(&win::_::ctx, row1, LAY_ROW);\r
104   lay_set_margins_ltrb(&win::_::ctx, row1, 5, 5, 5, 5);\r
105   lay_id row2 = lay_item(&win::_::ctx);\r
106   lay_insert(&win::_::ctx, win::_::root, row2);\r
107   lay_set_size_xy(&win::_::ctx, row2, 0, 0);\r
108   lay_set_behave(&win::_::ctx, row2, LAY_FILL);\r
109   lay_set_contain(&win::_::ctx, row2, LAY_ROW);\r
110   lay_set_margins_ltrb(&win::_::ctx, row2, 5, 5, 5, 5);\r
111   lay_id col1 = lay_item(&win::_::ctx);\r
112   lay_set_size_xy(&win::_::ctx, col1, 80, 0);\r
113   lay_set_behave(&win::_::ctx, col1, LAY_VCENTER);\r
114   lay_set_contain(&win::_::ctx, col1, LAY_COLUMN);\r
115   lay_set_margins_ltrb(&win::_::ctx, col1, 5, 0, 5, 0);\r
116 \r
117   HWND cbWindowTitle = win::CheckBox(window, "Window Title", row1, 100, 25, 0, 0);\r
118   HWND cbFullscreenWindow = win::CheckBox(window, "Any Fullscreen Application", row1, 200, 25, 0, 0);\r
119 \r
120   HWND btnConnect = win::Button(window, "Connect", row1, 100, 25, 0, 0);\r
121   win::Callback(btnConnect, BN_CLICKED, [&]() {\r
122     ws::connect("ws://127.0.0.1:4444");\r
123   });\r
124 \r
125   win::Callback(cbWindowTitle, BN_CLICKED, [&]() {\r
126     SendMessageA(cbWindowTitle, BM_SETCHECK, SendMessageA(cbWindowTitle, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);\r
127   });\r
128   win::Callback(cbFullscreenWindow, BN_CLICKED, [&]() {\r
129     SendMessageA(cbFullscreenWindow, BM_SETCHECK, SendMessageA(cbFullscreenWindow, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);\r
130   });\r
131 \r
132   HWND lstActiveProcesses = win::ListView(window, row2, 0, 0, 0, LAY_FILL);\r
133   lay_insert(&win::_::ctx, row2, col1);\r
134   HWND lstMonitoredProcesses = win::ListBox(window, row2, 0, 0, 0, LAY_FILL);\r
135   win::AddStyle(lstActiveProcesses, WS_VSCROLL);\r
136   win::AddStyle(lstMonitoredProcesses, WS_VSCROLL);\r
137 \r
138   HWND btnUpdateWindows = win::Button(window, "Update", col1, 85, 25, 0, 0);\r
139   HWND btnStartMonitoringName = win::Button(window, "Exe name >>", col1, 85, 25, 0, 0);\r
140   HWND btnStartMonitoringPath = win::Button(window, "Full path >>", col1, 85, 25, 0, 0);\r
141   HWND btnStopMonitoring = win::Button(window, "Remove", col1, 85, 25, 0, 0);\r
142   win::Callback(btnUpdateWindows, BN_CLICKED, [&]() {\r
143     win::ListClear(lstActiveProcesses);\r
144     for (HWND hwnd = GetTopWindow(NULL); hwnd != nullptr;\r
145          hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)) {\r
146       if (!IsWindowVisible(hwnd))\r
147         continue;\r
148 \r
149       RECT rect;\r
150       GetWindowRect(hwnd, &rect);\r
151 \r
152       char str[1024];\r
153       if (GetModuleFileNameExA(getHwndProcess(hwnd), 0, str, 1024) != 0 &&\r
154           win::ListFindString(lstActiveProcesses, str) == LB_ERR) {\r
155         win::ListAddString(lstActiveProcesses, str);\r
156       }\r
157     }\r
158   });\r
159   win::Callback(btnStartMonitoringName, BN_CLICKED, [&]() {\r
160     int sel = win::ListGetSelectedIndex(lstActiveProcesses);\r
161     if (sel < 0) return;\r
162 \r
163     std::string selStr = win::ListGetText(lstActiveProcesses, sel);\r
164     \r
165     char *filename = new char[selStr.size()];\r
166     std::memcpy(filename, selStr.c_str(), selStr.size());\r
167     PathStripPathA(filename);\r
168 \r
169     if (win::ListFindString(lstMonitoredProcesses, std::string(filename)) == LB_ERR)\r
170       win::ListAddString(lstMonitoredProcesses, std::string(filename));\r
171 \r
172     delete[] filename;\r
173   });\r
174   win::Callback(btnStartMonitoringPath, BN_CLICKED, [&]() {\r
175     int sel = win::ListGetSelectedIndex(lstActiveProcesses);\r
176     if (sel < 0) return;\r
177     std::string selStr = win::ListGetText(lstActiveProcesses, sel);\r
178     if (win::ListFindString(lstMonitoredProcesses, selStr) == LB_ERR)\r
179     win::ListAddString(lstMonitoredProcesses, selStr);\r
180   });\r
181   win::Callback(btnStopMonitoring, BN_CLICKED, [&]() {\r
182     int sel = win::ListGetSelectedIndex(lstMonitoredProcesses);\r
183     if (sel < 0) return;\r
184     win::ListRemove(lstMonitoredProcesses, sel);\r
185   });\r
186 \r
187   win::ShowWindow(window);\r
188 \r
189   ws::init();\r
190 \r
191   SetTimer(window, 10123, 100, [](HWND, UINT, UINT_PTR, DWORD) {\r
192     if (!recording) {\r
193       if (checkForegroundProcess("League of Legends.exe")) {\r
194         recording = true;\r
195         process = getHwndProcess(GetForegroundWindow());\r
196         startRecording();\r
197       }\r
198     } else {\r
199       if (!checkProcessRunning(process)) {\r
200         recording = false;\r
201         process = NULL;\r
202         stopRecording();\r
203       }\r
204     }\r
205   });\r
206 \r
207   while (win::UpdateWindow(window)) {\r
208     ws::update();\r
209   }\r
210 }\r