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
31 /* Internal structures */
43 static int shellhookid;
47 /* Conversion functions */
48 static keymap_t key2vk[] = {
49 {key_mouse1 , VK_LBUTTON },
50 {key_mouse2 , VK_MBUTTON },
51 {key_mouse3 , VK_RBUTTON },
52 {key_left , VK_LEFT },
53 {key_right , VK_RIGHT },
55 {key_down , VK_DOWN },
56 {key_home , VK_HOME },
58 {key_pageup , VK_PRIOR },
59 {key_pagedown, VK_NEXT },
72 {key_shift , VK_SHIFT },
73 {key_shift , VK_LSHIFT },
74 {key_shift , VK_RSHIFT },
75 {key_ctrl , VK_CONTROL },
76 {key_ctrl , VK_LCONTROL},
77 {key_ctrl , VK_RCONTROL},
79 {key_alt , VK_LMENU },
80 {key_alt , VK_RMENU },
86 static Key_t w2key(UINT vk)
88 keymap_t *km = map_getr(key2vk,vk);
89 return km ? km->key : tolower(vk);
92 static UINT key2w(Key_t key)
94 keymap_t *km = map_get(key2vk,key);
95 return km ? km->vk : toupper(key);
98 static mod_t getmod(void)
101 .alt = GetKeyState(VK_MENU) < 0,
102 .ctrl = GetKeyState(VK_CONTROL) < 0,
103 .shift = GetKeyState(VK_SHIFT) < 0,
104 .win = GetKeyState(VK_LWIN) < 0 ||
105 GetKeyState(VK_RWIN) < 0,
110 static ptr_t getptr(void)
114 return (ptr_t){-1, -1, wptr.x, wptr.y};
117 /* Window functions */
118 static win_t *win_new(HWND hwnd, int checkwin)
122 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
123 GetClassName(hwnd, winclass, sizeof(winclass));
124 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
125 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
126 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
127 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
131 GetWindowRect(hwnd, &rect);
132 win_t *win = new0(win_t);
135 win->w = rect.right - rect.left;
136 win->h = rect.bottom - rect.top;
137 win->sys = new0(win_sys_t);
138 win->sys->hwnd = hwnd;
139 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
140 win->x, win->y, win->w, win->h);
144 static int win_cmp(const void *_a, const void *_b)
146 const win_t *a = _a, *b = _b;
147 if (a->sys->hwnd < b->sys->hwnd) return -1;
148 if (a->sys->hwnd > b->sys->hwnd) return 1;
152 static win_t *win_find(HWND hwnd, int create)
156 //printf("win_find: %p, %d\n", dpy, (int)xid);
157 win_sys_t sys = {.hwnd=hwnd};
158 win_t tmp = {.sys=&sys};
159 win_t **old = NULL, *new = NULL;
160 if ((old = tfind(&tmp, &cache, win_cmp)))
162 if (create && (new = win_new(hwnd,1)))
163 tsearch(new, &cache, win_cmp);
167 static void win_remove(win_t *win)
169 tdelete(win, &cache, win_cmp);
174 static win_t *win_cursor(void)
178 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
181 static win_t *win_focused(void)
183 return win_find(GetForegroundWindow(),0);
187 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
189 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
190 Key_t key = w2key(st->vkCode);
191 mod_t mod = getmod();
192 mod.up = !!(st->flags & 0x80);
193 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
195 st->vkCode, st->scanCode, st->flags,
197 return wm_handle_key(win_focused() ?: root, key, mod, getptr())
198 || CallNextHookEx(0, msg, wParam, lParam);
201 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
203 Key_t key = key_none;
204 mod_t mod = getmod();
205 win_t *win = win_cursor();
207 /* Update modifiers */
209 case WM_LBUTTONDOWN: mod.up = 0; key = key_mouse1; break;
210 case WM_LBUTTONUP: mod.up = 1; key = key_mouse1; break;
211 case WM_RBUTTONDOWN: mod.up = 0; key = key_mouse3; break;
212 case WM_RBUTTONUP: mod.up = 1; key = key_mouse3; break;
215 /* Check for focus-in/focus-out */
216 static win_t *old = NULL;
217 if (win && win != old) {
218 wm_handle_key(old, key_leave, mod, getptr());
219 wm_handle_key(win, key_enter, mod, getptr());
223 /* Send mouse movement event */
224 if (wParam == WM_MOUSEMOVE)
225 return wm_handle_ptr(win_cursor(), getptr());
226 else if (key != key_none)
227 return wm_handle_key(win_cursor(), key, mod, getptr());
229 return CallNextHookEx(0, msg, wParam, lParam);
232 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
234 HWND hwnd = (HWND)wParam;
237 case HSHELL_WINDOWCREATED:
239 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
240 "redraw" : "window created");
241 if (!(win = win_find(hwnd,0)))
242 if ((win = win_find(hwnd,1)))
245 case HSHELL_WINDOWDESTROYED:
246 printf("ShlProc: %p - window destroyed\n", hwnd);
247 if ((win = win_find(hwnd,0)) &&
248 win->sys->state == st_show) {
253 case HSHELL_WINDOWACTIVATED:
254 printf("ShlProc: %p - window activated\n", hwnd);
257 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
262 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
264 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
266 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
267 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
268 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
269 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
271 if (msg == shellhookid)
272 if (ShlProc(wParam, lParam, 0))
274 return DefWindowProc(hwnd, msg, wParam, lParam);
277 BOOL CALLBACK MonProc(HMONITOR mon, HDC dc, LPRECT rect, LPARAM _screens)
279 MONITORINFO info = {.cbSize=sizeof(MONITORINFO)};
280 GetMonitorInfo(mon, &info);
281 RECT *work = &info.rcWork;
283 list_t **screens = (list_t**)_screens;
284 win_t *screen = new0(win_t);
285 screen->x = work->left;
286 screen->y = work->top;
287 screen->w = work->right - work->left;
288 screen->h = work->bottom - work->top;
289 *screens = list_append(*screens, screen);
290 printf("mon_proc: %d,%d %dx%d\n",
291 screen->x, screen->y, screen->w, screen->h);
295 /********************
297 ********************/
298 void sys_move(win_t *win, int x, int y, int w, int h)
300 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
301 win->x = x; win->y = y;
302 win->w = MAX(w,1); win->h = MAX(h,1);
303 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
306 void sys_raise(win_t *win)
308 printf("sys_raise: %p\n", win);
309 SetForegroundWindow(win->sys->hwnd);
311 //HWND hwnd = win->sys->hwnd;
312 //HWND top = GetAncestor(hwnd,GA_ROOT);
313 //SetWindowPos(top, HWND_TOPMOST, 0, 0, 0, 0,
314 // SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
317 void sys_focus(win_t *win)
319 printf("sys_focus: %p\n", win);
321 /* Windows prevents a thread from using SetForegroundInput under
322 * certain circumstances and instead flashes the windows toolbar icon.
323 * Attaching the thread input queues avoids this behavior */
324 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
325 DWORD newId = GetCurrentThreadId();
326 AttachThreadInput(oldId, newId, TRUE);
328 BringWindowToTop(win->sys->hwnd);
329 SetForegroundWindow(win->sys->hwnd);
330 SetFocus(win->sys->hwnd);
332 AttachThreadInput(oldId, newId, FALSE);
335 void sys_show(win_t *win, state_t state)
341 [st_show ] {"show" , SW_SHOW },
342 [st_full ] {"full" , SW_MAXIMIZE},
343 [st_shade] {"shade", SW_SHOW },
344 [st_icon ] {"icon" , SW_MINIMIZE},
345 [st_hide ] {"hide" , SW_HIDE },
347 win->sys->state = state;
348 printf("sys_show: %s\n", map[state].str);
349 ShowWindow(win->sys->hwnd, map[state].cmd);
352 void sys_watch(win_t *win, Key_t key, mod_t mod)
355 //printf("sys_watch: %p\n", win);
358 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
361 //printf("sys_unwatch: %p\n", win);
364 list_t *sys_info(win_t *win)
366 list_t *screens = NULL;
367 EnumDisplayMonitors(NULL, NULL, MonProc, (LPARAM)&screens);
371 win_t *sys_init(void)
373 HINSTANCE hInst = GetModuleHandle(NULL);
376 /* Setup window class */
378 .cbSize = sizeof(WNDCLASSEX),
379 .lpfnWndProc = WndProc,
381 .lpszClassName = "wmpus_class",
383 if (!RegisterClassEx(&wc))
384 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
388 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
390 /* Create shell hook window */
391 if (!(hwnd = CreateWindowEx(0, "wmpus_class", "wmpus", 0,
392 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
393 HWND_MESSAGE, NULL, hInst, NULL)))
394 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
396 /* Register shell hook */
397 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
398 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
399 if (!RegisterShellHookWindow)
400 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
401 if (!RegisterShellHookWindow(hwnd))
402 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
403 shellhookid = RegisterWindowMessage("SHELLHOOK");
406 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
407 SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
408 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
410 /* Alternate ways to get input */
411 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
412 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
413 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
414 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
416 return root = win_new(hwnd,0);
419 void sys_run(win_t *root)
422 while (GetMessage(&msg, NULL, 0, 0) > 0) {
423 TranslateMessage(&msg);
424 DispatchMessage(&msg);