-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
350 lines (294 loc) · 8.65 KB
/
main.cpp
File metadata and controls
350 lines (294 loc) · 8.65 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <iostream>
#include <thread>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_surface.h>
#include <SDL3/SDL_pixels.h>
#include <windows.h>
#include "resources/resource.h"
class ScopedMutex {
public:
ScopedMutex(const char* name)
{
hMutex_ = CreateMutex(NULL, TRUE, name);
isOwner_ = (GetLastError() != ERROR_ALREADY_EXISTS);
}
~ScopedMutex()
{
if (hMutex_)
{
if (isOwner_)
{
ReleaseMutex(hMutex_);
}
CloseHandle(hMutex_);
}
}
bool isOwner() const { return isOwner_; }
operator bool() const { return hMutex_ != NULL; }
private:
HANDLE hMutex_ = NULL;
bool isOwner_ = false;
};
HHOOK g_keyboardHook = NULL;
bool g_win_pressed = false;
bool g_enabled = true;
bool g_reverse = false;
bool g_pascal_case = false;
void simulate_button(BYTE bVk, DWORD dWFlag)
{
keybd_event(bVk, 0, dWFlag, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(30));
};
void simulate_ctrl_v(const char key)
{
simulate_button(key, KEYEVENTF_KEYUP); // key up
simulate_button(VK_LWIN, KEYEVENTF_KEYUP); // win up
simulate_button(VK_CONTROL, 0); // ctrl down
simulate_button('V', 0); // v down
simulate_button(VK_CONTROL, KEYEVENTF_KEYUP); // ctrl up
simulate_button(VK_LWIN, 0); // win down, so it feels good
}
void on_shortcut_pressed_snake()
{
// std::cout << "on_shortcut_pressed_snake" << std::endl;
if (g_enabled)
{
char* clipboard = SDL_GetClipboardText();
std::string text = clipboard;
SDL_free(clipboard);
std::string result;
result.reserve(text.size() * 2);
if (text.find("_") != std::string::npos)
{
for (size_t i = 0; i < text.size(); ++i)
{
char c = text[i];
if (c == '_')
{
c = text[++i];
result.push_back(std::toupper(c));
}
else
{
if (g_pascal_case)
{
if (i == 0) c = std::toupper(text[i]);
}
result.push_back(c);
}
}
}
else
{
for (size_t i = 0; i < text.size(); ++i)
{
char c = text[i];
if (std::isupper(c))
{
if (i > 0)
{
result.push_back('_');
}
result.push_back(std::tolower(c));
}
else
{
result.push_back(c);
}
}
}
// std::cout << result << std::endl;
SDL_SetClipboardText(result.c_str());
simulate_ctrl_v('O');
}
}
void on_shortcut_pressed()
{
// std::cout << "on_shortcut_pressed" << std::endl;
if (g_enabled)
{
char* clipboard = SDL_GetClipboardText();
std::string text = clipboard;
SDL_free(clipboard);
if (g_reverse)
{
for (char& c : text)
{
if (c == '/')
{
c = '\\';
}
}
}
else
{
for (char& c : text)
{
if (c == '\\')
{
c = '/';
}
}
}
// std::cout << text << std::endl;
SDL_SetClipboardText(text.c_str());
simulate_ctrl_v('V');
}
}
// Keyboard hook procedure
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT* kb = (KBDLLHOOKSTRUCT*)lParam;
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
{
// Check for modifier keys
if (kb->vkCode == VK_LWIN)
g_win_pressed = true;
// Check for our hotkey combination
else if (kb->vkCode == 'O' && g_win_pressed)
{
on_shortcut_pressed_snake();
return 1;
}
else if (kb->vkCode == 'V' && g_win_pressed)
{
on_shortcut_pressed();
return 1;
}
}
else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)
{
if (kb->vkCode == VK_LWIN)
g_win_pressed = false;
}
}
return CallNextHookEx(g_keyboardHook, nCode, wParam, lParam);
}
void callback_quit(void* userdata, SDL_TrayEntry* invoker)
{
SDL_Event e;
e.type = SDL_EVENT_QUIT;
SDL_PushEvent(&e);
}
void callback_reverse(void* userdata, SDL_TrayEntry* invoker)
{
g_reverse = SDL_GetTrayEntryChecked(invoker);
std::cout << "callback_reverse " << g_enabled << std::endl;
}
void callback_enable(void* userdata, SDL_TrayEntry* invoker)
{
g_enabled = SDL_GetTrayEntryChecked(invoker);
std::cout << "callback_enable " << g_enabled << std::endl;
}
void callback_pascal_case(void* userdata, SDL_TrayEntry* invoker)
{
g_pascal_case = SDL_GetTrayEntryChecked(invoker);
std::cout << "callback_pascal_case " << g_pascal_case << std::endl;
}
SDL_Surface* LoadIconFromResource(HINSTANCE hInstance, int resourceID)
{
HICON hIcon = (HICON)LoadImage(
hInstance,
MAKEINTRESOURCE(resourceID),
IMAGE_ICON,
0, 0,
LR_DEFAULTSIZE | LR_SHARED
);
if (!hIcon)
{
return NULL;
}
ICONINFO iconInfo;
if (!GetIconInfo(hIcon, &iconInfo))
{
DestroyIcon(hIcon);
return NULL;
}
BITMAP bmp;
GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bmp);
// Create SDL surface
SDL_Surface* surface = SDL_CreateSurface(
bmp.bmWidth,
bmp.bmHeight,
SDL_PIXELFORMAT_ARGB8888
);
if (!surface)
{
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
DestroyIcon(hIcon);
return NULL;
}
// Copy pixels from HICON into SDL surface
HDC hdc = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDC, iconInfo.hbmColor);
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bmp.bmWidth;
bmi.bmiHeader.biHeight = -bmp.bmHeight; // top-down
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
GetDIBits(hMemDC, iconInfo.hbmColor, 0, bmp.bmHeight,
surface->pixels, &bmi, DIB_RGB_COLORS);
// Cleanup
SelectObject(hMemDC, hOldBmp);
DeleteDC(hMemDC);
ReleaseDC(NULL, hdc);
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
DestroyIcon(hIcon);
return surface;
}
int main(int argc, char* argv[])
{
ScopedMutex mutex("Global.SlashFlipper");
if (!mutex.isOwner())
{
std::cout << "Another instance is running!" << std::endl;
return 0;
}
// Set low-level keyboard hook
g_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
if (!g_keyboardHook)
{
std::cerr << "Failed to set keyboard hook!" << std::endl;
return 1;
}
SDL_Event e;
SDL_Init(SDL_INIT_VIDEO);
HINSTANCE hInstance = GetModuleHandle(NULL);
SDL_Surface* icon = LoadIconFromResource(hInstance, IDI_ICON1);
SDL_Tray* tray = SDL_CreateTray(icon, "SlashFlipper");
SDL_TrayMenu* menu = SDL_CreateTrayMenu(tray);
SDL_TrayEntry* menu_enable = SDL_InsertTrayEntryAt(menu, -1, "Enable", SDL_TRAYENTRY_CHECKBOX);
SDL_SetTrayEntryChecked(menu_enable, g_enabled);
SDL_TrayEntry* menu_reverse = SDL_InsertTrayEntryAt(menu, -1, "Reverse", SDL_TRAYENTRY_CHECKBOX);
SDL_SetTrayEntryChecked(menu_reverse, g_reverse);
SDL_TrayEntry* menu_pascal_case = SDL_InsertTrayEntryAt(menu, -1, "Pascal Case", SDL_TRAYENTRY_CHECKBOX);
SDL_SetTrayEntryChecked(menu_reverse, g_pascal_case);
SDL_TrayEntry* menu_quit = SDL_InsertTrayEntryAt(menu, -1, "Quit", SDL_TRAYENTRY_BUTTON);
SDL_SetTrayEntryCallback(menu_reverse, callback_reverse, NULL);
SDL_SetTrayEntryCallback(menu_enable, callback_enable, NULL);
SDL_SetTrayEntryCallback(menu_pascal_case, callback_pascal_case, NULL);
SDL_SetTrayEntryCallback(menu_quit, callback_quit, NULL);
while (true)
{
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
goto quit;
}
}
SDL_Delay(10);
}
quit:
SDL_DestroyTray(tray);
UnhookWindowsHookEx(g_keyboardHook);
SDL_Quit();
return 0;
}