JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied James Turner's XkbKeycodeToKeysym patch, thanks
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 4c6af64..6b39519 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -36,6 +36,7 @@
 #include <X11/Xlib.h>
 #include <X11/Xproto.h>
 #include <X11/Xutil.h>
+#include <X11/XKBlib.h>
 #ifdef XINERAMA
 #include <X11/extensions/Xinerama.h>
 #endif /* XINERAMA */
@@ -43,7 +44,8 @@
 /* macros */
 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
-#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
+#define INTERSECT(x,y,w,h,m)    (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
+                               * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
 #define ISVISIBLE(C)            ((C->tags & C->mon->tagset[C->mon->seltags]))
 #define LENGTH(X)               (sizeof X / sizeof X[0])
 #define MAX(A, B)               ((A) > (B) ? (A) : (B))
@@ -59,7 +61,7 @@ enum { CurNormal, CurResize, CurMove, CurLast };        /* cursor */
 enum { ColBorder, ColFG, ColBG, ColLast };              /* color */
 enum { NetSupported, NetWMName, NetWMState,
        NetWMFullscreen, NetActiveWindow, NetWMWindowType,
-       NetWMWindowTypeDialog, NetLast };     /* EWMH atoms */
+       NetWMWindowTypeDialog, NetClientList, NetLast };     /* EWMH atoms */
 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
        ClkClientWin, ClkRootWin, ClkLast };             /* clicks */
@@ -199,12 +201,13 @@ static void manage(Window w, XWindowAttributes *wa);
 static void mappingnotify(XEvent *e);
 static void maprequest(XEvent *e);
 static void monocle(Monitor *m);
+static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static Client *nexttiled(Client *c);
 static void pop(Client *);
 static void propertynotify(XEvent *e);
-static Monitor *ptrtomon(int x, int y);
 static void quit(const Arg *arg);
+static Monitor *recttomon(int x, int y, int w, int h);
 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
 static void resizeclient(Client *c, int x, int y, int w, int h);
 static void resizemouse(const Arg *arg);
@@ -233,9 +236,10 @@ static void toggleview(const Arg *arg);
 static void unfocus(Client *c, Bool setfocus);
 static void unmanage(Client *c, Bool destroyed);
 static void unmapnotify(XEvent *e);
-static Bool updategeom(void);
+static void updategeom(void);
 static void updatebarpos(Monitor *m);
 static void updatebars(void);
+static void updateclientlist(void);
 static void updatenumlockmask(void);
 static void updatesizehints(Client *c);
 static void updatestatus(void);
@@ -270,6 +274,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
        [KeyPress] = keypress,
        [MappingNotify] = mappingnotify,
        [MapRequest] = maprequest,
+       [MotionNotify] = motionnotify,
        [PropertyNotify] = propertynotify,
        [UnmapNotify] = unmapnotify
 };
@@ -394,9 +399,10 @@ arrange(Monitor *m) {
                showhide(m->stack);
        else for(m = mons; m; m = m->next)
                showhide(m->stack);
-       if(m)
+       if(m) {
                arrangemon(m);
-       else for(m = mons; m; m = m->next)
+               restack(m);
+       } else for(m = mons; m; m = m->next)
                arrangemon(m);
 }
 
@@ -405,7 +411,6 @@ arrangemon(Monitor *m) {
        strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
        if(m->lt[m->sellt]->arrange)
                m->lt[m->sellt]->arrange(m);
-       restack(m);
 }
 
 void
@@ -496,6 +501,7 @@ cleanup(void) {
                cleanupmon(mons);
        XSync(dpy, False);
        XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
+       XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
 }
 
 void
