diff --git a/README.md b/README.md index 1a95221..70c4829 100644 --- a/README.md +++ b/README.md @@ -65,4 +65,5 @@ Most patches can be found on the suckless website: [https://dwm.suckless.org/pat * [cfacts](https://dwm.suckless.org/patches/cfacts/) - Assign different weights to clients in their respective stack in tiled layout * [cfacts-vanitygaps](https://github.com/bakkeby/patches/blob/master/dwm/dwm-cfacts-vanitygaps-6.2.diff) - Vanity gaps patch compatible with cfacts patch * [combo](https://dwm.suckless.org/patches/combo/) - Select multiple tags for tag or view by pressing all the right keys as a combo +* [cool autostart](https://dwm.suckless.org/patches/cool_autostart/) - Execute commands from `autostart` array in `config.h` on startup and kill processes upon dwm exit * [fixborders](https://dwm.suckless.org/patches/alpha/) - Make borders opaque diff --git a/config.def.h b/config.def.h index 068288b..dbbd0fe 100644 --- a/config.def.h +++ b/config.def.h @@ -27,6 +27,34 @@ static const char *colors[][3] = { [SchemeSel] = { col_gray4, col_cyan, col_cyan }, }; +static const char *const autostart[] = { + // System Restore Processes + "bash", "/home/sravan/.screenlayout/default-screen-layout.sh", NULL, // Restore default screen layout + "nitrogen", "--restore", NULL, // Restore wallpaper + // System Tray Applications + "volctl", NULL, // PulseAudio Volume Control + "nyrna", NULL, // Nyrna Application Suspend + "blueman-tray", NULL, // Blueman Bluetooth Manager + "nm-applet", NULL, // Network Manager Applet + "kdeconnect-indicator", NULL, // KDE Connect + "flameshot", NULL, // Flameshot Screenshot Tool + "xfce4-power-manager", NULL, // XFCE4 Power Manager + // Background Processes + "picom", "--config", "/home/sravan/.config/picom/picom.conf", NULL, // Picom Compositor + "/usr/lib/xfce4/notifyd/xfce4-notifyd", NULL, // Xfce Notification Daemon + "greenclip", "daemon", NULL, // Greenclip Clipboard Manager + "redshift", "-x", NULL, // Reset redshift display gamma + "redshift-gtk", NULL, // Redshift Blue Light Filter + "/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1", NULL, // GNOME Polkit Authentication Agent + "slstatus", NULL, // slstatus status bar + "light-locker", "--lock-on-suspend", "--lock-on-lid", NULL, // LightDM Locker + // Hardware Driver Applications + "solaar", "--window=hide", NULL, // Logitech Mouse Driver + "polychromatic-tray-applet", NULL, // Razer Keyboard Customization + "optimus-manager-qt", NULL, // Optimus Manager Qt + NULL /* terminate */ +}; + /* tagging */ static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; diff --git a/dwm.c b/dwm.c index ed5a1d9..5984def 100644 --- a/dwm.c +++ b/dwm.c @@ -253,6 +253,7 @@ static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); +static void autostart_exec(void); static void keyrelease(XEvent *e); static void combotag(const Arg *arg); @@ -301,6 +302,34 @@ static Window root, wmcheckwin; /* 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 */ static int combo = 0; @@ -1423,6 +1452,16 @@ propertynotify(XEvent *e) 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; } @@ -1858,9 +1897,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 @@ -2425,6 +2480,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) diff --git a/patches/dwm-cool-autostart-6.2.diff b/patches/dwm-cool-autostart-6.2.diff new file mode 100644 index 0000000..84a93ea --- /dev/null +++ b/patches/dwm-cool-autostart-6.2.diff @@ -0,0 +1,116 @@ +diff --git a/config.def.h b/config.def.h +index 1c0b587..ed056a4 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -18,6 +18,11 @@ static const char *colors[][3] = { + [SchemeSel] = { col_gray4, col_cyan, col_cyan }, + }; + ++static const char *const autostart[] = { ++ "st", NULL, ++ NULL /* terminate */ ++}; ++ + /* tagging */ + static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; + +diff --git a/dwm.c b/dwm.c +index 9fd0286..1facd56 100644 +--- a/dwm.c ++++ b/dwm.c +@@ -234,6 +234,7 @@ static int xerror(Display *dpy, XErrorEvent *ee); + static int xerrordummy(Display *dpy, XErrorEvent *ee); + static int xerrorstart(Display *dpy, XErrorEvent *ee); + static void zoom(const Arg *arg); ++static void autostart_exec(void); + + /* variables */ + static const char broken[] = "broken"; +@@ -275,6 +276,34 @@ static Window root, wmcheckwin; + /* 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) +@@ -1249,6 +1278,16 @@ propertynotify(XEvent *e) + 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; + } + +@@ -1632,9 +1671,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 +@@ -2139,6 +2194,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) +