X-Git-Url: https://git.armaanb.net/?p=dwm.git;a=blobdiff_plain;f=dwm.c;h=2a8696b1b6c468a6abde03bebc49d370105ae99f;hp=2202d3b90be4c9ec6eadc722152eb7af1e172972;hb=HEAD;hpb=dd94cb00e5b1d42ac672cb838a01d085c73c14e7 diff --git a/dwm.c b/dwm.c index 2202d3b..2a8696b 100644 --- a/dwm.c +++ b/dwm.c @@ -112,6 +112,7 @@ typedef struct { void (*arrange)(Monitor *); } Layout; +typedef struct Pertag Pertag; struct Monitor { char ltsymbol[16]; float mfact; @@ -131,6 +132,7 @@ struct Monitor { Monitor *next; Window barwin; const Layout *lt[2]; + Pertag *pertag; }; typedef struct { @@ -167,6 +169,7 @@ static void drawbar(Monitor *m); static void drawbars(void); static void enternotify(XEvent *e); static void expose(XEvent *e); +static Client *findbefore(Client *c); static void focus(Client *c); static void focusin(XEvent *e); static void focusmon(const Arg *arg); @@ -187,8 +190,10 @@ 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 Client *prevtiled(Client *c); static void propertynotify(XEvent *e); +static void pushdown(const Arg *arg); +static void pushup(const Arg *arg); 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, int interact); @@ -238,6 +243,7 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); /* variables */ +static Client *prevzoom = NULL; static const char broken[] = "broken"; static char stext[256]; static int screen; @@ -274,9 +280,46 @@ static Window root, wmcheckwin; /* configuration, allows nested code to access above variables */ #include "config.h" +struct Pertag { + unsigned int curtag, prevtag; /* current and previous tag */ + int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */ + float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */ + unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */ + const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */ + int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */ +}; + /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; +/* dwm will keep pid's of processes from autostart array and kill them at quit */ +static pid_t *autostart_pids; +static size_t autostart_len; + +/* execute command from autostart array */ +static void +autostart_exec() { + const char *const *p; + size_t i = 0; + + /* count entries */ + for (p = autostart; *p; autostart_len++, p++) + while (*++p); + + autostart_pids = malloc(autostart_len * sizeof(pid_t)); + for (p = autostart; *p; i++, p++) { + if ((autostart_pids[i] = fork()) == 0) { + setsid(); + execvp(*p, (char *const *)p); + fprintf(stderr, "dwm: execvp %s\n", *p); + perror(" failed"); + _exit(EXIT_FAILURE); + } + /* skip arguments */ + while (*++p); + } +} + /* function implementations */ void applyrules(Client *c) @@ -639,6 +682,7 @@ Monitor * createmon(void) { Monitor *m; + unsigned int i; m = ecalloc(1, sizeof(Monitor)); m->tagset[0] = m->tagset[1] = 1; @@ -649,6 +693,20 @@ createmon(void) m->lt[0] = &layouts[0]; m->lt[1] = &layouts[1 % LENGTH(layouts)]; strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); + m->pertag = ecalloc(1, sizeof(Pertag)); + m->pertag->curtag = m->pertag->prevtag = 1; + + for (i = 0; i <= LENGTH(tags); i++) { + m->pertag->nmasters[i] = m->nmaster; + m->pertag->mfacts[i] = m->mfact; + + m->pertag->ltidxs[i][0] = m->lt[0]; + m->pertag->ltidxs[i][1] = m->lt[1]; + m->pertag->sellts[i] = m->sellt; + + m->pertag->showbars[i] = m->showbar; + } + return m; } @@ -738,7 +796,7 @@ drawbar(Monitor *m) if ((w = m->ww - tw - x) > bh) { if (m->sel) { - drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]); + drw_setscheme(drw, scheme[m == selmon ? SchemeNorm : SchemeSel]); drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); if (m->sel->isfloating) drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); @@ -788,6 +846,16 @@ expose(XEvent *e) drawbar(m); } +Client * +findbefore(Client *c) +{ + Client *tmp; + if (c == selmon->clients) + return NULL; + for (tmp = selmon->clients; tmp && tmp->next != c; tmp = tmp->next); + return tmp; +} + void focus(Client *c) { @@ -835,6 +903,8 @@ focusmon(const Arg *arg) unfocus(selmon->sel, 0); selmon = m; focus(NULL); + if (selmon->sel) + XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2); } void @@ -860,6 +930,7 @@ focusstack(const Arg *arg) if (c) { focus(c); restack(selmon); + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2); } } @@ -974,7 +1045,7 @@ grabkeys(void) void incnmaster(const Arg *arg) { - selmon->nmaster = MAX(selmon->nmaster + arg->i, 0); + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0); arrange(selmon); } @@ -1205,13 +1276,14 @@ nexttiled(Client *c) return c; } -void -pop(Client *c) -{ - detach(c); - attach(c); - focus(c); - arrange(c->mon); +Client * +prevtiled(Client *c) { + Client *p, *r; + + for(p = selmon->clients, r = NULL; p && p != c; p = p->next) + if(!p->isfloating && ISVISIBLE(p)) + r = p; + return r; } void @@ -1251,9 +1323,62 @@ propertynotify(XEvent *e) } } +void +pushdown(const Arg *arg) { + Client *sel = selmon->sel, *c; + + if(!sel || sel->isfloating) + return; + if((c = nexttiled(sel->next))) { + detach(sel); + sel->next = c->next; + c->next = sel; + } else { + detach(sel); + attach(sel); + } + focus(sel); + arrange(selmon); +} + +void +pushup(const Arg *arg) { + Client *sel = selmon->sel, *c; + + if(!sel || sel->isfloating) + return; + if((c = prevtiled(sel))) { + detach(sel); + sel->next = c; + if(selmon->clients == c) + selmon->clients = sel; + else { + for(c = selmon->clients; c->next != sel->next; c = c->next); + c->next = sel; + } + } else { + for(c = sel; c->next; c = c->next); + detach(sel); + sel->next = NULL; + c->next = sel; + } + focus(sel); + arrange(selmon); +} + void quit(const Arg *arg) { + size_t i; + + /* kill child processes */ + for (i = 0; i < autostart_len; i++) { + if (0 < autostart_pids[i]) { + kill(autostart_pids[i], SIGTERM); + waitpid(autostart_pids[i], NULL, 0); + } + } + running = 0; } @@ -1297,9 +1422,14 @@ void resizemouse(const Arg *arg) { int ocx, ocy, nw, nh; + int ocx2, ocy2, nx, ny; Client *c; Monitor *m; XEvent ev; + int horizcorner, vertcorner; + int di; + unsigned int dui; + Window dummy; Time lasttime = 0; if (!(c = selmon->sel)) @@ -1307,10 +1437,18 @@ resizemouse(const Arg *arg) restack(selmon); ocx = c->x; ocy = c->y; + ocx2 = c->x + c->w; + ocy2 = c->y + c->h; if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) return; - XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); + if (!XQueryPointer (dpy, c->win, &dummy, &dummy, &di, &di, &nx, &ny, &dui)) + return; + horizcorner = nx < c->w / 2; + vertcorner = ny < c->h / 2; + XWarpPointer (dpy, None, c->win, 0, 0, 0, 0, + horizcorner ? (-c->bw) : (c->w + c->bw -1), + vertcorner ? (-c->bw) : (c->h + c->bw -1)); do { XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); switch(ev.type) { @@ -1326,6 +1464,11 @@ resizemouse(const Arg *arg) nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); + nx = horizcorner ? ev.xmotion.x : c->x; + ny = vertcorner ? ev.xmotion.y : c->y; + nw = MAX(horizcorner ? (ocx2 - nx) : (ev.xmotion.x - ocx - 2 * c->bw + 1), 1); + nh = MAX(vertcorner ? (ocy2 - ny) : (ev.xmotion.y - ocy - 2 * c->bw + 1), 1); + if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh) { @@ -1334,11 +1477,13 @@ resizemouse(const Arg *arg) togglefloating(NULL); } if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) - resize(c, c->x, c->y, nw, nh, 1); + resize(c, nx, ny, nw, nh, 1); break; } } while (ev.type != ButtonRelease); - XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, + horizcorner ? (-c->bw) : (c->w + c->bw - 1), + vertcorner ? (-c->bw) : (c->h + c->bw - 1)); XUngrabPointer(dpy, CurrentTime); while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { @@ -1491,9 +1636,9 @@ void setlayout(const Arg *arg) { if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) - selmon->sellt ^= 1; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1; if (arg && arg->v) - selmon->lt[selmon->sellt] = (Layout *)arg->v; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v; strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); if (selmon->sel) arrange(selmon); @@ -1512,7 +1657,7 @@ setmfact(const Arg *arg) f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; if (f < 0.05 || f > 0.95) return; - selmon->mfact = f; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f; arrange(selmon); } @@ -1621,9 +1766,25 @@ showhide(Client *c) void sigchld(int unused) { + pid_t pid; + if (signal(SIGCHLD, sigchld) == SIG_ERR) die("can't install SIGCHLD handler:"); - while (0 < waitpid(-1, NULL, WNOHANG)); + while (0 < (pid = waitpid(-1, NULL, WNOHANG))) { + pid_t *p, *lim; + + if (!(p = autostart_pids)) + continue; + lim = &p[autostart_len]; + + for (; p < lim; p++) { + if (*p == pid) { + *p = -1; + break; + } + } + + } } void @@ -1691,7 +1852,7 @@ tile(Monitor *m) void togglebar(const Arg *arg) { - selmon->showbar = !selmon->showbar; + selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar; updatebarpos(selmon); XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); arrange(selmon); @@ -1728,9 +1889,33 @@ void toggleview(const Arg *arg) { unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); + int i; if (newtagset) { selmon->tagset[selmon->seltags] = newtagset; + + if (newtagset == ~0) { + selmon->pertag->prevtag = selmon->pertag->curtag; + selmon->pertag->curtag = 0; + } + + /* test if the user did not select the same tag */ + if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) { + selmon->pertag->prevtag = selmon->pertag->curtag; + for (i = 0; !(newtagset & 1 << i); i++) ; + selmon->pertag->curtag = i + 1; + } + + /* apply settings for this view */ + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; + selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + + if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) + togglebar(NULL); + focus(NULL); arrange(selmon); } @@ -2026,11 +2211,37 @@ updatewmhints(Client *c) void view(const Arg *arg) { + int i; + unsigned int tmptag; + if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) return; selmon->seltags ^= 1; /* toggle sel tagset */ - if (arg->ui & TAGMASK) + if (arg->ui & TAGMASK) { selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; + selmon->pertag->prevtag = selmon->pertag->curtag; + + if (arg->ui == ~0) + selmon->pertag->curtag = 0; + else { + for (i = 0; !(arg->ui & 1 << i); i++) ; + selmon->pertag->curtag = i + 1; + } + } else { + tmptag = selmon->pertag->prevtag; + selmon->pertag->prevtag = selmon->pertag->curtag; + selmon->pertag->curtag = tmptag; + } + + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; + selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + + if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) + togglebar(NULL); + focus(NULL); arrange(selmon); } @@ -2105,14 +2316,38 @@ void zoom(const Arg *arg) { Client *c = selmon->sel; + Client *at = NULL, *cold, *cprevious = NULL; if (!selmon->lt[selmon->sellt]->arrange || (selmon->sel && selmon->sel->isfloating)) return; - if (c == nexttiled(selmon->clients)) - if (!c || !(c = nexttiled(c->next))) - return; - pop(c); + if (c == nexttiled(selmon->clients)) { + at = findbefore(prevzoom); + if (at) + cprevious = nexttiled(at->next); + if (!cprevious || cprevious != prevzoom) { + prevzoom = NULL; + if (!c || !(c = nexttiled(c->next))) + return; + } else + c = cprevious; + } + cold = nexttiled(selmon->clients); + if (c != cold && !at) + at = findbefore(c); + detach(c); + attach(c); + /* swap windows instead of pushing the previous one down */ + if (c != cold && at) { + prevzoom = cold; + if (cold && at != cold) { + detach(cold); + cold->next = at->next; + at->next = cold; + } + } + focus(c); + arrange(c->mon); } int @@ -2127,6 +2362,7 @@ main(int argc, char *argv[]) if (!(dpy = XOpenDisplay(NULL))) die("dwm: cannot open display"); checkotherwm(); + autostart_exec(); setup(); #ifdef __OpenBSD__ if (pledge("stdio rpath proc exec", NULL) == -1)