6 #define WIN32_LEAN_AND_MEAN
7 #define _WIN32_WINNT 0x0500
16 /* Internal structures */
27 static int shellhookid;
28 static void *win_cache;
30 /* Conversion functions */
31 static keymap_t key2vk[] = {
32 {key_mouse1 , VK_LBUTTON },
33 {key_mouse2 , VK_MBUTTON },
34 {key_mouse3 , VK_RBUTTON },
35 {key_left , VK_LEFT },
36 {key_right , VK_RIGHT },
38 {key_down , VK_DOWN },
39 {key_home , VK_HOME },
41 {key_pageup , VK_PRIOR },
42 {key_pagedown, VK_NEXT },
55 {key_shift , VK_SHIFT },
56 {key_shift , VK_LSHIFT },
57 {key_shift , VK_RSHIFT },
58 {key_ctrl , VK_CONTROL },
59 {key_ctrl , VK_LCONTROL},
60 {key_ctrl , VK_RCONTROL},
62 {key_alt , VK_LMENU },
63 {key_alt , VK_RMENU },
69 static Key_t w2key(UINT vk)
71 keymap_t *km = map_getr(key2vk,vk);
72 return km ? km->key : tolower(vk);
75 static UINT key2w(Key_t key)
77 keymap_t *km = map_get(key2vk,key);
78 return km ? km->vk : toupper(key);
81 static mod_t getmod(void)
84 .alt = GetKeyState(VK_MENU) < 0,
85 .ctrl = GetKeyState(VK_CONTROL) < 0,
86 .shift = GetKeyState(VK_SHIFT) < 0,
87 .win = GetKeyState(VK_LWIN) < 0 ||
88 GetKeyState(VK_RWIN) < 0,
93 static ptr_t getptr(void)
97 return (ptr_t){-1, -1, wptr.x, wptr.y};
101 static win_t *win_new(HWND hwnd, int checkwin)
105 int exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
106 GetClassName(hwnd, winclass, sizeof(winclass));
107 if (!IsWindowVisible(hwnd)) return NULL; // Invisible stuff..
108 if (!strcmp("#32770", winclass)) return NULL; // Message boxes
109 if (GetWindow(hwnd, GW_OWNER)) return NULL; // Dialog boxes, etc
110 if (exstyle & WS_EX_TOOLWINDOW) return NULL; // Floating toolbars
114 GetWindowRect(hwnd, &rect);
115 win_t *win = new0(win_t);
118 win->w = rect.right - rect.left;
119 win->h = rect.bottom - rect.top;
120 win->sys = new0(win_sys_t);
121 win->sys->hwnd = hwnd;
122 printf("win_new: %p = %p (%d,%d %dx%d)\n", win, hwnd,
123 win->x, win->y, win->w, win->h);
127 static int win_cmp(const void *_a, const void *_b)
129 const win_t *a = _a, *b = _b;
130 if (a->sys->hwnd < b->sys->hwnd) return -1;
131 if (a->sys->hwnd > b->sys->hwnd) return 1;
135 static win_t *win_find(HWND hwnd, int create)
139 //printf("win_find: %p, %d\n", dpy, (int)xid);
140 win_sys_t sys = {.hwnd=hwnd};
141 win_t tmp = {.sys=&sys};
142 win_t **old = NULL, *new = NULL;
143 if ((old = tfind(&tmp, &win_cache, win_cmp)))
145 if (create && (new = win_new(hwnd,1)))
146 tsearch(new, &win_cache, win_cmp);
150 static void win_remove(win_t *win)
152 tdelete(win, &win_cache, win_cmp);
157 static win_t *win_cursor(void)
161 return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
164 static win_t *win_focused(void)
166 return win_find(GetForegroundWindow(),0);
170 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
172 KBDLLHOOKSTRUCT *st = (KBDLLHOOKSTRUCT *)lParam;
173 Key_t key = w2key(st->vkCode);
174 mod_t mod = getmod();
175 mod.up = !!(st->flags & 0x80);
176 printf("KbdProc: %d,%x,%lx - %lx,%lx,%lx - %x,%x\n",
178 st->vkCode, st->scanCode, st->flags,
180 return wm_handle_key(win_focused(), key, mod, getptr())
181 || CallNextHookEx(0, msg, wParam, lParam);
184 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
186 Key_t key = key_none;
187 mod_t mod = getmod();
189 case WM_LBUTTONDOWN: mod.up = 0; key = key_mouse1; break;
190 case WM_LBUTTONUP: mod.up = 1; key = key_mouse1; break;
191 case WM_RBUTTONDOWN: mod.up = 0; key = key_mouse3; break;
192 case WM_RBUTTONUP: mod.up = 1; key = key_mouse3; break;
194 if (wParam == WM_MOUSEMOVE)
195 return wm_handle_ptr(win_cursor(), getptr());
196 else if (key != key_none)
197 return wm_handle_key(win_cursor(), key, mod, getptr());
199 return CallNextHookEx(0, msg, wParam, lParam);
202 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
204 HWND hwnd = (HWND)wParam;
207 case HSHELL_WINDOWCREATED:
209 printf("ShlProc: %p - %s\n", hwnd, msg == HSHELL_REDRAW ?
210 "redraw" : "window created");
211 if (!(win = win_find(hwnd,0)))
212 if ((win = win_find(hwnd,1)))
215 case HSHELL_WINDOWDESTROYED:
216 printf("ShlProc: %p - window destroyed\n", hwnd);
217 if ((win = win_find(hwnd,0))) {
222 case HSHELL_WINDOWACTIVATED:
223 printf("ShlProc: %p - window activated\n", hwnd);
226 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
231 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
233 //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
235 case WM_CREATE: printf("WndProc: %p - create\n", hwnd); return 0;
236 case WM_CLOSE: printf("WndProc: %p - close\n", hwnd); return 0;
237 case WM_DESTROY: printf("WndProc: %p - destroy\n", hwnd); return 0;
238 case WM_HOTKEY: printf("WndProc: %p - hotkey\n", hwnd); return 0;
240 if (msg == shellhookid)
241 if (ShlProc(wParam, lParam, 0))
243 return DefWindowProc(hwnd, msg, wParam, lParam);
249 void sys_move(win_t *win, int x, int y, int w, int h)
251 printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
252 win->x = MAX(x,0); win->y = MAX(y,0);
253 win->w = MAX(w,1); win->h = MAX(h,1);
254 MoveWindow(win->sys->hwnd, win->x, win->y, win->w, win->h, TRUE);
257 void sys_raise(win_t *win)
259 printf("sys_raise: %p\n", win);
260 SetForegroundWindow(win->sys->hwnd);
262 //HWND hwnd = win->sys->hwnd;
263 //HWND top = GetAncestor(hwnd,GA_ROOT);
264 //SetWindowPos(top, HWND_TOPMOST, 0, 0, 0, 0,
265 // SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
268 void sys_focus(win_t *win)
270 printf("sys_focus: %p\n", win);
272 /* Windows prevents a thread from using SetForegroundInput under
273 * certain circumstnaces and instead flashes the windows toolbar icon.
274 * Attaching the htread input queues avoids this behavior */
275 DWORD oldId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
276 DWORD newId = GetWindowThreadProcessId(win->sys->hwnd, NULL);
277 AttachThreadInput(oldId, newId, TRUE);
278 SetForegroundWindow(win->sys->hwnd);
279 AttachThreadInput(oldId, newId, FALSE);
282 void sys_watch(win_t *win, Key_t key, mod_t mod)
285 printf("sys_watch: %p\n", win);
288 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
291 printf("sys_unwatch: %p\n", win);
294 win_t *sys_init(void)
296 HINSTANCE hInst = GetModuleHandle(NULL);
299 /* Setup AWM window class */
301 .cbSize = sizeof(WNDCLASSEX),
302 .lpfnWndProc = WndProc,
304 .lpszClassName = "awm_class",
306 if (!RegisterClassEx(&wc))
307 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
311 SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
313 /* Create shell hook window */
314 if (!(hwnd = CreateWindowEx(0, "awm_class", "awm", 0,
315 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
316 HWND_MESSAGE, NULL, hInst, NULL)))
317 printf("sys_init: Error Creating Shell Hook Window - %lu\n", GetLastError());
319 /* Register shell hook */
320 BOOL (*RegisterShellHookWindow)(HWND) = (void*)GetProcAddress(
321 GetModuleHandle("USER32.DLL"), "RegisterShellHookWindow");
322 if (!RegisterShellHookWindow)
323 printf("sys_init: Error Finding RegisterShellHookWindow - %lu\n", GetLastError());
324 if (!RegisterShellHookWindow(hwnd))
325 printf("sys_init: Error Registering ShellHook Window - %lu\n", GetLastError());
326 shellhookid = RegisterWindowMessage("SHELLHOOK");
329 SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
330 //SetWindowsHookEx(WH_MOUSE_LL, MllProc, hInst, 0);
331 //SetWindowsHookEx(WH_SHELL, ShlProc, hInst, 0);
333 /* Alternate ways to get input */
334 //if (!RegisterHotKey(hwnd, 123, MOD_CONTROL, VK_LBUTTON))
335 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
336 //if (!RegisterHotKey(NULL, 123, MOD_CONTROL, VK_LBUTTON))
337 // printf("sys_init: Error Registering Hotkey - %lu\n", GetLastError());
339 return win_new(hwnd,0);
342 void sys_run(win_t *root)
345 while (GetMessage(&msg, NULL, 0, 0) > 0) {
346 TranslateMessage(&msg);
347 DispatchMessage(&msg);