diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-30 00:11:00 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-30 00:11:00 -0400 |
commit | 9d7f47746cdcb0eed3cf41875d06a8ef238eef1c (patch) | |
tree | 997658454de40db3f8b76b68d658efaf2b686188 /swaybar/ipc.c | |
parent | Merge pull request #1654 from acrisci/refactor-2-electric-boogaloo (diff) | |
parent | Merge remote-tracking branch 'origin/wlroots' into swaybar-layers (diff) | |
download | sway-9d7f47746cdcb0eed3cf41875d06a8ef238eef1c.tar.gz sway-9d7f47746cdcb0eed3cf41875d06a8ef238eef1c.tar.zst sway-9d7f47746cdcb0eed3cf41875d06a8ef238eef1c.zip |
Merge pull request #1648 from swaywm/swaybar-layers
Port swaybar to layer shell
Diffstat (limited to 'swaybar/ipc.c')
-rw-r--r-- | swaybar/ipc.c | 445 |
1 files changed, 198 insertions, 247 deletions
diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 2d2b3b69..a82904bd 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c | |||
@@ -1,37 +1,132 @@ | |||
1 | #define _XOPEN_SOURCE 500 | 1 | #define _XOPEN_SOURCE 500 |
2 | #include <limits.h> | ||
2 | #include <string.h> | 3 | #include <string.h> |
3 | #include <strings.h> | 4 | #include <strings.h> |
4 | #include <json-c/json.h> | 5 | #include <json-c/json.h> |
6 | #include <wlr/util/log.h> | ||
5 | #include "swaybar/config.h" | 7 | #include "swaybar/config.h" |
6 | #include "swaybar/ipc.h" | 8 | #include "swaybar/ipc.h" |
7 | #include "ipc-client.h" | 9 | #include "ipc-client.h" |
8 | #include "list.h" | ||
9 | #include "log.h" | ||
10 | 10 | ||
11 | void ipc_send_workspace_command(const char *workspace_name) { | 11 | char *parse_font(const char *font) { |
12 | uint32_t size = strlen("workspace \"\"") + strlen(workspace_name) + 1; | 12 | char *new_font = NULL; |
13 | 13 | if (strncmp("pango:", font, 6) == 0) { | |
14 | char command[size]; | 14 | font += 6; |
15 | sprintf(command, "workspace \"%s\"", workspace_name); | 15 | } |
16 | new_font = strdup(font); | ||
17 | return new_font; | ||
18 | } | ||
16 | 19 | ||
17 | ipc_single_command(swaybar.ipc_socketfd, IPC_COMMAND, command, &size); | 20 | static void ipc_parse_colors( |
21 | struct swaybar_config *config, json_object *colors) { | ||
22 | json_object *background, *statusline, *separator; | ||
23 | json_object *focused_background, *focused_statusline, *focused_separator; | ||
24 | json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; | ||
25 | json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text; | ||
26 | json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text; | ||
27 | json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text; | ||
28 | json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text; | ||
29 | json_object_object_get_ex(colors, "background", &background); | ||
30 | json_object_object_get_ex(colors, "statusline", &statusline); | ||
31 | json_object_object_get_ex(colors, "separator", &separator); | ||
32 | json_object_object_get_ex(colors, "focused_background", &focused_background); | ||
33 | json_object_object_get_ex(colors, "focused_statusline", &focused_statusline); | ||
34 | json_object_object_get_ex(colors, "focused_separator", &focused_separator); | ||
35 | json_object_object_get_ex(colors, "focused_workspace_border", &focused_workspace_border); | ||
36 | json_object_object_get_ex(colors, "focused_workspace_bg", &focused_workspace_bg); | ||
37 | json_object_object_get_ex(colors, "focused_workspace_text", &focused_workspace_text); | ||
38 | json_object_object_get_ex(colors, "active_workspace_border", &active_workspace_border); | ||
39 | json_object_object_get_ex(colors, "active_workspace_bg", &active_workspace_bg); | ||
40 | json_object_object_get_ex(colors, "active_workspace_text", &active_workspace_text); | ||
41 | json_object_object_get_ex(colors, "inactive_workspace_border", &inactive_workspace_border); | ||
42 | json_object_object_get_ex(colors, "inactive_workspace_bg", &inactive_workspace_bg); | ||
43 | json_object_object_get_ex(colors, "inactive_workspace_text", &inactive_workspace_text); | ||
44 | json_object_object_get_ex(colors, "urgent_workspace_border", &urgent_workspace_border); | ||
45 | json_object_object_get_ex(colors, "urgent_workspace_bg", &urgent_workspace_bg); | ||
46 | json_object_object_get_ex(colors, "urgent_workspace_text", &urgent_workspace_text); | ||
47 | json_object_object_get_ex(colors, "binding_mode_border", &binding_mode_border); | ||
48 | json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg); | ||
49 | json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); | ||
50 | if (background) { | ||
51 | config->colors.background = parse_color( | ||
52 | json_object_get_string(background)); | ||
53 | } | ||
54 | if (statusline) { | ||
55 | config->colors.statusline = parse_color( | ||
56 | json_object_get_string(statusline)); | ||
57 | } | ||
58 | if (separator) { | ||
59 | config->colors.separator = parse_color( | ||
60 | json_object_get_string(separator)); | ||
61 | } | ||
62 | if (focused_background) { | ||
63 | config->colors.focused_background = parse_color( | ||
64 | json_object_get_string(focused_background)); | ||
65 | } | ||
66 | if (focused_statusline) { | ||
67 | config->colors.focused_statusline = parse_color( | ||
68 | json_object_get_string(focused_statusline)); | ||
69 | } | ||
70 | if (focused_separator) { | ||
71 | config->colors.focused_separator = parse_color( | ||
72 | json_object_get_string(focused_separator)); | ||
73 | } | ||
74 | if (focused_workspace_border) { | ||
75 | config->colors.focused_workspace.border = parse_color( | ||
76 | json_object_get_string(focused_workspace_border)); | ||
77 | } | ||
78 | if (focused_workspace_bg) { | ||
79 | config->colors.focused_workspace.background = parse_color( | ||
80 | json_object_get_string(focused_workspace_bg)); | ||
81 | } | ||
82 | if (focused_workspace_text) { | ||
83 | config->colors.focused_workspace.text = parse_color( | ||
84 | json_object_get_string(focused_workspace_text)); | ||
85 | } | ||
86 | if (active_workspace_border) { | ||
87 | config->colors.active_workspace.border = parse_color( | ||
88 | json_object_get_string(active_workspace_border)); | ||
89 | } | ||
90 | if (active_workspace_bg) { | ||
91 | config->colors.active_workspace.background = parse_color( | ||
92 | json_object_get_string(active_workspace_bg)); | ||
93 | } | ||
94 | if (active_workspace_text) { | ||
95 | config->colors.active_workspace.text = parse_color( | ||
96 | json_object_get_string(active_workspace_text)); | ||
97 | } | ||
98 | if (inactive_workspace_border) { | ||
99 | config->colors.inactive_workspace.border = parse_color( | ||
100 | json_object_get_string(inactive_workspace_border)); | ||
101 | } | ||
102 | if (inactive_workspace_bg) { | ||
103 | config->colors.inactive_workspace.background = parse_color( | ||
104 | json_object_get_string(inactive_workspace_bg)); | ||
105 | } | ||
106 | if (inactive_workspace_text) { | ||
107 | config->colors.inactive_workspace.text = parse_color( | ||
108 | json_object_get_string(inactive_workspace_text)); | ||
109 | } | ||
110 | if (binding_mode_border) { | ||
111 | config->colors.binding_mode.border = parse_color( | ||
112 | json_object_get_string(binding_mode_border)); | ||
113 | } | ||
114 | if (binding_mode_bg) { | ||
115 | config->colors.binding_mode.background = parse_color( | ||
116 | json_object_get_string(binding_mode_bg)); | ||
117 | } | ||
118 | if (binding_mode_text) { | ||
119 | config->colors.binding_mode.text = parse_color( | ||
120 | json_object_get_string(binding_mode_text)); | ||
121 | } | ||
18 | } | 122 | } |
19 | 123 | ||
20 | static void ipc_parse_config(struct config *config, const char *payload) { | 124 | static void ipc_parse_config( |
125 | struct swaybar_config *config, const char *payload) { | ||
21 | json_object *bar_config = json_tokener_parse(payload); | 126 | json_object *bar_config = json_tokener_parse(payload); |
22 | json_object *markup, *mode, *hidden_bar, *position, *status_command; | 127 | json_object *markup, *mode, *hidden_bar, *position, *status_command; |
23 | json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; | 128 | json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; |
24 | json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; | 129 | json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; |
25 | #ifdef ENABLE_TRAY | ||
26 | json_object *tray_output, *icon_theme, *tray_padding, *activate_button, *context_button; | ||
27 | json_object *secondary_button; | ||
28 | json_object_object_get_ex(bar_config, "tray_output", &tray_output); | ||
29 | json_object_object_get_ex(bar_config, "icon_theme", &icon_theme); | ||
30 | json_object_object_get_ex(bar_config, "tray_padding", &tray_padding); | ||
31 | json_object_object_get_ex(bar_config, "activate_button", &activate_button); | ||
32 | json_object_object_get_ex(bar_config, "context_button", &context_button); | ||
33 | json_object_object_get_ex(bar_config, "secondary_button", &secondary_button); | ||
34 | #endif | ||
35 | json_object_object_get_ex(bar_config, "mode", &mode); | 130 | json_object_object_get_ex(bar_config, "mode", &mode); |
36 | json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); | 131 | json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar); |
37 | json_object_object_get_ex(bar_config, "position", &position); | 132 | json_object_object_get_ex(bar_config, "position", &position); |
@@ -47,230 +142,98 @@ static void ipc_parse_config(struct config *config, const char *payload) { | |||
47 | json_object_object_get_ex(bar_config, "colors", &colors); | 142 | json_object_object_get_ex(bar_config, "colors", &colors); |
48 | json_object_object_get_ex(bar_config, "outputs", &outputs); | 143 | json_object_object_get_ex(bar_config, "outputs", &outputs); |
49 | json_object_object_get_ex(bar_config, "pango_markup", &markup); | 144 | json_object_object_get_ex(bar_config, "pango_markup", &markup); |
50 | |||
51 | if (status_command) { | 145 | if (status_command) { |
52 | free(config->status_command); | 146 | free(config->status_command); |
53 | config->status_command = strdup(json_object_get_string(status_command)); | 147 | config->status_command = strdup(json_object_get_string(status_command)); |
54 | } | 148 | } |
55 | |||
56 | if (position) { | 149 | if (position) { |
57 | config->position = parse_position(json_object_get_string(position)); | 150 | config->position = parse_position(json_object_get_string(position)); |
58 | } | 151 | } |
59 | |||
60 | if (font) { | 152 | if (font) { |
61 | free(config->font); | 153 | free(config->font); |
62 | config->font = parse_font(json_object_get_string(font)); | 154 | config->font = parse_font(json_object_get_string(font)); |
63 | } | 155 | } |
64 | |||
65 | if (sep_symbol) { | 156 | if (sep_symbol) { |
66 | free(config->sep_symbol); | 157 | free(config->sep_symbol); |
67 | config->sep_symbol = strdup(json_object_get_string(sep_symbol)); | 158 | config->sep_symbol = strdup(json_object_get_string(sep_symbol)); |
68 | } | 159 | } |
69 | |||
70 | if (strip_workspace_numbers) { | 160 | if (strip_workspace_numbers) { |
71 | config->strip_workspace_numbers = json_object_get_boolean(strip_workspace_numbers); | 161 | config->strip_workspace_numbers = json_object_get_boolean(strip_workspace_numbers); |
72 | } | 162 | } |
73 | |||
74 | if (binding_mode_indicator) { | 163 | if (binding_mode_indicator) { |
75 | config->binding_mode_indicator = json_object_get_boolean(binding_mode_indicator); | 164 | config->binding_mode_indicator = json_object_get_boolean(binding_mode_indicator); |
76 | } | 165 | } |
77 | |||
78 | if (wrap_scroll) { | 166 | if (wrap_scroll) { |
79 | config->wrap_scroll = json_object_get_boolean(wrap_scroll); | 167 | config->wrap_scroll = json_object_get_boolean(wrap_scroll); |
80 | } | 168 | } |
81 | |||
82 | if (workspace_buttons) { | 169 | if (workspace_buttons) { |
83 | config->workspace_buttons = json_object_get_boolean(workspace_buttons); | 170 | config->workspace_buttons = json_object_get_boolean(workspace_buttons); |
84 | } | 171 | } |
85 | |||
86 | if (bar_height) { | 172 | if (bar_height) { |
87 | config->height = json_object_get_int(bar_height); | 173 | config->height = json_object_get_int(bar_height); |
88 | } | 174 | } |
89 | |||
90 | if (markup) { | 175 | if (markup) { |
91 | config->pango_markup = json_object_get_boolean(markup); | 176 | config->pango_markup = json_object_get_boolean(markup); |
92 | } | 177 | } |
93 | 178 | ||
94 | #ifdef ENABLE_TRAY | 179 | struct config_output *output, *tmp; |
95 | if (tray_output) { | 180 | wl_list_for_each_safe(output, tmp, &config->outputs, link) { |
96 | free(config->tray_output); | 181 | wl_list_remove(&output->link); |
97 | config->tray_output = strdup(json_object_get_string(tray_output)); | 182 | free(output->name); |
98 | } | 183 | free(output); |
99 | |||
100 | if (icon_theme) { | ||
101 | free(config->icon_theme); | ||
102 | config->icon_theme = strdup(json_object_get_string(icon_theme)); | ||
103 | } | ||
104 | |||
105 | if (tray_padding) { | ||
106 | config->tray_padding = json_object_get_int(tray_padding); | ||
107 | } | ||
108 | |||
109 | if (activate_button) { | ||
110 | config->activate_button = json_object_get_int(activate_button); | ||
111 | } | ||
112 | |||
113 | if (context_button) { | ||
114 | config->context_button = json_object_get_int(context_button); | ||
115 | } | ||
116 | |||
117 | if (secondary_button) { | ||
118 | config->secondary_button = json_object_get_int(secondary_button); | ||
119 | } | ||
120 | #endif | ||
121 | |||
122 | // free previous outputs list | ||
123 | int i; | ||
124 | for (i = 0; i < config->outputs->length; ++i) { | ||
125 | free(config->outputs->items[i]); | ||
126 | } | 184 | } |
127 | list_free(config->outputs); | ||
128 | config->outputs = create_list(); | ||
129 | |||
130 | if (outputs) { | 185 | if (outputs) { |
131 | int length = json_object_array_length(outputs); | 186 | int length = json_object_array_length(outputs); |
132 | json_object *output; | 187 | for (int i = 0; i < length; ++i) { |
133 | const char *output_str; | 188 | json_object *output = json_object_array_get_idx(outputs, i); |
134 | for (i = 0; i < length; ++i) { | 189 | const char *name = json_object_get_string(output); |
135 | output = json_object_array_get_idx(outputs, i); | 190 | if (strcmp("*", name) == 0) { |
136 | output_str = json_object_get_string(output); | 191 | // TODO: do we need to clear out the list here or something |
137 | if (strcmp("*", output_str) == 0) { | ||
138 | config->all_outputs = true; | ||
139 | break; | 192 | break; |
140 | } | 193 | } |
141 | list_add(config->outputs, strdup(output_str)); | 194 | struct config_output *coutput = calloc( |
195 | 1, sizeof(struct config_output)); | ||
196 | coutput->name = strdup(name); | ||
197 | coutput->index = SIZE_MAX; | ||
198 | wl_list_insert(&config->outputs, &coutput->link); | ||
142 | } | 199 | } |
143 | } else { | ||
144 | config->all_outputs = true; | ||
145 | } | 200 | } |
146 | 201 | ||
147 | if (colors) { | 202 | if (colors) { |
148 | json_object *background, *statusline, *separator; | 203 | ipc_parse_colors(config, colors); |
149 | json_object *focused_background, *focused_statusline, *focused_separator; | ||
150 | json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; | ||
151 | json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text; | ||
152 | json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text; | ||
153 | json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text; | ||
154 | json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text; | ||
155 | json_object_object_get_ex(colors, "background", &background); | ||
156 | json_object_object_get_ex(colors, "statusline", &statusline); | ||
157 | json_object_object_get_ex(colors, "separator", &separator); | ||
158 | json_object_object_get_ex(colors, "focused_background", &focused_background); | ||
159 | json_object_object_get_ex(colors, "focused_statusline", &focused_statusline); | ||
160 | json_object_object_get_ex(colors, "focused_separator", &focused_separator); | ||
161 | json_object_object_get_ex(colors, "focused_workspace_border", &focused_workspace_border); | ||
162 | json_object_object_get_ex(colors, "focused_workspace_bg", &focused_workspace_bg); | ||
163 | json_object_object_get_ex(colors, "focused_workspace_text", &focused_workspace_text); | ||
164 | json_object_object_get_ex(colors, "active_workspace_border", &active_workspace_border); | ||
165 | json_object_object_get_ex(colors, "active_workspace_bg", &active_workspace_bg); | ||
166 | json_object_object_get_ex(colors, "active_workspace_text", &active_workspace_text); | ||
167 | json_object_object_get_ex(colors, "inactive_workspace_border", &inactive_workspace_border); | ||
168 | json_object_object_get_ex(colors, "inactive_workspace_bg", &inactive_workspace_bg); | ||
169 | json_object_object_get_ex(colors, "inactive_workspace_text", &inactive_workspace_text); | ||
170 | json_object_object_get_ex(colors, "urgent_workspace_border", &urgent_workspace_border); | ||
171 | json_object_object_get_ex(colors, "urgent_workspace_bg", &urgent_workspace_bg); | ||
172 | json_object_object_get_ex(colors, "urgent_workspace_text", &urgent_workspace_text); | ||
173 | json_object_object_get_ex(colors, "binding_mode_border", &binding_mode_border); | ||
174 | json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg); | ||
175 | json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); | ||
176 | if (background) { | ||
177 | config->colors.background = parse_color(json_object_get_string(background)); | ||
178 | } | ||
179 | |||
180 | if (statusline) { | ||
181 | config->colors.statusline = parse_color(json_object_get_string(statusline)); | ||
182 | } | ||
183 | |||
184 | if (separator) { | ||
185 | config->colors.separator = parse_color(json_object_get_string(separator)); | ||
186 | } | ||
187 | |||
188 | if (focused_background) { | ||
189 | config->colors.focused_background = parse_color(json_object_get_string(focused_background)); | ||
190 | } | ||
191 | |||
192 | if (focused_statusline) { | ||
193 | config->colors.focused_statusline = parse_color(json_object_get_string(focused_statusline)); | ||
194 | } | ||
195 | |||
196 | if (focused_separator) { | ||
197 | config->colors.focused_separator = parse_color(json_object_get_string(focused_separator)); | ||
198 | } | ||
199 | |||
200 | if (focused_workspace_border) { | ||
201 | config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); | ||
202 | } | ||
203 | |||
204 | if (focused_workspace_bg) { | ||
205 | config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); | ||
206 | } | ||
207 | |||
208 | if (focused_workspace_text) { | ||
209 | config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); | ||
210 | } | ||
211 | |||
212 | if (active_workspace_border) { | ||
213 | config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); | ||
214 | } | ||
215 | |||
216 | if (active_workspace_bg) { | ||
217 | config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); | ||
218 | } | ||
219 | |||
220 | if (active_workspace_text) { | ||
221 | config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); | ||
222 | } | ||
223 | |||
224 | if (inactive_workspace_border) { | ||
225 | config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); | ||
226 | } | ||
227 | |||
228 | if (inactive_workspace_bg) { | ||
229 | config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); | ||
230 | } | ||
231 | |||
232 | if (inactive_workspace_text) { | ||
233 | config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); | ||
234 | } | ||
235 | |||
236 | if (binding_mode_border) { | ||
237 | config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); | ||
238 | } | ||
239 | |||
240 | if (binding_mode_bg) { | ||
241 | config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); | ||
242 | } | ||
243 | |||
244 | if (binding_mode_text) { | ||
245 | config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); | ||
246 | } | ||
247 | } | 204 | } |
248 | 205 | ||
249 | json_object_put(bar_config); | 206 | json_object_put(bar_config); |
250 | } | 207 | } |
251 | 208 | ||
252 | static void ipc_update_workspaces(struct bar *bar) { | 209 | static void free_workspaces(struct wl_list *list) { |
253 | int i; | 210 | struct swaybar_workspace *ws, *tmp; |
254 | for (i = 0; i < bar->outputs->length; ++i) { | 211 | wl_list_for_each_safe(ws, tmp, list, link) { |
255 | struct output *output = bar->outputs->items[i]; | 212 | wl_list_remove(&ws->link); |
256 | if (output->workspaces) { | 213 | free(ws->name); |
257 | free_workspaces(output->workspaces); | 214 | free(ws); |
258 | } | ||
259 | output->workspaces = create_list(); | ||
260 | } | 215 | } |
216 | } | ||
261 | 217 | ||
218 | void ipc_get_workspaces(struct swaybar *bar) { | ||
219 | bar->focused_output = NULL; | ||
220 | struct swaybar_output *output; | ||
221 | wl_list_for_each(output, &bar->outputs, link) { | ||
222 | free_workspaces(&output->workspaces); | ||
223 | output->focused = false; | ||
224 | } | ||
262 | uint32_t len = 0; | 225 | uint32_t len = 0; |
263 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); | 226 | char *res = ipc_single_command(bar->ipc_socketfd, |
227 | IPC_GET_WORKSPACES, NULL, &len); | ||
264 | json_object *results = json_tokener_parse(res); | 228 | json_object *results = json_tokener_parse(res); |
265 | if (!results) { | 229 | if (!results) { |
266 | free(res); | 230 | free(res); |
267 | return; | 231 | return; |
268 | } | 232 | } |
269 | 233 | size_t length = json_object_array_length(results); | |
270 | int length = json_object_array_length(results); | ||
271 | json_object *ws_json; | 234 | json_object *ws_json; |
272 | json_object *num, *name, *visible, *focused, *out, *urgent; | 235 | json_object *num, *name, *visible, *focused, *out, *urgent; |
273 | for (i = 0; i < length; ++i) { | 236 | for (size_t i = 0; i < length; ++i) { |
274 | ws_json = json_object_array_get_idx(results, i); | 237 | ws_json = json_object_array_get_idx(results, i); |
275 | 238 | ||
276 | json_object_object_get_ex(ws_json, "num", &num); | 239 | json_object_object_get_ex(ws_json, "num", &num); |
@@ -280,113 +243,95 @@ static void ipc_update_workspaces(struct bar *bar) { | |||
280 | json_object_object_get_ex(ws_json, "output", &out); | 243 | json_object_object_get_ex(ws_json, "output", &out); |
281 | json_object_object_get_ex(ws_json, "urgent", &urgent); | 244 | json_object_object_get_ex(ws_json, "urgent", &urgent); |
282 | 245 | ||
283 | int j; | 246 | wl_list_for_each(output, &bar->outputs, link) { |
284 | for (j = 0; j < bar->outputs->length; ++j) { | 247 | const char *ws_output = json_object_get_string(out); |
285 | struct output *output = bar->outputs->items[j]; | 248 | if (strcmp(ws_output, output->name) == 0) { |
286 | if (strcmp(json_object_get_string(out), output->name) == 0) { | 249 | struct swaybar_workspace *ws = |
287 | struct workspace *ws = malloc(sizeof(struct workspace)); | 250 | calloc(1, sizeof(struct swaybar_workspace)); |
288 | ws->num = json_object_get_int(num); | 251 | ws->num = json_object_get_int(num); |
289 | ws->name = strdup(json_object_get_string(name)); | 252 | ws->name = strdup(json_object_get_string(name)); |
290 | ws->visible = json_object_get_boolean(visible); | 253 | ws->visible = json_object_get_boolean(visible); |
291 | ws->focused = json_object_get_boolean(focused); | 254 | ws->focused = json_object_get_boolean(focused); |
292 | if (ws->focused) { | 255 | if (ws->focused) { |
293 | if (bar->focused_output) { | ||
294 | bar->focused_output->focused = false; | ||
295 | } | ||
296 | bar->focused_output = output; | ||
297 | output->focused = true; | 256 | output->focused = true; |
298 | } | 257 | } |
299 | ws->urgent = json_object_get_boolean(urgent); | 258 | ws->urgent = json_object_get_boolean(urgent); |
300 | list_add(output->workspaces, ws); | 259 | wl_list_insert(&output->workspaces, &ws->link); |
301 | } | 260 | } |
302 | } | 261 | } |
303 | } | 262 | } |
304 | |||
305 | json_object_put(results); | 263 | json_object_put(results); |
306 | free(res); | 264 | free(res); |
307 | } | 265 | } |
308 | 266 | ||
309 | void ipc_bar_init(struct bar *bar, const char *bar_id) { | 267 | static void ipc_get_outputs(struct swaybar *bar) { |
310 | // Get bar config | 268 | uint32_t len = 0; |
311 | uint32_t len = strlen(bar_id); | 269 | char *res = ipc_single_command(bar->ipc_socketfd, |
312 | char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); | 270 | IPC_GET_OUTPUTS, NULL, &len); |
313 | |||
314 | ipc_parse_config(bar->config, res); | ||
315 | free(res); | ||
316 | |||
317 | // Get outputs | ||
318 | len = 0; | ||
319 | res = ipc_single_command(bar->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); | ||
320 | json_object *outputs = json_tokener_parse(res); | 271 | json_object *outputs = json_tokener_parse(res); |
321 | int i; | 272 | for (size_t i = 0; i < json_object_array_length(outputs); ++i) { |
322 | int length = json_object_array_length(outputs); | 273 | json_object *output = json_object_array_get_idx(outputs, i); |
323 | json_object *output, *output_name, *output_active; | 274 | json_object *output_name, *output_active; |
324 | const char *name; | ||
325 | bool active; | ||
326 | for (i = 0; i < length; ++i) { | ||
327 | output = json_object_array_get_idx(outputs, i); | ||
328 | json_object_object_get_ex(output, "name", &output_name); | 275 | json_object_object_get_ex(output, "name", &output_name); |
329 | json_object_object_get_ex(output, "active", &output_active); | 276 | json_object_object_get_ex(output, "active", &output_active); |
330 | name = json_object_get_string(output_name); | 277 | const char *name = json_object_get_string(output_name); |
331 | active = json_object_get_boolean(output_active); | 278 | bool active = json_object_get_boolean(output_active); |
332 | if (!active) { | 279 | if (!active) { |
333 | continue; | 280 | continue; |
334 | } | 281 | } |
335 | 282 | if (wl_list_empty(&bar->config->outputs)) { | |
336 | bool use_output = false; | 283 | struct config_output *coutput = calloc( |
337 | if (bar->config->all_outputs) { | 284 | 1, sizeof(struct config_output)); |
338 | use_output = true; | 285 | coutput->name = strdup(name); |
286 | coutput->index = i; | ||
287 | wl_list_insert(&bar->config->outputs, &coutput->link); | ||
339 | } else { | 288 | } else { |
340 | int j = 0; | 289 | struct config_output *coutput; |
341 | for (j = 0; j < bar->config->outputs->length; ++j) { | 290 | wl_list_for_each(coutput, &bar->config->outputs, link) { |
342 | const char *conf_name = bar->config->outputs->items[j]; | 291 | if (strcmp(name, coutput->name) == 0) { |
343 | if (strcasecmp(name, conf_name) == 0) { | 292 | coutput->index = i; |
344 | use_output = true; | ||
345 | break; | 293 | break; |
346 | } | 294 | } |
347 | } | 295 | } |
348 | } | 296 | } |
349 | |||
350 | if (!use_output) { | ||
351 | continue; | ||
352 | } | ||
353 | |||
354 | // add bar to the output | ||
355 | struct output *bar_output = container_output_create(name); | ||
356 | bar_output->idx = i; | ||
357 | list_add(bar->outputs, bar_output); | ||
358 | } | 297 | } |
359 | free(res); | ||
360 | json_object_put(outputs); | 298 | json_object_put(outputs); |
299 | free(res); | ||
300 | } | ||
361 | 301 | ||
362 | const char *subscribe_json = "[ \"workspace\", \"mode\" ]"; | 302 | void ipc_initialize(struct swaybar *bar, const char *bar_id) { |
363 | len = strlen(subscribe_json); | 303 | uint32_t len = strlen(bar_id); |
364 | res = ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); | 304 | char *res = ipc_single_command(bar->ipc_socketfd, |
305 | IPC_GET_BAR_CONFIG, bar_id, &len); | ||
306 | ipc_parse_config(bar->config, res); | ||
365 | free(res); | 307 | free(res); |
308 | ipc_get_outputs(bar); | ||
366 | 309 | ||
367 | ipc_update_workspaces(bar); | 310 | const char *subscribe = "[ \"workspace\", \"mode\" ]"; |
311 | len = strlen(subscribe); | ||
312 | free(ipc_single_command(bar->ipc_event_socketfd, | ||
313 | IPC_SUBSCRIBE, subscribe, &len)); | ||
368 | } | 314 | } |
369 | 315 | ||
370 | bool handle_ipc_event(struct bar *bar) { | 316 | bool handle_ipc_event(struct swaybar *bar) { |
371 | struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd); | 317 | struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd); |
372 | if (!resp) { | 318 | if (!resp) { |
373 | return false; | 319 | return false; |
374 | } | 320 | } |
375 | switch (resp->type) { | 321 | switch (resp->type) { |
376 | case IPC_EVENT_WORKSPACE: | 322 | case IPC_EVENT_WORKSPACE: |
377 | ipc_update_workspaces(bar); | 323 | ipc_get_workspaces(bar); |
378 | break; | 324 | break; |
379 | case IPC_EVENT_MODE: { | 325 | case IPC_EVENT_MODE: { |
380 | json_object *result = json_tokener_parse(resp->payload); | 326 | json_object *result = json_tokener_parse(resp->payload); |
381 | if (!result) { | 327 | if (!result) { |
382 | free_ipc_response(resp); | 328 | free_ipc_response(resp); |
383 | sway_log(L_ERROR, "failed to parse payload as json"); | 329 | wlr_log(L_ERROR, "failed to parse payload as json"); |
384 | return false; | 330 | return false; |
385 | } | 331 | } |
386 | json_object *json_change; | 332 | json_object *json_change, *json_pango_markup; |
387 | if (json_object_object_get_ex(result, "change", &json_change)) { | 333 | if (json_object_object_get_ex(result, "change", &json_change)) { |
388 | const char *change = json_object_get_string(json_change); | 334 | const char *change = json_object_get_string(json_change); |
389 | |||
390 | free(bar->config->mode); | 335 | free(bar->config->mode); |
391 | if (strcmp(change, "default") == 0) { | 336 | if (strcmp(change, "default") == 0) { |
392 | bar->config->mode = NULL; | 337 | bar->config->mode = NULL; |
@@ -394,9 +339,16 @@ bool handle_ipc_event(struct bar *bar) { | |||
394 | bar->config->mode = strdup(change); | 339 | bar->config->mode = strdup(change); |
395 | } | 340 | } |
396 | } else { | 341 | } else { |
397 | sway_log(L_ERROR, "failed to parse response"); | 342 | wlr_log(L_ERROR, "failed to parse response"); |
343 | json_object_put(result); | ||
344 | free_ipc_response(resp); | ||
345 | return false; | ||
346 | } | ||
347 | if (json_object_object_get_ex(result, | ||
348 | "pango_markup", &json_pango_markup)) { | ||
349 | bar->config->mode_pango_markup = json_object_get_boolean( | ||
350 | json_pango_markup); | ||
398 | } | 351 | } |
399 | |||
400 | json_object_put(result); | 352 | json_object_put(result); |
401 | break; | 353 | break; |
402 | } | 354 | } |
@@ -404,7 +356,6 @@ bool handle_ipc_event(struct bar *bar) { | |||
404 | free_ipc_response(resp); | 356 | free_ipc_response(resp); |
405 | return false; | 357 | return false; |
406 | } | 358 | } |
407 | |||
408 | free_ipc_response(resp); | 359 | free_ipc_response(resp); |
409 | return true; | 360 | return true; |
410 | } | 361 | } |