JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Change some 'Window' types to xcb_window_t's.
[spectrwm.git] / spectrwm.c
index 63e925a..e2ebcf6 100644 (file)
@@ -336,8 +336,8 @@ struct swm_screen;
 struct workspace;
 
 struct swm_bar {
-       Window                  id;
-       Pixmap                  buffer;
+       xcb_window_t            id;
+       xcb_pixmap_t            buffer;
        struct swm_geometry     g;
 };
 
@@ -354,8 +354,8 @@ TAILQ_HEAD(swm_region_list, swm_region);
 
 struct ws_win {
        TAILQ_ENTRY(ws_win)     entry;
-       Window                  id;
-       Window                  transient;
+       xcb_window_t            id;
+       xcb_window_t            transient;
        struct ws_win           *child_trans;   /* transient child window */
        struct swm_geometry     g;              /* current geometry */
        struct swm_geometry     g_float;        /* region coordinates */
@@ -402,12 +402,12 @@ void      max_stack(struct workspace *, struct swm_geometry *);
 void   plain_stacker(struct workspace *);
 void   fancy_stacker(struct workspace *);
 
-struct ws_win *find_window(Window);
+struct ws_win *find_window(xcb_window_t);
 
 void   grabbuttons(struct ws_win *, int);
 void   new_region(struct swm_screen *, int, int, int, int);
 void   unmanage_window(struct ws_win *);
-long   getstate(Window);
+long   getstate(xcb_window_t);
 
 int    conf_load(char *, int);
 
@@ -479,7 +479,7 @@ struct swm_screen {
        int                     idx;    /* screen index */
        struct swm_region_list  rl;     /* list of regions on this screen */
        struct swm_region_list  orl;    /* list of old regions */
-       Window                  root;
+       xcb_window_t            root;
        struct workspace        ws[SWM_WS_MAX];
 
        /* colors */
@@ -587,34 +587,34 @@ enum {
 };
 
 struct ewmh_hint {
-       char    *name;
-       Atom     atom;
+       char            *name;
+       xcb_atom_t      atom;
 } ewmh[SWM_EWMH_HINT_MAX] =    {
     /* must be in same order as in the enum */
-    {"_NET_ACTIVE_WINDOW", None},
-    {"_NET_CLOSE_WINDOW", None},
-    {"_NET_MOVERESIZE_WINDOW", None},
-    {"_NET_WM_ACTION_CLOSE", None},
-    {"_NET_WM_ACTION_FULLSCREEN", None},
-    {"_NET_WM_ACTION_MOVE", None},
-    {"_NET_WM_ACTION_RESIZE", None},
-    {"_NET_WM_ALLOWED_ACTIONS", None},
-    {"_NET_WM_STATE", None},
-    {"_NET_WM_STATE_ABOVE", None},
-    {"_NET_WM_STATE_FULLSCREEN", None},
-    {"_NET_WM_STATE_HIDDEN", None},
-    {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
-    {"_NET_WM_STATE_MAXIMIZED_VERT", None},
-    {"_NET_WM_STATE_SKIP_PAGER", None},
-    {"_NET_WM_STATE_SKIP_TASKBAR", None},
-    {"_NET_WM_WINDOW_TYPE", None},
-    {"_NET_WM_WINDOW_TYPE_DIALOG", None},
-    {"_NET_WM_WINDOW_TYPE_DOCK", None},
-    {"_NET_WM_WINDOW_TYPE_NORMAL", None},
-    {"_NET_WM_WINDOW_TYPE_SPLASH", None},
-    {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
-    {"_NET_WM_WINDOW_TYPE_UTILITY", None},
-    {"_SWM_WM_STATE_MANUAL", None},
+    {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
+    {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
+    {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
+    {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
+    {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
+    {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
+    {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
+    {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
+    {"_NET_WM_STATE", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
+    {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
+    {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
+    {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
 };
 
 void            store_float_geom(struct ws_win *, struct swm_region *);
@@ -622,7 +622,7 @@ int          floating_toggle_win(struct ws_win *);
 void            constrain_window(struct ws_win *, struct swm_region *, int);
 void            update_window(struct ws_win *);
 void            spawn_select(struct swm_region *, union arg *, char *, int *);
-unsigned char  *get_win_name(Window);
+unsigned char  *get_win_name(xcb_window_t);
 
 int
 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
@@ -675,28 +675,33 @@ update_iconic(struct ws_win *win, int newv)
 int
 get_iconic(struct ws_win *win)
 {
-       int32_t v = 0;
-       int retfmt, status;
-       Atom iprop, rettype;
-       unsigned long nitems, extra;
-       unsigned char *prop = NULL;
+       int32_t v = 0, *vtmp;
+       xcb_atom_t                      iprop;
+       xcb_intern_atom_cookie_t        c;
+       xcb_intern_atom_reply_t         *r;
+       xcb_get_property_cookie_t       pc;
+       xcb_get_property_reply_t        *pr;
 
-       iprop = XInternAtom(display, "_SWM_ICONIC", False);
-       if (!iprop)
-               goto out;
-       status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
-           False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
-       if (status != Success)
-               goto out;
-       if (rettype != XA_INTEGER || retfmt != 32)
-               goto out;
-       if (nitems != 1)
+       c = xcb_intern_atom(conn, False, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
+       r = xcb_intern_atom_reply(conn, c, NULL);
+       if (r) {
+               iprop = r->atom;
+               free(r);
+       } else
                goto out;
-       v = *((int32_t *)prop);
 
+       pc = xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
+                       0, 1);
+       pr = xcb_get_property_reply(conn, pc, NULL);
+       if (!pr)
+               goto out;       
+       if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
+               goto out;
+       vtmp = xcb_get_property_value(pr);
+       v = *vtmp;
 out:
-       if (prop != NULL)
-               XFree(prop);
+       if (pr != NULL)
+               free(pr);
        return (v);
 }
 
@@ -845,7 +850,7 @@ ewmh_set_win_fullscreen(struct ws_win *win, int fs)
 void
 ewmh_update_actions(struct ws_win *win)
 {
-       Atom                    actions[SWM_EWMH_ACTION_COUNT_MAX];
+       xcb_atom_t              actions[SWM_EWMH_ACTION_COUNT_MAX];
        int                     n = 0;
 
        if (win == NULL)
@@ -858,8 +863,9 @@ ewmh_update_actions(struct ws_win *win)
                actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
        }
 
-       XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
-           XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
+       xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
+               ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
+               actions);
 }
 
 #define _NET_WM_STATE_REMOVE   0    /* remove/unset property */
@@ -1322,13 +1328,15 @@ void
 custom_region(char *val)
 {
        unsigned int                    sidx, x, y, w, h;
+       int                             num_screens;
 
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));      
        if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
                errx(1, "invalid custom region, "
                    "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
-       if (sidx < 1 || sidx > ScreenCount(display))
+       if (sidx < 1 || sidx > num_screens)
                errx(1, "invalid screen index: %d out of bounds (maximum %d)",
-                   sidx, ScreenCount(display));
+                   sidx, num_screens);
        sidx--;
 
        if (w < 1 || h < 1)
@@ -1929,8 +1937,8 @@ bar_cleanup(struct swm_region *r)
 {
        if (r->bar == NULL)
                return;
-       XDestroyWindow(display, r->bar->id);
-       XFreePixmap(display, r->bar->buffer);
+       xcb_destroy_window(conn, r->bar->id);
+       xcb_free_pixmap(conn, r->bar->buffer);
        free(r->bar);
        r->bar = NULL;
 }
@@ -1962,7 +1970,7 @@ set_win_state(struct ws_win *win, long state)
 }
 
 long
-getstate(Window w)
+getstate(xcb_window_t w)
 {
        long                    result = -1;
        unsigned char           *p = NULL;
@@ -2240,7 +2248,7 @@ find_unmanaged_window(Window id)
 }
 
 struct ws_win *
-find_window(Window id)
+find_window(xcb_window_t id)
 {
        struct ws_win           *win;
        Window                  wrr, wpr, *wcr = NULL;
@@ -2377,10 +2385,11 @@ validate_ws(struct workspace *testws)
 {
        struct swm_region       *r;
        struct workspace        *ws;
-       int                     i, x;
+       int                     i, x, num_screens;
 
        /* validate all ws */
-       for (i = 0; i < ScreenCount(display); i++)
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
+       for (i = 0; i < num_screens; i++)
                TAILQ_FOREACH(r, &screens[i].rl, entry)
                        for (x = 0; x < workspace_limit; x++) {
                                ws = &r->s->ws[x];
@@ -2394,7 +2403,7 @@ void
 unfocus_win(struct ws_win *win)
 {
        XEvent                  cne;
-       Window                  none = None;
+       xcb_window_t            none = XCB_WINDOW_NONE;
 
        DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
 
@@ -2436,20 +2445,21 @@ unfocus_win(struct ws_win *win)
        XSetWindowBorder(display, win->id,
            win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
 
-       XChangeProperty(display, win->s->root,
-           ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
-           PropModeReplace, (unsigned char *)&none, 1);
+       xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
+               ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
+               &none);
 }
 
 void
 unfocus_all(void)
 {
        struct ws_win           *win;
-       int                     i, j;
+       int                     i, j, num_screens;
 
        DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
 
-       for (i = 0; i < ScreenCount(display); i++)
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
+       for (i = 0; i < num_screens; i++)
                for (j = 0; j < workspace_limit; j++)
                        TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
                                unfocus_win(win);
@@ -3629,7 +3639,7 @@ iconify(struct swm_region *r, union arg *args)
 }
 
 unsigned char *
-get_win_name(Window win)
+get_win_name(xcb_window_t win)
 {
        unsigned char           *prop = NULL;
        unsigned long           nbytes, nitems;
@@ -3694,7 +3704,7 @@ uniconify(struct swm_region *r, union arg *args)
                name = get_win_name(win->id);
                if (name == NULL)
                        continue;
-               fprintf(lfile, "%s.%lu\n", name, win->id);
+               fprintf(lfile, "%s.%u\n", name, win->id);
                XFree(name);
        }
 
@@ -3851,7 +3861,7 @@ search_resp_uniconify(char *resp, unsigned long len)
                name = get_win_name(win->id);
                if (name == NULL)
                        continue;
-               if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
+               if (asprintf(&s, "%s.%u", name, win->id) == -1) {
                        XFree(name);
                        continue;
                }
@@ -5390,19 +5400,25 @@ grabbuttons(struct ws_win *win, int focused)
            { 0, LockMask, numlockmask, numlockmask|LockMask };
 
        updatenumlockmask();
-       XUngrabButton(display, AnyButton, AnyModifier, win->id);
+       xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
+               XCB_BUTTON_MASK_ANY);
        if (focused) {
                for (i = 0; i < LENGTH(buttons); i++)
                        if (buttons[i].action == client_click)
                                for (j = 0; j < LENGTH(modifiers); j++)
-                                       XGrabButton(display, buttons[i].button,
-                                           buttons[i].mask | modifiers[j],
-                                           win->id, False, BUTTONMASK,
-                                           GrabModeAsync, GrabModeSync, None,
-                                           None);
+                                       xcb_grab_button(conn, False, win->id,
+                                               BUTTONMASK,
+                                               XCB_GRAB_MODE_ASYNC,
+                                               XCB_GRAB_MODE_SYNC,
+                                               XCB_WINDOW_NONE,
+                                               XCB_CURSOR_NONE,
+                                               buttons[i].button,
+                                               buttons[i].mask);
        } else
-               XGrabButton(display, AnyButton, AnyModifier, win->id, False,
-                   BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
+               xcb_grab_button(conn, False, win->id, BUTTONMASK,
+                       XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
+                       XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
+                       XCB_BUTTON_MASK_ANY); 
 }
 
 const char *quirkname[] = {
@@ -6130,7 +6146,7 @@ set_child_transient(struct ws_win *win, Window *trans)
                    " for 0x%lx trans 0x%lx\n", win->id, win->transient);
 
                if (win->hints == NULL) {
-                       warnx("no hints for 0x%lx", win->id);
+                       warnx("no hints for 0x%x", win->id);
                        return;
                }
 
@@ -6142,7 +6158,7 @@ set_child_transient(struct ws_win *win, Window *trans)
                                XFree(wmh);
 
                        if ((wmh = XGetWMHints(display, w->id)) == NULL) {
-                               warnx("can't get hints for 0x%lx", w->id);
+                               warnx("can't get hints for 0x%x", w->id);
                                continue;
                        }
 
@@ -6153,7 +6169,7 @@ set_child_transient(struct ws_win *win, Window *trans)
                        win->transient = w->id;
                        *trans = w->id;
                        DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
-                           "transient to 0x%lx\n", win->transient);
+                           "transient to 0x%x\n", win->transient);
                        break;
                }
        }
@@ -6165,45 +6181,51 @@ set_child_transient(struct ws_win *win, Window *trans)
 long
 window_get_pid(Window win)
 {
-       Atom                    actual_type_return;
-       int                     actual_format_return = 0;
-       unsigned long           nitems_return = 0;
-       unsigned long           bytes_after_return = 0;
-       long                    *pid = NULL;
-       long                    ret = 0;
-       const char              *errstr;
-       unsigned char           *prop = NULL;
+       long                            ret = 0;
+       const char                      *errstr;
+       xcb_atom_t                      apid;
+       xcb_intern_atom_cookie_t        c;
+       xcb_intern_atom_reply_t         *r;
+       xcb_get_property_cookie_t       pc;
+       xcb_get_property_reply_t        *pr;    
 
-       if (XGetWindowProperty(display, win,
-           XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
-           XA_CARDINAL, &actual_type_return, &actual_format_return,
-           &nitems_return, &bytes_after_return,
-           (unsigned char**)(void*)&pid) != Success)
+       c = xcb_intern_atom(conn, False, strlen("_NET_WM_PID"), "_NET_WM_PID");
+       r = xcb_intern_atom_reply(conn, c, NULL);
+       if (r) {
+               apid = r->atom;
+               free(r);
+       } else
                goto tryharder;
-       if (actual_type_return != XA_CARDINAL)
+
+       pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
+       pr = xcb_get_property_reply(conn, pc, NULL);
+       if (!pr)
                goto tryharder;
-       if (pid == NULL)
+       if (pr->type != XCB_ATOM_CARDINAL)
                goto tryharder;
 
-       ret = *pid;
-       XFree(pid);
+       ret = *(long *)xcb_get_property_value(pr);
+       free(pr);
 
        return (ret);
 
 tryharder:
-       if (XGetWindowProperty(display, win,
-           XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
-           XA_STRING, &actual_type_return, &actual_format_return,
-           &nitems_return, &bytes_after_return, &prop) != Success)
-               return (0);
-       if (actual_type_return != XA_STRING)
+       c = xcb_intern_atom(conn, False, strlen("_SWM_PID"), "_SWM_PID");
+       r = xcb_intern_atom_reply(conn, c, NULL);
+       if (r) {
+               apid = r->atom;
+               free(r);
+       }
+       pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
+               0, SWM_PROPLEN);
+       pr = xcb_get_property_reply(conn, pc, NULL);
+       if (!pr)
                return (0);
-       if (prop == NULL)
+       if (pr->type != XCB_ATOM_STRING)
+               free(pr);
                return (0);
-
-       ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
-       /* ignore error because strtonum returns 0 anyway */
-       XFree(prop);
+       ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
+       free(pr);
 
        return (ret);
 }