dwlb

feature-complete bar for dwl
Log | Files | Refs | README | LICENSE

sndbusmenu.c (18596B) - View raw


  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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include "sndbusmenu.h"

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>

#include "snitem.h"


struct _SnDbusmenu {
	GObject parent_instance;

	char* busname;
	char* busobj;
	SnItem* snitem;

	GMenu* menu;
	GSimpleActionGroup* actiongroup;
	GDBusProxy* proxy;

	uint32_t revision;
	gboolean update_pending;
	gboolean reschedule;
};

G_DEFINE_FINAL_TYPE(SnDbusmenu, sn_dbusmenu, G_TYPE_OBJECT)

enum
{
	PROP_BUSNAME=1,
	PROP_BUSOBJ,
	PROP_SNITEM,
	PROP_PROXY,
	N_PROPERTIES
};

enum
{
	ABOUT_TO_SHOW_HANDLED,
	LAST_SIGNAL
};

#define ACTION_NAME_MAX_LEN 32

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static unsigned int signals[LAST_SIGNAL];
static const char actiongroup_pfx[] = "menuitem";
static const int layout_update_freq = 80;

typedef struct {
	uint32_t id;
	GDBusProxy* proxy;
} ActionCallbackData;

static void		sn_dbusmenu_constructed		(GObject *object);
static void		sn_dbusmenu_dispose		(GObject *object);
static void		sn_dbusmenu_finalize		(GObject *object);

static void		sn_dbusmenu_get_property	(GObject *object,
                                                        unsigned int property_id,
                                                        GValue *value,
                                                        GParamSpec *pspec);

static void		sn_dbusmenu_set_property	(GObject *object,
                                                        unsigned int property_id,
                                                        const GValue *value,
                                                        GParamSpec *pspec);

static GMenu*		create_menumodel		(GVariant *data, SnDbusmenu *self);

static GMenuItem*	create_menuitem			(int32_t id, GVariant *menu_data,
                                                        GVariant *submenu_data,
                                                        SnDbusmenu *self);


static void
action_activated_handler(GSimpleAction *action, GVariant* param, ActionCallbackData *data)
{
	g_dbus_proxy_call(data->proxy,
	                  "Event",
	                  g_variant_new("(isvu)",
	                                data->id,
	                                "clicked",
	                                g_variant_new_string(""),
	                                time(NULL)),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1,
	                  NULL,
	                  NULL,
	                  NULL);
}

static void
action_free(void *data, GClosure *closure)
{
	ActionCallbackData *acbd = (ActionCallbackData *)data;
	g_free(acbd);
}

static GSimpleAction*
create_action(uint32_t id, gboolean ischeckmark, SnDbusmenu *self)
{
	GSimpleAction *action;
	char name[ACTION_NAME_MAX_LEN];
	sprintf(name, "%u", id);

	if (ischeckmark)
		action = g_simple_action_new_stateful(name, NULL, g_variant_new("b", TRUE));
	else
		action = g_simple_action_new(name, NULL);

	ActionCallbackData *data = g_malloc(sizeof(ActionCallbackData));
	data->id = id;
	data->proxy = self->proxy;

	g_signal_connect_data(action,
	                      "activate",
	                      G_CALLBACK(action_activated_handler),
	                      data,
	                      action_free,
	                      G_CONNECT_DEFAULT);

	return action;
}

