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 | /*
* Copyright (C) 2016 Alberts Muktupāvels
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "sn-dbus-menu.h"
#include "sn-dbus-menu-gen.h"
#include "sn-dbus-menu-item.h"
struct _SnDBusMenu
{
CtkMenu parent;
GHashTable *items;
GCancellable *cancellable;
gchar *bus_name;
gchar *object_path;
guint name_id;
SnDBusMenuGen *proxy;
};
enum
{
PROP_0,
PROP_BUS_NAME,
PROP_OBJECT_PATH,
LAST_PROP
};
static GParamSpec *properties[LAST_PROP] = { NULL };
static const gchar *property_names[] =
{
"accessible-desc",
"children-display",
"disposition",
"enabled",
"icon-data",
"icon-name",
"label",
"shortcut",
"toggle-type",
"toggle-state",
"type",
"visible",
NULL
};
G_DEFINE_TYPE (SnDBusMenu, sn_dbus_menu, CTK_TYPE_MENU)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE is a macro then please configure it.
static void
activate_cb (CtkWidget *widget,
SnDBusMenu *menu)
{
guint id;
if (ctk_menu_item_get_submenu (CTK_MENU_ITEM (widget)) != NULL)
return;
id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "item-id"));
sn_dbus_menu_gen_call_event_sync (menu->proxy, id, "clicked",
g_variant_new ("v", g_variant_new_int32 (0)),
ctk_get_current_event_time (), NULL, NULL);
}
static CtkMenu *
layout_update_item (SnDBusMenu *menu,
CtkMenu *ctk_menu,
guint id,
GVariant *props)
{
SnDBusMenuItem *item;
if (id == 0)
return ctk_menu;
item = g_hash_table_lookup (menu->items, GUINT_TO_POINTER (id));
if (item == NULL)
{
item = sn_dbus_menu_item_new (props);
g_object_set_data (G_OBJECT (item->item), "item-id", GUINT_TO_POINTER (id));
ctk_menu_shell_append (CTK_MENU_SHELL (ctk_menu), item->item);
item->activate_id = g_signal_connect (item->item, "activate",
G_CALLBACK (activate_cb), menu);
g_hash_table_replace (menu->items, GUINT_TO_POINTER (id), item);
}
else
{
sn_dbus_menu_item_update_props (item, props);
}
return item->submenu;
}
static void
layout_parse (SnDBusMenu *menu,
GVariant *layout,
CtkMenu *ctk_menu)
{
guint id;
GVariant *props;
GVariant *items;
CtkMenu *submenu;
GVariantIter iter;
GVariant *child;
if (!g_variant_is_of_type (layout, G_VARIANT_TYPE ("(ia{sv}av)")))
{
g_warning ("Type of return value for 'layout' property in "
"'GetLayout' call should be '(ia{sv}av)' but got '%s'",
g_variant_get_type_string (layout));
return;
}
g_variant_get (layout, "(i@a{sv}@av)", &id, &props, &items);
submenu = layout_update_item (menu, ctk_menu, id, props);
g_variant_unref (props);
g_variant_iter_init (&iter, items);
while ((child = g_variant_iter_next_value (&iter)))
{
GVariant *value;
value = g_variant_get_variant (child);
layout_parse (menu, value, submenu);
g_variant_unref (value);
g_variant_unref (child);
}
g_variant_unref (items);
}
static void
get_layout_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GVariant *layout;
guint revision;
GError *error;
SnDBusMenu *menu;
error = NULL;
sn_dbus_menu_gen_call_get_layout_finish (SN_DBUS_MENU_GEN (source_object),
&revision, &layout, res, &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_error_free (error);
return;
}
menu = SN_DBUS_MENU (user_data);
if (error != NULL)
{
g_warning ("%s", error->message);
g_error_free (error);
return;
}
g_hash_table_remove_all (menu->items);
layout_parse (menu, layout, CTK_MENU (menu));
/* Reposition menu to accomodate any size changes */
/* Menu size never changes with CTK 3.20 or earlier */
ctk_menu_reposition(CTK_MENU(menu));
g_variant_unref (layout);
}
static void
update_layout (SnDBusMenu *menu,
gint parent)
{
gint depth;
parent = 0;
depth = -1;
sn_dbus_menu_gen_call_get_layout (menu->proxy, parent, depth,
property_names, menu->cancellable,
get_layout_cb, menu);
}
static void
items_properties_updated_cb (SnDBusMenuGen *proxy,
GVariant *updated_props,
GVariant *removed_props,
SnDBusMenu *menu)
{
GVariantIter iter;
guint id;
GVariant *props;
SnDBusMenuItem *item;
g_variant_iter_init (&iter, updated_props);
while (g_variant_iter_next (&iter, "(i@a{sv})", &id, &props))
{
item = g_hash_table_lookup (menu->items, GUINT_TO_POINTER (id));
if (item != NULL)
sn_dbus_menu_item_update_props (item, props);
g_variant_unref (props);
}
g_variant_iter_init (&iter, removed_props);
while (g_variant_iter_next (&iter, "(i@as)", &id, &props))
{
item = g_hash_table_lookup (menu->items, GUINT_TO_POINTER (id));
if (item != NULL)
sn_dbus_menu_item_remove_props (item, props);
g_variant_unref (props);
}
}
static void
layout_updated_cb (SnDBusMenuGen *proxy,
guint revision,
gint parent,
SnDBusMenu *menu)
{
update_layout (menu, parent);
}
static void
item_activation_requested_cb (SnDBusMenuGen *proxy,
gint id,
guint timestamp,
SnDBusMenu *menu)
{
g_debug ("activation requested: id - %d, timestamp - %d", id, timestamp);
}
static void
map_cb (CtkWidget *widget,
SnDBusMenu *menu)
{
gboolean need_update;
sn_dbus_menu_gen_call_event_sync (menu->proxy, 0, "opened",
g_variant_new ("v", g_variant_new_int32 (0)),
ctk_get_current_event_time (),
NULL, NULL);
sn_dbus_menu_gen_call_about_to_show_sync (menu->proxy, 0, &need_update,
NULL, NULL);
if (need_update)
{
update_layout (menu, 0);
}
}
static void
unmap_cb (CtkWidget *widget,
SnDBusMenu *menu)
{
sn_dbus_menu_gen_call_event_sync (menu->proxy, 0, "closed",
g_variant_new ("v", g_variant_new_int32 (0)),
ctk_get_current_event_time (),
NULL, NULL);
}
static void
proxy_ready_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
SnDBusMenuGen *proxy;
GError *error;
SnDBusMenu *menu;
error = NULL;
proxy = sn_dbus_menu_gen_proxy_new_finish (res, &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_error_free (error);
return;
}
menu = SN_DBUS_MENU (user_data);
menu->proxy = proxy;
if (error)
{
g_warning ("%s", error->message);
g_error_free (error);
return;
}
g_signal_connect (proxy, "items-properties-updated",
G_CALLBACK (items_properties_updated_cb), menu);
g_signal_connect (proxy, "layout-updated",
G_CALLBACK (layout_updated_cb), menu);
g_signal_connect (proxy, "item-activation-requested",
G_CALLBACK (item_activation_requested_cb), menu);
g_signal_connect (menu, "map", G_CALLBACK (map_cb), menu);
g_signal_connect (menu, "unmap", G_CALLBACK (unmap_cb), menu);
update_layout (menu, 0);
}
static void
name_appeared_cb (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
SnDBusMenu *menu;
menu = SN_DBUS_MENU (user_data);
sn_dbus_menu_gen_proxy_new (connection, G_DBUS_PROXY_FLAGS_NONE,
menu->bus_name, menu->object_path,
menu->cancellable, proxy_ready_cb, menu);
}
static void
name_vanished_cb (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
SnDBusMenu *menu;
menu = SN_DBUS_MENU (user_data);
g_clear_object (&menu->proxy);
}
static void
sn_dbus_menu_constructed (GObject *object)
{
SnDBusMenu *menu;
CtkWidget *toplevel;
CdkScreen *screen;
CdkVisual *visual;
CtkStyleContext *context;
G_OBJECT_CLASS (sn_dbus_menu_parent_class)->constructed (object);
menu = SN_DBUS_MENU (object);
/*Set up theme and transparency support*/
toplevel = ctk_widget_get_toplevel(CTK_WIDGET(menu));
/* Fix any failures of compiz/other wm's to communicate with ctk for transparency */
screen = ctk_widget_get_screen(CTK_WIDGET(toplevel));
visual = cdk_screen_get_rgba_visual(screen);
ctk_widget_set_visual(CTK_WIDGET(toplevel), visual);
/* Set menu and it's toplevel window to follow panel theme */
context = ctk_widget_get_style_context (CTK_WIDGET(toplevel));
ctk_style_context_add_class(context,"gnome-panel-menu-bar");
ctk_style_context_add_class(context,"cafe-panel-menu-bar");
menu->name_id = g_bus_watch_name (G_BUS_TYPE_SESSION, menu->bus_name,
G_BUS_NAME_WATCHER_FLAGS_NONE,
name_appeared_cb, name_vanished_cb,
menu, NULL);
}
static void
sn_dbus_menu_dispose (GObject *object)
{
SnDBusMenu *menu;
menu = SN_DBUS_MENU (object);
if (menu->name_id > 0)
{
g_bus_unwatch_name (menu->name_id);
menu->name_id = 0;
}
g_clear_pointer (&menu->items, g_hash_table_destroy);
g_cancellable_cancel (menu->cancellable);
g_clear_object (&menu->cancellable);
g_clear_object (&menu->proxy);
G_OBJECT_CLASS (sn_dbus_menu_parent_class)->dispose (object);
}
static void
sn_dbus_menu_finalize (GObject *object)
{
SnDBusMenu *menu;
menu = SN_DBUS_MENU (object);
g_free (menu->bus_name);
g_free (menu->object_path);
G_OBJECT_CLASS (sn_dbus_menu_parent_class)->finalize (object);
}
static void
sn_dbus_menu_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
SnDBusMenu *menu;
menu = SN_DBUS_MENU (object);
switch (property_id)
{
case PROP_BUS_NAME:
menu->bus_name = g_value_dup_string (value);
break;
case PROP_OBJECT_PATH:
menu->object_path = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
install_properties (GObjectClass *object_class)
{
properties[PROP_BUS_NAME] =
g_param_spec_string ("bus-name", "bus-name", "bus-name", NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
G_PARAM_STATIC_STRINGS);
properties[PROP_OBJECT_PATH] =
g_param_spec_string ("object-path", "object-path", "object-path", NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, LAST_PROP, properties);
}
static void
sn_dbus_menu_class_init (SnDBusMenuClass *menu_class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (menu_class);
object_class->constructed = sn_dbus_menu_constructed;
object_class->dispose = sn_dbus_menu_dispose;
object_class->finalize = sn_dbus_menu_finalize;
object_class->set_property = sn_dbus_menu_set_property;
install_properties (object_class);
}
static void
sn_dbus_menu_init (SnDBusMenu *menu)
{
menu->items = g_hash_table_new_full (NULL, NULL, NULL, sn_dubs_menu_item_free);
menu->cancellable = g_cancellable_new ();
}
CtkMenu *
sn_dbus_menu_new (const gchar *bus_name,
const gchar *object_path)
{
return g_object_new (SN_TYPE_DBUS_MENU,
"bus-name", bus_name,
"object-path", object_path,
NULL);
}
|