Merge pull request #492 from mikkeloscar/swaybar-multi-output

Display single swaybar on multiple outputs
This commit is contained in:
Drew DeVault 2016-02-27 16:18:50 -05:00
commit 3453910c3e
14 changed files with 259 additions and 147 deletions

View file

@ -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, i);
}
}
load_swaybars();
arrange_windows(&root_container, -1, -1);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);

View file

@ -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);
}
@ -79,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]);
@ -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,20 @@ void terminate_swaybg(pid_t pid) {
}
}
void load_swaybars(swayc_t *output, int output_idx) {
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;
@ -558,7 +554,7 @@ void load_swaybars(swayc_t *output, int output_idx) {
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;
}
@ -571,13 +567,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 bar id '%s'", bar->id);
invoke_swaybar(bar);
}
list_free(bars);
@ -700,8 +696,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
}
}
// load swaybars for output
load_swaybars(output, output_i);
// reload swaybars
load_swaybars();
}
char *do_var_replacement(char *str) {
@ -889,6 +885,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");

View file

@ -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);

View file

@ -628,6 +628,17 @@ json_object *ipc_json_describe_bar_config(struct bar_config *bar) {
json_object_object_add(json, "colors", colors);
// Add outputs if defined
if (bar->outputs && bar->outputs->length > 0) {
json_object *outputs = json_object_new_array();
int i;
for (i = 0; i < bar->outputs->length; ++i) {
const char *name = bar->outputs->items[i];
json_object_array_add(outputs, json_object_new_string(name));
}
json_object_object_add(json, "outputs", outputs);
}
return json;
}

View file

@ -228,6 +228,10 @@ int main(int argc, char **argv) {
ipc_terminate();
if (config) {
free_config(config);
}
return exit_value;
}