aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-05-30 13:20:02 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-06-02 08:07:44 -0400
commit7c810dc344c28d1876c5ee158cb0806289d0f813 (patch)
treedbe756bceca42ea6f9a6cf5e5771037417bb64c3 /sway/config.c
parentMerge pull request #2080 from frsfnrrg/keyboard-remodeling (diff)
downloadsway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.gz
sway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.zst
sway-7c810dc344c28d1876c5ee158cb0806289d0f813.zip
Make command block implementation generic
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c156
1 files changed, 37 insertions, 119 deletions
diff --git a/sway/config.c b/sway/config.c
index 27308066..26e6f3e3 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -59,12 +59,12 @@ static void free_mode(struct sway_mode *mode) {
59} 59}
60 60
61void free_config(struct sway_config *config) { 61void free_config(struct sway_config *config) {
62 config_clear_handler_context(config);
63
64 if (!config) { 62 if (!config) {
65 return; 63 return;
66 } 64 }
67 65
66 memset(&config->handler_context, 0, sizeof(config->handler_context));
67
68 // TODO: handle all currently unhandled lists as we add implementations 68 // TODO: handle all currently unhandled lists as we add implementations
69 if (config->symbols) { 69 if (config->symbols) {
70 for (int i = 0; i < config->symbols->length; ++i) { 70 for (int i = 0; i < config->symbols->length; ++i) {
@@ -107,7 +107,6 @@ void free_config(struct sway_config *config) {
107 list_free(config->command_policies); 107 list_free(config->command_policies);
108 list_free(config->feature_policies); 108 list_free(config->feature_policies);
109 list_free(config->ipc_policies); 109 list_free(config->ipc_policies);
110 free(config->current_bar);
111 free(config->floating_scroll_up_cmd); 110 free(config->floating_scroll_up_cmd);
112 free(config->floating_scroll_down_cmd); 111 free(config->floating_scroll_down_cmd);
113 free(config->floating_scroll_left_cmd); 112 free(config->floating_scroll_left_cmd);
@@ -514,37 +513,41 @@ bool load_include_configs(const char *path, struct sway_config *config) {
514 return true; 513 return true;
515} 514}
516 515
517void config_clear_handler_context(struct sway_config *config) {
518 free_input_config(config->handler_context.input_config);
519 free_seat_config(config->handler_context.seat_config);
520
521 memset(&config->handler_context, 0, sizeof(config->handler_context));
522}
523
524bool read_config(FILE *file, struct sway_config *config) { 516bool read_config(FILE *file, struct sway_config *config) {
525 bool success = true; 517 bool success = true;
526 enum cmd_status block = CMD_BLOCK_END;
527
528 int line_number = 0; 518 int line_number = 0;
529 char *line; 519 char *line;
520 list_t *stack = create_list();
530 while (!feof(file)) { 521 while (!feof(file)) {
522 char *block = stack->length ? stack->items[0] : NULL;
531 line = read_line(file); 523 line = read_line(file);
532 if (!line) { 524 if (!line) {
533 continue; 525 continue;
534 } 526 }
535 line_number++; 527 line_number++;
528 wlr_log(L_DEBUG, "Read line %d: %s", line_number, line);
536 line = strip_whitespace(line); 529 line = strip_whitespace(line);
537 if (line[0] == '#') { 530 if (line[0] == '#') {
538 free(line); 531 free(line);
539 continue; 532 continue;
540 } 533 }
534 if (strlen(line) == 0) {
535 free(line);
536 continue;
537 }
538 char *full = calloc(strlen(block ? block : "") + strlen(line) + 2, 1);
539 strcat(full, block ? block : "");
540 strcat(full, block ? " " : "");
541 strcat(full, line);
542 wlr_log(L_DEBUG, "Expanded line: %s", full);
541 struct cmd_results *res; 543 struct cmd_results *res;
542 if (block == CMD_BLOCK_COMMANDS) { 544 if (block && strcmp(block, "<commands>") == 0) {
543 // Special case 545 // Special case
544 res = config_commands_command(line); 546 res = config_commands_command(full);
545 } else { 547 } else {
546 res = config_command(line, block); 548 res = config_command(full);
547 } 549 }
550 free(full);
548 switch(res->status) { 551 switch(res->status) {
549 case CMD_FAILURE: 552 case CMD_FAILURE:
550 case CMD_INVALID: 553 case CMD_INVALID:
@@ -558,126 +561,41 @@ bool read_config(FILE *file, struct sway_config *config) {
558 list_add(config->cmd_queue, strdup(line)); 561 list_add(config->cmd_queue, strdup(line));
559 break; 562 break;
560 563
561 case CMD_BLOCK_MODE:
562 if (block == CMD_BLOCK_END) {
563 block = CMD_BLOCK_MODE;
564 } else {
565 wlr_log(L_ERROR, "Invalid block '%s'", line);
566 }
567 break;
568
569 case CMD_BLOCK_INPUT:
570 if (block == CMD_BLOCK_END) {
571 block = CMD_BLOCK_INPUT;
572 } else {
573 wlr_log(L_ERROR, "Invalid block '%s'", line);
574 }
575 break;
576
577 case CMD_BLOCK_SEAT:
578 if (block == CMD_BLOCK_END) {
579 block = CMD_BLOCK_SEAT;
580 } else {
581 wlr_log(L_ERROR, "Invalid block '%s'", line);
582 }
583 break;
584
585 case CMD_BLOCK_BAR:
586 if (block == CMD_BLOCK_END) {
587 block = CMD_BLOCK_BAR;
588 } else {
589 wlr_log(L_ERROR, "Invalid block '%s'", line);
590 }
591 break;
592
593 case CMD_BLOCK_BAR_COLORS:
594 if (block == CMD_BLOCK_BAR) {
595 block = CMD_BLOCK_BAR_COLORS;
596 } else {
597 wlr_log(L_ERROR, "Invalid block '%s'", line);
598 }
599 break;
600
601 case CMD_BLOCK_COMMANDS: 564 case CMD_BLOCK_COMMANDS:
602 if (block == CMD_BLOCK_END) { 565 wlr_log(L_DEBUG, "Entering commands block");
603 block = CMD_BLOCK_COMMANDS; 566 list_insert(stack, 0, "<commands>");
604 } else {
605 wlr_log(L_ERROR, "Invalid block '%s'", line);
606 }
607 break;
608
609 case CMD_BLOCK_IPC:
610 if (block == CMD_BLOCK_END) {
611 block = CMD_BLOCK_IPC;
612 } else {
613 wlr_log(L_ERROR, "Invalid block '%s'", line);
614 }
615 break; 567 break;
616 568
617 case CMD_BLOCK_IPC_EVENTS: 569 case CMD_BLOCK:
618 if (block == CMD_BLOCK_IPC) { 570 wlr_log(L_DEBUG, "Entering block '%s'", res->input);
619 block = CMD_BLOCK_IPC_EVENTS; 571 list_insert(stack, 0, strdup(res->input));
620 } else { 572 if (strcmp(res->input, "bar") == 0) {
621 wlr_log(L_ERROR, "Invalid block '%s'", line); 573 config->current_bar = NULL;
622 } 574 }
623 break; 575 break;
624 576
625 case CMD_BLOCK_END: 577 case CMD_BLOCK_END:
626 switch(block) { 578 if (!block) {
627 case CMD_BLOCK_MODE: 579 wlr_log(L_DEBUG, "Unmatched '}' on line %i", line_number);
628 wlr_log(L_DEBUG, "End of mode block"); 580 success = false;
629 config->current_mode = config->modes->items[0];
630 block = CMD_BLOCK_END;
631 break;
632
633 case CMD_BLOCK_INPUT:
634 wlr_log(L_DEBUG, "End of input block");
635 block = CMD_BLOCK_END;
636 break;
637
638 case CMD_BLOCK_SEAT:
639 wlr_log(L_DEBUG, "End of seat block");
640 block = CMD_BLOCK_END;
641 break; 581 break;
582 }
583 wlr_log(L_DEBUG, "Exiting block '%s'", block);
584 list_del(stack, 0);
585 free(block);
642 586
643 case CMD_BLOCK_BAR: 587 if (strcmp(block, "bar") == 0) {
644 wlr_log(L_DEBUG, "End of bar block");
645 config->current_bar = NULL; 588 config->current_bar = NULL;
646 block = CMD_BLOCK_END;
647 break;
648
649 case CMD_BLOCK_BAR_COLORS:
650 wlr_log(L_DEBUG, "End of bar colors block");
651 block = CMD_BLOCK_BAR;
652 break;
653
654 case CMD_BLOCK_COMMANDS:
655 wlr_log(L_DEBUG, "End of commands block");
656 block = CMD_BLOCK_END;
657 break;
658
659 case CMD_BLOCK_IPC:
660 wlr_log(L_DEBUG, "End of IPC block");
661 block = CMD_BLOCK_END;
662 break;
663
664 case CMD_BLOCK_IPC_EVENTS:
665 wlr_log(L_DEBUG, "End of IPC events block");
666 block = CMD_BLOCK_IPC;
667 break;
668
669 case CMD_BLOCK_END:
670 wlr_log(L_ERROR, "Unmatched }");
671 break;
672
673 default:;
674 } 589 }
675 config_clear_handler_context(config); 590 memset(&config->handler_context, 0,
591 sizeof(config->handler_context));
676 default:; 592 default:;
677 } 593 }
678 free(line); 594 free(line);
679 free_cmd_results(res); 595 free_cmd_results(res);
680 } 596 }
597 list_foreach(stack, free);
598 list_free(stack);
681 599
682 return success; 600 return success;
683} 601}