JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
convert window_get_pid and partially custom_region to xcb
[spectrwm.git] / spectrwm.c
index e70be92..c912e3f 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;
 };
 
@@ -650,46 +650,58 @@ get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
 void
 update_iconic(struct ws_win *win, int newv)
 {
-       int32_t v = newv;
-       Atom iprop;
+       int32_t                         v = newv;
+       xcb_atom_t                      iprop;
+       xcb_intern_atom_cookie_t        c;
+       xcb_intern_atom_reply_t         *r;
 
        win->iconic = newv;
 
-       iprop = XInternAtom(display, "_SWM_ICONIC", False);
-       if (!iprop)
+       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
                return;
+               
        if (newv)
-               XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
-                   PropModeReplace, (unsigned char *)&v, 1);
+               xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
+                       iprop, XCB_ATOM_INTEGER, 32, 1, &v);
        else
-               XDeleteProperty(display, win->id, iprop);
+               xcb_delete_property(conn, win->id, iprop);
 }
 
 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);
 }
 
@@ -1265,13 +1277,16 @@ name_to_color(char *colorname)
 void
 setscreencolor(char *val, int i, int c)
 {
-       if (i > 0 && i <= ScreenCount(display)) {
+       int     num_screens;
+
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
+       if (i > 0 && i <= num_screens) {
                screens[i - 1].c[c].color = name_to_color(val);
                free(screens[i - 1].c[c].name);
                if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
                        err(1, "strdup");
        } else if (i == -1) {
-               for (i = 0; i < ScreenCount(display); i++) {
+               for (i = 0; i < num_screens; i++) {
                        screens[i].c[c].color = name_to_color(val);
                        free(screens[i].c[c].name);
                        if ((screens[i].c[c].name = strdup(val)) == NULL)
@@ -1279,7 +1294,7 @@ setscreencolor(char *val, int i, int c)
                }
        } else
                errx(1, "invalid screen index: %d out of bounds (maximum %d)",
-                   i, ScreenCount(display));
+                   i, num_screens);
 }
 
 void
@@ -1312,13 +1327,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)
@@ -1919,8 +1936,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;
 }
@@ -6155,45 +6172,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);
 }
@@ -7305,12 +7328,13 @@ grab_windows(void)
 void
 setup_screens(void)
 {
-       int                     i, j, k;
+       int                     i, j, k, num_screens;
        int                     errorbase, major, minor;
        struct workspace        *ws;
        XGCValues               gcv;
 
-       if ((screens = calloc(ScreenCount(display),
+       num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
+       if ((screens = calloc(num_screens,
             sizeof(struct swm_screen))) == NULL)
                err(1, "setup_screens: calloc: failed to allocate memory for "
                    "screens");
@@ -7323,7 +7347,7 @@ setup_screens(void)
                        xrandr_support = 0;
 
        /* map physical screens */
-       for (i = 0; i < ScreenCount(display); i++) {
+       for (i = 0; i < num_screens; i++) {
                DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
                screens[i].idx = i;
                TAILQ_INIT(&screens[i].rl);