]> gitweb.ps.run Git - autorec/commitdiff
MVP
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Sun, 9 Jan 2022 20:31:23 +0000 (21:31 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Sun, 9 Jan 2022 20:31:23 +0000 (21:31 +0100)
build.cmd
src/main.cpp
src/win.h
src/ws.h

index 43b0f6c82034809ca17e9e1abf462ee5094ea7bc..cf6d9e9bff7b2cdc041eacf09eaed85e6e6d46d6 100644 (file)
--- a/build.cmd
+++ b/build.cmd
@@ -1,2 +1,5 @@
-cl /EHsc src/main.cpp src/mongoose.c /I ../libs/websocketpp/ /I ../libs/asio/asio/include /link user32.lib gdi32.lib shell32.lib Shlwapi.lib ws2_32.lib\r
+\r
+REM cl src/mongoose.c -c\r
+\r
+cl /EHsc src/main.cpp mongoose.obj /link user32.lib gdi32.lib shell32.lib Shlwapi.lib ws2_32.lib\r
 mt -manifest .\main.exe.manifest -outputresource:main.exe;1
\ No newline at end of file
index f4a47f314e3b446aaebaebfd7b57e4887be8625a..5056acf844a0b702d4de597004f95c7909942eda 100644 (file)
 void\r
 startRecording()\r
 {\r
-  MessageBoxA(NULL, "Start", "Start Start", MB_OK);\r
+  ws::sendRequest("StartRecord");\r
 }\r
 \r
 void\r
 stopRecording()\r
 {\r
-  MessageBoxA(NULL, "Stop", "Stop Stop", MB_OK);\r
+  ws::sendRequest("StopRecord");\r
 }\r
 \r
 bool\r
@@ -74,17 +74,20 @@ checkFullscreenWindow()
 }\r
 \r
 bool\r
-checkNotepadWindow()\r
+checkForegroundProcess(std::string exeName)\r
 {\r
   HWND fgHwnd = GetForegroundWindow();\r
   HANDLE fgHandle = getHwndProcess(fgHwnd);\r
 \r
   char filename[1024];\r
   int len = GetModuleFileNameExA(fgHandle, NULL, filename, 1024);\r
+  PathStripPathA(filename);\r
 \r
-  return strcmp(filename, "C:\\Windows\\System32\\notepad.exe") == 0;\r
+  return strcmp(filename, exeName.c_str()) == 0;\r
 }\r
 \r
