6 #include <X11/Xproto.h>
8 #include <X11/keysym.h>
9 #include <X11/extensions/Xinerama.h>
17 /* Internal structures */
22 int left, right, top, bottom;
33 wm_proto, wm_focus, net_strut, natoms
37 clr_focus, clr_unfocus, clr_urgent, ncolors
42 static Atom atoms[natoms];
43 static int (*xerrorxlib)(Display *, XErrorEvent *);
44 static unsigned long colors[ncolors];
46 /* Conversion functions */
47 static keymap_t key2sym[] = {
48 {key_left , XK_Left },
49 {key_right , XK_Right},
51 {key_down , XK_Down },
52 {key_home , XK_Home },
54 {key_pageup , XK_Prior},
55 {key_pagedown, XK_Next },
71 static mod_t x2mod(unsigned int state, int up)
74 .alt = !!(state & Mod1Mask ),
75 .ctrl = !!(state & ControlMask),
76 .shift = !!(state & ShiftMask ),
77 .win = !!(state & Mod4Mask ),
82 static unsigned int mod2x(mod_t mod)
84 return (mod.alt ? Mod1Mask : 0)
85 | (mod.ctrl ? ControlMask : 0)
86 | (mod.shift ? ShiftMask : 0)
87 | (mod.win ? Mod4Mask : 0);
91 static Key_t x2key(KeySym sym)
93 keymap_t *km = map_getr(key2sym,sym);
94 return km ? km->key : sym;
97 static KeySym key2x(Key_t key)
99 keymap_t *km = map_get(key2sym,key);
100 return km ? km->sym : key;
103 static Key_t x2btn(int btn)
105 return btn + key_mouse0;
108 static int btn2x(Key_t key)
110 return key - key_mouse0;
114 static ptr_t x2ptr(XEvent *_ev)
116 XKeyEvent *ev = &_ev->xkey;
117 return (ptr_t){ev->x, ev->y, ev->x_root, ev->y_root};
120 static Window getfocus(win_t *root, XEvent *event)
123 Window focus = PointerRoot;
124 if (event->type == KeyPress || event->type == KeyRelease)
125 XGetInputFocus(root->sys->dpy, &focus, &revert);
126 if (focus == PointerRoot)
127 focus = event->xkey.subwindow;
129 focus = event->xkey.window;
134 static int add_strut(win_t *root, win_t *win)
136 /* Get X11 strut data */
139 unsigned long ret_items, bytes_left;
140 unsigned char *xdata;
141 int status = XGetWindowProperty(win->sys->dpy, win->sys->xid,
142 atoms[net_strut], 0L, 4L, False, XA_CARDINAL,
143 &ret_type, &ret_size, &ret_items, &bytes_left, &xdata);
144 if (status != Success || ret_size != 32 || ret_items != 4)
147 int left = ((int*)xdata)[0];
148 int right = ((int*)xdata)[1];
149 int top = ((int*)xdata)[2];
150 int bottom = ((int*)xdata)[3];
151 if (left == 0 && right == 0 && top == 0 && bottom == 0)
154 win->sys->strut.left = left;
155 win->sys->strut.right = right;
156 win->sys->strut.top = top;
157 win->sys->strut.bottom = bottom;
160 root->w -= left+right;
161 root->h -= top+bottom;
165 static int del_strut(win_t *root, win_t *win)
167 int left = win->sys->strut.left;
168 int right = win->sys->strut.right;
169 int top = win->sys->strut.top;
170 int bottom = win->sys->strut.bottom;
171 if (left == 0 && right == 0 && top == 0 && bottom == 0)
176 root->w += left+right;
177 root->h += top+bottom;
181 /* Window functions */
182 static win_t *win_new(Display *dpy, Window xid)
184 XWindowAttributes attr;
185 if (XGetWindowAttributes(dpy, xid, &attr))
186 if (attr.override_redirect)
188 win_t *win = new0(win_t);
192 win->h = attr.height;
193 win->sys = new0(win_sys_t);
196 printf("win_new: %p = %p, %d (%d,%d %dx%d)\n",
198 win->x, win->y, win->w, win->h);
202 static int win_cmp(const void *_a, const void *_b)
204 const win_t *a = _a, *b = _b;
205 if (a->sys->dpy < b->sys->dpy) return -1;
206 if (a->sys->dpy > b->sys->dpy) return 1;
207 if (a->sys->xid < b->sys->xid) return -1;
208 if (a->sys->xid > b->sys->xid) return 1;
212 static win_t *win_find(Display *dpy, Window xid, int create)
216 //printf("win_find: %p, %d\n", dpy, (int)xid);
217 win_sys_t sys = {.dpy=dpy, .xid=xid};
218 win_t tmp = {.sys=&sys};
219 win_t **old = NULL, *new = NULL;
220 if ((old = tfind(&tmp, &cache, win_cmp)))
222 if (create && (new = win_new(dpy,xid)))
223 tsearch(new, &cache, win_cmp);
227 static void win_remove(win_t *win)
229 tdelete(win, &cache, win_cmp);
234 static int win_viewable(win_t *win)
236 XWindowAttributes attr;
237 if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
238 return attr.map_state == IsViewable;
243 /* Drawing functions */
244 unsigned long get_color(Display *dpy, const char *name)
247 int screen = DefaultScreen(dpy);
248 Colormap cmap = DefaultColormap(dpy, screen);
249 XAllocNamedColor(dpy, cmap, name, &color, &color);
254 static void process_event(int type, XEvent *ev, win_t *root)
256 Display *dpy = root->sys->dpy;
258 //printf("event: %d\n", type);
260 /* Common data for all these events ... */
261 ptr_t ptr; mod_t mod;
262 if (type == KeyPress || type == KeyRelease ||
263 type == ButtonPress || type == ButtonRelease ||
264 type == MotionNotify) {
265 Window xid = getfocus(root, ev);
266 if (!(win = win_find(dpy,xid,0)))
269 mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
272 /* Split based on event */
273 if (type == KeyPress) {
274 while (XCheckTypedEvent(dpy, KeyPress, ev));
275 KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
276 printf("got key %c %hhx\n", x2key(sym), mod2int(mod));
277 wm_handle_key(win, x2key(sym), mod, ptr);
279 else if (type == KeyRelease) {
280 //printf("release: %d\n", type);
282 else if (type == ButtonPress) {
283 if (wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr))
284 XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
285 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
287 printf("resending event\n");
288 XSendEvent(win->sys->dpy, ev->xbutton.window, True, NoEventMask, ev);
289 XSendEvent(win->sys->dpy, ev->xbutton.window, False, NoEventMask, ev);
290 XSendEvent(win->sys->dpy, ev->xbutton.root, True, NoEventMask, ev);
291 XSendEvent(win->sys->dpy, ev->xbutton.root, False, NoEventMask, ev);
292 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, True, NoEventMask, ev);
293 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, False, NoEventMask, ev);
296 else if (type == ButtonRelease) {
297 XUngrabPointer(dpy, CurrentTime);
298 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
300 else if (type == MotionNotify) {
301 while (XCheckTypedEvent(dpy, MotionNotify, ev));
302 wm_handle_ptr(win, ptr);
304 else if (type == EnterNotify || type == LeaveNotify) {
305 printf("enter: %d\n", type);
306 key_t key = EnterNotify ? key_enter : key_leave;
307 if ((win = win_find(dpy,ev->xcrossing.window,0)))
308 wm_handle_key(win, key, MOD(), PTR());
310 else if (type == FocusIn || type == FocusOut) {
311 //printf("focus: %d\n", type);
312 key_t key = FocusIn ? key_focus : key_unfocus;
313 if ((win = win_find(dpy,ev->xfocus.window,0)))
314 wm_handle_key(win, key, MOD(), PTR());
316 else if (type == ConfigureNotify) {
317 printf("configure: %d\n", type);
319 else if (type == MapNotify) {
320 printf("map: %d\n", type);
322 else if (type == UnmapNotify) {
323 if ((win = win_find(dpy,ev->xunmap.window,0)) &&
324 win->sys->state == st_show) {
325 if (!del_strut(root, win))
329 win->sys->state = st_hide;
332 else if (type == DestroyNotify) {
333 //printf("destroy: %d\n", type);
334 if ((win = win_find(dpy,ev->xdestroywindow.window,0)))
337 else if (type == ConfigureRequest) {
338 XConfigureRequestEvent *cre = &ev->xconfigurerequest;
339 printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
340 type, (int)cre->window, cre->value_mask,
341 cre->height, cre->width, cre->x, cre->y);
342 XConfigureWindow(dpy, cre->window, cre->value_mask, &(XWindowChanges){
346 .height = cre->height,
349 /* This seems necessasairy for, but causes flicker
350 * there could be a better way to do this */
351 if ((win = win_find(dpy,ev->xmaprequest.window,0)))
352 sys_move(win, win->x, win->y, win->w, win->h);
354 else if (type == MapRequest) {
355 printf("map_req: %d\n", type);
356 if ((win = win_find(dpy,ev->xmaprequest.window,1))) {
357 if (!add_strut(root, win))
362 XMapWindow(dpy,ev->xmaprequest.window);
365 printf("unknown event: %d\n", type);
369 static int xerror(Display *dpy, XErrorEvent *err)
371 if (err->error_code == BadWindow ||
372 (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
373 (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
374 (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
375 (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
376 (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
377 (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
378 (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
379 (err->request_code == X_CopyArea && err->error_code == BadDrawable))
381 return xerrorxlib(dpy, err);
387 void sys_move(win_t *win, int x, int y, int w, int h)
389 //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
391 win->x = x; win->y = y;
392 win->w = MAX(w,1+b); win->h = MAX(h,1+b);
393 w = MAX(w-b,1); h = MAX(h-b,1);
394 XMoveResizeWindow(win->sys->dpy, win->sys->xid, x, y, w, h);
396 /* Flush events, so moving window doesn't cuase re-focus
397 * There's probably a better way to do this */
399 XSync(win->sys->dpy, False);
400 while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
401 printf("Skipping enter/leave event\n");
404 void sys_raise(win_t *win)
406 //printf("sys_raise: %p\n", win);
407 XRaiseWindow(win->sys->dpy, win->sys->xid);
410 void sys_focus(win_t *win)
412 //printf("sys_focus: %p\n", win);
414 /* Set border on focused window */
415 static win_t *last = NULL;
417 XSetWindowBorder(last->sys->dpy, last->sys->xid, colors[clr_unfocus]);
418 XSetWindowBorder(win->sys->dpy, win->sys->xid, colors[clr_focus]);
419 XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, BORDER);
422 /* Set actual focus */
423 XSetInputFocus(win->sys->dpy, win->sys->xid,
424 RevertToPointerRoot, CurrentTime);
425 XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &(XEvent){
426 .type = ClientMessage,
427 .xclient.window = win->sys->xid,
428 .xclient.message_type = atoms[wm_proto],
429 .xclient.format = 32,
430 .xclient.data.l[0] = atoms[wm_focus],
431 .xclient.data.l[1] = CurrentTime,
435 void sys_show(win_t *win, state_t state)
437 win->sys->state = state;
440 printf("sys_show: show\n");
441 XMapWindow(win->sys->dpy, win->sys->xid);
444 printf("sys_show: full\n");
447 printf("sys_show: shade\n");
450 printf("sys_show: icon\n");
453 printf("sys_show: hide\n");
454 XUnmapWindow(win->sys->dpy, win->sys->xid);
459 void sys_watch(win_t *win, Key_t key, mod_t mod)
461 //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
462 XWindowAttributes attr;
463 XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
464 long mask = attr.your_event_mask;
465 if (key_mouse0 <= key && key <= key_mouse7)
466 XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, False,
467 mod.up ? ButtonReleaseMask : ButtonPressMask,
468 GrabModeAsync, GrabModeAsync, None, None);
469 else if (key == key_enter)
470 XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
471 else if (key == key_leave)
472 XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
473 else if (key == key_focus || key == key_unfocus)
474 XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
476 XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
477 mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
480 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
482 if (key_mouse0 <= key && key <= key_mouse7)
483 XUngrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid);
486 list_t *sys_info(win_t *win)
489 XineramaScreenInfo *info = NULL;
490 if (XineramaIsActive(win->sys->dpy))
491 info = XineramaQueryScreens(win->sys->dpy, &n);
493 win_t *screen = new0(win_t);
495 return list_insert(NULL, screen);
497 list_t *screens = NULL;
498 for (int i = 0; i < n; i++) {
499 win_t *screen = new0(win_t);
500 screen->x = info[i].x_org;
501 screen->y = info[i].y_org;
502 screen->w = info[i].width;
503 screen->h = info[i].height;
504 screens = list_append(screens, screen);
509 win_t *sys_init(void)
514 /* Open the display */
515 if (!(dpy = XOpenDisplay(NULL)))
516 error("Unable to get display");
517 if (!(xid = DefaultRootWindow(dpy)))
518 error("Unable to get root window");
521 atoms[wm_proto] = XInternAtom(dpy, "WM_PROTOCOLS", False);
522 atoms[wm_focus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
523 atoms[net_strut] = XInternAtom(dpy, "_NET_WM_STRUT", False);
525 colors[clr_focus] = get_color(dpy, "#a0a0ff");
526 colors[clr_unfocus] = get_color(dpy, "#101066");
527 colors[clr_urgent] = get_color(dpy, "#ff0000");
528 printf("colors = #%06lx #%06lx #%06lx\n", colors[0], colors[1], colors[2]);
530 /* Selec Window Managmenet events */
531 XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
532 XSetInputFocus(dpy, None, RevertToNone, CurrentTime);
533 xerrorxlib = XSetErrorHandler(xerror);
535 return win_find(dpy, xid, 1);
538 void sys_run(win_t *root)
540 /* Add each initial window */
542 Window par, xid, *kids = NULL;
543 if (XQueryTree(root->sys->dpy, root->sys->xid,
544 &par, &xid, &kids, &nkids))
545 for(int i = 0; i < nkids; i++) {
546 win_t *win = win_find(root->sys->dpy, kids[i], 1);
547 if (win && win_viewable(win) && !add_strut(root,win))
550 wm_update(); // For struts
556 XNextEvent(root->sys->dpy, &ev);
557 process_event(ev.type, &ev, root);