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