aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c73
1 files changed, 50 insertions, 23 deletions
diff --git a/sway/config.c b/sway/config.c
index 6e665434..fde386c7 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -26,7 +26,7 @@
26#include "sway/tree/arrange.h" 26#include "sway/tree/arrange.h"
27#include "sway/tree/root.h" 27#include "sway/tree/root.h"
28#include "sway/tree/workspace.h" 28#include "sway/tree/workspace.h"
29#include "cairo.h" 29#include "cairo_util.h"
30#include "pango.h" 30#include "pango.h"
31#include "stringop.h" 31#include "stringop.h"
32#include "list.h" 32#include "list.h"
@@ -338,35 +338,62 @@ 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
341static 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
341static char *get_config_path(void) { 354static 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 char *config_home_fallback = NULL;
345 "$HOME/.i3/config", 358
346 "$XDG_CONFIG_HOME/i3/config", 359 const char *config_home = getenv("XDG_CONFIG_HOME");
347 SYSCONFDIR "/sway/config", 360 if ((config_home == NULL || config_home[0] == '\0') && home != NULL) {
348 SYSCONFDIR "/i3/config", 361 size_t size_fallback = 1 + strlen(home) + strlen("/.config");
362 config_home_fallback = calloc(size_fallback, sizeof(char));
363 if (config_home_fallback != NULL)
364 snprintf(config_home_fallback, size_fallback, "%s/.config", home);
365 config_home = config_home_fallback;
366 }
367
368 struct config_path {
369 const char *prefix;
370 const char *config_folder;
349 }; 371 };
350 372
351 char *config_home = getenv("XDG_CONFIG_HOME"); 373 struct config_path config_paths[] = {
352 if (!config_home || !*config_home) { 374 { .prefix = home, .config_folder = ".sway"},
353 config_paths[1] = "$HOME/.config/sway/config"; 375 { .prefix = config_home, .config_folder = "sway"},
354 config_paths[3] = "$HOME/.config/i3/config"; 376 { .prefix = home, .config_folder = ".i3"},
355 } 377 { .prefix = config_home, .config_folder = "i3"},
378 { .prefix = SYSCONFDIR, .config_folder = "sway"},
379 { .prefix = SYSCONFDIR, .config_folder = "i3"}
380 };
356 381
357 for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { 382 size_t num_config_paths = sizeof(config_paths)/sizeof(config_paths[0]);
358 wordexp_t p; 383 for (size_t i = 0; i < num_config_paths; i++) {
359 if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) { 384 path = config_path(config_paths[i].prefix, config_paths[i].config_folder);
360 char *path = strdup(p.we_wordv[0]); 385 if (!path) {
361 wordfree(&p); 386 continue;
362 if (file_exists(path)) {
363 return path;
364 }
365 free(path);
366 } 387 }
388 if (file_exists(path)) {
389 break;
390 }
391 free(path);
392 path = NULL;
367 } 393 }
368 394
369 return NULL; 395 free(config_home_fallback);
396 return path;
370} 397}
371 398
372static bool load_config(const char *path, struct sway_config *config, 399static bool load_config(const char *path, struct sway_config *config,