twattr: util.c refactoring - wmutils - X windows manipulation utilities
 (HTM) git clone git://z3bra.org/wmutils
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit c11fa4d11c57d9099a09fb7c7aac6276ae11d60d
 (DIR) parent c76d1f6fa5c8fd13eab4b6c7a909426b3c52e446
 (HTM) Author: z3bra <willy@mailoo.org>
       Date:   Tue,  9 Dec 2014 12:47:14 +0100
       
       wattr: util.c refactoring
       
       Diffstat:
         M wattr.c                             |      86 +++----------------------------
       
       1 file changed, 8 insertions(+), 78 deletions(-)
       ---
 (DIR) diff --git a/wattr.c b/wattr.c
       t@@ -4,15 +4,13 @@
        #include <err.h>
        #include <xcb/xcb.h>
        
       +#include "util.h"
       +
        char *argv0;
        static xcb_connection_t *conn;
        
        static void usage(void);
       -static void xcbinit(void);
       -static void cleanup(void);
        static int getattribute(xcb_window_t, int);
       -static int mapped(xcb_window_t);
       -static int exists(xcb_window_t);
        
        enum {
                ATTR_W = 1 << 0,
       t@@ -32,75 +30,6 @@ usage(void)
                exit(1);
        }
        
       -static void
       -xcbinit(void)
       -{
       -        conn = xcb_connect(NULL, NULL);
       -        if (xcb_connection_has_error(conn))
       -                errx(1, "unable to connect to the X server");
       -}
       -
       -static void
       -cleanup(void)
       -{
       -        if (conn != NULL)
       -                xcb_disconnect(conn);
       -}
       -
       -static int
       -exists(xcb_window_t w)
       -{
       -        xcb_get_window_attributes_cookie_t c;
       -        xcb_get_window_attributes_reply_t  *r;
       -
       -        c = xcb_get_window_attributes(conn, w);
       -        r = xcb_get_window_attributes_reply(conn, c, NULL);
       -
       -        if (r == NULL)
       -                return 0;
       -
       -        free(r);
       -        return 1;
       -}
       -
       -static int
       -mapped(xcb_window_t w)
       -{
       -        int ms;
       -        xcb_get_window_attributes_cookie_t c;
       -        xcb_get_window_attributes_reply_t  *r;
       -
       -        c = xcb_get_window_attributes(conn, w);
       -        r = xcb_get_window_attributes_reply(conn, c, NULL);
       -
       -        if (r == NULL)
       -                return 0;
       -
       -        ms = r->map_state;
       -
       -        free(r);
       -        return ms == XCB_MAP_STATE_VIEWABLE;
       -}
       -
       -static int
       -ignored(xcb_window_t w)
       -{
       -        int or;
       -        xcb_get_window_attributes_cookie_t c;
       -        xcb_get_window_attributes_reply_t  *r;
       -
       -        c = xcb_get_window_attributes(conn, w);
       -        r = xcb_get_window_attributes_reply(conn, c, NULL);
       -
       -        if (r == NULL)
       -                return 0;
       -
       -        or = r->override_redirect;
       -
       -        free(r);
       -        return or;
       -}
       -
        static int
        getattribute(xcb_window_t w, int attr)
        {
       t@@ -136,8 +65,7 @@ main(int argc, char **argv)
                        usage();
                }
        
       -        atexit(cleanup);
       -        xcbinit();
       +        init_xcb(&conn);
        
                for (c=2; argv[c]; c++) {
                        w = strtoul(argv[c], NULL, 16);
       t@@ -156,14 +84,16 @@ main(int argc, char **argv)
                                                  break;
                                        case 'w': printf("%d", getattribute(w, ATTR_W));
                                                  break;
       -                                case 'o': ignored(w)? exit(0) : exit(1);
       -                                case 'm': mapped(w) ? exit(0) : exit(1);
       -                                default : exists(w) ? exit(0) : exit(1);
       +                                case 'o': ignore(conn, w) ? exit(0) : exit(1);
       +                                case 'm': mapped(conn, w) ? exit(0) : exit(1);
       +                                default : exists(conn, w) ? exit(0) : exit(1);
                                }
                                /* add a space if more attribute come after */
                                putc(i+1 < strlen(argv[1]) ? ' ' : '\n',stdout);
                        }
                }
        
       +        kill_xcb(&conn);
       +
                return 0;
        }