diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2019-09-02 21:41:11 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-09-04 16:48:50 -1000 |
commit | 1fd2c6ba498e61f4fe823bf552f9d2fce8612de4 (patch) | |
tree | 8e2d9adab3451f1f05c76340d466a442c840e558 /sway/commands/bar.c | |
parent | seatop_default: only focus container on press (diff) | |
download | sway-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/bar.c')
-rw-r--r-- | sway/commands/bar.c | 93 |
1 files changed, 42 insertions, 51 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 | } |