diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-08-19 07:50:27 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-08-19 07:50:27 -0400 |
commit | 79f9d93ef3581f399173e91db464137d77877c99 (patch) | |
tree | 06bdbc13ca80c1927c41096f4c3a6d876268bb43 | |
parent | Merge pull request #79 from taiyu-len/master (diff) | |
download | sway-79f9d93ef3581f399173e91db464137d77877c99.tar.gz sway-79f9d93ef3581f399173e91db464137d77877c99.tar.zst sway-79f9d93ef3581f399173e91db464137d77877c99.zip |
Refactor config file loading
-rw-r--r-- | sway/config.c | 211 |
1 files changed, 96 insertions, 115 deletions
diff --git a/sway/config.c b/sway/config.c index 0dc53c75..66a3fa21 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -16,121 +16,121 @@ static bool exists(const char *path) { | |||
16 | return access(path, R_OK) != -1; | 16 | return access(path, R_OK) != -1; |
17 | } | 17 | } |
18 | 18 | ||
19 | static char *get_config_path() { | 19 | void config_defaults(struct sway_config *config) { |
20 | char *name = "/.sway/config"; | 20 | config->symbols = create_list(); |
21 | const char *home = getenv("HOME"); | 21 | config->modes = create_list(); |
22 | 22 | config->cmd_queue = create_list(); | |
23 | // Check home dir | 23 | config->workspace_outputs = create_list(); |
24 | sway_log(L_DEBUG, "Trying to find config in ~/.sway/config"); | 24 | config->current_mode = malloc(sizeof(struct sway_mode)); |
25 | char *temp = malloc(strlen(home) + strlen(name) + 1); | 25 | config->current_mode->name = NULL; |
26 | strcpy(temp, home); | 26 | config->current_mode->bindings = create_list(); |
27 | strcat(temp, name); | 27 | list_add(config->modes, config->current_mode); |
28 | if (exists(temp)) { | 28 | // Flags |
29 | return temp; | 29 | config->focus_follows_mouse = true; |
30 | } | 30 | config->mouse_warping = true; |
31 | free(temp); | 31 | config->reloading = false; |
32 | 32 | config->active = false; | |
33 | // Check XDG_CONFIG_HOME with fallback to ~/.config/ | 33 | config->failed = false; |
34 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config"); | 34 | config->gaps_inner = 0; |
35 | char *xdg_config_home = getenv("XDG_CONFIG_HOME"); | 35 | config->gaps_outer = 0; |
36 | if (xdg_config_home == NULL) { | 36 | } |
37 | sway_log(L_DEBUG, "Falling back to ~/.config/sway/config"); | ||
38 | name = "/.config/sway/config"; | ||
39 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
40 | strcpy(temp, home); | ||
41 | strcat(temp, name); | ||
42 | } else { | ||
43 | name = "/sway/config"; | ||
44 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
45 | strcpy(temp, xdg_config_home); | ||
46 | strcat(temp, name); | ||
47 | } | ||
48 | if (exists(temp)) { | ||
49 | return temp; | ||
50 | } | ||
51 | 37 | ||
52 | // Check /etc/ | 38 | void free_mode(struct sway_mode *mode) { |
53 | sway_log(L_DEBUG, "Trying to find config in /etc/sway/config"); | 39 | free(mode->name); |
54 | strcpy(temp, "/etc/sway/config"); | 40 | free_flat_list(mode->bindings); |
55 | if (exists(temp)) { | 41 | } |
56 | return temp; | ||
57 | } | ||
58 | free(temp); | ||
59 | 42 | ||
60 | // Check XDG_CONFIG_DIRS | 43 | void free_config(struct sway_config *config) { |
61 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | 44 | int i; |
62 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | 45 | for (i = 0; i < config->modes->length; ++i) { |
63 | if (xdg_config_dirs != NULL) { | 46 | free_mode((struct sway_mode *)config->modes->items[i]); |
64 | list_t *paths = split_string(xdg_config_dirs, ":"); | 47 | } |
65 | name = "/sway/config"; | 48 | free_flat_list(config->modes); |
66 | int i; | 49 | for (i = 0; i < config->workspace_outputs->length; ++i) { |
67 | for (i = 0; i < paths->length; i++ ) { | 50 | struct workspace_output *wso = config->workspace_outputs->items[i]; |
68 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | 51 | free(wso->output); |
69 | strcpy(temp, paths->items[i]); | 52 | free(wso->workspace); |
70 | strcat(temp, name); | 53 | } |
71 | if (exists(temp)) { | 54 | free_flat_list(config->workspace_outputs); |
72 | free_flat_list(paths); | 55 | free_flat_list(config->cmd_queue); |
73 | return temp; | 56 | for (i = 0; i < config->symbols->length; ++i) { |
74 | } | 57 | struct sway_variable *sym = config->symbols->items[i]; |
75 | free(temp); | 58 | free(sym->name); |
76 | } | 59 | free(sym->value); |
77 | free_flat_list(paths); | 60 | } |
78 | } | 61 | free_flat_list(config->symbols); |
62 | } | ||
79 | 63 | ||
80 | //Now fall back to i3 paths and try the same thing | 64 | static const char *search_paths[] = { |
81 | name = "/.i3/config"; | 65 | "$home/.sway/config", |
82 | sway_log(L_DEBUG, "Trying to find config in ~/.i3/config"); | 66 | "$config/.sway/config", |
83 | temp = malloc(strlen(home) + strlen(name) + 1); | 67 | "/etc/sway/config", |
84 | strcpy(temp, home); | 68 | "$home/.i3/config", |
85 | strcat(temp, name); | 69 | "$config/.i3/config", |
86 | if (exists(temp)) { | 70 | "/etc/i3/config" |
87 | return temp; | 71 | }; |
88 | } | ||
89 | free(temp); | ||
90 | 72 | ||
91 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config"); | 73 | static char *get_config_path() { |
92 | if (xdg_config_home == NULL) { | 74 | char *home = getenv("HOME"); |
93 | sway_log(L_DEBUG, "Falling back to ~/.config/i3/config"); | 75 | char *config = getenv("XDG_CONFIG_HOME"); |
94 | name = "/.config/i3/config"; | 76 | if (!config) { |
95 | temp = malloc(strlen(home) + strlen(name) + 1); | 77 | const char *def = "/.config/sway"; |
96 | strcpy(temp, home); | 78 | config = malloc(strlen(home) + strlen(def) + 1); |
97 | strcat(temp, name); | 79 | strcpy(config, home); |
98 | } else { | 80 | strcat(config, def); |
99 | name = "/i3/config"; | ||
100 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
101 | strcpy(temp, xdg_config_home); | ||
102 | strcat(temp, name); | ||
103 | } | ||
104 | if (exists(temp)) { | ||
105 | return temp; | ||
106 | } | 81 | } |
107 | 82 | ||
108 | sway_log(L_DEBUG, "Trying to find config in /etc/i3/config"); | 83 | // Set up a temporary config for holding set variables |
109 | strcpy(temp, "/etc/i3/config"); | 84 | struct sway_config *temp_config = malloc(sizeof(struct sway_config)); |
110 | if (exists(temp)) { | 85 | config_defaults(temp_config); |
111 | return temp; | 86 | const char *set_home = "set $home "; |
87 | char *_home = malloc(strlen(home) + strlen(set_home) + 1); | ||
88 | strcpy(_home, set_home); | ||
89 | strcat(_home, home); | ||
90 | handle_command(temp_config, _home); | ||
91 | free(_home); | ||
92 | const char *set_config = "set $config "; | ||
93 | char *_config = malloc(strlen(config) + strlen(set_config) + 1); | ||
94 | strcpy(_config, set_config); | ||
95 | strcat(_config, config); | ||
96 | handle_command(temp_config, _config); | ||
97 | free(_config); | ||
98 | |||
99 | char *test = NULL; | ||
100 | int i; | ||
101 | for (i = 0; i < sizeof(search_paths) / sizeof(char *); ++i) { | ||
102 | test = strdup(search_paths[i]); | ||
103 | test = do_var_replacement(temp_config, test); | ||
104 | sway_log(L_DEBUG, "Checking for config at %s", test); | ||
105 | if (exists(test)) { | ||
106 | goto _continue; | ||
107 | } | ||
108 | free(test); | ||
109 | test = NULL; | ||
112 | } | 110 | } |
113 | free(temp); | ||
114 | 111 | ||
115 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | 112 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); |
113 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | ||
116 | if (xdg_config_dirs != NULL) { | 114 | if (xdg_config_dirs != NULL) { |
117 | list_t *paths = split_string(xdg_config_dirs, ":"); | 115 | list_t *paths = split_string(xdg_config_dirs, ":"); |
118 | name = "/i3/config"; | 116 | char *name = "/sway/config"; |
119 | int i; | 117 | int i; |
120 | for (i = 0; i < paths->length; i++ ) { | 118 | for (i = 0; i < paths->length; i++ ) { |
121 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | 119 | test = malloc(strlen(paths->items[i]) + strlen(name) + 1); |
122 | strcpy(temp, paths->items[i]); | 120 | strcpy(test, paths->items[i]); |
123 | strcat(temp, name); | 121 | strcat(test, name); |
124 | if (exists(temp)) { | 122 | if (exists(test)) { |
125 | free_flat_list(paths); | 123 | free_flat_list(paths); |
126 | return temp; | 124 | return test; |
127 | } | 125 | } |
128 | free(temp); | 126 | free(test); |
129 | } | 127 | } |
130 | free_flat_list(paths); | 128 | free_flat_list(paths); |
131 | } | 129 | } |
132 | 130 | ||
133 | return NULL; | 131 | _continue: |
132 | free_config(temp_config); | ||
133 | return test; | ||
134 | } | 134 | } |
135 | 135 | ||
136 | bool load_config(void) { | 136 | bool load_config(void) { |
@@ -162,25 +162,6 @@ bool load_config(void) { | |||
162 | return config_load_success; | 162 | return config_load_success; |
163 | } | 163 | } |
164 | 164 | ||
165 | void config_defaults(struct sway_config *config) { | ||
166 | config->symbols = create_list(); | ||
167 | config->modes = create_list(); | ||
168 | config->cmd_queue = create_list(); | ||
169 | config->workspace_outputs = create_list(); | ||
170 | config->current_mode = malloc(sizeof(struct sway_mode)); | ||
171 | config->current_mode->name = NULL; | ||
172 | config->current_mode->bindings = create_list(); | ||
173 | list_add(config->modes, config->current_mode); | ||
174 | // Flags | ||
175 | config->focus_follows_mouse = true; | ||
176 | config->mouse_warping = true; | ||
177 | config->reloading = false; | ||
178 | config->active = false; | ||
179 | config->failed = false; | ||
180 | config->gaps_inner = 0; | ||
181 | config->gaps_outer = 0; | ||
182 | } | ||
183 | |||
184 | bool read_config(FILE *file, bool is_active) { | 165 | bool read_config(FILE *file, bool is_active) { |
185 | struct sway_config *temp_config = malloc(sizeof(struct sway_config)); | 166 | struct sway_config *temp_config = malloc(sizeof(struct sway_config)); |
186 | config_defaults(temp_config); | 167 | config_defaults(temp_config); |