+ else if (type == PropertyNotify) {
+ printf("prop: %d - %d\n", type, xe->xproperty.state);
+ }
+ 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;
+ if (err->request_code == X_ChangeWindowAttributes && err->error_code == BadAccess)
+ error("Another window manager is already running");
+ return xerrorxlib(dpy, err);
+}
+
+static int xnoerror(Display *dpy, XErrorEvent *err)
+{
+ return 0;
+}
+
+
+/********************
+ * 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 xe;
+ XSync(win->sys->dpy, False);
+ while (XCheckMaskEvent(win->sys->dpy, EnterWindowMask|LeaveWindowMask, &xe))
+ 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);
+ for (list_t *cur = struts; cur; cur = cur->next)
+ XRaiseWindow(((win_t*)cur->data)->sys->dpy,
+ ((win_t*)cur->data)->sys->xid);
+}
+
+void sys_focus(win_t *win)
+{
+ printf("sys_focus: %p\n", win);
+
+ /* Set actual focus */
+ XSetInputFocus(win->sys->dpy, win->sys->xid,
+ RevertToPointerRoot, CurrentTime);
+ win_msg(win, WM_FOCUS);
+
+ /* Set border on focused window */
+ static win_t *last = NULL;
+ if (last)
+ XSetWindowBorder(last->sys->dpy, last->sys->xid, colors[CLR_UNFOCUS]);
+ XSync(win->sys->dpy, False);
+ XSetWindowBorder(win->sys->dpy, win->sys->xid, colors[CLR_FOCUS]);
+ last = win;
+}
+
+void sys_show(win_t *win, state_t state)
+{
+ switch (state) {
+ case ST_HIDE:
+ printf("sys_show: hide %p\n", win);
+ XUnmapWindow(win->sys->dpy, win->sys->xid);
+ break;
+ case ST_SHOW:
+ printf("sys_show: show %p\n", win);
+ if (win->state == ST_FULL)
+ sys_move(win, win->x, win->y, win->w, win->h);
+ XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, border);
+ XMapWindow(win->sys->dpy, win->sys->xid);
+ XSync(win->sys->dpy, False);
+ break;
+ case ST_FULL:
+ printf("sys_show: full %p\n", win);
+ win_t *screen = NULL;
+ for (list_t *cur = screens; cur; cur = cur->next) {
+ screen = cur->data;
+ if (win->x >= screen->x && win->x <= screen->x+screen->w &&
+ win->y >= screen->y && win->y <= screen->y+screen->h)
+ break;
+ }
+ XSetWindowBorderWidth(win->sys->dpy, win->sys->xid, 0);
+ XMapWindow(win->sys->dpy, win->sys->xid);
+ XMoveResizeWindow(win->sys->dpy, win->sys->xid,
+ screen->x - screen->sys->strut.left,
+ screen->y - screen->sys->strut.top,
+ screen->w + screen->sys->strut.left + screen->sys->strut.right,
+ screen->h + screen->sys->strut.top + screen->sys->strut.bottom);
+ XRaiseWindow(win->sys->dpy, win->sys->xid);
+ break;
+ case ST_SHADE:
+ printf("sys_show: shade %p\n", win);
+ XMapWindow(win->sys->dpy, win->sys->xid);
+ break;
+ case ST_ICON:
+ printf("sys_show: icon %p\n", win);
+ break;
+ case ST_CLOSE:
+ printf("sys_show: close %p\n", win);
+ if (!win_msg(win, WM_DELETE)) {
+ XGrabServer(win->sys->dpy);
+ XSetErrorHandler(xnoerror);
+ XSetCloseDownMode(win->sys->dpy, DestroyAll);
+ XKillClient(win->sys->dpy, win->sys->xid);
+ XSync(win->sys->dpy, False);
+ XSetErrorHandler(xerror);
+ XUngrabServer(win->sys->dpy);
+ }
+ XDestroyWindow(win->sys->dpy, win->sys->xid);
+ break;
+ }
+ win->state = state;
+}
+
+void sys_watch(win_t *win, event_t ev, mod_t mod)
+{
+ //printf("sys_watch: %p - %x %hhx\n", win, ev, mod);
+ XWindowAttributes attr;
+ XGetWindowAttributes(win->sys->dpy, win->sys->xid, &attr);
+ long mask = attr.your_event_mask;
+ if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7)
+ XGrabButton(win->sys->dpy, ev2xb(ev), mod2x(mod), win->sys->xid, False,
+ mod.up ? ButtonReleaseMask : ButtonPressMask,
+ GrabModeSync, GrabModeAsync, None, None);
+ else if (ev == EV_ENTER)
+ XSelectInput(win->sys->dpy, win->sys->xid, EnterWindowMask|mask);
+ else if (ev == EV_LEAVE)
+ XSelectInput(win->sys->dpy, win->sys->xid, LeaveWindowMask|mask);
+ else if (ev == EV_FOCUS || ev == EV_UNFOCUS)
+ XSelectInput(win->sys->dpy, win->sys->xid, FocusChangeMask|mask);
+ else
+ XGrabKey(win->sys->dpy, XKeysymToKeycode(win->sys->dpy, ev2xk(ev)),
+ mod2x(mod), win->sys->xid, True, GrabModeAsync, GrabModeAsync);
+}
+
+void sys_unwatch(win_t *win, event_t ev, mod_t mod)
+{
+ if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7)
+ XUngrabButton(win->sys->dpy, ev2xb(ev), 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) {
+ /* Add Xinerama screens */
+ int n = 0;
+ XineramaScreenInfo *info = NULL;
+ if (XineramaIsActive(win->sys->dpy))
+ info = XineramaQueryScreens(win->sys->dpy, &n);
+ 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;
+ screen->sys = new0(win_sys_t);
+ screens = list_append(screens, screen);
+ }
+ }
+ if (screens == NULL) {
+ /* No xinerama support */
+ win_t *screen = new0(win_t);
+ *screen = *win;
+ screens = list_insert(NULL, screen);
+ }
+ return screens;
+}
+
+win_t *sys_init(void)
+{
+ Display *dpy;
+ Window xid;
+
+ /* Load configuration */
+ border = conf_get_int("main.border", border);
+ no_capture = conf_get_int("main.no-capture", no_capture);
+
+ /* Open the display */
+ if (!(dpy = XOpenDisplay(NULL)))
+ error("Unable to get display");
+ if (!(xid = DefaultRootWindow(dpy)))
+ error("Unable to get root window");
+
+ /* Setup X11 data */
+ atoms[WM_PROTO] = XInternAtom(dpy, "WM_PROTOCOLS", False);
+ atoms[WM_FOCUS] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
+ atoms[WM_DELETE] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
+ atoms[NET_STATE] = XInternAtom(dpy, "_NET_WM_STATE", False);
+ atoms[NET_FULL] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
+ atoms[NET_STRUT] = XInternAtom(dpy, "_NET_WM_STRUT", False);
+
+ colors[CLR_FOCUS] = get_color(dpy, "#a0a0ff");
+ colors[CLR_UNFOCUS] = get_color(dpy, "#101066");
+ colors[CLR_URGENT] = get_color(dpy, "#ff0000");
+ //printf("colors = #%06lx #%06lx #%06lx\n", colors[0], colors[1], colors[2]);
+
+ /* Select window management events */
+ XSelectInput(dpy, xid, SubstructureRedirectMask|SubstructureNotifyMask);
+ xerrorxlib = XSetErrorHandler(xerror);
+
+ return win_find(dpy, xid, 1);
+}
+
+void sys_run(win_t *root)
+{
+ /* Add each initial window */
+ if (!no_capture) {
+ unsigned int nkids;
+ Window par, xid, *kids = NULL;
+ if (XQueryTree(root->sys->dpy, root->sys->xid,
+ &par, &xid, &kids, &nkids)) {
+ for(int i = 0; i < nkids; i++) {
+ win_t *win = win_find(root->sys->dpy, kids[i], 1);
+ if (win && win_viewable(win) && !strut_add(root,win))
+ wm_insert(win);
+ }
+ XFree(kids);
+ }
+ wm_update(); // For struts
+ }
+
+ /* Main loop */
+ running = 1;
+ while (running)
+ {
+ XEvent xe;
+ XNextEvent(root->sys->dpy, &xe);
+ process_event(xe.type, &xe, root);
+ }
+}
+
+void sys_exit(void)
+{
+ running = 0;
+}
+
+void sys_free(win_t *root)
+{
+ XCloseDisplay(root->sys->dpy);
+ while (screens) {
+ win_free(screens->data);
+ screens = list_remove(screens, screens, 0);
+ }
+ tdestroy(cache, (void(*)(void*))win_free);