static GMenuItem*
create_menuitem(int32_t id, GVariant *menuitem_data, GVariant *submenuitem_data, SnDbusmenu *self)
{
	GActionMap *actionmap = G_ACTION_MAP(self->actiongroup);

	char detailed_name[ACTION_NAME_MAX_LEN];
	const char *label = NULL;
	const char *type_s = NULL;
	const char *toggle_type_s = NULL;
	const char *has_submenu_s = NULL;

	gboolean isenabled = TRUE;
	gboolean isvisible = TRUE;
	gboolean isseparator = FALSE;
	gboolean ischeckmark = FALSE;
	gboolean has_submenu = FALSE;
	// gboolean isradio = FALSE;

	gboolean checkmark_toggle_state = TRUE;

	GVariantDict dict;
	g_variant_dict_init(&dict, menuitem_data);
	g_variant_dict_lookup(&dict, "label", "&s", &label);
	g_variant_dict_lookup(&dict, "type", "&s", &type_s);
	g_variant_dict_lookup(&dict, "enabled", "b", &isenabled);
	g_variant_dict_lookup(&dict, "visible", "b", &isvisible);
	g_variant_dict_lookup(&dict, "children-display", "&s", &has_submenu_s);
	g_variant_dict_lookup(&dict, "toggle-type", "&s", &toggle_type_s);
	g_variant_dict_lookup(&dict, "toggle-state", "i", &checkmark_toggle_state);
	g_variant_dict_clear(&dict);

	if (has_submenu_s && strcmp(has_submenu_s, "submenu") == 0)
		has_submenu = TRUE;

	if (type_s && strcmp(type_s, "separator") == 0)
		isseparator = TRUE;
	else if (toggle_type_s && strcmp(toggle_type_s, "checkmark") == 0)
		ischeckmark = TRUE;
	/*
	else if (toggle_type_s && strcmp(toggle_type_s, "radio") == 0)
		isradio = TRUE;
	*/

	if (!isvisible || isseparator)
		return NULL;

	GSimpleAction *action = create_action(id, ischeckmark, self);
	sprintf(detailed_name, "%s.%u", actiongroup_pfx, id);

	GMenuItem *menuitem = g_menu_item_new(label, detailed_name);

	if (!isenabled)
		g_simple_action_set_enabled(action, FALSE);

	if (ischeckmark)
		g_simple_action_set_state(action,
		                          g_variant_new("b",
		                                        checkmark_toggle_state));

	if (has_submenu) {
		GMenu *submenu = create_menumodel(submenuitem_data, self);
		g_menu_item_set_submenu(menuitem, G_MENU_MODEL(submenu));
		g_object_unref(submenu);
	}

	g_action_map_add_action(actionmap, G_ACTION(action));
	g_object_unref(action);

	return menuitem;
}

static GMenu*
create_menumodel(GVariant *data, SnDbusmenu *self)
{
	GMenu *ret = g_menu_new();
	GVariantIter iter;

	//  (ia{sv}av)
	GVariant *menuitem_data_packed;
	GVariant *menuitem_data;
	int32_t id;

	g_variant_iter_init(&iter, data);
	while ((g_variant_iter_next(&iter, "v", &menuitem_data_packed))) {
		g_variant_get_child(menuitem_data_packed, 0, "i", &id);
		menuitem_data = g_variant_get_child_value(menuitem_data_packed, 1);
		GVariant *submenu_data = g_variant_get_child_value(menuitem_data_packed, 2);
		GMenuItem *menuitem = create_menuitem(id, menuitem_data, submenu_data, self);
		if (menuitem) {
			g_menu_append_item(ret, menuitem);
			g_object_unref(menuitem);
		}
		g_variant_unref(submenu_data);
		g_variant_unref(menuitem_data);
		g_variant_unref(menuitem_data_packed);
	}

	return ret;
}

static void
layout_update_finish(GObject *obj, GAsyncResult *res, void *udata)
{
	SnDbusmenu *self = SN_DBUSMENU(udata);
	GError *err = NULL;

	GVariant *data = g_dbus_proxy_call_finish(self->proxy, res, &err);

	if (err) {
		g_debug("Error in layout_update %s", err->message);
		g_error_free(err);

		g_object_unref(self->snitem);
		g_object_unref(self);
		return;
	}

	GVariant *layout;
	GVariant *menuitems;

	layout = g_variant_get_child_value(data, 1);
	menuitems = g_variant_get_child_value(layout, 2);

	gboolean isvisible = sn_item_get_popover_visible(self->snitem);
	if (isvisible) {
		self->reschedule = TRUE;
		g_debug("Popover was visible, couldn't update menu %s", self->busname);
	} else {
		GSimpleActionGroup *newag = g_simple_action_group_new();
		sn_item_set_actiongroup(self->snitem, actiongroup_pfx, newag);
		g_object_unref(self->actiongroup);
		self->actiongroup = newag;

		GMenu *newmenu = create_menumodel(menuitems, self);
		sn_item_set_menu_model(self->snitem, newmenu);
		g_object_unref(self->menu);
		self->menu = newmenu;
	}

	g_variant_unref(menuitems);
	g_variant_unref(layout);

	g_variant_unref(data);

	g_object_unref(self->snitem);
	g_object_unref(self);
}

