6 #include <X11/Xproto.h>
8 #include <X11/keysym.h>
14 /* Internal structures */
19 int left, right, top, bottom;
29 wm_proto, wm_focus, net_strut, natoms
33 static void *win_cache = NULL;
34 static Atom atoms[natoms];
35 static int (*xerrorxlib)(Display *, XErrorEvent *);
37 /* Conversion functions */
38 static keymap_t key2sym[] = {
39 {key_left , XK_Left },
40 {key_right , XK_Right},
42 {key_down , XK_Down },
43 {key_home , XK_Home },
45 {key_pageup , XK_Prior},
46 {key_pagedown, XK_Next },
62 static mod_t x2mod(unsigned int state, int up)
65 .alt = !!(state & Mod1Mask ),
66 .ctrl = !!(state & ControlMask),
67 .shift = !!(state & ShiftMask ),
68 .win = !!(state & Mod4Mask ),
73 static unsigned int mod2x(mod_t mod)
75 return (mod.alt ? Mod1Mask : 0)
76 | (mod.ctrl ? ControlMask : 0)
77 | (mod.shift ? ShiftMask : 0)
78 | (mod.win ? Mod4Mask : 0);
82 static Key_t x2key(KeySym sym)
84 keymap_t *km = map_getr(key2sym,sym);
85 return km ? km->key : sym;
88 static KeySym key2x(Key_t key)
90 keymap_t *km = map_get(key2sym,key);
91 return km ? km->sym : key;
94 static Key_t x2btn(int btn)
96 return btn + key_mouse0;
99 static int btn2x(Key_t key)
101 return key - key_mouse0;
105 static ptr_t x2ptr(XEvent *_ev)
107 XKeyEvent *ev = &_ev->xkey;
108 return (ptr_t){ev->x, ev->y, ev->x_root, ev->y_root};
111 static Window getfocus(win_t *root, XEvent *event)
114 Window focus = PointerRoot;
115 if (event->type == KeyPress || event->type == KeyRelease)
116 XGetInputFocus(root->sys->dpy, &focus, &revert);
117 if (focus == PointerRoot)
118 focus = event->xkey.subwindow;
120 focus = event->xkey.window;
125 static int add_strut(win_t *root, win_t *win)
127 /* Get X11 strut data */
130 unsigned long ret_items, bytes_left;
131 unsigned char *xdata;
132 int status = XGetWindowProperty(win->sys->dpy, win->sys->xid,
133 atoms[net_strut], 0L, 4L, False, XA_CARDINAL,
134 &ret_type, &ret_size, &ret_items, &bytes_left, &xdata);
135 if (status != Success || ret_size != 32 || ret_items != 4)
138 int left = ((int*)xdata)[0];
139 int right = ((int*)xdata)[1];
140 int top = ((int*)xdata)[2];
141 int bottom = ((int*)xdata)[3];
142 if (left == 0 && right == 0 && top == 0 && bottom == 0)
145 win->sys->strut.left = left;
146 win->sys->strut.right = right;
147 win->sys->strut.top = top;
148 win->sys->strut.bottom = bottom;
151 root->w -= left+right;
152 root->h -= top+bottom;
156 static int del_strut(win_t *root, win_t *win)
158 int left = win->sys->strut.left;
159 int right = win->sys->strut.right;
160 int top = win->sys->strut.top;
161 int bottom = win->sys->strut.bottom;
162 if (left == 0 && right == 0 && top == 0 && bottom == 0)
167 root->w += left+right;
168 root->h += top+bottom;
172 /* Window functions */
173 static win_t *win_new(Display *dpy, Window xid)
175 XWindowAttributes attr;
176 if (XGetWindowAttributes(dpy, xid, &attr))
177 if (attr.override_redirect)
179 win_t *win = new0(win_t);
183 win->h = attr.height;
184 win->sys = new0(win_sys_t);
187 printf("%p = %p, %d\n", win, dpy, (int)xid);
191 static int win_cmp(const void *_a, const void *_b)
193 const win_t *a = _a, *b = _b;
194 if (a->sys->dpy < b->sys->dpy) return -1;
195 if (a->sys->dpy > b->sys->dpy) return 1;
196 if (a->sys->xid < b->sys->xid) return -1;
197 if (a->sys->xid > b->sys->xid) return 1;
201 static win_t *win_find(Display *dpy, Window xid, int create)
205 //printf("win_find: %p, %d\n", dpy, (int)xid);
206 win_sys_t sys = {.dpy=dpy, .xid=xid};
207 win_t tmp = {.sys=&sys};
208 win_t **old = NULL, *new = NULL;
209 if ((old = tfind(&tmp, &win_cache, win_cmp)))
211 if (create && (new = win_new(dpy,xid)))
212 tsearch(new, &win_cache, win_cmp);
216 static void win_remove(win_t *win)
218 tdelete(win, &win_cache, win_cmp);
223 static int win_viewable(win_t *win)
225 XWindowAttributes attr;
226 if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
227 return attr.map_state == IsViewable;
233 static void process_event(int type, XEvent *ev, win_t *root)
235 Display *dpy = root->sys->dpy;
237 printf("event: %d\n", type);
239 /* Common data for all these events ... */
240 ptr_t ptr; mod_t mod;
241 if (type == KeyPress || type == KeyRelease ||
242 type == ButtonPress || type == ButtonRelease ||
243 type == MotionNotify) {
244 Window xid = getfocus(root, ev);
245 if (!(win = win_find(dpy,xid,0)))
248 mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
251 /* Split based on event */
252 if (type == KeyPress) {
253 while (XCheckTypedEvent(dpy, KeyPress, ev));
254 KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
255 wm_handle_key(win, x2key(sym), mod, ptr);
257 else if (type == KeyRelease) {
258 //printf("release: %d\n", type);
260 else if (type == ButtonPress) {
261 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
262 XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
263 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
265 else if (type == ButtonRelease) {
266 XUngrabPointer(dpy, CurrentTime);
267 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
269 else if (type == MotionNotify) {
270 while (XCheckTypedEvent(dpy, MotionNotify, ev));
271 wm_handle_ptr(win, ptr);
273 else if (type == EnterNotify || type == LeaveNotify) {
274 printf("enter: %d\n", type);
275 key_t key = EnterNotify ? key_enter : key_leave;
276 if ((win = win_find(dpy,ev->xcrossing.window,0)))
277 wm_handle_key(win, key, MOD(), PTR());
279 else if (type == FocusIn || type == FocusOut) {
280 printf("focus: %d\n", type);
281 key_t key = FocusIn ? key_focus : key_unfocus;
282 if ((win = win_find(dpy,ev->xfocus.window,0)))
283 wm_handle_key(win, key, MOD(), PTR());
285 else if (type == ConfigureNotify) {
286 printf("configure: %d\n", type);
288 else if (type == MapNotify) {
289 printf("map: %d\n", type);
291 else if (type == UnmapNotify) {
292 //printf("unmap: %d\n", type);
293 if ((win = win_find(dpy,ev->xunmap.window,0))) {
294 if (!del_strut(root, win))
301 else if (type == ConfigureRequest) {
302 XConfigureRequestEvent *cre = &ev->xconfigurerequest;
303 XWindowChanges wc = {
304 .x = cre->x, .y = cre->y,
305 .width = cre->width, .height = cre->height,
307 printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
308 type, (int)cre->window, cre->value_mask,
309 cre->height, cre->width, cre->x, cre->y);
310 XConfigureWindow(dpy, cre->window, cre->value_mask, &wc);
312 else if (type == MapRequest) {
313 printf("map_req: %d\n", type);
314 if ((win = win_find(dpy,ev->xmaprequest.window,1))) {
315 if (!add_strut(root, win))
320 XMapWindow(dpy,ev->xmaprequest.window);
323 printf("unknown event: %d\n", type);
327 static int xerror(Display *dpy, XErrorEvent *err)
329 if (err->error_code == BadWindow ||
330 (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
331 (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
332 (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
333 (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
334 (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
335 (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
336 (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
337 (err->request_code == X_CopyArea && err->error_code == BadDrawable))
339 return xerrorxlib(dpy, err);
345 void sys_move(win_t *win, int x, int y, int w, int h)
347 //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
348 win->x = MAX(x,0); win->y = MAX(y,0);
349 win->w = MAX(w,1); win->h = MAX(h,1);
350 XMoveResizeWindow(win->sys->dpy, win->sys->xid,
351 win->x, win->y, win->w, win->h);
353 /* Flush events, so moving window doesn't cuase re-focus
354 * There's probably a better way to do this */
356 XSync(win->sys->dpy, False);
357 while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
358 printf("Skipping enter/leave event\n");
361 void sys_raise(win_t *win)
363 //printf("sys_raise: %p\n", win);
364 XRaiseWindow(win->sys->dpy, win->sys->xid);
367 void sys_focus(win_t *win)
369 printf("sys_focus: %p\n", win);
371 .type = ClientMessage,
372 .xclient.window = win->sys->xid,
373 .xclient.message_type = atoms[wm_proto],
374 .xclient.format = 32,
375 .xclient.data.l[0] = atoms[wm_focus],
376 .xclient.data.l[1] = CurrentTime,
378 XSetInputFocus(win->sys->dpy, win->sys->xid,
379 RevertToPointerRoot, CurrentTime);
380 //XSetInputFocus(win->sys->dpy, PointerRoot,
381 // RevertToPointerRoot, CurrentTime);
382 XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &ev);
385 void sys_watch(win_t *win, Key_t key, mod_t mod)
387 //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
388 XWindowAttributes attr;
389 XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
390 long mask = attr.your_event_mask;
391 if (key_mouse0 <= key && key <= key_mouse7)
392 XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, True,
393 mod.up ? ButtonReleaseMask : ButtonPressMask,
394 GrabModeAsync, GrabModeAsync, None, None);
395 else if (key == key_enter)
396 XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
397 else if (key == key_leave)
398 XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
399 else if (key == key_focus || key == key_unfocus)
400 XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
402 XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
403 mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
406 win_t *sys_init(void)
410 if (!(dpy = XOpenDisplay(NULL)))
411 error("Unable to get display");
412 if (!(xid = DefaultRootWindow(dpy)))
413 error("Unable to get root window");
414 atoms[wm_proto] = XInternAtom(dpy, "WM_PROTOCOLS", False);
415 atoms[wm_focus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
416 atoms[net_strut] = XInternAtom(dpy, "_NET_WM_STRUT", False);
417 XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
418 XSetInputFocus(dpy, None, RevertToNone, CurrentTime);
419 xerrorxlib = XSetErrorHandler(xerror);
420 return win_find(dpy, xid, 1);
423 void sys_run(win_t *root)
425 /* Add each initial window */
427 Window par, xid, *kids = NULL;
428 if (XQueryTree(root->sys->dpy, root->sys->xid,
429 &par, &xid, &kids, &nkids))
430 for(int i = 0; i < nkids; i++) {
431 win_t *win = win_find(root->sys->dpy, kids[i], 1);
432 if (win && win_viewable(win) && !add_strut(root,win))
435 wm_update(); // For struts
441 XNextEvent(root->sys->dpy, &ev);
442 process_event(ev.type, &ev, root);