diff options
author | 2018-04-12 20:19:54 -0400 | |
---|---|---|
committer | 2018-04-12 20:19:54 -0400 | |
commit | cd1b32453a9296c18b28bff71607aeb22987b5cd (patch) | |
tree | c653c6d525b471914c01a9d7ae543f521b6138ed /swaybar/tray/tray.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/tray/tray.c')
-rw-r--r-- | swaybar/tray/tray.c | 398 |
1 files changed, 0 insertions, 398 deletions
diff --git a/swaybar/tray/tray.c b/swaybar/tray/tray.c deleted file mode 100644 index 91c3af06..00000000 --- a/swaybar/tray/tray.c +++ /dev/null | |||
@@ -1,398 +0,0 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <unistd.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <sys/wait.h> | ||
6 | #include <dbus/dbus.h> | ||
7 | #include "swaybar/bar.h" | ||
8 | #include "swaybar/tray/tray.h" | ||
9 | #include "swaybar/tray/dbus.h" | ||
10 | #include "swaybar/tray/sni.h" | ||
11 | #include "swaybar/tray/sni_watcher.h" | ||
12 | #include "swaybar/bar.h" | ||
13 | #include "swaybar/config.h" | ||
14 | #include "list.h" | ||
15 | #include "log.h" | ||
16 | |||
17 | struct tray *tray; | ||
18 | |||
19 | static void register_host(char *name) { | ||
20 | DBusMessage *message; | ||
21 | |||
22 | message = dbus_message_new_method_call( | ||
23 | "org.freedesktop.StatusNotifierWatcher", | ||
24 | "/StatusNotifierWatcher", | ||
25 | "org.freedesktop.StatusNotifierWatcher", | ||
26 | "RegisterStatusNotifierHost"); | ||
27 | if (!message) { | ||
28 | sway_log(L_ERROR, "Cannot allocate dbus method call"); | ||
29 | return; | ||
30 | } | ||
31 | |||
32 | dbus_message_append_args(message, | ||
33 | DBUS_TYPE_STRING, &name, | ||
34 | DBUS_TYPE_INVALID); | ||
35 | |||
36 | dbus_connection_send(conn, message, NULL); | ||
37 | |||
38 | dbus_message_unref(message); | ||
39 | } | ||
40 | |||
41 | static void get_items_reply(DBusPendingCall *pending, void *_data) { | ||
42 | DBusMessage *reply = dbus_pending_call_steal_reply(pending); | ||
43 | |||
44 | if (!reply) { | ||
45 | sway_log(L_ERROR, "Got no items reply from sni watcher"); | ||
46 | goto bail; | ||
47 | } | ||
48 | |||
49 | int message_type = dbus_message_get_type(reply); | ||
50 | |||
51 | if (message_type == DBUS_MESSAGE_TYPE_ERROR) { | ||
52 | char *msg; | ||
53 | |||
54 | dbus_message_get_args(reply, NULL, | ||
55 | DBUS_TYPE_STRING, &msg, | ||
56 | DBUS_TYPE_INVALID); | ||
57 | |||
58 | sway_log(L_ERROR, "Message is error: %s", msg); | ||
59 | goto bail; | ||
60 | } | ||
61 | |||
62 | DBusMessageIter iter; | ||
63 | DBusMessageIter variant; | ||
64 | DBusMessageIter array; | ||
65 | |||
66 | dbus_message_iter_init(reply, &iter); | ||
67 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) { | ||
68 | sway_log(L_ERROR, "Replyed with wrong type, not v(as)"); | ||
69 | goto bail; | ||
70 | } | ||
71 | dbus_message_iter_recurse(&iter, &variant); | ||
72 | if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_ARRAY || | ||
73 | dbus_message_iter_get_element_type(&variant) != DBUS_TYPE_STRING) { | ||
74 | sway_log(L_ERROR, "Replyed with wrong type, not v(as)"); | ||
75 | goto bail; | ||
76 | } | ||
77 | |||
78 | // Clear list | ||
79 | list_foreach(tray->items, (void (*)(void *))sni_free); | ||
80 | list_free(tray->items); | ||
81 | tray->items = create_list(); | ||
82 | |||
83 | // O(n) function, could be faster dynamically reading values | ||
84 | int len = dbus_message_iter_get_element_count(&variant); | ||
85 | |||
86 | dbus_message_iter_recurse(&variant, &array); | ||
87 | for (int i = 0; i < len; i++) { | ||
88 | const char *name; | ||
89 | dbus_message_iter_get_basic(&array, &name); | ||
90 | |||
91 | struct StatusNotifierItem *item = sni_create(name); | ||
92 | |||
93 | if (item) { | ||
94 | sway_log(L_DEBUG, "Item registered with host: %s", name); | ||
95 | list_add(tray->items, item); | ||
96 | dirty = true; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | bail: | ||
101 | dbus_message_unref(reply); | ||
102 | dbus_pending_call_unref(pending); | ||
103 | return; | ||
104 | } | ||
105 | static void get_items() { | ||
106 | DBusPendingCall *pending; | ||
107 | DBusMessage *message = dbus_message_new_method_call( | ||
108 | "org.freedesktop.StatusNotifierWatcher", | ||
109 | "/StatusNotifierWatcher", | ||
110 | "org.freedesktop.DBus.Properties", | ||
111 | "Get"); | ||
112 | |||
113 | const char *iface = "org.freedesktop.StatusNotifierWatcher"; | ||
114 | const char *prop = "RegisteredStatusNotifierItems"; | ||
115 | dbus_message_append_args(message, | ||
116 | DBUS_TYPE_STRING, &iface, | ||
117 | DBUS_TYPE_STRING, &prop, | ||
118 | DBUS_TYPE_INVALID); | ||
119 | |||
120 | bool status = | ||
121 | dbus_connection_send_with_reply(conn, message, &pending, -1); | ||
122 | dbus_message_unref(message); | ||
123 | |||
124 | if (!(pending || status)) { | ||
125 | sway_log(L_ERROR, "Could not get items"); | ||
126 | return; | ||
127 | } | ||
128 | |||
129 | dbus_pending_call_set_notify(pending, get_items_reply, NULL, NULL); | ||
130 | } | ||
131 | |||
132 | static DBusHandlerResult signal_handler(DBusConnection *connection, | ||
133 | DBusMessage *message, void *_data) { | ||
134 | if (dbus_message_is_signal(message, "org.freedesktop.StatusNotifierWatcher", | ||
135 | "StatusNotifierItemRegistered")) { | ||
136 | const char *name; | ||
137 | if (!dbus_message_get_args(message, NULL, | ||
138 | DBUS_TYPE_STRING, &name, | ||
139 | DBUS_TYPE_INVALID)) { | ||
140 | sway_log(L_ERROR, "Error getting StatusNotifierItemRegistered args"); | ||
141 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | ||
142 | } | ||
143 | |||
144 | if (list_seq_find(tray->items, sni_str_cmp, name) == -1) { | ||
145 | struct StatusNotifierItem *item = sni_create(name); | ||
146 | |||
147 | if (item) { | ||
148 | list_add(tray->items, item); | ||
149 | dirty = true; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return DBUS_HANDLER_RESULT_HANDLED; | ||
154 | } else if (dbus_message_is_signal(message, "org.freedesktop.StatusNotifierWatcher", | ||
155 | "StatusNotifierItemUnregistered")) { | ||
156 | const char *name; | ||
157 | if (!dbus_message_get_args(message, NULL, | ||
158 | DBUS_TYPE_STRING, &name, | ||
159 | DBUS_TYPE_INVALID)) { | ||
160 | sway_log(L_ERROR, "Error getting StatusNotifierItemUnregistered args"); | ||
161 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | ||
162 | } | ||
163 | |||
164 | int index; | ||
165 | if ((index = list_seq_find(tray->items, sni_str_cmp, name)) != -1) { | ||
166 | sni_free(tray->items->items[index]); | ||
167 | list_del(tray->items, index); | ||
168 | dirty = true; | ||
169 | } else { | ||
170 | // If it's not in our list, then our list is incorrect. | ||
171 | // Fetch all items again | ||
172 | sway_log(L_INFO, "Host item list incorrect, refreshing"); | ||
173 | get_items(); | ||
174 | } | ||
175 | |||
176 | return DBUS_HANDLER_RESULT_HANDLED; | ||
177 | } else if (dbus_message_is_signal(message, "org.freedesktop.StatusNotifierItem", | ||
178 | "NewIcon") || dbus_message_is_signal(message, | ||
179 | "org.kde.StatusNotifierItem", "NewIcon")) { | ||
180 | const char *name; | ||
181 | int index; | ||
182 | struct StatusNotifierItem *item; | ||
183 | |||
184 | name = dbus_message_get_sender(message); | ||
185 | if ((index = list_seq_find(tray->items, sni_uniq_cmp, name)) != -1) { | ||
186 | item = tray->items->items[index]; | ||
187 | sway_log(L_INFO, "NewIcon signal from item %s", item->name); | ||
188 | get_icon(item); | ||
189 | } | ||
190 | |||
191 | return DBUS_HANDLER_RESULT_HANDLED; | ||
192 | } | ||
193 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | ||
194 | } | ||
195 | |||
196 | static int init_host() { | ||
197 | tray = (struct tray *)malloc(sizeof(tray)); | ||
198 | |||
199 | tray->items = create_list(); | ||
200 | |||
201 | DBusError error; | ||
202 | dbus_error_init(&error); | ||
203 | char *name = NULL; | ||
204 | if (!conn) { | ||
205 | sway_log(L_ERROR, "Connection is null, cannot init SNI host"); | ||
206 | goto err; | ||
207 | } | ||
208 | name = calloc(sizeof(char), 256); | ||
209 | |||
210 | if (!name) { | ||
211 | sway_log(L_ERROR, "Cannot allocate name"); | ||
212 | goto err; | ||
213 | } | ||
214 | |||
215 | pid_t pid = getpid(); | ||
216 | if (snprintf(name, 256, "org.freedesktop.StatusNotifierHost-%d", pid) | ||
217 | >= 256) { | ||
218 | sway_log(L_ERROR, "Cannot get host name because string is too short." | ||
219 | "This should not happen"); | ||
220 | goto err; | ||
221 | } | ||
222 | |||
223 | // We want to be the sole owner of this name | ||
224 | if (dbus_bus_request_name(conn, name, DBUS_NAME_FLAG_DO_NOT_QUEUE, | ||
225 | &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { | ||
226 | sway_log(L_ERROR, "Cannot get host name and start the tray"); | ||
227 | goto err; | ||
228 | } | ||
229 | if (dbus_error_is_set(&error)) { | ||
230 | sway_log(L_ERROR, "Dbus err getting host name: %s\n", error.message); | ||
231 | goto err; | ||
232 | } | ||
233 | sway_log(L_DEBUG, "Got host name"); | ||
234 | |||
235 | register_host(name); | ||
236 | |||
237 | get_items(); | ||
238 | |||
239 | // Perhaps use addmatch helper functions like wlc does? | ||
240 | dbus_bus_add_match(conn, | ||
241 | "type='signal',\ | ||
242 | sender='org.freedesktop.StatusNotifierWatcher',\ | ||
243 | member='StatusNotifierItemRegistered'", | ||
244 | &error); | ||
245 | if (dbus_error_is_set(&error)) { | ||
246 | sway_log(L_ERROR, "dbus_err: %s", error.message); | ||
247 | goto err; | ||
248 | } | ||
249 | dbus_bus_add_match(conn, | ||
250 | "type='signal',\ | ||
251 | sender='org.freedesktop.StatusNotifierWatcher',\ | ||
252 | member='StatusNotifierItemUnregistered'", | ||
253 | &error); | ||
254 | if (dbus_error_is_set(&error)) { | ||
255 | sway_log(L_ERROR, "dbus_err: %s", error.message); | ||
256 | return -1; | ||
257 | } | ||
258 | |||
259 | // SNI matches | ||
260 | dbus_bus_add_match(conn, | ||
261 | "type='signal',\ | ||
262 | interface='org.freedesktop.StatusNotifierItem',\ | ||
263 | member='NewIcon'", | ||
264 | &error); | ||
265 | if (dbus_error_is_set(&error)) { | ||
266 | sway_log(L_ERROR, "dbus_err %s", error.message); | ||
267 | goto err; | ||
268 | } | ||
269 | dbus_bus_add_match(conn, | ||
270 | "type='signal',\ | ||
271 | interface='org.kde.StatusNotifierItem',\ | ||
272 | member='NewIcon'", | ||
273 | &error); | ||
274 | if (dbus_error_is_set(&error)) { | ||
275 | sway_log(L_ERROR, "dbus_err %s", error.message); | ||
276 | goto err; | ||
277 | } | ||
278 | |||
279 | dbus_connection_add_filter(conn, signal_handler, NULL, NULL); | ||
280 | |||
281 | free(name); | ||
282 | return 0; | ||
283 | |||
284 | err: | ||
285 | // TODO better handle errors | ||
286 | free(name); | ||
287 | return -1; | ||
288 | } | ||
289 | |||
290 | void tray_mouse_event(struct output *output, int x, int y, | ||
291 | uint32_t button, uint32_t state) { | ||
292 | |||
293 | struct window *window = output->window; | ||
294 | uint32_t tray_padding = swaybar.config->tray_padding; | ||
295 | int tray_width = window->width * window->scale; | ||
296 | |||
297 | for (int i = 0; i < output->items->length; ++i) { | ||
298 | struct sni_icon_ref *item = | ||
299 | output->items->items[i]; | ||
300 | int icon_width = cairo_image_surface_get_width(item->icon); | ||
301 | |||
302 | tray_width -= tray_padding; | ||
303 | if (x <= tray_width && x >= tray_width - icon_width) { | ||
304 | if (button == swaybar.config->activate_button) { | ||
305 | sni_activate(item->ref, x, y); | ||
306 | } else if (button == swaybar.config->context_button) { | ||
307 | sni_context_menu(item->ref, x, y); | ||
308 | } else if (button == swaybar.config->secondary_button) { | ||
309 | sni_secondary(item->ref, x, y); | ||
310 | } | ||
311 | break; | ||
312 | } | ||
313 | tray_width -= icon_width; | ||
314 | } | ||
315 | } | ||
316 | |||
317 | uint32_t tray_render(struct output *output, struct config *config) { | ||
318 | struct window *window = output->window; | ||
319 | cairo_t *cairo = window->cairo; | ||
320 | |||
321 | // Tray icons | ||
322 | uint32_t tray_padding = config->tray_padding; | ||
323 | uint32_t tray_width = window->width * window->scale; | ||
324 | const int item_size = (window->height * window->scale) - (2 * tray_padding); | ||
325 | |||
326 | if (item_size < 0) { | ||
327 | // Can't render items if the padding is too large | ||
328 | return tray_width; | ||
329 | } | ||
330 | |||
331 | if (config->tray_output && strcmp(config->tray_output, output->name) != 0) { | ||
332 | return tray_width; | ||
333 | } | ||
334 | |||
335 | for (int i = 0; i < tray->items->length; ++i) { | ||
336 | struct StatusNotifierItem *item = | ||
337 | tray->items->items[i]; | ||
338 | if (!item->image) { | ||
339 | continue; | ||
340 | } | ||
341 | |||
342 | struct sni_icon_ref *render_item = NULL; | ||
343 | int j; | ||
344 | for (j = i; j < output->items->length; ++j) { | ||
345 | struct sni_icon_ref *ref = | ||
346 | output->items->items[j]; | ||
347 | if (ref->ref == item) { | ||
348 | render_item = ref; | ||
349 | break; | ||
350 | } else { | ||
351 | sni_icon_ref_free(ref); | ||
352 | list_del(output->items, j); | ||
353 | } | ||
354 | } | ||
355 | |||
356 | if (!render_item) { | ||
357 | render_item = sni_icon_ref_create(item, item_size); | ||
358 | list_add(output->items, render_item); | ||
359 | } else if (item->dirty) { | ||
360 | // item needs re-render | ||
361 | sni_icon_ref_free(render_item); | ||
362 | output->items->items[j] = render_item = | ||
363 | sni_icon_ref_create(item, item_size); | ||
364 | } | ||
365 | |||
366 | tray_width -= tray_padding; | ||
367 | tray_width -= item_size; | ||
368 | |||
369 | cairo_operator_t op = cairo_get_operator(cairo); | ||
370 | cairo_set_operator(cairo, CAIRO_OPERATOR_OVER); | ||
371 | cairo_set_source_surface(cairo, render_item->icon, tray_width, tray_padding); | ||
372 | cairo_rectangle(cairo, tray_width, tray_padding, item_size, item_size); | ||
373 | cairo_fill(cairo); | ||
374 | cairo_set_operator(cairo, op); | ||
375 | |||
376 | item->dirty = false; | ||
377 | } | ||
378 | |||
379 | |||
380 | if (tray_width != window->width * window->scale) { | ||
381 | tray_width -= tray_padding; | ||
382 | } | ||
383 | |||
384 | return tray_width; | ||
385 | } | ||
386 | |||
387 | void init_tray(struct bar *bar) { | ||
388 | if (!bar->config->tray_output || strcmp(bar->config->tray_output, "none") != 0) { | ||
389 | /* Connect to the D-Bus */ | ||
390 | dbus_init(); | ||
391 | |||
392 | /* Start the SNI watcher */ | ||
393 | init_sni_watcher(); | ||
394 | |||
395 | /* Start the SNI host */ | ||
396 | init_host(); | ||
397 | } | ||
398 | } | ||