static void
layout_update(SnDbusmenu *self)
{
	g_debug("%s running menu layout update", self->busname);
	self->update_pending = FALSE;

	g_dbus_proxy_call(self->proxy,
	                  "GetLayout",
	                  g_variant_new("(iias)", 0, -1, NULL),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1,
	                  NULL,
	                  layout_update_finish,
	                  self);
}

static void
reschedule_update(SnItem *snitem, GParamSpec *pspec, void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	g_return_if_fail(SN_IS_ITEM(self->snitem));

	gboolean popover_visible = sn_item_get_popover_visible(snitem);
	if (popover_visible || !self->reschedule)
		return;

	g_debug("%s rescheduling layout update", self->busname);

	self->reschedule = FALSE;

	g_object_ref(self);
	g_object_ref(self->snitem);
	layout_update(self);
}

// Update signals are often received multiple times in row,
// we throttle update frequency to *layout_update_freq*
static void
proxy_signal_handler(GDBusProxy *proxy,
                     const char *sender,
                     const char *signal,
                     GVariant *params,
                     void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	g_return_if_fail(SN_IS_ITEM(self->snitem));

	if (strcmp(signal, "LayoutUpdated") == 0) {
		uint32_t revision;
		int32_t parentid;
		g_variant_get(params, "(ui)", &revision, &parentid);
		g_debug("%s got LayoutUpdated, revision %i, parentid %i", self->busname, revision, parentid);

		if (self->revision == UINT32_MAX || self->revision < revision) {
			self->revision = revision;
		}

		if (!self->update_pending) {
			self->update_pending = TRUE;
			g_object_ref(self->snitem);
			g_timeout_add_once(layout_update_freq,
			                   (GSourceOnceFunc)layout_update,
			                   g_object_ref(self));
		} else {
			g_debug("skipping update");
		}

	} else if (strcmp(signal, "ItemsPropertiesUpdated") == 0) {
		g_debug("%s got ItemsPropertiesUpdated", self->busname);

		if (!self->update_pending) {
			self->update_pending = TRUE;
			g_object_ref(self->snitem);
			g_timeout_add_once(layout_update_freq,
			                   (GSourceOnceFunc)layout_update,
			                   g_object_ref(self));
		} else {
			g_debug("skipping update");
		}
	}
}

static void
menulayout_ready_handler(GObject *obj, GAsyncResult *res, void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	GError *err = NULL;
	GVariant *retvariant = g_dbus_proxy_call_finish(self->proxy, res, &err);
	// (u(ia{sv}av))

	// "No such object path '/NO_DBUSMENU'"
	// generated by QBittorrent when it sends a broken trayitem on startup
	// and replaces it later
	if (err && g_error_matches(err, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_OBJECT)) {
		g_error_free(err);
		g_object_unref(self);
		return;
	} else if (err) {
		g_warning("%s\n", err->message);
		g_error_free(err);
		g_object_unref(self);
		return;
	}

	uint32_t revision = 0;
	GVariant *layout;
	GVariant *menuitems;

	g_variant_get_child(retvariant, 0, "u", &revision);

	layout = g_variant_get_child_value(retvariant, 1);
	menuitems = g_variant_get_child_value(layout, 2);

	self->menu = create_menumodel(menuitems, self);
	sn_item_set_menu_model(self->snitem, self->menu);

	g_variant_unref(menuitems);
	g_variant_unref(layout);
	g_variant_unref(retvariant);
	g_object_unref(self);
}

