diff options
author | 2015-11-28 10:00:53 -0500 | |
---|---|---|
committer | 2015-11-28 10:00:53 -0500 | |
commit | 2f192ccecabfaa4ab85f6cfb616380c571c67ce6 (patch) | |
tree | 2f060cd47f652be0b33ca86fb929e113c098f503 /sway | |
parent | Remove extraneous logging from swaygrab (diff) | |
download | sway-2f192ccecabfaa4ab85f6cfb616380c571c67ce6.tar.gz sway-2f192ccecabfaa4ab85f6cfb616380c571c67ce6.tar.zst sway-2f192ccecabfaa4ab85f6cfb616380c571c67ce6.zip |
Improve config file loading
This also makes it so that your i3 config is used before
/etc/sway/config.
Diffstat (limited to 'sway')
-rw-r--r-- | sway/config.c | 90 |
1 files changed, 27 insertions, 63 deletions
diff --git a/sway/config.c b/sway/config.c index ba88a315..5f8e4d6a 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -2,6 +2,7 @@ | |||
2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <wordexp.h> | ||
5 | #include "readline.h" | 6 | #include "readline.h" |
6 | #include "stringop.h" | 7 | #include "stringop.h" |
7 | #include "list.h" | 8 | #include "list.h" |
@@ -14,7 +15,6 @@ | |||
14 | 15 | ||
15 | struct sway_config *config = NULL; | 16 | struct sway_config *config = NULL; |
16 | 17 | ||
17 | |||
18 | static void free_variable(struct sway_variable *var) { | 18 | static void free_variable(struct sway_variable *var) { |
19 | free(var->name); | 19 | free(var->name); |
20 | free(var->value); | 20 | free(var->value); |
@@ -118,79 +118,41 @@ static void config_defaults(struct sway_config *config) { | |||
118 | } | 118 | } |
119 | 119 | ||
120 | static char *get_config_path(void) { | 120 | static char *get_config_path(void) { |
121 | char *config_path = NULL; | 121 | static const char *config_paths[] = { |
122 | char *paths[3] = { getenv("HOME"), getenv("XDG_CONFIG_HOME"), "" }; | 122 | "$HOME/.sway/config", |
123 | int pathlen[3] = { 0, 0, 0 }; | 123 | "$XDG_CONFIG_HOME/sway/config", |
124 | int i; | 124 | "$HOME/.i3/config", |
125 | #define home paths[0] | 125 | "$XDG_CONFIG_HOME/i3/config", |
126 | #define conf paths[1] | ||
127 | // Get home and config directories | ||
128 | conf = conf ? strdup(conf) : NULL; | ||
129 | home = home ? strdup(home) : NULL; | ||
130 | // If config folder is unset, set it to $HOME/.config | ||
131 | if (!conf && home) { | ||
132 | const char *def = "/.config"; | ||
133 | conf = malloc(strlen(home) + strlen(def) + 1); | ||
134 | strcpy(conf, home); | ||
135 | strcat(conf, def); | ||
136 | } | ||
137 | // Get path lengths | ||
138 | pathlen[0] = home ? strlen(home) : 0; | ||
139 | pathlen[1] = conf ? strlen(conf) : 0; | ||
140 | #undef home | ||
141 | #undef conf | ||
142 | |||
143 | // Search for config file from search paths | ||
144 | static const char *search_paths[] = { | ||
145 | "/.sway/config", // Prepend with $home | ||
146 | "/sway/config", // Prepend with $config | ||
147 | "/etc/sway/config", | 126 | "/etc/sway/config", |
148 | "/.i3/config", // $home | 127 | "/etc/i3/config", |
149 | "/i3/config", // $config | ||
150 | "/etc/i3/config" | ||
151 | }; | 128 | }; |
152 | for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { | 129 | |
153 | // Only try path if it is set by enviroment variables | 130 | if (!getenv("XDG_CONFIG_HOME")) { |
154 | if (paths[i%3]) { | 131 | char *home = getenv("HOME"); |
155 | char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1); | 132 | char *config_home = malloc(strlen("home") + strlen("/.config") + 1); |
156 | strcpy(test, paths[i%3]); | 133 | strcpy(config_home, home); |
157 | strcpy(test + pathlen[i%3], search_paths[i]); | 134 | strcat(config_home, "/.config"); |
158 | sway_log(L_DEBUG, "Checking for config at %s", test); | 135 | setenv("XDG_CONFIG_HOME", config_home, 1); |
159 | if (file_exists(test)) { | 136 | sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); |
160 | config_path = test; | ||
161 | goto cleanup; | ||
162 | } | ||
163 | free(test); | ||
164 | } | ||
165 | } | 137 | } |
166 | 138 | ||
167 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | 139 | wordexp_t p; |
168 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | 140 | char *path; |
169 | if (xdg_config_dirs) { | 141 | |
170 | list_t *paths = split_string(xdg_config_dirs, ":"); | 142 | int i; |
171 | const char *name = "/sway/config"; | 143 | for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) { |
172 | for (i = 0; i < paths->length; i++ ) { | 144 | if (wordexp(config_paths[i], &p, 0) == 0) { |
173 | char *test = malloc(strlen(paths->items[i]) + strlen(name) + 1); | 145 | path = p.we_wordv[0]; |
174 | strcpy(test, paths->items[i]); | 146 | if (file_exists(path)) { |
175 | strcat(test, name); | 147 | return path; |
176 | if (file_exists(test)) { | ||
177 | config_path = test; | ||
178 | break; | ||
179 | } | 148 | } |
180 | free(test); | ||
181 | } | 149 | } |
182 | free_flat_list(paths); | ||
183 | } | 150 | } |
184 | 151 | ||
185 | cleanup: | 152 | return NULL; // Not reached |
186 | free(paths[0]); | ||
187 | free(paths[1]); | ||
188 | return config_path; | ||
189 | } | 153 | } |
190 | 154 | ||
191 | bool load_config(const char *file) { | 155 | bool load_config(const char *file) { |
192 | sway_log(L_INFO, "Loading config"); | ||
193 | |||
194 | input_init(); | 156 | input_init(); |
195 | 157 | ||
196 | char *path; | 158 | char *path; |
@@ -200,6 +162,8 @@ bool load_config(const char *file) { | |||
200 | path = get_config_path(); | 162 | path = get_config_path(); |
201 | } | 163 | } |
202 | 164 | ||
165 | sway_log(L_INFO, "Loading config from %s", path); | ||
166 | |||
203 | if (path == NULL) { | 167 | if (path == NULL) { |
204 | sway_log(L_ERROR, "Unable to find a config file!"); | 168 | sway_log(L_ERROR, "Unable to find a config file!"); |
205 | return false; | 169 | return false; |