aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2019-09-02 21:41:11 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2019-09-04 16:48:50 -1000
commit1fd2c6ba498e61f4fe823bf552f9d2fce8612de4 (patch)
tree8e2d9adab3451f1f05c76340d466a442c840e558 /sway/commands
parentseatop_default: only focus container on press (diff)
downloadsway-1fd2c6ba498e61f4fe823bf552f9d2fce8612de4.tar.gz
sway-1fd2c6ba498e61f4fe823bf552f9d2fce8612de4.tar.zst
sway-1fd2c6ba498e61f4fe823bf552f9d2fce8612de4.zip
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
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/bar.c93
-rw-r--r--sway/commands/bar/hidden_state.c8
-rw-r--r--sway/commands/bar/mode.c8
-rw-r--r--sway/commands/bar/output.c15
-rw-r--r--sway/commands/bar/status_command.c5
-rw-r--r--sway/commands/bar/tray_output.c12
6 files changed, 77 insertions, 64 deletions
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index 88580ffb..7370910d 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -4,6 +4,7 @@
4#include <strings.h> 4#include <strings.h>
5#include "sway/commands.h" 5#include "sway/commands.h"
6#include "sway/config.h" 6#include "sway/config.h"
7#include "sway/ipc-server.h"
7#include "log.h" 8#include "log.h"
8 9
9// Must be in alphabetical order for bsearch 10// Must be in alphabetical order for bsearch
@@ -56,30 +57,27 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
56 return error; 57 return error;
57 } 58 }
58 59
59 bool spawn = false; 60 char *id = NULL;
60 struct bar_config *bar = NULL;
61 if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) { 61 if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) {
62 for (int i = 0; i < config->bars->length; ++i) { 62 for (int i = 0; i < config->bars->length; ++i) {
63 struct bar_config *item = config->bars->items[i]; 63 struct bar_config *item = config->bars->items[i];
64 if (strcmp(item->id, argv[0]) == 0) { 64 if (strcmp(item->id, argv[0]) == 0) {
65 sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]); 65 sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]);
66 bar = item; 66 config->current_bar = item;
67 break; 67 break;
68 } 68 }
69 } 69 }
70 if (!bar) { 70 if (!config->current_bar) {
71 spawn = !config->reading; 71 id = strdup(argv[0]);
72 sway_log(SWAY_DEBUG, "Creating bar: %s", argv[0]);
73 bar = default_bar_config();
74 if (!bar) {
75 return cmd_results_new(CMD_FAILURE,
76 "Unable to allocate bar state");
77 }
78
79 bar->id = strdup(argv[0]);
80 } 72 }
81 config->current_bar = bar;
82 ++argv; --argc; 73 ++argv; --argc;
74 } else if (config->reading && !config->current_bar) {
75 int len = snprintf(NULL, 0, "bar-%d", config->bars->length) + 1;
76 id = malloc(len * sizeof(char));
77 if (!id) {
78 return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id");
79 }
80 snprintf(id, len, "bar-%d", config->bars->length);
83 } else if (!config->reading && strcmp(argv[0], "mode") != 0 && 81 } else if (!config->reading && strcmp(argv[0], "mode") != 0 &&
84 strcmp(argv[0], "hidden_state") != 0) { 82 strcmp(argv[0], "hidden_state") != 0) {
85 if (is_subcommand(argv[0])) { 83 if (is_subcommand(argv[0])) {
@@ -90,56 +88,49 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
90 } 88 }
91 } 89 }
92 90
93 if (!config->current_bar) { 91 if (id) {
94 if (config->reading) { 92 sway_log(SWAY_DEBUG, "Creating bar: %s", id);
95 // Create new bar with default values 93 config->current_bar = default_bar_config();
96 struct bar_config *bar = default_bar_config(); 94 if (!config->current_bar) {
97 if (!bar) { 95 free(id);
98 return cmd_results_new(CMD_FAILURE, 96 return cmd_results_new(CMD_FAILURE, "Unable to allocate bar config");
99 "Unable to allocate bar state");
100 }
101
102 // set bar id
103 int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
104 bar->id = malloc(len * sizeof(char));
105 if (bar->id) {
106 snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
107 } else {
108 return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
109 }
110
111 // Set current bar
112 config->current_bar = bar;
113 sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
114 } 97 }
98 config->current_bar->id = id;
115 } 99 }
116 100
101 struct cmd_results *res = NULL;
117 if (find_handler(argv[0], bar_config_handlers, 102 if (find_handler(argv[0], bar_config_handlers,
118 sizeof(bar_config_handlers))) { 103 sizeof(bar_config_handlers))) {
119 if (config->reading) { 104 if (config->reading) {
120 return config_subcommand(argv, argc, bar_config_handlers, 105 res = config_subcommand(argv, argc, bar_config_handlers,
121 sizeof(bar_config_handlers)); 106 sizeof(bar_config_handlers));
122 } else if (spawn) { 107 } else {
123 for (int i = config->bars->length - 1; i >= 0; i--) { 108 res = cmd_results_new(CMD_INVALID,
124 struct bar_config *bar = config->bars->items[i]; 109 "Can only be used in the config file");
125 if (bar == config->current_bar) { 110 }
126 list_del(config->bars, i); 111 } else {
127 free_bar_config(bar); 112 res = config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
128 break; 113 }
129 } 114
130 } 115 if (res && res->status != CMD_SUCCESS) {
116 if (id) {
117 free_bar_config(config->current_bar);
118 id = NULL;
131 } 119 }
132 return cmd_results_new(CMD_INVALID, 120 return res;
133 "Can only be used in the config file."); 121 }
122
123 if (id) {
124 list_add(config->bars, config->current_bar);
134 } 125 }
135 126
136 struct cmd_results *res = 127 if (!config->reading && config->current_bar) {
137 config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers)); 128 ipc_event_barconfig_update(config->current_bar);
138 if (!config->reading) { 129 if (id) {
139 if (spawn) {
140 load_swaybar(config->current_bar); 130 load_swaybar(config->current_bar);
141 } 131 }
142 config->current_bar = NULL; 132 config->current_bar = NULL;
143 } 133 }
134
144 return res; 135 return res;
145} 136}
diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c
index b2c2d245..1f08a5d2 100644
--- a/sway/commands/bar/hidden_state.c
+++ b/sway/commands/bar/hidden_state.c
@@ -23,7 +23,7 @@ static struct cmd_results *bar_set_hidden_state(struct bar_config *bar,
23 return cmd_results_new(CMD_INVALID, "Invalid value %s", hidden_state); 23 return cmd_results_new(CMD_INVALID, "Invalid value %s", hidden_state);
24 } 24 }
25 if (strcmp(old_state, bar->hidden_state) != 0) { 25 if (strcmp(old_state, bar->hidden_state) != 0) {
26 if (!config->reading) { 26 if (!config->current_bar) {
27 ipc_event_barconfig_update(bar); 27 ipc_event_barconfig_update(bar);
28 } 28 }
29 sway_log(SWAY_DEBUG, "Setting hidden_state: '%s' for bar: %s", 29 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) {
47 "Unexpected value %s in config mode", argv[1]); 47 "Unexpected value %s in config mode", argv[1]);
48 } 48 }
49 49
50 if (config->current_bar && argc == 2 &&
51 strcmp(config->current_bar->id, argv[1]) != 0) {
52 return cmd_results_new(CMD_INVALID, "Conflicting bar ids: %s and %s",
53 config->current_bar->id, argv[1]);
54 }
55
50 const char *state = argv[0]; 56 const char *state = argv[0];
51 if (config->reading) { 57 if (config->reading) {
52 error = bar_set_hidden_state(config->current_bar, state); 58 error = bar_set_hidden_state(config->current_bar, state);
diff --git a/sway/commands/bar/mode.c b/sway/commands/bar/mode.c
index 1081ad4b..8b3fb275 100644
--- a/sway/commands/bar/mode.c
+++ b/sway/commands/bar/mode.c
@@ -27,7 +27,7 @@ static struct cmd_results *bar_set_mode(struct bar_config *bar, const char *mode
27 } 27 }
28 28
29 if (strcmp(old_mode, bar->mode) != 0) { 29 if (strcmp(old_mode, bar->mode) != 0) {
30 if (!config->reading) { 30 if (!config->current_bar) {
31 ipc_event_barconfig_update(bar); 31 ipc_event_barconfig_update(bar);
32 } 32 }
33 sway_log(SWAY_DEBUG, "Setting mode: '%s' for bar: %s", bar->mode, bar->id); 33 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) {
51 "Unexpected value %s in config mode", argv[1]); 51 "Unexpected value %s in config mode", argv[1]);
52 } 52 }
53 53
54 if (config->current_bar && argc == 2 &&
55 strcmp(config->current_bar->id, argv[1]) != 0) {
56 return cmd_results_new(CMD_INVALID, "Conflicting bar ids: %s and %s",
57 config->current_bar->id, argv[1]);
58 }
59
54 const char *mode = argv[0]; 60 const char *mode = argv[0];
55 if (config->reading) { 61 if (config->reading) {
56 error = bar_set_mode(config->current_bar, mode); 62 error = bar_set_mode(config->current_bar, mode);
diff --git a/sway/commands/bar/output.c b/sway/commands/bar/output.c
index 6a78b30d..cac1d056 100644
--- a/sway/commands/bar/output.c
+++ b/sway/commands/bar/output.c
@@ -21,16 +21,19 @@ struct cmd_results *bar_cmd_output(int argc, char **argv) {
21 bool add_output = true; 21 bool add_output = true;
22 if (strcmp("*", output) == 0) { 22 if (strcmp("*", output) == 0) {
23 // remove all previous defined outputs and replace with '*' 23 // remove all previous defined outputs and replace with '*'
24 for (int i = 0; i < outputs->length; ++i) { 24 while (outputs->length) {
25 free(outputs->items[i]); 25 free(outputs->items[0]);
26 list_del(outputs, i); 26 list_del(outputs, 0);
27 } 27 }
28 } else { 28 } else {
29 // only add output if not already defined with either the same 29 // only add output if not already defined, if the list has '*', remove
30 // name or as '*' 30 // it, in favor of a manual list
31 for (int i = 0; i < outputs->length; ++i) { 31 for (int i = 0; i < outputs->length; ++i) {
32 const char *find = outputs->items[i]; 32 const char *find = outputs->items[i];
33 if (strcmp("*", find) == 0 || strcmp(output, find) == 0) { 33 if (strcmp("*", find) == 0) {
34 free(outputs->items[i]);
35 list_del(outputs, i);
36 } else if (strcmp(output, find) == 0) {
34 add_output = false; 37 add_output = false;
35 break; 38 break;
36 } 39 }
diff --git a/sway/commands/bar/status_command.c b/sway/commands/bar/status_command.c
index 77a73ab6..bb92e8e0 100644
--- a/sway/commands/bar/status_command.c
+++ b/sway/commands/bar/status_command.c
@@ -19,10 +19,5 @@ struct cmd_results *bar_cmd_status_command(int argc, char **argv) {
19 } else { 19 } else {
20 free(new_command); 20 free(new_command);
21 } 21 }
22
23 if (config->active && !config->validating) {
24 load_swaybar(config->current_bar);
25 }
26
27 return cmd_results_new(CMD_SUCCESS, NULL); 22 return cmd_results_new(CMD_SUCCESS, NULL);
28} 23}
diff --git a/sway/commands/bar/tray_output.c b/sway/commands/bar/tray_output.c
index 8bfdf193..eb3b486e 100644
--- a/sway/commands/bar/tray_output.c
+++ b/sway/commands/bar/tray_output.c
@@ -24,9 +24,21 @@ struct cmd_results *bar_cmd_tray_output(int argc, char **argv) {
24 free(outputs->items[i]); 24 free(outputs->items[i]);
25 } 25 }
26 outputs->length = 0; 26 outputs->length = 0;
27 } else if (strcmp(argv[0], "*") == 0) {
28 sway_log(SWAY_DEBUG, "Showing tray on all outputs for bar: %s",
29 config->current_bar->id);
30 while (outputs->length) {
31 free(outputs->items[0]);
32 list_del(outputs, 0);
33 }
34 return cmd_results_new(CMD_SUCCESS, NULL);
27 } else { 35 } else {
28 sway_log(SWAY_DEBUG, "Showing tray on output '%s' for bar: %s", argv[0], 36 sway_log(SWAY_DEBUG, "Showing tray on output '%s' for bar: %s", argv[0],
29 config->current_bar->id); 37 config->current_bar->id);
38 if (outputs->length == 1 && strcmp(outputs->items[0], "none") == 0) {
39 free(outputs->items[0]);
40 list_del(outputs, 0);
41 }
30 } 42 }
31 list_add(outputs, strdup(argv[0])); 43 list_add(outputs, strdup(argv[0]));
32 44