X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=dwm.c;h=5bd7221f04e2a4cffeb2f80b0476d2bc6c84fd0d;hb=c36f7c3c5e5db297689765142c64977f6317a21c;hp=87622695617562253740b38d3cf29b5eb4e179ae;hpb=1fafcb1820d37dbf58a864f2c10e1e24ae1a21d1;p=dwm.git diff --git a/dwm.c b/dwm.c index 8762269..5bd7221 100644 --- a/dwm.c +++ b/dwm.c @@ -27,14 +27,15 @@ */ #include #include -#include #include #include #include #include #include #include +#include #include +#include #include #include #include @@ -62,7 +63,7 @@ struct Client { int rx, ry, rw, rh; /* revert geometry */ int basew, baseh, incw, inch, maxw, maxh, minw, minh; int minax, maxax, minay, maxay; - long flags; + long flags; unsigned int border, oldborder; Bool isbanned, isfixed, ismax, isfloating, wasfloating; Bool *tags; @@ -110,7 +111,7 @@ typedef struct { regex_t *tagregex; } Regs; -/* forward declarations */ +/* function declarations */ void applyrules(Client *c); void arrange(void); void attach(Client *c); @@ -144,7 +145,6 @@ Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); void grabbuttons(Client *c, Bool focused); unsigned int idxoftag(const char *tag); void initfont(const char *fontstr); -Bool isarrange(void (*func)()); Bool isoccupied(unsigned int t); Bool isprotodel(Client *c); Bool isvisible(Client *c); @@ -184,6 +184,7 @@ void updatebarpos(void); void updatesizehints(Client *c); void updatetitle(Client *c); void view(const char *arg); +void viewprevtag(const char *arg); /* views previous selected tags */ int xerror(Display *dpy, XErrorEvent *ee); int xerrordummy(Display *dsply, XErrorEvent *ee); int xerrorstart(Display *dsply, XErrorEvent *ee); @@ -194,11 +195,8 @@ char stext[256]; double mwfact; int screen, sx, sy, sw, sh, wax, way, waw, wah; int (*xerrorxlib)(Display *, XErrorEvent *); -unsigned int bh, bpos, ntags; +unsigned int bh, bpos; unsigned int blw = 0; -unsigned int ltidx = 0; /* default */ -unsigned int nlayouts = 0; -unsigned int nrules = 0; unsigned int numlockmask = 0; void (*handler[LASTEvent]) (XEvent *) = { [ButtonPress] = buttonpress, @@ -215,9 +213,10 @@ void (*handler[LASTEvent]) (XEvent *) = { [UnmapNotify] = unmapnotify }; Atom wmatom[WMLast], netatom[NetLast]; +Bool domwfact = True; +Bool dozoom = True; Bool otherwm, readin; Bool running = True; -Bool *seltags; Bool selscreen = True; Client *clients = NULL; Client *sel = NULL; @@ -225,13 +224,14 @@ Client *stack = NULL; Cursor cursor[CurLast]; Display *dpy; DC dc = {0}; +Layout *layout = NULL; Window barwin, root; Regs *regs = NULL; /* configuration, allows nested code to access above variables */ #include "config.h" -/* functions*/ +/* function implementations */ void applyrules(Client *c) { static char buf[512]; @@ -245,10 +245,10 @@ applyrules(Client *c) { snprintf(buf, sizeof buf, "%s:%s:%s", ch.res_class ? ch.res_class : "", ch.res_name ? ch.res_name : "", c->name); - for(i = 0; i < nrules; i++) + for(i = 0; i < NRULES; i++) if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) { c->isfloating = rules[i].isfloating; - for(j = 0; regs[i].tagregex && j < ntags; j++) { + for(j = 0; regs[i].tagregex && j < NTAGS; j++) { if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) { matched = True; c->tags[j] = True; @@ -260,8 +260,7 @@ applyrules(Client *c) { if(ch.res_name) XFree(ch.res_name); if(!matched) - for(i = 0; i < ntags; i++) - c->tags[i] = seltags[i]; + memcpy(c->tags, seltags, sizeof seltags); } void @@ -273,7 +272,7 @@ arrange(void) { unban(c); else ban(c); - layouts[ltidx].arrange(); + layout->arrange(); focus(NULL); restack(); } @@ -308,7 +307,7 @@ buttonpress(XEvent *e) { if(barwin == ev->window) { x = 0; - for(i = 0; i < ntags; i++) { + for(i = 0; i < NTAGS; i++) { x += textw(tags[i]); if(ev->x < x) { if(ev->button == Button1) { @@ -334,20 +333,20 @@ buttonpress(XEvent *e) { if(CLEANMASK(ev->state) != MODKEY) return; if(ev->button == Button1) { - if(isarrange(floating) || c->isfloating) + if((floating == layout->arrange) || c->isfloating) restack(); else togglefloating(NULL); movemouse(c); } else if(ev->button == Button2) { - if(isarrange(tile) && !c->isfixed && c->isfloating) + if((floating != layout->arrange) && !c->isfixed && c->isfloating) togglefloating(NULL); else zoom(NULL); } else if(ev->button == Button3 && !c->isfixed) { - if(isarrange(floating) || c->isfloating) + if((floating == layout->arrange) || c->isfloating) restack(); else togglefloating(NULL); @@ -392,7 +391,6 @@ cleanup(void) { XFreeCursor(dpy, cursor[CurMove]); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XSync(dpy, False); - free(seltags); } void @@ -402,9 +400,8 @@ compileregs(void) { if(regs) return; - nrules = sizeof rules / sizeof rules[0]; - regs = emallocz(nrules * sizeof(Regs)); - for(i = 0; i < nrules; i++) { + regs = emallocz(NRULES * sizeof(Regs)); + for(i = 0; i < NRULES; i++) { if(rules[i].prop) { reg = emallocz(sizeof(regex_t)); if(regcomp(reg, rules[i].prop, REG_EXTENDED)) @@ -465,7 +462,7 @@ configurerequest(XEvent *e) { c->ismax = False; if(ev->value_mask & CWBorderWidth) c->border = ev->border_width; - if(c->isfixed || c->isfloating || isarrange(floating)) { + if(c->isfixed || c->isfloating || (floating == layout->arrange)) { if(ev->value_mask & CWX) c->x = ev->x; if(ev->value_mask & CWY) @@ -533,7 +530,7 @@ drawbar(void) { int i, x; dc.x = dc.y = 0; - for(i = 0; i < ntags; i++) { + for(i = 0; i < NTAGS; i++) { dc.w = textw(tags[i]); if(seltags[i]) { drawtext(tags[i], dc.sel); @@ -546,7 +543,7 @@ drawbar(void) { dc.x += dc.w; } dc.w = blw; - drawtext(layouts[ltidx].symbol, dc.norm); + drawtext(layout->symbol, dc.norm); x = dc.x + dc.w; dc.w = textw(stext); dc.x = sw - dc.w; @@ -667,7 +664,7 @@ void expose(XEvent *e) { XExposeEvent *ev = &e->xexpose; - if(ev->count == 0) { + if(0 == ev->count) { if(barwin == ev->window) drawbar(); } @@ -677,6 +674,7 @@ void floating(void) { /* default floating layout */ Client *c; + domwfact = dozoom = False; for(c = clients; c; c = c->next) if(isvisible(c)) resize(c, c->x, c->y, c->w, c->h, True); @@ -781,7 +779,7 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) { int n; XTextProperty name; - if(!text || size == 0) + if(!text || 0 == size) return False; text[0] = '\0'; XGetTextProperty(dpy, w, &name, atom); @@ -843,10 +841,8 @@ unsigned int idxoftag(const char *tag) { unsigned int i; - for(i = 0; i < ntags; i++) - if(tags[i] == tag) - return i; - return 0; + for(i = 0; (i < NTAGS) && (tags[i] != tag); i++); + return (i < NTAGS) ? i : 0; } void @@ -892,12 +888,6 @@ initfont(const char *fontstr) { } Bool -isarrange(void (*func)()) -{ - return func == layouts[ltidx].arrange; -} - -Bool isoccupied(unsigned int t) { Client *c; @@ -926,7 +916,7 @@ Bool isvisible(Client *c) { unsigned int i; - for(i = 0; i < ntags; i++) + for(i = 0; i < NTAGS; i++) if(c->tags[i] && seltags[i]) return True; return False; @@ -935,7 +925,6 @@ isvisible(Client *c) { void keypress(XEvent *e) { KEYS - unsigned int len = sizeof keys / sizeof keys[0]; unsigned int i; KeyCode code; KeySym keysym; @@ -943,7 +932,7 @@ keypress(XEvent *e) { if(!e) { /* grabkeys */ XUngrabKey(dpy, AnyKey, AnyModifier, root); - for(i = 0; i < len; i++) { + for(i = 0; i < NKEYS; i++) { code = XKeysymToKeycode(dpy, keys[i].keysym); XGrabKey(dpy, code, keys[i].mod, root, True, GrabModeAsync, GrabModeAsync); @@ -958,7 +947,7 @@ keypress(XEvent *e) { } ev = &e->xkey; keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); - for(i = 0; i < len; i++) + for(i = 0; i < NKEYS; i++) if(keysym == keys[i].keysym && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)) { @@ -998,14 +987,13 @@ leavenotify(XEvent *e) { void manage(Window w, XWindowAttributes *wa) { - unsigned int i; Client *c, *t = NULL; Window trans; Status rettrans; XWindowChanges wc; c = emallocz(sizeof(Client)); - c->tags = emallocz(ntags * sizeof(Bool)); + c->tags = emallocz(sizeof seltags); c->win = w; c->x = wa->x; c->y = wa->y; @@ -1040,8 +1028,7 @@ manage(Window w, XWindowAttributes *wa) { if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success)) for(t = clients; t && t->win != trans; t = t->next); if(t) - for(i = 0; i < ntags; i++) - c->tags[i] = t->tags[i]; + memcpy(c->tags, t->tags, sizeof seltags); applyrules(c); if(!c->isfloating) c->isfloating = (rettrans == Success) || c->isfixed; @@ -1138,7 +1125,7 @@ propertynotify(XEvent *e) { default: break; case XA_WM_TRANSIENT_FOR: XGetTransientForHint(dpy, c->win, &trans); - if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL))) + if(!c->isfloating && (c->isfloating = (NULL != getclient(trans)))) arrange(); break; case XA_WM_NORMAL_HINTS: @@ -1160,43 +1147,45 @@ quit(const char *arg) { void resize(Client *c, int x, int y, int w, int h, Bool sizehints) { - double dx, dy, max, min, ratio; - XWindowChanges wc; + XWindowChanges wc; if(sizehints) { - if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0) { - dx = (double)(w - c->basew); - dy = (double)(h - c->baseh); - min = (double)(c->minax) / (double)(c->minay); - max = (double)(c->maxax) / (double)(c->maxay); - ratio = dx / dy; - if(max > 0 && min > 0 && ratio > 0) { - if(ratio < min) { - dy = (dx * min + dy) / (min * min + 1); - dx = dy * min; - w = (int)dx + c->basew; - h = (int)dy + c->baseh; - } - else if(ratio > max) { - dy = (dx * min + dy) / (max * max + 1); - dx = dy * min; - w = (int)dx + c->basew; - h = (int)dy + c->baseh; - } - } + /* set minimum possible */ + if (w < 1) + w = 1; + if (h < 1) + h = 1; + + /* 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) + w = h * c->maxax / c->maxay; + else if (w * c->minay < h * c->minax) + h = w * c->minay / c->minax; } - if(c->minw && w < c->minw) + + /* adjust for increment value */ + if(c->incw) + w -= w % c->incw; + if(c->inch) + h -= h % c->inch; + + /* restore base dimensions */ + w += c->basew; + h += c->baseh; + + if(c->minw > 0 && w < c->minw) w = c->minw; - if(c->minh && h < c->minh) + if(c->minh > 0 && h < c->minh) h = c->minh; - if(c->maxw && w > c->maxw) + if(c->maxw > 0 && w > c->maxw) w = c->maxw; - if(c->maxh && h > c->maxh) + if(c->maxh > 0 && h > c->maxh) h = c->maxh; - if(c->incw) - w -= (w - c->basew) % c->incw; - if(c->inch) - h -= (h - c->baseh) % c->inch; } if(w <= 0 || h <= 0) return; @@ -1269,9 +1258,9 @@ restack(void) { drawbar(); if(!sel) return; - if(sel->isfloating || isarrange(floating)) + if(sel->isfloating || (floating == layout->arrange)) XRaiseWindow(dpy, sel->win); - if(!isarrange(floating)) { + if(floating != layout->arrange) { wc.stack_mode = Below; wc.sibling = barwin; if(!sel->isfloating) { @@ -1377,16 +1366,16 @@ setlayout(const char *arg) { unsigned int i; if(!arg) { - if(++ltidx == nlayouts) - ltidx = 0;; + if(++layout == &layouts[NLAYOUTS]) + layout = &layouts[0]; } else { - for(i = 0; i < nlayouts; i++) + for(i = 0; i < NLAYOUTS; i++) if(!strcmp(arg, layouts[i].symbol)) break; - if(i == nlayouts) + if(i == NLAYOUTS) return; - ltidx = i; + layout = &layouts[i]; } if(sel) arrange(); @@ -1398,10 +1387,10 @@ void setmwfact(const char *arg) { double delta; - if(!isarrange(tile)) + if(!domwfact) return; /* arg handling, manipulate mwfact */ - if(arg == NULL) + if(NULL == arg) mwfact = MWFACT; else if(1 == sscanf(arg, "%lf", &delta)) { if(arg[0] == '+' || arg[0] == '-') @@ -1418,6 +1407,7 @@ setmwfact(const char *arg) { void setup(void) { + int d; unsigned int i, j, mask; Window w; XModifierKeymap *modmap; @@ -1465,9 +1455,6 @@ setup(void) { /* init tags */ compileregs(); - for(ntags = 0; tags[ntags]; ntags++); - seltags = emallocz(sizeof(Bool) * ntags); - seltags[0] = True; /* init appearance */ dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR); @@ -1481,8 +1468,8 @@ setup(void) { /* init layouts */ mwfact = MWFACT; - nlayouts = sizeof layouts / sizeof layouts[0]; - for(blw = i = 0; i < nlayouts; i++) { + layout = &layouts[0]; + for(blw = i = 0; i < NLAYOUTS; i++) { j = textw(layouts[i].symbol); if(j > blw) blw = j; @@ -1507,7 +1494,7 @@ setup(void) { XSetFont(dpy, dc.gc, dc.font.xfont->fid); /* multihead support */ - selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); + selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask); } void @@ -1520,8 +1507,8 @@ spawn(const char *arg) { return; /* The double-fork construct avoids zombie processes and keeps the code * clean from stupid signal handlers. */ - if(fork() == 0) { - if(fork() == 0) { + if(0 == fork()) { + if(0 == fork()) { if(dpy) close(ConnectionNumber(dpy)); setsid(); @@ -1540,11 +1527,9 @@ tag(const char *arg) { if(!sel) return; - for(i = 0; i < ntags; i++) - sel->tags[i] = arg == NULL; - i = idxoftag(arg); - if(i >= 0 && i < ntags) - sel->tags[i] = True; + for(i = 0; i < NTAGS; i++) + sel->tags[i] = (NULL == arg); + sel->tags[idxoftag(arg)] = True; arrange(); } @@ -1567,8 +1552,9 @@ textw(const char *text) { void tile(void) { unsigned int i, n, nx, ny, nw, nh, mw, th; - Client *c; + Client *c, *mc; + domwfact = dozoom = True; for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next)) n++; @@ -1580,26 +1566,30 @@ tile(void) { nx = wax; ny = way; - for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) { + nw = 0; /* gcc stupidity requires this */ + for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) { c->ismax = False; - if(i == 0) { /* master */ + if(0 == i) { /* master */ nw = mw - 2 * c->border; nh = wah - 2 * c->border; } else { /* tile window */ if(i == 1) { ny = way; - nx += mw; + nx += mc->w + 2 * mc->border; + nw = waw - nx - 2 * c->border; } - nw = waw - mw - 2 * c->border; if(i + 1 == n) /* remainder */ nh = (way + wah) - ny - 2 * c->border; else nh = th - 2 * c->border; } - resize(c, nx, ny, nw, nh, RESIZEHINTS); + resize(c, nx, ny, nw, nh, True); + if((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)) + /* client doesn't accept size constraints */ + resize(c, nx, ny, nw, nh, False); if(n > 1 && th != wah) - ny += nh + 2 * c->border; + ny = c->y + c->h + 2 * c->border; } } @@ -1630,7 +1620,7 @@ togglemax(const char *arg) { if(!sel || sel->isfixed) return; if((sel->ismax = !sel->ismax)) { - if(isarrange(floating) || sel->isfloating) + if((floating == layout->arrange) || sel->isfloating) sel->wasfloating = True; else { togglefloating(NULL); @@ -1659,9 +1649,9 @@ toggletag(const char *arg) { return; i = idxoftag(arg); sel->tags[i] = !sel->tags[i]; - for(j = 0; j < ntags && !sel->tags[j]; j++); - if(j == ntags) - sel->tags[i] = True; + for(j = 0; j < NTAGS && !sel->tags[j]; j++); + if(j == NTAGS) + sel->tags[i] = True; /* at least one tag must be enabled */ arrange(); } @@ -1671,9 +1661,9 @@ toggleview(const char *arg) { i = idxoftag(arg); seltags[i] = !seltags[i]; - for(j = 0; j < ntags && !seltags[j]; j++); - if(j == ntags) - seltags[i] = True; /* cannot toggle last view */ + for(j = 0; j < NTAGS && !seltags[j]; j++); + if(j == NTAGS) + seltags[i] = True; /* at least one tag must be viewed */ arrange(); } @@ -1837,11 +1827,20 @@ void view(const char *arg) { unsigned int i; - for(i = 0; i < ntags; i++) - seltags[i] = arg == NULL; - i = idxoftag(arg); - if(i >= 0 && i < ntags) - seltags[i] = True; + memcpy(prevtags, seltags, sizeof seltags); + for(i = 0; i < NTAGS; i++) + seltags[i] = (NULL == arg); + seltags[idxoftag(arg)] = True; + arrange(); +} + +void +viewprevtag(const char *arg) { + static Bool tmptags[sizeof tags / sizeof tags[0]]; + + memcpy(tmptags, seltags, sizeof seltags); + memcpy(seltags, prevtags, sizeof seltags); + memcpy(prevtags, tmptags, sizeof seltags); arrange(); } @@ -1849,7 +1848,7 @@ void zoom(const char *arg) { Client *c; - if(!sel || !isarrange(tile) || sel->isfloating) + if(!sel || !dozoom || sel->isfloating) return; if((c = sel) == nexttiled(clients)) if(!(c = nexttiled(c->next)))