dwm-cool_autostart-6.5.diff - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
dwm-cool_autostart-6.5.diff (2985B)
---
1 diff --git a/config.def.h b/config.def.h
2 index 9efa774..aba210d 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 f1d86b2..3ce99fc 100644
19 --- a/dwm.c
20 +++ b/dwm.c
21 @@ -233,6 +233,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 @@ -274,6 +275,36 @@ 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 +
62 +
63 /* function implementations */
64 void
65 applyrules(Client *c)
66 @@ -1258,6 +1289,16 @@ propertynotify(XEvent *e)
67 void
68 quit(const Arg *arg)
69 {
70 + size_t i;
71 +
72 + /* kill child processes */
73 + for (i = 0; i < autostart_len; i++) {
74 + if (0 < autostart_pids[i]) {
75 + kill(autostart_pids[i], SIGTERM);
76 + waitpid(autostart_pids[i], NULL, 0);
77 + }
78 + }
79 +
80 running = 0;
81 }
82
83 @@ -1543,6 +1584,7 @@ setup(void)
84 XSetWindowAttributes wa;
85 Atom utf8string;
86 struct sigaction sa;
87 + pid_t pid;
88
89 /* do not transform children into zombies when they terminate */
90 sigemptyset(&sa.sa_mask);
91 @@ -1551,7 +1593,21 @@ setup(void)
92 sigaction(SIGCHLD, &sa, NULL);
93
94 /* clean up any zombies (inherited from .xinitrc etc) immediately */
95 - while (waitpid(-1, NULL, WNOHANG) > 0);
96 + while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
97 + pid_t *p, *lim;
98 +
99 + if (!(p = autostart_pids))
100 + continue;
101 + lim = &p[autostart_len];
102 +
103 + for (; p < lim; p++) {
104 + if (*p == pid) {
105 + *p = -1;
106 + break;
107 + }
108 + }
109 +
110 + }
111
112 /* init screen */
113 screen = DefaultScreen(dpy);
114 @@ -2152,6 +2208,7 @@ main(int argc, char *argv[])
115 if (!(dpy = XOpenDisplay(NULL)))
116 die("dwm: cannot open display");
117 checkotherwm();
118 + autostart_exec();
119 setup();
120 #ifdef __OpenBSD__
121 if (pledge("stdio rpath proc exec", NULL) == -1)