tnew tool: pdw - wmutils - X windows manipulation utilities
 (HTM) git clone git://z3bra.org/wmutils
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit d989db82b83cf457a3fb9bcd87637cf29770f9a4
 (DIR) parent e5f30f4bd0c10ee3a43055a9e6c5814a555982ed
 (HTM) Author: Willy Goiffon <contact@z3bra.org>
       Date:   Tue,  5 Oct 2021 16:53:39 +0200
       
       new tool: pdw
       
       Diffstat:
         M Makefile                            |       1 +
         M README.md                           |       1 +
         M man/wmutils.1                       |       3 +++
         A pdw.c                               |      81 ++++++++++++++++++++++++++++++
       
       4 files changed, 86 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -3,6 +3,7 @@ include config.mk
        HDR = arg.h util.h
        SRC =           \
                pfw.c   \
       +        pdw.c   \
                lsw.c   \
                mapw.c  \
                killw.c \
 (DIR) diff --git a/README.md b/README.md
       t@@ -38,6 +38,7 @@ without being added to this list, so take it with a grain of salt.
        * lsw   - list windows
        * mapw  - map/unmap windows
        * pfw   - print focused window
       +* pdw   - print decoration window
        * slw   - select window interactively
        * wattr - show window's attributes
        * wmp   - move the mouse pointer
 (DIR) diff --git a/man/wmutils.1 b/man/wmutils.1
       t@@ -23,6 +23,8 @@ list windows
        map/unmap windows
        .It pfw
        print focused window
       +.It pdw
       +print decoration window
        .It wattr
        show window's attributes
        .It wmp
       t@@ -51,6 +53,7 @@ variable.
        .Xr lsw 1 ,
        .Xr mapw 1 ,
        .Xr pfw 1 ,
       +.Xr pdw 1 ,
        .Xr wattr 1 ,
        .Xr wmp 1 ,
        .Xr wmv 1 ,
 (DIR) diff --git a/pdw.c b/pdw.c
       t@@ -0,0 +1,81 @@
       +/* See LICENSE file for copyright and license details. */
       +
       +#include <err.h>
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +#include <xcb/xcb.h>
       +#include <xcb/xcb_aux.h>
       +
       +#include "util.h"
       +
       +static xcb_connection_t *conn;
       +static xcb_screen_t *scrn;
       +
       +static void usage(char *);
       +static xcb_window_t focus_window(void);
       +static xcb_window_t parent(xcb_window_t);
       +
       +static void
       +usage(char *name)
       +{
       +        fprintf(stderr, "usage: %s [wid]\n", name);
       +        exit(1);
       +}
       +
       +static xcb_window_t
       +focus_window(void)
       +{
       +        xcb_window_t w = 0;
       +        xcb_get_input_focus_cookie_t c;
       +        xcb_get_input_focus_reply_t *r;
       +
       +        c = xcb_get_input_focus(conn);
       +        r = xcb_get_input_focus_reply(conn, c, NULL);
       +        if (r == NULL)
       +                errx(1, "xcb_get_input_focus");
       +
       +        w = r->focus;
       +        free(r);
       +
       +        if (w == XCB_NONE || w == XCB_INPUT_FOCUS_POINTER_ROOT)
       +                errx(1, "focus not set");
       +
       +        return w;
       +}
       +
       +static xcb_window_t
       +parent(xcb_window_t w)
       +{
       +        xcb_query_tree_cookie_t c;
       +        xcb_query_tree_reply_t *r;
       +
       +        c = xcb_query_tree(conn, w);
       +        r = xcb_query_tree_reply(conn, c, NULL);
       +        if (r == NULL)
       +                errx(1, "failed to get parent");
       +
       +        w = (!r->parent || r->parent == scrn->root) ? w : parent(r->parent);
       +        free(r);
       +
       +        return w;
       +}
       +
       +int
       +main(int argc, char **argv)
       +{
       +        xcb_window_t w;
       +
       +        if (argc > 2 || (argc > 1 && (!strncmp(argv[1], "-h", 2))))
       +                usage(argv[0]);
       +
       +        init_xcb(&conn);
       +        get_screen(conn, &scrn);
       +
       +        w = (argc > 1) ? strtoul(argv[1], NULL, 16) : focus_window();
       +
       +        printf("0x%08x\n", parent(w));
       +
       +        kill_xcb(&conn);
       +        return 0;
       +}