X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=dwm.c;h=56ca615bbcc53594c7992c79b660132ddd83c3fa;hb=5602f44b29b5c9a9b66b012b34f5749929c5cd31;hp=c1749941faf2df7d110804a3c76cadb0dcfee2e7;hpb=5fa559dbfc6238a911c210ae4124586c6886df23;p=dwm.git diff --git a/dwm.c b/dwm.c index c174994..56ca615 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]) @@ -66,6 +68,7 @@ typedef struct Client Client; struct Client { char name[256]; int x, y, w, h; + int fx, fy, fw, fh; int basew, baseh, incw, inch, maxw, maxh, minw, minh; int minax, maxax, minay, maxay; long flags; @@ -109,7 +112,7 @@ typedef struct { const char *symbol; void (*arrange)(void); Bool isfloating; -} Layout; +} Layout; typedef struct { const char *class; @@ -142,7 +145,6 @@ void *emallocz(unsigned int size); void enternotify(XEvent *e); void eprint(const char *errstr, ...); void expose(XEvent *e); -void floating(void); /* default floating layout */ void focus(Client *c); void focusin(XEvent *e); void focusnext(const char *arg); @@ -158,7 +160,7 @@ void initfont(const char *fontstr); Bool isoccupied(unsigned int t); Bool isprotodel(Client *c); Bool isurgent(unsigned int t); -Bool isvisible(Client *c); +Bool isvisible(Client *c, Bool *cmp); void keypress(XEvent *e); void killclient(const char *arg); void manage(Window w, XWindowAttributes *wa); @@ -208,10 +210,11 @@ int xerrorstart(Display *dpy, XErrorEvent *ee); void zoom(const char *arg); /* variables */ -char stext[256], buf[256]; +char stext[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; +int seltags = 0; double mfact; unsigned int numlockmask = 0; void (*handler[LASTEvent]) (XEvent *) = { @@ -231,22 +234,22 @@ void (*handler[LASTEvent]) (XEvent *) = { Atom wmatom[WMLast], netatom[NetLast]; Bool otherwm, readin; Bool running = True; -Bool *prevtags; -Bool *seltags; +Bool *tagset[2]; Client *clients = NULL; Client *sel = NULL; Client *stack = NULL; Cursor cursor[CurLast]; Display *dpy; DC dc = {0}; +Geom geoms[]; +Geom *geom = geoms; +Layout layouts[]; +Layout *lt = layouts; Window root, barwin; /* configuration, allows nested code to access above variables */ #include "config.h" #define TAGSZ (LENGTH(tags) * sizeof(Bool)) -Bool tmp[LENGTH(tags)]; -Layout *lt = layouts; -Geom *geom = geoms; /* function implementations */ @@ -276,7 +279,7 @@ applyrules(Client *c) { if(ch.res_name) XFree(ch.res_name); if(!matched) - memcpy(c->tags, seltags, TAGSZ); + memcpy(c->tags, tagset[seltags], TAGSZ); } void @@ -284,13 +287,17 @@ arrange(void) { Client *c; for(c = clients; c; c = c->next) - if(isvisible(c)) + if(isvisible(c, NULL)) { unban(c); + if(lt->isfloating || c->isfloating) + resize(c, c->fx, c->fy, c->fw, c->fh, True); + } else ban(c); focus(NULL); - lt->arrange(); + if(lt->arrange) + lt->arrange(); restack(); } @@ -358,7 +365,7 @@ buttonpress(XEvent *e) { movemouse(c); } else if(ev->button == Button2) { - if((floating != lt->arrange) && c->isfloating) + if(!lt->isfloating && c->isfloating) togglefloating(NULL); else zoom(NULL); @@ -462,7 +469,7 @@ configurerequest(XEvent *e) { if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) configure(c); - if(isvisible(c)) + if(isvisible(c, NULL)) XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); } else @@ -529,10 +536,10 @@ drawbar(void) { drawtext(geom->symbol, dc.norm, False); dc.x += bgw; } - for(c = stack; c && !isvisible(c); c = c->snext); + for(c = stack; c && !isvisible(c, NULL); c = c->snext); for(i = 0; i < LENGTH(tags); i++) { dc.w = textw(tags[i]); - if(seltags[i]) { + if(tagset[seltags][i]) { drawtext(tags[i], dc.sel, isurgent(i)); drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.sel); } @@ -595,23 +602,23 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) { int x, y, w, h; unsigned int 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; - w = 0; - olen = len = strlen(text); - if(len >= sizeof buf) - len = sizeof buf - 1; + olen = strlen(text); + len = MIN(olen, sizeof buf); memcpy(buf, text, len); - buf[len] = 0; + w = 0; 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 */ - while(len && (w = textnw(buf, len)) > dc.w - h) - buf[--len] = 0; + for(; len && (w = textnw(buf, len)) > dc.w - h; len--); + if (!len) + return; if(len < olen) { if(len > 1) buf[len - 1] = '.'; @@ -620,8 +627,6 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) { if(len > 3) buf[len - 3] = '.'; } - if(w > dc.w) - return; /* too long */ XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]); if(dc.font.set) XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); @@ -670,18 +675,9 @@ expose(XEvent *e) { } void -floating(void) { /* default floating layout */ - Client *c; - - for(c = clients; c; c = c->next) - if(isvisible(c)) - resize(c, c->x, c->y, c->w, c->h, True); -} - -void focus(Client *c) { - if(!c || (c && !isvisible(c))) - for(c = stack; c && !isvisible(c); c = c->snext); + if(!c || (c && !isvisible(c, NULL))) + for(c = stack; c && !isvisible(c, NULL); c = c->snext); if(sel && sel != c) { grabbuttons(sel, False); XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]); @@ -715,9 +711,9 @@ focusnext(const char *arg) { if(!sel) return; - for(c = sel->next; c && !isvisible(c); c = c->next); + for(c = sel->next; c && !isvisible(c, arg ? sel->tags : NULL); c = c->next); if(!c) - for(c = clients; c && !isvisible(c); c = c->next); + for(c = clients; c && !isvisible(c, arg ? sel->tags : NULL); c = c->next); if(c) { focus(c); restack(); @@ -730,10 +726,10 @@ focusprev(const char *arg) { if(!sel) return; - for(c = sel->prev; c && !isvisible(c); c = c->prev); + for(c = sel->prev; c && !isvisible(c, arg ? sel->tags : NULL); c = c->prev); if(!c) { for(c = clients; c && c->next; c = c->next); - for(; c && !isvisible(c); c = c->prev); + for(; c && !isvisible(c, arg ? sel->tags : NULL); c = c->prev); } if(c) { focus(c); @@ -880,10 +876,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++; } } @@ -936,11 +930,13 @@ isurgent(unsigned int t) { } Bool -isvisible(Client *c) { +isvisible(Client *c, Bool *cmp) { unsigned int i; + if(!cmp) + cmp = tagset[seltags]; for(i = 0; i < LENGTH(tags); i++) - if(c->tags[i] && seltags[i]) + if(c->tags[i] && cmp[i]) return True; return False; } @@ -995,8 +991,8 @@ manage(Window w, XWindowAttributes *wa) { /* geometry */ c->x = wa->x; c->y = wa->y; - c->w = wa->width; - c->h = wa->height; + c->w = c->fw = wa->width; + c->h = c->fh = wa->height; c->oldbw = wa->border_width; if(c->w == sw && c->h == sh) { c->x = sx; @@ -1008,12 +1004,12 @@ 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; } + c->fx = c->x; + c->fy = c->y; wc.border_width = c->bw; XConfigureWindow(dpy, w, CWBorderWidth, &wc); @@ -1067,7 +1063,7 @@ monocle(void) { Client *c; for(c = clients; c; c = c->next) - if((lt->isfloating || !c->isfloating) && isvisible(c)) + if((lt->isfloating || !c->isfloating) && isvisible(c, NULL)) resize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw, RESIZEHINTS); } @@ -1109,8 +1105,11 @@ movemouse(Client *c) { ny = wy + wh - c->h - 2 * c->bw; if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP)) togglefloating(NULL); - if((lt->isfloating) || c->isfloating) + if(lt->isfloating || c->isfloating) { + c->fx = nx; + c->fy = ny; resize(c, nx, ny, c->w, c->h, False); + } break; } } @@ -1118,7 +1117,7 @@ movemouse(Client *c) { Client * nexttiled(Client *c) { - for(; c && (c->isfloating || !isvisible(c)); c = c->next); + for(; c && (c->isfloating || !isvisible(c, NULL)); c = c->next); return c; } @@ -1161,11 +1160,10 @@ quit(const char *arg) { void reapply(const char *arg) { - static Bool zerotags[LENGTH(tags)] = { 0 }; Client *c; for(c = clients; c; c = c->next) { - memcpy(c->tags, zerotags, sizeof zerotags); + memset(c->tags, 0, TAGSZ); applyrules(c); } arrange(); @@ -1177,10 +1175,8 @@ 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; @@ -1206,14 +1202,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; @@ -1266,14 +1262,18 @@ 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; - if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP)) + 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)) { + c->fx = c->x; + c->fy = c->y; togglefloating(NULL); - if((lt->isfloating) || c->isfloating) + } + if((lt->isfloating) || c->isfloating) { resize(c, c->x, c->y, nw, nh, True); + c->fw = nw; + c->fh = nh; + } break; } } @@ -1293,16 +1293,11 @@ restack(void) { if(!lt->isfloating) { wc.stack_mode = Below; wc.sibling = barwin; - if(!sel->isfloating) { - XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc); - wc.sibling = sel->win; - } - for(c = nexttiled(clients); c; c = nexttiled(c->next)) { - if(c == sel) - continue; - XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc); - wc.sibling = c->win; - } + for(c = stack; c; c = c->snext) + if(!c->isfloating && isvisible(c, NULL)) { + XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc); + wc.sibling = c->win; + } } XSync(dpy, False); while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); @@ -1513,20 +1508,18 @@ setup(void) { XSetFont(dpy, dc.gc, dc.font.xfont->fid); /* init tags */ - seltags = emallocz(TAGSZ); - prevtags = emallocz(TAGSZ); - seltags[0] = prevtags[0] = True; + tagset[0] = emallocz(TAGSZ); + tagset[1] = emallocz(TAGSZ); + tagset[0][0] = tagset[1][0] = True; /* 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; @@ -1710,10 +1703,10 @@ toggleview(const char *arg) { unsigned int i, j; i = idxoftag(arg); - seltags[i] = !seltags[i]; - for(j = 0; j < LENGTH(tags) && !seltags[j]; j++); + tagset[seltags][i] = !tagset[seltags][i]; + for(j = 0; j < LENGTH(tags) && !tagset[seltags][j]; j++); if(j == LENGTH(tags)) - seltags[i] = True; /* at least one tag must be viewed */ + tagset[seltags][i] = True; /* at least one tag must be viewed */ arrange(); } @@ -1837,28 +1830,17 @@ updatewmhints(Client *c) { } } - void view(const char *arg) { - unsigned int i; - - for(i = 0; i < LENGTH(tags); i++) - tmp[i] = (NULL == arg); - tmp[idxoftag(arg)] = True; - - if(memcmp(seltags, tmp, TAGSZ) != 0) { - memcpy(prevtags, seltags, TAGSZ); - memcpy(seltags, tmp, TAGSZ); - arrange(); - } + seltags ^= 1; + memset(tagset[seltags], (NULL == arg), TAGSZ); + tagset[seltags][idxoftag(arg)] = True; + arrange(); } void viewprevtag(const char *arg) { - - memcpy(tmp, seltags, TAGSZ); - memcpy(seltags, prevtags, TAGSZ); - memcpy(prevtags, tmp, TAGSZ); + seltags ^= 1; /* toggle sel tagset */ arrange(); } @@ -1873,6 +1855,7 @@ xerror(Display *dpy, XErrorEvent *ee) { || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) + || (ee->request_code == X_GrabButton && ee->error_code == BadAccess) || (ee->request_code == X_GrabKey && ee->error_code == BadAccess) || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) return 0; @@ -1898,14 +1881,14 @@ void zoom(const char *arg) { Client *c = sel; - if(!sel || lt->isfloating || sel->isfloating) - return; if(c == nexttiled(clients)) - if(!(c = nexttiled(c->next))) + if(!c || !(c = nexttiled(c->next))) return; - detach(c); - attach(c); - focus(c); + if(!lt->isfloating && !sel->isfloating) { + detach(c); + attach(c); + focus(c); + } arrange(); }