swaybar: complete barconfig_update event handling

This adds complete support for the barconfig_update ipc event. This also
changes the bar command and subcommand handlers to correctly emit the
event. This makes it so all bar subcommands other than id and
swaybar_command are dynamically changeable at runtime. sway-bar.5 has
been updated accordingly
This commit is contained in:
Brian Ashworth 2019-09-02 21:41:11 -04:00 committed by Drew DeVault
parent 187306640b
commit 1fd2c6ba49
15 changed files with 412 additions and 321 deletions

View file

@ -4,6 +4,7 @@
#include <strings.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/ipc-server.h"
#include "log.h"
// Must be in alphabetical order for bsearch
@ -56,30 +57,27 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
return error;
}
bool spawn = false;
struct bar_config *bar = NULL;
char *id = NULL;
if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) {
for (int i = 0; i < config->bars->length; ++i) {
struct bar_config *item = config->bars->items[i];
if (strcmp(item->id, argv[0]) == 0) {
sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]);
bar = item;
config->current_bar = item;
break;
}
}
if (!bar) {
spawn = !config->reading;
sway_log(SWAY_DEBUG, "Creating bar: %s", argv[0]);
bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
bar->id = strdup(argv[0]);
if (!config->current_bar) {
id = strdup(argv[0]);
}
config->current_bar = bar;
++argv; --argc;
} else if (config->reading && !config->current_bar) {
int len = snprintf(NULL, 0, "bar-%d", config->bars->length) + 1;
id = malloc(len * sizeof(char));
if (!id) {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id");
}
snprintf(id, len, "bar-%d", config->bars->length);
} else if (!config->reading && strcmp(argv[0], "mode") != 0 &&
strcmp(argv[0], "hidden_state") != 0) {
if (is_subcommand(argv[0])) {
@ -90,56 +88,49 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
}
}
if (!config->current_bar) {
if (config->reading) {
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
// set bar id
int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
bar->id = malloc(len * sizeof(char));
if (bar->id) {
snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
} else {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
}
// Set current bar
config->current_bar = bar;
sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
if (id) {
sway_log(SWAY_DEBUG, "Creating bar: %s", id);
config->current_bar = default_bar_config();
if (!config->current_bar) {
free(id);
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar config");
}
config->current_bar->id = id;
}
struct cmd_results *res = NULL;
if (find_handler(argv[0], bar_config_handlers,
sizeof(bar_config_handlers))) {
if (config->reading) {
return config_subcommand(argv, argc, bar_config_handlers,
res = config_subcommand(argv, argc, bar_config_handlers,
sizeof(bar_config_handlers));
} else if (spawn) {
for (int i = config->bars->length - 1; i >= 0; i--) {
struct bar_config *bar = config->bars->items[i];
if (bar == config->current_bar) {
list_del(config->bars, i);
free_bar_config(bar);
break;
}
}
} else {
res = cmd_results_new(CMD_INVALID,
"Can only be used in the config file");
}
return cmd_results_new(CMD_INVALID,
"Can only be used in the config file.");
} else {
res = config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
}
struct cmd_results *res =
config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
if (!config->reading) {
if (spawn) {
if (res && res->status != CMD_SUCCESS) {
if (id) {
free_bar_config(config->current_bar);
id = NULL;
}
return res;
}
if (id) {
list_add(config->bars, config->current_bar);
}
if (!config->reading && config->current_bar) {
ipc_event_barconfig_update(config->current_bar);
if (id) {
load_swaybar(config->current_bar);
}
config->current_bar = NULL;
}
return res;
}

View file

@ -23,7 +23,7 @@ static struct cmd_results *bar_set_hidden_state(struct bar_config *bar,
return cmd_results_new(CMD_INVALID, "Invalid value %s", hidden_state);
}
if (strcmp(old_state, bar->hidden_state) != 0) {
if (!config->reading) {
if (!config->current_bar) {
ipc_event_barconfig_update(bar);
}
sway_log(SWAY_DEBUG, "Setting hidden_state: '%s' for bar: %s",
@ -47,6 +47,12 @@ struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) {
"Unexpected value %s in config mode", argv[1]);
}
if (config->current_bar && argc == 2 &&
strcmp(config->current_bar->id, argv[1]) != 0) {
return cmd_results_new(CMD_INVALID, "Conflicting bar ids: %s and %s",
config->current_bar->id, argv[1]);
}
const char *state = argv[0];
if (config->reading) {
error = bar_set_hidden_state(config->current_bar, state);

View file

@ -27,7 +27,7 @@ static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode
}
if (strcmp(old_mode, bar->mode) != 0) {
if (!config->reading) {
if (!config->current_bar) {
ipc_event_barconfig_update(bar);
}
sway_log(SWAY_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id);
@ -51,6 +51,12 @@ struct cmd_results *bar_cmd_mode(int argc, char **argv) {
"Unexpected value %s in config mode", argv[1]);
}
if (config->current_bar && argc == 2 &&
strcmp(config->current_bar->id, argv[1]) != 0) {
return cmd_results_new(CMD_INVALID, "Conflicting bar ids: %s and %s",
config->current_bar->id, argv[1]);
}
const char *mode = argv[0];
if (config->reading) {
error = bar_set_mode(config->current_bar, mode);

View file

@ -21,16 +21,19 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) {
bool add_output = true;
if (strcmp("*", output) == 0) {
// remove all previous defined outputs and replace with '*'
for (int i = 0; i < outputs->length; ++i) {
free(outputs->items[i]);
list_del(outputs, i);
while (outputs->length) {
free(outputs->items[0]);
list_del(outputs, 0);
}
} else {
// only add output if not already defined with either the same
// name or as '*'
// only add output if not already defined, if the list has '*', remove
// it, in favor of a manual list
for (int i = 0; i < outputs->length; ++i) {
const char *find = outputs->items[i];
if (strcmp("*", find) == 0 || strcmp(output, find) == 0) {
if (strcmp("*", find) == 0) {
free(outputs->items[i]);
list_del(outputs, i);
} else if (strcmp(output, find) == 0) {
add_output = false;
break;
}

View file

@ -19,10 +19,5 @@ struct cmd_results *bar_cmd_status_command(int argc, char **argv) {
} else {
free(new_command);
}
if (config->active && !config->validating) {
load_swaybar(config->current_bar);
}
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -24,9 +24,21 @@ struct cmd_results *bar_cmd_tray_output(int argc, char **argv) {
free(outputs->items[i]);
}
outputs->length = 0;
} else if (strcmp(argv[0], "*") == 0) {
sway_log(SWAY_DEBUG, "Showing tray on all outputs for bar: %s",
config->current_bar->id);
while (outputs->length) {
free(outputs->items[0]);
list_del(outputs, 0);
}
return cmd_results_new(CMD_SUCCESS, NULL);
} else {
sway_log(SWAY_DEBUG, "Showing tray on output '%s' for bar: %s", argv[0],
config->current_bar->id);
if (outputs->length == 1 && strcmp(outputs->items[0], "none") == 0) {
free(outputs->items[0]);
list_del(outputs, 0);
}
}
list_add(outputs, strdup(argv[0]));