@@ -568,22 +574,18 @@ void
 configurenotify(XEvent *e) {
        Monitor *m;
        XConfigureEvent *ev = &e->xconfigure;
-       Bool dirty;
 
        if(ev->window == root) {
-               dirty = (sw != ev->width);
                sw = ev->width;
                sh = ev->height;
-               if(updategeom() || dirty) {
-                       if(dc.drawable != 0)
-                               XFreePixmap(dpy, dc.drawable);
-                       dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
-                       updatebars();
-                       for(m = mons; m; m = m->next)
-                               XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
-                       focus(NULL);
-                       arrange(NULL);
-               }
+               if(dc.drawable != 0)
+                       XFreePixmap(dpy, dc.drawable);
+               dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
+               updatebars();
+               for(m = mons; m; m = m->next)
+                       XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
+               focus(NULL);
+               arrange(NULL);
        }
 }
 
@@ -855,8 +857,10 @@ focus(Client *c) {
                XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
                setfocus(c);
        }
-       else
+       else {
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+               XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
+       }
        selmon->sel = c;
        drawbars();
 }
@@ -877,7 +881,8 @@ focusmon(const Arg *arg) {
                return;
        if((m = dirtomon(arg->i)) == selmon)
                return;
-       unfocus(selmon->sel, True);
+       unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
+                                       in gedit and anjuta */
        selmon = m;
        focus(NULL);
 }
@@ -1067,8 +1072,8 @@ initfont(const char *fontstr) {
 static Bool
 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
        while(n--)
-               if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
-               && unique[n].width == info->width && unique[n].height == info->height)
+               /* treat origin (x, y) as fixpoint for uniqueness only, first screen wins */
+               if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org)
                        return False;
        return True;
 }
