diff options
-rw-r--r-- | sway/config.c | 69 |
1 files changed, 47 insertions, 22 deletions
diff --git a/sway/config.c b/sway/config.c index 6e665434..76b9ec08 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -338,35 +338,60 @@ static bool file_exists(const char *path) { | |||
338 | return path && access(path, R_OK) != -1; | 338 | return path && access(path, R_OK) != -1; |
339 | } | 339 | } |
340 | 340 | ||
341 | static char *config_path(const char *prefix, const char *config_folder) { | ||
342 | if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) { | ||
343 | return NULL; | ||
344 | } | ||
345 | |||
346 | const char *filename = "config"; | ||
347 | |||
348 | size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename); | ||
349 | char *path = calloc(size, sizeof(char)); | ||
350 | snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename); | ||
351 | return path; | ||
352 | } | ||
353 | |||
341 | static char *get_config_path(void) { | 354 | static char *get_config_path(void) { |
342 | static const char *config_paths[] = { | 355 | char *path = NULL; |
343 | "$HOME/.sway/config", | 356 | const char *home = getenv("HOME"); |
344 | "$XDG_CONFIG_HOME/sway/config", | 357 | size_t size_fallback = 1 + strlen(home) + strlen("/.config"); |
345 | "$HOME/.i3/config", | 358 | char *config_home_fallback = calloc(size_fallback, sizeof(char)); |
346 | "$XDG_CONFIG_HOME/i3/config", | 359 | snprintf(config_home_fallback, size_fallback, "%s/.config", home); |
347 | SYSCONFDIR "/sway/config", | 360 | |
348 | SYSCONFDIR "/i3/config", | 361 | const char *config_home = getenv("XDG_CONFIG_HOME"); |
362 | if (config_home == NULL || config_home[0] == '\0') { | ||
363 | config_home = config_home_fallback; | ||
364 | } | ||
365 | |||
366 | struct config_path { | ||
367 | const char *prefix; | ||
368 | const char *config_folder; | ||
349 | }; | 369 | }; |
350 | 370 | ||
351 | char *config_home = getenv("XDG_CONFIG_HOME"); | 371 | struct config_path config_paths[] = { |
352 | if (!config_home || !*config_home) { | 372 | { .prefix = home, .config_folder = ".sway"}, |
353 | config_paths[1] = "$HOME/.config/sway/config"; | 373 | { .prefix = config_home, .config_folder = "sway"}, |
354 | config_paths[3] = "$HOME/.config/i3/config"; | 374 | { .prefix = home, .config_folder = ".i3"}, |
355 | } | 375 | { .prefix = config_home, .config_folder = "i3"}, |
376 | { .prefix = SYSCONFDIR, .config_folder = "sway"}, | ||
377 | { .prefix = SYSCONFDIR, .config_folder = "i3"} | ||
378 | }; | ||
356 | 379 | ||
357 | for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { | 380 | size_t num_config_paths = sizeof(config_paths)/sizeof(config_paths[0]); |
358 | wordexp_t p; | 381 | for (size_t i = 0; i < num_config_paths; i++) { |
359 | if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) { | 382 | path = config_path(config_paths[i].prefix, config_paths[i].config_folder); |
360 | char *path = strdup(p.we_wordv[0]); | 383 | if (!path) { |
361 | wordfree(&p); | 384 | continue; |
362 | if (file_exists(path)) { | ||
363 | return path; | ||
364 | } | ||
365 | free(path); | ||
366 | } | 385 | } |
386 | if (file_exists(path)) { | ||
387 | break; | ||
388 | } | ||
389 | free(path); | ||
390 | path = NULL; | ||
367 | } | 391 | } |
368 | 392 | ||
369 | return NULL; | 393 | free(config_home_fallback); |
394 | return path; | ||
370 | } | 395 | } |
371 | 396 | ||
372 | static bool load_config(const char *path, struct sway_config *config, | 397 | static bool load_config(const char *path, struct sway_config *config, |