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
|
/*
* mate-panel-applet-factory.c: panel applet writing API.
*
* Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
* Copyright (C) 2012-2021 MATE Developers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include "mate-panel-applet.h"
#include "panel-applet-private.h"
#include "mate-panel-applet-factory.h"
#ifdef HAVE_X11
#include <gdk/gdkx.h>
#endif
struct _MatePanelAppletFactory {
GObject base;
gchar *factory_id;
guint n_applets;
gboolean out_of_process;
GType applet_type;
GClosure *closure;
GDBusConnection *connection;
gint owner_id;
gint registration_id;
GHashTable *applets;
guint next_uid;
};
#define MATE_PANEL_APPLET_FACTORY_OBJECT_PATH "/org/mate/panel/applet/%s"
#define MATE_PANEL_APPLET_FACTORY_SERVICE_NAME "org.mate.panel.applet.%s"
G_DEFINE_TYPE (MatePanelAppletFactory, mate_panel_applet_factory, G_TYPE_OBJECT)
static GHashTable *factories = NULL;
static void
mate_panel_applet_factory_finalize (GObject *object)
{
MatePanelAppletFactory *factory = MATE_PANEL_APPLET_FACTORY (object);
if (factory->registration_id) {
g_dbus_connection_unregister_object (factory->connection,
factory->registration_id);
factory->registration_id = 0;
}
if (factory->owner_id) {
g_bus_unown_name (factory->owner_id);
factory->owner_id = 0;
}
g_hash_table_remove (factories, factory->factory_id);
if (g_hash_table_size (factories) == 0) {
g_hash_table_unref (factories);
factories = NULL;
}
if (factory->factory_id) {
g_free (factory->factory_id);
factory->factory_id = NULL;
}
if (factory->applets) {
g_hash_table_unref (factory->applets);
factory->applets = NULL;
}
if (factory->closure) {
g_closure_unref (factory->closure);
factory->closure = NULL;
}
G_OBJECT_CLASS (mate_panel_applet_factory_parent_class)
->finalize (object);
}
static void
mate_panel_applet_factory_init (MatePanelAppletFactory *factory)
{
factory->applets = g_hash_table_new (NULL, NULL);
factory->next_uid = 1;
}
static void
mate_panel_applet_factory_class_init (MatePanelAppletFactoryClass *klass)
{
GObjectClass *g_object_class;
g_object_class = G_OBJECT_CLASS (klass);
g_object_class->finalize = mate_panel_applet_factory_finalize;
}
static void
mate_panel_applet_factory_applet_removed (MatePanelAppletFactory *factory,
GObject *applet)
{
guint uid;
uid = GPOINTER_TO_UINT (g_object_get_data (applet, "uid"));
g_hash_table_remove (factory->applets, GUINT_TO_POINTER (uid));
factory->n_applets--;
if (factory->n_applets == 0)
g_object_unref (factory);
}
MatePanelAppletFactory *
mate_panel_applet_factory_new (const gchar *factory_id,
gboolean out_of_process,
GType applet_type,
GClosure *closure)
{
MatePanelAppletFactory *factory;
factory = MATE_PANEL_APPLET_FACTORY (g_object_new (PANEL_TYPE_APPLET_FACTORY, NULL));
factory->factory_id = g_strdup (factory_id);
factory->out_of_process = out_of_process;
factory->applet_type = applet_type;
factory->closure = g_closure_ref (closure);
if (factories == NULL)
factories = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (factories, factory->factory_id, factory);
return factory;
}
static void
set_applet_constructor_properties (GObject *applet,
GVariant *props)
{
GVariantIter iter;
GVariant *value;
gchar *key;
g_variant_iter_init (&iter, props);
while (g_variant_iter_loop (&iter, "{sv}", &key, &value)) {
switch (g_variant_classify (value)) {
case G_VARIANT_CLASS_UINT32: {
guint32 v = g_variant_get_uint32 (value);
g_object_set (applet, key, v, NULL);
break;
} case G_VARIANT_CLASS_STRING: {
const gchar *v = g_variant_get_string (value, NULL);
g_object_set (applet, key, v, NULL);
break;
} case G_VARIANT_CLASS_BOOLEAN: {
gboolean v = g_variant_get_boolean (value);
g_object_set (applet, key, v, NULL);
break;
} default:
g_assert_not_reached ();
}
}
}
static void
mate_panel_applet_factory_get_applet (MatePanelAppletFactory *factory,
GDBusConnection *connection,
GVariant *parameters,
GDBusMethodInvocation *invocation)
{
GObject *applet;
const gchar *applet_id;
gint screen_num;
GVariant *props;
GVariant *return_value;
guint32 xid;
guint32 uid;
const gchar *object_path;
g_variant_get (parameters, "(&si@a{sv})", &applet_id, &screen_num, &props);
applet = g_object_new (factory->applet_type,
"out-of-process", factory->out_of_process,
"id", applet_id,
"connection", connection,
"closure", factory->closure,
NULL);
factory->n_applets++;
g_object_weak_ref (applet,
(GWeakNotify) mate_panel_applet_factory_applet_removed,
factory);
set_applet_constructor_properties (applet, props);
g_variant_unref (props);
#ifdef HAVE_X11
if (GDK_IS_X11_DISPLAY (gdk_display_get_default ())) {
GdkScreen *screen;
screen = screen_num != -1 ?
gdk_display_get_default_screen (gdk_display_get_default ()) :
gdk_screen_get_default ();
xid = mate_panel_applet_get_xid (MATE_PANEL_APPLET (applet), screen);
} else
#endif /* HAVE_X11 */
{ /* Not using X11 */
xid = 0;
}
uid = factory->next_uid++;
object_path = mate_panel_applet_get_object_path (MATE_PANEL_APPLET (applet));
g_hash_table_insert (factory->applets, GUINT_TO_POINTER (uid), applet);
g_object_set_data (applet, "uid", GUINT_TO_POINTER (uid));
return_value = g_variant_new ("(obuu)",
object_path,
factory->out_of_process,
xid,
uid);
g_dbus_method_invocation_return_value (invocation, return_value);
}
static void
method_call_cb (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
MatePanelAppletFactory *factory = MATE_PANEL_APPLET_FACTORY (user_data);
if (g_strcmp0 (method_name, "GetApplet") == 0)
mate_panel_applet_factory_get_applet (factory, connection,
parameters, invocation);
}
static const gchar introspection_xml[] =
"<node>"
"<interface name='org.mate.panel.applet.AppletFactory'>"
"<method name='GetApplet'>"
"<arg name='applet_id' type='s' direction='in'/>"
"<arg name='screen' type='i' direction='in'/>"
"<arg name='props' type='a{sv}' direction='in'/>"
"<arg name='applet' type='o' direction='out'/>"
"<arg name='out-of-process' type='b' direction='out'/>"
"<arg name='xid' type='u' direction='out'/>"
"<arg name='uid' type='u' direction='out'/>"
"</method>"
"</interface>"
"</node>";
static const GDBusInterfaceVTable interface_vtable = {
method_call_cb,
NULL,
NULL,
{ 0 }
};
static GDBusNodeInfo *introspection_data = NULL;
static void
on_bus_acquired (GDBusConnection *connection,
const gchar *name,
MatePanelAppletFactory *factory)
{
gchar *object_path;
GError *error = NULL;
if (!introspection_data)
introspection_data
= g_dbus_node_info_new_for_xml (introspection_xml, NULL);
object_path = g_strdup_printf (MATE_PANEL_APPLET_FACTORY_OBJECT_PATH,
factory->factory_id);
factory->connection = connection;
factory->registration_id
= g_dbus_connection_register_object (connection,
object_path,
introspection_data->interfaces[0],
&interface_vtable,
factory, NULL,
&error);
if (error) {
g_printerr ("Failed to register object %s: %s\n",
object_path, error->message);
g_error_free (error);
}
g_free (object_path);
}
static void
on_name_lost (GDBusConnection *connection,
const gchar *name,
MatePanelAppletFactory *factory)
{
g_object_unref (factory);
}
gboolean
mate_panel_applet_factory_register_service (MatePanelAppletFactory *factory)
{
gchar *service_name;
if (!factory)
return FALSE;
service_name = g_strdup_printf (MATE_PANEL_APPLET_FACTORY_SERVICE_NAME,
factory->factory_id);
factory->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
service_name,
G_BUS_NAME_OWNER_FLAGS_NONE,
(GBusAcquiredCallback) on_bus_acquired,
NULL,
(GBusNameLostCallback) on_name_lost,
factory, NULL);
g_free (service_name);
return TRUE;
}
GtkWidget *
mate_panel_applet_factory_get_applet_widget (const gchar *id,
guint uid)
{
MatePanelAppletFactory *factory;
GObject *object;
if (!factories)
return NULL;
factory = g_hash_table_lookup (factories, id);
if (!factory)
return NULL;
object = g_hash_table_lookup (factory->applets, GUINT_TO_POINTER (uid));
if (!object || !GTK_IS_WIDGET (object))
return NULL;
return GTK_WIDGET (object);
}
|