]> git.armaanb.net Git - dwm.git/blobdiff - dwm.c
Update
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 4cdc1f777f078c8d3a6f413cef27d8186f95ecc5..2a8696b1b6c468a6abde03bebc49d370105ae99f 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -292,6 +292,34 @@ struct Pertag {
 /* 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)
@@ -768,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);
@@ -875,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
@@ -900,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);
        }
 }
 
@@ -1338,6 +1369,16 @@ pushup(const Arg *arg) {
 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;
 }
 
@@ -1381,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))
@@ -1391,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) {
@@ -1410,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)
                        {
@@ -1418,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) {
@@ -1705,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
@@ -2285,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)