From 91f888978575d57d09681c6b0355416a8f659f91 Mon Sep 17 00:00:00 2001 From: Simon Long Date: Tue, 9 Jan 2024 08:00:05 +0000 Subject: [PATCH] Use system directory as fallback location for environment and autostart --- include/config/session.h | 6 ++-- src/config/session.c | 60 ++++++++++++++++++++++++++-------------- src/main.c | 4 +-- src/server.c | 2 +- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/include/config/session.h b/include/config/session.h index 4cc19b6e..d1efbb60 100644 --- a/include/config/session.h +++ b/include/config/session.h @@ -4,17 +4,15 @@ /** * session_environment_init - set enrivonment variables based on = - * @dir: path to config directory * pairs in `${XDG_CONFIG_DIRS:-/etc/xdg}/lawbc/environment` with user override * in `${XDG_CONFIG_HOME:-$HOME/.config}` */ -void session_environment_init(const char *dir); +void session_environment_init(void); /** * session_autostart_init - run autostart file as shell script - * @dir: path to config directory * Note: Same as `sh ~/.config/labwc/autostart` (or equivalent XDG config dir) */ -void session_autostart_init(const char *dir); +void session_autostart_init(void); #endif /* LABWC_SESSION_H */ diff --git a/src/config/session.c b/src/config/session.c index 00fbabaf..d1b713a2 100644 --- a/src/config/session.c +++ b/src/config/session.c @@ -7,6 +7,7 @@ #include #include #include "common/buf.h" +#include "common/dir.h" #include "common/file-helpers.h" #include "common/spawn.h" #include "common/string-helpers.h" @@ -45,14 +46,14 @@ error: free(value.buf); } -static void +static bool read_environment_file(const char *filename) { char *line = NULL; size_t len = 0; FILE *stream = fopen(filename, "r"); if (!stream) { - return; + return false; } wlr_log(WLR_INFO, "read environment file %s", filename); while (getline(&line, &len, stream) != -1) { @@ -64,6 +65,7 @@ read_environment_file(const char *filename) } free(line); fclose(stream); + return true; } static char * @@ -96,7 +98,7 @@ update_activation_env(const char *env_keys) } void -session_environment_init(const char *dir) +session_environment_init(void) { /* * Set default for XDG_CURRENT_DESKTOP so xdg-desktop-portal-wlr is happy. @@ -114,32 +116,50 @@ session_environment_init(const char *dir) */ setenv("_JAVA_AWT_WM_NONREPARENTING", "1", 0); - char *environment = build_path(dir, "environment"); - if (!environment) { - return; + int i; + bool read = false; + char *environment; + for (i = 0; i < 2; i++) + { + environment = build_path(i ? sys_config_dir() : user_config_dir(), "environment"); + if (environment) { + read = read_environment_file(environment); + free(environment); + } + if (read) { + return; + } } - read_environment_file(environment); - free(environment); } void -session_autostart_init(const char *dir) +session_autostart_init(void) { /* Update dbus and systemd user environment, each may fail gracefully */ update_activation_env("DISPLAY WAYLAND_DISPLAY XDG_CURRENT_DESKTOP"); - char *autostart = build_path(dir, "autostart"); - if (!autostart) { - return; + int i; + char *autostart; + for (i = 0; i < 2; i++) + { + autostart = build_path(i ? sys_config_dir() : user_config_dir(), "autostart"); + + if (autostart) { + if (file_exists(autostart)) { + break; + } + free(autostart); + autostart = NULL; + } } - if (!file_exists(autostart)) { + + if (autostart) { + wlr_log(WLR_INFO, "run autostart file %s", autostart); + char *cmd = strdup_printf("sh %s", autostart); + spawn_async_no_shell(cmd); + free(cmd); + free(autostart); + } else { wlr_log(WLR_ERROR, "no autostart file"); - goto out; } - wlr_log(WLR_INFO, "run autostart file %s", autostart); - char *cmd = strdup_printf("sh %s", autostart); - spawn_async_no_shell(cmd); - free(cmd); -out: - free(autostart); } diff --git a/src/main.c b/src/main.c index 04314327..82aea3f4 100644 --- a/src/main.c +++ b/src/main.c @@ -150,7 +150,7 @@ main(int argc, char *argv[]) } wlr_log(WLR_INFO, "using config dir (%s)\n", rc.config_dir); - session_environment_init(rc.config_dir); + session_environment_init(); rcxml_read(config_file); /* @@ -184,7 +184,7 @@ main(int argc, char *argv[]) menu_init(&server); - session_autostart_init(rc.config_dir); + session_autostart_init(); if (startup_cmd) { spawn_async_no_shell(startup_cmd); } diff --git a/src/server.c b/src/server.c index e7a58be9..fdde0d94 100644 --- a/src/server.c +++ b/src/server.c @@ -67,7 +67,7 @@ reload_config_and_theme(void) static int handle_sighup(int signal, void *data) { - session_environment_init(rc.config_dir); + session_environment_init(); reload_config_and_theme(); return 0; }