static void
about_to_show_timeout_handler(void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	g_signal_emit(self, signals[ABOUT_TO_SHOW_HANDLED], 0);
	g_object_unref(self);
}

static void
about_to_show_handler(GObject *obj, GAsyncResult *res, void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	GError *err = NULL;
	GVariant *val =  g_dbus_proxy_call_finish(self->proxy, res, &err);

	// I give up trying to get nm-applet working properly. Wait 2 seconds until popping the menu
	// to let it finish its business.
	int timeout;
	if (strcmp(self->busobj, "/org/ayatana/NotificationItem/nm_applet/Menu" ) == 0) {
		timeout = 2000;
	} else {
		timeout = 100;
	}

	// Discord generates the following error here:
	// 'G_DBUS_ERROR' 'G_DBUS_ERROR_FAILED' 'error occurred in AboutToShow'
	// We ignore it.
	if (err && !g_error_matches(err, G_DBUS_ERROR, G_DBUS_ERROR_FAILED) &&
	    g_strrstr(err->message, "error occured in AboutToShow") != 0) {
		g_warning("%s\n", err->message);

	} else {
		// This dbusmenu call might have triggered a menu update,
		g_timeout_add_once(timeout, about_to_show_timeout_handler, g_object_ref(self));
	}

	err ? g_error_free(err) : g_variant_unref(val);
	g_object_unref(self);
}


static void
rightclick_handler(GObject *obj, void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	g_assert(SN_IS_DBUSMENU(self));
	g_dbus_proxy_call(self->proxy,
	                  "AboutToShow",
	                  g_variant_new("(i)", 0),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1,
	                  NULL,
	                  about_to_show_handler,
	                  g_object_ref(self));
}

static void
proxy_ready_handler(GObject *obj, GAsyncResult *res, void *data)
{
	SnDbusmenu *self = SN_DBUSMENU(data);

	GError *err = NULL;
	GDBusProxy *proxy = g_dbus_proxy_new_for_bus_finish(res, &err);

	if (err) {
		g_warning("Failed to construct gdbusproxy for menu: %s\n", err->message);
		g_error_free(err);
		g_object_unref(self);
		return;
	}

	g_debug("Created gdbusproxy for menu %s %s",
		g_dbus_proxy_get_name(proxy),
		g_dbus_proxy_get_object_path(proxy));

	g_object_set(self, "proxy", proxy, NULL);

	g_dbus_proxy_call(self->proxy,
	                  "GetLayout",
	                  g_variant_new ("(iias)", 0, -1, NULL),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1,
	                  NULL,
	                  menulayout_ready_handler,
	                  g_object_ref(self));

	g_signal_connect(self->proxy, "g-signal", G_CALLBACK(proxy_signal_handler), self);
	g_object_unref(self);
}

static void
sn_dbusmenu_get_property(GObject *object,
                         unsigned int property_id,
                         GValue *value,
                         GParamSpec *pspec)
{
	G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}

static void
sn_dbusmenu_set_property(GObject *object,
                         unsigned int property_id,
                         const GValue *value,
                         GParamSpec *pspec)
{
	SnDbusmenu *self = SN_DBUSMENU(object);

	switch (property_id) {
		case PROP_BUSNAME:
			self->busname = g_strdup(g_value_get_string(value));
			break;
		case PROP_BUSOBJ:
			self->busobj = g_strdup(g_value_get_string(value));
			break;
		case PROP_SNITEM:
			self->snitem = g_value_get_object(value);
			break;
		case PROP_PROXY:
			self->proxy = g_value_get_object(value);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
			break;
	}
}

