6 #include <X11/Xproto.h>
8 #include <X11/keysym.h>
16 /* Internal structures */
21 int left, right, top, bottom;
31 wm_proto, wm_focus, net_strut, natoms
35 clr_focus, clr_unfocus, clr_urgent, ncolors
39 static void *win_cache;
40 static Atom atoms[natoms];
41 static int (*xerrorxlib)(Display *, XErrorEvent *);
42 static unsigned long colors[ncolors];
44 /* Conversion functions */
45 static keymap_t key2sym[] = {
46 {key_left , XK_Left },
47 {key_right , XK_Right},
49 {key_down , XK_Down },
50 {key_home , XK_Home },
52 {key_pageup , XK_Prior},
53 {key_pagedown, XK_Next },
69 static mod_t x2mod(unsigned int state, int up)
72 .alt = !!(state & Mod1Mask ),
73 .ctrl = !!(state & ControlMask),
74 .shift = !!(state & ShiftMask ),
75 .win = !!(state & Mod4Mask ),
80 static unsigned int mod2x(mod_t mod)
82 return (mod.alt ? Mod1Mask : 0)
83 | (mod.ctrl ? ControlMask : 0)
84 | (mod.shift ? ShiftMask : 0)
85 | (mod.win ? Mod4Mask : 0);
89 static Key_t x2key(KeySym sym)
91 keymap_t *km = map_getr(key2sym,sym);
92 return km ? km->key : sym;
95 static KeySym key2x(Key_t key)
97 keymap_t *km = map_get(key2sym,key);
98 return km ? km->sym : key;
101 static Key_t x2btn(int btn)
103 return btn + key_mouse0;
106 static int btn2x(Key_t key)
108 return key - key_mouse0;
112 static ptr_t x2ptr(XEvent *_ev)
114 XKeyEvent *ev = &_ev->xkey;
115 return (ptr_t){ev->x, ev->y, ev->x_root, ev->y_root};
118 static Window getfocus(win_t *root, XEvent *event)
121 Window focus = PointerRoot;
122 if (event->type == KeyPress || event->type == KeyRelease)
123 XGetInputFocus(root->sys->dpy, &focus, &revert);
124 if (focus == PointerRoot)
125 focus = event->xkey.subwindow;
127 focus = event->xkey.window;
132 static int add_strut(win_t *root, win_t *win)
134 /* Get X11 strut data */
137 unsigned long ret_items, bytes_left;
138 unsigned char *xdata;
139 int status = XGetWindowProperty(win->sys->dpy, win->sys->xid,
140 atoms[net_strut], 0L, 4L, False, XA_CARDINAL,
141 &ret_type, &ret_size, &ret_items, &bytes_left, &xdata);
142 if (status != Success || ret_size != 32 || ret_items != 4)
145 int left = ((int*)xdata)[0];
146 int right = ((int*)xdata)[1];
147 int top = ((int*)xdata)[2];
148 int bottom = ((int*)xdata)[3];
149 if (left == 0 && right == 0 && top == 0 && bottom == 0)
152 win->sys->strut.left = left;
153 win->sys->strut.right = right;
154 win->sys->strut.top = top;
155 win->sys->strut.bottom = bottom;
158 root->w -= left+right;
159 root->h -= top+bottom;
163 static int del_strut(win_t *root, win_t *win)
165 int left = win->sys->strut.left;
166 int right = win->sys->strut.right;
167 int top = win->sys->strut.top;
168 int bottom = win->sys->strut.bottom;
169 if (left == 0 && right == 0 && top == 0 && bottom == 0)
174 root->w += left+right;
175 root->h += top+bottom;
179 /* Window functions */
180 static win_t *win_new(Display *dpy, Window xid)
182 XWindowAttributes attr;
183 if (XGetWindowAttributes(dpy, xid, &attr))
184 if (attr.override_redirect)
186 win_t *win = new0(win_t);
190 win->h = attr.height;
191 win->sys = new0(win_sys_t);
194 printf("win_new: %p = %p, %d (%d,%d %dx%d)\n",
196 win->x, win->y, win->w, win->h);
200 static int win_cmp(const void *_a, const void *_b)
202 const win_t *a = _a, *b = _b;
203 if (a->sys->dpy < b->sys->dpy) return -1;
204 if (a->sys->dpy > b->sys->dpy) return 1;
205 if (a->sys->xid < b->sys->xid) return -1;
206 if (a->sys->xid > b->sys->xid) return 1;
210 static win_t *win_find(Display *dpy, Window xid, int create)
214 //printf("win_find: %p, %d\n", dpy, (int)xid);
215 win_sys_t sys = {.dpy=dpy, .xid=xid};
216 win_t tmp = {.sys=&sys};
217 win_t **old = NULL, *new = NULL;
218 if ((old = tfind(&tmp, &win_cache, win_cmp)))
220 if (create && (new = win_new(dpy,xid)))
221 tsearch(new, &win_cache, win_cmp);
225 static void win_remove(win_t *win)
227 tdelete(win, &win_cache, win_cmp);
232 static int win_viewable(win_t *win)
234 XWindowAttributes attr;
235 if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
236 return attr.map_state == IsViewable;
241 /* Drawing functions */
242 unsigned long get_color(Display *dpy, const char *name)
245 int screen = DefaultScreen(dpy);
246 Colormap cmap = DefaultColormap(dpy, screen);
247 XAllocNamedColor(dpy, cmap, name, &color, &color);
252 static void process_event(int type, XEvent *ev, win_t *root)
254 Display *dpy = root->sys->dpy;
256 printf("event: %d\n", type);
258 /* Common data for all these events ... */
259 ptr_t ptr; mod_t mod;
260 if (type == KeyPress || type == KeyRelease ||
261 type == ButtonPress || type == ButtonRelease ||
262 type == MotionNotify) {
263 Window xid = getfocus(root, ev);
264 if (!(win = win_find(dpy,xid,0)))
267 mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
270 /* Split based on event */
271 if (type == KeyPress) {
272 while (XCheckTypedEvent(dpy, KeyPress, ev));
273 KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
274 wm_handle_key(win, x2key(sym), mod, ptr);
276 else if (type == KeyRelease) {
277 //printf("release: %d\n", type);
279 else if (type == ButtonPress) {
280 if (wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr))
281 XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
282 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
284 printf("resending event\n");
285 XSendEvent(win->sys->dpy, ev->xbutton.window, True, NoEventMask, ev);
286 XSendEvent(win->sys->dpy, ev->xbutton.window, False, NoEventMask, ev);
287 XSendEvent(win->sys->dpy, ev->xbutton.root, True, NoEventMask, ev);
288 XSendEvent(win->sys->dpy, ev->xbutton.root, False, NoEventMask, ev);
289 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, True, NoEventMask, ev);
290 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, False, NoEventMask, ev);
293 else if (type == ButtonRelease) {
294 XUngrabPointer(dpy, CurrentTime);
295 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
297 else if (type == MotionNotify) {
298 while (XCheckTypedEvent(dpy, MotionNotify, ev));
299 wm_handle_ptr(win, ptr);
301 else if (type == EnterNotify || type == LeaveNotify) {
302 printf("enter: %d\n", type);
303 key_t key = EnterNotify ? key_enter : key_leave;
304 if ((win = win_find(dpy,ev->xcrossing.window,0)))
305 wm_handle_key(win, key, MOD(), PTR());
307 else if (type == FocusIn || type == FocusOut) {
308 printf("focus: %d\n", type);
309 key_t key = FocusIn ? key_focus : key_unfocus;
310 if ((win = win_find(dpy,ev->xfocus.window,0)))
311 wm_handle_key(win, key, MOD(), PTR());
313 else if (type == ConfigureNotify) {
314 printf("configure: %d\n", type);
316 else if (type == MapNotify) {
317 printf("map: %d\n", type);
319 else if (type == UnmapNotify) {
320 //printf("unmap: %d\n", type);
321 if ((win = win_find(dpy,ev->xunmap.window,0))) {
322 if (!del_strut(root, win))
329 else if (type == ConfigureRequest) {
330 XConfigureRequestEvent *cre = &ev->xconfigurerequest;
331 printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
332 type, (int)cre->window, cre->value_mask,
333 cre->height, cre->width, cre->x, cre->y);
334 XConfigureWindow(dpy, cre->window, cre->value_mask, &(XWindowChanges){
338 .height = cre->height,
341 /* This seems necessasairy for, but causes flicker
342 * there could be a better way to do this */
343 if ((win = win_find(dpy,ev->xmaprequest.window,0)))
344 sys_move(win, win->x, win->y, win->w, win->h);
346 else if (type == MapRequest) {
347 printf("map_req: %d\n", type);
348 if ((win = win_find(dpy,ev->xmaprequest.window,1))) {
349 if (!add_strut(root, win))
354 XMapWindow(dpy,ev->xmaprequest.window);
357 printf("unknown event: %d\n", type);
361 static int xerror(Display *dpy, XErrorEvent *err)
363 if (err->error_code == BadWindow ||
364 (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
365 (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
366 (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
367 (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
368 (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
369 (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
370 (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
371 (err->request_code == X_CopyArea && err->error_code == BadDrawable))
373 return xerrorxlib(dpy, err);
379 void sys_move(win_t *win, int x, int y, int w, int h)
381 //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
383 win->x = MAX(x,0); win->y = MAX(y,0);
384 win->w = MAX(w,1+b); win->h = MAX(h,1+b);
385 w = MAX(w-b,1); h = MAX(h-b,1);
386 XMoveResizeWindow(win->sys->dpy, win->sys->xid, x, y, w, h);
388 /* Flush events, so moving window doesn't cuase re-focus
389 * There's probably a better way to do this */
391 XSync(win->sys->dpy, False);
392 while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
393 printf("Skipping enter/leave event\n");
396 void sys_raise(win_t *win)
398 //printf("sys_raise: %p\n", win);
399 XRaiseWindow(win->sys->dpy, win->sys->xid);
402 void sys_focus(win_t *win)
404 printf("sys_focus: %p\n", win);
406 /* Set border on focused window */
407 static win_t *last = NULL;
409 XSetWindowBorder(last->sys->dpy, last->sys->xid, colors[clr_unfocus]);
410 XSetWindowBorder(win->sys->dpy, win->sys->xid, colors[clr_focus]);
411 XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, BORDER);
414 /* Set actual focus */
415 XSetInputFocus(win->sys->dpy, win->sys->xid,
416 RevertToPointerRoot, CurrentTime);
417 XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &(XEvent){
418 .type = ClientMessage,
419 .xclient.window = win->sys->xid,
420 .xclient.message_type = atoms[wm_proto],
421 .xclient.format = 32,
422 .xclient.data.l[0] = atoms[wm_focus],
423 .xclient.data.l[1] = CurrentTime,
427 void sys_watch(win_t *win, Key_t key, mod_t mod)
429 //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
430 XWindowAttributes attr;
431 XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
432 long mask = attr.your_event_mask;
433 if (key_mouse0 <= key && key <= key_mouse7)
434 XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, False,
435 mod.up ? ButtonReleaseMask : ButtonPressMask,
436 GrabModeAsync, GrabModeAsync, None, None);
437 else if (key == key_enter)
438 XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
439 else if (key == key_leave)
440 XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
441 else if (key == key_focus || key == key_unfocus)
442 XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
444 XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
445 mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
448 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
450 if (key_mouse0 <= key && key <= key_mouse7)
451 XUngrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid);
454 win_t *sys_init(void)
458 if (!(dpy = XOpenDisplay(NULL)))
459 error("Unable to get display");
460 if (!(xid = DefaultRootWindow(dpy)))
461 error("Unable to get root window");
462 atoms[wm_proto] = XInternAtom(dpy, "WM_PROTOCOLS", False);
463 atoms[wm_focus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
464 atoms[net_strut] = XInternAtom(dpy, "_NET_WM_STRUT", False);
465 colors[clr_focus] = get_color(dpy, "#a0a0ff");
466 colors[clr_unfocus] = get_color(dpy, "#101066");
467 colors[clr_urgent] = get_color(dpy, "#ff0000");
468 printf("colors = #%06lx #%06lx #%06lx\n", colors[0], colors[1], colors[2]);
469 XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
470 XSetInputFocus(dpy, None, RevertToNone, CurrentTime);
471 xerrorxlib = XSetErrorHandler(xerror);
472 return win_find(dpy, xid, 1);
475 void sys_run(win_t *root)
477 /* Add each initial window */
479 Window par, xid, *kids = NULL;
480 if (XQueryTree(root->sys->dpy, root->sys->xid,
481 &par, &xid, &kids, &nkids))
482 for(int i = 0; i < nkids; i++) {
483 win_t *win = win_find(root->sys->dpy, kids[i], 1);
484 if (win && win_viewable(win) && !add_strut(root,win))
487 wm_update(); // For struts
493 XNextEvent(root->sys->dpy, &ev);
494 process_event(ev.type, &ev, root);