JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
topbar: hide layout indicator
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index dc9f318..3d42441 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -32,6 +32,7 @@
 #include <sys/wait.h>
 #include <X11/cursorfont.h>
 #include <X11/keysym.h>
+#include <X11/XF86keysym.h>
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
 #include <X11/Xproto.h>
@@ -64,7 +65,7 @@ enum { NetSupported, NetWMName, NetWMState,
        NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
-       ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
+       ClkClientWin, ClkRootWin, ClkAnywhere, ClkLast }; /* clicks */
 
 typedef union {
        int i;
@@ -177,7 +178,6 @@ static long getstate(Window w);
 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, Bool focused);
 static void grabkeys(void);
-static void grab_typing_keys(void);
 static void incnmaster(const Arg *arg);
 static void keypress(XEvent *e);
 static void killclient(const Arg *arg);
@@ -201,7 +201,6 @@ static void restack(Monitor *m);
 static void run(void);
 static void scan(void);
 static Bool sendevent(Client *c, Atom proto);
-static void send_keycode(KeyCode key);
 static void sendmon(Client *c, Monitor *m);
 static void setclientstate(Client *c, long state);
 static void setfocus(Client *c);
@@ -211,6 +210,7 @@ static void setmfact(const Arg *arg);
 static void setup(void);
 static void showhide(Client *c);
 static void sigchld(int unused);
+static void kbspawn(const Arg *arg);
 static void spawn(const Arg *arg);
 static void tag(const Arg *arg);
 static void tagmon(const Arg *arg);
@@ -242,8 +242,6 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
 
 /* variables */
-static KeyCode key_buffer[100];
-static int key_buffer_len = 0;
 static Bool key_buffering = False;
 static const char broken[] = "broken";
 static char stext[256];
@@ -363,6 +361,9 @@ applyrules(Client *c) {
                && (!r->instance || strstr(instance, r->instance)))
                {
                        c->isfloating = r->isfloating;
+                       if(r->isfloating) {
+                               c->x = -1; c->y = -2; // secret code for centered
+                       }
                        c->tags |= r->tags;
                        c->screen_hog = r->screen_hog;
                        for(m = mons; m && m->num != r->monitor; m = m->next);
@@ -487,11 +488,23 @@ attachstack(Client *c) {
 
 void
 buttonpress(XEvent *e) {
-       unsigned int i, x, click;
+       unsigned int i, click;
        Arg arg = {0};
        Client *c;
        Monitor *m;
        XButtonPressedEvent *ev = &e->xbutton;
+       Bool called = False;
+
+       for(i = 0; i < LENGTH(buttons); i++) {
+               if(buttons[i].click == ClkAnywhere && buttons[i].button == ev->button
+               && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
+                       buttons[i].func(&buttons[i].arg);
+                       called = True;
+               }
+       }
+       if (called) {
+               return;
+       }
 
        click = ClkRootWin;
        /* focus monitor if necessary */
@@ -501,29 +514,21 @@ buttonpress(XEvent *e) {
                focus(NULL);
        }
        if(ev->window == selmon->barwin) {
-               i = x = 0;
-               do
-                       x += TEXTW(tags[i]);
-               while(ev->x >= x && ++i < LENGTH(tags));
-               if(i < LENGTH(tags)) {
-                       click = ClkTagBar;
-                       arg.ui = 1 << i;
-               }
-               else if(ev->x < x + blw)
-                       click = ClkLtSymbol;
-               else if(ev->x > selmon->ww - TEXTW(stext))
-                       click = ClkStatusText;
-               else
-                       click = ClkWinTitle;
-       }
-       else if((c = wintoclient(ev->window))) {
+               return;
+       } else if((c = wintoclient(ev->window))) {
                focus(c);
                click = ClkClientWin;
        }
-       for(i = 0; i < LENGTH(buttons); i++)
+       for(i = 0; i < LENGTH(buttons); i++) {
                if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
-               && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
-                       buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
+               && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
+                       if (click == ClkTagBar && buttons[i].arg.i == 0) {
+                               buttons[i].func(&arg);
+                       } else {
+                               buttons[i].func(&buttons[i].arg);
+                       }
+               }
+       }
 }
 
 void
@@ -757,10 +762,11 @@ detachstack(Client *c) {
                for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
                        next_sel = i;
                // failing that, find first visible window (besides c)
-               if (!next_sel)
+               if (!next_sel) {
                        for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
                        if (i != c)
                                next_sel = i;
+               }
                c->mon->sel = next_sel;
        }
        if (prev) {
@@ -805,10 +811,6 @@ drawbar(Monitor *m) {
                           occ & 1 << i, urg & 1 << i);
                x += w;
        }
-       w = blw = TEXTW(m->ltsymbol);
-       drw_setscheme(drw, &scheme[SchemeNorm]);
-       drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
-       x += w;
        xx = x;
        if(m == selmon) { /* status is only drawn on selected monitor */
                w = TEXTW(stext);
@@ -1034,22 +1036,23 @@ grabbuttons(Client *c, Bool focused) {
                XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
                if(focused) {
                        for(i = 0; i < LENGTH(buttons); i++)
-                               if(buttons[i].click == ClkClientWin)
+                               if(buttons[i].click == ClkClientWin || buttons[i].click == ClkAnywhere)
                                        for(j = 0; j < LENGTH(modifiers); j++)
                                                XGrabButton(dpy, buttons[i].button,
                                                            buttons[i].mask | modifiers[j],
                                                            c->win, False, BUTTONMASK,
                                                            GrabModeAsync, GrabModeSync, None, None);
-               }
-               else
+               } else {
                        XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
                                    BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
+               }
        }
 }
 
 void
 grabkeys(void) {
        updatenumlockmask();
+       //XUngrabKey(dpy, AnyKey, AnyModifier, root);
        {
                unsigned int i, j;
                unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
@@ -1060,33 +1063,8 @@ grabkeys(void) {
                        if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
                                for(j = 0; j < LENGTH(modifiers); j++)
                                        XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
-                                                True, GrabModeAsync, GrabModeAsync);
-               if(key_buffering)
-                       grab_typing_keys();
-       }
-}
-
-void
-grab_typing_keys(void) {
-       updatenumlockmask();
-       {
-               unsigned int i, j;
-               unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
-               KeySym typing_keys[] = {
-                       XK_space, XK_Return, XK_period, XK_slash, XK_minus, XK_apostrophe,
-                       XK_A, XK_B, XK_C, XK_D, XK_E, XK_F, XK_G, XK_H, XK_I, XK_J, XK_K,
-                       XK_L, XK_M, XK_N, XK_O, XK_P, XK_Q, XK_R, XK_S, XK_T, XK_U, XK_V,
-                       XK_W, XK_X, XK_Y, XK_Z, XK_0, XK_1, XK_2, XK_3, XK_4, XK_5, XK_6,
-                       XK_7, XK_8, XK_9,
-               };
-
-               KeyCode code;
-
-               for(i = 0; i < LENGTH(typing_keys); i++)
-                       if((code = XKeysymToKeycode(dpy, typing_keys[i])))
-                               for(j = 0; j < LENGTH(modifiers); j++)
-                                       XGrabKey(dpy, code, modifiers[j], root,
-                                                True, GrabModeAsync, GrabModeAsync);
+                                                True, GrabModeAsync,
+                                                keys[i].func == kbspawn ? GrabModeSync : GrabModeAsync);
        }
 }
 
