]> jxnshi.xyz Git - dwm-config.git/commitdiff
Add cool autostart
authorjxnshi <jxnshi@proton.me>
Sat, 25 Jan 2025 19:57:25 +0000 (20:57 +0100)
committerjxnshi <jxnshi@proton.me>
Sat, 25 Jan 2025 19:57:25 +0000 (20:57 +0100)
config.def.h
dwm.c

index 88002bfd17d1b56795c5066837cb5764303ee874..b5c72ca3cba00c72daf7d13b8f5a4ad8474be6f7 100644 (file)
@@ -19,6 +19,11 @@ static const char *colors[][3]      = {
        [SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
 };
 
+static const char *const autostart[] = {
+       "slstatus", 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 21ac013235e15e14cd87eb50fa01ab477ad2c0ee..7d61022569a0c5f6a21de4b837aaa3b3af5d11dd 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -193,7 +193,6 @@ static void resizeclient(Client *c, int x, int y, int w, int h);
 static void resizemouse(const Arg *arg);
 static void restack(Monitor *m);
 static void run(void);
-static void runAutostart(void);
 static void scan(void);
 static int sendevent(Client *c, Atom proto);
 static void sendmon(Client *c, Monitor *m);
@@ -233,6 +232,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";
@@ -274,6 +274,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)
@@ -1258,6 +1286,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;
 }
 
@@ -1390,12 +1428,6 @@ run(void)
                        handler[ev.type](&ev); /* call handler */
 }
 
-void
-runAutostart(void) {
-       system("cd ~/.dwm; ./autostart_blocking.sh");
-       system("cd ~/.dwm; ./autostart.sh &");
-}
-
 void
 scan(void)
 {
@@ -1556,8 +1588,25 @@ setup(void)
        sa.sa_handler = SIG_IGN;
        sigaction(SIGCHLD, &sa, NULL);
 
+       pid_t pid;
+
        /* clean up any zombies (inherited from .xinitrc etc) immediately */
-       while (waitpid(-1, NULL, WNOHANG) > 0);
+
+       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;
+                       }
+               }
+
+       }
 
        /* init screen */
        screen = DefaultScreen(dpy);
@@ -2189,6 +2238,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)
@@ -2196,7 +2246,6 @@ main(int argc, char *argv[])
 #endif /* __OpenBSD__ */
        scan();
        run();
-       runAutostart();
        cleanup();
        XCloseDisplay(dpy);
        return EXIT_SUCCESS;