-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
altdrag.c
452 lines (410 loc) · 12.8 KB
/
altdrag.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
Copyright (C) 2015 Stefan Sundin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0600
#define _WIN32_IE 0x0600
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <shlwapi.h>
// App
#define APP_NAME L"AltDrag"
#define APP_VERSION "1.1"
#define APP_URL L"https://stefansundin.github.io/altdrag/"
// Messages
#define WM_TRAY WM_USER+1
#define SWM_TOGGLE WM_APP+1
#define SWM_HIDE WM_APP+2
#define SWM_UPDATE WM_APP+3
#define SWM_CONFIG WM_APP+4
#define SWM_ABOUT WM_APP+5
#define SWM_EXIT WM_APP+6
// Boring stuff
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define ENABLED() (keyhook || msghook)
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst = NULL;
HWND g_hwnd = NULL;
UINT WM_TASKBARCREATED = 0;
UINT WM_UPDATESETTINGS = 0;
UINT WM_ADDTRAY = 0;
UINT WM_HIDETRAY = 0;
UINT WM_OPENCONFIG = 0;
UINT WM_CLOSECONFIG = 0;
wchar_t inipath[MAX_PATH];
int HookSystem();
int HookMouse();
// Cool stuff
HINSTANCE hinstDLL = NULL;
HHOOK keyhook = NULL;
HHOOK msghook = NULL;
BOOL x64 = FALSE;
int vista = 0;
int elevated = 0;
// Include stuff
#include "localization/strings.h"
#include "localization/languages.h"
#include "include/error.c"
#include "include/localization.c"
#include "include/languages.c"
#include "include/tray.c"
#include "include/update.c"
#include "config/config.c"
// Entry point
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, char *szCmdLine, int iCmdShow) {
g_hinst = hInst;
IsWow64Process(GetCurrentProcess(), &x64);
// Get ini path
GetModuleFileName(NULL, inipath, ARRAY_SIZE(inipath));
PathRemoveFileSpec(inipath);
wcscat(inipath, L"\\"APP_NAME".ini");
wchar_t txt[10];
// Convert szCmdLine to argv and argc (max 10 arguments)
char *argv[10];
int argc = 1;
argv[0] = szCmdLine;
while ((argv[argc]=strchr(argv[argc-1],' ')) != NULL) {
*argv[argc] = '\0';
if (argc == ARRAY_SIZE(argv)) break;
argv[argc++]++;
}
// Check arguments
int i;
int elevate=0, quiet=0, config=-1, multi=0;
for (i=0; i < argc; i++) {
if (!strcmp(argv[i],"-hide") || !strcmp(argv[i],"-h")) {
// -hide = do not add tray icon, hide it if already running
hide = 1;
}
else if (!strcmp(argv[i],"-quiet") || !strcmp(argv[i],"-q")) {
// -quiet = do nothing if already running
quiet = 1;
}
else if (!strcmp(argv[i],"-elevate") || !strcmp(argv[i],"-e")) {
// -elevate = create a new instance with administrator privileges
elevate = 1;
}
else if (!strcmp(argv[i],"-config") || !strcmp(argv[i],"-c")) {
// -config = open config (with requested page)
config = (i+1 < argc)?atoi(argv[i+1]):0;
}
else if (!strcmp(argv[i],"-multi")) {
// -multi = allow multiple instances, used internally when elevating via config window
multi = 1;
}
}
// Check if elevated if in >= Vista
OSVERSIONINFO vi = { sizeof(OSVERSIONINFO) };
GetVersionEx(&vi);
vista = (vi.dwMajorVersion >= 6);
if (vista) {
HANDLE token;
TOKEN_ELEVATION elevation;
DWORD len;
if (OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&token) && GetTokenInformation(token,TokenElevation,&elevation,sizeof(elevation),&len)) {
elevated = elevation.TokenIsElevated;
}
}
// Register some messages
WM_UPDATESETTINGS = RegisterWindowMessage(L"UpdateSettings");
WM_OPENCONFIG = RegisterWindowMessage(L"OpenConfig");
WM_CLOSECONFIG = RegisterWindowMessage(L"CloseConfig");
WM_ADDTRAY = RegisterWindowMessage(L"AddTray");
WM_HIDETRAY = RegisterWindowMessage(L"HideTray");
// Look for previous instance
GetPrivateProfileString(L"Advanced", L"MultipleInstances", L"0", txt, ARRAY_SIZE(txt), inipath);
if (!_wtoi(txt) && !multi) {
HWND previnst = FindWindow(APP_NAME, NULL);
if (previnst != NULL) {
if (quiet) {
return 0;
}
PostMessage(previnst, WM_UPDATESETTINGS, 0, 0);
PostMessage(previnst, (hide && !config?WM_CLOSECONFIG:WM_OPENCONFIG), config, 0);
PostMessage(previnst, (hide?WM_HIDETRAY:WM_ADDTRAY), 0, 0);
return 0;
}
}
// Check AlwaysElevate
if (!elevated) {
GetPrivateProfileString(L"Advanced", L"AlwaysElevate", L"0", txt, ARRAY_SIZE(txt), inipath);
if (_wtoi(txt)) {
elevate = 1;
}
// Handle request to elevate to administrator privileges
if (elevate) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAY_SIZE(path));
int ret = (INT_PTR) ShellExecute(NULL, L"runas", path, (hide?L"-hide":NULL), NULL, SW_SHOWNORMAL);
if (ret > 32) {
return 0;
}
}
}
// Language
memset(&l10n_ini, 0, sizeof(l10n_ini));
UpdateLanguage();
// Create window
WNDCLASSEX wnd = { sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), NULL, APP_NAME, NULL };
RegisterClassEx(&wnd);
g_hwnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST|WS_EX_LAYERED, wnd.lpszClassName, NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInst, NULL);
SetLayeredWindowAttributes(g_hwnd, 0, 1, LWA_ALPHA); // Almost transparent
// Tray icon
InitTray();
UpdateTray();
// Hook system
HookSystem();
// Add tray if hook failed, even though -hide was supplied
if (hide && !keyhook) {
hide = 0;
UpdateTray();
}
// Check for update
GetPrivateProfileString(L"Update", L"CheckOnStartup", L"0", txt, ARRAY_SIZE(txt), inipath);
if (_wtoi(txt)) {
CheckForUpdate(0);
}
// Open config if -config was supplied
if (config != -1) {
PostMessage(g_hwnd, WM_OPENCONFIG, config, 0);
}
// Message loop
MSG msg;
while (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int HookSystem() {
if (keyhook && msghook) {
// System already hooked
return 1;
}
// Load library
if (!hinstDLL) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAY_SIZE(path));
PathRemoveFileSpec(path);
wcscat(path, L"\\hooks.dll");
hinstDLL = LoadLibrary(path);
if (hinstDLL == NULL) {
Error(L"LoadLibrary('hooks.dll')", L"This probably means that the file hooks.dll is missing. You can try reinstalling "APP_NAME".", GetLastError());
return 1;
}
}
// Load keyboard hook
HOOKPROC procaddr;
if (!keyhook) {
// Get address to keyboard hook (beware name mangling)
procaddr = (HOOKPROC) GetProcAddress(hinstDLL, "LowLevelKeyboardProc@12");
if (procaddr == NULL) {
Error(L"GetProcAddress('LowLevelKeyboardProc@12')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
return 1;
}
// Set up the keyboard hook
keyhook = SetWindowsHookEx(WH_KEYBOARD_LL, procaddr, hinstDLL, 0);
if (keyhook == NULL) {
Error(L"SetWindowsHookEx(WH_KEYBOARD_LL)", L"Could not hook keyboard. Another program might be interfering.", GetLastError());
return 1;
}
}
// HookWindows
wchar_t txt[10];
GetPrivateProfileString(L"Advanced", L"HookWindows", L"0", txt, ARRAY_SIZE(txt), inipath);
if (!msghook && _wtoi(txt)) {
// Get address to message hook (beware name mangling)
procaddr = (HOOKPROC) GetProcAddress(hinstDLL, "CallWndProc@12");
if (procaddr == NULL) {
Error(L"GetProcAddress('CallWndProc@12')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
return 1;
}
// Set up the message hook
msghook = SetWindowsHookEx(WH_CALLWNDPROC, procaddr, hinstDLL, 0);
if (msghook == NULL) {
Error(L"SetWindowsHookEx(WH_CALLWNDPROC)", L"Could not hook message hook. Another program might be interfering.", GetLastError());
return 1;
}
// x64
if (x64) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAY_SIZE(path));
PathRemoveFileSpec(path);
wcscat(path, L"\\HookWindows_x64.exe");
ShellExecute(NULL, L"open", path, L"nowarning", NULL, SW_SHOWNORMAL);
}
}
// Success
UpdateTray();
return 0;
}
// Force processes to unload hooks.dll by sending them a dummy message (HookWindows)
// To be honest I don't really know if this makes a difference anymore
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
PostMessage(hwnd, WM_NULL, 0, 0);
return TRUE;
}
int UnhookSystem() {
if (!keyhook && !msghook) {
// System not hooked
return 1;
}
// Remove keyboard hook
if (keyhook) {
if (UnhookWindowsHookEx(keyhook) == 0) {
#ifdef DEBUG
Error(L"UnhookWindowsHookEx(keyhook)", L"Could not unhook keyboard. Try restarting "APP_NAME".", GetLastError());
#else
if (showerror) {
MessageBox(NULL, l10n->unhook_error, APP_NAME, MB_ICONINFORMATION|MB_OK|MB_TOPMOST|MB_SETFOREGROUND);
}
#endif
}
keyhook = NULL;
}
// Remove message hook
if (msghook) {
if (UnhookWindowsHookEx(msghook) == 0) {
#ifdef DEBUG
Error(L"UnhookWindowsHookEx(msghook)", L"Could not unhook message hook. Try restarting "APP_NAME".", GetLastError());
#endif
}
msghook = NULL;
// Close HookWindows_x64.exe
if (x64) {
HWND window = FindWindow(L"AltDrag-x64", NULL);
if (window != NULL) {
PostMessage(window, WM_CLOSE, 0, 0);
}
}
// Send dummy messages to all processes to make them unload hooks.dll
EnumWindows(EnumWindowsProc, 0);
}
// Tell dll file that we are unloading
void (*Unload)() = (void*) GetProcAddress(hinstDLL, "Unload");
if (Unload == NULL) {
Error(L"GetProcAddress('Unload')", L"This probably means that the file hooks.dll is from an old version or corrupt. You can try reinstalling "APP_NAME".", GetLastError());
}
else {
Unload();
}
// Unload library
if (hinstDLL) {
if (FreeLibrary(hinstDLL) == 0) {
Error(L"FreeLibrary()", L"Could not free hooks.dll. Try restarting "APP_NAME".", GetLastError());
}
hinstDLL = NULL;
}
// Success
UpdateTray();
return 0;
}
void ToggleState() {
if (ENABLED()) {
UnhookSystem();
}
else {
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
HookSystem();
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_TRAY) {
if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK) {
ToggleState();
if (lParam == WM_LBUTTONDBLCLK && !(GetAsyncKeyState(VK_SHIFT)&0x8000)) {
SendMessage(hwnd, WM_OPENCONFIG, 0, 0);
}
}
else if (lParam == WM_MBUTTONDOWN) {
ShellExecute(NULL, L"open", inipath, NULL, NULL, SW_SHOWNORMAL);
}
else if (lParam == WM_RBUTTONUP) {
ShowContextMenu(hwnd);
}
else if (lParam == NIN_BALLOONUSERCLICK) {
hide = 0;
SendMessage(hwnd, WM_COMMAND, SWM_UPDATE, 0);
}
else if (lParam == NIN_BALLOONTIMEOUT) {
if (hide) {
RemoveTray();
}
}
}
else if (msg == WM_UPDATESETTINGS) {
UpdateLanguage();
// Reload hooks
if (ENABLED()) {
UnhookSystem();
HookSystem();
}
// Reload config language
if (!wParam && IsWindow(g_cfgwnd)) {
SendMessage(g_cfgwnd, WM_UPDATESETTINGS, 0, 0);
}
}
else if (msg == WM_ADDTRAY) {
hide = 0;
UpdateTray();
}
else if (msg == WM_HIDETRAY) {
hide = 1;
RemoveTray();
}
else if (msg == WM_OPENCONFIG && (lParam || !hide)) {
OpenConfig(wParam);
}
else if (msg == WM_CLOSECONFIG) {
CloseConfig();
}
else if (msg == WM_TASKBARCREATED) {
tray_added = 0;
UpdateTray();
}
else if (msg == WM_COMMAND) {
int wmId=LOWORD(wParam), wmEvent=HIWORD(wParam);
if (wmId == SWM_TOGGLE) {
ToggleState();
}
else if (wmId == SWM_HIDE) {
hide = 1;
RemoveTray();
}
else if (wmId == SWM_UPDATE) {
if (MessageBox(NULL,l10n->update_dialog,APP_NAME,MB_ICONINFORMATION|MB_YESNO|MB_TOPMOST|MB_SETFOREGROUND) == IDYES) {
OpenUrl(APP_URL);
}
}
else if (wmId == SWM_CONFIG) {
SendMessage(hwnd, WM_OPENCONFIG, 0, 0);
}
else if (wmId == SWM_ABOUT) {
SendMessage(hwnd, WM_OPENCONFIG, 4, 0);
}
else if (wmId == SWM_EXIT) {
DestroyWindow(hwnd);
}
}
else if (msg == WM_QUERYENDSESSION && msghook) {
showerror = 0;
UnhookSystem();
}
else if (msg == WM_DESTROY) {
showerror = 0;
UnhookSystem();
RemoveTray();
PostQuitMessage(0);
}
else if (msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
// Hide cursorwnd if clicked on, this might happen if it wasn't hidden by hooks.c for some reason
ShowWindow(hwnd, SW_HIDE);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}