@@ -1108,58 +1086,18 @@ isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
 #endif /* XINERAMA */
 
 void
-send_keycode(KeyCode key) {
-       XKeyEvent event;
-       if(!selmon->sel) {
-               return;
-       }
-       event.display = dpy;
-       event.root = root;
-       event.window = selmon->sel->win;
-       event.subwindow = None;
-       event.same_screen = True;
-       event.x = 1;
-       event.y = 1;
-       event.x_root = 1;
-       event.y_root = 1;
-       event.time = CurrentTime;
-       event.state = 0; // modifiers
-       event.keycode = key;
-       event.type = KeyPress;
-       XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
-       event.type = KeyRelease;
-       XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
-}
-
-void
 keypress(XEvent *e) {
        unsigned int i;
        KeySym keysym;
        XKeyEvent *ev;
-       Bool called = False;
 
        ev = &e->xkey;
        keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
-       for(i = 0; i < LENGTH(keys); i++)
+       for(i = 0; i < LENGTH(keys); i++) {
                if(keysym == keys[i].keysym
                && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
                && keys[i].func) {
                        keys[i].func(&(keys[i].arg));
-                       called = True;
-               }
-       if(!called) {
-               if(key_buffering) {
-                       if(key_buffer_len == LENGTH(key_buffer)) {
-                               // buffer full, bail
-                               key_buffer_len = 0;
-                               key_buffering = False;
-                               grabkeys(); // stop grabbing typing keys
-                       } else {
-                               key_buffer[key_buffer_len] = (KeyCode)ev->keycode;
-                               key_buffer_len += 1;
-                       }
-               } else {
-                       send_keycode(ev->keycode);
                }
        }
 }
@@ -1184,7 +1122,6 @@ manage(Window w, XWindowAttributes *wa) {
        Client *c, *t = NULL;
        Window trans = None;
        XWindowChanges wc;
-       int i;
 
        if(!(c = calloc(1, sizeof(Client))))
                die("fatal: could not malloc() %u bytes\n", sizeof(Client));
@@ -1200,8 +1137,13 @@ manage(Window w, XWindowAttributes *wa) {
                applyrules(c);
        }
        /* geometry */
-       c->x = c->oldx = wa->x;
-       c->y = c->oldy = wa->y;
+       if(c->x == -1 && c->y == -2) { // secret code for centered
+               c->x = c->oldx = (c->mon->ww - wa->width) / 2;
+               c->y = c->oldy = (c->mon->wh - wa->height) / 2;
+       } else {
+               c->x = c->oldx = wa->x;
+               c->y = c->oldy = wa->y;
+       }
        c->w = c->oldw = wa->width;
        c->h = c->oldh = wa->height;
        c->oldbw = wa->border_width;
@@ -1243,19 +1185,14 @@ manage(Window w, XWindowAttributes *wa) {
        focus(c);
        if(key_buffering) {
                key_buffering = False;
-               grabkeys(); // stop grabbing typing keys
-               if(key_buffer_len > 0) {
-                       for(i = 0; i < key_buffer_len; ++i) {
-                               send_keycode(key_buffer[i]);
-                       }
-               }
-               key_buffer_len = 0;
+               XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
        }
 }
 
 void
 mappingnotify(XEvent *e) {
        XMappingEvent *ev = &e->xmapping;
+       // fprintf(stderr, "MapNotify\n");
 
        XRefreshKeyboardMapping(ev);
        if(ev->request == MappingKeyboard)
@@ -1267,10 +1204,12 @@ maprequest(XEvent *e) {
        static XWindowAttributes wa;
        XMapRequestEvent *ev = &e->xmaprequest;
 
+       // fprintf(stderr, "MapRequest\n");
        if(!XGetWindowAttributes(dpy, ev->window, &wa))
                return;
-       if(wa.override_redirect)
+       if(wa.override_redirect) {
                return;
+       }
        if(!wintoclient(ev->window))
                manage(ev->window, &wa);
 }
@@ -1566,9 +1505,14 @@ run(void) {
        XEvent ev;
        /* main event loop */
        XSync(dpy, False);
-       while(running && !XNextEvent(dpy, &ev))
-               if(handler[ev.type])
+       while(running && !XNextEvent(dpy, &ev)) {
+               if(handler[ev.type]) {
+                       // fprintf(stderr, "handling event type %i\n", ev.type);
                        handler[ev.type](&ev); /* call handler */
+               } else {
+                       // fprintf(stderr, "evt type %i\n", ev.type);
+               }
+       }
 }
 
 void
@@ -1795,9 +1739,15 @@ sigchld(int unused) {
 }
 
 void
+kbspawn(const Arg *arg) {
+       key_buffering = True;
+       spawn(arg);
+}
+
+void
 spawn(const Arg *arg) {
        int tag = 0, i;
-       if(arg->v == termcmd) {
+       if(arg->v == termcmd || arg->v == runcmd) {
                for(i = 0; i < 32; ++i) {
                        if(selmon->tagset[selmon->seltags] & (1 << i)) {
                                tag = i;
@@ -1805,13 +1755,10 @@ spawn(const Arg *arg) {
                        }
                }
                WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
-               WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1];
+               WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1]; // 2nd digit or null
        }
        if(arg->v == dmenucmd)
                dmenumon[0] = '0' + selmon->num;
-       key_buffering = True;
-       key_buffer_len = 0;
-       grab_typing_keys();
        if(fork() == 0) {
                if(dpy)
                        close(ConnectionNumber(dpy));
@@ -1872,6 +1819,7 @@ tile(Monitor *m) {
 
 #define TAB_HEIGHT 19
 #define TAB_PAD     7
+#define GUTTER_PX   8
 
 void
 jason_layout(Monitor *m) {
@@ -1907,7 +1855,7 @@ jason_layout(Monitor *m) {
        } else {
                mw = m->ww;
        }
-       right_width = m->ww - mw;
+       right_width = m->ww - mw - GUTTER_PX;
        tab_count = &(tab_counts[0]);
        tab_top = m->wy - (m->wh - (2 * (TAB_HEIGHT + TAB_PAD))) + TAB_HEIGHT;
        for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
@@ -1915,14 +1863,14 @@ jason_layout(Monitor *m) {
                        resize(c, m->wx, m->wy, mw, m->wh, False, 0);
                } else {
                        if (c == vis_slave) {
-                               resize(c, m->wx + mw, m->wy + TAB_HEIGHT + TAB_PAD, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
+                               resize(c, m->wx + mw + GUTTER_PX, m->wy + TAB_HEIGHT + TAB_PAD, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
                                tab_count = &(tab_counts[1]);
                                tab_top = m->wy + m->wh - TAB_HEIGHT;
                                cur_tab = 0;
                        } else {
                                // this function does not get called when focus changes
                                // resize(c, m->wx + m->ww, m->wy, m->ww - mw, m->wh, False);
-                               resize(c, m->wx + mw + right_width * cur_tab / (*tab_count), tab_top, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
+                               resize(c, m->wx + mw + GUTTER_PX + right_width * cur_tab / (*tab_count), tab_top, right_width, m->wh - 2 * (TAB_HEIGHT + TAB_PAD), False, base);
                                cur_tab += 1;
                        }
                }