diff options
author | 2016-02-27 16:18:50 -0500 | |
---|---|---|
committer | 2016-02-27 16:18:50 -0500 | |
commit | 3453910c3e887be900c69394b738e34d9d8ba095 (patch) | |
tree | 28e0d8d72557ac57a2f9aed5c94818c8ba20e97f /swaybar/ipc.c | |
parent | Set pointer pos before focusing view under it (diff) | |
parent | Free config before exiting sway. (diff) | |
download | sway-3453910c3e887be900c69394b738e34d9d8ba095.tar.gz sway-3453910c3e887be900c69394b738e34d9d8ba095.tar.zst sway-3453910c3e887be900c69394b738e34d9d8ba095.zip |
Merge pull request #492 from mikkeloscar/swaybar-multi-output
Display single swaybar on multiple outputs
Diffstat (limited to 'swaybar/ipc.c')
-rw-r--r-- | swaybar/ipc.c | 119 |
1 files changed, 94 insertions, 25 deletions
diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 4104103d..312c79b9 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c | |||
@@ -11,7 +11,7 @@ static void ipc_parse_config(struct config *config, const char *payload) { | |||
11 | json_object *bar_config = json_tokener_parse(payload); | 11 | json_object *bar_config = json_tokener_parse(payload); |
12 | json_object *tray_output, *mode, *hidden_bar, *position, *status_command; | 12 | json_object *tray_output, *mode, *hidden_bar, *position, *status_command; |
13 | json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers; | 13 | json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers; |
14 | json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol; | 14 | json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; |
15 | json_object_object_get_ex(bar_config, "tray_output", &tray_output); | 15 | json_object_object_get_ex(bar_config, "tray_output", &tray_output); |
16 | json_object_object_get_ex(bar_config, "mode", &mode); | 16 | json_object_object_get_ex(bar_config, "mode", &mode); |
17 | json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); | 17 | json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); |
@@ -25,6 +25,7 @@ static void ipc_parse_config(struct config *config, const char *payload) { | |||
25 | json_object_object_get_ex(bar_config, "verbose", &verbose); | 25 | json_object_object_get_ex(bar_config, "verbose", &verbose); |
26 | json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol); | 26 | json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol); |
27 | json_object_object_get_ex(bar_config, "colors", &colors); | 27 | json_object_object_get_ex(bar_config, "colors", &colors); |
28 | json_object_object_get_ex(bar_config, "outputs", &outputs); | ||
28 | 29 | ||
29 | if (status_command) { | 30 | if (status_command) { |
30 | free(config->status_command); | 31 | free(config->status_command); |
@@ -61,6 +62,31 @@ static void ipc_parse_config(struct config *config, const char *payload) { | |||
61 | config->height = json_object_get_int(bar_height); | 62 | config->height = json_object_get_int(bar_height); |
62 | } | 63 | } |
63 | 64 | ||
65 | // free previous outputs list | ||
66 | int i; | ||
67 | for (i = 0; i < config->outputs->length; ++i) { | ||
68 | free(config->outputs->items[i]); | ||
69 | } | ||
70 | list_free(config->outputs); | ||
71 | config->outputs = create_list(); | ||
72 | |||
73 | if (outputs) { | ||
74 | int length = json_object_array_length(outputs); | ||
75 | json_object *output; | ||
76 | const char *output_str; | ||
77 | for (i = 0; i < length; ++i) { | ||
78 | output = json_object_array_get_idx(outputs, i); | ||
79 | output_str = json_object_get_string(output); | ||
80 | if (strcmp("*", output_str) == 0) { | ||
81 | config->all_outputs = true; | ||
82 | break; | ||
83 | } | ||
84 | list_add(config->outputs, strdup(output_str)); | ||
85 | } | ||
86 | } else { | ||
87 | config->all_outputs = true; | ||
88 | } | ||
89 | |||
64 | if (colors) { | 90 | if (colors) { |
65 | json_object *background, *statusline, *separator; | 91 | json_object *background, *statusline, *separator; |
66 | json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; | 92 | json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; |
@@ -151,10 +177,14 @@ static void ipc_parse_config(struct config *config, const char *payload) { | |||
151 | } | 177 | } |
152 | 178 | ||
153 | static void ipc_update_workspaces(struct bar *bar) { | 179 | static void ipc_update_workspaces(struct bar *bar) { |
154 | if (bar->output->workspaces) { | 180 | int i; |
155 | free_workspaces(bar->output->workspaces); | 181 | for (i = 0; i < bar->outputs->length; ++i) { |
182 | struct output *output = bar->outputs->items[i]; | ||
183 | if (output->workspaces) { | ||
184 | free_workspaces(output->workspaces); | ||
185 | } | ||
186 | output->workspaces = create_list(); | ||
156 | } | 187 | } |
157 | bar->output->workspaces = create_list(); | ||
158 | 188 | ||
159 | uint32_t len = 0; | 189 | uint32_t len = 0; |
160 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); | 190 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); |
@@ -164,7 +194,6 @@ static void ipc_update_workspaces(struct bar *bar) { | |||
164 | return; | 194 | return; |
165 | } | 195 | } |
166 | 196 | ||
167 | int i; | ||
168 | int length = json_object_array_length(results); | 197 | int length = json_object_array_length(results); |
169 | json_object *ws_json; | 198 | json_object *ws_json; |
170 | json_object *num, *name, *visible, *focused, *out, *urgent; | 199 | json_object *num, *name, *visible, *focused, *out, *urgent; |
@@ -178,14 +207,18 @@ static void ipc_update_workspaces(struct bar *bar) { | |||
178 | json_object_object_get_ex(ws_json, "output", &out); | 207 | json_object_object_get_ex(ws_json, "output", &out); |
179 | json_object_object_get_ex(ws_json, "urgent", &urgent); | 208 | json_object_object_get_ex(ws_json, "urgent", &urgent); |
180 | 209 | ||
181 | if (strcmp(json_object_get_string(out), bar->output->name) == 0) { | 210 | int j; |
182 | struct workspace *ws = malloc(sizeof(struct workspace)); | 211 | for (j = 0; j < bar->outputs->length; ++j) { |
183 | ws->num = json_object_get_int(num); | 212 | struct output *output = bar->outputs->items[j]; |
184 | ws->name = strdup(json_object_get_string(name)); | 213 | if (strcmp(json_object_get_string(out), output->name) == 0) { |
185 | ws->visible = json_object_get_boolean(visible); | 214 | struct workspace *ws = malloc(sizeof(struct workspace)); |
186 | ws->focused = json_object_get_boolean(focused); | 215 | ws->num = json_object_get_int(num); |
187 | ws->urgent = json_object_get_boolean(urgent); | 216 | ws->name = strdup(json_object_get_string(name)); |
188 | list_add(bar->output->workspaces, ws); | 217 | ws->visible = json_object_get_boolean(visible); |
218 | ws->focused = json_object_get_boolean(focused); | ||
219 | ws->urgent = json_object_get_boolean(urgent); | ||
220 | list_add(output->workspaces, ws); | ||
221 | } | ||
189 | } | 222 | } |
190 | } | 223 | } |
191 | 224 | ||
@@ -193,22 +226,58 @@ static void ipc_update_workspaces(struct bar *bar) { | |||
193 | free(res); | 226 | free(res); |
194 | } | 227 | } |
195 | 228 | ||
196 | void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id) { | 229 | void ipc_bar_init(struct bar *bar, const char *bar_id) { |
197 | uint32_t len = 0; | 230 | // Get bar config |
198 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); | 231 | uint32_t len = strlen(bar_id); |
199 | json_object *outputs = json_tokener_parse(res); | 232 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); |
200 | json_object *info = json_object_array_get_idx(outputs, outputi); | 233 | |
201 | json_object *name; | 234 | ipc_parse_config(bar->config, res); |
202 | json_object_object_get_ex(info, "name", &name); | ||
203 | bar->output->name = strdup(json_object_get_string(name)); | ||
204 | free(res); | 235 | free(res); |
205 | json_object_put(outputs); | ||
206 | 236 | ||
207 | len = strlen(bar_id); | 237 | // Get outputs |
208 | res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); | 238 | len = 0; |
239 | res = ipc_single_command(bar->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); | ||
240 | json_object *outputs = json_tokener_parse(res); | ||
241 | int i; | ||
242 | int length = json_object_array_length(outputs); | ||
243 | json_object *output, *output_name, *output_active; | ||
244 | const char *name; | ||
245 | bool active; | ||
246 | for (i = 0; i < length; ++i) { | ||
247 | output = json_object_array_get_idx(outputs, i); | ||
248 | json_object_object_get_ex(output, "name", &output_name); | ||
249 | json_object_object_get_ex(output, "active", &output_active); | ||
250 | name = json_object_get_string(output_name); | ||
251 | active = json_object_get_boolean(output_active); | ||
252 | if (!active) { | ||
253 | continue; | ||
254 | } | ||
209 | 255 | ||
210 | ipc_parse_config(bar->config, res); | 256 | bool use_output = false; |
257 | if (bar->config->all_outputs) { | ||
258 | use_output = true; | ||
259 | } else { | ||
260 | int j = 0; | ||
261 | for (j = 0; j < bar->config->outputs->length; ++j) { | ||
262 | const char *conf_name = bar->config->outputs->items[i]; | ||
263 | if (strcasecmp(name, conf_name) == 0) { | ||
264 | use_output = true; | ||
265 | break; | ||
266 | } | ||
267 | } | ||
268 | } | ||
269 | |||
270 | if (!use_output) { | ||
271 | continue; | ||
272 | } | ||
273 | |||
274 | // add bar to the output | ||
275 | struct output *bar_output = new_output(name); | ||
276 | bar_output->idx = i; | ||
277 | list_add(bar->outputs, bar_output); | ||
278 | } | ||
211 | free(res); | 279 | free(res); |
280 | json_object_put(outputs); | ||
212 | 281 | ||
213 | const char *subscribe_json = "[ \"workspace\", \"mode\" ]"; | 282 | const char *subscribe_json = "[ \"workspace\", \"mode\" ]"; |
214 | len = strlen(subscribe_json); | 283 | len = strlen(subscribe_json); |