JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
floating rules trigger centering too
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 5e9b184..3196192 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -177,6 +177,7 @@ 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);
@@ -200,6 +201,7 @@ 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);
@@ -209,6 +211,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);
@@ -240,6 +243,9 @@ 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];
 static int screen;
@@ -358,6 +364,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);
@@ -1056,6 +1065,32 @@ grabkeys(void) {
                                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);
        }
 }
 
@@ -1077,18 +1112,60 @@ 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++)
                if(keysym == keys[i].keysym
                && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
-               && keys[i].func)
+               && 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);
+               }
+       }
 }
 
 void
@@ -1111,6 +1188,7 @@ 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));
@@ -1126,8 +1204,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;
@@ -1167,6 +1250,16 @@ manage(Window w, XWindowAttributes *wa) {
        arrange(c->mon);
        XMapWindow(dpy, c->win);
        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;
+       }
 }
 
 void
@@ -1185,8 +1278,11 @@ maprequest(XEvent *e) {
 
        if(!XGetWindowAttributes(dpy, ev->window, &wa))
                return;
-       if(wa.override_redirect)
+       if(wa.override_redirect) {
+               key_buffer_len = 0;
+               key_buffering = False;
                return;
+       }
        if(!wintoclient(ev->window))
                manage(ev->window, &wa);
 }
@@ -1711,6 +1807,14 @@ sigchld(int unused) {
 }
 
 void
+kbspawn(const Arg *arg) {
+       key_buffering = True;
+       key_buffer_len = 0;
+       grab_typing_keys();
+       spawn(arg);
+}
+
+void
 spawn(const Arg *arg) {
        int tag = 0, i;
        if(arg->v == termcmd) {