tnew tool: pfw - wmutils - X windows manipulation utilities
 (HTM) git clone git://z3bra.org/wmutils
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a7b76073340d7a8641916bc3a36e8df2cb27320c
 (DIR) parent cc0672a62f42b17e8ef050c87bb43c00cb703696
 (HTM) Author: z3bra <willy@mailoo.org>
       Date:   Thu, 27 Nov 2014 23:02:41 +0100
       
       new tool: pfw
       
       Diffstat:
         A Makefile                            |      45 +++++++++++++++++++++++++++++++
         A arg.h                               |      63 +++++++++++++++++++++++++++++++
         A pfw.c                               |      54 +++++++++++++++++++++++++++++++
       
       3 files changed, 162 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -0,0 +1,45 @@
       +PREFIX:=/usr
       +MANPREFIX:=$(PREFIX)/share/man
       +
       +CC = cc
       +LD = $(CC)
       +CFLAGS  += -std=c99 -pedantic -Wall -Os
       +LDFLAGS += -lxcb
       +
       +HDR = arg.h
       +SRC =           \
       +        pfw.c   \
       +        lsw.c   \
       +        mapw.c  \
       +        killw.c \
       +        wattr.c
       +
       +OBJ = $(SRC:.c=.o)
       +BIN = $(SRC:.c=)
       +
       +.POSIX:
       +
       +all: $(BIN)
       +
       +$(OBJ): $(HDR)
       +
       +.o:
       +        @echo "LD $@"
       +        @$(LD) $< -o $@ $(LDFLAGS)
       +
       +.c.o:
       +        @echo "CC $<"
       +        @$(CC) -c $< -o $@ $(LDFLAGS)
       +
       +install: $(BIN)
       +        mkdir -p $(DESTDIR)$(PREFIX)/bin/
       +        cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin/
       +
       +uninstall:
       +        @echo "uninstalling $(BIN)"
       +        for util in $(BIN); do \
       +                rm -f $(DESTDIR)$(PREFIX)/bin/$$util; \
       +        done
       +
       +clean :
       +        rm -f $(OBJ) $(BIN)
 (DIR) diff --git a/arg.h b/arg.h
       t@@ -0,0 +1,63 @@
       +/*
       + * Copy me if you can.
       + * by 20h
       + */
       +
       +#ifndef ARG_H__
       +#define ARG_H__
       +
       +extern char *argv0;
       +
       +/* use main(int argc, char *argv[]) */
       +#define ARGBEGIN        for (argv0 = *argv, argv++, argc--;\
       +                                        argv[0] && argv[0][1]\
       +                                        && argv[0][0] == '-';\
       +                                        argc--, argv++) {\
       +                                char argc_;\
       +                                char **argv_;\
       +                                int brk_;\
       +                                if (argv[0][1] == '-' && argv[0][2] == '\0') {\
       +                                        argv++;\
       +                                        argc--;\
       +                                        break;\
       +                                }\
       +                                for (brk_ = 0, argv[0]++, argv_ = argv;\
       +                                                argv[0][0] && !brk_;\
       +                                                argv[0]++) {\
       +                                        if (argv_ != argv)\
       +                                                break;\
       +                                        argc_ = argv[0][0];\
       +                                        switch (argc_)
       +
       +/* Handles obsolete -NUM syntax */
       +#define ARGNUM                                case '0':\
       +                                        case '1':\
       +                                        case '2':\
       +                                        case '3':\
       +                                        case '4':\
       +                                        case '5':\
       +                                        case '6':\
       +                                        case '7':\
       +                                        case '8':\
       +                                        case '9'
       +
       +#define ARGEND                        }\
       +                        }
       +
       +#define ARGC()                argc_
       +
       +#define ARGNUMF(base)        (brk_ = 1, estrtol(argv[0], (base)))
       +
       +#define EARGF(x)        ((argv[0][1] == '\0' && argv[1] == NULL)?\
       +                                ((x), abort(), (char *)0) :\
       +                                (brk_ = 1, (argv[0][1] != '\0')?\
       +                                        (&argv[0][1]) :\
       +                                        (argc--, argv++, argv[0])))
       +
       +#define ARGF()                ((argv[0][1] == '\0' && argv[1] == NULL)?\
       +                                (char *)0 :\
       +                                (brk_ = 1, (argv[0][1] != '\0')?\
       +                                        (&argv[0][1]) :\
       +                                        (argc--, argv++, argv[0])))
       +
       +#endif
 (DIR) diff --git a/pfw.c b/pfw.c
       t@@ -0,0 +1,54 @@
       +/* See LICENSE file for copyright and license details. */
       +
       +#include <err.h>
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <xcb/xcb.h>
       +
       +static xcb_connection_t *conn;
       +
       +static void xcbinit(void);
       +static void cleanup(void);
       +static xcb_window_t focuswindow(void);
       +
       +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 xcb_window_t
       +focuswindow(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);
       +        return w;
       +}
       +
       +int
       +main(int argc, char **argv)
       +{
       +        atexit(cleanup);
       +        xcbinit();
       +
       +        printf("0x%08x\n", focuswindow());
       +        return 0;
       +}