From 6d57f03028b4b32b4377d869be8de9a7e6aa55ee Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Mon, 22 Feb 2016 17:27:17 +0100 Subject: Make single bar handle multiple outputs --- include/bar/bar.h | 11 ++++++++--- include/bar/config.h | 3 +++ include/bar/ipc.h | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/bar/bar.h b/include/bar/bar.h index 89496da6..c20efc55 100644 --- a/include/bar/bar.h +++ b/include/bar/bar.h @@ -8,8 +8,7 @@ struct bar { struct config *config; struct status_line *status; - struct output *output; - /* list_t *outputs; */ + list_t *outputs; int ipc_event_socketfd; int ipc_socketfd; @@ -22,6 +21,7 @@ struct output { struct registry *registry; list_t *workspaces; char *name; + int idx; }; struct workspace { @@ -35,7 +35,12 @@ struct workspace { /** * Setup bar. */ -void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output); +void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id); + +/** + * Create new output struct from name. + */ +struct output *new_output(const char *name); /** * Bar mainloop. diff --git a/include/bar/config.h b/include/bar/config.h index 508b9c42..dfad0ba4 100644 --- a/include/bar/config.h +++ b/include/bar/config.h @@ -4,6 +4,8 @@ #include #include +#include "list.h" + /** * Colors for a box with background, border and text colors. */ @@ -25,6 +27,7 @@ struct config { bool strip_workspace_numbers; bool binding_mode_indicator; bool workspace_buttons; + list_t *outputs; int height; diff --git a/include/bar/ipc.h b/include/bar/ipc.h index c3f661f8..741c067b 100644 --- a/include/bar/ipc.h +++ b/include/bar/ipc.h @@ -6,7 +6,7 @@ /** * Initialize ipc connection to sway and get sway state, outputs, bar_config. */ -void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id); +void ipc_bar_init(struct bar *bar, const char *bar_id); /** * Handle ipc event from sway. -- cgit v1.2.3-70-g09d2 From bad4e22f3be658f8688f308e54a48b65c071a952 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Tue, 23 Feb 2016 14:25:09 +0100 Subject: Make sway spawn only one bar per bar config --- include/config.h | 4 +-- include/container.h | 4 +-- sway/commands.c | 2 +- sway/config.c | 70 +++++++++++++++++++++-------------------------------- sway/container.c | 5 ---- 5 files changed, 31 insertions(+), 54 deletions(-) (limited to 'include') 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 { bool strip_workspace_numbers; bool binding_mode_indicator; bool verbose; + pid_t pid; struct { char background[10]; char statusline[10]; @@ -226,8 +227,7 @@ int sway_mouse_binding_cmp_qsort(const void *a, const void *b); int sway_mouse_binding_cmp_buttons(const void *a, const void *b); void free_sway_mouse_binding(struct sway_mouse_binding *smb); -void load_swaybars(swayc_t *output, int output_idx); -void terminate_swaybars(list_t *pids); +void load_swaybars(swayc_t *output); void terminate_swaybg(pid_t pid); /** 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 { char *class; char *app_id; - // Used by output containers to keep track of swaybar/swaybg child - // processes. - list_t *bar_pids; + // Used by output containers to keep track of swaybg child processes. pid_t bg_pid; 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) { for (i = 0; i < root_container.children->length; ++i) { cont = root_container.children->items[i]; if (cont->type == C_OUTPUT) { - load_swaybars(cont, i); + load_swaybars(cont); } } 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 @@ struct sway_config *config = NULL; +static void terminate_swaybar(pid_t pid); + static void free_variable(struct sway_variable *var) { free(var->name); free(var->value); @@ -60,6 +62,11 @@ static void free_bar(struct bar_config *bar) { if (bar->outputs) { free_flat_list(bar->outputs); } + + if (bar->pid != 0) { + terminate_swaybar(bar->pid); + } + free(bar); } @@ -471,32 +478,23 @@ void merge_output_config(struct output_config *dst, struct output_config *src) { } } -static void invoke_swaybar(swayc_t *output, struct bar_config *bar, int output_i) { - sway_log(L_DEBUG, "Invoking swaybar for output %s[%d] and bar %s", output->name, output_i, bar->id); - - size_t bufsize = 4; - char output_id[bufsize]; - snprintf(output_id, bufsize, "%d", output_i); - output_id[bufsize-1] = 0; - - pid_t *pid = malloc(sizeof(pid_t)); - *pid = fork(); - if (*pid == 0) { +static void invoke_swaybar(struct bar_config *bar) { + bar->pid = fork(); + if (bar->pid == 0) { if (!bar->swaybar_command) { char *const cmd[] = { "swaybar", "-b", bar->id, - output_id, NULL, }; execvp(cmd[0], cmd); } else { // run custom swaybar - int len = strlen(bar->swaybar_command) + strlen(bar->id) + strlen(output_id) + 6; + int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; char *command = malloc(len * sizeof(char)); - snprintf(command, len, "%s -b %s %s", bar->swaybar_command, bar->id, output_id); + snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); char *const cmd[] = { "sh", @@ -509,30 +507,15 @@ static void invoke_swaybar(swayc_t *output, struct bar_config *bar, int output_i free(command); } } - - // add swaybar pid to output - list_add(output->bar_pids, pid); } -void terminate_swaybars(list_t *pids) { - int i, ret; - pid_t *pid; - for (i = 0; i < pids->length; ++i) { - pid = pids->items[i]; - ret = kill(*pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", *pid); - } else { - int status; - waitpid(*pid, &status, 0); - } - } - - // empty pids list - for (i = 0; i < pids->length; ++i) { - pid = pids->items[i]; - list_del(pids, i); - free(pid); +static void terminate_swaybar(pid_t pid) { + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); } } @@ -546,7 +529,7 @@ void terminate_swaybg(pid_t pid) { } } -void load_swaybars(swayc_t *output, int output_idx) { +void load_swaybars(swayc_t *output) { // Check for bars list_t *bars = create_list(); struct bar_config *bar = NULL; @@ -571,13 +554,13 @@ void load_swaybars(swayc_t *output, int output_idx) { } } - // terminate swaybar processes previously spawned for this - // output. - terminate_swaybars(output->bar_pids); - for (i = 0; i < bars->length; ++i) { bar = bars->items[i]; - invoke_swaybar(output, bar, output_idx); + if (bar->pid != 0) { + terminate_swaybar(bar->pid); + } + sway_log(L_DEBUG, "Invoking swaybar for output %s and bar '%s'", output->name, bar->id); + invoke_swaybar(bar); } list_free(bars); @@ -701,7 +684,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } // load swaybars for output - load_swaybars(output, output_i); + load_swaybars(output); } char *do_var_replacement(char *str) { @@ -889,6 +872,7 @@ struct bar_config *default_bar_config(void) { bar->strip_workspace_numbers = false; bar->binding_mode_indicator = true; bar->tray_padding = 2; + bar->pid = 0; // set default colors strcpy(bar->colors.background, "#000000ff"); 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) { if (cont->app_id) { free(cont->app_id); } - if (cont->bar_pids) { - terminate_swaybars(cont->bar_pids); - free_flat_list(cont->bar_pids); - } if (cont->bg_pid != 0) { terminate_swaybg(cont->bg_pid); } @@ -123,7 +119,6 @@ swayc_t *new_output(wlc_handle handle) { output->width = size->w; output->height = size->h; output->unmanaged = create_list(); - output->bar_pids = create_list(); output->bg_pid = 0; apply_output_config(oc, output); -- cgit v1.2.3-70-g09d2 From e4c1176b726ab9391a3c9eb69670c09ffe3e4f0a Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Tue, 23 Feb 2016 15:06:04 +0100 Subject: Differentiate between all or no outputs --- include/bar/config.h | 1 + swaybar/config.c | 1 + swaybar/ipc.c | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/bar/config.h b/include/bar/config.h index dfad0ba4..fc938c38 100644 --- a/include/bar/config.h +++ b/include/bar/config.h @@ -27,6 +27,7 @@ struct config { bool strip_workspace_numbers; bool binding_mode_indicator; bool workspace_buttons; + bool all_outputs; list_t *outputs; int height; diff --git a/swaybar/config.c b/swaybar/config.c index 5609f85f..fddea791 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -51,6 +51,7 @@ struct config *init_config() { config->strip_workspace_numbers = false; config->binding_mode_indicator = true; config->workspace_buttons = true; + config->all_outputs = false; config->outputs = create_list(); /* height */ diff --git a/swaybar/ipc.c b/swaybar/ipc.c index b5ae6747..312c79b9 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -78,10 +78,13 @@ static void ipc_parse_config(struct config *config, const char *payload) { output = json_object_array_get_idx(outputs, i); output_str = json_object_get_string(output); if (strcmp("*", output_str) == 0) { + config->all_outputs = true; break; } list_add(config->outputs, strdup(output_str)); } + } else { + config->all_outputs = true; } if (colors) { @@ -251,7 +254,7 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) { } bool use_output = false; - if (bar->config->outputs->length == 0) { + if (bar->config->all_outputs) { use_output = true; } else { int j = 0; -- cgit v1.2.3-70-g09d2 From e15a8a03769736f588f68ae5e1cc24611ed334ae Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Wed, 24 Feb 2016 18:52:57 +0100 Subject: Improve how swaybars are spawned --- include/config.h | 2 +- sway/commands.c | 9 +-------- sway/config.c | 23 ++++++++++++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/include/config.h b/include/config.h index 0b19580c..8907e019 100644 --- a/include/config.h +++ b/include/config.h @@ -227,7 +227,7 @@ int sway_mouse_binding_cmp_qsort(const void *a, const void *b); int sway_mouse_binding_cmp_buttons(const void *a, const void *b); void free_sway_mouse_binding(struct sway_mouse_binding *smb); -void load_swaybars(swayc_t *output); +void load_swaybars(); void terminate_swaybg(pid_t pid); /** diff --git a/sway/commands.c b/sway/commands.c index 548b7d9f..3b8556ca 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -1526,14 +1526,7 @@ static struct cmd_results *cmd_reload(int argc, char **argv) { } if (!load_config(NULL)) return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); - int i; - swayc_t *cont = NULL; - for (i = 0; i < root_container.children->length; ++i) { - cont = root_container.children->items[i]; - if (cont->type == C_OUTPUT) { - load_swaybars(cont); - } - } + load_swaybars(); arrange_windows(&root_container, -1, -1); return cmd_results_new(CMD_SUCCESS, NULL, NULL); diff --git a/sway/config.c b/sway/config.c index c34a660e..16adaf0d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -529,7 +529,20 @@ void terminate_swaybg(pid_t pid) { } } -void load_swaybars(swayc_t *output) { +static bool active_output(const char *name) { + int i; + swayc_t *cont = NULL; + for (i = 0; i < root_container.children->length; ++i) { + cont = root_container.children->items[i]; + if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { + return true; + } + } + + return false; +} + +void load_swaybars() { // Check for bars list_t *bars = create_list(); struct bar_config *bar = NULL; @@ -541,7 +554,7 @@ void load_swaybars(swayc_t *output) { int j; for (j = 0; j < bar->outputs->length; ++j) { char *o = bar->outputs->items[j]; - if (!strcmp(o, "*") || !strcasecmp(o, output->name)) { + if (!strcmp(o, "*") || active_output(o)) { apply = true; break; } @@ -559,7 +572,7 @@ void load_swaybars(swayc_t *output) { if (bar->pid != 0) { terminate_swaybar(bar->pid); } - sway_log(L_DEBUG, "Invoking swaybar for output %s and bar '%s'", output->name, bar->id); + sway_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); invoke_swaybar(bar); } @@ -683,8 +696,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) { } } - // load swaybars for output - load_swaybars(output); + // reload swaybars + load_swaybars(); } char *do_var_replacement(char *str) { -- cgit v1.2.3-70-g09d2 From 67bbcceba1433e41b5edfca32532b7d55a39a395 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Wed, 24 Feb 2016 18:53:09 +0100 Subject: Free config before exiting sway. Apart from freeing the sway_config struct, this also terminates the swaybars spawned by sway, since they are linked by PID to the bar config structs. --- include/config.h | 4 ++++ sway/config.c | 2 +- sway/main.c | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/config.h b/include/config.h index 8907e019..d77872ee 100644 --- a/include/config.h +++ b/include/config.h @@ -192,6 +192,10 @@ bool load_config(const char *file); /** Reads the config from the given FILE. */ bool read_config(FILE *file, bool is_active); +/** + * Free config struct + */ +void free_config(struct sway_config *config); /** * Does variable replacement for a string based on the config's currently loaded variables. */ diff --git a/sway/config.c b/sway/config.c index 16adaf0d..296e164c 100644 --- a/sway/config.c +++ b/sway/config.c @@ -86,7 +86,7 @@ static void free_workspace_output(struct workspace_output *wo) { free(wo); } -static void free_config(struct sway_config *config) { +void free_config(struct sway_config *config) { int i; for (i = 0; i < config->symbols->length; ++i) { free_variable(config->symbols->items[i]); diff --git a/sway/main.c b/sway/main.c index 7ea392b6..7c712281 100644 --- a/sway/main.c +++ b/sway/main.c @@ -228,6 +228,10 @@ int main(int argc, char **argv) { ipc_terminate(); + if (config) { + free_config(config); + } + return exit_value; } -- cgit v1.2.3-70-g09d2