sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-cool-autostart-6.2.diff (2759B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 1c0b587..ed056a4 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -18,6 +18,11 @@ static const char *colors[][3]      = {
      6  	[SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
      7  };
      8  
      9 +static const char *const autostart[] = {
     10 +	"st", NULL,
     11 +	NULL /* terminate */
     12 +};
     13 +
     14  /* tagging */
     15  static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     16  
     17 diff --git a/dwm.c b/dwm.c
     18 index 9fd0286..1facd56 100644
     19 --- a/dwm.c
     20 +++ b/dwm.c
     21 @@ -234,6 +234,7 @@ static int xerror(Display *dpy, XErrorEvent *ee);
     22  static int xerrordummy(Display *dpy, XErrorEvent *ee);
     23  static int xerrorstart(Display *dpy, XErrorEvent *ee);
     24  static void zoom(const Arg *arg);
     25 +static void autostart_exec(void);
     26  
     27  /* variables */
     28  static const char broken[] = "broken";
     29 @@ -275,6 +276,34 @@ static Window root, wmcheckwin;
     30  /* compile-time check if all tags fit into an unsigned int bit array. */
     31  struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
     32  
     33 +/* dwm will keep pid's of processes from autostart array and kill them at quit */
     34 +static pid_t *autostart_pids;
     35 +static size_t autostart_len;
     36 +
     37 +/* execute command from autostart array */
     38 +static void
     39 +autostart_exec() {
     40 +	const char *const *p;
     41 +	size_t i = 0;
     42 +
     43 +	/* count entries */
     44 +	for (p = autostart; *p; autostart_len++, p++)
     45 +		while (*++p);
     46 +
     47 +	autostart_pids = malloc(autostart_len * sizeof(pid_t));
     48 +	for (p = autostart; *p; i++, p++) {
     49 +		if ((autostart_pids[i] = fork()) == 0) {
     50 +			setsid();
     51 +			execvp(*p, (char *const *)p);
     52 +			fprintf(stderr, "dwm: execvp %s\n", *p);
     53 +			perror(" failed");
     54 +			_exit(EXIT_FAILURE);
     55 +		}
     56 +		/* skip arguments */
     57 +		while (*++p);
     58 +	}
     59 +}
     60 +
     61  /* function implementations */
     62  void
     63  applyrules(Client *c)
     64 @@ -1249,6 +1278,16 @@ propertynotify(XEvent *e)
     65  void
     66  quit(const Arg *arg)
     67  {
     68 +	size_t i;
     69 +
     70 +	/* kill child processes */
     71 +	for (i = 0; i < autostart_len; i++) {
     72 +		if (0 < autostart_pids[i]) {
     73 +			kill(autostart_pids[i], SIGTERM);
     74 +			waitpid(autostart_pids[i], NULL, 0);
     75 +		}
     76 +	}
     77 +
     78  	running = 0;
     79  }
     80  
     81 @@ -1632,9 +1671,25 @@ showhide(Client *c)
     82  void
     83  sigchld(int unused)
     84  {
     85 +	pid_t pid;
     86 +
     87  	if (signal(SIGCHLD, sigchld) == SIG_ERR)
     88  		die("can't install SIGCHLD handler:");
     89 -	while (0 < waitpid(-1, NULL, WNOHANG));
     90 +	while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
     91 +		pid_t *p, *lim;
     92 +
     93 +		if (!(p = autostart_pids))
     94 +			continue;
     95 +		lim = &p[autostart_len];
     96 +
     97 +		for (; p < lim; p++) {
     98 +			if (*p == pid) {
     99 +				*p = -1;
    100 +				break;
    101 +			}
    102 +		}
    103 +
    104 +	}
    105  }
    106  
    107  void
    108 @@ -2139,6 +2194,7 @@ main(int argc, char *argv[])
    109  	if (!(dpy = XOpenDisplay(NULL)))
    110  		die("dwm: cannot open display");
    111  	checkotherwm();
    112 +	autostart_exec();
    113  	setup();
    114  #ifdef __OpenBSD__
    115  	if (pledge("stdio rpath proc exec", NULL) == -1)
    116