2 * Copyright (c) 2011, Andy Spencer <andy753421@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 #define WIN32_LEAN_AND_MEAN
22 #define _WIN32_WINNT 0x0500
33 static int NO_CAPTURE = 0;
35 /* Internal structures */
46 static int shellhookid;
49 static list_t *screens;
51 /* Conversion functions */
52 static keymap_t key2vk[] = {
53 {key_mouse1 , VK_LBUTTON },
54 {key_mouse2 , VK_MBUTTON },
55 {key_mouse3 , VK_RBUTTON },
56 {key_left , VK_LEFT },
57 {key_right , VK_RIGHT },
59 {key_down , VK_DOWN },
60 {key_home , VK_HOME },
62 {key_pageup , VK_PRIOR },
63 {key_pagedown, VK_NEXT },
76 {key_shift , VK_SHIFT },
77 {key_shift , VK_LSHIFT },
78 {key_shift , VK_RSHIFT },
79 {key_ctrl , VK_CONTROL },
80 {key_ctrl , VK_LCONTROL},
81 {key_ctrl , VK_RCONTROL},
83 {key_alt , VK_LMENU },
84 {key_alt , VK_RMENU },
90 static Key_t w2key(UINT vk)
92 keymap_t *km = map_getr(key2vk,vk);
93 return km ? km->key : tolower(vk);
96 static UINT key2w(Key_t key)
98 keymap_t *km = map_get(key2vk,key);
99 return km ? km->vk : toupper(key);
102 static mod_t getmod(void)
105 .alt = GetKeyState(VK_MENU) < 0,
106 .ctrl = GetKeyState(VK_CONTROL) < 0,
107 .shift = GetKeyState(VK_SHIFT) < 0,
108 .win = GetKeyState(VK_LWIN) < 0 ||
109 GetKeyState(VK_RWIN) < 0,
114 static ptr_t getptr(void)
118 return (ptr_t){-1, -1, wptr.x, wptr.y};
121 /* Window functions */
122 static win_t *win_new(HWND hwnd, int checkwin)
126 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
127 GetClassName(hwnd, winclass, sizeof(winclass));
128 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
129 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
130 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
131 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
135 GetWindowRect(hwnd, &rect);
136 win_t *win = new0(win_t);
139 win->w = rect.right - rect.left;
140 win->h = rect.bottom - rect.top;
141 win->sys = new0(win_sys_t);
142 win->sys->hwnd = hwnd;
143 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
144 win->x, win->y, win->w, win->h);
148 static int win_cmp(const void *_a, const void *_b)
150 const win_t *a = _a, *b = _b;
151 if (a->sys->hwnd < b->sys->hwnd) return -1;
152 if (a->sys->hwnd > b->sys->hwnd) return 1;
156 static win_t *win_find(HWND hwnd, int create)
160 //printf("win_find: %p, %d\n", dpy, (int)xid);
161 win_sys_t sys = {.hwnd=hwnd};
162 win_t tmp = {.sys=&sys};
163 win_t **old = NULL, *new = NULL;
164 if ((old = tfind(&tmp, &cache, win_cmp)))
166 if (create && (new = win_new(hwnd,1)))
167 tsearch(new, &cache, win_cmp);
171 static void win_remove(win_t *win)
173 tdelete(win, &cache, win_cmp);
178 static win_t *win_cursor(void)
182 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
185 static win_t *win_focused(void)
187 return win_find(GetForegroundWindow(),0);
191 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
193 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
194 Key_t key = w2key(st->vkCode);
195 mod_t mod = getmod();
196 mod.up = !!(st->flags & 0x80);
197 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
199 st->vkCode, st->scanCode, st->flags,
201 return wm_handle_key(win_focused() ?: root, key, mod, getptr())
202 || CallNextHookEx(0, msg, wParam, lParam);
205 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
207 Key_t key = key_none;
208 mod_t mod = getmod();
209 win_t *win = win_cursor();
211 /* Update modifiers */
213 case WM_LBUTTONDOWN: mod.up = 0; key = key_mouse1; break;
214 case WM_LBUTTONUP: mod.up = 1; key = key_mouse1; break;
215 case WM_RBUTTONDOWN: mod.up = 0; key = key_mouse3; break;
216 case WM_RBUTTONUP: mod.up = 1; key = key_mouse3; break;
219 /* Check for focus-in/focus-out */
220 static win_t *old = NULL;
221 if (win && win != old) {
222 wm_handle_key(old, key_leave, mod, getptr());
223 wm_handle_key(win, key_enter, mod, getptr());
227 /* Send mouse movement event */
228 if (wParam == WM_MOUSEMOVE)
229 return wm_handle_ptr(win_cursor(), getptr());
230 else if (key != key_none)
231 return wm_handle_key(win_cursor(), key, mod, getptr());
233 return CallNextHookEx(0, msg, wParam, lParam);
236 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
238 HWND hwnd = (HWND)wParam;
241 case HSHELL_WINDOWCREATED:
243 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
244 "redraw" : "window created");
245 if (!(win = win_find(hwnd,0)))
246 if ((win = win_find(hwnd,1)))
249 case HSHELL_WINDOWDESTROYED:
250 printf("ShlProc: %p - window destroyed\n", hwnd);
251 if ((win = win_find(hwnd,0)) &&
252 win->state == st_show) {
257 case HSHELL_WINDOWACTIVATED:
258 printf("ShlProc: %p - window activated\n", hwnd);
261 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
266 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
268 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
270 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
271 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
272 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
273 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
275 if (msg == shellhookid)
276 if (ShlProc(wParam, lParam, 0))
278 return DefWindowProc(hwnd, msg, wParam, lParam);
281 BOOL CALLBACK MonProc(HMONITOR mon, HDC dc, LPRECT rect, LPARAM _screens)
283 MONITORINFO info = {.cbSize=sizeof(MONITORINFO)};
284 GetMonitorInfo(mon, &info);
285 RECT *work = &info.rcWork;
287 list_t **screens = (list_t**)_screens;
288 win_t *screen = new0(win_t);
289 screen->x = work->left;
290 screen->y = work->top;
291 screen->w = work->right - work->left;
292 screen->h = work->bottom - work->top;
293 *screens = list_append(*screens, screen);
294 printf("mon_proc: %d,%d %dx%d\n",
295 screen->x, screen->y, screen->w, screen->h);
299 BOOL CALLBACK LoopProc(HWND hwnd, LPARAM user)
302 if ((win = win_find(hwnd,1)))
307 BOOL WINAPI CtrlProc(DWORD type)
313 /********************
315 ********************/
316 void sys_move(win_t *win, int x, int y, int w, int h)
318 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
319 win->x = x; win->y = y;
320 win->w = MAX(w,1); win->h = MAX(h,1);
321 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
324 void sys_raise(win_t *win)
326 printf("sys_raise: %p\n", win);
328 /* See note in sys_focus */
329 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
330 DWORD newId = GetCurrentThreadId();
331 AttachThreadInput(oldId, newId, TRUE);
333 SetWindowPos(win->sys->hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
335 AttachThreadInput(oldId, newId, FALSE);
338 void sys_focus(win_t *win)
340 printf("sys_focus: %p\n", win);
342 /* Windows prevents a thread from using SetForegroundInput under
343 * certain circumstances and instead flashes the windows toolbar icon.
344 * Attaching the thread input queues avoids this behavior */
345 HWND fgWin = GetForegroundWindow();
346 if (fgWin == win->sys->hwnd)
347 return; // already focused
348 DWORD oldId = GetWindowThreadProcessId(fgWin, NULL);
349 DWORD newId = GetCurrentThreadId();
351 AttachThreadInput(oldId, newId, TRUE);
353 HWND next = GetWindow(win->sys->hwnd, GW_HWNDNEXT);
354 SetWindowPos(win->sys->hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
356 SetWindowPos(win->sys->hwnd, next, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
359 AttachThreadInput(oldId, newId, FALSE);
362 void sys_show(win_t *win, state_t state)
368 [st_show ] {"show" , SW_SHOW },
369 [st_full ] {"full" , SW_MAXIMIZE},
370 [st_shade] {"shade", SW_SHOW },
371 [st_icon ] {"icon" , SW_MINIMIZE},
372 [st_hide ] {"hide" , SW_HIDE },
374 if (win->state != state && win->state == st_shade)
375 SetWindowRgn(win->sys->hwnd, NULL, TRUE);
377 printf("sys_show: %s\n", map[state].str);
378 ShowWindow(win->sys->hwnd, map[state].cmd);
379 if (state == st_shade)
380 SetWindowRgn(win->sys->hwnd, CreateRectRgn(0,0,win->w,25), TRUE);
383 void sys_watch(win_t *win, Key_t key, mod_t mod)
386 //printf("sys_watch: %p\n", win);
389 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
392 //printf("sys_unwatch: %p\n", win);
395 list_t *sys_info(win_t *win)
398 EnumDisplayMonitors(NULL, NULL, MonProc, (LPARAM)&screens);
402 win_t *sys_init(void)
404 HINSTANCE hInst = GetModuleHandle(NULL);
407 /* Load configuration */
408 NO_CAPTURE = conf_get_int("main.no-capture", NO_CAPTURE);
410 /* Setup window class */
412 .cbSize = sizeof(WNDCLASSEX),
413 .lpfnWndProc = WndProc,
415 .lpszClassName = "wmpus_class",
417 if (!RegisterClassEx(&wc))
418 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
422 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
424 /* Create shell hook window */
425 if (!(hwnd = CreateWindowEx(0, "wmpus_class", "wmpus", 0,
426 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
427 HWND_MESSAGE, NULL, hInst, NULL)))
428 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
430 /* Register shell hook */
431 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
432 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
433 if (!RegisterShellHookWindow)
434 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
435 if (!RegisterShellHookWindow(hwnd))
436 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
437 shellhookid = RegisterWindowMessage("SHELLHOOK");
440 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
441 SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
442 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
444 /* Alternate ways to get input */
445 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
446 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
447 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
448 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
450 /* Capture ctrl-c and console widnow close */
451 SetConsoleCtrlHandler(CtrlProc, TRUE);
453 return root = win_new(hwnd,0);
456 void sys_run(win_t *root)
460 EnumWindows(LoopProc, 0);
461 while (GetMessage(&msg, NULL, 0, 0) > 0 &&
462 msg.message != WM_QUIT) {
463 TranslateMessage(&msg);
464 DispatchMessage(&msg);
470 PostMessage(root->sys->hwnd, WM_QUIT, 0, 0);
473 void sys_free(win_t *root)
475 /* I don't really care about this
476 * since I don't know how to use
477 * valgrind on win32 anyway.. */