diff options
Diffstat (limited to 'sway/config.c')
-rw-r--r-- | sway/config.c | 108 |
1 files changed, 54 insertions, 54 deletions
diff --git a/sway/config.c b/sway/config.c index b64dd4b1..5ece2810 100644 --- a/sway/config.c +++ b/sway/config.c @@ -13,6 +13,7 @@ struct sway_config *config = NULL; + static void free_variable(struct sway_variable *var) { free(var->name); free(var->value); @@ -46,6 +47,33 @@ static void free_workspace_output(struct workspace_output *wo) { free(wo); } +static void free_config(struct sway_config *config) { + int i; + for (i = 0; i < config->symbols->length; ++i) { + free_variable(config->symbols->items[i]); + } + list_free(config->symbols); + + for (i = 0; i < config->modes->length; ++i) { + free_mode(config->modes->items[i]); + } + list_free(config->modes); + + free_flat_list(config->cmd_queue); + + for (i = 0; i < config->workspace_outputs->length; ++i) { + free_workspace_output(config->workspace_outputs->items[i]); + } + list_free(config->workspace_outputs); + + for (i = 0; i < config->output_configs->length; ++i) { + free_outut_config(config->output_configs->items[i]); + } + list_free(config->output_configs); + free(config); +} + + static bool file_exists(const char *path) { return access(path, R_OK) != -1; } @@ -59,7 +87,8 @@ static void config_defaults(struct sway_config *config) { config->cmd_queue = create_list(); config->current_mode = malloc(sizeof(struct sway_mode)); - config->current_mode->name = NULL; + config->current_mode->name = malloc(sizeof("default")); + strcpy(config->current_mode->name, "default"); config->current_mode->bindings = create_list(); list_add(config->modes, config->current_mode); @@ -78,32 +107,6 @@ static void config_defaults(struct sway_config *config) { config->gaps_outer = 0; } -void free_config(struct sway_config *config) { - int i; - for (i = 0; i < config->symbols->length; ++i) { - free_variable(config->symbols->items[i]); - } - list_free(config->symbols); - - for (i = 0; i < config->modes->length; ++i) { - free_mode(config->modes->items[i]); - } - list_free(config->modes); - - free_flat_list(config->cmd_queue); - - for (i = 0; i < config->workspace_outputs->length; ++i) { - free_workspace_output(config->workspace_outputs->items[i]); - } - list_free(config->workspace_outputs); - - for (i = 0; i < config->output_configs->length; ++i) { - free_outut_config(config->output_configs->items[i]); - } - list_free(config->output_configs); - free(config); -} - static char *get_config_path(void) { char *config_path = NULL; char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""}; @@ -210,47 +213,48 @@ bool load_config(const char *file) { } bool read_config(FILE *file, bool is_active) { - struct sway_config *temp_config = malloc(sizeof(struct sway_config)); - config_defaults(temp_config); + struct sway_config *old_config = config; + struct sway_mode *default_mode; + config = malloc(sizeof(struct sway_config)); + + config_defaults(config); + default_mode = config->current_mode; + if (is_active) { sway_log(L_DEBUG, "Performing configuration file reload"); - temp_config->reloading = true; - temp_config->active = true; + config->reloading = true; + config->active = true; } - bool success = true; - int temp_depth = 0; // Temporary: skip all config sections with depth - + char *line; while (!feof(file)) { - int _; - char *line = read_line(file); - line = strip_whitespace(line, &_); + line = read_line(file); + line = strip_whitespace(line); line = strip_comments(line); - if (!line[0]) { + if (line[0] == '\0') { goto _continue; } - if (temp_depth && line[0] == '}') { - temp_depth--; + if (line[0] == '}') { + config->current_mode = default_mode; goto _continue; } // Any command which would require wlc to be initialized // should be queued for later execution - list_t *args = split_string(line, " "); + list_t *args = split_string(line, whitespace); struct cmd_handler *handler; if ((handler = find_handler(args->items[0]))) { if (handler->config_type == CMD_KEYBIND) { sway_log(L_ERROR, "Invalid command during config ``%s''", line); } else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) { sway_log(L_DEBUG, "Deferring command ``%s''", line); - char *cmd = malloc(strlen(line) + 1); - strcpy(cmd, line); - list_add(temp_config->cmd_queue, cmd); - } else if (!temp_depth && !handle_command(temp_config, line)) { + char *cmd = strdup(line); + list_add(config->cmd_queue, cmd); + } else if (!handle_command(line)) { sway_log(L_DEBUG, "Config load failed for line ``%s''", line); success = false; - temp_config->failed = true; + config->failed = true; } } else { sway_log(L_ERROR, "Invalid command ``%s''", line); @@ -258,25 +262,21 @@ bool read_config(FILE *file, bool is_active) { free_flat_list(args); _continue: - if (line && line[strlen(line) - 1] == '{') { - temp_depth++; - } free(line); } if (is_active) { - temp_config->reloading = false; + config->reloading = false; arrange_windows(&root_container, -1, -1); } - if (config) { - free_config(config); + if (old_config) { + free_config(old_config); } - config = temp_config; return success; } -char *do_var_replacement(struct sway_config *config, char *str) { +char *do_var_replacement(char *str) { // TODO: Handle escaping $ and using $ in string literals int i; for (i = 0; str[i]; ++i) { |