]> Pileus Git - wmpus/blob - sys-win32.c
Add multi-monitor support to wm-wmii
[wmpus] / sys-win32.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <search.h>
5
6 #define WIN32_LEAN_AND_MEAN
7 #define _WIN32_WINNT 0x0500
8 #include <windows.h>
9 #include <winbase.h>
10 #include <winuser.h>
11
12 #include "util.h"
13 #include "sys.h"
14 #include "wm.h"
15
16 /* Internal structures */
17 struct win_sys {
18         HWND hwnd;
19 };
20
21 typedef struct {
22         Key_t key;
23         int   vk;
24 } keymap_t;
25
26 /* Global data */
27 static int   shellhookid;
28 static void *win_cache;
29
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   },
37         {key_up      , VK_UP      },
38         {key_down    , VK_DOWN    },
39         {key_home    , VK_HOME    },
40         {key_end     , VK_END     },
41         {key_pageup  , VK_PRIOR   },
42         {key_pagedown, VK_NEXT    },
43         {key_f1      , VK_F1      },
44         {key_f2      , VK_F2      },
45         {key_f3      , VK_F3      },
46         {key_f4      , VK_F4      },
47         {key_f5      , VK_F5      },
48         {key_f6      , VK_F6      },
49         {key_f7      , VK_F7      },
50         {key_f8      , VK_F8      },
51         {key_f9      , VK_F9      },
52         {key_f10     , VK_F10     },
53         {key_f11     , VK_F11     },
54         {key_f12     , VK_F12     },
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},
61         {key_alt     , VK_MENU    },
62         {key_alt     , VK_LMENU   },
63         {key_alt     , VK_RMENU   },
64         {key_win     , VK_LWIN    },
65         {key_win     , VK_RWIN    },
66 };
67
68 /* - Keycodes */
69 static Key_t w2key(UINT vk)
70 {
71         keymap_t *km = map_getr(key2vk,vk);
72         return km ? km->key : tolower(vk);
73 }
74
75 static UINT key2w(Key_t key)
76 {
77         keymap_t *km = map_get(key2vk,key);
78         return km ? km->vk : toupper(key);
79 }
80
81 static mod_t getmod(void)
82 {
83         return (mod_t){
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,
89         };
90 }
91
92 /* - Pointers */
93 static ptr_t getptr(void)
94 {
95         POINT wptr;
96         GetCursorPos(&wptr);
97         return (ptr_t){-1, -1, wptr.x, wptr.y};
98 }
99
100 /* Helpers */
101 static win_t *win_new(HWND hwnd, int checkwin)
102 {
103         if (checkwin) {
104                 char winclass[256];
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
111         }
112
113         RECT rect = {};
114         GetWindowRect(hwnd, &rect);
115         win_t *win = new0(win_t);
116         win->x         = rect.left;
117         win->y         = rect.top;
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);
124         return win;
125 }
126
127 static int win_cmp(const void *_a, const void *_b)
128 {
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;
132         return 0;
133 }
134
135 static win_t *win_find(HWND hwnd, int create)
136 {
137         if (!hwnd)
138                 return NULL;
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)))
144                 return *old;
145         if (create && (new = win_new(hwnd,1)))
146                 tsearch(new, &win_cache, win_cmp);
147         return new;
148 }
149
150 static void win_remove(win_t *win)
151 {
152         tdelete(win, &win_cache, win_cmp);
153         free(win->sys);
154         free(win);
155 }
156
157 static win_t *win_cursor(void)
158 {
159         POINT wptr;
160         GetCursorPos(&wptr);
161         return win_find(GetAncestor(WindowFromPoint(wptr),GA_ROOT),0);
162 }
163
164 static win_t *win_focused(void)
165 {
166         return win_find(GetForegroundWindow(),0);
167 }
168
169 /* Callbacks */
170 LRESULT CALLBACK KbdProc(int msg, WPARAM wParam, LPARAM lParam)
171 {
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",
177                         msg, wParam, lParam,
178                         st->vkCode, st->scanCode, st->flags,
179                         key, mod2int(mod));
180         return wm_handle_key(win_focused(), key, mod, getptr())
181                 || CallNextHookEx(0, msg, wParam, lParam);
182 }
183
184 LRESULT CALLBACK MllProc(int msg, WPARAM wParam, LPARAM lParam)
185 {
186         Key_t key = key_none;
187         mod_t mod = getmod();
188         switch (wParam) {
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;
193         }
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());
198         else
199                 return CallNextHookEx(0, msg, wParam, lParam);
200 }
201
202 LRESULT CALLBACK ShlProc(int msg, WPARAM wParam, LPARAM lParam)
203 {
204         HWND hwnd = (HWND)wParam;
205         win_t *win = NULL;
206         switch (msg) {
207         case HSHELL_WINDOWCREATED:
208         case HSHELL_REDRAW:
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)))
213                                 wm_insert(win);
214                 return 1;
215         case HSHELL_WINDOWDESTROYED:
216                 printf("ShlProc: %p - window destroyed\n", hwnd);
217                 if ((win = win_find(hwnd,0))) {
218                         wm_remove(win);
219                         win_remove(win);
220                 }
221                 return 1;
222         case HSHELL_WINDOWACTIVATED:
223                 printf("ShlProc: %p - window activated\n", hwnd);
224                 return 1;
225         default:
226                 printf("ShlProc: %p - unknown msg, %d\n", hwnd, msg);
227                 return 0;
228         }
229 }
230
231 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
232 {
233         //printf("WndProc: %d, %x, %lx\n", msg, wParam, lParam);
234         switch (msg) {
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;
239         }
240         if (msg == shellhookid)
241                 if (ShlProc(wParam, lParam, 0))
242                         return 1;
243         return DefWindowProc(hwnd, msg, wParam, lParam);
244 }
245
246 /*****************
247  * Sys functions *
248  *****************/
249 void sys_move(win_t *win, int x, int y, int w, int h)
250 {
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);
255 }
256
257 void sys_raise(win_t *win)
258 {
259         printf("sys_raise: %p\n", win);
260         SetForegroundWindow(win->sys->hwnd);
261
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);
266 }
267
268 void sys_focus(win_t *win)
269 {
270         printf("sys_focus: %p\n", win);
271
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);
280 }
281
282 void sys_watch(win_t *win, Key_t key, mod_t mod)
283 {
284         (void)key2w; // TODO
285         printf("sys_watch: %p\n", win);
286 }
287
288 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
289 {
290         (void)key2w; // TODO
291         printf("sys_unwatch: %p\n", win);
292 }
293
294 win_t *sys_init(void)
295 {
296         HINSTANCE hInst = GetModuleHandle(NULL);
297         HWND      hwnd  = NULL;
298
299         /* Setup AWM window class */
300         WNDCLASSEX wc    = {
301                 .cbSize        = sizeof(WNDCLASSEX),
302                 .lpfnWndProc   = WndProc,
303                 .hInstance     = hInst,
304                 .lpszClassName = "awm_class",
305         };
306         if (!RegisterClassEx(&wc))
307                 printf("sys_init: Error Registering Class - %lu\n", GetLastError());
308
309         /* Get work area */
310         RECT rc;
311         SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
312
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());
318
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");
327
328         /* Input hooks */
329         SetWindowsHookEx(WH_KEYBOARD_LL, KbdProc, hInst, 0);
330         //SetWindowsHookEx(WH_MOUSE_LL,    MllProc, hInst, 0);
331         //SetWindowsHookEx(WH_SHELL,       ShlProc, hInst, 0);
332
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());
338
339         return win_new(hwnd,0);
340 }
341
342 void sys_run(win_t *root)
343 {
344         MSG msg;
345         while (GetMessage(&msg, NULL, 0, 0) > 0) {
346                 TranslateMessage(&msg);
347                 DispatchMessage(&msg);
348         }
349 }