6 #include <X11/Xproto.h>
7 #include <X11/keysym.h>
13 /* Internal structures */
25 wm_proto, wm_focus, natoms
29 static void *win_cache = NULL;
30 static Atom atoms[natoms];
31 static int (*xerrorxlib)(Display *, XErrorEvent *);
33 /* Conversion functions */
34 static keymap_t key2sym[] = {
35 {key_left , XK_Left },
36 {key_right , XK_Right},
38 {key_down , XK_Down },
39 {key_home , XK_Home },
41 {key_pageup , XK_Prior},
42 {key_pagedown, XK_Next },
58 static mod_t x2mod(unsigned int state, int up)
61 .alt = !!(state & Mod1Mask ),
62 .ctrl = !!(state & ControlMask),
63 .shift = !!(state & ShiftMask ),
64 .win = !!(state & Mod4Mask ),
69 static unsigned int mod2x(mod_t mod)
71 return (mod.alt ? Mod1Mask : 0)
72 | (mod.ctrl ? ControlMask : 0)
73 | (mod.shift ? ShiftMask : 0)
74 | (mod.win ? Mod4Mask : 0);
78 static Key_t x2key(KeySym sym)
80 keymap_t *km = map_getr(key2sym,sym);
81 return km ? km->key : sym;
84 static KeySym key2x(Key_t key)
86 keymap_t *km = map_get(key2sym,key);
87 return km ? km->sym : key;
90 static Key_t x2btn(int btn)
92 return btn + key_mouse0;
95 static int btn2x(Key_t key)
97 return key - key_mouse0;
101 static ptr_t x2ptr(XEvent *_ev)
103 XKeyEvent *ev = &_ev->xkey;
104 return (ptr_t){ev->x, ev->y, ev->x_root, ev->y_root};
106 static Window getfocus(win_t *root, XEvent *event)
109 Window focus = PointerRoot;
110 if (event->type == KeyPress || event->type == KeyRelease)
111 XGetInputFocus(root->sys->dpy, &focus, &revert);
112 if (focus == PointerRoot)
113 focus = event->xkey.subwindow;
115 focus = event->xkey.window;
119 /* Window functions */
120 static win_t *win_new(Display *dpy, Window xid)
122 XWindowAttributes attr;
123 if (XGetWindowAttributes(dpy, xid, &attr))
124 if (attr.override_redirect)
126 win_t *win = new0(win_t);
130 win->h = attr.height;
131 win->sys = new0(win_sys_t);
134 printf("%p = %p, %d\n", win, dpy, (int)xid);
138 static int win_cmp(const void *_a, const void *_b)
140 const win_t *a = _a, *b = _b;
141 if (a->sys->dpy < b->sys->dpy) return -1;
142 if (a->sys->dpy > b->sys->dpy) return 1;
143 if (a->sys->xid < b->sys->xid) return -1;
144 if (a->sys->xid > b->sys->xid) return 1;
148 static win_t *win_find(Display *dpy, Window xid, int create)
152 //printf("win_find: %p, %d\n", dpy, (int)xid);
153 win_sys_t sys = {.dpy=dpy, .xid=xid};
154 win_t tmp = {.sys=&sys};
155 win_t **old = NULL, *new = NULL;
156 if ((old = tfind(&tmp, &win_cache, win_cmp)))
158 if (create && (new = win_new(dpy,xid)))
159 tsearch(new, &win_cache, win_cmp);
163 static void win_remove(win_t *win)
165 tdelete(win, &win_cache, win_cmp);
170 static int win_viewable(win_t *win)
172 XWindowAttributes attr;
173 if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
174 return attr.map_state == IsViewable;
180 static void process_event(int type, XEvent *ev, win_t *root)
182 Display *dpy = root->sys->dpy;
184 printf("event: %d\n", type);
186 /* Common data for all these events ... */
187 ptr_t ptr; mod_t mod;
188 if (type == KeyPress || type == KeyRelease ||
189 type == ButtonPress || type == ButtonRelease ||
190 type == MotionNotify) {
191 Window xid = getfocus(root, ev);
192 if (!(win = win_find(dpy,xid,0)))
195 mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
198 /* Split based on event */
199 if (type == KeyPress) {
200 while (XCheckTypedEvent(dpy, KeyPress, ev));
201 KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
202 wm_handle_key(win, x2key(sym), mod, ptr);
204 else if (type == KeyRelease) {
205 //printf("release: %d\n", type);
207 else if (type == ButtonPress) {
208 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
209 XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
210 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
212 else if (type == ButtonRelease) {
213 XUngrabPointer(dpy, CurrentTime);
214 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
216 else if (type == MotionNotify) {
217 while (XCheckTypedEvent(dpy, MotionNotify, ev));
218 wm_handle_ptr(win, ptr);
220 else if (type == EnterNotify || type == LeaveNotify) {
221 printf("enter: %d\n", type);
222 key_t key = EnterNotify ? key_enter : key_leave;
223 if ((win = win_find(dpy,ev->xcrossing.window,0)))
224 wm_handle_key(win, key, MOD(), PTR());
226 else if (type == FocusIn || type == FocusOut) {
227 printf("focus: %d\n", type);
228 key_t key = FocusIn ? key_focus : key_unfocus;
229 if ((win = win_find(dpy,ev->xfocus.window,0)))
230 wm_handle_key(win, key, MOD(), PTR());
232 else if (type == ConfigureNotify) {
233 printf("configure: %d\n", type);
235 else if (type == MapNotify) {
236 printf("map: %d\n", type);
238 else if (type == UnmapNotify) {
239 //printf("unmap: %d\n", type);
240 if ((win = win_find(dpy,ev->xmap.window,0))) {
245 else if (type == ConfigureRequest) {
246 XConfigureRequestEvent *cre = &ev->xconfigurerequest;
247 XWindowChanges wc = {
248 .x = cre->x, .y = cre->y,
249 .width = cre->width, .height = cre->height,
251 printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
252 type, (int)cre->window, cre->value_mask,
253 cre->height, cre->width, cre->x, cre->y);
254 XConfigureWindow(dpy, cre->window, cre->value_mask, &wc);
256 else if (type == MapRequest) {
257 printf("map_req: %d\n", type);
258 if ((win = win_find(dpy,ev->xmaprequest.window,1)))
260 XMapWindow(dpy,ev->xmaprequest.window);
263 printf("unknown event: %d\n", type);
267 static int xerror(Display *dpy, XErrorEvent *err)
269 if (err->error_code == BadWindow ||
270 (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
271 (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
272 (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
273 (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
274 (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
275 (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
276 (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
277 (err->request_code == X_CopyArea && err->error_code == BadDrawable))
279 return xerrorxlib(dpy, err);
285 void sys_move(win_t *win, int x, int y, int w, int h)
287 //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
288 win->x = MAX(x,0); win->y = MAX(y,0);
289 win->w = MAX(w,1); win->h = MAX(h,1);
290 XMoveResizeWindow(win->sys->dpy, win->sys->xid,
291 win->x, win->y, win->w, win->h);
293 /* Flush events, so moving window doesn't cuase re-focus
294 * There's probably a better way to do this */
296 XSync(win->sys->dpy, False);
297 while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
298 printf("Skipping enter/leave event\n");
301 void sys_raise(win_t *win)
303 //printf("sys_raise: %p\n", win);
304 XRaiseWindow(win->sys->dpy, win->sys->xid);
307 void sys_focus(win_t *win)
309 printf("sys_focus: %p\n", win);
311 .type = ClientMessage,
312 .xclient.window = win->sys->xid,
313 .xclient.message_type = atoms[wm_proto],
314 .xclient.format = 32,
315 .xclient.data.l[0] = atoms[wm_focus],
316 .xclient.data.l[1] = CurrentTime,
318 XSetInputFocus(win->sys->dpy, win->sys->xid,
319 RevertToPointerRoot, CurrentTime);
320 //XSetInputFocus(win->sys->dpy, PointerRoot,
321 // RevertToPointerRoot, CurrentTime);
322 XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &ev);
325 void sys_watch(win_t *win, Key_t key, mod_t mod)
327 //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
328 XWindowAttributes attr;
329 XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
330 long mask = attr.your_event_mask;
331 if (key_mouse0 <= key && key <= key_mouse7)
332 XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, True,
333 mod.up ? ButtonReleaseMask : ButtonPressMask,
334 GrabModeAsync, GrabModeAsync, None, None);
335 else if (key == key_enter)
336 XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
337 else if (key == key_leave)
338 XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
339 else if (key == key_focus || key == key_unfocus)
340 XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
342 XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
343 mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
346 win_t *sys_init(void)
350 if (!(dpy = XOpenDisplay(NULL)))
351 error("Unable to get display");
352 if (!(xid = DefaultRootWindow(dpy)))
353 error("Unable to get root window");
354 atoms[wm_proto] = XInternAtom(dpy, "WM_PROTOCOLS", False);
355 atoms[wm_focus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
356 XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
357 XSetInputFocus(dpy, None, RevertToNone, CurrentTime);
358 xerrorxlib = XSetErrorHandler(xerror);
359 return win_find(dpy, xid, 1);
362 void sys_run(win_t *root)
364 /* Add each initial window */
366 Window par, xid, *kids = NULL;
367 if (XQueryTree(root->sys->dpy, root->sys->xid,
368 &par, &xid, &kids, &nkids))
369 for(int i = 0; i < nkids; i++) {
370 win_t *win = win_find(root->sys->dpy, kids[i], 1);
371 if (win && win_viewable(win))
379 XNextEvent(root->sys->dpy, &ev);
380 process_event(ev.type, &ev, root);