@@ -1081,7 +1086,7 @@ keypress(XEvent *e) {
        XKeyEvent *ev;
 
        ev = &e->xkey;
-       keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
+       keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
        for(i = 0; i < LENGTH(keys); i++)
                if(keysym == keys[i].keysym
                && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
@@ -1154,6 +1159,8 @@ manage(Window w, XWindowAttributes *wa) {
                XRaiseWindow(dpy, c->win);
        attach(c);
        attachstack(c);
+       XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
+                       (unsigned char *) &(c->win), 1);
        XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
        setclientstate(c, NormalState);
        if (c->mon == selmon)
@@ -1201,6 +1208,22 @@ monocle(Monitor *m) {
 }
 
 void
+motionnotify(XEvent *e) {
+       static Monitor *mon = NULL;
+       Monitor *m;
+       XMotionEvent *ev = &e->xmotion;
+
+       if(ev->window != root)
+               return;
+       if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
+               unfocus(selmon->sel, True);
+               selmon = m;
+               focus(NULL);
+       }
+       mon = m;
+}
+
+void
 movemouse(const Arg *arg) {
        int x, y, ocx, ocy, nx, ny;
        Client *c;
@@ -1209,6 +1232,8 @@ movemouse(const Arg *arg) {
 
        if(!(c = selmon->sel))
                return;
+       if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
+               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
@@ -1248,7 +1273,7 @@ movemouse(const Arg *arg) {
                }
        } while(ev.type != ButtonRelease);
        XUngrabPointer(dpy, CurrentTime);
-       if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
+       if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
                sendmon(c, m);
                selmon = m;
                focus(NULL);
@@ -1305,21 +1330,24 @@ propertynotify(XEvent *e) {
        }
 }
 
-Monitor *
-ptrtomon(int x, int y) {
-       Monitor *m;
-
-       for(m = mons; m; m = m->next)
-               if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
-                       return m;
-       return selmon;
-}
-
 void
 quit(const Arg *arg) {
        running = False;
 }
 
+Monitor *
+recttomon(int x, int y, int w, int h) {
+       Monitor *m, *r = selmon;
+       int a, area = 0;
+
+       for(m = mons; m; m = m->next)
+               if((a = INTERSECT(x, y, w, h, m)) > area) {
+                       area = a;
+                       r = m;
+               }
+       return r;
+}
+
 void
 resize(Client *c, int x, int y, int w, int h, Bool interact) {
        if(applysizehints(c, &x, &y, &w, &h, interact))
@@ -1350,6 +1378,8 @@ resizemouse(const Arg *arg) {
 
        if(!(c = selmon->sel))
                return;
+       if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
+               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
@@ -1383,7 +1413,7 @@ resizemouse(const Arg *arg) {
        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));
-       if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
+       if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
                sendmon(c, m);
                selmon = m;
                focus(NULL);
@@ -1499,8 +1529,12 @@ sendevent(Client *c, Atom proto) {
 
 void
 setfocus(Client *c) {
-       if(!c->neverfocus)
+       if(!c->neverfocus) {
                XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
+               XChangeProperty(dpy, root, netatom[NetActiveWindow],
+                               XA_WINDOW, 32, PropModeReplace,
+                               (unsigned char *) &(c->win), 1);
+       }
        sendevent(c, wmatom[WMTakeFocus]);
 }
 
@@ -1586,6 +1620,7 @@ setup(void) {
        netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
        netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
        netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
+       netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
        /* init cursors */
        cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
        cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
@@ -1608,11 +1643,11 @@ setup(void) {
        /* EWMH support per view */
        XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
                        PropModeReplace, (unsigned char *) netatom, NetLast);
+       XDeleteProperty(dpy, root, netatom[NetClientList]);
        /* select for events */
        wa.cursor = cursor[CurNormal];
-       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
-                       |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
-                       |PropertyChangeMask;
+       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
+                       |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
        grabkeys();
@@ -1719,6 +1754,8 @@ void
 togglefloating(const Arg *arg) {
        if(!selmon->sel)
                return;
+       if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
+               return;
        selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
        if(selmon->sel->isfloating)
                resize(selmon->sel, selmon->sel->x, selmon->sel->y,
@@ -1757,8 +1794,10 @@ unfocus(Client *c, Bool setfocus) {
                return;
        grabbuttons(c, False);
        XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
-       if(setfocus)
+       if(setfocus) {
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+               XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
+       }
 }
 
 void
@@ -1782,6 +1821,7 @@ unmanage(Client *c, Bool destroyed) {
        }
        free(c);
        focus(NULL);
+       updateclientlist();
        arrange(m);
 }
 
@@ -1807,6 +1847,8 @@ updatebars(void) {
                .event_mask = ButtonPressMask|ExposureMask
        };
        for(m = mons; m; m = m->next) {
+               if (m->barwin)
+                       continue;
                m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
                                          CopyFromParent, DefaultVisual(dpy, screen),
                                          CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
@@ -1828,86 +1870,87 @@ updatebarpos(Monitor *m) {
                m->by = -bh;
 }
 
-Bool
+void
+updateclientlist() {
+       Client *c;
+       Monitor *m;
+
+       XDeleteProperty(dpy, root, netatom[NetClientList]);
+       for(m = mons; m; m = m->next)
+               for(c = m->clients; c; c = c->next)
+                       XChangeProperty(dpy, root, netatom[NetClientList],
+                                       XA_WINDOW, 32, PropModeAppend,
+                                       (unsigned char *) &(c->win), 1);
+}
+
+void
 updategeom(void) {
-       Bool dirty = False;
+       /* Starting with dwm 6.1 this function uses a new (simpler) strategy:
+        * whenever screen changes are reported, we destroy all monitors
+        * and recreate all unique origin monitors and add all clients to
+        * the first monitor, only. In several circumstances this may suck,
+        * but dealing with all corner-cases sucks even more.*/
 
 #ifdef XINERAMA
        if(XineramaIsActive(dpy)) {
-               int i, j, n, nn;
+               int i, j, n;
                Client *c;
-               Monitor *m;
-               XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
+               Monitor *m, *oldmons = mons;
+               XineramaScreenInfo *info = XineramaQueryScreens(dpy, &n);
                XineramaScreenInfo *unique = NULL;
 
-               for(n = 0, m = mons; m; m = m->next, n++);
                /* only consider unique geometries as separate screens */
-               if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
-                       die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
-               for(i = 0, j = 0; i < nn; i++)
+               if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * n)))
+                       die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * n);
+               for(i = 0, j = 0; i < n; i++)
                        if(isuniquegeom(unique, j, &info[i]))
                                memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
                XFree(info);
-               nn = j;
-               if(n <= nn) {
-                       for(i = 0; i < (nn - n); i++) { /* new monitors available */
-                               for(m = mons; m && m->next; m = m->next);
-                               if(m)
-                                       m->next = createmon();
-                               else
-                                       mons = createmon();
-                       }
-                       for(i = 0, m = mons; i < nn && m; m = m->next, i++)
-                               if(i >= n
-                               || (unique[i].x_org != m->mx || unique[i].y_org != m->my
-                                   || unique[i].width != m->mw || unique[i].height != m->mh))
-                               {
-                                       dirty = True;
-                                       m->num = i;
-                                       m->mx = m->wx = unique[i].x_org;
-                                       m->my = m->wy = unique[i].y_org;
-                                       m->mw = m->ww = unique[i].width;
-                                       m->mh = m->wh = unique[i].height;
-                                       updatebarpos(m);
-                               }
+               /* create new monitor structure */
+               n = j;
+               mons = m = createmon(); /* new first monitor */
+               for(i = 1; i < n; i++) {
+                       m->next = createmon();
+                       m = m->next;
                }
-               else { /* less monitors available nn < n */
-                       for(i = nn; i < n; i++) {
-                               for(m = mons; m && m->next; m = m->next);
-                               while(m->clients) {
-                                       dirty = True;
-                                       c = m->clients;
-                                       m->clients = c->next;
-                                       detachstack(c);
-                                       c->mon = mons;
-                                       attach(c);
-                                       attachstack(c);
-                               }
-                               if(m == selmon)
-                                       selmon = mons;
-                               cleanupmon(m);
-                       }
+               for(i = 0, m = mons; i < n && m; m = m->next, i++) {
+                       m->num = i;
+                       m->mx = m->wx = unique[i].x_org;
+                       m->my = m->wy = unique[i].y_org;
+                       m->mw = m->ww = unique[i].width;
+                       m->mh = m->wh = unique[i].height;
+                       updatebarpos(m);
                }
                free(unique);
+               /* re-attach old clients and cleanup old monitor structure */
+               while(oldmons) {
+                       m = oldmons;
+                       while(m->clients) {
+                               c = m->clients;
+                               m->clients = c->next;
+                               detachstack(c);
+                               c->mon = mons;
+                               attach(c);
+                               attachstack(c);
+                       }
+                       oldmons = m->next;
+                       cleanupmon(m);
+               }
        }
        else
 #endif /* XINERAMA */
        /* default monitor setup */
        {
-               if(!mons)
+               if(!mons) /* only true if !XINERAMA compile flag */
                        mons = createmon();
                if(mons->mw != sw || mons->mh != sh) {
-                       dirty = True;
                        mons->mw = mons->ww = sw;
                        mons->mh = mons->wh = sh;
                        updatebarpos(mons);
                }
        }
-       if(dirty) {
-               selmon = mons;
-               selmon = wintomon(root);
-       }
-       return dirty;
+       selmon = mons;
+       selmon = wintomon(root);
 }
 
 void
@@ -1997,7 +2040,6 @@ updatewindowtype(Client *c) {
 
        if(state == netatom[NetWMFullscreen])
                setfullscreen(c, True);
-
        if(wtype == netatom[NetWMWindowTypeDialog])
                c->isfloating = True;
 }
@@ -2051,7 +2093,7 @@ wintomon(Window w) {
        Monitor *m;
 
        if(w == root && getrootptr(&x, &y))
-               return ptrtomon(x, y);
+               return recttomon(x, y, 1, 1);
        for(m = mons; m; m = m->next)
                if(w == m->barwin)
                        return m;
@@ -2109,7 +2151,7 @@ zoom(const Arg *arg) {
 int
 main(int argc, char *argv[]) {
        if(argc == 2 && !strcmp("-v", argv[1]))
-               die("dwm-"VERSION", © 2006-2011 dwm engineers, see LICENSE for details\n");
+               die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
        else if(argc != 1)
                die("usage: dwm [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())