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;
32 wm_proto, wm_focus, net_strut, natoms
36 clr_focus, clr_unfocus, clr_urgent, ncolors
40 static void *win_cache;
41 static Atom atoms[natoms];
42 static int (*xerrorxlib)(Display *, XErrorEvent *);
43 static unsigned long colors[ncolors];
45 /* Conversion functions */
46 static keymap_t key2sym[] = {
47 {key_left , XK_Left },
48 {key_right , XK_Right},
50 {key_down , XK_Down },
51 {key_home , XK_Home },
53 {key_pageup , XK_Prior},
54 {key_pagedown, XK_Next },
70 static mod_t x2mod(unsigned int state, int up)
73 .alt = !!(state & Mod1Mask ),
74 .ctrl = !!(state & ControlMask),
75 .shift = !!(state & ShiftMask ),
76 .win = !!(state & Mod4Mask ),
81 static unsigned int mod2x(mod_t mod)
83 return (mod.alt ? Mod1Mask : 0)
84 | (mod.ctrl ? ControlMask : 0)
85 | (mod.shift ? ShiftMask : 0)
86 | (mod.win ? Mod4Mask : 0);
90 static Key_t x2key(KeySym sym)
92 keymap_t *km = map_getr(key2sym,sym);
93 return km ? km->key : sym;
96 static KeySym key2x(Key_t key)
98 keymap_t *km = map_get(key2sym,key);
99 return km ? km->sym : key;
102 static Key_t x2btn(int btn)
104 return btn + key_mouse0;
107 static int btn2x(Key_t key)
109 return key - key_mouse0;
113 static ptr_t x2ptr(XEvent *_ev)
115 XKeyEvent *ev = &_ev->xkey;
116 return (ptr_t){ev->x, ev->y, ev->x_root, ev->y_root};
119 static Window getfocus(win_t *root, XEvent *event)
122 Window focus = PointerRoot;
123 if (event->type == KeyPress || event->type == KeyRelease)
124 XGetInputFocus(root->sys->dpy, &focus, &revert);
125 if (focus == PointerRoot)
126 focus = event->xkey.subwindow;
128 focus = event->xkey.window;
133 static int add_strut(win_t *root, win_t *win)
135 /* Get X11 strut data */
138 unsigned long ret_items, bytes_left;
139 unsigned char *xdata;
140 int status = XGetWindowProperty(win->sys->dpy, win->sys->xid,
141 atoms[net_strut], 0L, 4L, False, XA_CARDINAL,
142 &ret_type, &ret_size, &ret_items, &bytes_left, &xdata);
143 if (status != Success || ret_size != 32 || ret_items != 4)
146 int left = ((int*)xdata)[0];
147 int right = ((int*)xdata)[1];
148 int top = ((int*)xdata)[2];
149 int bottom = ((int*)xdata)[3];
150 if (left == 0 && right == 0 && top == 0 && bottom == 0)
153 win->sys->strut.left = left;
154 win->sys->strut.right = right;
155 win->sys->strut.top = top;
156 win->sys->strut.bottom = bottom;
159 root->w -= left+right;
160 root->h -= top+bottom;
164 static int del_strut(win_t *root, win_t *win)
166 int left = win->sys->strut.left;
167 int right = win->sys->strut.right;
168 int top = win->sys->strut.top;
169 int bottom = win->sys->strut.bottom;
170 if (left == 0 && right == 0 && top == 0 && bottom == 0)
175 root->w += left+right;
176 root->h += top+bottom;
180 /* Window functions */
181 static win_t *win_new(Display *dpy, Window xid)
183 XWindowAttributes attr;
184 if (XGetWindowAttributes(dpy, xid, &attr))
185 if (attr.override_redirect)
187 win_t *win = new0(win_t);
191 win->h = attr.height;
192 win->sys = new0(win_sys_t);
195 printf("win_new: %p = %p, %d (%d,%d %dx%d)\n",
197 win->x, win->y, win->w, win->h);
201 static int win_cmp(const void *_a, const void *_b)
203 const win_t *a = _a, *b = _b;
204 if (a->sys->dpy < b->sys->dpy) return -1;
205 if (a->sys->dpy > b->sys->dpy) return 1;
206 if (a->sys->xid < b->sys->xid) return -1;
207 if (a->sys->xid > b->sys->xid) return 1;
211 static win_t *win_find(Display *dpy, Window xid, int create)
215 //printf("win_find: %p, %d\n", dpy, (int)xid);
216 win_sys_t sys = {.dpy=dpy, .xid=xid};
217 win_t tmp = {.sys=&sys};
218 win_t **old = NULL, *new = NULL;
219 if ((old = tfind(&tmp, &win_cache, win_cmp)))
221 if (create && (new = win_new(dpy,xid)))
222 tsearch(new, &win_cache, win_cmp);
226 static void win_remove(win_t *win)
228 tdelete(win, &win_cache, win_cmp);
233 static int win_viewable(win_t *win)
235 XWindowAttributes attr;
236 if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
237 return attr.map_state == IsViewable;
242 /* Drawing functions */
243 unsigned long get_color(Display *dpy, const char *name)
246 int screen = DefaultScreen(dpy);
247 Colormap cmap = DefaultColormap(dpy, screen);
248 XAllocNamedColor(dpy, cmap, name, &color, &color);
253 static void process_event(int type, XEvent *ev, win_t *root)
255 Display *dpy = root->sys->dpy;
257 printf("event: %d\n", type);
259 /* Common data for all these events ... */
260 ptr_t ptr; mod_t mod;
261 if (type == KeyPress || type == KeyRelease ||
262 type == ButtonPress || type == ButtonRelease ||
263 type == MotionNotify) {
264 Window xid = getfocus(root, ev);
265 if (!(win = win_find(dpy,xid,0)))
268 mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
271 /* Split based on event */
272 if (type == KeyPress) {
273 while (XCheckTypedEvent(dpy, KeyPress, ev));
274 KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
275 wm_handle_key(win, x2key(sym), mod, ptr);
277 else if (type == KeyRelease) {
278 //printf("release: %d\n", type);
280 else if (type == ButtonPress) {
281 if (wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr))
282 XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
283 GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
285 printf("resending event\n");
286 XSendEvent(win->sys->dpy, ev->xbutton.window, True, NoEventMask, ev);
287 XSendEvent(win->sys->dpy, ev->xbutton.window, False, NoEventMask, ev);
288 XSendEvent(win->sys->dpy, ev->xbutton.root, True, NoEventMask, ev);
289 XSendEvent(win->sys->dpy, ev->xbutton.root, False, NoEventMask, ev);
290 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, True, NoEventMask, ev);
291 XSendEvent(win->sys->dpy, ev->xbutton.subwindow, False, NoEventMask, ev);
294 else if (type == ButtonRelease) {
295 XUngrabPointer(dpy, CurrentTime);
296 wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
298 else if (type == MotionNotify) {
299 while (XCheckTypedEvent(dpy, MotionNotify, ev));
300 wm_handle_ptr(win, ptr);
302 else if (type == EnterNotify || type == LeaveNotify) {
303 printf("enter: %d\n", type);
304 key_t key = EnterNotify ? key_enter : key_leave;
305 if ((win = win_find(dpy,ev->xcrossing.window,0)))
306 wm_handle_key(win, key, MOD(), PTR());
308 else if (type == FocusIn || type == FocusOut) {
309 printf("focus: %d\n", type);
310 key_t key = FocusIn ? key_focus : key_unfocus;
311 if ((win = win_find(dpy,ev->xfocus.window,0)))
312 wm_handle_key(win, key, MOD(), PTR());
314 else if (type == ConfigureNotify) {
315 printf("configure: %d\n", type);
317 else if (type == MapNotify) {
318 printf("map: %d\n", type);
320 else if (type == UnmapNotify) {
321 //printf("unmap: %d\n", type);
322 if ((win = win_find(dpy,ev->xunmap.window,0))) {
323 if (!del_strut(root, win))
330 else if (type == ConfigureRequest) {
331 XConfigureRequestEvent *cre = &ev->xconfigurerequest;
332 printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
333 type, (int)cre->window, cre->value_mask,
334 cre->height, cre->width, cre->x, cre->y);
335 XConfigureWindow(dpy, cre->window, cre->value_mask, &(XWindowChanges){
339 .height = cre->height,
342 /* This seems necessasairy for, but causes flicker
343 * there could be a better way to do this */
344 if ((win = win_find(dpy,ev->xmaprequest.window,0)))
345 sys_move(win, win->x, win->y, win->w, win->h);
347 else if (type == MapRequest) {
348 printf("map_req: %d\n", type);
349 if ((win = win_find(dpy,ev->xmaprequest.window,1))) {
350 if (!add_strut(root, win))
355 XMapWindow(dpy,ev->xmaprequest.window);
358 printf("unknown event: %d\n", type);
362 static int xerror(Display *dpy, XErrorEvent *err)
364 if (err->error_code == BadWindow ||
365 (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
366 (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
367 (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
368 (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
369 (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
370 (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
371 (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
372 (err->request_code == X_CopyArea && err->error_code == BadDrawable))
374 return xerrorxlib(dpy, err);
380 void sys_move(win_t *win, int x, int y, int w, int h)
382 //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
384 win->x = MAX(x,0); win->y = MAX(y,0);
385 win->w = MAX(w,1+b); win->h = MAX(h,1+b);
386 w = MAX(w-b,1); h = MAX(h-b,1);
387 XMoveResizeWindow(win->sys->dpy, win->sys->xid, x, y, w, h);
389 /* Flush events, so moving window doesn't cuase re-focus
390 * There's probably a better way to do this */
392 XSync(win->sys->dpy, False);
393 while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
394 printf("Skipping enter/leave event\n");
397 void sys_raise(win_t *win)
399 //printf("sys_raise: %p\n", win);
400 XRaiseWindow(win->sys->dpy, win->sys->xid);
403 void sys_focus(win_t *win)
405 printf("sys_focus: %p\n", win);
407 /* Set border on focused window */
408 static win_t *last = NULL;
410 XSetWindowBorder(last->sys->dpy, last->sys->xid, colors[clr_unfocus]);
411 XSetWindowBorder(win->sys->dpy, win->sys->xid, colors[clr_focus]);
412 XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, BORDER);
415 /* Set actual focus */
416 XSetInputFocus(win->sys->dpy, win->sys->xid,
417 RevertToPointerRoot, CurrentTime);
418 XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &(XEvent){
419 .type = ClientMessage,
420 .xclient.window = win->sys->xid,
421 .xclient.message_type = atoms[wm_proto],
422 .xclient.format = 32,
423 .xclient.data.l[0] = atoms[wm_focus],
424 .xclient.data.l[1] = CurrentTime,
428 void sys_watch(win_t *win, Key_t key, mod_t mod)
430 //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
431 XWindowAttributes attr;
432 XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
433 long mask = attr.your_event_mask;
434 if (key_mouse0 <= key && key <= key_mouse7)
435 XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, False,
436 mod.up ? ButtonReleaseMask : ButtonPressMask,
437 GrabModeAsync, GrabModeAsync, None, None);
438 else if (key == key_enter)
439 XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
440 else if (key == key_leave)
441 XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
442 else if (key == key_focus || key == key_unfocus)
443 XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
445 XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
446 mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
449 void sys_unwatch(win_t *win, Key_t key, mod_t mod)
451 if (key_mouse0 <= key && key <= key_mouse7)
452 XUngrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid);
455 list_t *sys_info(win_t *win)
458 XineramaScreenInfo *info = NULL;
459 if (XineramaIsActive(win->sys->dpy))
460 info = XineramaQueryScreens(win->sys->dpy, &n);
462 win_t *screen = new0(win_t);
464 return list_insert(NULL, screen);
466 list_t *screens = NULL;
467 for (int i = 0; i < n; i++) {
468 win_t *screen = new0(win_t);
469 screen->x = info[i].x_org;
470 screen->y = info[i].y_org;
471 screen->w = info[i].width;
472 screen->h = info[i].height;
473 screens = list_append(screens, screen);
478 win_t *sys_init(void)
483 /* Open the display */
484 if (!(dpy = XOpenDisplay(NULL)))
485 error("Unable to get display");
486 if (!(xid = DefaultRootWindow(dpy)))
487 error("Unable to get root window");
490 atoms[wm_proto] = XInternAtom(dpy, "WM_PROTOCOLS", False);
491 atoms[wm_focus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
492 atoms[net_strut] = XInternAtom(dpy, "_NET_WM_STRUT", False);
494 colors[clr_focus] = get_color(dpy, "#a0a0ff");
495 colors[clr_unfocus] = get_color(dpy, "#101066");
496 colors[clr_urgent] = get_color(dpy, "#ff0000");
497 printf("colors = #%06lx #%06lx #%06lx\n", colors[0], colors[1], colors[2]);
499 /* Selec Window Managmenet events */
500 XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
501 XSetInputFocus(dpy, None, RevertToNone, CurrentTime);
502 xerrorxlib = XSetErrorHandler(xerror);
504 return win_find(dpy, xid, 1);
507 void sys_run(win_t *root)
509 /* Add each initial window */
511 Window par, xid, *kids = NULL;
512 if (XQueryTree(root->sys->dpy, root->sys->xid,
513 &par, &xid, &kids, &nkids))
514 for(int i = 0; i < nkids; i++) {
515 win_t *win = win_find(root->sys->dpy, kids[i], 1);
516 if (win && win_viewable(win) && !add_strut(root,win))
519 wm_update(); // For struts
525 XNextEvent(root->sys->dpy, &ev);
526 process_event(ev.type, &ev, root);