From 03d03691d3c17307bc04f2ec7e2491d48699821a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?= Date: Wed, 9 Feb 2022 07:02:47 -0600 Subject: [PATCH] apply autostart patch from dwm https://dwm.suckless.org/patches/cool_autostart/ --- config.def.h | 6 ++++ dwl.c | 99 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/config.def.h b/config.def.h index 190b0da4..bfff6041 100644 --- a/config.def.h +++ b/config.def.h @@ -51,6 +51,12 @@ static const int repeat_delay = 600; static const int tap_to_click = 1; static const int natural_scrolling = 0; +/* Autostart */ +static const char *const autostart[] = { + "sh", "-c", "swaybg --image /xap/local/background", NULL, + NULL /* terminate */ +}; + /* If you want to use the windows key change this to WLR_MODIFIER_LOGO */ #define MODKEY WLR_MODIFIER_ALT #define TAGKEYS(KEY,SKEY,TAG) \ diff --git a/dwl.c b/dwl.c index b56abd7c..e342557b 100644 --- a/dwl.c +++ b/dwl.c @@ -202,6 +202,7 @@ static void arrange(Monitor *m); static void arrangelayer(Monitor *m, struct wl_list *list, struct wlr_box *usable_area, int exclusive); static void arrangelayers(Monitor *m); +static void autostartexec(void); static void axisnotify(struct wl_listener *listener, void *data); static void buttonpress(struct wl_listener *listener, void *data); static void chvt(const Arg *arg); @@ -264,6 +265,7 @@ static void setmon(Client *c, Monitor *m, unsigned int newtags); static void setpsel(struct wl_listener *listener, void *data); static void setsel(struct wl_listener *listener, void *data); static void setup(void); +static void sigchld(int unused); static void spawn(const Arg *arg); static void startdrag(struct wl_listener *listener, void *data); static void tag(const Arg *arg); @@ -351,7 +353,6 @@ static void configurex11(struct wl_listener *listener, void *data); static void createnotifyx11(struct wl_listener *listener, void *data); static Atom getatom(xcb_connection_t *xc, const char *name); static void sethints(struct wl_listener *listener, void *data); -static void sigchld(int unused); static void xwaylandready(struct wl_listener *listener, void *data); static struct wl_listener new_xwayland_surface = {.notify = createnotifyx11}; static struct wl_listener xwayland_ready = {.notify = xwaylandready}; @@ -368,6 +369,9 @@ static Atom netatom[NetLast]; /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; +static pid_t *autostart_pids; +static size_t autostart_len; + /* function implementations */ void applybounds(Client *c, struct wlr_box *bbox) @@ -386,6 +390,30 @@ applybounds(Client *c, struct wlr_box *bbox) c->geom.y = bbox->y; } +void +autostartexec(void) +{ + const char *const *p; + size_t i = 0; + + /* count entries */ + for (p = autostart; *p; autostart_len++, p++) + while (*++p); + + autostart_pids = calloc(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, "dwl: execvp %s\n", *p); + perror(" failed"); + _exit(EXIT_FAILURE); + } + /* skip arguments */ + while (*++p); + } +} + void applyrules(Client *c) { @@ -1535,6 +1563,16 @@ printstatus(void) 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); + } + } + wl_display_terminate(dpy); } @@ -1630,6 +1668,7 @@ run(char *startup_cmd) setenv("WAYLAND_DISPLAY", socket, 1); /* Now that the socket exists, run the startup command */ + autostartexec(); if (startup_cmd) { int piperw[2]; if (pipe(piperw) < 0) @@ -1816,11 +1855,7 @@ setup(void) dpy = wl_display_create(); /* Set up signal handlers */ -#ifdef XWAYLAND sigchld(0); -#else - signal(SIGCHLD, SIG_IGN); -#endif signal(SIGINT, quitsignal); signal(SIGTERM, quitsignal); @@ -1985,6 +2020,41 @@ setup(void) #endif } +void +sigchld(int unused) +{ + siginfo_t in; + pid_t pid; + /* We should be able to remove this function in favor of a simple + * signal(SIGCHLD, SIG_IGN); + * but the Xwayland implementation in wlroots currently prevents us from + * setting our own disposition for SIGCHLD. + */ + + if (signal(SIGCHLD, sigchld) == SIG_ERR) + die("can't install SIGCHLD handler:"); + /* WNOWAIT leaves the child in a waitable state, in case this is the + * XWayland process + */ + while (!waitid(P_ALL, 0, &in, WEXITED|WNOHANG|WNOWAIT) && in.si_pid + && in.si_pid != xwayland->server->pid) { + if (0 < (pid = waitpid(in.si_pid, 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 spawn(const Arg *arg) { @@ -2379,25 +2449,6 @@ sethints(struct wl_listener *listener, void *data) } } -void -sigchld(int unused) -{ - siginfo_t in; - /* We should be able to remove this function in favor of a simple - * signal(SIGCHLD, SIG_IGN); - * but the Xwayland implementation in wlroots currently prevents us from - * setting our own disposition for SIGCHLD. - */ - if (signal(SIGCHLD, sigchld) == SIG_ERR) - die("can't install SIGCHLD handler:"); - /* WNOWAIT leaves the child in a waitable state, in case this is the - * XWayland process - */ - while (!waitid(P_ALL, 0, &in, WEXITED|WNOHANG|WNOWAIT) && in.si_pid - && in.si_pid != xwayland->server->pid) - waitpid(in.si_pid, NULL, 0); -} - void xwaylandready(struct wl_listener *listener, void *data) {