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