+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 *
+ ********************/