Merge lp:~ballogy/gloobus-preview/gdbus-port into lp:gloobus-preview

Proposed by Balló György
Status: Merged
Merged at revision: 308
Proposed branch: lp:~ballogy/gloobus-preview/gdbus-port
Merge into: lp:gloobus-preview
Diff against target: 420 lines (+196/-146)
4 files modified
configure.ac (+0/-1)
src/Makefile.am (+0/-2)
src/gloobus-preview-interface-media.cpp (+189/-139)
src/gloobus-preview-interface-media.h (+7/-4)
To merge this branch: bzr merge lp:~ballogy/gloobus-preview/gdbus-port
Reviewer Review Type Date Requested Status
Gloobus Developers Pending
Review via email: mp+245386@code.launchpad.net

Description of the change

Port media keys handling to GDBus

This removes the dbus-glib dependency.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'configure.ac'
2--- configure.ac 2014-12-25 16:27:26 +0000
3+++ configure.ac 2014-12-26 19:18:18 +0000
4@@ -54,7 +54,6 @@
5 PKG_CHECK_MODULES(GXPS, libgxps)
6 PKG_CHECK_MODULES(FREETYPE,freetype2)
7 PKG_CHECK_MODULES(GLIB, glib-2.0)
8-PKG_CHECK_MODULES(DBUS, dbus-glib-1)
9 PKG_CHECK_MODULES(X11, x11)
10
11 PKG_CHECK_MODULES(GMODULE, gmodule-2.0) # To compile XCF loader
12
13=== modified file 'src/Makefile.am'
14--- src/Makefile.am 2014-12-25 16:27:26 +0000
15+++ src/Makefile.am 2014-12-26 19:18:18 +0000
16@@ -26,7 +26,6 @@
17 $(PTHREAD_CFLAGS) \
18 $(GTKSOURCEVIEW_CFLAGS) \
19 $(GLIB_CFLAGS) \
20- $(DBUS_CFLAGS) \
21 $(X11_CFLAGS) \
22 $(BOOST_CPPFLAGS)
23
24@@ -36,7 +35,6 @@
25 $(GTK_LIBS) \
26 $(GTKSOURCEVIEW_LIBS) \
27 $(GLIB_LIBS) \
28- $(DBUS_LIBS) \
29 $(X11_LIBS) \
30 -ldl
31
32
33=== modified file 'src/gloobus-preview-interface-media.cpp'
34--- src/gloobus-preview-interface-media.cpp 2012-04-06 10:59:53 +0000
35+++ src/gloobus-preview-interface-media.cpp 2014-12-26 19:18:18 +0000
36@@ -27,17 +27,6 @@
37
38 #include "gloobus-preview-interface-media.h"
39
40-// GNOME MEDIA KEYS HANDLING ================================================ //
41-#include <dbus/dbus-glib.h>
42-
43-static void dbus_proxy_destroy_cb (DBusGProxy *proxy, gpointer data);
44-static void media_key_pressed_cb (DBusGProxy *proxy, const gchar *application, const gchar *key, gpointer data);
45-static void media_marshal_VOID__STRING_STRING (GClosure *closure,
46- GValue *return_value G_GNUC_UNUSED,
47- guint n_param_values,
48- const GValue *param_values,
49- gpointer invocation_hint G_GNUC_UNUSED,
50- gpointer marshal_data);
51 // ========================================================================== //
52
53 iMedia::iMedia( void )
54@@ -47,8 +36,11 @@
55 ,timeout_id(0)
56 ,f_visible(false)
57 ,duration(0)
58- ,dbus_proxy(0)
59- ,focus_in_id(0)
60+ ,proxy(0)
61+ ,handler_id(0)
62+ ,watch_id(0)
63+ ,cancellable_init(0)
64+ ,cancellable(0)
65 {
66 g_debug("Creating iMedia");
67 activate_media_keys(); //TODO: is it the best place to do it?
68@@ -306,139 +298,197 @@
69 ((iMedia*)data)->f_visible = false;
70 }
71
72+static void on_media_player_key_pressed (gpointer data,
73+ const gchar *key)
74+{
75+ iMedia *media = (iMedia*)data;
76+
77+ if (g_strcmp0 ("Play", key) == 0)
78+ media->play_pause();
79+ else if (g_strcmp0 ("Previous", key) == 0)
80+ media->previous();
81+ else if (g_strcmp0 ("Next", key) == 0)
82+ media->next();
83+ else if (g_strcmp0 ("Stop", key) == 0)
84+ media->stop();
85+}
86+
87+static void grab_media_player_keys_cb (GDBusProxy *proxy,
88+ GAsyncResult *res,
89+ gpointer data)
90+{
91+ iMedia *media = (iMedia*)data;
92+ GVariant *variant;
93+ GError *error = NULL;
94+
95+ variant = g_dbus_proxy_call_finish (proxy, res, &error);
96+ media->cancellable = NULL;
97+
98+ if (variant == NULL) {
99+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
100+ g_warning ("Failed to call \"GrabMediaPlayerKeys\": %s", error->message);
101+ g_error_free (error);
102+ return;
103+ }
104+ g_variant_unref (variant);
105+}
106+
107+static void grab_media_player_keys (gpointer data)
108+{
109+ iMedia *media = (iMedia*)data;
110+ GCancellable *cancellable;
111+
112+ if (media->proxy == NULL)
113+ return;
114+
115+ /* Only allow one key grab operation to happen concurrently */
116+ if (media->cancellable) {
117+ g_cancellable_cancel (media->cancellable);
118+ }
119+
120+ cancellable = g_cancellable_new ();
121+ media->cancellable = cancellable;
122+
123+ g_dbus_proxy_call (media->proxy,
124+ "GrabMediaPlayerKeys",
125+ g_variant_new ("(su)", "Gloobus-Preview", 0),
126+ G_DBUS_CALL_FLAGS_NONE,
127+ -1, cancellable,
128+ (GAsyncReadyCallback) grab_media_player_keys_cb,
129+ data);
130+
131+ /* GDBus keeps a reference throughout the async call */
132+ g_object_unref (cancellable);
133+}
134+
135+static gboolean on_window_focus_in_event (GtkWidget *window,
136+ GdkEventFocus *event,
137+ gpointer data)
138+{
139+ grab_media_player_keys (data);
140+
141+ return FALSE;
142+}
143+
144+static void key_pressed (GDBusProxy *proxy,
145+ gchar *sender_name,
146+ gchar *signal_name,
147+ GVariant *parameters,
148+ gpointer data)
149+{
150+ char *app, *cmd;
151+
152+ if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
153+ return;
154+ g_variant_get (parameters, "(ss)", &app, &cmd);
155+ if (g_strcmp0 (app, "Gloobus-Preview") == 0) {
156+ on_media_player_key_pressed (data, cmd);
157+ }
158+ g_free (app);
159+ g_free (cmd);
160+}
161+
162+static void got_proxy_cb (GObject *source_object,
163+ GAsyncResult *res,
164+ gpointer data)
165+{
166+ iMedia *media = (iMedia*)data;
167+ GError *error = NULL;
168+
169+ media->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
170+ media->cancellable_init = NULL;
171+
172+ if (media->proxy == NULL) {
173+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
174+ g_warning ("Failed to contact settings daemon: %s", error->message);
175+ g_error_free (error);
176+ return;
177+ }
178+
179+ grab_media_player_keys (data);
180+
181+ g_signal_connect (G_OBJECT (media->proxy), "g-signal",
182+ G_CALLBACK (key_pressed), data);
183+}
184+
185+static void name_appeared_cb (GDBusConnection *connection,
186+ const gchar *name,
187+ const gchar *name_owner,
188+ gpointer data)
189+{
190+ iMedia *media = (iMedia*)data;
191+ GCancellable *cancellable;
192+
193+ cancellable = g_cancellable_new ();
194+ media->cancellable_init = cancellable;
195+
196+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
197+ G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
198+ NULL,
199+ "org.gnome.SettingsDaemon",
200+ "/org/gnome/SettingsDaemon/MediaKeys",
201+ "org.gnome.SettingsDaemon.MediaKeys",
202+ cancellable,
203+ (GAsyncReadyCallback) got_proxy_cb,
204+ data);
205+
206+ /* GDBus keeps a reference throughout the async call */
207+ g_object_unref (cancellable);
208+}
209+
210+static void name_vanished_cb (GDBusConnection *connection,
211+ const gchar *name,
212+ gpointer data)
213+{
214+ iMedia *media = (iMedia*)data;
215+
216+ if (media->proxy != NULL) {
217+ g_object_unref (media->proxy);
218+ media->proxy = NULL;
219+ }
220+
221+ if (media->cancellable) {
222+ g_cancellable_cancel (media->cancellable);
223+ }
224+}
225+
226 void iMedia::activate_media_keys( void )
227 {
228- DBusGConnection *connection;
229- GError *err = NULL;
230- DBusGProxy *proxy;
231-
232- connection = dbus_g_bus_get (DBUS_BUS_SESSION, &err);
233- if (connection == NULL) {
234- g_warning ("Error connecting to D-Bus: %s", err->message);
235- return;
236- }
237-
238- /* Try the gnome-settings-daemon version,
239- * then the gnome-control-center version of things */
240- proxy = dbus_g_proxy_new_for_name_owner (connection,
241- "org.gnome.SettingsDaemon",
242- "/org/gnome/SettingsDaemon/MediaKeys",
243- "org.gnome.SettingsDaemon.MediaKeys", NULL);
244- if (proxy == NULL) {
245- proxy = dbus_g_proxy_new_for_name_owner (connection,
246- "org.gnome.SettingsDaemon",
247- "/org/gnome/SettingsDaemon",
248- "org.gnome.SettingsDaemon", &err);
249- }
250-
251- dbus_g_connection_unref (connection);
252-
253- if (err != NULL) {
254- g_warning ("Failed to create dbus proxy for org.gnome.SettingsDaemon: %s", err->message);
255- g_error_free (err);
256- } else {
257- dbus_proxy = proxy;
258- //g_signal_connect_object (proxy, "destroy", G_CALLBACK (dbus_proxy_destroy_cb), &dbus_proxy, GConnectFlags(0));
259- g_signal_connect(G_OBJECT(proxy), "destroy", G_CALLBACK (dbus_proxy_destroy_cb), &dbus_proxy);
260- }
261-
262- dbus_g_proxy_call (proxy, "GrabMediaPlayerKeys", NULL,
263- G_TYPE_STRING, "Gloobus-Preview", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_INVALID);
264-
265- dbus_g_object_register_marshaller (media_marshal_VOID__STRING_STRING,
266- G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
267- dbus_g_proxy_add_signal (proxy, "MediaPlayerKeyPressed",
268- G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
269- dbus_g_proxy_connect_signal (proxy, "MediaPlayerKeyPressed",
270- G_CALLBACK (media_key_pressed_cb), this, NULL);
271-
272- focus_in_id = g_signal_connect (G_OBJECT ( ui->get_window() ),
273- "focus-in-event", G_CALLBACK (focus_in_cb), this);
274-
275- dbus_proxy = proxy;
276+ watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
277+ "org.gnome.SettingsDaemon",
278+ G_BUS_NAME_WATCHER_FLAGS_NONE,
279+ (GBusNameAppearedCallback) name_appeared_cb,
280+ (GBusNameVanishedCallback) name_vanished_cb,
281+ this, NULL);
282+
283+ handler_id = g_signal_connect (G_OBJECT (ui->get_window()), "focus-in-event",
284+ G_CALLBACK (on_window_focus_in_event), this);
285 }
286
287 void iMedia::deactivate_media_keys( void )
288 {
289- if (dbus_proxy != NULL) {
290- DBusGProxy *proxy = (DBusGProxy*)dbus_proxy;
291- dbus_g_proxy_call (proxy, "ReleaseMediaPlayerKeys", NULL,
292- G_TYPE_STRING, "Gloobus-Preview", G_TYPE_INVALID, G_TYPE_INVALID);
293+ if (cancellable_init) {
294+ g_cancellable_cancel (cancellable_init);
295+ }
296+
297+ if (cancellable) {
298+ g_cancellable_cancel (cancellable);
299+ }
300+
301+ if (proxy != NULL) {
302 g_object_unref (proxy);
303- dbus_proxy = NULL;
304- }
305-
306- if (focus_in_id) {
307- g_signal_handler_disconnect (G_OBJECT (ui->get_window()), focus_in_id);
308- }
309-}
310-
311-static void
312-dbus_proxy_destroy_cb (DBusGProxy *proxy, gpointer data)
313-{
314- DBusGProxy **dbus_proxy = (DBusGProxy**)data;
315- *dbus_proxy = 0;
316-}
317-
318-gboolean iMedia::
319-focus_in_cb( GtkWidget *window, GdkEventFocus *event, gpointer data )
320-{
321- iMedia *media = (iMedia*)data;
322- if (media->dbus_proxy) {
323- DBusGProxy *proxy = (DBusGProxy*)media->dbus_proxy;
324- dbus_g_proxy_call (proxy, "GrabMediaPlayerKeys", NULL,
325- G_TYPE_STRING, "Gloobus-Preview", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_INVALID);
326- }
327- return false;
328-}
329-
330-static void
331-media_key_pressed_cb (DBusGProxy *proxy,
332- const gchar *application,
333- const gchar *key,
334- gpointer data)
335-{
336- iMedia *media = (iMedia*)data;
337- if (g_strcmp0 ("Gloobus-Preview", application) == 0) {
338- if (g_strcmp0 ("Play", key) == 0)
339- media->play_pause();
340- else if (g_strcmp0 ("Previous", key) == 0)
341- media->previous();
342- else if (g_strcmp0 ("Next", key) == 0)
343- media->next();
344- else if (g_strcmp0 ("Stop", key) == 0)
345- media->stop();
346- }
347-}
348-
349-static void
350-media_marshal_VOID__STRING_STRING (GClosure *closure,
351- GValue *return_value G_GNUC_UNUSED,
352- guint n_param_values,
353- const GValue *param_values,
354- gpointer invocation_hint G_GNUC_UNUSED,
355- gpointer marshal_data)
356-{
357- typedef void (*GMarshalFunc_VOID__STRING_STRING) (gpointer data1,
358- gpointer arg_1, gpointer arg_2, gpointer data2);
359-
360- register GMarshalFunc_VOID__STRING_STRING callback;
361- register GCClosure *cc = (GCClosure*) closure;
362- register gpointer data1, data2;
363-
364- g_return_if_fail (n_param_values == 3);
365-
366- if (G_CCLOSURE_SWAP_DATA (closure)) {
367- data1 = closure->data;
368- data2 = param_values->data[0].v_pointer;
369- } else {
370- data1 = param_values->data[0].v_pointer;
371- data2 = closure->data;
372- }
373- callback = (GMarshalFunc_VOID__STRING_STRING) (marshal_data ? marshal_data : cc->callback);
374-
375- callback (data1, (param_values+1)->data[0].v_pointer,
376- (param_values+2)->data[0].v_pointer, data2);
377+ proxy = NULL;
378+ }
379+
380+ if (handler_id != 0) {
381+ g_signal_handler_disconnect (G_OBJECT (ui->get_window()), handler_id);
382+ handler_id = 0;
383+ }
384+
385+ if (watch_id != 0) {
386+ g_bus_unwatch_name (watch_id);
387+ watch_id = 0;
388+ }
389 }
390
391 void iMedia::next( void )
392
393=== modified file 'src/gloobus-preview-interface-media.h'
394--- src/gloobus-preview-interface-media.h 2010-08-25 15:08:27 +0000
395+++ src/gloobus-preview-interface-media.h 2014-12-26 19:18:18 +0000
396@@ -48,9 +48,13 @@
397 bool f_visible;
398 gint duration;
399
400- gpointer dbus_proxy;
401- guint focus_in_id;
402-
403+ public:
404+ GDBusProxy * proxy;
405+ guint handler_id;
406+ guint watch_id;
407+ GCancellable * cancellable_init;
408+ GCancellable * cancellable;
409+
410 public:
411 iMedia ( void );
412 virtual ~iMedia ( void );
413@@ -79,7 +83,6 @@
414 virtual void update_duration ( gint sec );
415 void activate_media_keys ( void );
416 void deactivate_media_keys ( void );
417- static gboolean focus_in_cb ( GtkWidget *window, GdkEventFocus *event, gpointer data );
418 static gboolean next_file_cb ( gpointer );
419 static gboolean previous_file_cb ( gpointer );
420 };

Subscribers

People subscribed via source and target branches