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
=== modified file 'configure.ac'
--- configure.ac 2014-12-25 16:27:26 +0000
+++ configure.ac 2014-12-26 19:18:18 +0000
@@ -54,7 +54,6 @@
54PKG_CHECK_MODULES(GXPS, libgxps)54PKG_CHECK_MODULES(GXPS, libgxps)
55PKG_CHECK_MODULES(FREETYPE,freetype2)55PKG_CHECK_MODULES(FREETYPE,freetype2)
56PKG_CHECK_MODULES(GLIB, glib-2.0)56PKG_CHECK_MODULES(GLIB, glib-2.0)
57PKG_CHECK_MODULES(DBUS, dbus-glib-1)
58PKG_CHECK_MODULES(X11, x11)57PKG_CHECK_MODULES(X11, x11)
5958
60PKG_CHECK_MODULES(GMODULE, gmodule-2.0) # To compile XCF loader59PKG_CHECK_MODULES(GMODULE, gmodule-2.0) # To compile XCF loader
6160
=== modified file 'src/Makefile.am'
--- src/Makefile.am 2014-12-25 16:27:26 +0000
+++ src/Makefile.am 2014-12-26 19:18:18 +0000
@@ -26,7 +26,6 @@
26 $(PTHREAD_CFLAGS) \26 $(PTHREAD_CFLAGS) \
27 $(GTKSOURCEVIEW_CFLAGS) \27 $(GTKSOURCEVIEW_CFLAGS) \
28 $(GLIB_CFLAGS) \28 $(GLIB_CFLAGS) \
29 $(DBUS_CFLAGS) \
30 $(X11_CFLAGS) \29 $(X11_CFLAGS) \
31 $(BOOST_CPPFLAGS)30 $(BOOST_CPPFLAGS)
3231
@@ -36,7 +35,6 @@
36 $(GTK_LIBS) \35 $(GTK_LIBS) \
37 $(GTKSOURCEVIEW_LIBS) \36 $(GTKSOURCEVIEW_LIBS) \
38 $(GLIB_LIBS) \37 $(GLIB_LIBS) \
39 $(DBUS_LIBS) \
40 $(X11_LIBS) \38 $(X11_LIBS) \
41 -ldl39 -ldl
4240
4341
=== modified file 'src/gloobus-preview-interface-media.cpp'
--- src/gloobus-preview-interface-media.cpp 2012-04-06 10:59:53 +0000
+++ src/gloobus-preview-interface-media.cpp 2014-12-26 19:18:18 +0000
@@ -27,17 +27,6 @@
2727
28#include "gloobus-preview-interface-media.h"28#include "gloobus-preview-interface-media.h"
2929
30// GNOME MEDIA KEYS HANDLING ================================================ //
31#include <dbus/dbus-glib.h>
32
33static void dbus_proxy_destroy_cb (DBusGProxy *proxy, gpointer data);
34static void media_key_pressed_cb (DBusGProxy *proxy, const gchar *application, const gchar *key, gpointer data);
35static void media_marshal_VOID__STRING_STRING (GClosure *closure,
36 GValue *return_value G_GNUC_UNUSED,
37 guint n_param_values,
38 const GValue *param_values,
39 gpointer invocation_hint G_GNUC_UNUSED,
40 gpointer marshal_data);
41// ========================================================================== //30// ========================================================================== //
4231
43iMedia::iMedia( void )32iMedia::iMedia( void )
@@ -47,8 +36,11 @@
47 ,timeout_id(0)36 ,timeout_id(0)
48 ,f_visible(false)37 ,f_visible(false)
49 ,duration(0)38 ,duration(0)
50 ,dbus_proxy(0)39 ,proxy(0)
51 ,focus_in_id(0)40 ,handler_id(0)
41 ,watch_id(0)
42 ,cancellable_init(0)
43 ,cancellable(0)
52{44{
53 g_debug("Creating iMedia");45 g_debug("Creating iMedia");
54 activate_media_keys(); //TODO: is it the best place to do it?46 activate_media_keys(); //TODO: is it the best place to do it?
@@ -306,139 +298,197 @@
306 ((iMedia*)data)->f_visible = false;298 ((iMedia*)data)->f_visible = false;
307}299}
308300
301static void on_media_player_key_pressed (gpointer data,
302 const gchar *key)
303{
304 iMedia *media = (iMedia*)data;
305
306 if (g_strcmp0 ("Play", key) == 0)
307 media->play_pause();
308 else if (g_strcmp0 ("Previous", key) == 0)
309 media->previous();
310 else if (g_strcmp0 ("Next", key) == 0)
311 media->next();
312 else if (g_strcmp0 ("Stop", key) == 0)
313 media->stop();
314}
315
316static void grab_media_player_keys_cb (GDBusProxy *proxy,
317 GAsyncResult *res,
318 gpointer data)
319{
320 iMedia *media = (iMedia*)data;
321 GVariant *variant;
322 GError *error = NULL;
323
324 variant = g_dbus_proxy_call_finish (proxy, res, &error);
325 media->cancellable = NULL;
326
327 if (variant == NULL) {
328 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
329 g_warning ("Failed to call \"GrabMediaPlayerKeys\": %s", error->message);
330 g_error_free (error);
331 return;
332 }
333 g_variant_unref (variant);
334}
335
336static void grab_media_player_keys (gpointer data)
337{
338 iMedia *media = (iMedia*)data;
339 GCancellable *cancellable;
340
341 if (media->proxy == NULL)
342 return;
343
344 /* Only allow one key grab operation to happen concurrently */
345 if (media->cancellable) {
346 g_cancellable_cancel (media->cancellable);
347 }
348
349 cancellable = g_cancellable_new ();
350 media->cancellable = cancellable;
351
352 g_dbus_proxy_call (media->proxy,
353 "GrabMediaPlayerKeys",
354 g_variant_new ("(su)", "Gloobus-Preview", 0),
355 G_DBUS_CALL_FLAGS_NONE,
356 -1, cancellable,
357 (GAsyncReadyCallback) grab_media_player_keys_cb,
358 data);
359
360 /* GDBus keeps a reference throughout the async call */
361 g_object_unref (cancellable);
362}
363
364static gboolean on_window_focus_in_event (GtkWidget *window,
365 GdkEventFocus *event,
366 gpointer data)
367{
368 grab_media_player_keys (data);
369
370 return FALSE;
371}
372
373static void key_pressed (GDBusProxy *proxy,
374 gchar *sender_name,
375 gchar *signal_name,
376 GVariant *parameters,
377 gpointer data)
378{
379 char *app, *cmd;
380
381 if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
382 return;
383 g_variant_get (parameters, "(ss)", &app, &cmd);
384 if (g_strcmp0 (app, "Gloobus-Preview") == 0) {
385 on_media_player_key_pressed (data, cmd);
386 }
387 g_free (app);
388 g_free (cmd);
389}
390
391static void got_proxy_cb (GObject *source_object,
392 GAsyncResult *res,
393 gpointer data)
394{
395 iMedia *media = (iMedia*)data;
396 GError *error = NULL;
397
398 media->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
399 media->cancellable_init = NULL;
400
401 if (media->proxy == NULL) {
402 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
403 g_warning ("Failed to contact settings daemon: %s", error->message);
404 g_error_free (error);
405 return;
406 }
407
408 grab_media_player_keys (data);
409
410 g_signal_connect (G_OBJECT (media->proxy), "g-signal",
411 G_CALLBACK (key_pressed), data);
412}
413
414static void name_appeared_cb (GDBusConnection *connection,
415 const gchar *name,
416 const gchar *name_owner,
417 gpointer data)
418{
419 iMedia *media = (iMedia*)data;
420 GCancellable *cancellable;
421
422 cancellable = g_cancellable_new ();
423 media->cancellable_init = cancellable;
424
425 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
426 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
427 NULL,
428 "org.gnome.SettingsDaemon",
429 "/org/gnome/SettingsDaemon/MediaKeys",
430 "org.gnome.SettingsDaemon.MediaKeys",
431 cancellable,
432 (GAsyncReadyCallback) got_proxy_cb,
433 data);
434
435 /* GDBus keeps a reference throughout the async call */
436 g_object_unref (cancellable);
437}
438
439static void name_vanished_cb (GDBusConnection *connection,
440 const gchar *name,
441 gpointer data)
442{
443 iMedia *media = (iMedia*)data;
444
445 if (media->proxy != NULL) {
446 g_object_unref (media->proxy);
447 media->proxy = NULL;
448 }
449
450 if (media->cancellable) {
451 g_cancellable_cancel (media->cancellable);
452 }
453}
454
309void iMedia::activate_media_keys( void )455void iMedia::activate_media_keys( void )
310{456{
311 DBusGConnection *connection;457 watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
312 GError *err = NULL;458 "org.gnome.SettingsDaemon",
313 DBusGProxy *proxy;459 G_BUS_NAME_WATCHER_FLAGS_NONE,
314460 (GBusNameAppearedCallback) name_appeared_cb,
315 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &err);461 (GBusNameVanishedCallback) name_vanished_cb,
316 if (connection == NULL) {462 this, NULL);
317 g_warning ("Error connecting to D-Bus: %s", err->message);463
318 return;464 handler_id = g_signal_connect (G_OBJECT (ui->get_window()), "focus-in-event",
319 }465 G_CALLBACK (on_window_focus_in_event), this);
320
321 /* Try the gnome-settings-daemon version,
322 * then the gnome-control-center version of things */
323 proxy = dbus_g_proxy_new_for_name_owner (connection,
324 "org.gnome.SettingsDaemon",
325 "/org/gnome/SettingsDaemon/MediaKeys",
326 "org.gnome.SettingsDaemon.MediaKeys", NULL);
327 if (proxy == NULL) {
328 proxy = dbus_g_proxy_new_for_name_owner (connection,
329 "org.gnome.SettingsDaemon",
330 "/org/gnome/SettingsDaemon",
331 "org.gnome.SettingsDaemon", &err);
332 }
333
334 dbus_g_connection_unref (connection);
335
336 if (err != NULL) {
337 g_warning ("Failed to create dbus proxy for org.gnome.SettingsDaemon: %s", err->message);
338 g_error_free (err);
339 } else {
340 dbus_proxy = proxy;
341 //g_signal_connect_object (proxy, "destroy", G_CALLBACK (dbus_proxy_destroy_cb), &dbus_proxy, GConnectFlags(0));
342 g_signal_connect(G_OBJECT(proxy), "destroy", G_CALLBACK (dbus_proxy_destroy_cb), &dbus_proxy);
343 }
344
345 dbus_g_proxy_call (proxy, "GrabMediaPlayerKeys", NULL,
346 G_TYPE_STRING, "Gloobus-Preview", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_INVALID);
347
348 dbus_g_object_register_marshaller (media_marshal_VOID__STRING_STRING,
349 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
350 dbus_g_proxy_add_signal (proxy, "MediaPlayerKeyPressed",
351 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
352 dbus_g_proxy_connect_signal (proxy, "MediaPlayerKeyPressed",
353 G_CALLBACK (media_key_pressed_cb), this, NULL);
354
355 focus_in_id = g_signal_connect (G_OBJECT ( ui->get_window() ),
356 "focus-in-event", G_CALLBACK (focus_in_cb), this);
357
358 dbus_proxy = proxy;
359}466}
360467
361void iMedia::deactivate_media_keys( void )468void iMedia::deactivate_media_keys( void )
362{469{
363 if (dbus_proxy != NULL) {470 if (cancellable_init) {
364 DBusGProxy *proxy = (DBusGProxy*)dbus_proxy;471 g_cancellable_cancel (cancellable_init);
365 dbus_g_proxy_call (proxy, "ReleaseMediaPlayerKeys", NULL,472 }
366 G_TYPE_STRING, "Gloobus-Preview", G_TYPE_INVALID, G_TYPE_INVALID);473
474 if (cancellable) {
475 g_cancellable_cancel (cancellable);
476 }
477
478 if (proxy != NULL) {
367 g_object_unref (proxy);479 g_object_unref (proxy);
368 dbus_proxy = NULL;480 proxy = NULL;
369 }481 }
370482
371 if (focus_in_id) {483 if (handler_id != 0) {
372 g_signal_handler_disconnect (G_OBJECT (ui->get_window()), focus_in_id);484 g_signal_handler_disconnect (G_OBJECT (ui->get_window()), handler_id);
373 }485 handler_id = 0;
374}486 }
375487
376static void488 if (watch_id != 0) {
377dbus_proxy_destroy_cb (DBusGProxy *proxy, gpointer data)489 g_bus_unwatch_name (watch_id);
378{490 watch_id = 0;
379 DBusGProxy **dbus_proxy = (DBusGProxy**)data;491 }
380 *dbus_proxy = 0;
381}
382
383gboolean iMedia::
384focus_in_cb( GtkWidget *window, GdkEventFocus *event, gpointer data )
385{
386 iMedia *media = (iMedia*)data;
387 if (media->dbus_proxy) {
388 DBusGProxy *proxy = (DBusGProxy*)media->dbus_proxy;
389 dbus_g_proxy_call (proxy, "GrabMediaPlayerKeys", NULL,
390 G_TYPE_STRING, "Gloobus-Preview", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_INVALID);
391 }
392 return false;
393}
394
395static void
396media_key_pressed_cb (DBusGProxy *proxy,
397 const gchar *application,
398 const gchar *key,
399 gpointer data)
400{
401 iMedia *media = (iMedia*)data;
402 if (g_strcmp0 ("Gloobus-Preview", application) == 0) {
403 if (g_strcmp0 ("Play", key) == 0)
404 media->play_pause();
405 else if (g_strcmp0 ("Previous", key) == 0)
406 media->previous();
407 else if (g_strcmp0 ("Next", key) == 0)
408 media->next();
409 else if (g_strcmp0 ("Stop", key) == 0)
410 media->stop();
411 }
412}
413
414static void
415media_marshal_VOID__STRING_STRING (GClosure *closure,
416 GValue *return_value G_GNUC_UNUSED,
417 guint n_param_values,
418 const GValue *param_values,
419 gpointer invocation_hint G_GNUC_UNUSED,
420 gpointer marshal_data)
421{
422 typedef void (*GMarshalFunc_VOID__STRING_STRING) (gpointer data1,
423 gpointer arg_1, gpointer arg_2, gpointer data2);
424
425 register GMarshalFunc_VOID__STRING_STRING callback;
426 register GCClosure *cc = (GCClosure*) closure;
427 register gpointer data1, data2;
428
429 g_return_if_fail (n_param_values == 3);
430
431 if (G_CCLOSURE_SWAP_DATA (closure)) {
432 data1 = closure->data;
433 data2 = param_values->data[0].v_pointer;
434 } else {
435 data1 = param_values->data[0].v_pointer;
436 data2 = closure->data;
437 }
438 callback = (GMarshalFunc_VOID__STRING_STRING) (marshal_data ? marshal_data : cc->callback);
439
440 callback (data1, (param_values+1)->data[0].v_pointer,
441 (param_values+2)->data[0].v_pointer, data2);
442}492}
443493
444void iMedia::next( void )494void iMedia::next( void )
445495
=== modified file 'src/gloobus-preview-interface-media.h'
--- src/gloobus-preview-interface-media.h 2010-08-25 15:08:27 +0000
+++ src/gloobus-preview-interface-media.h 2014-12-26 19:18:18 +0000
@@ -48,9 +48,13 @@
48 bool f_visible;48 bool f_visible;
49 gint duration;49 gint duration;
5050
51 gpointer dbus_proxy;51 public:
52 guint focus_in_id;52 GDBusProxy * proxy;
53 53 guint handler_id;
54 guint watch_id;
55 GCancellable * cancellable_init;
56 GCancellable * cancellable;
57
54 public:58 public:
55 iMedia ( void );59 iMedia ( void );
56 virtual ~iMedia ( void );60 virtual ~iMedia ( void );
@@ -79,7 +83,6 @@
79 virtual void update_duration ( gint sec );83 virtual void update_duration ( gint sec );
80 void activate_media_keys ( void );84 void activate_media_keys ( void );
81 void deactivate_media_keys ( void );85 void deactivate_media_keys ( void );
82 static gboolean focus_in_cb ( GtkWidget *window, GdkEventFocus *event, gpointer data );
83 static gboolean next_file_cb ( gpointer );86 static gboolean next_file_cb ( gpointer );
84 static gboolean previous_file_cb ( gpointer );87 static gboolean previous_file_cb ( gpointer );
85};88};

Subscribers

People subscribed via source and target branches