JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Peter Hartlich's initmodmap patch
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 3d993c5..fa8344a 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -158,6 +158,7 @@ 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 initfont(const char *fontstr);
+static void initmodmap(void);
 static Bool isprotodel(Client *c);
 static void keypress(XEvent *e);
 static void killclient(const Arg *arg);
@@ -202,7 +203,7 @@ static void zoom(const Arg *arg);
 /* variables */
 static char stext[256];
 static int screen;
-static int sx, sy, sw, sh; /* display geometry x, y, width, height */ 
+static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */ 
 static int by, bh, blw;    /* bar geometry y, height and layout symbol width */
 static int wx, wy, ww, wh; /* window area geometry x, y, width, height, bar excluded */
 static unsigned int seltags = 0, sellt = 0;
@@ -568,24 +569,23 @@ drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
 
 void
 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
+       char buf[256];
        int i, x, y, h, len, olen;
        XRectangle r = { dc.x, dc.y, dc.w, dc.h };
-       char buf[256];
 
        XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
        XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
        if(!text)
                return;
        olen = strlen(text);
-       len = MIN(olen, sizeof buf);
-       memcpy(buf, text, len);
        h = dc.font.ascent + dc.font.descent;
        y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
        x = dc.x + (h / 2);
        /* shorten text if necessary */
-       for(; len && (i = textnw(buf, len)) > dc.w - h; len--);
+       for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
        if(!len)
                return;
+       memcpy(buf, text, len);
        if(len < olen)
                for(i = len; i && i > len - 3; buf[--i] = '.');
        XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
@@ -754,16 +754,6 @@ grabkeys(void) {
        unsigned int i, j;
        unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
        KeyCode code;
-       XModifierKeymap *modmap;
-
-       /* init modifier map */
-       modmap = XGetModifierMapping(dpy);
-       for(i = 0; i < 8; i++)
-               for(j = 0; j < modmap->max_keypermod; j++) {
-                       if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
-                               numlockmask = (1 << i);
-               }
-       XFreeModifiermap(modmap);
 
        XUngrabKey(dpy, AnyKey, AnyModifier, root);
        for(i = 0; i < LENGTH(keys); i++) {
@@ -814,6 +804,19 @@ initfont(const char *fontstr) {
        dc.font.height = dc.font.ascent + dc.font.descent;
 }
 
+void
+initmodmap(void) {
+       unsigned int i, j;
+       XModifierKeymap *modmap;
+
+       modmap = XGetModifierMapping(dpy);
+       for(i = 0; i < 8; i++)
+               for(j = 0; j < modmap->max_keypermod; j++)
+                       if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
+                               numlockmask = (1 << i);
+       XFreeModifiermap(modmap);
+}
+
 Bool
 isprotodel(Client *c) {
        int i, n;
@@ -866,7 +869,7 @@ killclient(const Arg *arg) {
 void
 manage(Window w, XWindowAttributes *wa) {
        Client *c, *t = NULL;
-       Window trans = 0;
+       Window trans = None;
        XWindowChanges wc;
 
        if(!(c = calloc(1, sizeof(Client))))
@@ -910,7 +913,7 @@ manage(Window w, XWindowAttributes *wa) {
        else
                applyrules(c);
        if(!c->isfloating)
-               c->isfloating = trans || c->isfixed;
+               c->isfloating = trans != None || c->isfixed;
        if(c->isfloating)
                XRaiseWindow(dpy, c->win);
        attach(c);
@@ -926,8 +929,10 @@ mappingnotify(XEvent *e) {
        XMappingEvent *ev = &e->xmapping;
 
        XRefreshKeyboardMapping(ev);
-       if(ev->request == MappingKeyboard)
+       if(ev->request == MappingKeyboard) {
+               initmodmap();
                grabkeys();
+       }
 }
 
 void
@@ -962,18 +967,15 @@ movemouse(const Arg *arg) {
        if(!(c = sel))
                return;
        restack();
-       ocx = nx = c->x;
-       ocy = ny = c->y;
+       ocx = c->x;
+       ocy = c->y;
        if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
        None, cursor[CurMove], CurrentTime) != GrabSuccess)
                return;
        XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
-       for(;;) {
+       do {
                XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
                switch (ev.type) {
-               case ButtonRelease:
-                       XUngrabPointer(dpy, CurrentTime);
-                       return;
                case ConfigureRequest:
                case Expose:
                case MapRequest:
@@ -1001,6 +1003,8 @@ movemouse(const Arg *arg) {
                        break;
                }
        }
+       while(ev.type != ButtonRelease);
+       XUngrabPointer(dpy, CurrentTime);
 }
 
 Client *
@@ -1129,15 +1133,9 @@ resizemouse(const Arg *arg) {
        None, cursor[CurResize], CurrentTime) != GrabSuccess)
                return;
        XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
-       for(;;) {
-               XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
+       do {
+               XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
                switch(ev.type) {
-               case ButtonRelease:
-                       XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
-                                       c->w + c->bw - 1, c->h + c->bw - 1);
-                       XUngrabPointer(dpy, CurrentTime);
-                       while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
-                       return;
                case ConfigureRequest:
                case Expose:
                case MapRequest:
@@ -1159,6 +1157,10 @@ resizemouse(const Arg *arg) {
                        break;
                }
        }
+       while(ev.type != ButtonRelease);
+       XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
+       XUngrabPointer(dpy, CurrentTime);
+       while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
 }
 
 void
@@ -1247,10 +1249,9 @@ run(void) {
 void
 scan(void) {
        unsigned int i, num;
-       Window *wins, d1, d2;
+       Window d1, d2, *wins = NULL;
        XWindowAttributes wa;
 
-       wins = NULL;
        if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
                for(i = 0; i < num; i++) {
                        if(!XGetWindowAttributes(dpy, wins[i], &wa)
@@ -1266,9 +1267,9 @@ scan(void) {
                        && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
                                manage(wins[i], &wa);
                }
+               if(wins)
+                       XFree(wins);
        }
-       if(wins)
-               XFree(wins);
 }
 
 void
@@ -1378,8 +1379,7 @@ setup(void) {
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
 
-
-       /* grab keys */
+       initmodmap();
        grabkeys();
 }
 
@@ -1474,8 +1474,12 @@ togglefloating(const Arg *arg) {
 
 void
 toggletag(const Arg *arg) {
-       unsigned int mask = sel->tags ^ (arg->ui & TAGMASK);
+       unsigned int mask;
 
+       if (!sel)
+               return;
+       
+       mask = sel->tags ^ (arg->ui & TAGMASK);
        if(sel && mask) {
                sel->tags = mask;
                arrange();
@@ -1640,10 +1644,10 @@ updatewmhints(Client *c) {
 
 void
 view(const Arg *arg) {
-       if(arg && (arg->ui & TAGMASK) == tagset[seltags])
+       if((arg->ui & TAGMASK) == tagset[seltags])
                return;
        seltags ^= 1; /* toggle sel tagset */
-       if(arg && (arg->ui & TAGMASK))
+       if(arg->ui & TAGMASK)
                tagset[seltags] = arg->ui & TAGMASK;
        clearurgent();
        arrange();