aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-10-14 14:30:52 +0200
committerGitHub <noreply@github.com>2018-10-14 14:30:52 +0200
commitabde9d6627483256ccfd20bdeeaa52d7790b0426 (patch)
treea851a6f4f6356470123d5d870c5d12fb9de78f73
parentdbb169bf1588b524bde8a2b7bfb83d5e2bb9b506 (diff)
parent2a0c7ebd43a82197754586de32deb3cdbbfa3cd9 (diff)
Merge pull request #2808 from RedSoxFan/bar-subcommands
Fix bar subcommand handler structs and selection
-rw-r--r--include/sway/config.h2
-rw-r--r--sway/commands/bar.c115
-rw-r--r--sway/commands/bar/id.c2
-rw-r--r--sway/commands/bar/status_command.c2
-rw-r--r--sway/config/bar.c14
5 files changed, 79 insertions, 56 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index ca17a645..f21ecbb1 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -530,6 +530,8 @@ void free_sway_binding(struct sway_binding *sb);
void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding);
+void load_swaybar(struct bar_config *bar);
+
void load_swaybars(void);
void terminate_swaybg(pid_t pid);
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index 03f4c557..c808aef2 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -17,7 +17,6 @@ static struct cmd_handler bar_handlers[] = {
{ "height", bar_cmd_height },
{ "hidden_state", bar_cmd_hidden_state },
{ "icon_theme", bar_cmd_icon_theme },
- { "id", bar_cmd_id },
{ "mode", bar_cmd_mode },
{ "modifier", bar_cmd_modifier },
{ "output", bar_cmd_output },
@@ -27,7 +26,6 @@ static struct cmd_handler bar_handlers[] = {
{ "separator_symbol", bar_cmd_separator_symbol },
{ "status_command", bar_cmd_status_command },
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
- { "swaybar_command", bar_cmd_swaybar_command },
{ "tray_output", bar_cmd_tray_output },
{ "tray_padding", bar_cmd_tray_padding },
{ "workspace_buttons", bar_cmd_workspace_buttons },
@@ -36,54 +34,49 @@ static struct cmd_handler bar_handlers[] = {
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_config_handlers[] = {
- { "hidden_state", bar_cmd_hidden_state },
- { "mode", bar_cmd_mode }
+ { "id", bar_cmd_id },
+ { "swaybar_command", bar_cmd_swaybar_command },
};
+// Determines whether the subcommand is valid in any bar handler struct
+static bool is_subcommand(char *name) {
+ return find_handler(name, bar_handlers, sizeof(bar_handlers)) ||
+ find_handler(name, bar_config_handlers, sizeof(bar_config_handlers));
+}
+
struct cmd_results *cmd_bar(int argc, char **argv) {
struct cmd_results *error = NULL;
- if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
+ if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 2))) {
return error;
}
- if (find_handler(argv[0], bar_config_handlers,
- sizeof(bar_config_handlers))) {
- if (config->reading) {
- return config_subcommand(argv, argc, bar_config_handlers,
- sizeof(bar_config_handlers));
- }
- return cmd_results_new(CMD_FAILURE, "bar",
- "Can only be used in config file.");
- }
-
- if (argc > 1) {
- struct bar_config *bar = NULL;
- if (!find_handler(argv[0], bar_handlers, sizeof(bar_handlers))
- && find_handler(argv[1], bar_handlers, sizeof(bar_handlers))) {
- for (int i = 0; i < config->bars->length; ++i) {
- struct bar_config *item = config->bars->items[i];
- if (strcmp(item->id, argv[0]) == 0) {
- wlr_log(WLR_DEBUG, "Selecting bar: %s", argv[0]);
- bar = item;
- break;
- }
+ bool spawn = false;
+ struct bar_config *bar = 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) {
+ wlr_log(WLR_DEBUG, "Selecting bar: %s", argv[0]);
+ bar = item;
+ break;
}
+ }
+ if (!bar) {
+ spawn = !config->reading;
+ wlr_log(WLR_DEBUG, "Creating bar: %s", argv[0]);
+ bar = default_bar_config();
if (!bar) {
- wlr_log(WLR_DEBUG, "Creating bar: %s", argv[0]);
- bar = default_bar_config();
- if (!bar) {
- return cmd_results_new(CMD_FAILURE, "bar",
- "Unable to allocate bar state");
- }
-
- bar->id = strdup(argv[0]);
+ return cmd_results_new(CMD_FAILURE, "bar",
+ "Unable to allocate bar state");
}
- config->current_bar = bar;
- ++argv; --argc;
+
+ bar->id = strdup(argv[0]);
}
+ config->current_bar = bar;
+ ++argv; --argc;
}
- if (!config->current_bar) {
+ if (!config->current_bar && config->reading) {
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
@@ -92,18 +85,13 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
}
// set bar id
- for (int i = 0; i < config->bars->length; ++i) {
- if (bar == config->bars->items[i]) {
- const int len = 5 + numlen(i); // "bar-" + i + \0
- bar->id = malloc(len * sizeof(char));
- if (bar->id) {
- snprintf(bar->id, len, "bar-%d", i);
- } else {
- return cmd_results_new(CMD_FAILURE,
- "bar", "Unable to allocate bar ID");
- }
- break;
- }
+ const int len = 5 + numlen(config->bars->length - 1); // "bar-"+i+\0
+ 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,
+ "bar", "Unable to allocate bar ID");
}
// Set current bar
@@ -111,5 +99,32 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
wlr_log(WLR_DEBUG, "Creating bar %s", bar->id);
}
- return config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
+ if (find_handler(argv[0], bar_config_handlers,
+ sizeof(bar_config_handlers))) {
+ if (config->reading) {
+ return 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;
+ }
+ }
+ }
+ return cmd_results_new(CMD_INVALID, "bar",
+ "Can only be used in the config file.");
+ }
+
+ struct cmd_results *res =
+ config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
+ if (!config->reading) {
+ if (spawn) {
+ load_swaybar(config->current_bar);
+ }
+ config->current_bar = NULL;
+ }
+ return res;
}
diff --git a/sway/commands/bar/id.c b/sway/commands/bar/id.c
index 65fa69fd..7690a852 100644
--- a/sway/commands/bar/id.c
+++ b/sway/commands/bar/id.c
@@ -13,6 +13,8 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) {
const char *oldname = config->current_bar->id;
if (strcmp(name, oldname) == 0) {
return cmd_results_new(CMD_SUCCESS, NULL, NULL); // NOP
+ } else if (strcmp(name, "id") == 0) {
+ return cmd_results_new(CMD_INVALID, "id", "id cannot be 'id'");
}
// check if id is used by a previously defined bar
for (int i = 0; i < config->bars->length; ++i) {
diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c
index 5b4fdc87..490393f1 100644
--- a/sway/commands/bar/status_command.c
+++ b/sway/commands/bar/status_command.c
@@ -25,7 +25,7 @@ struct cmd_results *bar_cmd_status_command(int argc, char **argv) {
}
if (config->active && !config->validating) {
- load_swaybars();
+ load_swaybar(config->current_bar);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
diff --git a/sway/config/bar.c b/sway/config/bar.c
index c6899f57..5726e95b 100644
--- a/sway/config/bar.c
+++ b/sway/config/bar.c
@@ -226,13 +226,17 @@ static void invoke_swaybar(struct bar_config *bar) {
close(filedes[1]);
}
+void load_swaybar(struct bar_config *bar) {
+ if (bar->pid != 0) {
+ terminate_swaybar(bar->pid);
+ }
+ wlr_log(WLR_DEBUG, "Invoking swaybar for bar id '%s'", bar->id);
+ invoke_swaybar(bar);
+}
+
void load_swaybars(void) {
for (int i = 0; i < config->bars->length; ++i) {
struct bar_config *bar = config->bars->items[i];
- if (bar->pid != 0) {
- terminate_swaybar(bar->pid);
- }
- wlr_log(WLR_DEBUG, "Invoking swaybar for bar id '%s'", bar->id);
- invoke_swaybar(bar);
+ load_swaybar(bar);
}
}