aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar Ian Fan <ianfan0@gmail.com>2018-07-08 20:34:47 +0100
committerLibravatar Ian Fan <ianfan0@gmail.com>2018-07-10 12:37:37 +0100
commit23c1c26c3fedf5470dbee9fe97c2374a48588863 (patch)
tree751b5569e4e05149c7a7cd7e0d8084be5ff62063 /sway/config.c
parentAdd get_binding_modes message type to ipc (diff)
downloadsway-23c1c26c3fedf5470dbee9fe97c2374a48588863.tar.gz
sway-23c1c26c3fedf5470dbee9fe97c2374a48588863.tar.zst
sway-23c1c26c3fedf5470dbee9fe97c2374a48588863.zip
Add get_config message type to ipc
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c53
1 files changed, 41 insertions, 12 deletions
diff --git a/sway/config.c b/sway/config.c
index d0e0e432..c59f4f0d 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -117,6 +117,7 @@ void free_config(struct sway_config *config) {
117 free(config->floating_scroll_left_cmd); 117 free(config->floating_scroll_left_cmd);
118 free(config->floating_scroll_right_cmd); 118 free(config->floating_scroll_right_cmd);
119 free(config->font); 119 free(config->font);
120 free((char *)config->current_config_path);
120 free((char *)config->current_config); 121 free((char *)config->current_config);
121 free(config); 122 free(config);
122} 123}
@@ -205,6 +206,7 @@ static void config_defaults(struct sway_config *config) {
205 if (!(config->active_bar_modifiers = create_list())) goto cleanup; 206 if (!(config->active_bar_modifiers = create_list())) goto cleanup;
206 207
207 if (!(config->config_chain = create_list())) goto cleanup; 208 if (!(config->config_chain = create_list())) goto cleanup;
209 config->current_config_path = NULL;
208 config->current_config = NULL; 210 config->current_config = NULL;
209 211
210 // borders 212 // borders
@@ -304,8 +306,6 @@ static char *get_config_path(void) {
304 return NULL; // Not reached 306 return NULL; // Not reached
305} 307}
306 308
307const char *current_config_path;
308
309static bool load_config(const char *path, struct sway_config *config) { 309static bool load_config(const char *path, struct sway_config *config) {
310 if (path == NULL) { 310 if (path == NULL) {
311 wlr_log(WLR_ERROR, "Unable to find a config file!"); 311 wlr_log(WLR_ERROR, "Unable to find a config file!");
@@ -313,7 +313,6 @@ static bool load_config(const char *path, struct sway_config *config) {
313 } 313 }
314 314
315 wlr_log(WLR_INFO, "Loading config from %s", path); 315 wlr_log(WLR_INFO, "Loading config from %s", path);
316 current_config_path = path;
317 316
318 struct stat sb; 317 struct stat sb;
319 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 318 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
@@ -333,7 +332,6 @@ static bool load_config(const char *path, struct sway_config *config) {
333 wlr_log(WLR_ERROR, "Error(s) loading config!"); 332 wlr_log(WLR_ERROR, "Error(s) loading config!");
334 } 333 }
335 334
336 current_config_path = NULL;
337 return true; 335 return true;
338} 336}
339 337
@@ -358,7 +356,7 @@ bool load_main_config(const char *file, bool is_active) {
358 config->active = true; 356 config->active = true;
359 } 357 }
360 358
361 config->current_config = path; 359 config->current_config_path = path;
362 list_add(config->config_chain, path); 360 list_add(config->config_chain, path);
363 361
364 config->reading = true; 362 config->reading = true;
@@ -428,7 +426,7 @@ bool load_main_config(const char *file, bool is_active) {
428static bool load_include_config(const char *path, const char *parent_dir, 426static bool load_include_config(const char *path, const char *parent_dir,
429 struct sway_config *config) { 427 struct sway_config *config) {
430 // save parent config 428 // save parent config
431 const char *parent_config = config->current_config; 429 const char *parent_config = config->current_config_path;
432 430
433 char *full_path; 431 char *full_path;
434 int len = strlen(path); 432 int len = strlen(path);
@@ -466,25 +464,25 @@ static bool load_include_config(const char *path, const char *parent_dir,
466 } 464 }
467 } 465 }
468 466
469 config->current_config = real_path; 467 config->current_config_path = real_path;
470 list_add(config->config_chain, real_path); 468 list_add(config->config_chain, real_path);
471 int index = config->config_chain->length - 1; 469 int index = config->config_chain->length - 1;
472 470
473 if (!load_config(real_path, config)) { 471 if (!load_config(real_path, config)) {
474 free(real_path); 472 free(real_path);
475 config->current_config = parent_config; 473 config->current_config_path = parent_config;
476 list_del(config->config_chain, index); 474 list_del(config->config_chain, index);
477 return false; 475 return false;
478 } 476 }
479 477
480 // restore current_config 478 // restore current_config_path
481 config->current_config = parent_config; 479 config->current_config_path = parent_config;
482 return true; 480 return true;
483} 481}
484 482
485bool load_include_configs(const char *path, struct sway_config *config) { 483bool load_include_configs(const char *path, struct sway_config *config) {
486 char *wd = getcwd(NULL, 0); 484 char *wd = getcwd(NULL, 0);
487 char *parent_path = strdup(config->current_config); 485 char *parent_path = strdup(config->current_config_path);
488 const char *parent_dir = dirname(parent_path); 486 const char *parent_dir = dirname(parent_path);
489 487
490 if (chdir(parent_dir) < 0) { 488 if (chdir(parent_dir) < 0) {
@@ -561,6 +559,23 @@ static char *expand_line(const char *block, const char *line, bool add_brace) {
561} 559}
562 560
563bool read_config(FILE *file, struct sway_config *config) { 561bool read_config(FILE *file, struct sway_config *config) {
562 bool reading_main_config = false;
563 char *current_config, *config_pos;
564 long config_size = 0;
565 if (config->current_config == NULL) {
566 reading_main_config = true;
567
568 fseek(file, 0, SEEK_END);
569 config_size = ftell(file);
570 rewind(file);
571
572 config_pos = current_config = malloc(config_size + 1);
573 if (current_config == NULL) {
574 wlr_log(WLR_ERROR, "Unable to allocate buffer for config contents");
575 return false;
576 }
577 }
578
564 bool success = true; 579 bool success = true;
565 int line_number = 0; 580 int line_number = 0;
566 char *line; 581 char *line;
@@ -573,6 +588,14 @@ bool read_config(FILE *file, struct sway_config *config) {
573 } 588 }
574 line_number++; 589 line_number++;
575 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); 590 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
591
592 if (reading_main_config) {
593 size_t l = strlen(line);
594 memcpy(config_pos, line, l); // don't copy terminating character
595 config_pos += l;
596 *config_pos++ = '\n';
597 }
598
576 line = strip_whitespace(line); 599 line = strip_whitespace(line);
577 if (line[0] == '#') { 600 if (line[0] == '#') {
578 free(line); 601 free(line);
@@ -592,6 +615,8 @@ bool read_config(FILE *file, struct sway_config *config) {
592 if (!expanded) { 615 if (!expanded) {
593 list_foreach(stack, free); 616 list_foreach(stack, free);
594 list_free(stack); 617 list_free(stack);
618 free(line);
619 free(current_config);
595 return false; 620 return false;
596 } 621 }
597 wlr_log(WLR_DEBUG, "Expanded line: %s", expanded); 622 wlr_log(WLR_DEBUG, "Expanded line: %s", expanded);
@@ -607,7 +632,7 @@ bool read_config(FILE *file, struct sway_config *config) {
607 case CMD_FAILURE: 632 case CMD_FAILURE:
608 case CMD_INVALID: 633 case CMD_INVALID:
609 wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number, 634 wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number,
610 line, res->error, config->current_config); 635 line, res->error, config->current_config_path);
611 success = false; 636 success = false;
612 break; 637 break;
613 638
@@ -652,6 +677,10 @@ bool read_config(FILE *file, struct sway_config *config) {
652 list_foreach(stack, free); 677 list_foreach(stack, free);
653 list_free(stack); 678 list_free(stack);
654 679
680 if (reading_main_config) {
681 current_config[config_size - 1] = '\0';
682 config->current_config = current_config;
683 }
655 return success; 684 return success;
656} 685}
657 686