tnew tool: wattr - wmutils - X windows manipulation utilities
 (HTM) git clone git://z3bra.org/wmutils
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 9645cf41239653d177d74bc31fedc51b070b80ce
 (DIR) parent d2bcf543e73fd93dea485f9ed93355a2ee7c3395
 (HTM) Author: z3bra <willy@mailoo.org>
       Date:   Thu, 27 Nov 2014 23:05:25 +0100
       
       new tool: wattr
       
       Diffstat:
         A wattr.c                             |      95 ++++++++++++++++++++++++++++++
       
       1 file changed, 95 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/wattr.c b/wattr.c
       t@@ -0,0 +1,95 @@
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +#include <err.h>
       +#include <xcb/xcb.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);
       +
       +enum {
       +        ATTR_W = 1 << 0,
       +        ATTR_H = 1 << 1,
       +        ATTR_X = 1 << 2,
       +        ATTR_Y = 1 << 3,
       +        ATTR_MAX
       +};
       +
       +static void
       +usage(void)
       +{
       +        fprintf(stderr, "usage: %s [-h] [whxy] <wid>\n", argv0);
       +        exit(1);
       +}
       +
       +static void
       +xcbinit(void)
       +{
       +        conn = xcb_connect(NULL, NULL);
       +        if (xcb_connection_has_error(conn))
       +                errx(1, "xcb_connect");
       +}
       +
       +static void
       +cleanup(void)
       +{
       +        if (conn != NULL)
       +                xcb_disconnect(conn);
       +}
       +
       +static int
       +getattribute(xcb_window_t w, int attr)
       +{
       +        xcb_get_geometry_cookie_t c;
       +        xcb_get_geometry_reply_t *r;
       +
       +        c = xcb_get_geometry(conn, w);
       +        r = xcb_get_geometry_reply(conn, c, NULL);
       +
       +        if (r == NULL)
       +                errx(1, "xcb_get_geometry");
       +
       +        switch (attr) {
       +                case ATTR_W: attr = r->width  + 2 * r->border_width; break;
       +                case ATTR_H: attr = r->height + 2 * r->border_width; break;
       +                case ATTR_X: attr = r->x - r->border_width; break;
       +                case ATTR_Y: attr = r->y - r->border_width; break;
       +        }
       +
       +        free(r);
       +        return attr;
       +}
       +
       +int
       +main(int argc, char **argv)
       +{
       +        int i;
       +        xcb_window_t w = 0;
       +
       +        if (argc < 2 || (strncmp(argv[1], "-h", 2) == 0))
       +                usage();
       +
       +        atexit(cleanup);
       +        xcbinit();
       +
       +        w = strtoul(argv[2], NULL, 16);
       +
       +        for (i=0; i<strnlen(argv[1], ATTR_MAX); i++) {
       +                switch (argv[1][i]) {
       +                        case 'w': printf("%d", getattribute(w, ATTR_W)); break;
       +                        case 'h': printf("%d", getattribute(w, ATTR_H)); break;
       +                        case 'x': printf("%d", getattribute(w, ATTR_X)); break;
       +                        case 'y': printf("%d", getattribute(w, ATTR_Y)); break;
       +                }
       +
       +                /* add a space if more attribute come after */
       +                putc(i+1 < strnlen(argv[1], ATTR_MAX) ? ' ' : '\n',stdout);
       +        }
       +
       +        return 0;
       +}