+bool recording = false;\r
+HANDLE process = NULL;\r
 \r
 int WINAPI\r
 WinMain(HINSTANCE hInstance,\r
@@ -92,8 +95,6 @@ WinMain(HINSTANCE hInstance,
         LPSTR lpCmdLine,\r
         int nCmdShow)\r
 {\r
-  bool recording = false;\r
-  HANDLE process = NULL;\r
 \r
   HWND window = win::Window("Title", "MyWindowClass", hInstance);\r
 \r
@@ -108,6 +109,7 @@ WinMain(HINSTANCE hInstance,
   lay_set_size_xy(&win::_::ctx, row2, 0, 0);\r
   lay_set_behave(&win::_::ctx, row2, LAY_FILL);\r
   lay_set_contain(&win::_::ctx, row2, LAY_ROW);\r
+  lay_set_margins_ltrb(&win::_::ctx, row2, 5, 5, 5, 5);\r
   lay_id col1 = lay_item(&win::_::ctx);\r
   lay_set_size_xy(&win::_::ctx, col1, 80, 0);\r
   lay_set_behave(&win::_::ctx, col1, LAY_VCENTER);\r
@@ -121,11 +123,6 @@ WinMain(HINSTANCE hInstance,
   win::Callback(btnConnect, BN_CLICKED, [&]() {\r
     ws::connect("ws://127.0.0.1:4444");\r
   });\r
-  \r
-  HWND btnIdentify = win::Button(window, "Identify", row1, 100, 25, 0, 0);\r
-  win::Callback(btnIdentify, BN_CLICKED, [&]() {\r
-    ws::identify();\r
-  });\r
 \r
   win::Callback(cbWindowTitle, BN_CLICKED, [&]() {\r
     SendMessageA(cbWindowTitle, BM_SETCHECK, SendMessageA(cbWindowTitle, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);\r
@@ -134,7 +131,7 @@ WinMain(HINSTANCE hInstance,
     SendMessageA(cbFullscreenWindow, BM_SETCHECK, SendMessageA(cbFullscreenWindow, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);\r
   });\r
 \r
-  HWND lstActiveProcesses = win::ListBox(window, row2, 0, 0, 0, LAY_FILL);\r
+  HWND lstActiveProcesses = win::ListView(window, row2, 0, 0, 0, LAY_FILL);\r
   lay_insert(&win::_::ctx, row2, col1);\r
   HWND lstMonitoredProcesses = win::ListBox(window, row2, 0, 0, 0, LAY_FILL);\r
   win::AddStyle(lstActiveProcesses, WS_VSCROLL);\r
@@ -189,28 +186,27 @@ WinMain(HINSTANCE hInstance,
     win::ListRemove(lstMonitoredProcesses, sel);\r
   });\r
 \r
-\r
-  win::ShowNotificationIcon(hInstance, window);\r
-\r
   win::ShowWindow(window);\r
 \r
   ws::init();\r
 \r
-  while (win::UpdateWindow(window)) {\r
-    ws::update();\r
-\r
+  SetTimer(window, 10123, 100, [](HWND, UINT, UINT_PTR, DWORD) {\r
     if (!recording) {\r
-      if (checkNotepadWindow()) {\r
+      if (checkForegroundProcess("League of Legends.exe")) {\r
+        recording = true;\r
         process = getHwndProcess(GetForegroundWindow());\r
         startRecording();\r
-        recording = true;\r
       }\r
     } else {\r
       if (!checkProcessRunning(process)) {\r
+        recording = false;\r
         process = NULL;\r
         stopRecording();\r
-        recording = false;\r
       }\r
     }\r
+  });\r
+\r
+  while (win::UpdateWindow(window)) {\r
+    ws::update();\r
   }\r
 }\r
index 65f25952f09dc965b459fb37e6239fe681856e05..9d21f680bf7001dd3d8ee09ecdbe0135e951b2d4 100644 (file)
--- a/src/win.h
+++ b/src/win.h
@@ -14,46 +14,24 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
 using std::string;\r
 \r
 namespace win {\r
-struct Length\r
-{\r
-  int pixel;\r
-  float percent;\r
-};\r
-struct Size\r
-{\r
-  Length x, y, w, h;\r
-  int getX(HWND hwnd)\r
-  {\r
-    RECT rect;\r
-    GetClientRect(hwnd, &rect);\r
-    return x.percent * (rect.right - rect.left) + x.pixel;\r
-  }\r
-  int getY(HWND hwnd)\r
-  {\r
-    RECT rect;\r
-    GetClientRect(hwnd, &rect);\r
-    return y.percent * (rect.bottom - rect.top) + y.pixel;\r
-  }\r
-  int getW(HWND hwnd)\r
-  {\r
-    RECT rect;\r
-    GetClientRect(hwnd, &rect);\r
-    return w.percent * (rect.right - rect.left) + w.pixel;\r
-  }\r
-  int getH(HWND hwnd, int _w, int _h)\r
-  {\r
-    RECT rect;\r
-    GetWindowRect(hwnd, &rect);\r
-    return h.percent * (rect.bottom - rect.top) + h.pixel;\r
-  }\r
-};\r
-\r
 namespace _ {\r
 using CallbackFn = std::function<void()>;\r
 std::map<HWND, std::map<WORD, CallbackFn>> handlers;\r
 std::map<HWND, lay_id> lIds;\r
 \r
 NOTIFYICONDATA niData = { 0 };\r
+void\r
+ShowNotificationIcon()\r
+{\r
+  Shell_NotifyIconA(NIM_ADD, &_::niData);\r
+  Shell_NotifyIconA(NIM_SETVERSION, &_::niData);\r
+}\r
+\r
+void\r
+HideNotificationIcon()\r
+{\r
+  Shell_NotifyIconA(NIM_DELETE, &_::niData);\r
+}\r
 \r
 lay_context ctx;\r
 lay_id root;\r
@@ -72,9 +50,8 @@ WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
       break;\r
     case WM_SIZE:\r
       if (wParam == SIZE_MINIMIZED) {\r
+        ShowNotificationIcon();\r
         ShowWindow(hwnd, false);\r
-        SetForegroundWindow(hwnd);\r
-        SetActiveWindow(hwnd);\r
       }\r
       else {\r
         lay_set_size_xy(&_::ctx, _::root, LOWORD(lParam), HIWORD(lParam));\r
@@ -105,8 +82,12 @@ WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     case WM_NOTIFY:\r
       break;\r
     case WM_APP + 1:\r
-      if (LOWORD(lParam) == NIN_SELECT)\r
+      if (LOWORD(lParam) == NIN_SELECT) {\r
+        HideNotificationIcon();\r
         ShowWindow(hwnd, true);\r
+        SetForegroundWindow(hwnd);\r
+        SetActiveWindow(hwnd);\r
+      }\r
       break;\r
     case WM_CTLCOLORSTATIC:\r
       return (LONG)GetStockObject(WHITE_BRUSH);\r
@@ -123,20 +104,6 @@ WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 }\r
 }\r
 \r
-void\r
-ShowNotificationIcon(HINSTANCE hInstance, HWND hwnd)\r
-{\r
-  _::niData.cbSize = sizeof(_::niData);\r
-  _::niData.uID = 12345;\r
-  _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;\r
-  _::niData.hIcon = LoadIconA(nullptr, IDI_WINLOGO);\r
-  _::niData.hWnd = hwnd;\r
-  _::niData.uCallbackMessage = WM_APP+1;\r
-  _::niData.uVersion = NOTIFYICON_VERSION_4;\r
-\r
-  Shell_NotifyIconA(NIM_ADD, &_::niData);\r
-  Shell_NotifyIconA(NIM_SETVERSION, &_::niData);\r
-}\r
 \r
 void\r
 Callback(HWND hwnd, WORD ev, std::function<void()> cb)\r
@@ -165,9 +132,8 @@ Window(string title, string className, HINSTANCE hInstance)
   lay_init_context(&_::ctx);\r
   _::root = lay_item(&_::ctx);\r
   lay_set_contain(&_::ctx, _::root, LAY_COLUMN);\r
-  lay_set_margins_ltrb(&_::ctx, _::root, 5, 5, 5, 5);\r
 \r
-  return CreateWindowA(className.c_str(),\r
+  HWND result = CreateWindowA(className.c_str(),\r
                        title.c_str(),\r
                        WS_OVERLAPPEDWINDOW,\r
                        CW_USEDEFAULT,\r
@@ -178,6 +144,16 @@ Window(string title, string className, HINSTANCE hInstance)
                        nullptr,\r
                        hInstance,\r
                        nullptr);\r
+                       \r
+  _::niData.cbSize = sizeof(_::niData);\r
+  _::niData.uID = 12345;\r
+  _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;\r
+  _::niData.hIcon = LoadIconA(nullptr, IDI_WINLOGO);\r
+  _::niData.hWnd = result;\r
+  _::niData.uCallbackMessage = WM_APP+1;\r
+  _::niData.uVersion = NOTIFYICON_VERSION_4;\r
+\r
+  return result;\r
 }\r
 \r
 bool\r
@@ -288,6 +264,28 @@ void ListRemove(HWND hwnd, int index)
   SendMessageA(hwnd, LB_DELETESTRING, index, 0);\r
 }\r
 \r
+HWND\r
+ListView(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+{\r
+  lay_id lId = lay_item(&_::ctx);\r
+  lay_insert(&_::ctx, parent, lId);\r
+  lay_set_size_xy(&_::ctx, lId, w, h);\r
+  lay_set_contain(&_::ctx, lId, contain);\r
+  lay_set_behave(&_::ctx, lId, behave);\r
+\r
+  HWND result = CreateWindowExA(0,\r
+                         WC_LISTVIEWA,\r
+                         "",\r
+                         WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,\r
+                         0, 0, 0, 0,\r
+                         hwnd,\r
+                         nullptr,\r
+                         nullptr,\r
+                         nullptr);\r
+  _::lIds[result] = lId;\r
+  return result;\r
+}\r
+\r
 HWND\r
 CheckBox(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
 {\r
index 82d79767fdbf2a2ec6d212e5e12f40c8d0ba3348..ecc88b3b042c4db79be2d32143e852980ab49fe7 100644 (file)
--- a/src/ws.h
+++ b/src/ws.h
@@ -1,10 +1,3 @@
-// #define _WEBSOCKETPP_CPP11_INTERNAL_\r
-// #define ASIO_STANDALONE\r
-// #include <websocketpp/config/asio_no_tls_client.hpp>\r
-// #include <websocketpp/client.hpp>\r
-\r
-// using client = websocketpp::client<websocketpp::config::asio_client>;\r
-\r
 #include "mongoose.h"\r
 \r
 #include "json.hpp"\r
@@ -14,71 +7,13 @@ using json = nlohmann::json;
 \r
 namespace ws\r
 {\r
-  // client c;\r
-\r
-  // void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {\r
-  //   auto j = json::parse(msg->get_payload());\r
-  //   int op = j["op"].get<int>();\r
-\r
-  //   if (op == 0) {\r
-  //     json response = { { "op", 1 },\r
-  //                       { "d", {\r
-  //                           { "rpcVersion", 1 },\r
-  //                         } } };\r
-  //     auto responseStr = response.dump();\r
-  //     websocketpp::lib::error_code ec;\r
-  //     c.send(hdl, responseStr, websocketpp::frame::opcode::TEXT, ec);\r
-  //     if (ec)\r
-  //       MessageBoxA(NULL, ec.message().c_str(), "error", MB_OK);\r
-  //   } else if (op == 2) {\r
-  //     MessageBoxA(NULL, "hura", "connected", MB_OK);\r
-  //   }\r
-  // }\r
-\r
-  // void init()\r
-  // {\r
-  //   c.set_access_channels(websocketpp::log::alevel::all);\r
-  //   c.clear_access_channels(websocketpp::log::alevel::frame_payload);\r
-  //   c.set_error_channels(websocketpp::log::elevel::all);\r
-\r
-  //   // Initialize ASIO\r
-  //   c.init_asio();\r
-\r
-  //   // Register our message handler\r
-  //   c.set_message_handler(&on_message);\r
-  // }\r
-\r
-  // void connect(std::string address)\r
-  // {\r
-  //   websocketpp::lib::error_code ec;\r
-  //   client::connection_ptr con = c.get_connection(address, ec);\r
-  //   if (ec) {\r
-  //       std::cout << "could not create connection because: " << ec.message() << std::endl;\r
-  //       return;\r
-  //   }\r
-\r
-  //   c.connect(con);\r
-  // }\r
-\r
-  // void update()\r
-  // {\r
-  //   c.run_one();\r
-  // }\r
-\r
-\r
-\r
-\r
-\r
   mg_mgr mgr;\r
   mg_connection *c = nullptr;\r
 \r
   bool done = false;\r
 \r
   static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {\r
-    if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {\r
-      MessageBoxA(NULL, "", "Error", MB_OK);\r
-    } else if (ev == MG_EV_WS_OPEN) {\r
-      //mg_ws_send(c, "hello", 5, WEBSOCKET_OP_TEXT);\r
+    if (ev == MG_EV_WS_OPEN) {\r
     } else if (ev == MG_EV_WS_MSG) {\r
       struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;\r
       std::string jsonStr(wm->data.ptr, wm->data.len);\r
@@ -93,13 +28,7 @@ namespace ws
           }}\r
         };\r
         auto responseStr = response.dump();\r
-        MessageBoxA(NULL,\r
-          (std::string("is_client") + std::to_string(c->is_client) +\r
-          std::string(" is_accepted") + std::to_string(c->is_accepted) +\r
-          std::string(" is_readable") + std::to_string(c->is_readable) +\r
-          std::string(" is_writable") + std::to_string(c->is_writable)).c_str(),\r
-          "", MB_OK);\r
-        mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT);\r
+        mg_ws_send(c, responseStr.c_str(), responseStr.size(), WEBSOCKET_OP_TEXT);\r
       }\r
       else if (op == 2)\r
       {\r
@@ -107,7 +36,12 @@ namespace ws
       }\r
     }\r
 \r
-    if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {\r
+    if (ev == MG_EV_ERROR) {\r
+      MessageBoxA(NULL, "", "Error", MB_OK);\r
+      done = true;\r
+    }\r
+    if (ev == MG_EV_CLOSE) {\r
+      MessageBoxA(NULL, "", "Close", MB_OK);\r
       done = true;\r
     }\r
   }\r
@@ -126,18 +60,17 @@ namespace ws
   {\r
     if (c && !done)\r
       mg_mgr_poll(&mgr, 10);\r
-    else\r
-      MessageBoxA(NULL, "cant update", "Nio", MB_OK);\r
   }\r
 \r
-  void identify()\r
+  void sendRequest(std::string requestType)\r
   {\r
-        MessageBoxA(NULL,\r
-          (std::string("is_client") + std::to_string(c->is_client) +\r
-          std::string(" is_accepted") + std::to_string(c->is_accepted) +\r
-          std::string(" is_readable") + std::to_string(c->is_readable) +\r
-          std::string(" is_writable") + std::to_string(c->is_writable)).c_str(),\r
-          "", MB_OK);\r
-    mg_ws_send(c, "{\"op\":1,\"d\":{\"rpcVersion\":1}}", 29, WEBSOCKET_OP_TEXT);\r
+    json request = { { "op", 6 },\r
+                      { "d",\r
+                        {\r
+                          { "requestType", requestType },\r
+                          { "requestId", std::to_string(NULL).c_str() }\r
+                        } } };\r
+    auto requestStr = request.dump();\r
+    mg_ws_send(c, requestStr.c_str(), requestStr.size(), WEBSOCKET_OP_TEXT);\r
   }\r
 }
\ No newline at end of file