2 * Copyright (C) 2011 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #define WIN32_LEAN_AND_MEAN
24 #define _WIN32_WINNT 0x0500
33 /* Internal structures */
45 static int shellhookid;
49 /* Conversion functions */
50 static keymap_t key2vk[] = {
51 {key_mouse1 , VK_LBUTTON },
52 {key_mouse2 , VK_MBUTTON },
53 {key_mouse3 , VK_RBUTTON },
54 {key_left , VK_LEFT },
55 {key_right , VK_RIGHT },
57 {key_down , VK_DOWN },
58 {key_home , VK_HOME },
60 {key_pageup , VK_PRIOR },
61 {key_pagedown, VK_NEXT },
74 {key_shift , VK_SHIFT },
75 {key_shift , VK_LSHIFT },
76 {key_shift , VK_RSHIFT },
77 {key_ctrl , VK_CONTROL },
78 {key_ctrl , VK_LCONTROL},
79 {key_ctrl , VK_RCONTROL},
81 {key_alt , VK_LMENU },
82 {key_alt , VK_RMENU },
88 static Key_t w2key(UINT vk)
90 keymap_t *km = map_getr(key2vk,vk);
91 return km ? km->key : tolower(vk);
94 static UINT key2w(Key_t key)
96 keymap_t *km = map_get(key2vk,key);
97 return km ? km->vk : toupper(key);
100 static mod_t getmod(void)
103 .alt = GetKeyState(VK_MENU) < 0,
104 .ctrl = GetKeyState(VK_CONTROL) < 0,
105 .shift = GetKeyState(VK_SHIFT) < 0,
106 .win = GetKeyState(VK_LWIN) < 0 ||
107 GetKeyState(VK_RWIN) < 0,
112 static ptr_t getptr(void)
116 return (ptr_t){-1, -1, wptr.x, wptr.y};
119 /* Window functions */
120 static win_t *win_new(HWND hwnd, int checkwin)
124 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
125 GetClassName(hwnd, winclass, sizeof(winclass));
126 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
127 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
128 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
129 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
133 GetWindowRect(hwnd, &rect);
134 win_t *win = new0(win_t);
137 win->w = rect.right - rect.left;
138 win->h = rect.bottom - rect.top;
139 win->sys = new0(win_sys_t);
140 win->sys->hwnd = hwnd;
141 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
142 win->x, win->y, win->w, win->h);
146 static int win_cmp(const void *_a, const void *_b)
148 const win_t *a = _a, *b = _b;
149 if (a->sys->hwnd < b->sys->hwnd) return -1;
150 if (a->sys->hwnd > b->sys->hwnd) return 1;
154 static win_t *win_find(HWND hwnd, int create)
158 //printf("win_find: %p, %d\n", dpy, (int)xid);
159 win_sys_t sys = {.hwnd=hwnd};
160 win_t tmp = {.sys=&sys};
161 win_t **old = NULL, *new = NULL;
162 if ((old = tfind(&tmp, &cache, win_cmp)))
164 if (create && (new = win_new(hwnd,1)))
165 tsearch(new, &cache, win_cmp);
169 static void win_remove(win_t *win)
171 tdelete(win, &cache, win_cmp);
176 static win_t *win_cursor(void)
180 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
183 static win_t *win_focused(void)
185 return win_find(GetForegroundWindow(),0);
189 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
191 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
192 Key_t key = w2key(st->vkCode);
193 mod_t mod = getmod();
194 mod.up = !!(st->flags & 0x80);
195 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
197 st->vkCode, st->scanCode, st->flags,
199 return wm_handle_key(win_focused() ?: root, key, mod, getptr())
200 || CallNextHookEx(0, msg, wParam, lParam);
203 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
205 Key_t key = key_none;
206 mod_t mod = getmod();
207 win_t *win = win_cursor();
209 /* Update modifiers */
211 case WM_LBUTTONDOWN: mod.up = 0; key = key_mouse1; break;
212 case WM_LBUTTONUP: mod.up = 1; key = key_mouse1; break;
213 case WM_RBUTTONDOWN: mod.up = 0; key = key_mouse3; break;
214 case WM_RBUTTONUP: mod.up = 1; key = key_mouse3; break;
217 /* Check for focus-in/focus-out */
218 static win_t *old = NULL;
219 if (win && win != old) {
220 wm_handle_key(old, key_leave, mod, getptr());
221 wm_handle_key(win, key_enter, mod, getptr());
225 /* Send mouse movement event */
226 if (wParam == WM_MOUSEMOVE)
227 return wm_handle_ptr(win_cursor(), getptr());
228 else if (key != key_none)
229 return wm_handle_key(win_cursor(), key, mod, getptr());
231 return CallNextHookEx(0, msg, wParam, lParam);
234 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
236 HWND hwnd = (HWND)wParam;
239 case HSHELL_WINDOWCREATED:
241 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
242 "redraw" : "window created");
243 if (!(win = win_find(hwnd,0)))
244 if ((win = win_find(hwnd,1)))
247 case HSHELL_WINDOWDESTROYED:
248 printf("ShlProc: %p - window destroyed\n", hwnd);
249 if ((win = win_find(hwnd,0)) &&
250 win->sys->state == st_show) {
255 case HSHELL_WINDOWACTIVATED:
256 printf("ShlProc: %p - window activated\n", hwnd);
259 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
264 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
266 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
268 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
269 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
270 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
271 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
273 if (msg == shellhookid)
274 if (ShlProc(wParam, lParam, 0))
276 return DefWindowProc(hwnd, msg, wParam, lParam);
279 BOOL CALLBACK MonProc(HMONITOR mon, HDC dc, LPRECT rect, LPARAM _screens)
281 MONITORINFO info = {.cbSize=sizeof(MONITORINFO)};
282 GetMonitorInfo(mon, &info);
283 RECT *work = &info.rcWork;
285 list_t **screens = (list_t**)_screens;
286 win_t *screen = new0(win_t);
287 screen->x = work->left;
288 screen->y = work->top;
289 screen->w = work->right - work->left;
290 screen->h = work->bottom - work->top;
291 *screens = list_append(*screens, screen);
292 printf("mon_proc: %d,%d %dx%d\n",
293 screen->x, screen->y, screen->w, screen->h);
297 /********************
299 ********************/
300 void sys_move(win_t *win, int x, int y, int w, int h)
302 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
303 win->x = x; win->y = y;
304 win->w = MAX(w,1); win->h = MAX(h,1);
305 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
308 void sys_raise(win_t *win)
310 printf("sys_raise: %p\n", win);
311 SetForegroundWindow(win->sys->hwnd);
313 //HWND hwnd = win->sys->hwnd;
314 //HWND top = GetAncestor(hwnd,GA_ROOT);
315 //SetWindowPos(top, HWND_TOPMOST, 0, 0, 0, 0,
316 // SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
319 void sys_focus(win_t *win)
321 printf("sys_focus: %p\n", win);
323 /* Windows prevents a thread from using SetForegroundInput under
324 * certain circumstances and instead flashes the windows toolbar icon.
325 * Attaching the thread input queues avoids this behavior */
326 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
327 DWORD newId = GetWindowThreadProcessId(win->sys->hwnd, NULL);
328 AttachThreadInput(oldId, newId, TRUE);
329 SetForegroundWindow(win->sys->hwnd);
330 AttachThreadInput(oldId, newId, FALSE);
333 void sys_show(win_t *win, state_t state)
339 [st_show ] {"show" , SW_SHOW },
340 [st_full ] {"full" , SW_MAXIMIZE},
341 [st_shade] {"shade", SW_SHOW },
342 [st_icon ] {"icon" , SW_MINIMIZE},
343 [st_hide ] {"hide" , SW_HIDE },
345 win->sys->state = state;
346 printf("sys_show: %s\n", map[state].str);
347 ShowWindow(win->sys->hwnd, map[state].cmd);
350 void sys_watch(win_t *win, Key_t key, mod_t mod)
353 //printf("sys_watch: %p\n", win);
356 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
359 //printf("sys_unwatch: %p\n", win);
362 list_t *sys_info(win_t *win)
364 list_t *screens = NULL;
365 EnumDisplayMonitors(NULL, NULL, MonProc, (LPARAM)&screens);
369 win_t *sys_init(void)
371 HINSTANCE hInst = GetModuleHandle(NULL);
374 /* Setup window class */
376 .cbSize = sizeof(WNDCLASSEX),
377 .lpfnWndProc = WndProc,
379 .lpszClassName = "wmpus_class",
381 if (!RegisterClassEx(&wc))
382 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
386 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
388 /* Create shell hook window */
389 if (!(hwnd = CreateWindowEx(0, "wmpus_class", "wmpus", 0,
390 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
391 HWND_MESSAGE, NULL, hInst, NULL)))
392 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
394 /* Register shell hook */
395 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
396 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
397 if (!RegisterShellHookWindow)
398 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
399 if (!RegisterShellHookWindow(hwnd))
400 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
401 shellhookid = RegisterWindowMessage("SHELLHOOK");
404 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
405 SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
406 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
408 /* Alternate ways to get input */
409 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
410 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
411 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
412 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
414 return root = win_new(hwnd,0);
417 void sys_run(win_t *root)
420 while (GetMessage(&msg, NULL, 0, 0) > 0) {
421 TranslateMessage(&msg);
422 DispatchMessage(&msg);