-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (167 loc) · 5.86 KB
/
main.cpp
File metadata and controls
193 lines (167 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Proudly engineered by itzzexcel
// Under the MIT License
// https://github.com/itzzexcel/winq-remapper
#include <filesystem>
#include <unordered_map>
#include <wctype.h>
#include "hooks.hpp"
std::wstring mode = L"default";
bool hoverSetting = false;
bool hoverwFocusSetting = false;
bool isDebugMode = false;
bool wKeyPressed = false;
bool enableForceKeybind = false;
HHOOK kbHook;
HWND lastHoverWindow = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Kill existing instances
DWORD currentProcessId = GetCurrentProcessId();
PROCESSENTRY32W processEntry{};
processEntry.dwSize = sizeof(PROCESSENTRY32W);
HANDLE processSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (processSnapshot != INVALID_HANDLE_VALUE)
{
if (Process32FirstW(processSnapshot, &processEntry))
{
do
{
if (_wcsicmp(processEntry.szExeFile, L"winq-remapper.exe") == 0 && processEntry.th32ProcessID != currentProcessId ||
_wcsicmp(processEntry.szExeFile, L"wnq-rmp.exe") == 0 && processEntry.th32ProcessID != currentProcessId)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processEntry.th32ProcessID);
if (hProcess != nullptr)
{
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
}
} while (Process32NextW(processSnapshot, &processEntry));
}
CloseHandle(processSnapshot);
}
// cmd argument parser
std::wstring wideCmdLine = GetCommandLineW();
size_t firstSpace = wideCmdLine.find(L' ');
if (firstSpace != std::wstring::npos)
{
wideCmdLine = wideCmdLine.substr(firstSpace + 1);
}
else
{
wideCmdLine = L"";
}
// Help arg
if (wideCmdLine.find(L"--help") != std::wstring::npos)
{
AttachToConsole();
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
print("Usage: winq-remapper.exe [--debug] [--mode <hover|hovfocus|default>] [--uninstall]");
SetConsoleTextAttribute(hConsole, 7);
return 0;
}
if (wideCmdLine.find(L"--attach") != std::wstring::npos)
AttachToConsole();
if (wideCmdLine.find(L"--enable-force-keybind") != std::wstring::npos)
enableForceKeybind = true;
// Uninstall arg
if (wideCmdLine.find(L"--uninstall") != std::wstring::npos)
return Uninstall() ? 0 : 1;
// Debug arg
if (wideCmdLine.find(L"--debug") != std::wstring::npos)
isDebugMode = true;
// Mode arg
if (wideCmdLine.find(L"--mode") != std::wstring::npos)
{
std::wstring modeValue = L"default";
// Check for --mode=value
size_t modePos = wideCmdLine.find(L"--mode=");
if (modePos != std::wstring::npos)
{
size_t valueStart = modePos + 7; // Length of "--mode="
size_t valueEnd = wideCmdLine.find(L' ', valueStart);
if (valueEnd == std::wstring::npos)
{
valueEnd = wideCmdLine.length();
}
modeValue = wideCmdLine.substr(valueStart, valueEnd - valueStart);
}
else
{
// Check for --mode value format
modePos = wideCmdLine.find(L"--mode");
if (modePos != std::wstring::npos)
{
size_t valueStart = modePos + 6; // Length of "--mode"
// Skip whitespace
while (valueStart < wideCmdLine.length() && iswspace(wideCmdLine[valueStart]))
{
valueStart++;
}
if (valueStart < wideCmdLine.length())
{
size_t valueEnd = wideCmdLine.find(L' ', valueStart);
if (valueEnd == std::wstring::npos)
valueEnd = wideCmdLine.length();
modeValue = wideCmdLine.substr(valueStart, valueEnd - valueStart);
}
}
}
// Apply mode settings
if (modeValue == L"hover")
{
hoverSetting = true;
hoverwFocusSetting = false;
mode = L"hover";
}
else if (modeValue == L"hovfocus")
{
hoverSetting = false;
hoverwFocusSetting = true;
mode = L"hovfocus";
}
else
{
hoverSetting = false;
hoverwFocusSetting = false;
mode = L"default";
}
}
// Debug console setup
if (isDebugMode)
{
uintptr_t base = (uintptr_t)GetModuleHandle(nullptr);
uintptr_t func = (uintptr_t)&kbHook;
uintptr_t offset = func - base;
if (GetConsoleWindow() == NULL)
{
AllocConsole();
freopen_s((FILE **)stdout, "CONOUT$", "w", stdout);
}
print("[DEBUG] Mode: %ls", mode.c_str());
print("[DEBUG] Hover setting: %d", hoverSetting);
print("[DEBUG] Hover with focus setting: %d", hoverwFocusSetting);
print("[DEBUG] Full command line: %ls", wideCmdLine.c_str());
print("[DEBUG] Keyboard hook offset: 0x%p", (void *)offset);
}
else
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
HWND hwnd = CreateWindowExW(WS_EX_TOOLWINDOW, L"STATIC", L"wnq/rmp", WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
RegisterStartup(hInstance, wideCmdLine);
kbHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
if (!kbHook)
return 1;
SetTimer(NULL, 1, 50, HoverTimerProc);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(NULL, 1);
UnhookWindowsHookEx(kbHook);
return 0;
}