dwm

git clone git://mattcarlson.org/repos/dwm.git
Log | Files | Refs

commit 304975c11d82e1a3fbe3a070c8256c9628397f3e
parent db0d75f37084e0a4c19e3c1d8ad1d14349ec2f02
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Wed, 18 Aug 2021 21:31:56 -0400

cool autostart

Diffstat:
Mconfig.h | 5+++++
Mdwm.c | 58+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/config.h b/config.h @@ -51,6 +51,11 @@ static const unsigned int alphas[][3] = { [SchemeSel] = { OPAQUE, baralpha, borderalpha }, }; +static const char *const autostart[] = { + "st", NULL, + NULL /* terminate */ +}; + // tags static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; diff --git a/dwm.c b/dwm.c @@ -276,6 +276,7 @@ static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void xinitvisual(); static void zoom(const Arg *arg); +static void autostart_exec(void); static void load_xresources(void); static void reload(const Arg *arg); static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); @@ -337,6 +338,34 @@ static xcb_connection_t *xcon; /* 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) @@ -1481,6 +1510,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); + } + } + if(arg->i) restart = 1; running = 0; } @@ -1873,9 +1912,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 @@ -2663,6 +2718,7 @@ main(int argc, char *argv[]) if (!(xcon = XGetXCBConnection(dpy))) die("dwm: cannot get xcb connection\n"); checkotherwm(); + autostart_exec(); XrmInitialize(); load_xresources(); setup();