X-Git-Url: https://jasonwoof.com/gitweb/?p=dwm.git;a=blobdiff_plain;f=dwm.c;h=7f2a638ddd23525531e95336fa0e25f77a97b312;hp=37e833e3cafa9a1bf59e588061c83825c1c8e8d1;hb=874837f653433315bd2733823a0a1efe6f76b373;hpb=0c71b16b9282562ee59f73f215c6ba827df25927 diff --git a/dwm.c b/dwm.c index 37e833e..7f2a638 100644 --- a/dwm.c +++ b/dwm.c @@ -41,6 +41,8 @@ #include /* macros */ +#define MAX(a, b) ((a)>(b)?(a):(b)) +#define MIN(a, b) ((a)<(b)?(a):(b)) #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask)) #define LENGTH(x) (sizeof x / sizeof x[0]) @@ -212,6 +214,7 @@ char stext[256], buf[256]; int screen, sx, sy, sw, sh; int (*xerrorxlib)(Display *, XErrorEvent *); int bx, by, bw, bh, blw, bgw, mx, my, mw, mh, mox, moy, mow, moh, tx, ty, tw, th, wx, wy, ww, wh; +double mfact; unsigned int numlockmask = 0; void (*handler[LASTEvent]) (XEvent *) = { [ButtonPress] = buttonpress, @@ -238,14 +241,14 @@ Client *stack = NULL; Cursor cursor[CurLast]; Display *dpy; DC dc = {0}; -Geom *geom = NULL; -Layout *lt = NULL; Window root, barwin; /* configuration, allows nested code to access above variables */ #include "config.h" #define TAGSZ (LENGTH(tags) * sizeof(Bool)) -static Bool tmp[LENGTH(tags)]; +Bool tmp[LENGTH(tags)]; +Layout *lt = layouts; +Geom *geom = geoms; /* function implementations */ @@ -260,10 +263,9 @@ applyrules(Client *c) { XGetClassHint(dpy, c->win, &ch); for(i = 0; i < LENGTH(rules); i++) { r = &rules[i]; - if((r->title && strstr(c->name, r->title)) - || (ch.res_class && r->class && strstr(ch.res_class, r->class)) - || (ch.res_name && r->instance && strstr(ch.res_name, r->instance))) - { + 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)))) { c->isfloating = r->isfloating; if(r->tag) { c->tags[idxoftag(r->tag)] = True; @@ -601,9 +603,8 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) { if(!text) return; w = 0; - olen = len = strlen(text); - if(len >= sizeof buf) - len = sizeof buf - 1; + olen = strlen(text); + len = MIN(olen, sizeof buf - 1); memcpy(buf, text, len); buf[len] = 0; h = dc.font.ascent + dc.font.descent; @@ -805,39 +806,20 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) { void grabbuttons(Client *c, Bool focused) { - XUngrabButton(dpy, AnyButton, AnyModifier, c->win); + int i, j; + unsigned int buttons[] = { Button1, Button2, Button3 }; + unsigned int modifiers[] = { MODKEY, MODKEY|LockMask, MODKEY|numlockmask, + MODKEY|numlockmask|LockMask} ; - if(focused) { - XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - - XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - - XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); - } + XUngrabButton(dpy, AnyButton, AnyModifier, c->win); + if(focused) + for(i = 0; i < LENGTH(buttons); i++) + for(j = 0; j < LENGTH(modifiers); j++) + XGrabButton(dpy, buttons[i], modifiers[j], c->win, False, + BUTTONMASK, GrabModeAsync, GrabModeSync, None, None); else - XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK, - GrabModeAsync, GrabModeSync, None, None); + XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, + BUTTONMASK, GrabModeAsync, GrabModeSync, None, None); } void @@ -899,10 +881,8 @@ initfont(const char *fontstr) { font_extents = XExtentsOfFontSet(dc.font.set); n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { - if(dc.font.ascent < (*xfonts)->ascent) - dc.font.ascent = (*xfonts)->ascent; - if(dc.font.descent < (*xfonts)->descent) - dc.font.descent = (*xfonts)->descent; + dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent); + dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent); xfonts++; } } @@ -1027,10 +1007,8 @@ manage(Window w, XWindowAttributes *wa) { c->x = wx + ww - c->w - 2 * c->bw; if(c->y + c->h + 2 * c->bw > wy + wh) c->y = wy + wh - c->h - 2 * c->bw; - if(c->x < wx) - c->x = wx; - if(c->y < wy) - c->y = wy; + c->x = MAX(c->x, wx); + c->y = MAX(c->y, wy); c->bw = BORDERPX; } @@ -1086,7 +1064,7 @@ monocle(void) { Client *c; for(c = clients; c; c = c->next) - if(isvisible(c)) + if((lt->isfloating || !c->isfloating) && isvisible(c)) resize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw, RESIZEHINTS); } @@ -1196,20 +1174,20 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) { if(sizehints) { /* set minimum possible */ - if (w < 1) - w = 1; - if (h < 1) - h = 1; + w = MAX(1, w); + h = MAX(1, h); /* temporarily remove base dimensions */ w -= c->basew; h -= c->baseh; /* adjust for aspect limits */ - if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) { - if (w * c->maxay > h * c->maxax) + if(c->minax != c->maxax && c->minay != c->maxay + && c->minax > 0 && c->maxax > 0 && c->minay > 0 && c->maxay > 0) + { + if(w * c->maxay > h * c->maxax) w = h * c->maxax / c->maxay; - else if (w * c->minay < h * c->minax) + else if(w * c->minay < h * c->minax) h = w * c->minay / c->minax; } @@ -1223,14 +1201,14 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) { w += c->basew; h += c->baseh; - if(c->minw > 0 && w < c->minw) - w = c->minw; - if(c->minh > 0 && h < c->minh) - h = c->minh; - if(c->maxw > 0 && w > c->maxw) - w = c->maxw; - if(c->maxh > 0 && h > c->maxh) - h = c->maxh; + w = MAX(w, c->minw); + h = MAX(h, c->minh); + + if (c->maxw) + w = MIN(w, c->maxw); + + if (c->maxh) + h = MIN(h, c->maxh); } if(w <= 0 || h <= 0) return; @@ -1283,10 +1261,8 @@ resizemouse(Client *c) { break; case MotionNotify: XSync(dpy, False); - if((nw = ev.xmotion.x - ocx - 2 * c->bw + 1) <= 0) - nw = 1; - if((nh = ev.xmotion.y - ocy - 2 * c->bw + 1) <= 0) - nh = 1; + nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); + nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP)) togglefloating(NULL); if((lt->isfloating) || c->isfloating) @@ -1465,20 +1441,19 @@ setlayout(const char *arg) { void setmfact(const char *arg) { - double delta; + double d; - if(!arg || lt->isfloating) + if(lt->isfloating) return; - delta = strtod(arg, NULL); - if(arg[0] == '-' || arg[0] == '+') { - if(mfact + delta < 0.1 || mfact + delta > 0.9) - return; - mfact += delta; - } + if(!arg) + mfact = MFACT; else { - if(delta < 0.1 || delta > 0.9) + d = strtod(arg, NULL); + if(arg[0] == '-' || arg[0] == '+') + d += mfact; + if(d < 0.1 || d > 0.9) return; - mfact = delta; + mfact = d; } setgeom(geom->symbol); } @@ -1499,7 +1474,7 @@ setup(void) { sw = DisplayWidth(dpy, screen); sh = DisplayHeight(dpy, screen); bh = dc.font.height + 2; - geom = &geoms[0]; + mfact = MFACT; geom->apply(); /* init atoms */ @@ -1535,19 +1510,14 @@ setup(void) { prevtags = emallocz(TAGSZ); seltags[0] = prevtags[0] = True; - /* init layouts */ - lt = &layouts[0]; - /* init bar */ for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) { w = textw(layouts[i].symbol); - if(w > blw) - blw = w; + blw = MAX(blw, w); } for(bgw = i = 0; LENGTH(geoms) > 1 && i < LENGTH(geoms); i++) { w = textw(geoms[i].symbol); - if(w > bgw) - bgw = w; + bgw = MAX(bgw, w); } wa.override_redirect = 1; @@ -1950,3 +1920,4 @@ main(int argc, char *argv[]) { XCloseDisplay(dpy); return 0; } +