static void
sn_dbusmenu_class_init(SnDbusmenuClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->set_property = sn_dbusmenu_set_property;
	object_class->get_property = sn_dbusmenu_get_property;
	object_class->constructed = sn_dbusmenu_constructed;
	object_class->dispose = sn_dbusmenu_dispose;
	object_class->finalize = sn_dbusmenu_finalize;

	obj_properties[PROP_BUSNAME] =
		g_param_spec_string("busname", NULL, NULL,
		                    NULL,
		                    G_PARAM_CONSTRUCT_ONLY |
		                    G_PARAM_WRITABLE |
		                    G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_BUSOBJ] =
		g_param_spec_string("busobj", NULL, NULL,
		                    NULL,
		                    G_PARAM_CONSTRUCT_ONLY |
		                    G_PARAM_WRITABLE |
		                    G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_SNITEM] =
		g_param_spec_object("snitem", NULL, NULL,
		                    SN_TYPE_ITEM,
		                    G_PARAM_CONSTRUCT_ONLY |
		                    G_PARAM_WRITABLE |
		                    G_PARAM_STATIC_STRINGS);

	obj_properties[PROP_PROXY] =
		g_param_spec_object("proxy", NULL, NULL,
		                    G_TYPE_DBUS_PROXY,
		                    G_PARAM_WRITABLE |
		                    G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);

	signals[ABOUT_TO_SHOW_HANDLED] = g_signal_new("abouttoshowhandled",
	                                              SN_TYPE_DBUSMENU,
	                                              G_SIGNAL_RUN_LAST,
	                                              0,
	                                              NULL,
	                                              NULL,
	                                              NULL,
	                                              G_TYPE_NONE,
	                                              0);
}

static void
sn_dbusmenu_init(SnDbusmenu *self)
{
	// When reschedule is TRUE, menu will be updated next time it is closed.
	self->reschedule = FALSE;
	self->update_pending = FALSE;

	self->actiongroup = g_simple_action_group_new();
}

static void
sn_dbusmenu_constructed(GObject *obj)
{
	SnDbusmenu *self = SN_DBUSMENU(obj);

	sn_item_set_actiongroup(self->snitem, actiongroup_pfx, self->actiongroup);

	GDBusNodeInfo *nodeinfo = g_dbus_node_info_new_for_xml(DBUSMENU_XML, NULL);
	g_dbus_proxy_new_for_bus(G_BUS_TYPE_SESSION,
	                         G_DBUS_PROXY_FLAGS_NONE,
	                         nodeinfo->interfaces[0],
	                         self->busname,
	                         self->busobj,
	                         "com.canonical.dbusmenu",
	                         NULL,
	                         (GAsyncReadyCallback)proxy_ready_handler,
	                         g_object_ref(self));
	g_dbus_node_info_unref(nodeinfo);

	g_signal_connect(self->snitem, "notify::menuvisible", G_CALLBACK(reschedule_update), self);

	g_signal_connect(self->snitem, "rightclick", G_CALLBACK(rightclick_handler), self);

	G_OBJECT_CLASS(sn_dbusmenu_parent_class)->constructed(obj);
}

static void
sn_dbusmenu_dispose(GObject *obj)
{
	SnDbusmenu *self = SN_DBUSMENU(obj);

	g_debug("Disposing sndbusmenu %s %s", self->busname, self->busobj);

	if (self->proxy) {
		g_object_unref(self->proxy);
		self->proxy = NULL;
	}

	if (self->actiongroup) {
		sn_item_clear_actiongroup(self->snitem, actiongroup_pfx);
		g_object_unref(self->actiongroup);
		self->actiongroup = NULL;
	}

	if (self->menu) {
		sn_item_clear_menu_model(self->snitem);
		g_object_unref(self->menu);
		self->menu = NULL;
	}

	if (self->snitem) {
		g_object_unref(self->snitem);
		self->snitem = NULL;
	}

	G_OBJECT_CLASS(sn_dbusmenu_parent_class)->dispose(obj);
}

static void
sn_dbusmenu_finalize(GObject *obj)
{
	SnDbusmenu *self = SN_DBUSMENU(obj);
	g_free(self->busname);
	g_free(self->busobj);
	G_OBJECT_CLASS(sn_dbusmenu_parent_class)->finalize(obj);
}

SnDbusmenu*
sn_dbusmenu_new(const char *busname, const char *busobj, SnItem *snitem)
{
	return g_object_new(SN_TYPE_DBUSMENU,
	                    "busname", busname,
	                    "busobj", busobj,
	                    "snitem", snitem,
	                    NULL);
}