diff options
Diffstat (limited to 'sway/config.c')
-rw-r--r-- | sway/config.c | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/sway/config.c b/sway/config.c index 65dba365..296e164c 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -23,6 +23,8 @@ | |||
23 | 23 | ||
24 | struct sway_config *config = NULL; | 24 | struct sway_config *config = NULL; |
25 | 25 | ||
26 | static void terminate_swaybar(pid_t pid); | ||
27 | |||
26 | static void free_variable(struct sway_variable *var) { | 28 | static void free_variable(struct sway_variable *var) { |
27 | free(var->name); | 29 | free(var->name); |
28 | free(var->value); | 30 | free(var->value); |
@@ -60,6 +62,11 @@ static void free_bar(struct bar_config *bar) { | |||
60 | if (bar->outputs) { | 62 | if (bar->outputs) { |
61 | free_flat_list(bar->outputs); | 63 | free_flat_list(bar->outputs); |
62 | } | 64 | } |
65 | |||
66 | if (bar->pid != 0) { | ||
67 | terminate_swaybar(bar->pid); | ||
68 | } | ||
69 | |||
63 | free(bar); | 70 | free(bar); |
64 | } | 71 | } |
65 | 72 | ||
@@ -79,7 +86,7 @@ static void free_workspace_output(struct workspace_output *wo) { | |||
79 | free(wo); | 86 | free(wo); |
80 | } | 87 | } |
81 | 88 | ||
82 | static void free_config(struct sway_config *config) { | 89 | void free_config(struct sway_config *config) { |
83 | int i; | 90 | int i; |
84 | for (i = 0; i < config->symbols->length; ++i) { | 91 | for (i = 0; i < config->symbols->length; ++i) { |
85 | free_variable(config->symbols->items[i]); | 92 | free_variable(config->symbols->items[i]); |
@@ -471,32 +478,23 @@ void merge_output_config(struct output_config *dst, struct output_config *src) { | |||
471 | } | 478 | } |
472 | } | 479 | } |
473 | 480 | ||
474 | static void invoke_swaybar(swayc_t *output, struct bar_config *bar, int output_i) { | 481 | static void invoke_swaybar(struct bar_config *bar) { |
475 | sway_log(L_DEBUG, "Invoking swaybar for output %s[%d] and bar %s", output->name, output_i, bar->id); | 482 | bar->pid = fork(); |
476 | 483 | if (bar->pid == 0) { | |
477 | size_t bufsize = 4; | ||
478 | char output_id[bufsize]; | ||
479 | snprintf(output_id, bufsize, "%d", output_i); | ||
480 | output_id[bufsize-1] = 0; | ||
481 | |||
482 | pid_t *pid = malloc(sizeof(pid_t)); | ||
483 | *pid = fork(); | ||
484 | if (*pid == 0) { | ||
485 | if (!bar->swaybar_command) { | 484 | if (!bar->swaybar_command) { |
486 | char *const cmd[] = { | 485 | char *const cmd[] = { |
487 | "swaybar", | 486 | "swaybar", |
488 | "-b", | 487 | "-b", |
489 | bar->id, | 488 | bar->id, |
490 | output_id, | ||
491 | NULL, | 489 | NULL, |
492 | }; | 490 | }; |
493 | 491 | ||
494 | execvp(cmd[0], cmd); | 492 | execvp(cmd[0], cmd); |
495 | } else { | 493 | } else { |
496 | // run custom swaybar | 494 | // run custom swaybar |
497 | int len = strlen(bar->swaybar_command) + strlen(bar->id) + strlen(output_id) + 6; | 495 | int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; |
498 | char *command = malloc(len * sizeof(char)); | 496 | char *command = malloc(len * sizeof(char)); |
499 | snprintf(command, len, "%s -b %s %s", bar->swaybar_command, bar->id, output_id); | 497 | snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); |
500 | 498 | ||
501 | char *const cmd[] = { | 499 | char *const cmd[] = { |
502 | "sh", | 500 | "sh", |
@@ -509,30 +507,15 @@ static void invoke_swaybar(swayc_t *output, struct bar_config *bar, int output_i | |||
509 | free(command); | 507 | free(command); |
510 | } | 508 | } |
511 | } | 509 | } |
512 | |||
513 | // add swaybar pid to output | ||
514 | list_add(output->bar_pids, pid); | ||
515 | } | 510 | } |
516 | 511 | ||
517 | void terminate_swaybars(list_t *pids) { | 512 | static void terminate_swaybar(pid_t pid) { |
518 | int i, ret; | 513 | int ret = kill(pid, SIGTERM); |
519 | pid_t *pid; | 514 | if (ret != 0) { |
520 | for (i = 0; i < pids->length; ++i) { | 515 | sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", pid); |
521 | pid = pids->items[i]; | 516 | } else { |
522 | ret = kill(*pid, SIGTERM); | 517 | int status; |
523 | if (ret != 0) { | 518 | waitpid(pid, &status, 0); |
524 | sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", *pid); | ||
525 | } else { | ||
526 | int status; | ||
527 | waitpid(*pid, &status, 0); | ||
528 | } | ||
529 | } | ||
530 | |||
531 | // empty pids list | ||
532 | for (i = 0; i < pids->length; ++i) { | ||
533 | pid = pids->items[i]; | ||
534 | list_del(pids, i); | ||
535 | free(pid); | ||
536 | } | 519 | } |
537 | } | 520 | } |
538 | 521 | ||
@@ -546,7 +529,20 @@ void terminate_swaybg(pid_t pid) { | |||
546 | } | 529 | } |
547 | } | 530 | } |
548 | 531 | ||
549 | void load_swaybars(swayc_t *output, int output_idx) { | 532 | static bool active_output(const char *name) { |
533 | int i; | ||
534 | swayc_t *cont = NULL; | ||
535 | for (i = 0; i < root_container.children->length; ++i) { | ||
536 | cont = root_container.children->items[i]; | ||
537 | if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { | ||
538 | return true; | ||
539 | } | ||
540 | } | ||
541 | |||
542 | return false; | ||
543 | } | ||
544 | |||
545 | void load_swaybars() { | ||
550 | // Check for bars | 546 | // Check for bars |
551 | list_t *bars = create_list(); | 547 | list_t *bars = create_list(); |
552 | struct bar_config *bar = NULL; | 548 | struct bar_config *bar = NULL; |
@@ -558,7 +554,7 @@ void load_swaybars(swayc_t *output, int output_idx) { | |||
558 | int j; | 554 | int j; |
559 | for (j = 0; j < bar->outputs->length; ++j) { | 555 | for (j = 0; j < bar->outputs->length; ++j) { |
560 | char *o = bar->outputs->items[j]; | 556 | char *o = bar->outputs->items[j]; |
561 | if (!strcmp(o, "*") || !strcasecmp(o, output->name)) { | 557 | if (!strcmp(o, "*") || active_output(o)) { |
562 | apply = true; | 558 | apply = true; |
563 | break; | 559 | break; |
564 | } | 560 | } |
@@ -571,13 +567,13 @@ void load_swaybars(swayc_t *output, int output_idx) { | |||
571 | } | 567 | } |
572 | } | 568 | } |
573 | 569 | ||
574 | // terminate swaybar processes previously spawned for this | ||
575 | // output. | ||
576 | terminate_swaybars(output->bar_pids); | ||
577 | |||
578 | for (i = 0; i < bars->length; ++i) { | 570 | for (i = 0; i < bars->length; ++i) { |
579 | bar = bars->items[i]; | 571 | bar = bars->items[i]; |
580 | invoke_swaybar(output, bar, output_idx); | 572 | if (bar->pid != 0) { |
573 | terminate_swaybar(bar->pid); | ||
574 | } | ||
575 | sway_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); | ||
576 | invoke_swaybar(bar); | ||
581 | } | 577 | } |
582 | 578 | ||
583 | list_free(bars); | 579 | list_free(bars); |
@@ -700,8 +696,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { | |||
700 | } | 696 | } |
701 | } | 697 | } |
702 | 698 | ||
703 | // load swaybars for output | 699 | // reload swaybars |
704 | load_swaybars(output, output_i); | 700 | load_swaybars(); |
705 | } | 701 | } |
706 | 702 | ||
707 | char *do_var_replacement(char *str) { | 703 | char *do_var_replacement(char *str) { |
@@ -889,6 +885,7 @@ struct bar_config *default_bar_config(void) { | |||
889 | bar->strip_workspace_numbers = false; | 885 | bar->strip_workspace_numbers = false; |
890 | bar->binding_mode_indicator = true; | 886 | bar->binding_mode_indicator = true; |
891 | bar->tray_padding = 2; | 887 | bar->tray_padding = 2; |
888 | bar->pid = 0; | ||
892 | // set default colors | 889 | // set default colors |
893 | strcpy(bar->colors.background, "#000000ff"); | 890 | strcpy(bar->colors.background, "#000000ff"); |
894 | strcpy(bar->colors.statusline, "#ffffffff"); | 891 | strcpy(bar->colors.statusline, "#ffffffff"); |