forked from stefansundin/killkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkillkeys.c
440 lines (398 loc) · 10.8 KB
/
killkeys.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
/*
Copyright (C) 2010 Stefan Sundin ([email protected])
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_IE 0x0600
#define _CRT_NON_CONFORMING_SWPRINTFS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <shlwapi.h>
//App
#define APP_NAME L"KillKeys"
#define APP_VERSION "1.2"
#define APP_URL L"http://code.google.com/p/killkeys/"
#define APP_UPDATE_STABLE L"http://killkeys.googlecode.com/svn/wiki/latest-stable.txt"
#define APP_UPDATE_UNSTABLE L"http://killkeys.googlecode.com/svn/wiki/latest-unstable.txt"
//Messages
#define WM_TRAY WM_USER+1
#define SWM_TOGGLE WM_APP+1
#define SWM_HIDE WM_APP+2
#define SWM_AUTOSTART_ON WM_APP+3
#define SWM_AUTOSTART_OFF WM_APP+4
#define SWM_AUTOSTART_HIDE_ON WM_APP+5
#define SWM_AUTOSTART_HIDE_OFF WM_APP+6
#define SWM_SETTINGS WM_APP+7
#define SWM_CHECKFORUPDATE WM_APP+8
#define SWM_UPDATE WM_APP+9
#define SWM_ABOUT WM_APP+10
#define SWM_EXIT WM_APP+11
//Stuff missing in MinGW
#define HWND_MESSAGE ((HWND)-3)
#ifndef NIIF_USER
#define NIIF_USER 4
#define NIN_BALLOONSHOW WM_USER+2
#define NIN_BALLOONHIDE WM_USER+3
#define NIN_BALLOONTIMEOUT WM_USER+4
#define NIN_BALLOONUSERCLICK WM_USER+5
#endif
//Boring stuff
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;
//Cool stuff
HHOOK keyhook = NULL;
int *keys = NULL;
int numkeys = 0;
int *keys_fullscreen = NULL;
int numkeys_fullscreen = 0;
//Include stuff
#include "localization/strings.h"
#include "include/error.c"
#include "include/autostart.c"
#include "include/tray.c"
#include "include/update.c"
//Entry point
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
g_hinst = hInst;
//Check command line
if (!strcmp(szCmdLine,"-hide")) {
hide = 1;
}
//Look for previous instance
WM_UPDATESETTINGS = RegisterWindowMessage(L"UpdateSettings");
WM_ADDTRAY = RegisterWindowMessage(L"AddTray");
WM_HIDETRAY = RegisterWindowMessage(L"HideTray");
HWND previnst = FindWindow(APP_NAME, NULL);
if (previnst != NULL) {
PostMessage(previnst, WM_UPDATESETTINGS, 0, 0);
PostMessage(previnst, (hide?WM_HIDETRAY:WM_ADDTRAY), 0, 0);
return 0;
}
//Create window
WNDCLASSEX wnd = {sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, NULL, NULL, APP_NAME, NULL};
RegisterClassEx(&wnd);
g_hwnd = CreateWindowEx(0, wnd.lpszClassName, APP_NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInst, NULL);
//Load settings
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path)/sizeof(wchar_t));
PathRemoveFileSpec(path);
wcscat(path, L"\\"APP_NAME".ini");
wchar_t txt[10];
GetPrivateProfileString(APP_NAME, L"Language", L"en-US", txt, sizeof(txt)/sizeof(wchar_t), path);
int i;
for (i=0; languages[i].code != NULL; i++) {
if (!wcsicmp(txt,languages[i].code)) {
l10n = languages[i].strings;
break;
}
}
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
//Tray icon
InitTray();
UpdateTray();
//Hook keyboard
HookKeyboard();
//Add tray if hooking failed, even though -hide was supplied
if (hide && !enabled()) {
hide = 0;
UpdateTray();
}
//Check for update
GetPrivateProfileString(L"Update", L"CheckOnStartup", L"0", txt, sizeof(txt)/sizeof(wchar_t), path);
int checkforupdate = _wtoi(txt);
if (checkforupdate) {
CheckForUpdate(0);
}
//Message loop
MSG msg;
while (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//Monitors
RECT *monitors = NULL;
int nummonitors = 0;
int monitors_alloc = 0;
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
//Make sure we have enough space allocated
if (nummonitors == monitors_alloc) {
monitors_alloc++;
monitors = realloc(monitors, monitors_alloc*sizeof(RECT));
if (monitors == NULL) {
#ifdef DEBUG
Error(L"realloc(monitors)", L"Out of memory?", 0, TEXT(__FILE__), __LINE__);
#endif
return FALSE;
}
}
//Add monitor
monitors[nummonitors++] = *lprcMonitor;
return TRUE;
}
//Fullscreen
int IsFullscreen(HWND window) {
if (GetWindowLongPtr(window,GWL_STYLE)&WS_CAPTION) {
return 0;
}
wchar_t classname[9] = L"";
GetClassName(window, classname, sizeof(classname)/sizeof(wchar_t));
if (!wcscmp(classname,L"WorkerW") || !wcscmp(classname,L"Progman")) {
return 0;
}
//Get window size
RECT wnd;
if (GetWindowRect(window,&wnd) == 0) {
//Error(L"GetWindowRect(&wnd)", L"IsFullscreen()", GetLastError(), TEXT(__FILE__), __LINE__);
return 0;
}
//Enumerate monitors
nummonitors = 0;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);
//Loop monitors
int i;
for (i=0; i < nummonitors; i++) {
if (wnd.left == monitors[i].left
&& wnd.top == monitors[i].top
&& wnd.right == monitors[i].right
&& wnd.bottom == monitors[i].bottom) {
return 1;
}
}
//Not fullscreen
return 0;
}
//Hook
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {
int vkey = ((PKBDLLHOOKSTRUCT)lParam)->vkCode;
int stop = 0;
int i;
HWND window = GetForegroundWindow();
int fullscreen = IsFullscreen(window);
//Check if the key should be blocked
if (!fullscreen) {
for (i=0; i < numkeys; i++) {
if (vkey == keys[i]) {
stop = 1;
break;
}
}
}
else {
for (i=0; i < numkeys_fullscreen; i++) {
if (vkey == keys_fullscreen[i]) {
stop = 1;
break;
}
}
}
//Stop this key
if (stop) {
return 1;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int HookKeyboard() {
if (keyhook) {
//Keyboard already hooked
return 1;
}
//Set up the hook
keyhook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_hinst, 0);
if (keyhook == NULL) {
Error(L"SetWindowsHookEx(WH_KEYBOARD_LL)", L"Could not hook keyboard. Another program might be interfering.", GetLastError(), TEXT(__FILE__), __LINE__);
return 1;
}
//Success
UpdateTray();
return 0;
}
int UnhookKeyboard() {
if (!keyhook) {
//Keyboard not hooked
return 1;
}
//Reset keys
numkeys = 0;
numkeys_fullscreen = 0;
free(keys);
free(keys_fullscreen);
keys = NULL;
keys_fullscreen = NULL;
//Remove keyboard hook
if (UnhookWindowsHookEx(keyhook) == 0) {
Error(L"UnhookWindowsHookEx(keyhook)", L"Could not unhook keyboard. Try restarting "APP_NAME".", GetLastError(), TEXT(__FILE__), __LINE__);
return 1;
}
//Success
keyhook = NULL;
UpdateTray();
return 0;
}
int enabled() {
return keyhook != NULL;
}
void ToggleState() {
if (enabled()) {
UnhookKeyboard();
}
else {
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
HookKeyboard();
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_TRAY) {
if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK) {
ToggleState();
}
else if (lParam == WM_MBUTTONDOWN) {
SendMessage(hwnd, WM_COMMAND, SWM_SETTINGS, 0);
}
else if (lParam == WM_RBUTTONDOWN) {
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) {
//Load settings
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path)/sizeof(wchar_t));
PathRemoveFileSpec(path);
wcscat(path, L"\\"APP_NAME".ini");
wchar_t txt[1000];
int keys_alloc = 0;
int temp;
int numread;
//Keys
GetPrivateProfileString(APP_NAME, L"Keys", L"", txt, sizeof(txt)/sizeof(wchar_t), path);
wchar_t *pos = txt;
numkeys = 0;
while (*pos != '\0' && swscanf(pos,L"%02X%n",&temp,&numread) != EOF) {
//Make sure we have enough space
if (numkeys == keys_alloc) {
keys_alloc += 100;
keys = realloc(keys, keys_alloc*sizeof(int));
if (keys == NULL) {
Error(L"realloc(keys)", L"Out of memory?", GetLastError(), TEXT(__FILE__), __LINE__);
break;
}
}
//Store key
keys[numkeys++] = temp;
pos += numread;
}
//Keys_Fullscreen
GetPrivateProfileString(APP_NAME, L"Keys_Fullscreen", L"", txt, sizeof(txt)/sizeof(wchar_t), path);
pos = txt;
numkeys_fullscreen = 0;
keys_alloc = 0;
while (*pos != '\0' && swscanf(pos,L"%02X%n",&temp,&numread) != EOF) {
//Make sure we have enough space
if (numkeys_fullscreen == keys_alloc) {
keys_alloc += 100;
keys_fullscreen = realloc(keys_fullscreen, keys_alloc*sizeof(int));
if (keys_fullscreen == NULL) {
Error(L"realloc(keys_fullscreen)", L"Out of memory?", GetLastError(), TEXT(__FILE__), __LINE__);
break;
}
}
//Store key
keys_fullscreen[numkeys_fullscreen++] = temp;
pos += numread;
}
//Language
GetPrivateProfileString(APP_NAME, L"Language", L"en-US", txt, sizeof(txt)/sizeof(wchar_t), path);
int i;
for (i=0; languages[i].code != NULL; i++) {
if (!wcsicmp(txt,languages[i].code)) {
l10n = languages[i].strings;
break;
}
}
}
else if (msg == WM_ADDTRAY) {
hide = 0;
UpdateTray();
}
else if (msg == WM_HIDETRAY) {
hide = 1;
RemoveTray();
}
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_AUTOSTART_ON) {
SetAutostart(1, 0);
}
else if (wmId == SWM_AUTOSTART_OFF) {
SetAutostart(0, 0);
}
else if (wmId == SWM_AUTOSTART_HIDE_ON) {
SetAutostart(1, 1);
}
else if (wmId == SWM_AUTOSTART_HIDE_OFF) {
SetAutostart(1, 0);
}
else if (wmId == SWM_SETTINGS) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path)/sizeof(wchar_t));
PathRemoveFileSpec(path);
wcscat(path, L"\\"APP_NAME".ini");
ShellExecute(NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL);
}
else if (wmId == SWM_CHECKFORUPDATE) {
CheckForUpdate(1);
}
else if (wmId == SWM_UPDATE) {
if (MessageBox(NULL,l10n->update_dialog,APP_NAME,MB_ICONINFORMATION|MB_YESNO|MB_SYSTEMMODAL) == IDYES) {
ShellExecute(NULL, L"open", APP_URL, NULL, NULL, SW_SHOWNORMAL);
}
}
else if (wmId == SWM_ABOUT) {
MessageBox(NULL, l10n->about, l10n->about_title, MB_ICONINFORMATION|MB_OK);
}
else if (wmId == SWM_EXIT) {
DestroyWindow(hwnd);
}
}
else if (msg == WM_DESTROY) {
showerror = 0;
UnhookKeyboard();
RemoveTray();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}