6 #define WIN32_LEAN_AND_MEAN
7 #define _WIN32_WINNT 0x0500
16 /* Internal structures */
28 static int shellhookid;
32 /* Conversion functions */
33 static keymap_t key2vk[] = {
34 {key_mouse1 , VK_LBUTTON },
35 {key_mouse2 , VK_MBUTTON },
36 {key_mouse3 , VK_RBUTTON },
37 {key_left , VK_LEFT },
38 {key_right , VK_RIGHT },
40 {key_down , VK_DOWN },
41 {key_home , VK_HOME },
43 {key_pageup , VK_PRIOR },
44 {key_pagedown, VK_NEXT },
57 {key_shift , VK_SHIFT },
58 {key_shift , VK_LSHIFT },
59 {key_shift , VK_RSHIFT },
60 {key_ctrl , VK_CONTROL },
61 {key_ctrl , VK_LCONTROL},
62 {key_ctrl , VK_RCONTROL},
64 {key_alt , VK_LMENU },
65 {key_alt , VK_RMENU },
71 static Key_t w2key(UINT vk)
73 keymap_t *km = map_getr(key2vk,vk);
74 return km ? km->key : tolower(vk);
77 static UINT key2w(Key_t key)
79 keymap_t *km = map_get(key2vk,key);
80 return km ? km->vk : toupper(key);
83 static mod_t getmod(void)
86 .alt = GetKeyState(VK_MENU) < 0,
87 .ctrl = GetKeyState(VK_CONTROL) < 0,
88 .shift = GetKeyState(VK_SHIFT) < 0,
89 .win = GetKeyState(VK_LWIN) < 0 ||
90 GetKeyState(VK_RWIN) < 0,
95 static ptr_t getptr(void)
99 return (ptr_t){-1, -1, wptr.x, wptr.y};
103 static win_t *win_new(HWND hwnd, int checkwin)
107 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
108 GetClassName(hwnd, winclass, sizeof(winclass));
109 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
110 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
111 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
112 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
116 GetWindowRect(hwnd, &rect);
117 win_t *win = new0(win_t);
120 win->w = rect.right - rect.left;
121 win->h = rect.bottom - rect.top;
122 win->sys = new0(win_sys_t);
123 win->sys->hwnd = hwnd;
124 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
125 win->x, win->y, win->w, win->h);
129 static int win_cmp(const void *_a, const void *_b)
131 const win_t *a = _a, *b = _b;
132 if (a->sys->hwnd < b->sys->hwnd) return -1;
133 if (a->sys->hwnd > b->sys->hwnd) return 1;
137 static win_t *win_find(HWND hwnd, int create)
141 //printf("win_find: %p, %d\n", dpy, (int)xid);
142 win_sys_t sys = {.hwnd=hwnd};
143 win_t tmp = {.sys=&sys};
144 win_t **old = NULL, *new = NULL;
145 if ((old = tfind(&tmp, &cache, win_cmp)))
147 if (create && (new = win_new(hwnd,1)))
148 tsearch(new, &cache, win_cmp);
152 static void win_remove(win_t *win)
154 tdelete(win, &cache, win_cmp);
159 static win_t *win_cursor(void)
163 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
166 static win_t *win_focused(void)
168 return win_find(GetForegroundWindow(),0);
172 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
174 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
175 Key_t key = w2key(st->vkCode);
176 mod_t mod = getmod();
177 mod.up = !!(st->flags & 0x80);
178 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
180 st->vkCode, st->scanCode, st->flags,
182 return wm_handle_key(win_focused() ?: root, key, mod, getptr())
183 || CallNextHookEx(0, msg, wParam, lParam);
186 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
188 Key_t key = key_none;
189 mod_t mod = getmod();
190 win_t *win = win_cursor();
192 /* Update modifiers */
194 case WM_LBUTTONDOWN: mod.up = 0; key = key_mouse1; break;
195 case WM_LBUTTONUP: mod.up = 1; key = key_mouse1; break;
196 case WM_RBUTTONDOWN: mod.up = 0; key = key_mouse3; break;
197 case WM_RBUTTONUP: mod.up = 1; key = key_mouse3; break;
200 /* Check for focus-in/focus-out */
201 static win_t *old = NULL;
202 if (win && win != old) {
203 wm_handle_key(old, key_leave, mod, getptr());
204 wm_handle_key(win, key_enter, mod, getptr());
208 /* Send mouse movement event */
209 if (wParam == WM_MOUSEMOVE)
210 return wm_handle_ptr(win_cursor(), getptr());
211 else if (key != key_none)
212 return wm_handle_key(win_cursor(), key, mod, getptr());
214 return CallNextHookEx(0, msg, wParam, lParam);
217 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
219 HWND hwnd = (HWND)wParam;
222 case HSHELL_WINDOWCREATED:
224 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
225 "redraw" : "window created");
226 if (!(win = win_find(hwnd,0)))
227 if ((win = win_find(hwnd,1)))
230 case HSHELL_WINDOWDESTROYED:
231 printf("ShlProc: %p - window destroyed\n", hwnd);
232 if ((win = win_find(hwnd,0)) &&
233 win->sys->state == st_show) {
238 case HSHELL_WINDOWACTIVATED:
239 printf("ShlProc: %p - window activated\n", hwnd);
242 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
247 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
249 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
251 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
252 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
253 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
254 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
256 if (msg == shellhookid)
257 if (ShlProc(wParam, lParam, 0))
259 return DefWindowProc(hwnd, msg, wParam, lParam);
265 void sys_move(win_t *win, int x, int y, int w, int h)
267 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
268 win->x = x; win->y = y;
269 win->w = MAX(w,1); win->h = MAX(h,1);
270 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
273 void sys_raise(win_t *win)
275 printf("sys_raise: %p\n", win);
276 SetForegroundWindow(win->sys->hwnd);
278 //HWND hwnd = win->sys->hwnd;
279 //HWND top = GetAncestor(hwnd,GA_ROOT);
280 //SetWindowPos(top, HWND_TOPMOST, 0, 0, 0, 0,
281 // SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
284 void sys_focus(win_t *win)
286 printf("sys_focus: %p\n", win);
288 /* Windows prevents a thread from using SetForegroundInput under
289 * certain circumstnaces and instead flashes the windows toolbar icon.
290 * Attaching the htread input queues avoids this behavior */
291 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
292 DWORD newId = GetWindowThreadProcessId(win->sys->hwnd, NULL);
293 AttachThreadInput(oldId, newId, TRUE);
294 SetForegroundWindow(win->sys->hwnd);
295 AttachThreadInput(oldId, newId, FALSE);
298 void sys_show(win_t *win, state_t state)
304 [st_show ] {"show" , SW_SHOW },
305 [st_full ] {"full" , SW_MAXIMIZE},
306 [st_shade] {"shade", SW_SHOW },
307 [st_icon ] {"icon" , SW_MINIMIZE},
308 [st_hide ] {"hide" , SW_HIDE },
310 win->sys->state = state;
311 printf("sys_show: %s\n", map[state].str);
312 ShowWindow(win->sys->hwnd, map[state].cmd);
315 void sys_watch(win_t *win, Key_t key, mod_t mod)
318 //printf("sys_watch: %p\n", win);
321 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
324 //printf("sys_unwatch: %p\n", win);
327 BOOL CALLBACK Mon(HMONITOR mon, HDC dc, LPRECT rect, LPARAM _screens)
329 MONITORINFO info = {.cbSize=sizeof(MONITORINFO)};
330 GetMonitorInfo(mon, &info);
331 RECT *work = &info.rcWork;
333 list_t **screens = (list_t**)_screens;
334 win_t *screen = new0(win_t);
335 screen->x = work->left;
336 screen->y = work->top;
337 screen->w = work->right - work->left;
338 screen->h = work->bottom - work->top;
339 *screens = list_append(*screens, screen);
340 printf("mon_proc: %d,%d %dx%d\n",
341 screen->x, screen->y, screen->w, screen->h);
345 list_t *sys_info(win_t *win)
347 list_t *screens = NULL;
348 EnumDisplayMonitors(NULL, NULL, Mon, (LPARAM)&screens);
352 win_t *sys_init(void)
354 HINSTANCE hInst = GetModuleHandle(NULL);
357 /* Setup window class */
359 .cbSize = sizeof(WNDCLASSEX),
360 .lpfnWndProc = WndProc,
362 .lpszClassName = "wmpus_class",
364 if (!RegisterClassEx(&wc))
365 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
369 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
371 /* Create shell hook window */
372 if (!(hwnd = CreateWindowEx(0, "wmpus_class", "wmpus", 0,
373 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
374 HWND_MESSAGE, NULL, hInst, NULL)))
375 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
377 /* Register shell hook */
378 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
379 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
380 if (!RegisterShellHookWindow)
381 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
382 if (!RegisterShellHookWindow(hwnd))
383 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
384 shellhookid = RegisterWindowMessage("SHELLHOOK");
387 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
388 SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
389 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
391 /* Alternate ways to get input */
392 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
393 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
394 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
395 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
397 return root = win_new(hwnd,0);
400 void sys_run(win_t *root)
403 while (GetMessage(&msg, NULL, 0, 0) > 0) {
404 TranslateMessage(&msg);
405 DispatchMessage(&msg);