+static int win_viewable(win_t *win)
+{
+ XWindowAttributes attr;
+ if (XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr))
+ return attr.map_state == IsViewable;
+ else
+ return True;
+}
+
+/* Drawing functions */
+static unsigned long get_color(Display *dpy, const char *name)
+{
+ XColor color;
+ int screen = DefaultScreen(dpy);
+ Colormap cmap = DefaultColormap(dpy, screen);
+ XAllocNamedColor(dpy, cmap, name, &color, &color);
+ return color.pixel;
+}
+
+/* Callbacks */
+static void process_event(int type, XEvent *ev, win_t *root)
+{
+ Display *dpy = root->sys->dpy;
+ win_t *win = NULL;
+ //printf("event: %d\n", type);
+
+ /* Common data for all these events ... */
+ ptr_t ptr; mod_t mod;
+ if (type == KeyPress || type == KeyRelease ||
+ type == ButtonPress || type == ButtonRelease ||
+ type == MotionNotify) {
+ Window xid = getfocus(root, ev);
+ if (!(win = win_find(dpy,xid,0)))
+ return;
+ ptr = x2ptr(ev);
+ mod = x2mod(ev->xkey.state, type==KeyRelease||type==ButtonRelease);
+ }
+
+ /* Split based on event */
+ if (type == KeyPress) {
+ while (XCheckTypedEvent(dpy, KeyPress, ev));
+ KeySym sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0);
+ printf("got key %c %hhx\n", x2key(sym), mod2int(mod));
+ wm_handle_key(win, x2key(sym), mod, ptr);
+ }
+ else if (type == KeyRelease) {
+ //printf("release: %d\n", type);
+ }
+ else if (type == ButtonPress) {
+ if (wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr))
+ XGrabPointer(dpy, ev->xbutton.root, True, PointerMotionMask|ButtonReleaseMask,
+ GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
+ else {
+ printf("resending event\n");
+ XSendEvent(win->sys->dpy, ev->xbutton.window, True, NoEventMask, ev);
+ XSendEvent(win->sys->dpy, ev->xbutton.window, False, NoEventMask, ev);
+ XSendEvent(win->sys->dpy, ev->xbutton.root, True, NoEventMask, ev);
+ XSendEvent(win->sys->dpy, ev->xbutton.root, False, NoEventMask, ev);
+ XSendEvent(win->sys->dpy, ev->xbutton.subwindow, True, NoEventMask, ev);
+ XSendEvent(win->sys->dpy, ev->xbutton.subwindow, False, NoEventMask, ev);
+ }
+ }
+ else if (type == ButtonRelease) {
+ XUngrabPointer(dpy, CurrentTime);
+ wm_handle_key(win, x2btn(ev->xbutton.button), mod, ptr);
+ }
+ else if (type == MotionNotify) {
+ while (XCheckTypedEvent(dpy, MotionNotify, ev));
+ wm_handle_ptr(win, ptr);
+ }
+ else if (type == EnterNotify || type == LeaveNotify) {
+ printf("enter: %d\n", type);
+ key_t key = EnterNotify ? key_enter : key_leave;
+ if ((win = win_find(dpy,ev->xcrossing.window,0)))
+ wm_handle_key(win, key, MOD(), PTR());
+ }
+ else if (type == FocusIn || type == FocusOut) {
+ //printf("focus: %d\n", type);
+ key_t key = FocusIn ? key_focus : key_unfocus;
+ if ((win = win_find(dpy,ev->xfocus.window,0)))
+ wm_handle_key(win, key, MOD(), PTR());
+ }
+ else if (type == ConfigureNotify) {
+ printf("configure: %d\n", type);
+ }
+ else if (type == MapNotify) {
+ printf("map: %d\n", type);
+ }
+ else if (type == UnmapNotify) {
+ if ((win = win_find(dpy,ev->xunmap.window,0)) &&
+ win->sys->state == st_show) {
+ if (!strut_del(root, win))
+ wm_remove(win);
+ else
+ wm_update();
+ win->sys->state = st_hide;
+ }
+ }
+ else if (type == DestroyNotify) {
+ //printf("destroy: %d\n", type);
+ if ((win = win_find(dpy,ev->xdestroywindow.window,0)))
+ win_remove(win);
+ }
+ else if (type == ConfigureRequest) {
+ XConfigureRequestEvent *cre = &ev->xconfigurerequest;
+ printf("configure_req: %d - %x, (0x%lx) %dx%d @ %d,%d\n",
+ type, (int)cre->window, cre->value_mask,
+ cre->height, cre->width, cre->x, cre->y);
+ XConfigureWindow(dpy, cre->window, cre->value_mask, &(XWindowChanges){
+ .x = cre->x,
+ .y = cre->y,
+ .width = cre->width,
+ .height = cre->height,
+ });
+
+ /* This seems necessary for, but causes flicker
+ * there could be a better way to do this */
+ if ((win = win_find(dpy,ev->xmaprequest.window,0)))
+ sys_move(win, win->x, win->y, win->w, win->h);
+ }
+ else if (type == MapRequest) {
+ printf("map_req: %d\n", type);
+ if ((win = win_find(dpy,ev->xmaprequest.window,1))) {
+ if (!strut_add(root, win))
+ wm_insert(win);
+ else
+ wm_update();
+ }
+ XMapWindow(dpy,ev->xmaprequest.window);
+ }
+ else {
+ printf("unknown event: %d\n", type);
+ }
+}
+
+static int xerror(Display *dpy, XErrorEvent *err)
+{
+ if (err->error_code == BadWindow ||
+ (err->request_code == X_SetInputFocus && err->error_code == BadMatch ) ||
+ (err->request_code == X_PolyText8 && err->error_code == BadDrawable) ||
+ (err->request_code == X_PolyFillRectangle && err->error_code == BadDrawable) ||
+ (err->request_code == X_PolySegment && err->error_code == BadDrawable) ||
+ (err->request_code == X_ConfigureWindow && err->error_code == BadMatch ) ||
+ (err->request_code == X_GrabButton && err->error_code == BadAccess ) ||
+ (err->request_code == X_GrabKey && err->error_code == BadAccess ) ||
+ (err->request_code == X_CopyArea && err->error_code == BadDrawable))
+ return 0;
+ return xerrorxlib(dpy, err);
+}
+
+/********************
+ * System functions *
+ ********************/
+void sys_move(win_t *win, int x, int y, int w, int h)
+{
+ //printf("sys_move: %p - %d,%d %dx%d\n", win, x, y, w, h);
+ int b = 2*BORDER;
+ win->x = x; win->y = y;
+ win->w = MAX(w,1+b); win->h = MAX(h,1+b);
+ w = MAX(w-b,1); h = MAX(h-b,1);
+ XMoveResizeWindow(win->sys->dpy, win->sys->xid, x, y, w, h);
+
+ /* Flush events, so moving window doesn't cause re-focus
+ * There's probably a better way to do this */
+ XEvent ev;
+ XSync(win->sys->dpy, False);
+ while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &ev))
+ printf("Skipping enter/leave event\n");
+}
+
+void sys_raise(win_t *win)
+{
+ //printf("sys_raise: %p\n", win);
+ XRaiseWindow(win->sys->dpy, win->sys->xid);
+}
+
+void sys_focus(win_t *win)
+{
+ //printf("sys_focus: %p\n", win);
+
+ /* Set border on focused window */
+ static win_t *last = NULL;
+ if (last)
+ XSetWindowBorder(last->sys->dpy, last->sys->xid, colors[clr_unfocus]);
+ XSetWindowBorder(win->sys->dpy, win->sys->xid, colors[clr_focus]);
+ XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, BORDER);
+ last = win;
+
+ /* Set actual focus */
+ XSetInputFocus(win->sys->dpy, win->sys->xid,
+ RevertToPointerRoot, CurrentTime);
+ XSendEvent(win->sys->dpy, win->sys->xid, False, NoEventMask, &(XEvent){
+ .type = ClientMessage,
+ .xclient.window = win->sys->xid,
+ .xclient.message_type = atoms[wm_proto],
+ .xclient.format = 32,
+ .xclient.data.l[0] = atoms[wm_focus],
+ .xclient.data.l[1] = CurrentTime,
+ });
+}
+
+void sys_show(win_t *win, state_t state)
+{
+ win->sys->state = state;
+ switch (state) {
+ case st_show:
+ printf("sys_show: show\n");
+ XMapWindow(win->sys->dpy, win->sys->xid);
+ return;
+ case st_full:
+ printf("sys_show: full\n");
+ return;
+ case st_shade:
+ printf("sys_show: shade\n");
+ return;
+ case st_icon:
+ printf("sys_show: icon\n");
+ return;
+ case st_hide:
+ printf("sys_show: hide\n");
+ XUnmapWindow(win->sys->dpy, win->sys->xid);
+ return;
+ }
+}
+
+void sys_watch(win_t *win, Key_t key, mod_t mod)
+{
+ //printf("sys_watch: %p - %x %hhx\n", win, key, mod);
+ XWindowAttributes attr;
+ XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
+ long mask = attr.your_event_mask;
+ if (key_mouse0 <= key && key <= key_mouse7)
+ XGrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid, False,
+ mod.up ? ButtonReleaseMask : ButtonPressMask,
+ GrabModeAsync, GrabModeAsync, None, None);
+ else if (key == key_enter)
+ XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
+ else if (key == key_leave)
+ XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
+ else if (key == key_focus || key == key_unfocus)
+ XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
+ else
+ XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, key2x(key)),
+ mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
+}
+
+void sys_unwatch(win_t *win, Key_t key, mod_t mod)
+{
+ if (key_mouse0 <= key && key <= key_mouse7)
+ XUngrabButton(win->sys->dpy, btn2x(key), mod2x(mod), win->sys->xid);
+}
+
+list_t *sys_info(win_t *win)
+{
+ /* Use global copy of screens so we can add struts */
+ if (screens == NULL) {
+ int n;
+ XineramaScreenInfo *info = NULL;
+ if (XineramaIsActive(win->sys->dpy))
+ info = XineramaQueryScreens(win->sys->dpy, &n);
+ if (!info) {
+ win_t *screen = new0(win_t);
+ *screen = *win;
+ return list_insert(NULL, screen);
+ }
+ for (int i = 0; i < n; i++) {
+ win_t *screen = new0(win_t);
+ screen->x = info[i].x_org;
+ screen->y = info[i].y_org;
+ screen->w = info[i].width;
+ screen->h = info[i].height;
+ screens = list_append(screens, screen);
+ }
+ }
+ return screens;
+}
+