2 * Copyright (c) 2011-2012, 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 0x0501
33 static int no_capture = 0;
34 static int stack = 25;
36 /* Internal structures */
47 static int shellhookid;
50 static list_t *screens;
52 /* Conversion functions */
53 static event_map_t ev2vk[] = {
54 {EV_MOUSE1 , VK_LBUTTON },
55 {EV_MOUSE2 , VK_MBUTTON },
56 {EV_MOUSE3 , VK_RBUTTON },
58 {EV_RIGHT , VK_RIGHT },
63 {EV_PAGEUP , VK_PRIOR },
64 {EV_PAGEDOWN, VK_NEXT },
77 {EV_SHIFT , VK_SHIFT },
78 {EV_SHIFT , VK_LSHIFT },
79 {EV_SHIFT , VK_RSHIFT },
80 {EV_CTRL , VK_CONTROL },
81 {EV_CTRL , VK_LCONTROL},
82 {EV_CTRL , VK_RCONTROL},
91 static event_t w2ev(UINT vk)
93 event_map_t *em = map_getr(ev2vk,vk);
94 return em ? em->ev : tolower(vk);
97 static UINT ev2w(event_t ev)
99 event_map_t *em = map_get(ev2vk,ev);
100 return em ? em->vk : toupper(ev);
103 static mod_t getmod(void)
106 .alt = GetKeyState(VK_MENU) < 0,
107 .ctrl = GetKeyState(VK_CONTROL) < 0,
108 .shift = GetKeyState(VK_SHIFT) < 0,
109 .win = GetKeyState(VK_LWIN) < 0 ||
110 GetKeyState(VK_RWIN) < 0,
115 static ptr_t getptr(void)
119 return (ptr_t){-1, -1, wptr.x, wptr.y};
122 /* Window functions */
123 static win_t *win_new(HWND hwnd, int checkwin)
127 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
128 GetClassName(hwnd, winclass, sizeof(winclass));
129 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
130 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
131 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
132 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
136 GetWindowRect(hwnd, &rect);
137 win_t *win = new0(win_t);
140 win->w = rect.right - rect.left;
141 win->h = rect.bottom - rect.top;
142 win->sys = new0(win_sys_t);
143 win->sys->hwnd = hwnd;
144 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
145 win->x, win->y, win->w, win->h);
149 static int win_cmp(const void *_a, const void *_b)
151 const win_t *a = _a, *b = _b;
152 if (a->sys->hwnd < b->sys->hwnd) return -1;
153 if (a->sys->hwnd > b->sys->hwnd) return 1;
157 static win_t *win_find(HWND hwnd, int create)
161 //printf("win_find: %p, %d\n", dpy, (int)xid);
162 win_sys_t sys = {.hwnd=hwnd};
163 win_t tmp = {.sys=&sys};
164 win_t **old = NULL, *new = NULL;
165 if ((old = tfind(&tmp, &cache, win_cmp)))
167 if (create && (new = win_new(hwnd,1)))
168 tsearch(new, &cache, win_cmp);
172 static void win_remove(win_t *win)
174 tdelete(win, &cache, win_cmp);
179 static win_t *win_cursor(void)
183 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
186 static win_t *win_focused(void)
188 return win_find(GetForegroundWindow(),0);
192 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
194 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
195 event_t ev = w2ev(st->vkCode);
196 mod_t mod = getmod();
197 mod.up = !!(st->flags & 0x80);
198 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
200 st->vkCode, st->scanCode, st->flags,
202 return wm_handle_event(win_focused() ?: root, ev, mod, getptr())
203 || CallNextHookEx(0, msg, wParam, lParam);
206 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
208 event_t ev = EV_NONE;
209 mod_t mod = getmod();
210 win_t *win = win_cursor();
212 /* Update modifiers */
214 case WM_LBUTTONDOWN: mod.up = 0; ev = EV_MOUSE1; break;
215 case WM_LBUTTONUP: mod.up = 1; ev = EV_MOUSE1; break;
216 case WM_RBUTTONDOWN: mod.up = 0; ev = EV_MOUSE3; break;
217 case WM_RBUTTONUP: mod.up = 1; ev = EV_MOUSE3; break;
220 /* Check for focus-in/focus-out */
221 static win_t *old = NULL;
222 if (win && win != old) {
223 wm_handle_event(old, EV_LEAVE, mod, getptr());
224 wm_handle_event(win, EV_ENTER, mod, getptr());
228 /* Send mouse movement event */
229 if (wParam == WM_MOUSEMOVE)
230 return wm_handle_ptr(win_cursor(), getptr());
231 else if (ev != EV_NONE)
232 return wm_handle_event(win_cursor(), ev, mod, getptr());
234 return CallNextHookEx(0, msg, wParam, lParam);
237 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
239 HWND hwnd = (HWND)wParam;
243 case HSHELL_WINDOWCREATED:
244 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
245 "redraw" : "window created");
246 if (!(win = win_find(hwnd,0)))
247 if ((win = win_find(hwnd,1)))
250 case HSHELL_WINDOWREPLACED:
251 case HSHELL_WINDOWDESTROYED:
252 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_WINDOWREPLACED ?
253 "window replaced" : "window destroyed");
254 if ((win = win_find(hwnd,0)) &&
255 (win->state == ST_SHOW ||
256 win->state == ST_SHADE)) {
261 case HSHELL_WINDOWACTIVATED:
262 printf("ShlProc: %p - window activated\n", hwnd);
263 // Fake button-click (causes crazy switching)
264 //if ((win = win_find(hwnd,0)))
265 // wm_handle_event(win, EV_MOUSE1, MOD(), getptr());
268 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
273 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
275 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
277 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
278 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
279 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
280 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
282 if (msg == shellhookid)
283 if (ShlProc(wParam, lParam, 0))
285 return DefWindowProc(hwnd, msg, wParam, lParam);
288 BOOL CALLBACK MonProc(HMONITOR mon, HDC dc, LPRECT rect, LPARAM _screens)
290 MONITORINFO info = {.cbSize=sizeof(MONITORINFO)};
291 GetMonitorInfo(mon, &info);
292 RECT *work = &info.rcWork;
294 list_t **screens = (list_t**)_screens;
295 win_t *screen = new0(win_t);
296 screen->x = work->left;
297 screen->y = work->top;
298 screen->z = !!(info.dwFlags & MONITORINFOF_PRIMARY);
299 screen->w = work->right - work->left;
300 screen->h = work->bottom - work->top;
301 *screens = list_append(*screens, screen);
302 printf("mon_proc: %d,%d %dx%d\n",
303 screen->x, screen->y, screen->w, screen->h);
307 BOOL CALLBACK LoopProc(HWND hwnd, LPARAM user)
310 if ((win = win_find(hwnd,1)))
315 BOOL WINAPI CtrlProc(DWORD type)
321 /********************
323 ********************/
324 void sys_move(win_t *win, int x, int y, int w, int h)
326 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
327 win->x = x; win->y = y;
328 win->w = MAX(w,1); win->h = MAX(h,1);
329 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
332 void sys_raise(win_t *win)
334 printf("sys_raise: %p\n", win);
336 /* See note in sys_focus */
337 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
338 DWORD newId = GetCurrentThreadId();
339 AttachThreadInput(oldId, newId, TRUE);
341 SetWindowPos(win->sys->hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
343 AttachThreadInput(oldId, newId, FALSE);
346 void sys_focus(win_t *win)
348 printf("sys_focus: %p\n", win);
350 /* Windows prevents a thread from using SetForegroundInput under
351 * certain circumstances and instead flashes the windows toolbar icon.
352 * Attaching the thread input queues avoids this behavior */
353 HWND fgWin = GetForegroundWindow();
354 if (fgWin == win->sys->hwnd)
355 return; // already focused
356 DWORD oldId = GetWindowThreadProcessId(fgWin, NULL);
357 DWORD newId = GetCurrentThreadId();
359 AttachThreadInput(oldId, newId, TRUE);
361 HWND next = GetWindow(win->sys->hwnd, GW_HWNDNEXT);
362 SetWindowPos(win->sys->hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
364 SetWindowPos(win->sys->hwnd, next, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
367 AttachThreadInput(oldId, newId, FALSE);
370 void sys_show(win_t *win, state_t state)
376 [ST_SHOW ] {"show" , SW_SHOW },
377 [ST_FULL ] {"full" , SW_MAXIMIZE},
378 [ST_SHADE] {"shade", SW_SHOW },
379 [ST_ICON ] {"icon" , SW_MINIMIZE},
380 [ST_HIDE ] {"hide" , SW_HIDE },
382 if (win->state != state && win->state == ST_SHADE)
383 SetWindowRgn(win->sys->hwnd, NULL, TRUE);
385 printf("sys_show: %s\n", map[state].str);
386 ShowWindow(win->sys->hwnd, map[state].cmd);
387 if (state == ST_SHADE)
388 SetWindowRgn(win->sys->hwnd, CreateRectRgn(0,0,win->w,stack), TRUE);
391 void sys_watch(win_t *win, event_t ev, mod_t mod)
394 //printf("sys_watch: %p\n", win);
397 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
400 //printf("sys_unwatch: %p\n", win);
403 list_t *sys_info(win_t *win)
406 EnumDisplayMonitors(NULL, NULL, MonProc, (LPARAM)&screens);
410 win_t *sys_init(void)
412 HINSTANCE hInst = GetModuleHandle(NULL);
415 /* Load configuration */
416 no_capture = conf_get_int("main.no-capture", no_capture);
417 stack = conf_get_int("main.stack", stack);
419 /* Setup window class */
421 .cbSize = sizeof(WNDCLASSEX),
422 .lpfnWndProc = WndProc,
424 .lpszClassName = "wmpus_class",
426 if (!RegisterClassEx(&wc))
427 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
431 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
433 /* Create shell hook window */
434 if (!(hwnd = CreateWindowEx(0, "wmpus_class", "wmpus", 0,
435 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
436 HWND_MESSAGE, NULL, hInst, NULL)))
437 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
439 /* Register shell hook */
440 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
441 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
442 if (!RegisterShellHookWindow)
443 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
444 if (!RegisterShellHookWindow(hwnd))
445 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
446 shellhookid = RegisterWindowMessage("SHELLHOOK");
449 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
450 SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
451 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
453 /* Alternate ways to get input */
454 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
455 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
456 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
457 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
459 /* Capture ctrl-c and console widnow close */
460 SetConsoleCtrlHandler(CtrlProc, TRUE);
462 return root = win_new(hwnd,0);
465 void sys_run(win_t *root)
469 EnumWindows(LoopProc, 0);
470 while (GetMessage(&msg, NULL, 0, 0) > 0 &&
471 msg.message != WM_QUIT) {
472 TranslateMessage(&msg);
473 DispatchMessage(&msg);
479 PostMessage(root->sys->hwnd, WM_QUIT, 0, 0);
482 void sys_free(win_t *root)
484 /* I don't really care about this
485 * since I don't know how to use
486 * valgrind on win32 anyway.. */