summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorbakkeby <[email protected]>2020-06-14 19:56:52 +0200
committerbakkeby <[email protected]>2020-06-14 19:56:52 +0200
commitf3151887288895944d17026b81e8d0f0a3a92d66 (patch)
treeed262845209f8579917f6ca98b02f27a2464db5f
parent6f20203975e0d8e18b7206ffa72d95f4558e5f6c (diff)
downloaddwm-flexipatch-f3151887288895944d17026b81e8d0f0a3a92d66.tar.gz
dwm-flexipatch-f3151887288895944d17026b81e8d0f0a3a92d66.zip
Autostart: Make autostart conform to XDG Base Directory specification (upgrade)
-rw-r--r--dwm.c2
-rw-r--r--patch/autostart.c88
-rw-r--r--patch/autostart.h2
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;
+
+ if ((home = getenv("HOME")) == NULL)
+ /* this is almost impossible */
+ return;
+
+ /* 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 (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);
+ }
- int ret;
+ if (access(path, X_OK) == 0)
+ system(path);
- ret = system("cd ~/.config/dwm; ./autostart_blocking.sh");
- ret = system("cd ~/.config/dwm; ./autostart.sh &");
+ /* 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 (ret); // ignore, hide compilation warnings
+ 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