-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
648 lines (508 loc) · 14.1 KB
/
main.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* main.c -- main part of mouse-processor
*
*/
#include "main.h"
#include "util.h"
#include "resource.h"
#define LR_VGACOLOR 0x0080
typedef LRESULT (* msg_proc_t)(HWND, UINT, WPARAM, LPARAM);
struct app_context ctx;
const LPTSTR window_class_name = _T("mouse-processor");
const LPTSTR window_title_name = _T("mouse-processor");
const int notify_icon_id = 1;
static int taskbar_created_message = 0;
static HMENU popup_menu = NULL;
static int pause = 0;
static
int is_winxp_or_later(void)
{
OSVERSIONINFOEX osv;
DWORDLONG cond;
memset(&osv, 0, sizeof(osv));
osv.dwOSVersionInfoSize = sizeof(osv);
osv.dwMajorVersion = 5;
osv.dwMinorVersion = 1;
cond = 0;
cond = VerSetConditionMask(cond, VER_MAJORVERSION, VER_GREATER_EQUAL);
cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
return VerifyVersionInfo(&osv, VER_MAJORVERSION | VER_MINORVERSION, cond);
}
/* tasktray icon */
static
int set_tasktray_icon(HWND hwnd, int msg)
{
NOTIFYICONDATA ni;
if(ctx.tray_icon == NULL) {
ctx.tray_icon = LoadImage(ctx.instance, MAKEINTRESOURCE(ID_ICON_MAIN),
IMAGE_ICON, 16, 16,
(is_winxp_or_later() ?
LR_DEFAULTCOLOR : LR_VGACOLOR));
if(ctx.tray_icon == NULL) {
return 0;
}
}
memset(&ni, 0, sizeof(ni));
ni.cbSize = sizeof(ni);
ni.hWnd = hwnd;
ni.uID = notify_icon_id;
ni.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
ni.hIcon = ctx.tray_icon;
ni.uCallbackMessage = WM_TASKTRAY;
_tcscpy(ni.szTip, window_title_name);
return Shell_NotifyIcon(msg, &ni);
}
/* menu item state */
static
int set_pause_menu_item(int state)
{
MENUITEMINFO mi;
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
mi.fMask = MIIM_STATE;
mi.fState = state;
return SetMenuItemInfo(popup_menu, ID_MENU_PAUSE, FALSE, &mi);
}
static
void error_message(LPWSTR msg)
{
if(ctx.log_window != NULL) {
log_printf(LOG_LEVEL_ERROR, L"Error: %ls\n", msg);
} else {
MessageBoxW(NULL, msg, window_title_name, MB_OK | MB_ICONERROR);
}
}
static
void error_message_id(const char *msg, DWORD id, BOOL is_hex)
{
LPWSTR buf1 = NULL, buf2 = NULL;
if(FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, id, 0, (LPWSTR)(void *)&buf1, 0, NULL) != 0) {
int len = wcslen(buf1);
while(buf1[len - 1] == L'\n') {
buf1[len - 1] = 0;
len -= 1;
}
buf2 = (WCHAR *)
malloc(sizeof(WCHAR) * (strlen(msg) + wcslen(buf1) + 32));
if(buf2 == NULL) {
goto end;
}
wsprintfW(buf2, (is_hex ? L"%hs: %ls (0x%08X)" : L"%hs: %ls (%d)"),
msg, buf1, id);
} else {
buf2 = (WCHAR *)malloc(sizeof(WCHAR) * (strlen(msg) + 32));
if(buf2 == NULL) {
goto end;
}
wsprintfW(buf2, (is_hex ? L"%hs: 0x%08X" : L"%hs: %d"), msg, id);
}
error_message(buf2);
end:
LocalFree(buf1);
free(buf2);
}
void error_message_le(const char *msg)
{
error_message_id(msg, GetLastError(), FALSE);
}
void error_message_hr(const char *msg, HRESULT hr)
{
if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS) {
error_message_id(msg, HRESULT_CODE(hr), FALSE);
} else {
error_message_id(msg, hr, TRUE);
}
}
static
LRESULT default_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
return DefWindowProc(hwnd, msg, wparam, lparam);
}
/* menu item: reload */
static
LRESULT menu_reload(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
load_setting(ctx.app_conf.conf_file, FALSE);
/* unhook and hook again if not paused */
if(! pause) {
int ret;
ret = clear_hook();
if(ret == 0) {
error_message_le("clear_hook() failed");
}
ret = set_hook();
if(ret == 0) {
error_message_le("set_hook() failed");
}
}
return 0;
}
/* menu item: pause */
static
LRESULT menu_pause(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
int ret;
/* toggle pasue state */
pause = !pause;
set_pause_menu_item(pause ? MFS_CHECKED : MFS_UNCHECKED);
if(pause) {
ret = clear_hook();
if(ret == 0) {
error_message_le("clear_hook() failed");
}
} else {
ret = set_hook();
if(ret == 0) {
error_message_le("set_hook() failed");
}
}
return 0;
}
/* menu item: log */
static
LRESULT menu_log(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
show_logger(TRUE);
return 0;
}
/* menu item: exit */
static
LRESULT menu_exit(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
DestroyWindow(hwnd);
return 0;
}
/* tray: WM_RBUTTONUP */
static
LRESULT tray_rbutton_up(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
POINT pos;
int ret;
GetCursorPos(&pos);
SetForegroundWindow(hwnd);
SetFocus(hwnd);
ret = TrackPopupMenu(popup_menu, TPM_LEFTALIGN | TPM_BOTTOMALIGN,
pos.x, pos.y, 0, hwnd, NULL);
if(ret == 0) {
error_message_le("TrackPopupMenu() failed");
}
return 0;
}
/* tray: WM_LBUTTONDBLCLK */
static
LRESULT tray_lbutton_dblclick(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam)
{
show_logger(TRUE);
return 0;
}
/* WM_CREATE */
static
LRESULT main_create(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
int ret;
{
HMENU menu;
menu = LoadMenu(ctx.instance, MAKEINTRESOURCE(ID_MENU_TRAY));
if(menu == NULL) {
error_message_le("LoadMenu() failed");
return -1;
}
popup_menu = GetSubMenu(menu, 0);
if(popup_menu == 0) {
error_message_le("GetSubMenu() failed");
return -1;
}
ret = SetMenuDefaultItem(popup_menu, ID_MENU_LOG, FALSE);
if(ret == 0) {
error_message_le("SetMenuDefaultItem() failed");
return -1;
}
}
/* message id of taskbar created notify */
taskbar_created_message = RegisterWindowMessage(_T("TaskbarCreated"));
if(taskbar_created_message == 0) {
error_message_le("RegisterWindowMessage() failed");
return 1;
}
ret = set_tasktray_icon(hwnd, NIM_ADD);
if(ret == 0) {
error_message_le("set_tasktray_icon() failed");
return -1;
}
ret = set_hook();
if(ret == 0) {
error_message_le("set_hook() failed");
return -1;
}
return 0;
}
/* WM_DESTROY */
static
LRESULT main_destroy(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
set_tasktray_icon(hwnd, NIM_DELETE);
DestroyIcon(ctx.tray_icon);
ctx.tray_icon = NULL;
clear_hook();
PostQuitMessage(0);
return 0;
}
/* "TaskbarCreated" */
static
LRESULT main_taskbar_created(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
int ret;
ret = set_tasktray_icon(hwnd, NIM_ADD);
if(ret == 0) {
error_message_le("set_tasktray_icon() failed");
}
return 0;
}
/* WM_TASKTRAY_CH */
static
LRESULT main_tasktray_ch(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
int ret;
HICON icon;
if(ctx.app_conf.tray_icon_hide) {
set_tasktray_icon(hwnd, NIM_DELETE);
return 0;
}
if(ctx.app_conf.tray_icon_file != NULL) {
if(ExtractIconExW(ctx.app_conf.tray_icon_file,
ctx.app_conf.tray_icon_idx,
NULL, &icon, 1) != 1) {
error_message(L"ExtractIconEx() failed");
return 0;
}
} else {
icon = NULL;
}
DestroyIcon(ctx.tray_icon);
ctx.tray_icon = icon;
ret = set_tasktray_icon(hwnd, NIM_MODIFY);
if(ret == 0) {
error_message_le("set_tasktray_icon() failed");
}
return 0;
}
/* WM_TASKTRAY */
static
LRESULT main_tasktray(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static struct uint_ptr_pair msg_map[] = {
{WM_RBUTTONUP, tray_rbutton_up},
{WM_LBUTTONDBLCLK, tray_lbutton_dblclick},
{0, NULL}
};
msg_proc_t proc;
proc = (msg_proc_t)assq_pair(msg_map, lparam, NULL);
if(proc) {
return proc(hwnd, msg, wparam, lparam);
} else {
return 0;
}
}
/* WM_MOUSEHOOK_WHEELPOST */
static
LRESULT wheel_post(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
HWND target;
MSG m;
int delta;
WPARAM wp;
LPARAM lp;
delta = (int)wparam;
lp = lparam;
while(PeekMessage(&m, hwnd, msg, msg, PM_NOREMOVE)) {
delta += (int)m.wParam;
lp = m.lParam;
/* discard */
PeekMessage(&m, hwnd, msg, msg, PM_REMOVE);
}
{
POINT pt;
pt.x = (short)LOWORD(lparam);
pt.y = (short)HIWORD(lparam);
target = get_window_for_mouse_input(pt);
if(target == NULL) {
return 0;
}
}
wp = MAKEWPARAM((GetKeyState(VK_CONTROL) & 0x8000 ? MK_CONTROL : 0) |
(GetKeyState(VK_LBUTTON) & 0x8000 ? MK_LBUTTON : 0) |
(GetKeyState(VK_MBUTTON) & 0x8000 ? MK_MBUTTON : 0) |
(GetKeyState(VK_RBUTTON) & 0x8000 ? MK_RBUTTON : 0) |
(GetKeyState(VK_SHIFT) & 0x8000 ? MK_SHIFT : 0) |
(GetKeyState(VK_XBUTTON1) & 0x8000 ? MK_XBUTTON1 : 0) |
(GetKeyState(VK_XBUTTON2) & 0x8000 ? MK_XBUTTON2 : 0),
delta);
SendMessageTimeout(target, WM_MOUSEWHEEL, wp, lp,
SMTO_ABORTIFHUNG, 1000, NULL);
return 0;
}
/* WM_COMMAND */
static
LRESULT main_command(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static struct uint_ptr_pair msg_map[] = {
{MAKEWPARAM(ID_MENU_PAUSE, 0), menu_pause},
{MAKEWPARAM(ID_MENU_EXIT, 0), menu_exit},
{MAKEWPARAM(ID_MENU_LOG, 0), menu_log},
{MAKEWPARAM(ID_MENU_RELOAD, 0), menu_reload},
{0, NULL}
};
msg_proc_t proc;
proc = (msg_proc_t)assq_pair(msg_map, wparam, default_proc);
return proc(hwnd, msg, wparam, lparam);
}
/* window procedure */
static
LRESULT CALLBACK main_window_proc(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam)
{
static struct uint_ptr_pair msg_map[] = {
{0, main_taskbar_created},
{WM_CREATE, main_create},
{WM_DESTROY, main_destroy},
{WM_COMMAND, main_command},
{WM_TASKTRAY, main_tasktray},
{WM_TASKTRAY_CH, main_tasktray_ch},
{WM_MOUSEHOOK_MODECH, scroll_modech},
{WM_MOUSEHOOK_MODEMSG, scroll_modemsg},
{WM_MOUSEHOOK_WHEELPOST, wheel_post},
{0, NULL}
};
msg_proc_t proc;
msg_map[0].key = taskbar_created_message;
proc = (msg_proc_t)assq_pair(msg_map, msg, default_proc);
return proc(hwnd, msg, wparam, lparam);
}
static
HWND create_main_window(void)
{
{
WNDCLASSEX wc;
int ret;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = 0;
wc.lpfnWndProc = main_window_proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ctx.instance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = window_class_name;
wc.hIconSm = NULL;
ret = RegisterClassEx(&wc);
if(ret == 0) {
return NULL;
}
}
{
HWND wnd;
wnd = CreateWindow(window_class_name, window_title_name,
0, 0, 0, 0, 0,
NULL, NULL, ctx.instance, NULL);
return wnd;
}
}
static
int message_loop(void)
{
MSG msg;
int ret;
while(1) {
ret = GetMessage(&msg, NULL, 0, 0);
if(ret < 0) {
error_message_le("GetMessage() failed");
return 1;
}
if(ret == 0) {
return msg.wParam;
}
if(ctx.log_window != NULL &&
IsDialogMessage(ctx.log_window, &msg) != 0) {
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0; /* not reach */
}
/* entry point */
int main(int ac, char **av)
{
int ret;
memset(&ctx, 0, sizeof(ctx));
/* instance handle of this module */
{
HMODULE module;
module = GetModuleHandle(NULL);
if(module == NULL) {
error_message_le("GetModuleHandle() failed");
return 1;
}
ctx.instance = (HINSTANCE)module;
}
/* initialize COM */
{
HRESULT hres;
hres = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if(FAILED(hres)) {
error_message_hr("CoInitializeEx() failed", hres);
return 1;
}
hres = init_regexp();
if(FAILED(hres)) {
error_message_hr("init_regexp() failed", hres);
return 1;
}
}
/* create main message window */
ctx.main_window = create_main_window();
if(ctx.main_window == NULL) {
error_message_le("create_main_window() failed");
return 1;
}
/* logger */
ret = create_logger();
if(ret == 0) {
error_message_le("create_logger() failed");
return 1;
}
ctx.sprocs.hdr.api_ver = MP_OP_API_VERSION;
ctx.sprocs.hdr.type = MP_OP_TYPE_SUPPORT;
ctx.sprocs.log_printf = log_printf;
ctx.sprocs.log_s_exp = log_print_s_exp;
ctx.sprocs.log_lasterror = log_print_lasterror;
ctx.sprocs.log_hresult = log_print_hresult;
/* command line option */
{
LPWSTR *avw, file;
int acw;
avw = CommandLineToArgvW(GetCommandLineW(), &acw);
if(avw != NULL && acw >= 2) {
file = avw[1];
} else {
file = NULL;
}
/* load setting file */
load_setting(file, TRUE);
}
/* start message */
log_printf(LOG_LEVEL_NOTIFY, L"\nmouse-processor started\n");
/* main message loop */
ret = message_loop();
/* uninitialize COM */
CoUninitialize();
/* end logger */
destroy_logger();
return ret;
}