From f3151887288895944d17026b81e8d0f0a3a92d66 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Sun, 14 Jun 2020 19:56:52 +0200 Subject: [PATCH] Autostart: Make autostart conform to XDG Base Directory specification (upgrade) --- dwm.c | 2 +- patch/autostart.c | 88 ++++++++++++++++++++++++++++++++++++++++++++--- patch/autostart.h | 2 +- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/dwm.c b/dwm.c index a796b2d..4dd26b4 100644 --- a/dwm.c +++ b/dwm.c @@ -4370,7 +4370,7 @@ main(int argc, char *argv[]) #endif /* __OpenBSD__ */ scan(); #if AUTOSTART_PATCH - runAutostart(); + runautostart(); #endif run(); #if RESTARTSIG_PATCH diff --git a/patch/autostart.c b/patch/autostart.c index c61ea7f..265d4e8 100644 --- a/patch/autostart.c +++ b/patch/autostart.c @@ -1,10 +1,88 @@ +static const char autostartblocksh[] = "autostart_blocking.sh"; +static const char autostartsh[] = "autostart.sh"; +static const char dwmdir[] = "dwm"; +static const char localshare[] = ".local/share"; + void -runAutostart(void) { +runautostart(void) +{ + char *pathpfx; + char *path; + char *xdgdatahome; + char *home; - int ret; + if ((home = getenv("HOME")) == NULL) + /* this is almost impossible */ + return; - ret = system("cd ~/.config/dwm; ./autostart_blocking.sh"); - ret = system("cd ~/.config/dwm; ./autostart.sh &"); + /* if $XDG_DATA_HOME is defined, use $XDG_DATA_HOME/dwm, + * otherwise use ~/.local/share/dwm as autostart script directory + */ + if ((xdgdatahome = getenv("XDG_DATA_HOME")) != NULL) { + /* space for path segments, separators and nul */ + if ((pathpfx = malloc(strlen(xdgdatahome) + strlen(dwmdir) + 2)) == NULL) + return; - if (ret); // ignore, hide compilation warnings + if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) { + free(pathpfx); + return; + } + } else { + /* space for path segments, separators and nul */ + if ((pathpfx = malloc(strlen(home) + strlen(localshare) + strlen(dwmdir) + 3)) == NULL) + return; + + if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) { + free(pathpfx); + return; + } + } + + /* check if the autostart script directory exists */ + struct stat sb; + + if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) { + /* the XDG conformant path does not exist or are not directories + * so we try ~/.dwm instead + */ + if (realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3) == NULL) { + free(pathpfx); + return; + } + + if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) { + free(pathpfx); + return; + } + } + + /* try the blocking script first */ + if ((path = malloc(strlen(pathpfx) + strlen(autostartblocksh) + 2)) == NULL) { + free(pathpfx); + return; + } else + if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) { + free(path); + free(pathpfx); + } + + if (access(path, X_OK) == 0) + system(path); + + /* now the non-blocking script */ + if ((path = realloc(path, strlen(pathpfx) + strlen(autostartsh) + 4)) == NULL) { + free(pathpfx); + free(path); + return; + } else + if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) { + free(path); + free(pathpfx); + } + + if (access(path, X_OK) == 0) { + system(strcat(path, " &")); + free(pathpfx); + free(path); + } } \ No newline at end of file diff --git a/patch/autostart.h b/patch/autostart.h index 6058a3d..299eaef 100644 --- a/patch/autostart.h +++ b/patch/autostart.h @@ -1 +1 @@ -static void runAutostart(void); \ No newline at end of file +static void runautostart(void); \ No newline at end of file