JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed int cast in TAGMASK as suggested by nsz
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index d71bb58..6ddf266 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -51,7 +51,7 @@
 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask)
 #define WIDTH(X)                ((X)->w + 2 * (X)->bw)
 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
-#define TAGMASK                 ((int)((1LL << LENGTH(tags)) - 1))
+#define TAGMASK                 ((1 << LENGTH(tags)) - 1)
 #define TEXTW(X)                (textnw(X, strlen(X)) + dc.font.height)
 
 /* enums */
@@ -121,8 +121,8 @@ typedef struct {
 } Layout;
 
 struct Monitor {
-       int screen_number;
        float mfact;
+       int num;
        int by;               /* bar geometry */
        int mx, my, mw, mh;   /* screen size */
        int wx, wy, ww, wh;   /* window area  */
@@ -145,6 +145,7 @@ typedef struct {
        const char *title;
        unsigned int tags;
        Bool isfloating;
+       int monitor;
 } Rule;
 
 /* function declarations */
@@ -236,9 +237,10 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
 
 /* variables */
+static const char broken[] = "broken";
 static char stext[256], ntext[8];
 static int screen;
-static int sw, sh;           /* X display screen geometry x, y, width, height */
+static int sw, sh;           /* X display screen geometry width, height */
 static int bh, blw = 0;      /* bar geometry */
 static int (*xerrorxlib)(Display *, XErrorEvent *);
 static unsigned int numlockmask = 0;
@@ -269,26 +271,33 @@ static Window root;
 #include "config.h"
 
 /* compile-time check if all tags fit into an unsigned int bit array. */
-struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
+struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
 
 /* function implementations */
 void
 applyrules(Client *c) {
+       const char *class, *instance;
        unsigned int i;
        const Rule *r;
+       Monitor *m;
        XClassHint ch = { 0 };
 
        /* rule matching */
        c->isfloating = c->tags = 0;
        if(XGetClassHint(dpy, c->win, &ch)) {
+               class = ch.res_class ? ch.res_class : broken;
+               instance = ch.res_name ? ch.res_name : broken;
                for(i = 0; i < LENGTH(rules); i++) {
                        r = &rules[i];
                        if((!r->title || strstr(c->name, r->title))
-                       && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
-                       && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance))))
+                       && (!r->class || strstr(class, r->class))
+                       && (!r->instance || strstr(instance, r->instance)))
                        {
                                c->isfloating = r->isfloating;
                                c->tags |= r->tags;
+                               for(m = mons; m && m->num != r->monitor; m = m->next);
+                               if(m)
+                                       c->mon = m;
                        }
                }
                if(ch.res_class)
@@ -411,9 +420,9 @@ buttonpress(XEvent *e) {
        }
        if(ev->window == selmon->barwin) {
                i = x = 0;
-               do
+               do {
                        x += TEXTW(tags[i]);
-               while(ev->x >= x && ++i < LENGTH(tags));
+               } while(ev->x >= x && ++i < LENGTH(tags));
                if(i < LENGTH(tags)) {
                        click = ClkTagBar;
                        arg.ui = 1 << i;
@@ -968,6 +977,7 @@ initfont(const char *fontstr) {
                XFontSetExtents *font_extents;
                XFontStruct **xfonts;
                char **font_names;
+
                dc.font.ascent = dc.font.descent = 0;
                font_extents = XExtentsOfFontSet(dc.font.set);
                n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
@@ -1047,6 +1057,7 @@ manage(Window w, XWindowAttributes *wa) {
                die("fatal: could not malloc() %u bytes\n", sizeof(Client));
        *c = cz;
        c->win = w;
+       updatetitle(c);
        if(XGetTransientForHint(dpy, w, &trans))
                t = wintoclient(trans);
        if(t) {
@@ -1086,7 +1097,6 @@ manage(Window w, XWindowAttributes *wa) {
        updatesizehints(c);
        XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
        grabbuttons(c, False);
-       updatetitle(c);
        if(!c->isfloating)
                c->isfloating = trans != None || c->isfixed;
        if(c->isfloating)
@@ -1175,8 +1185,7 @@ movemouse(const Arg *arg) {
                                resize(c, nx, ny, c->w, c->h, True);
                        break;
                }
-       }
-       while(ev.type != ButtonRelease);
+       } while(ev.type != ButtonRelease);
        XUngrabPointer(dpy, CurrentTime);
        if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
                sendmon(c, m);
@@ -1294,8 +1303,7 @@ resizemouse(const Arg *arg) {
                                resize(c, c->x, c->y, nw, nh, True);
                        break;
                }
-       }
-       while(ev.type != ButtonRelease);
+       } 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));
@@ -1338,7 +1346,7 @@ run(void) {
        XSync(dpy, False);
        while(running && !XNextEvent(dpy, &ev))
                if(handler[ev.type])
-                       (handler[ev.type])(&ev); /* call handler */
+                       handler[ev.type](&ev); /* call handler */
 }
 
 void
@@ -1679,7 +1687,7 @@ void
 updategeom(void) {
        int i, n = 1;
        Client *c;
-       Monitor *newmons = NULL, *m, *tm;
+       Monitor *newmons = NULL, *m = NULL, *tm;
 
 #ifdef XINERAMA
        XineramaScreenInfo *info = NULL;
@@ -1698,7 +1706,7 @@ updategeom(void) {
 #ifdef XINERAMA
        if(XineramaIsActive(dpy)) {
                for(i = 0, m = newmons; m; m = m->next, i++) {
-                       m->screen_number = info[i].screen_number;
+                       m->num = info[i].screen_number;
                        m->mx = m->wx = info[i].x_org;
                        m->my = m->wy = info[i].y_org;
                        m->mw = m->ww = info[i].width;
@@ -1710,7 +1718,7 @@ updategeom(void) {
 #endif /* XINERAMA */
        /* default monitor setup */
        {
-               m->screen_number = 0;
+               m->num = 0;
                m->mx = m->wx = 0;
                m->my = m->wy = 0;
                m->mw = m->ww = sw;
@@ -1814,6 +1822,8 @@ void
 updatetitle(Client *c) {
        if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
                gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
+       if(c->name[0] == '\0') /* hack to mark broken clients */
+               strcpy(c->name, broken);
 }
 
 void