updating formatting and bindings. - tmuxwm - A window manager utilising only tmux.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0c9292114ad047eb9cd8596e19bf25b3ca11f25f
 (DIR) parent 54885b726e65161bdd389e6a6bcf67d4fa66d692
 (HTM) Author: Jay Scott <me@jay.scot>
       Date:   Sun,  8 Dec 2019 19:38:16 +0000
       
       updating formatting and bindings.
       
       Diffstat:
         M Makefile                            |       4 ++--
         M tmuxwm.c                            |     169 ++++++++++++++++++-------------
       
       2 files changed, 100 insertions(+), 73 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       @@ -1,4 +1,4 @@
       -CC = gcc 
       +CC = gcc
        CFLAGS = -lX11
        OBJ = tmuxwm.c
        NAME = tmuxwm
       @@ -11,5 +11,5 @@ tmuxwm: ${OBJ}
        clean:
                rm -f ${NAME}
        
       -install: 
       +install:
                cp ${NAME} /usr/bin/
 (DIR) diff --git a/tmuxwm.c b/tmuxwm.c
       @@ -15,36 +15,33 @@
        #include <X11/Xutil.h>
        #include <X11/XF86keysym.h>
        
       -typedef struct Bind { KeySym sym; int mod; const char * const *args; } Bind;
       -typedef const char * const Arg[];
       -
       -/* The next ~30 lines could be separated into a "config.h", but this is BMFM
       - * code: By Me For Me.  So I don't care to separate it for your convenience.
       - */
       -
       -Arg tmux_client = (Arg) { "st", "-e", "tmux", "new-session", NULL };
       -
       -static Bind bind[] = {
       -        { XK_Return,          Mod4Mask, (Arg) { "tmux", "rename-window", NULL } },
       -        { XK_Tab,             Mod4Mask, (Arg) { "tmux", "last-pane", NULL } },
       -        { XK_space,           Mod4Mask, (Arg) { "tmux", "new-window", NULL } },
       -        { XK_1,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "1",  NULL } },
       -        { XK_2,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "2",  NULL } },
       -        { XK_3,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "3",  NULL } },
       -        { XK_4,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "4",  NULL } },
       -        { XK_5,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "5",  NULL } },
       -        { XK_6,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "6",  NULL } },
       -        { XK_7,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "7",  NULL } },
       -        { XK_8,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "8",  NULL } },
       -        { XK_9,               Mod4Mask, (Arg) { "tmux", "select-window", "-t", "9",  NULL } },
       -        { XK_h,               Mod4Mask, (Arg) { "tmux", "split-window", "-h", NULL} },
       -        { XK_v,               Mod4Mask, (Arg) { "tmux", "split-window", "-v", NULL} },
       -        { XK_w,               Mod4Mask,    (Arg) { "firefox", NULL} },
       -        { XK_Menu,                      0, (Arg) { "interrobang", NULL } },
       +typedef struct Bind { KeySym sym; int mod; const char *const *args; }   Bind;
       +typedef const char *const Arg[];
       +
       +Arg tmux_client = (Arg) {"st", "-e", "tmux", "new-session", NULL };
       +
       +static Bind bind[] =
       +{
       +        { XK_Return, Mod4Mask, (Arg) {"tmux",                         "rename-window", NULL } },
       +        { XK_Tab,    Mod4Mask, (Arg) {"tmux",                         "last-pane",          NULL } },
       +        { XK_space,  Mod4Mask, (Arg) {"tmux",                         "new-window",          NULL } },
       +        { XK_1,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "1", NULL} },
       +        { XK_2,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "2", NULL} },
       +        { XK_3,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "3", NULL} },
       +        { XK_4,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "4", NULL} },
       +        { XK_5,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "5", NULL} },
       +        { XK_6,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "6", NULL} },
       +        { XK_7,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "7", NULL} },
       +        { XK_8,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "8", NULL} },
       +        { XK_9,             Mod4Mask, (Arg) {"tmux",                         "select-window", "-t", "9", NULL} },
       +        { XK_h,             Mod4Mask, (Arg) {"tmux",                         "split-window",  "-h", NULL} },
       +        { XK_v,             Mod4Mask, (Arg) {"tmux",                         "split-window",  "-v", NULL} },
       +        { XK_f,             Mod4Mask, (Arg) {"firefox",                 NULL } },
       +        { XK_p,             Mod4Mask, (Arg) {"rofi",                         "-show",          "run", NULL} },
       +        { XK_z,             Mod4Mask, (Arg) {"/home/jay/.bin/battery",         NULL } },
       +        { XK_x,             Mod4Mask, (Arg) {"/home/jay/.bin/all-time", NULL } },
        };
        
       -/* End of potential "config.h" content
       - */
        
        static void buttonpress(XEvent *);
        static void configurerequest(XEvent *);
       @@ -56,122 +53,152 @@ static void unmapnotify(XEvent *);
        static Display *dpy;
        static Window root;
        static int sw, sh;
       -static void (*handler[LASTEvent])(XEvent *) = {
       -        [ButtonPress]        = buttonpress,
       -        [ConfigureRequest]   = configurerequest,
       -        [EnterNotify]        = enternotify,
       -        [KeyPress]           = keypress,
       -        [MapRequest]         = maprequest,
       +static void (*handler[LASTEvent])(XEvent *) =
       +{
       +        [ButtonPress] = buttonpress,
       +        [ConfigureRequest] = configurerequest,
       +        [EnterNotify] = enternotify,
       +        [KeyPress] = keypress,
       +        [MapRequest] = maprequest,
        };
        
       -static int xerror(Display *d, XErrorEvent *e) {
       +static int xerror(Display *d, XErrorEvent *e)
       +{
                char msg[256];
       +
                XGetErrorText(d, e->error_code, msg, sizeof(msg));
       -        fprintf(stderr,"[X11 %d:%d] %s\n", e->request_code, e->error_code, msg);
       +        fprintf(stderr, "[X11 %d:%d] %s\n", e->request_code, e->error_code, msg);
        }
        
       -static inline int spawn(Arg args) {
       -        if (fork() != 0) return 1;
       +static inline int spawn(Arg args)
       +{
       +        if (fork() != 0)
       +                return 1;
                close(ConnectionNumber(dpy));
                setsid();
                execvp(args[0], args);
        }
        
       -int main(int argc, const char **argv) {
       +int main(int argc, const char **argv)
       +{
                signal(SIGCHLD, SIG_IGN);
       -        if(!(dpy = XOpenDisplay(0x0))) return 1;
       +        if (!(dpy = XOpenDisplay(0x0)))
       +                return 1;
                XSetErrorHandler(xerror);
                root = DefaultRootWindow(dpy);
                sw = DisplayWidth(dpy, DefaultScreen(dpy));
                sh = DisplayHeight(dpy, DefaultScreen(dpy));
                XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Tab), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync);
       -        XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Return), Mod4Mask|ShiftMask, root, True, GrabModeAsync, GrabModeAsync);
       +        XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Return), Mod4Mask | ShiftMask, root, True, GrabModeAsync, GrabModeAsync);
                int i;
       -        for (i = 0; i < sizeof(bind)/sizeof(bind[0]); ++i)
       +        for (i = 0; i < sizeof(bind) / sizeof(bind[0]); ++i)
                        XGrabKey(dpy, XKeysymToKeycode(dpy, bind[i].sym), bind[i].mod, root, True, GrabModeAsync, GrabModeAsync);
                XGrabButton(dpy, AnyButton, Mod4Mask, root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
                XSelectInput(dpy, root, SubstructureNotifyMask | SubstructureRedirectMask);
                XEvent ev;
                spawn(tmux_client);
       -        while (!XNextEvent(dpy,&ev))
       -                if (handler[ev.type]) handler[ev.type](&ev);
       +        while (!XNextEvent(dpy, &ev))
       +                if (handler[ev.type])
       +                        handler[ev.type](&ev);
                return 0;
        }
        
       -void buttonpress(XEvent *ev) {
       +void buttonpress(XEvent *ev)
       +{
                XButtonEvent *e = &ev->xbutton;
       +
                XRaiseWindow(dpy, e->subwindow);
                if (e->button == 2)
                        XMoveResizeWindow(dpy, e->subwindow, 0, 0, sw, sh);
       -        if (e->button != 1 && e->button !=3)
       +        if (e->button != 1 && e->button != 3)
                        return;
                XGrabPointer(dpy, root, True, PointerMotionMask | ButtonReleaseMask,
       -                        GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
       +                     GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
                XWindowAttributes attr;
                XGetWindowAttributes(dpy, e->subwindow, &attr);
                int xx = e->x_root, yy = e->y_root, px, py;
                XEvent ee;
                while (!XMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask, &ee)) {
       -                if (ee.type == ButtonRelease) break;
       +                if (ee.type == ButtonRelease)
       +                        break;
                        px = xx; py = yy; xx = ee.xbutton.x_root; yy = ee.xbutton.y_root;
       -                if (e->button == 1) { attr.x += xx - px; attr.y += yy - py; }
       -                else if (e->button == 3) {
       +                if (e->button == 1) {
       +                        attr.x += xx - px; attr.y += yy - py;
       +                } else if (e->button == 3) {
                                attr.width += xx - px; attr.height += yy - py;
       -                        if (attr.width < 12) attr.width = 12;
       -                        else if (attr.height < 12) attr.height = 12;
       +                        if (attr.width < 12)
       +                                attr.width = 12;
       +                        else if (attr.height < 12)
       +                                attr.height = 12;
                        }
                        XMoveResizeWindow(dpy, e->subwindow, attr.x, attr.y, attr.width, attr.height);
                }
                XUngrabPointer(dpy, CurrentTime);
        }
        
       -void configurerequest(XEvent *ev) {
       +void configurerequest(XEvent *ev)
       +{
                XConfigureRequestEvent *e = &ev->xconfigurerequest;
                XWindowChanges wc;
       +
                wc.x = e->x; wc.y = e->y; wc.width = e->width; wc.height = e->height;
                wc.sibling = Above; wc.stack_mode = e->detail; wc.border_width = e->border_width;
                XConfigureWindow(dpy, e->window, e->value_mask, &wc);
        }
        
       -void enternotify(XEvent *ev) {
       +void enternotify(XEvent *ev)
       +{
                XSetInputFocus(dpy, ev->xcrossing.window, RevertToPointerRoot, CurrentTime);
        }
        
       -void keypress(XEvent *ev) {
       +void keypress(XEvent *ev)
       +{
                XKeyEvent *e = &ev->xkey;
                KeySym sym = XkbKeycodeToKeysym(dpy, e->keycode, 0, 0);
                int i;
       -        if (e->state == Mod1Mask && sym == XK_Tab)
       +
       +        if (e->state == Mod1Mask && sym == XK_Tab) {
                        XCirculateSubwindowsUp(dpy, root);
       -        else if (e->state == (Mod4Mask|ShiftMask) && sym == XK_Return)
       +        } else if (e->state == (Mod4Mask | ShiftMask) && sym == XK_Return) {
                        spawn(tmux_client);
       -        else for (i = 0; i < sizeof(bind)/sizeof(bind[0]); ++i)
       -                if (bind[i].sym == sym && bind[i].mod == e->state && bind[i].args)
       +        } else {
       +                for (i = 0; i < sizeof(bind) / sizeof(bind[0]); ++i)
       +                        if (bind[i].sym == sym && bind[i].mod == e->state && bind[i].args)
                                        spawn(bind[i].args);
       +        }
        }
        
       -#define MapFocus                0x01
       -#define MapFull                0x02
       -void maprequest(XEvent *ev) {
       +#define MapFocus    0x01
       +#define MapFull     0x02
       +
       +void maprequest(XEvent *ev)
       +{
                XMapRequestEvent *e = &ev->xmaprequest;
                XWindowAttributes wa;
       +
                if (!XGetWindowAttributes(dpy, e->window, &wa) || wa.override_redirect)
                        return;
                int flags = MapFocus | MapFull;
                XWMHints *wmHint;
       -        if ( (wmHint=XGetWMHints(dpy,e->window)) ) {
       -                if (wmHint->flags & InputHint && !wmHint->input) flags &= ~MapFocus;
       +        if ((wmHint = XGetWMHints(dpy, e->window))) {
       +                if (wmHint->flags & InputHint && !wmHint->input)
       +                        flags &= ~MapFocus;
                        XFree(wmHint);
                }
                XClassHint csHint;
                XGetClassHint(dpy, e->window, &csHint);
       -        if (strncmp(csHint.res_name,"Float",5) == 0) flags &= ~MapFull;
       -        if (strncmp(csHint.res_class,"Float",5) == 0) flags &= ~MapFull;
       +        if (strncmp(csHint.res_name, "Float", 5) == 0)
       +                flags &= ~MapFull;
       +        if (strncmp(csHint.res_class, "Float", 5) == 0)
       +                flags &= ~MapFull;
                Window parent;
       -        if (XGetTransientForHint(dpy, e->window, &parent)) flags &= ~MapFull;
       -        if (flags & MapFocus) XSelectInput(dpy, e->window, EnterWindowMask);
       -        if (flags & MapFull) XMoveResizeWindow(dpy, e->window, 0, 0, sw, sh);
       -        else XMoveResizeWindow(dpy, e->window, wa.x, wa.y, wa.width, wa.height);
       +        if (XGetTransientForHint(dpy, e->window, &parent))
       +                flags &= ~MapFull;
       +        if (flags & MapFocus)
       +                XSelectInput(dpy, e->window, EnterWindowMask);
       +        if (flags & MapFull)
       +                XMoveResizeWindow(dpy, e->window, 0, 0, sw, sh);
       +        else
       +                XMoveResizeWindow(dpy, e->window, wa.x, wa.y, wa.width, wa.height);
                XMapWindow(dpy, e->window);
        }
       -