tnew tool: atomx - wmutils - X windows manipulation utilities
 (HTM) git clone git://z3bra.org/wmutils
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 6131b2b8fe4244fe8b26364c7a293ff1590c62ff
 (DIR) parent 354acb7922e8d630bb56240934a1071e21031ab8
 (HTM) Author: Willy Goiffon <contact@z3bra.org>
       Date:   Thu, 21 Nov 2019 17:23:35 +0100
       
       new tool: atomx
       
       Diffstat:
         M Makefile                            |       3 ++-
         M README.md                           |       1 +
         A atomx.c                             |     137 +++++++++++++++++++++++++++++++
         M man/Makefile                        |       1 +
         A man/atomx.1                         |      36 +++++++++++++++++++++++++++++++
       
       5 files changed, 177 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -15,7 +15,8 @@ SRC =           \
                chwb.c  \
                ignw.c  \
                wmp.c   \
       -        slw.c
       +        slw.c   \
       +        atomx.c
        
        OBJ = $(SRC:.c=.o)
        BIN = $(SRC:.c=)
 (DIR) diff --git a/README.md b/README.md
       t@@ -45,6 +45,7 @@ without being added to this list, so take it with a grain of salt.
        * wrs   - resize a window
        * wtf   - focus a window
        * wtp   - teleport a window
       +* atomx - modify atoms on a window
        
        All these tools come with a manpage ! read them for further informations.
        
 (DIR) diff --git a/atomx.c b/atomx.c
       t@@ -0,0 +1,137 @@
       +/* 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"
       +#include "arg.h"
       +
       +#define MAXLEN 512
       +
       +static xcb_connection_t *conn;
       +
       +void
       +usage(char *name)
       +{
       +        fprintf(stderr, "%s [-ds] wid atom [value]\n", name);
       +}
       +
       +xcb_atom_t
       +add_atom(xcb_atom_t type, char *name, size_t len)
       +{
       +        xcb_atom_t atom;
       +        xcb_intern_atom_cookie_t c;
       +        xcb_intern_atom_reply_t *r;
       +
       +        c = xcb_intern_atom(conn, 0, len, name);
       +        r = xcb_intern_atom_reply(conn, c, NULL);
       +        if (!r)
       +                return 0;
       +
       +        atom = r->atom;
       +        free(r);
       +
       +        return atom;
       +}
       +
       +int
       +set_atom(xcb_window_t wid, xcb_atom_t atom, xcb_atom_t type, size_t len, void *data)
       +{
       +        int errcode;
       +        xcb_void_cookie_t c;
       +        xcb_generic_error_t *e;
       +
       +        c = xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE,
       +                wid, atom, type, 8, len, data);
       +        e = xcb_request_check(conn, c);
       +        if (!e)
       +                return 0;
       +
       +        errcode = e->error_code;
       +        free(e);
       +
       +        return errcode;
       +}
       +
       +int
       +get_atom(xcb_window_t wid, xcb_atom_t atom, xcb_atom_t type, char *data, size_t len)
       +{
       +        size_t n;
       +        xcb_get_property_cookie_t c;
       +        xcb_get_property_reply_t *r;
       +
       +        c = xcb_get_property(conn, 0, wid, atom, type, 0, MAXLEN);
       +        r = xcb_get_property_reply(conn, c, NULL);
       +        if (!r)
       +                return -1;
       +
       +        if (!(n = xcb_get_property_value_length(r))) {
       +                free(r);
       +                return -1;
       +        }
       +
       +        strncpy(data, xcb_get_property_value(r), n);
       +        data[n] = 0;
       +
       +        free(r);
       +
       +        return 0;
       +}
       +
       +
       +int
       +main(int argc, char **argv)
       +{
       +        int dflag = 0, sflag = 0;
       +        char *key, *val, *argv0;
       +        char data[MAXLEN];
       +        xcb_window_t wid;
       +        xcb_atom_t atom;
       +
       +        ARGBEGIN {
       +        case 'd':
       +                dflag = 1;
       +                break;
       +        case 's':
       +                sflag = 1;
       +                break;
       +        default:
       +                usage(argv0);
       +                return -1;
       +        } ARGEND;
       +
       +        if (argc < 2 + sflag)
       +                return -1;
       +
       +        wid = strtoul(argv[0], NULL, 16);
       +        key = argv[1];
       +        if (sflag)
       +                val = argv[2];
       +
       +        init_xcb(&conn);
       +
       +        /* retrieve atom ID from server */
       +        atom = add_atom(XCB_ATOM_STRING, key, strlen(key));
       +        if (!atom)
       +                return -1;
       +
       +        /* remove property from window */
       +        if (dflag)
       +                xcb_delete_property(conn, wid, atom);
       +
       +        /* set property on window (must be a string) */
       +        if (sflag)
       +                set_atom(wid, atom, XCB_ATOM_STRING, strlen(val), val);
       +
       +        /* retrieve and print atom value to stdout */
       +        if (!get_atom(wid, atom, XCB_ATOM_STRING, data, MAXLEN))
       +                printf("%s\n", data);
       +
       +        kill_xcb(&conn);
       +
       +        return 0;
       +}
 (DIR) diff --git a/man/Makefile b/man/Makefile
       t@@ -15,6 +15,7 @@ MAN =           \
                ignw.1  \
                wmp.1   \
                slw.1   \
       +        atomx.1 \
                wmutils.1
        
        .POSIX:
 (DIR) diff --git a/man/atomx.1 b/man/atomx.1
       t@@ -0,0 +1,36 @@
       +.Dd November 21, 2019
       +.Dt ATOMX 1
       +.Os wmutils
       +.Sh NAME
       +.Nm atomx
       +.Nd manage X atoms on a window
       +.Sh SYNOPSIS
       +.Nm atomx
       +.Op Fl sd
       +.Ar wid atom
       +.Op Ar value
       +.Sh DESCRIPTION
       +.Nm
       +will print, add, change and remove X atoms on the window with ID
       +.Ar wid .
       +By default
       +.Nm
       +will print the value of the atom
       +.Ar atom
       +if it exists.
       +.Bl -tag -width Ds
       +.It Fl d
       +Delete atom
       +.Ar atom
       +from the window.
       +.It Fl s
       +Set atom
       +.Ar atom
       +value to
       +.Ar value .
       +Any existing atom with the same name will be overwritten.
       +.Sh ENVIRONMENT
       +.Nm
       +acts on the X display specified by the
       +.Ev DISPLAY
       +variable.