diff options
author | Mikkel Oscar Lyderik <mikkeloscar@gmail.com> | 2016-02-23 14:25:09 +0100 |
---|---|---|
committer | Mikkel Oscar Lyderik <mikkeloscar@gmail.com> | 2016-02-27 22:12:35 +0100 |
commit | bad4e22f3be658f8688f308e54a48b65c071a952 (patch) | |
tree | d4cbcd353d981d8fb1e0bb7eb4b78dc3aea023d8 | |
parent | Make single bar handle multiple outputs (diff) | |
download | sway-bad4e22f3be658f8688f308e54a48b65c071a952.tar.gz sway-bad4e22f3be658f8688f308e54a48b65c071a952.tar.zst sway-bad4e22f3be658f8688f308e54a48b65c071a952.zip |
Make sway spawn only one bar per bar config
-rw-r--r-- | include/config.h | 4 | ||||
-rw-r--r-- | include/container.h | 4 | ||||
-rw-r--r-- | sway/commands.c | 2 | ||||
-rw-r--r-- | sway/config.c | 70 | ||||
-rw-r--r-- | sway/container.c | 5 |
5 files changed, 31 insertions, 54 deletions
diff --git a/include/config.h b/include/config.h index 655d0a9c..0b19580c 100644 --- a/include/config.h +++ b/include/config.h | |||
@@ -125,6 +125,7 @@ struct bar_config { | |||
125 | bool strip_workspace_numbers; | 125 | bool strip_workspace_numbers; |
126 | bool binding_mode_indicator; | 126 | bool binding_mode_indicator; |
127 | bool verbose; | 127 | bool verbose; |
128 | pid_t pid; | ||
128 | struct { | 129 | struct { |
129 | char background[10]; | 130 | char background[10]; |
130 | char statusline[10]; | 131 | char statusline[10]; |
@@ -226,8 +227,7 @@ int sway_mouse_binding_cmp_qsort(const void *a, const void *b); | |||
226 | int sway_mouse_binding_cmp_buttons(const void *a, const void *b); | 227 | int sway_mouse_binding_cmp_buttons(const void *a, const void *b); |
227 | void free_sway_mouse_binding(struct sway_mouse_binding *smb); | 228 | void free_sway_mouse_binding(struct sway_mouse_binding *smb); |
228 | 229 | ||
229 | void load_swaybars(swayc_t *output, int output_idx); | 230 | void load_swaybars(swayc_t *output); |
230 | void terminate_swaybars(list_t *pids); | ||
231 | void terminate_swaybg(pid_t pid); | 231 | void terminate_swaybg(pid_t pid); |
232 | 232 | ||
233 | /** | 233 | /** |
diff --git a/include/container.h b/include/container.h index d5126e74..a96beab9 100644 --- a/include/container.h +++ b/include/container.h | |||
@@ -82,9 +82,7 @@ struct sway_container { | |||
82 | char *class; | 82 | char *class; |
83 | char *app_id; | 83 | char *app_id; |
84 | 84 | ||
85 | // Used by output containers to keep track of swaybar/swaybg child | 85 | // Used by output containers to keep track of swaybg child processes. |
86 | // processes. | ||
87 | list_t *bar_pids; | ||
88 | pid_t bg_pid; | 86 | pid_t bg_pid; |
89 | 87 | ||
90 | int gaps; | 88 | int gaps; |
diff --git a/sway/commands.c b/sway/commands.c index c4b7f6ab..548b7d9f 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -1531,7 +1531,7 @@ static struct cmd_results *cmd_reload(int argc, char **argv) { | |||
1531 | for (i = 0; i < root_container.children->length; ++i) { | 1531 | for (i = 0; i < root_container.children->length; ++i) { |
1532 | cont = root_container.children->items[i]; | 1532 | cont = root_container.children->items[i]; |
1533 | if (cont->type == C_OUTPUT) { | 1533 | if (cont->type == C_OUTPUT) { |
1534 | load_swaybars(cont, i); | 1534 | load_swaybars(cont); |
1535 | } | 1535 | } |
1536 | } | 1536 | } |
1537 | 1537 | ||
diff --git a/sway/config.c b/sway/config.c index 65dba365..c34a660e 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 | ||
@@ -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,7 @@ void terminate_swaybg(pid_t pid) { | |||
546 | } | 529 | } |
547 | } | 530 | } |
548 | 531 | ||
549 | void load_swaybars(swayc_t *output, int output_idx) { | 532 | void load_swaybars(swayc_t *output) { |
550 | // Check for bars | 533 | // Check for bars |
551 | list_t *bars = create_list(); | 534 | list_t *bars = create_list(); |
552 | struct bar_config *bar = NULL; | 535 | struct bar_config *bar = NULL; |
@@ -571,13 +554,13 @@ void load_swaybars(swayc_t *output, int output_idx) { | |||
571 | } | 554 | } |
572 | } | 555 | } |
573 | 556 | ||
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) { | 557 | for (i = 0; i < bars->length; ++i) { |
579 | bar = bars->items[i]; | 558 | bar = bars->items[i]; |
580 | invoke_swaybar(output, bar, output_idx); | 559 | if (bar->pid != 0) { |
560 | terminate_swaybar(bar->pid); | ||
561 | } | ||
562 | sway_log(L_DEBUG, "Invoking swaybar for output %s and bar '%s'", output->name, bar->id); | ||
563 | invoke_swaybar(bar); | ||
581 | } | 564 | } |
582 | 565 | ||
583 | list_free(bars); | 566 | list_free(bars); |
@@ -701,7 +684,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { | |||
701 | } | 684 | } |
702 | 685 | ||
703 | // load swaybars for output | 686 | // load swaybars for output |
704 | load_swaybars(output, output_i); | 687 | load_swaybars(output); |
705 | } | 688 | } |
706 | 689 | ||
707 | char *do_var_replacement(char *str) { | 690 | char *do_var_replacement(char *str) { |
@@ -889,6 +872,7 @@ struct bar_config *default_bar_config(void) { | |||
889 | bar->strip_workspace_numbers = false; | 872 | bar->strip_workspace_numbers = false; |
890 | bar->binding_mode_indicator = true; | 873 | bar->binding_mode_indicator = true; |
891 | bar->tray_padding = 2; | 874 | bar->tray_padding = 2; |
875 | bar->pid = 0; | ||
892 | // set default colors | 876 | // set default colors |
893 | strcpy(bar->colors.background, "#000000ff"); | 877 | strcpy(bar->colors.background, "#000000ff"); |
894 | strcpy(bar->colors.statusline, "#ffffffff"); | 878 | strcpy(bar->colors.statusline, "#ffffffff"); |
diff --git a/sway/container.c b/sway/container.c index 746890c9..2db7b218 100644 --- a/sway/container.c +++ b/sway/container.c | |||
@@ -61,10 +61,6 @@ static void free_swayc(swayc_t *cont) { | |||
61 | if (cont->app_id) { | 61 | if (cont->app_id) { |
62 | free(cont->app_id); | 62 | free(cont->app_id); |
63 | } | 63 | } |
64 | if (cont->bar_pids) { | ||
65 | terminate_swaybars(cont->bar_pids); | ||
66 | free_flat_list(cont->bar_pids); | ||
67 | } | ||
68 | if (cont->bg_pid != 0) { | 64 | if (cont->bg_pid != 0) { |
69 | terminate_swaybg(cont->bg_pid); | 65 | terminate_swaybg(cont->bg_pid); |
70 | } | 66 | } |
@@ -123,7 +119,6 @@ swayc_t *new_output(wlc_handle handle) { | |||
123 | output->width = size->w; | 119 | output->width = size->w; |
124 | output->height = size->h; | 120 | output->height = size->h; |
125 | output->unmanaged = create_list(); | 121 | output->unmanaged = create_list(); |
126 | output->bar_pids = create_list(); | ||
127 | output->bg_pid = 0; | 122 | output->bg_pid = 0; |
128 | 123 | ||
129 | apply_output_config(oc, output); | 124 | apply_output_config(oc, output); |