Commit 8ba8b826cf3cba28d08b304a6acf356d67bd8304 Parent: 874a9fd70d0caf02ae8f0ef231184811b49a2cc7 Author: mcol <mcol@posteo.net> Date: 2021-01-26 20:43:47 +0000 Committer: mcol <mcol@posteo.net> Committed: 2021-01-26 20:43:47 +0000 Move config folder to ~/.config/wimp and add auto-start functionality
wimp.conf Modified
@@ -11,6 +11,11 @@ # # All possible options are described here. Lines that start with '#' are # ignored. Options that are commented out contain their default values. +# +# Config is read from the first found of: +# -c command line argument +# $XDG_CONFIG_HOME/.config/wimp/config +# $HOME/.config/wimp/config ## General settings @@ -83,6 +88,9 @@ # Scroll direction. Can be one of: natural, reverse #scroll_direction natural +# Set an auto-start script. This will be executed after setting up the compositor. +#auto_start ${XDG_CONFIG_HOME:-$HOME}/.config/wimp/autostart + ## Configure desks
src/types.h Modified
@@ -15,6 +15,8 @@ #include <wlr/types/wlr_xdg_shell.h> #include <wlr/util/log.h> +#define is_readable(p) (access(p, R_OK) != -1) +#define is_executable(p) (access(p, R_OK | X_OK) != -1) enum cursor_mode { CURSOR_PASSTHROUGH, @@ -34,7 +36,10 @@ struct wl_display *display; struct wlr_backend *backend; struct wlr_renderer *renderer; + + char *config_directory; char *config_file; + char *auto_start; struct wlr_output_layout *output_layout; struct wl_list outputs;
src/main.c Modified
@@ -46,6 +46,10 @@ static void free_stuff() { + free(wimp.config_directory); + free(wimp.config_file); + free(wimp.auto_start); + struct binding *kb, *tkb; wl_list_for_each_safe(kb, tkb, &wimp.mouse_bindings, link) { wl_list_remove(&kb->link); @@ -169,6 +173,7 @@ } setenv("WAYLAND_DISPLAY", socket, true); wlr_log(WLR_INFO, "Starting with WAYLAND_DISPLAY=%s", socket); + schedule_auto_start(); wl_display_run(wimp.display); // stop
src/config.h Modified
@@ -4,6 +4,7 @@ #include "types.h" void assign_colour(char *hex, float dest[4]); +void schedule_auto_start(); void load_config(); void locate_config();
src/config.c Modified
@@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <wordexp.h> +#include <wayland-server-core.h> #include <wlr/render/wlr_texture.h> #include <unistd.h> @@ -12,8 +13,8 @@ #include "types.h" #define is_number(s) (strspn(s, "0123456789.-") == strlen(s)) -#define CONFIG_HOME "$HOME/.config/wimp.conf" -#define CONFIG_HOME_XDG "$XDG_CONFIG_HOME/.config/wimp.conf" +#define CONFIG_HOME "$HOME/.config/wimp/" +#define CONFIG_HOME_XDG "$XDG_CONFIG_HOME/.config/wimp/" static const char *DEFAULT_CONFIG = "\n\ @@ -27,6 +28,16 @@ "; +static char *expand(char *path) { + wordexp_t p; + wordexp(path, &p, WRDE_NOCMD | WRDE_UNDEF); + free(path); + char *new = strdup(p.we_wordv[0]); + wordfree(&p); + return new; +} + + struct value_map { const char *name; const int value; @@ -331,6 +342,48 @@ } +struct source { + struct wl_event_source *wl_event_source; +}; + + +static int _auto_start(void *data) { + struct source *source = data; + wl_event_source_remove(source->wl_event_source); + free(source); + + char *script = wimp.auto_start; + if (fork() == 0) { + if (execl(script, script, (void *)NULL) == -1) { + exit(EXIT_FAILURE); + } + } else { + wlr_log(WLR_DEBUG, "Running autostart script: %s", script); + } + free(wimp.auto_start); + return 0; +} + + +void schedule_auto_start() { + if (wimp.auto_start) { + wimp.auto_start = expand(wimp.auto_start); + } else { + wimp.auto_start = strndup( + wimp.config_directory, strlen(wimp.config_directory) + strlen("autostart") + ); + strcat(wimp.auto_start, "autostart"); + } + + if (is_executable(wimp.auto_start)) { + struct wl_event_loop *event_loop = wl_display_get_event_loop(wimp.display); + struct source *source = calloc(1, sizeof(struct source)); + source->wl_event_source = wl_event_loop_add_timer(event_loop, _auto_start, source); + wl_event_source_timer_update(source->wl_event_source, 1); + } +} + + static void parse_config(FILE *stream) { char *s; char linebuf[1024]; @@ -383,6 +436,8 @@ else if (!strcasecmp(s, "width")) if ((s = strtok(NULL, " \t\n\r")) && is_number(s)) desk->border_width = strtod(s, NULL); + } else if (!strcasecmp(s, "auto_start")) { + wimp.auto_start = strdup(strtok(NULL, " \t\n\r")); } else if (!strcasecmp(s, "scroll_direction")) { s = strtok(NULL, " \t\n\r"); if (!strcasecmp(s, "natural")) @@ -416,38 +471,30 @@ free(buffer); fclose(stream); - // load custom configuration file - if (wimp.config_file) { + // load configuration file + if (is_readable(wimp.config_file)) { stream = fopen(wimp.config_file, "r"); parse_config(stream); fclose(stream); + } else { + wlr_log(WLR_ERROR, "%s not accessible. Using defaults.", wimp.config_file); } setup_vt_switching(); } -void locate_config() -{ - char *config; - char *xdg_config; +void locate_config() { + char *xdg_config = getenv("XDG_CONFIG_HOME"); + char *config_dir = (xdg_config && *xdg_config) ? CONFIG_HOME_XDG : CONFIG_HOME; + wimp.config_directory = strdup(config_dir); + wimp.config_directory = expand(wimp.config_directory); + config_dir = wimp.config_directory; if (wimp.config_file == NULL) { - xdg_config = getenv("XDG_CONFIG_HOME"); - config = (xdg_config && *xdg_config) ? CONFIG_HOME_XDG : CONFIG_HOME; - wimp.config_file = strdup(config); - }; - - wordexp_t p; - wordexp(wimp.config_file, &p, WRDE_NOCMD | WRDE_UNDEF); - free(wimp.config_file); - - if (access(p.we_wordv[0], R_OK) == -1) { - wlr_log(WLR_ERROR, "%s not accessible. Using defaults.", p.we_wordv[0]); - wimp.config_file = NULL; + wimp.config_file = strndup(config_dir, strlen(config_dir) + strlen("config")); + strcat(wimp.config_file, "config"); } else { - wimp.config_file = strdup(p.we_wordv[0]); - } - - wordfree(&p); + wimp.config_file = expand(wimp.config_file); + }; }