]> git.armaanb.net Git - dwm.git/commitdiff
Apply cool-autostart patch
authorArmaan Bhojwani <me@armaanb.net>
Wed, 23 Jun 2021 15:31:15 +0000 (11:31 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Wed, 23 Jun 2021 17:55:10 +0000 (13:55 -0400)
config.h
dwm.c

index 493ab8d7d840d72b256d65074cb20993e54510ae..97cf4b0300717c48e3720be0d948d6fd60095e20 100644 (file)
--- a/config.h
+++ b/config.h
@@ -142,3 +142,12 @@ static Button buttons[] = {
        { ClkTagBar,            MODKEY,         Button3,        toggletag,      {0} },
 };
 
+static const char *const autostart[] = {
+       "dwmblocks", NULL,
+       "xmodmap", "/home/armaa/.config/xmodmap", NULL,
+       "xcape", NULL,
+       "redshift", "-l", "42.4:-71.3", "-t", "6500:2500", NULL,
+       "emacs", "--daemon", NULL,
+       "syncthing", NULL,
+       NULL /* terminate */
+};
diff --git a/dwm.c b/dwm.c
index 4cdc1f777f078c8d3a6f413cef27d8186f95ecc5..166e19c4ae3a890dc09330ebff580da7ee280d9e 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)
@@ -1338,6 +1366,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;
 }
 
@@ -1705,9 +1743,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 +2339,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)