aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/tray/sni.c
blob: be7d9fd7f45cd5d5c36e20474bbf51bb5b2ae2ad (plain) (blame)
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
#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"

static const char *KDE_IFACE = "org.kde.StatusNotifierItem";
static const char *FD_IFACE = "org.freedesktop.StatusNotifierItem";

// 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(DBusMessageIter *iter /* a(iiay) */, void *_data, enum property_status status) {
	if (status != PROP_EXISTS) {
		return;
	}
	struct StatusNotifierItem *item = _data;

	DBusMessageIter d_struct; /* (iiay) */
	DBusMessageIter struct_items;
	DBusMessageIter icon;

	if (dbus_message_iter_get_element_count(iter) == 0) {
		// Can't recurse if there are no items
		sway_log(L_INFO, "Item has no icon");
		return;
	}

	dbus_message_iter_recurse(iter, &d_struct);
	dbus_message_iter_recurse(&d_struct, &struct_items);

	int width;
	dbus_message_iter_get_basic(&struct_items, &width);
	dbus_message_iter_next(&struct_items);

	int height;
	dbus_message_iter_get_basic(&struct_items, &height);
	dbus_message_iter_next(&struct_items);

	int len = dbus_message_iter_get_element_count(&struct_items);

	if (!len) {
		sway_log(L_ERROR, "No icon data");
		return;
	}

	// Also implies len % 4 == 0, useful below
	if (len != width * height * 4) {
		sway_log(L_ERROR, "Incorrect array size passed");
		return;
	}

	dbus_message_iter_recurse(&struct_items, &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")) {
		return;
	}

	// 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");
		return;
	}

	// 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;

		return;
	} else {
		sway_log(L_ERROR, "Could not create image surface");
		free(image_data);
	}

	sway_log(L_ERROR, "Could not get icon from item");
	return;
}

/* Get an icon by its name */
static void reply_icon_name(DBusMessageIter *iter, void *_data, enum property_status status) {
	struct StatusNotifierItem *item = _data;

	if (status != PROP_EXISTS) {
		dbus_get_prop_async(item->name, item->object_path,
			(item->kde_special_snowflake ? KDE_IFACE : FD_IFACE),
			"IconPixmap", "a(iiay)", reply_icon, item);
		return;
	}

	char *icon_name;
	dbus_message_iter_get_basic(iter, &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;

		return;
	}

	// Now try the pixmap
	dbus_get_prop_async(item->name, item->object_path,
		(item->kde_special_snowflake ? KDE_IFACE : FD_IFACE),
		"IconPixmap", "a(iiay)", reply_icon, item);
}

void get_icon(struct StatusNotifierItem *item) {
	dbus_get_prop_async(item->name, item->object_path,
		(item->kde_special_snowflake ? KDE_IFACE : FD_IFACE),
		"IconName", "s", reply_icon_name, 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,
			item->object_path,
			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");
	sway_log(L_INFO, "Activating context menu for item: (%s,%s)", item->name, item->object_path);
	DBusMessage *message = dbus_message_new_method_call(
			item->name,
			item->object_path,
			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,
			item->object_path,
			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;
	// TODO use static str if the default path instead of all these god-damn strdups
	item->object_path = strdup("/StatusNotifierItem");
	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;
}
struct StatusNotifierItem *sni_create_from_obj_path(const char *unique_name,
		const char *object_path) {
	struct StatusNotifierItem *item = malloc(sizeof(struct StatusNotifierItem));
	// XXX strdup-ing twice to avoid a double-free; see above todo
	item->name = strdup(unique_name);
	item->unique_name = strdup(unique_name);
	item->object_path = strdup(object_path);
	item->image = NULL;
	item->dirty = false;
	// If they're registering by obj-path they're a special snowflake
	item->kde_special_snowflake = true;

	get_icon(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);
}
int sni_obj_name_cmp(const void *_item, const void *_obj_name) {
	const struct StatusNotifierItem *item = _item;
	const struct ObjName *obj_name = _obj_name;

	if (strcmp(item->unique_name, obj_name->name) == 0 &&
			strcmp(item->object_path, obj_name->obj_path) == 0) {
		return 0;
	}
	return 1;
}

void sni_free(struct StatusNotifierItem *item) {
	if (!item) {
		return;
	}
	free(item->name);
	free(item->unique_name);
	free(item->object_path);
	if (item->image) {
		cairo_surface_destroy(item->image);
	}
	free(item);
}