diff options
Diffstat (limited to 'sway/config.c')
-rw-r--r-- | sway/config.c | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/sway/config.c b/sway/config.c index b64dd4b1..119730ca 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -13,6 +13,7 @@ | |||
13 | 13 | ||
14 | struct sway_config *config = NULL; | 14 | struct sway_config *config = NULL; |
15 | 15 | ||
16 | |||
16 | static void free_variable(struct sway_variable *var) { | 17 | static void free_variable(struct sway_variable *var) { |
17 | free(var->name); | 18 | free(var->name); |
18 | free(var->value); | 19 | free(var->value); |
@@ -46,6 +47,33 @@ static void free_workspace_output(struct workspace_output *wo) { | |||
46 | free(wo); | 47 | free(wo); |
47 | } | 48 | } |
48 | 49 | ||
50 | static void free_config(struct sway_config *config) { | ||
51 | int i; | ||
52 | for (i = 0; i < config->symbols->length; ++i) { | ||
53 | free_variable(config->symbols->items[i]); | ||
54 | } | ||
55 | list_free(config->symbols); | ||
56 | |||
57 | for (i = 0; i < config->modes->length; ++i) { | ||
58 | free_mode(config->modes->items[i]); | ||
59 | } | ||
60 | list_free(config->modes); | ||
61 | |||
62 | free_flat_list(config->cmd_queue); | ||
63 | |||
64 | for (i = 0; i < config->workspace_outputs->length; ++i) { | ||
65 | free_workspace_output(config->workspace_outputs->items[i]); | ||
66 | } | ||
67 | list_free(config->workspace_outputs); | ||
68 | |||
69 | for (i = 0; i < config->output_configs->length; ++i) { | ||
70 | free_outut_config(config->output_configs->items[i]); | ||
71 | } | ||
72 | list_free(config->output_configs); | ||
73 | free(config); | ||
74 | } | ||
75 | |||
76 | |||
49 | static bool file_exists(const char *path) { | 77 | static bool file_exists(const char *path) { |
50 | return access(path, R_OK) != -1; | 78 | return access(path, R_OK) != -1; |
51 | } | 79 | } |
@@ -59,7 +87,8 @@ static void config_defaults(struct sway_config *config) { | |||
59 | config->cmd_queue = create_list(); | 87 | config->cmd_queue = create_list(); |
60 | 88 | ||
61 | config->current_mode = malloc(sizeof(struct sway_mode)); | 89 | config->current_mode = malloc(sizeof(struct sway_mode)); |
62 | config->current_mode->name = NULL; | 90 | config->current_mode->name = malloc(sizeof("default")); |
91 | strcpy(config->current_mode->name, "default"); | ||
63 | config->current_mode->bindings = create_list(); | 92 | config->current_mode->bindings = create_list(); |
64 | list_add(config->modes, config->current_mode); | 93 | list_add(config->modes, config->current_mode); |
65 | 94 | ||
@@ -78,32 +107,6 @@ static void config_defaults(struct sway_config *config) { | |||
78 | config->gaps_outer = 0; | 107 | config->gaps_outer = 0; |
79 | } | 108 | } |
80 | 109 | ||
81 | void free_config(struct sway_config *config) { | ||
82 | int i; | ||
83 | for (i = 0; i < config->symbols->length; ++i) { | ||
84 | free_variable(config->symbols->items[i]); | ||
85 | } | ||
86 | list_free(config->symbols); | ||
87 | |||
88 | for (i = 0; i < config->modes->length; ++i) { | ||
89 | free_mode(config->modes->items[i]); | ||
90 | } | ||
91 | list_free(config->modes); | ||
92 | |||
93 | free_flat_list(config->cmd_queue); | ||
94 | |||
95 | for (i = 0; i < config->workspace_outputs->length; ++i) { | ||
96 | free_workspace_output(config->workspace_outputs->items[i]); | ||
97 | } | ||
98 | list_free(config->workspace_outputs); | ||
99 | |||
100 | for (i = 0; i < config->output_configs->length; ++i) { | ||
101 | free_outut_config(config->output_configs->items[i]); | ||
102 | } | ||
103 | list_free(config->output_configs); | ||
104 | free(config); | ||
105 | } | ||
106 | |||
107 | static char *get_config_path(void) { | 110 | static char *get_config_path(void) { |
108 | char *config_path = NULL; | 111 | char *config_path = NULL; |
109 | char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""}; | 112 | char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""}; |
@@ -210,34 +213,35 @@ bool load_config(const char *file) { | |||
210 | } | 213 | } |
211 | 214 | ||
212 | bool read_config(FILE *file, bool is_active) { | 215 | bool read_config(FILE *file, bool is_active) { |
213 | struct sway_config *temp_config = malloc(sizeof(struct sway_config)); | 216 | struct sway_config *old_config = config; |
214 | config_defaults(temp_config); | 217 | struct sway_mode *default_mode; |
218 | config = malloc(sizeof(struct sway_config)); | ||
219 | |||
220 | config_defaults(config); | ||
221 | default_mode = config->current_mode; | ||
222 | |||
215 | if (is_active) { | 223 | if (is_active) { |
216 | sway_log(L_DEBUG, "Performing configuration file reload"); | 224 | sway_log(L_DEBUG, "Performing configuration file reload"); |
217 | temp_config->reloading = true; | 225 | config->reloading = true; |
218 | temp_config->active = true; | 226 | config->active = true; |
219 | } | 227 | } |
220 | |||
221 | bool success = true; | 228 | bool success = true; |
222 | 229 | ||
223 | int temp_depth = 0; // Temporary: skip all config sections with depth | 230 | char *line; |
224 | |||
225 | while (!feof(file)) { | 231 | while (!feof(file)) { |
226 | int _; | 232 | line = read_line(file); |
227 | char *line = read_line(file); | ||
228 | line = strip_whitespace(line, &_); | ||
229 | line = strip_comments(line); | 233 | line = strip_comments(line); |
230 | if (!line[0]) { | 234 | if (line[0] == '\0') { |
231 | goto _continue; | 235 | goto _continue; |
232 | } | 236 | } |
233 | if (temp_depth && line[0] == '}') { | 237 | if (line[0] == '}') { |
234 | temp_depth--; | 238 | config->current_mode = default_mode; |
235 | goto _continue; | 239 | goto _continue; |
236 | } | 240 | } |
237 | 241 | ||
238 | // Any command which would require wlc to be initialized | 242 | // Any command which would require wlc to be initialized |
239 | // should be queued for later execution | 243 | // should be queued for later execution |
240 | list_t *args = split_string(line, " "); | 244 | list_t *args = split_string(line, whitespace); |
241 | struct cmd_handler *handler; | 245 | struct cmd_handler *handler; |
242 | if ((handler = find_handler(args->items[0]))) { | 246 | if ((handler = find_handler(args->items[0]))) { |
243 | if (handler->config_type == CMD_KEYBIND) { | 247 | if (handler->config_type == CMD_KEYBIND) { |
@@ -246,11 +250,11 @@ bool read_config(FILE *file, bool is_active) { | |||
246 | sway_log(L_DEBUG, "Deferring command ``%s''", line); | 250 | sway_log(L_DEBUG, "Deferring command ``%s''", line); |
247 | char *cmd = malloc(strlen(line) + 1); | 251 | char *cmd = malloc(strlen(line) + 1); |
248 | strcpy(cmd, line); | 252 | strcpy(cmd, line); |
249 | list_add(temp_config->cmd_queue, cmd); | 253 | list_add(config->cmd_queue, cmd); |
250 | } else if (!temp_depth && !handle_command(temp_config, line)) { | 254 | } else if (!handle_command(line)) { |
251 | sway_log(L_DEBUG, "Config load failed for line ``%s''", line); | 255 | sway_log(L_DEBUG, "Config load failed for line ``%s''", line); |
252 | success = false; | 256 | success = false; |
253 | temp_config->failed = true; | 257 | config->failed = true; |
254 | } | 258 | } |
255 | } else { | 259 | } else { |
256 | sway_log(L_ERROR, "Invalid command ``%s''", line); | 260 | sway_log(L_ERROR, "Invalid command ``%s''", line); |
@@ -258,25 +262,21 @@ bool read_config(FILE *file, bool is_active) { | |||
258 | free_flat_list(args); | 262 | free_flat_list(args); |
259 | 263 | ||
260 | _continue: | 264 | _continue: |
261 | if (line && line[strlen(line) - 1] == '{') { | ||
262 | temp_depth++; | ||
263 | } | ||
264 | free(line); | 265 | free(line); |
265 | } | 266 | } |
266 | 267 | ||
267 | if (is_active) { | 268 | if (is_active) { |
268 | temp_config->reloading = false; | 269 | config->reloading = false; |
269 | arrange_windows(&root_container, -1, -1); | 270 | arrange_windows(&root_container, -1, -1); |
270 | } | 271 | } |
271 | if (config) { | 272 | if (old_config) { |
272 | free_config(config); | 273 | free_config(old_config); |
273 | } | 274 | } |
274 | config = temp_config; | ||
275 | 275 | ||
276 | return success; | 276 | return success; |
277 | } | 277 | } |
278 | 278 | ||
279 | char *do_var_replacement(struct sway_config *config, char *str) { | 279 | char *do_var_replacement(char *str) { |
280 | // TODO: Handle escaping $ and using $ in string literals | 280 | // TODO: Handle escaping $ and using $ in string literals |
281 | int i; | 281 | int i; |
282 | for (i = 0; str[i]; ++i) { | 282 | for (i = 0; str[i]; ++i) { |