tInitial commit - randr - print current monitor size
 (HTM) git clone git://git.z3bra.org/randr.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 728f2629f27d94e75c49306e3f321745cfb34884
 (HTM) Author: Willy Goiffon <wgoiffon@LPWGF01.centro.fr>
       Date:   Sat, 28 Dec 2019 22:29:32 +0100
       
       Initial commit
       
       Diffstat:
         A LICENSE                             |      13 +++++++++++++
         A arg.h                               |      65 +++++++++++++++++++++++++++++++
         A config.mk                           |      12 ++++++++++++
         A makefile                            |      15 +++++++++++++++
         A randr.c                             |     156 +++++++++++++++++++++++++++++++
       
       5 files changed, 261 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/LICENSE b/LICENSE
       t@@ -0,0 +1,13 @@
       +Copyright (c) 2019 z3bra <dev@z3bra.org>
       +
       +Permission to use, copy, modify, and distribute this software for any
       +purpose with or without fee is hereby granted, provided that the above
       +copyright notice and this permission notice appear in all copies.
       +
       +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
       +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
       +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
       +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
       +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
       +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
       +PERFORMANCE OF THIS SOFTWARE.
 (DIR) diff --git a/arg.h b/arg.h
       t@@ -0,0 +1,65 @@
       +/*
       + * 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()        (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
       +
       +#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])))
       +
       +#define LNGARG()        &argv[0][0]
       +
       +#endif
 (DIR) diff --git a/config.mk b/config.mk
       t@@ -0,0 +1,12 @@
       +VERSION = 0.0
       +
       +CC = cc
       +LD = $(CC)
       +
       +PREFIX = /usr/local
       +MANDIR = ${PREFIX}/man
       +
       +CPPFLAGS = -DVERSION=\"${VERSION}\"
       +CFLAGS = $(CPPFLAGS) -Wall -Wextra -pedantic -g
       +LDFLAGS =
       +LDLIBS = -lxcb -lxcb-randr
 (DIR) diff --git a/makefile b/makefile
       t@@ -0,0 +1,15 @@
       +include config.mk
       +
       +randr: randr.o
       +randr.o: randr.c
       +
       +clean:
       +        rm -f randr *.o
       +
       +install: randr
       +        mkdir -p $(DESTDIR)$(PREFIX)/bin
       +        cp randr $(DESTDIR)$(PREFIX)/bin/randr
       +        chmod 755 $(DESTDIR)$(PREFIX)/bin/randr
       +
       +uninstall:
       +        rm $(DESTDIR)$(PREFIX)/bin/randr
 (DIR) diff --git a/randr.c b/randr.c
       t@@ -0,0 +1,156 @@
       +/* 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_cursor.h>
       +#include <xcb/randr.h>
       +
       +#include "arg.h"
       +
       +struct position {
       +        int x, y;
       +};
       +
       +enum {
       +        MONITOR_CURSOR = 1,
       +        MONITOR_WINDOW = 1<<2,
       +};
       +
       +static xcb_connection_t *dpy;
       +static xcb_screen_t *scr;
       +static struct position pos = { 0, 0 };
       +
       +char *fmt = "%.*s\t%d\t%d\t%d\t%d\t%d\n";
       +char *argv0;
       +
       +int
       +cursor_position(xcb_window_t wid)
       +{
       +        xcb_query_pointer_cookie_t c;
       +        xcb_query_pointer_reply_t *r;
       +
       +        c =  xcb_query_pointer(dpy, wid);
       +        r = xcb_query_pointer_reply(dpy, c, NULL);
       +        if (!r)
       +                return -1;
       +
       +        pos.x = r->root_x;
       +        pos.y = r->root_y;
       +        free(r);
       +
       +        return 0;
       +}
       +
       +int
       +window_position(xcb_window_t wid)
       +{
       +        xcb_get_geometry_reply_t *r;
       +
       +        r = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, wid), NULL);
       +        if (!r)
       +                return -1;
       +
       +        pos.x = r->x;
       +        pos.y = r->y;
       +        free(r);
       +
       +        return 0;
       +}
       +
       +int
       +listmonitors(xcb_window_t wid)
       +{
       +        int i, n;
       +        xcb_randr_output_t *monitors;
       +        xcb_randr_get_screen_resources_reply_t *r;
       +
       +        r = xcb_randr_get_screen_resources_reply(dpy,
       +                xcb_randr_get_screen_resources(dpy, wid), NULL);
       +        if (!r)
       +                return -1;
       +
       +        n = xcb_randr_get_screen_resources_outputs_length(r);
       +        monitors = xcb_randr_get_screen_resources_outputs(r);
       +
       +        for (i = 0; i < n; i++) {
       +                xcb_randr_get_output_info_reply_t *info;
       +                info = xcb_randr_get_output_info_reply(dpy,
       +                        xcb_randr_get_output_info(dpy, monitors[i], XCB_CURRENT_TIME),
       +                        NULL);
       +                if (!info)
       +                        continue;
       +
       +                xcb_randr_get_crtc_info_reply_t *crtc;
       +                crtc = xcb_randr_get_crtc_info_reply(dpy,
       +                        xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME),
       +                        NULL);
       +                if (crtc && (pos.x >= crtc->x && pos.x <= crtc->x + crtc->width)) {
       +                        printf(fmt,
       +                                xcb_randr_get_output_info_name_length(info),
       +                                xcb_randr_get_output_info_name(info),
       +                                crtc->width, crtc->height, crtc->x, crtc->y, crtc->status);
       +                }
       +
       +                free(crtc);
       +                free(info);
       +        }
       +
       +        return 0;
       +}
       +
       +void
       +usage()
       +{
       +        fprintf(stderr, "usage: %s [-w wid]\n", argv0);
       +}
       +
       +int
       +main(int argc, char *argv[])
       +{
       +        int aflag = 0, wflag = 0;
       +        xcb_window_t wid;
       +
       +        ARGBEGIN {
       +        /* print all monitors (not used yet) */
       +        case 'a':
       +                aflag = 1;
       +                break;
       +        /* change output format */
       +        case 'f':
       +                fmt = EARGF(usage());
       +                break;
       +        /* return monitor for focused window rather than cursor */
       +        case 'w':
       +                wflag = 1;
       +                wid = strtoul(EARGF(usage()), NULL, 16);
       +                break;
       +        case 'h':
       +                usage();
       +                return 0;
       +        } ARGEND;
       +
       +        dpy = xcb_connect(NULL, NULL);
       +        if (xcb_connection_has_error(dpy))
       +                errx(1, "unable connect to the X server");
       +
       +        scr = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
       +        if (!scr)
       +                return -1;
       +                
       +        else if (wflag)
       +                window_position(wid);
       +        else
       +                cursor_position(scr->root);
       +
       +        scr = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
       +        if (!scr)
       +                errx(1, "unable to retrieve screen informations");
       +
       +        listmonitors(scr->root);
       +
       +        xcb_disconnect(dpy);
       +        return 0;
       +}