// LESRO PROGRAM | R.P.D.N. - Remapper Program For Desktop Navigation (v02.0) // Annotation: Doideira. #include "header-source.h" LRESULT CALLBACK AdviceProcedure(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK HookProcedure(int, WPARAM, LPARAM); BOOL my_CreateWindow_Function(); // ENTRY POINT int main() { std::cout << "\n You can search for things to watch in portuguese, english or german,"; std::cout << "\n stop watching shits like internet drama."; std::cout << "\n _____________________________________________________________________________" << std::endl; // HIDDEN WINDOW if (!my_CreateWindow_Function()) { my_ShowProgramStatus_Function(ATTEMPT_FAILED, "create", "Hidden Window"); return 1; } else { my_ShowProgramStatus_Function( ATTEMPT_SUCCESS , "create" , "Hidden Window" ); } // HOTKEY #01 if (!RegisterHotKey(global_Main_hWnd, HOTKEY_CODEMSG_TOGGLE_REMAPPING, MOD_CONTROL | MOD_SHIFT, VK_F1)) { DestroyWindow(global_Main_hWnd); my_ShowProgramStatus_Function(ATTEMPT_FAILED, "register", "HotKey #01"); return 1; } else { my_ShowProgramStatus_Function( ATTEMPT_SUCCESS , "register" , "HotKey #01" ); } std::thread t(my_WorkerThread_Function); t.detach(); // MOUSE HOOK if (!(global_hHook = SetWindowsHookExW(WH_MOUSE_LL, HookProcedure, GetModuleHandle(NULL), NULL))) { UnregisterHotKey(global_Main_hWnd, HOTKEY_CODEMSG_TOGGLE_REMAPPING); DestroyWindow(global_Main_hWnd); my_ShowProgramStatus_Function(ATTEMPT_FAILED, "create", "Mouse Hook"); return 1; } else { my_ShowProgramStatus_Function( ATTEMPT_SUCCESS , "set" , "Windows Hook" ); } std::cout << "\n _____________________________________________________________________________" << std::endl; // MESSAGE LOOP MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0) != 0) { TranslateMessage(&msg); DispatchMessage(&msg); } // It's All Folks UnhookWindowsHookEx(global_hHook); UnregisterHotKey(global_Main_hWnd, HOTKEY_CODEMSG_TOGGLE_REMAPPING); DestroyWindow(global_Main_hWnd); return 0; } // CREATE WINDOW BOOL my_CreateWindow_Function() { const wchar_t CLASS_NAME[] = L"An Extraordinary Window Class Name"; HINSTANCE hInstance = GetModuleHandle(NULL); WNDCLASSEXW wc = {}; wc.cbSize = sizeof(WNDCLASSEXW); wc.lpszClassName = CLASS_NAME; wc.hInstance = hInstance; wc.lpfnWndProc = WindowProcedure; RegisterClassExW(&wc); HWND local_hWnd = CreateWindowExW( 0, CLASS_NAME, L"Title", NULL, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, nullptr ); global_Main_hWnd = local_hWnd; if (local_hWnd == NULL) { return FALSE; } else { return TRUE; } } // CREATE MESSAGE WINDOW CLASS MessageWindowClassInfo my_CreateMessageWindowClass () { static const wchar_t CLASS_NAME[] = L"MY_UNIQUE_POPUP_CLASS"; HINSTANCE hInst = GetModuleHandle(NULL); COLORREF boxColor = RGB(25, 25, 25); WNDCLASSEXW wc = { 0 }; wc.cbSize = sizeof(WNDCLASSEXW); if (!GetClassInfoExW(hInst, CLASS_NAME, &wc)) { wc.cbSize = sizeof(WNDCLASSEXW); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = AdviceProcedure; wc.hInstance = hInst; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = CLASS_NAME; wc.hbrBackground = CreateSolidBrush(boxColor); if (!RegisterClassExW(&wc)) { my_ShowProgramStatus_Function(ATTEMPT_FAILED, "register", "Message Window Class"); } } return { (const wchar_t*)CLASS_NAME, hInst }; } // SHOW DISPLACEMENT void my_ShowDisplacement_Function(std::string which_button, int x, int y) { std::cout << "\n | Displacement: " << "[ Button = " << which_button << " ] & " << "[ dx = " << std::showpos << std::internal << std::setfill('0') << std::setw(5) << x << " ] & " << "[ dy = " << std::showpos << std::internal << std::setfill('0') << std::setw(5) << y << " ]" << std::endl; } // SEND COMMAND void my_SendCommand_Function(UINT mod_01, UINT mod_02, UINT key_03) { std::vector inputs(6); int i = 0; if (mod_01) { inputs[i].ki.wVk = mod_01; inputs[i].ki.dwFlags = 0; i++; } if (mod_02) { inputs[i].ki.wVk = mod_02; inputs[i].ki.dwFlags = 0; i++; } if (key_03) { inputs[i].ki.wVk = key_03; inputs[i].ki.dwFlags = 0; i++; } if (key_03) { inputs[i].ki.wVk = key_03; inputs[i].ki.dwFlags = KEYEVENTF_KEYUP; i++; } if (mod_02) { inputs[i].ki.wVk = mod_02; inputs[i].ki.dwFlags = KEYEVENTF_KEYUP; i++; } if (mod_01) { inputs[i].ki.wVk = mod_01; inputs[i].ki.dwFlags = KEYEVENTF_KEYUP; i++; } for (int counter = 0; counter < i; counter++) { inputs[counter].type = INPUT_KEYBOARD; inputs[counter].ki.wScan = 0; inputs[counter].ki.time = 0; inputs[counter].ki.dwExtraInfo = 0; } inputs.resize(i); SendInput(i, inputs.data(), sizeof(INPUT)); } // HOTKEY AND WINDOW PROCEDURE LRESULT CALLBACK WindowProcedure(HWND Wnd_Handle, UINT Wnd_Msg, WPARAM wParam, LPARAM lParam) { switch (Wnd_Msg) { case WM_HOTKEY: if (wParam == HOTKEY_CODEMSG_TOGGLE_REMAPPING) { will_Allow_MouseRemapping = !will_Allow_MouseRemapping; std::cout << "\n | Modificated: The remapping logic of the program was turned " << (will_Allow_MouseRemapping ? "on" : "off") << std::endl; my_ShowToggleAdvice(L"Mouse Remapping: "); } break; default: return DefWindowProc(Wnd_Handle, Wnd_Msg, wParam, lParam); } return 0; } // HOOK PROCEDURE LRESULT CALLBACK HookProcedure(int Hook_Msg, WPARAM wParam, LPARAM lParam) { if (Hook_Msg >= 0) { bool is_ControlKey_Pressed = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0; if (is_ControlKey_Pressed && wParam == WM_LBUTTONDOWN) { std::cout << "\n | Remapping: The [ Control + Left Click ] event was blocked!" << std::endl; return 1; } MSLLHOOKSTRUCT* pMouseStruct = reinterpret_cast (lParam); HWND actual_hWnd = GetForegroundWindow(); wchar_t title[1024]; GetWindowText(actual_hWnd, title, 1024); std::wstring wTitle = title; if (wTitle.find(L"GIMP") != std::wstring::npos) { is_Gimp_Active = true; } else { is_Gimp_Active - false; } if (will_Allow_MouseRemapping) { // RIGHT MOUSE BUTTON if (wParam == WM_RBUTTONDOWN) { if (pMouseStruct->flags & LLMHF_INJECTED) { return CallNextHookEx(NULL, Hook_Msg, wParam, lParam); } init_Cursor = pMouseStruct->pt; return 1; } else if (wParam == WM_RBUTTONUP) { if (pMouseStruct->flags & LLMHF_INJECTED) { return CallNextHookEx(NULL, Hook_Msg, wParam, lParam); } POINT final_Cursor = pMouseStruct->pt; { std::lock_guard lock(queueMutex); actionQueue.push({ (final_Cursor.x - init_Cursor.x) , (final_Cursor.y - init_Cursor.y) , "Right " }); } queueCV.notify_one(); return 1; } // MIDDLE MOUSE BUTTON if (wParam == WM_MBUTTONDOWN) { if (pMouseStruct->flags & LLMHF_INJECTED) { return CallNextHookEx(NULL, Hook_Msg, wParam, lParam); } init_Cursor = pMouseStruct->pt; return 1; } else if (wParam == WM_MBUTTONUP) { if (pMouseStruct->flags & LLMHF_INJECTED) { return CallNextHookEx(NULL, Hook_Msg, wParam, lParam); } POINT final_Cursor = pMouseStruct->pt; { std::lock_guard lock(queueMutex); actionQueue.push({ (final_Cursor.x - init_Cursor.x) , (final_Cursor.y - init_Cursor.y) , "Middle" }); } queueCV.notify_one(); return 1; } } else { if ( ( wParam == WM_XBUTTONDOWN || wParam == WM_NCXBUTTONDOWN ) && is_Gimp_Active ) { // O HIWORD do mouseData contém qual XBUTTON foi usado WORD btnType = HIWORD(pMouseStruct->mouseData); if (btnType == XBUTTON1) { std::cout << "\n | XBUTTON1: Shortcut [ remapped = Z ] & [ action = Undo ]" << std::endl; PostMessage(actual_hWnd, WM_KEYDOWN, static_cast ('Z'), 0); PostMessage(actual_hWnd, WM_KEYUP, static_cast ('Z'), 0); } else if (btnType == XBUTTON2) { WORD wParam = 'Z'; std::cout << "\n | XBUTTON1: Shortcut [ remapped = X ] & [ action = Redo ]" << std::endl; PostMessage(actual_hWnd, WM_KEYDOWN, static_cast ('X'), 0); PostMessage(actual_hWnd, WM_KEYUP, static_cast ('X'), 0); } } if ( wParam == WM_MBUTTONDOWN && is_Gimp_Active ) { init_Cursor = pMouseStruct->pt; } else if ( wParam == WM_MBUTTONUP && is_Gimp_Active ) { POINT final_Cursor = pMouseStruct->pt; int dx = init_Cursor.x - final_Cursor.x; int dy = init_Cursor.y - final_Cursor.y; bool isThereAny_Remap = ( ( abs(dx) < MAX_REMAPLESS_DISPLACEMENT ) && ( abs(dy) < MAX_REMAPLESS_DISPLACEMENT ) ); if (isThereAny_Remap) { is_Brush_or_Eraser = !is_Brush_or_Eraser; if (is_Brush_or_Eraser) { PostMessage(actual_hWnd, WM_KEYDOWN, static_cast('E'), 0); PostMessage(actual_hWnd, WM_KEYUP , static_cast('E'), 0); } else { PostMessage(actual_hWnd, WM_KEYDOWN, static_cast('B'), 0); PostMessage(actual_hWnd, WM_KEYUP , static_cast('B'), 0); } } } } } return CallNextHookEx(global_hHook, Hook_Msg, wParam, lParam); } // ADVICE WINDOW PROCEDURE LRESULT CALLBACK AdviceProcedure( HWND hWnd , UINT Wnd_Msg , WPARAM wParam , LPARAM lParam ) { static HBRUSH hDarkBrush = CreateSolidBrush(RGB(30, 30, 30)); switch (Wnd_Msg) { case WM_CTLCOLORSTATIC: { HDC hdcStatic = (HDC)wParam; SetTextColor(hdcStatic, RGB( 255 , 255 , 255 )); // Texto Branco SetBkMode(hdcStatic, TRANSPARENT); // Fundo do texto transparente return (LRESULT)GetStockObject(HOLLOW_BRUSH); // Retorna um pincel vazio para o fundo } break; default: return DefWindowProcW(hWnd, Wnd_Msg, wParam, lParam); } return 0; }