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