1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <dbus/dbus.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "swaybar/tray/dbus.h"
#include "swaybar/tray/sni.h"
#include "swaybar/tray/icon.h"
#include "swaybar/bar.h"
#include "client/cairo.h"
#include "log.h"
// Not sure what this is but cairo needs it.
static const cairo_user_data_key_t cairo_user_data_key;
struct sni_icon_ref *sni_icon_ref_create(struct StatusNotifierItem *item,
int height) {
struct sni_icon_ref *sni_ref = malloc(sizeof(struct sni_icon_ref));
if (!sni_ref) {
return NULL;
}
sni_ref->icon = cairo_image_surface_scale(item->image, height, height);
sni_ref->ref = item;
return sni_ref;
}
void sni_icon_ref_free(struct sni_icon_ref *sni_ref) {
if (!sni_ref) {
return;
}
cairo_surface_destroy(sni_ref->icon);
free(sni_ref);
}
/* Gets the pixmap of an icon */
static void reply_icon(DBusPendingCall *pending, void *_data) {
struct StatusNotifierItem *item = _data;
DBusMessage *reply = dbus_pending_call_steal_reply(pending);
if (!reply) {
sway_log(L_ERROR, "Did not get reply");
goto bail;
}
int message_type = dbus_message_get_type(reply);
if (message_type == DBUS_MESSAGE_TYPE_ERROR) {
char *msg;
dbus_message_get_args(reply, NULL,
DBUS_TYPE_STRING, &msg,
DBUS_TYPE_INVALID);
sway_log(L_ERROR, "Message is error: %s", msg);
goto bail;
}
DBusMessageIter iter;
DBusMessageIter variant; /* v[a(iiay)] */
DBusMessageIter array; /* a(iiay) */
DBusMessageIter d_struct; /* (iiay) */
DBusMessageIter icon; /* ay */
dbus_message_iter_init(reply, &iter);
// Each if here checks the types above before recursing
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
sway_log(L_ERROR, "Relpy type incorrect");
sway_log(L_ERROR, "Should be \"v\", is \"%s\"",
dbus_message_iter_get_signature(&iter));
goto bail;
}
dbus_message_iter_recurse(&iter, &variant);
if (strcmp("a(iiay)", dbus_message_iter_get_signature(&variant)) != 0) {
sway_log(L_ERROR, "Relpy type incorrect");
sway_log(L_ERROR, "Should be \"a(iiay)\", is \"%s\"",
dbus_message_iter_get_signature(&variant));
goto bail;
}
if (dbus_message_iter_get_element_count(&variant) == 0) {
// Can't recurse if there are no items
sway_log(L_INFO, "Item has no icon");
goto bail;
}
dbus_message_iter_recurse(&variant, &array);
dbus_message_iter_recurse(&array, &d_struct);
int width;
dbus_message_iter_get_basic(&d_struct, &width);
dbus_message_iter_next(&d_struct);
int height;
dbus_message_iter_get_basic(&d_struct, &height);
dbus_message_iter_next(&d_struct);
int len = dbus_message_iter_get_element_count(&d_struct);
if (!len) {
sway_log(L_ERROR, "No icon data");
goto bail;
}
// Also implies len % 4 == 0, useful below
if (len != width * height * 4) {
sway_log(L_ERROR, "Incorrect array size passed");
goto bail;
}
dbus_message_iter_recurse(&d_struct, &icon);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
// FIXME support a variable stride
// (works on my machine though for all tested widths)
if (!sway_assert(stride == width * 4, "Stride must be equal to byte length")) {
goto bail;
}
// Data is by reference, no need to free
uint8_t *message_data;
dbus_message_iter_get_fixed_array(&icon, &message_data, &len);
uint8_t *image_data = malloc(stride * height);
if (!image_data) {
sway_log(L_ERROR, "Could not allocate memory for icon");
goto bail;
}
// Transform from network byte order to host byte order
// Assumptions are safe because the equality above
uint32_t *network = (uint32_t *) message_data;
uint32_t *host = (uint32_t *)image_data;
for (int i = 0; i < width * height; ++i) {
host[i] = ntohl(network[i]);
}
cairo_surface_t *image = cairo_image_surface_create_for_data(
image_data, CAIRO_FORMAT_ARGB32,
width, height, stride);
if (image) {
if (item->image) {
cairo_surface_destroy(item->image);
}
item->image = image;
// Free the image data on surface destruction
cairo_surface_set_user_data(image,
&cairo_user_data_key,
image_data,
free);
item->dirty = true;
dirty = true;
dbus_message_unref(reply);
return;
} else {
sway_log(L_ERROR, "Could not create image surface");
free(image_data);
}
bail:
if (reply) {
dbus_message_unref(reply);
}
sway_log(L_ERROR, "Could not get icon from item");
return;
}
static void send_icon_msg(struct StatusNotifierItem *item) {
DBusPendingCall *pending;
DBusMessage *message = dbus_message_new_method_call(
item->name,
"/StatusNotifierItem",
"org.freedesktop.DBus.Properties",
"Get");
const char *iface;
if (item->kde_special_snowflake) {
iface = "org.kde.StatusNotifierItem";
} else {
iface = "org.freedesktop.StatusNotifierItem";
}
const char *prop = "IconPixmap";
dbus_message_append_args(message,
DBUS_TYPE_STRING, &iface,
DBUS_TYPE_STRING, &prop,
DBUS_TYPE_INVALID);
bool status =
dbus_connection_send_with_reply(conn, message, &pending, -1);
dbus_message_unref(message);
if (!(pending || status)) {
sway_log(L_ERROR, "Could not get item icon");
return;
}
dbus_pending_call_set_notify(pending, reply_icon, item, NULL);
}
/* Get an icon by its name */
static void reply_icon_name(DBusPendingCall *pending, void *_data) {
struct StatusNotifierItem *item = _data;
DBusMessage *reply = dbus_pending_call_steal_reply(pending);
if (!reply) {
sway_log(L_INFO, "Got no icon name reply from item");
goto bail;
}
int message_type = dbus_message_get_type(reply);
if (message_type == DBUS_MESSAGE_TYPE_ERROR) {
char *msg;
dbus_message_get_args(reply, NULL,
DBUS_TYPE_STRING, &msg,
DBUS_TYPE_INVALID);
sway_log(L_INFO, "Could not get icon name: %s", msg);
goto bail;
}
DBusMessageIter iter; /* v[s] */
DBusMessageIter variant; /* s */
dbus_message_iter_init(reply, &iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
sway_log(L_ERROR, "Relpy type incorrect");
sway_log(L_ERROR, "Should be \"v\", is \"%s\"",
dbus_message_iter_get_signature(&iter));
goto bail;
}
dbus_message_iter_recurse(&iter, &variant);
if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_STRING) {
sway_log(L_ERROR, "Relpy type incorrect");
sway_log(L_ERROR, "Should be \"s\", is \"%s\"",
dbus_message_iter_get_signature(&iter));
goto bail;
}
char *icon_name;
dbus_message_iter_get_basic(&variant, &icon_name);
cairo_surface_t *image = find_icon(icon_name, 256);
if (image) {
sway_log(L_DEBUG, "Icon for %s found with size %d", icon_name,
cairo_image_surface_get_width(image));
if (item->image) {
cairo_surface_destroy(item->image);
}
item->image = image;
item->dirty = true;
dirty = true;
dbus_message_unref(reply);
return;
}
bail:
if (reply) {
dbus_message_unref(reply);
}
// Now try the pixmap
send_icon_msg(item);
return;
}
static void send_icon_name_msg(struct StatusNotifierItem *item) {
DBusPendingCall *pending;
DBusMessage *message = dbus_message_new_method_call(
item->name,
"/StatusNotifierItem",
"org.freedesktop.DBus.Properties",
"Get");
const char *iface;
if (item->kde_special_snowflake) {
iface = "org.kde.StatusNotifierItem";
} else {
iface = "org.freedesktop.StatusNotifierItem";
}
const char *prop = "IconName";
dbus_message_append_args(message,
DBUS_TYPE_STRING, &iface,
DBUS_TYPE_STRING, &prop,
DBUS_TYPE_INVALID);
bool status =
dbus_connection_send_with_reply(conn, message, &pending, -1);
dbus_message_unref(message);
if (!(pending || status)) {
sway_log(L_ERROR, "Could not get item icon name");
return;
}
dbus_pending_call_set_notify(pending, reply_icon_name, item, NULL);
}
void get_icon(struct StatusNotifierItem *item) {
send_icon_name_msg(item);
}
void sni_activate(struct StatusNotifierItem *item, uint32_t x, uint32_t y) {
const char *iface =
(item->kde_special_snowflake ? "org.kde.StatusNotifierItem"
: "org.freedesktop.StatusNotifierItem");
DBusMessage *message = dbus_message_new_method_call(
item->name,
"/StatusNotifierItem",
iface,
"Activate");
dbus_message_append_args(message,
DBUS_TYPE_INT32, &x,
DBUS_TYPE_INT32, &y,
DBUS_TYPE_INVALID);
dbus_connection_send(conn, message, NULL);
dbus_message_unref(message);
}
void sni_context_menu(struct StatusNotifierItem *item, uint32_t x, uint32_t y) {
const char *iface =
(item->kde_special_snowflake ? "org.kde.StatusNotifierItem"
: "org.freedesktop.StatusNotifierItem");
DBusMessage *message = dbus_message_new_method_call(
item->name,
"/StatusNotifierItem",
iface,
"ContextMenu");
dbus_message_append_args(message,
DBUS_TYPE_INT32, &x,
DBUS_TYPE_INT32, &y,
DBUS_TYPE_INVALID);
dbus_connection_send(conn, message, NULL);
dbus_message_unref(message);
}
void sni_secondary(struct StatusNotifierItem *item, uint32_t x, uint32_t y) {
const char *iface =
(item->kde_special_snowflake ? "org.kde.StatusNotifierItem"
: "org.freedesktop.StatusNotifierItem");
DBusMessage *message = dbus_message_new_method_call(
item->name,
"/StatusNotifierItem",
iface,
"SecondaryActivate");
dbus_message_append_args(message,
DBUS_TYPE_INT32, &x,
DBUS_TYPE_INT32, &y,
DBUS_TYPE_INVALID);
dbus_connection_send(conn, message, NULL);
dbus_message_unref(message);
}
static void get_unique_name(struct StatusNotifierItem *item) {
// I think that we're fine being sync here becaues the message is
// directly to the message bus. Could be async though.
DBusMessage *message = dbus_message_new_method_call(
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"GetNameOwner");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &item->name,
DBUS_TYPE_INVALID);
DBusMessage *reply = dbus_connection_send_with_reply_and_block(
conn, message, -1, NULL);
dbus_message_unref(message);
if (!reply) {
sway_log(L_ERROR, "Could not get unique name for item: %s",
item->name);
return;
}
char *unique_name;
if (!dbus_message_get_args(reply, NULL,
DBUS_TYPE_STRING, &unique_name,
DBUS_TYPE_INVALID)) {
sway_log(L_ERROR, "Error parsing method args");
} else {
if (item->unique_name) {
free(item->unique_name);
}
item->unique_name = strdup(unique_name);
}
dbus_message_unref(reply);
}
struct StatusNotifierItem *sni_create(const char *name) {
// Make sure `name` is well formed
if (!dbus_validate_bus_name(name, NULL)) {
sway_log(L_INFO, "Name (%s) is not a bus name. We cannot create an item.", name);
return NULL;
}
struct StatusNotifierItem *item = malloc(sizeof(struct StatusNotifierItem));
item->name = strdup(name);
item->unique_name = NULL;
item->image = NULL;
item->dirty = false;
// If it doesn't use this name then assume that it uses the KDE spec
// This is because xembed-sni-proxy uses neither "org.freedesktop" nor
// "org.kde" and just gives us the items "unique name"
//
// We could use this to our advantage and fill out the "unique name"
// field with the given name if it is neither freedesktop or kde, but
// that's makes us rely on KDE hackyness which is bad practice
const char freedesktop_name[] = "org.freedesktop";
if (strncmp(name, freedesktop_name, sizeof(freedesktop_name) - 1) != 0) {
item->kde_special_snowflake = true;
} else {
item->kde_special_snowflake = false;
}
get_icon(item);
get_unique_name(item);
return item;
}
/* Return 0 if `item` has a name of `str` */
int sni_str_cmp(const void *_item, const void *_str) {
const struct StatusNotifierItem *item = _item;
const char *str = _str;
return strcmp(item->name, str);
}
/* Returns 0 if `item` has a unique name of `str` */
int sni_uniq_cmp(const void *_item, const void *_str) {
const struct StatusNotifierItem *item = _item;
const char *str = _str;
if (!item->unique_name) {
return false;
}
return strcmp(item->unique_name, str);
}
void sni_free(struct StatusNotifierItem *item) {
if (!item) {
return;
}
free(item->name);
if (item->unique_name) {
free(item->unique_name);
}
if (item->image) {
cairo_surface_destroy(item->image);
}
free(item);
}
|