diff --git a/README.md b/README.md index b7aabdc..8f9dc15 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Refer to [https://dwm.suckless.org/](https://dwm.suckless.org/) for details on t ### Changelog: +2020-08-10 - Added cool autostart patch + 2020-08-02 - Added reorganizetags patch 2020-07-19 - Added barmodules patch - making extrabar, leftlayout, staticstatus and statusallmons patches redundant, added powerline patch @@ -199,6 +201,10 @@ Refer to [https://dwm.suckless.org/](https://dwm.suckless.org/) for details on t - [combo](https://dwm.suckless.org/patches/combo/) - allows you to select multiple tags by pressing all the right keys as a combo, e.g. hold MOD and press and hold 1 and 3 together to view those two tags + - [cool_autostart](https://dwm.suckless.org/patches/cool_autostart/) + - allows dwm to execute commands from an array in the config.h file + - when dwm exits all processes from the autostart array will be killed automatically + - [cyclelayouts](https://dwm.suckless.org/patches/cyclelayouts/) - lets you cycle through all your layouts diff --git a/config.def.h b/config.def.h index 86118ce..76b9d01 100644 --- a/config.def.h +++ b/config.def.h @@ -302,6 +302,13 @@ char *statuscolors[][ColCount] = { }; #endif // BAR_POWERLINE_STATUS_PATCH +#if COOL_AUTOSTART_PATCH +static const char *const autostart[] = { + "st", NULL, + NULL /* terminate */ +}; +#endif // COOL_AUTOSTART_PATCH + #if SCRATCHPADS_PATCH const char *spcmd1[] = {"st", "-n", "spterm", "-g", "120x34", NULL }; const char *spcmd2[] = {"st", "-n", "spfm", "-g", "144x41", "-e", "ranger", NULL }; diff --git a/dwm.c b/dwm.c index d745ee1..296490f 100644 --- a/dwm.c +++ b/dwm.c @@ -2274,13 +2274,21 @@ propertynotify(XEvent *e) void quit(const Arg *arg) { + #if COOL_AUTOSTART_PATCH + size_t i; + #endif // COOL_AUTOSTART_PATCH #if ONLYQUITONEMPTY_PATCH unsigned int n; Window *junk = malloc(1); XQueryTree(dpy, root, junk, junk, &junk, &n); - if (n <= quit_empty_window_count) { + #if COOL_AUTOSTART_PATCH + if (n - autostart_len <= quit_empty_window_count) + #else + if (n <= quit_empty_window_count) + #endif // COOL_AUTOSTART_PATCH + { #if RESTARTSIG_PATCH if (arg->i) restart = 1; @@ -2298,6 +2306,16 @@ quit(const Arg *arg) #endif // RESTARTSIG_PATCH running = 0; #endif // ONLYQUITONEMPTY_PATCH + + #if COOL_AUTOSTART_PATCH + /* 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); + } + } + #endif // COOL_AUTOSTART_PATCH } Monitor * @@ -3031,9 +3049,29 @@ showhide(Client *c) void sigchld(int unused) { + #if COOL_AUTOSTART_PATCH + pid_t pid; + #endif // COOL_AUTOSTART_PATCH if (signal(SIGCHLD, sigchld) == SIG_ERR) die("can't install SIGCHLD handler:"); + #if COOL_AUTOSTART_PATCH + 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; + } + } + } + #else while (0 < waitpid(-1, NULL, WNOHANG)); + #endif // COOL_AUTOSTART_PATCH } void @@ -4009,7 +4047,9 @@ main(int argc, char *argv[]) XrmInitialize(); loadxrdb(); #endif // XRDB_PATCH && !BAR_VTCOLORS_PATCH - + #if COOL_AUTOSTART_PATCH + autostart_exec(); + #endif // COOL_AUTOSTART_PATCH setup(); #ifdef __OpenBSD__ if (pledge("stdio rpath proc exec", NULL) == -1) diff --git a/patch/cool_autostart.c b/patch/cool_autostart.c new file mode 100644 index 0000000..2bc3c3b --- /dev/null +++ b/patch/cool_autostart.c @@ -0,0 +1,28 @@ +/* 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); + } +} \ No newline at end of file diff --git a/patch/cool_autostart.h b/patch/cool_autostart.h new file mode 100644 index 0000000..8dc0d19 --- /dev/null +++ b/patch/cool_autostart.h @@ -0,0 +1 @@ +static void autostart_exec(void); \ No newline at end of file diff --git a/patch/include.c b/patch/include.c index 9e008b9..aaaf123 100644 --- a/patch/include.c +++ b/patch/include.c @@ -76,6 +76,9 @@ #if CMDCUSTOMIZE_PATCH #include "cmdcustomize.c" #endif +#if COOL_AUTOSTART_PATCH +#include "cool_autostart.c" +#endif #if CYCLELAYOUTS_PATCH #include "cyclelayouts.c" #endif diff --git a/patch/include.h b/patch/include.h index 245933e..e879d80 100644 --- a/patch/include.h +++ b/patch/include.h @@ -73,6 +73,9 @@ #if CMDCUSTOMIZE_PATCH #include "cmdcustomize.h" #endif +#if COOL_AUTOSTART_PATCH +#include "cool_autostart.h" +#endif #if CYCLELAYOUTS_PATCH #include "cyclelayouts.h" #endif diff --git a/patches.def.h b/patches.def.h index 7834b58..b88338b 100644 --- a/patches.def.h +++ b/patches.def.h @@ -337,6 +337,12 @@ */ #define COMBO_PATCH 0 +/* Allow dwm to execute commands from autostart array in your config.h file. When dwm exits + * then all processes from autostart array will be killed. + * https://dwm.suckless.org/patches/cool_autostart/ + */ +#define COOL_AUTOSTART_PATCH 0 + /* The cyclelayouts patch lets you cycle through all your layouts. * https://dwm.suckless.org/patches/cyclelayouts/ */