0e28707
diff -uNrp a/plugins/media-keys/bus-watch-namespace.c b/plugins/media-keys/bus-watch-namespace.c
0e28707
--- a/plugins/media-keys/bus-watch-namespace.c	1970-01-01 01:00:00.000000000 +0100
0e28707
+++ b/plugins/media-keys/bus-watch-namespace.c	2013-11-04 10:41:10.925022405 +0000
0e28707
@@ -0,0 +1,347 @@
0e28707
+/*
0e28707
+ * Copyright 2013 Canonical Ltd.
0e28707
+ *
0e28707
+ * This program is free software; you can redistribute it and/or modify
0e28707
+ * it under the terms of the GNU Lesser General Public License as
0e28707
+ * published by the Free Software Foundation; either version 2 of the
0e28707
+ * License, or (at your option) any later version.
0e28707
+ *
0e28707
+ * This program is distributed in the hope that it will be useful, but
0e28707
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
0e28707
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0e28707
+ * Lesser General Public License for more details.
0e28707
+ *
0e28707
+ * You should have received a copy of the GNU Lesser General Public License
0e28707
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0e28707
+ *
0e28707
+ * Author: Lars Uebernickel <lars.uebernickel@canonical.com>
0e28707
+ */
0e28707
+
0e28707
+#include <gio/gio.h>
0e28707
+#include <string.h>
0e28707
+#include "bus-watch-namespace.h"
0e28707
+
0e28707
+typedef struct
0e28707
+{
0e28707
+  guint                     id;
0e28707
+  gchar                    *name_space;
0e28707
+  GBusNameAppearedCallback  appeared_handler;
0e28707
+  GBusNameVanishedCallback  vanished_handler;
0e28707
+  gpointer                  user_data;
0e28707
+  GDestroyNotify            user_data_destroy;
0e28707
+
0e28707
+  GDBusConnection          *connection;
0e28707
+  GCancellable             *cancellable;
0e28707
+  GHashTable               *names;
0e28707
+  guint                     subscription_id;
0e28707
+} NamespaceWatcher;
0e28707
+
0e28707
+typedef struct
0e28707
+{
0e28707
+  NamespaceWatcher *watcher;
0e28707
+  gchar            *name;
0e28707
+} GetNameOwnerData;
0e28707
+
0e28707
+static guint namespace_watcher_next_id;
0e28707
+static GHashTable *namespace_watcher_watchers;
0e28707
+
0e28707
+static void
0e28707
+namespace_watcher_stop (gpointer data)
0e28707
+{
0e28707
+  NamespaceWatcher *watcher = data;
0e28707
+
0e28707
+  g_cancellable_cancel (watcher->cancellable);
0e28707
+  g_object_unref (watcher->cancellable);
0e28707
+
0e28707
+  if (watcher->subscription_id)
0e28707
+    g_dbus_connection_signal_unsubscribe (watcher->connection, watcher->subscription_id);
0e28707
+
0e28707
+  if (watcher->vanished_handler)
0e28707
+    {
0e28707
+      GHashTableIter it;
0e28707
+      const gchar *name;
0e28707
+
0e28707
+      g_hash_table_iter_init (&it, watcher->names);
0e28707
+      while (g_hash_table_iter_next (&it, (gpointer *) &name, NULL))
0e28707
+        watcher->vanished_handler (watcher->connection, name, watcher->user_data);
0e28707
+    }
0e28707
+
0e28707
+  if (watcher->user_data_destroy)
0e28707
+    watcher->user_data_destroy (watcher->user_data);
0e28707
+
0e28707
+  if (watcher->connection)
0e28707
+    {
0e28707
+      g_signal_handlers_disconnect_by_func (watcher->connection, namespace_watcher_stop, watcher);
0e28707
+      g_object_unref (watcher->connection);
0e28707
+    }
0e28707
+
0e28707
+  g_hash_table_unref (watcher->names);
0e28707
+
0e28707
+  g_hash_table_remove (namespace_watcher_watchers, GUINT_TO_POINTER (watcher->id));
0e28707
+  if (g_hash_table_size (namespace_watcher_watchers) == 0)
0e28707
+    g_clear_pointer (&namespace_watcher_watchers, g_hash_table_destroy);
0e28707
+
0e28707
+  g_free (watcher);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+namespace_watcher_name_appeared (NamespaceWatcher *watcher,
0e28707
+                                 const gchar      *name,
0e28707
+                                 const gchar      *owner)
0e28707
+{
0e28707
+  /* There's a race between NameOwnerChanged signals arriving and the
0e28707
+   * ListNames/GetNameOwner sequence returning, so this function might
0e28707
+   * be called more than once for the same name. To ensure that
0e28707
+   * appeared_handler is only called once for each name, it is only
0e28707
+   * called when inserting the name into watcher->names (each name is
0e28707
+   * only inserted once there).
0e28707
+   */
0e28707
+  if (g_hash_table_contains (watcher->names, name))
0e28707
+    return;
0e28707
+
0e28707
+  g_hash_table_add (watcher->names, g_strdup (name));
0e28707
+
0e28707
+  if (watcher->appeared_handler)
0e28707
+    watcher->appeared_handler (watcher->connection, name, owner, watcher->user_data);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+namespace_watcher_name_vanished (NamespaceWatcher *watcher,
0e28707
+                                 const gchar      *name)
0e28707
+{
0e28707
+  if (g_hash_table_remove (watcher->names, name) && watcher->vanished_handler)
0e28707
+    watcher->vanished_handler (watcher->connection, name, watcher->user_data);
0e28707
+}
0e28707
+
0e28707
+static gboolean
0e28707
+dbus_name_has_namespace (const gchar *name,
0e28707
+                         const gchar *name_space)
0e28707
+{
0e28707
+  gint len_name;
0e28707
+  gint len_namespace;
0e28707
+
0e28707
+  len_name = strlen (name);
0e28707
+  len_namespace = strlen (name_space);
0e28707
+
0e28707
+  if (len_name < len_namespace)
0e28707
+    return FALSE;
0e28707
+
0e28707
+  if (memcmp (name_space, name, len_namespace) != 0)
0e28707
+    return FALSE;
0e28707
+
0e28707
+  return len_namespace == len_name || name[len_namespace] == '.';
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+name_owner_changed (GDBusConnection *connection,
0e28707
+                    const gchar     *sender_name,
0e28707
+                    const gchar     *object_path,
0e28707
+                    const gchar     *interface_name,
0e28707
+                    const gchar     *signal_name,
0e28707
+                    GVariant        *parameters,
0e28707
+                    gpointer         user_data)
0e28707
+{
0e28707
+  NamespaceWatcher *watcher = user_data;
0e28707
+  const gchar *name;
0e28707
+  const gchar *old_owner;
0e28707
+  const gchar *new_owner;
0e28707
+
0e28707
+  g_variant_get (parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
0e28707
+
0e28707
+  if (old_owner[0] != '\0')
0e28707
+    namespace_watcher_name_vanished (watcher, name);
0e28707
+
0e28707
+  if (new_owner[0] != '\0')
0e28707
+    namespace_watcher_name_appeared (watcher, name, new_owner);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+got_name_owner (GObject      *object,
0e28707
+                GAsyncResult *result,
0e28707
+                gpointer      user_data)
0e28707
+{
0e28707
+  GetNameOwnerData *data = user_data;
0e28707
+  GError *error = NULL;
0e28707
+  GVariant *reply;
0e28707
+  const gchar *owner;
0e28707
+
0e28707
+  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
0e28707
+
0e28707
+  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
0e28707
+    {
0e28707
+      g_error_free (error);
0e28707
+      goto out;
0e28707
+    }
0e28707
+
0e28707
+  if (reply == NULL)
0e28707
+    {
0e28707
+      if (!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER))
0e28707
+        g_warning ("bus_watch_namespace: error calling org.freedesktop.DBus.GetNameOwner: %s", error->message);
0e28707
+      g_error_free (error);
0e28707
+      goto out;
0e28707
+    }
0e28707
+
0e28707
+  g_variant_get (reply, "(&s)", &owner);
0e28707
+  namespace_watcher_name_appeared (data->watcher, data->name, owner);
0e28707
+
0e28707
+  g_variant_unref (reply);
0e28707
+
0e28707
+out:
0e28707
+  g_free (data->name);
0e28707
+  g_slice_free (GetNameOwnerData, data);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+names_listed (GObject      *object,
0e28707
+              GAsyncResult *result,
0e28707
+              gpointer      user_data)
0e28707
+{
0e28707
+  NamespaceWatcher *watcher;
0e28707
+  GError *error = NULL;
0e28707
+  GVariant *reply;
0e28707
+  GVariantIter *iter;
0e28707
+  const gchar *name;
0e28707
+
0e28707
+  reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
0e28707
+
0e28707
+  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
0e28707
+    {
0e28707
+      g_error_free (error);
0e28707
+      return;
0e28707
+    }
0e28707
+
0e28707
+  watcher = user_data;
0e28707
+
0e28707
+  if (reply == NULL)
0e28707
+    {
0e28707
+      g_warning ("bus_watch_namespace: error calling org.freedesktop.DBus.ListNames: %s", error->message);
0e28707
+      g_error_free (error);
0e28707
+      return;
0e28707
+    }
0e28707
+
0e28707
+  g_variant_get (reply, "(as)", &iter);
0e28707
+  while (g_variant_iter_next (iter, "&s", &name))
0e28707
+    {
0e28707
+      if (dbus_name_has_namespace (name, watcher->name_space))
0e28707
+        {
0e28707
+          GetNameOwnerData *data = g_slice_new (GetNameOwnerData);
0e28707
+          data->watcher = watcher;
0e28707
+          data->name = g_strdup (name);
0e28707
+          g_dbus_connection_call (watcher->connection, "org.freedesktop.DBus", "/",
0e28707
+                                  "org.freedesktop.DBus", "GetNameOwner",
0e28707
+                                  g_variant_new ("(s)", name), G_VARIANT_TYPE ("(s)"),
0e28707
+                                  G_DBUS_CALL_FLAGS_NONE, -1, watcher->cancellable,
0e28707
+                                  got_name_owner, data);
0e28707
+        }
0e28707
+    }
0e28707
+
0e28707
+  g_variant_iter_free (iter);
0e28707
+  g_variant_unref (reply);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+connection_closed (GDBusConnection *connection,
0e28707
+                   gboolean         remote_peer_vanished,
0e28707
+                   GError          *error,
0e28707
+                   gpointer         user_data)
0e28707
+{
0e28707
+  NamespaceWatcher *watcher = user_data;
0e28707
+
0e28707
+  namespace_watcher_stop (watcher);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+got_bus (GObject      *object,
0e28707
+         GAsyncResult *result,
0e28707
+         gpointer      user_data)
0e28707
+{
0e28707
+  GDBusConnection *connection;
0e28707
+  NamespaceWatcher *watcher;
0e28707
+  GError *error = NULL;
0e28707
+
0e28707
+  connection = g_bus_get_finish (result, &error);
0e28707
+
0e28707
+  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
0e28707
+    {
0e28707
+      g_error_free (error);
0e28707
+      return;
0e28707
+    }
0e28707
+
0e28707
+  watcher = user_data;
0e28707
+
0e28707
+  if (connection == NULL)
0e28707
+    {
0e28707
+      namespace_watcher_stop (watcher);
0e28707
+      return;
0e28707
+    }
0e28707
+
0e28707
+  watcher->connection = connection;
0e28707
+  g_signal_connect (watcher->connection, "closed", G_CALLBACK (connection_closed), watcher);
0e28707
+
0e28707
+  watcher->subscription_id =
0e28707
+    g_dbus_connection_signal_subscribe (watcher->connection, "org.freedesktop.DBus",
0e28707
+                                        "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus",
0e28707
+                                        watcher->name_space, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE,
0e28707
+                                        name_owner_changed, watcher, NULL);
0e28707
+
0e28707
+  g_dbus_connection_call (watcher->connection, "org.freedesktop.DBus", "/",
0e28707
+                          "org.freedesktop.DBus", "ListNames", NULL, G_VARIANT_TYPE ("(as)"),
0e28707
+                          G_DBUS_CALL_FLAGS_NONE, -1, watcher->cancellable,
0e28707
+                          names_listed, watcher);
0e28707
+}
0e28707
+
0e28707
+guint
0e28707
+bus_watch_namespace (GBusType                  bus_type,
0e28707
+                     const gchar              *name_space,
0e28707
+                     GBusNameAppearedCallback  appeared_handler,
0e28707
+                     GBusNameVanishedCallback  vanished_handler,
0e28707
+                     gpointer                  user_data,
0e28707
+                     GDestroyNotify            user_data_destroy)
0e28707
+{
0e28707
+  NamespaceWatcher *watcher;
0e28707
+
0e28707
+  /* same rules for interfaces and well-known names */
0e28707
+  g_return_val_if_fail (name_space != NULL && g_dbus_is_interface_name (name_space), 0);
0e28707
+  g_return_val_if_fail (appeared_handler || vanished_handler, 0);
0e28707
+
0e28707
+  watcher = g_new0 (NamespaceWatcher, 1);
0e28707
+  watcher->id = namespace_watcher_next_id++;
0e28707
+  watcher->name_space = g_strdup (name_space);
0e28707
+  watcher->appeared_handler = appeared_handler;
0e28707
+  watcher->vanished_handler = vanished_handler;
0e28707
+  watcher->user_data = user_data;
0e28707
+  watcher->user_data_destroy = user_data_destroy;
0e28707
+  watcher->cancellable = g_cancellable_new ();;
0e28707
+  watcher->names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
0e28707
+
0e28707
+  if (namespace_watcher_watchers == NULL)
0e28707
+    namespace_watcher_watchers = g_hash_table_new (g_direct_hash, g_direct_equal);
0e28707
+  g_hash_table_insert (namespace_watcher_watchers, GUINT_TO_POINTER (watcher->id), watcher);
0e28707
+
0e28707
+  g_bus_get (bus_type, watcher->cancellable, got_bus, watcher);
0e28707
+
0e28707
+  return watcher->id;
0e28707
+}
0e28707
+
0e28707
+void
0e28707
+bus_unwatch_namespace (guint id)
0e28707
+{
0e28707
+  /* namespace_watcher_stop() might have already removed the watcher
0e28707
+   * with @id in the case of a connection error. Thus, this function
0e28707
+   * doesn't warn when @id is absent from the hash table.
0e28707
+   */
0e28707
+
0e28707
+  if (namespace_watcher_watchers)
0e28707
+    {
0e28707
+      NamespaceWatcher *watcher;
0e28707
+
0e28707
+      watcher = g_hash_table_lookup (namespace_watcher_watchers, GUINT_TO_POINTER (id));
0e28707
+      if (watcher)
0e28707
+        {
0e28707
+          /* make sure vanished() is not called as a result of this function */
0e28707
+          g_hash_table_remove_all (watcher->names);
0e28707
+
0e28707
+          namespace_watcher_stop (watcher);
0e28707
+        }
0e28707
+    }
0e28707
+}
0e28707
diff -uNrp a/plugins/media-keys/bus-watch-namespace.h b/plugins/media-keys/bus-watch-namespace.h
0e28707
--- a/plugins/media-keys/bus-watch-namespace.h	1970-01-01 01:00:00.000000000 +0100
0e28707
+++ b/plugins/media-keys/bus-watch-namespace.h	2013-11-04 10:41:10.926022360 +0000
0e28707
@@ -0,0 +1,34 @@
0e28707
+/*
0e28707
+ * Copyright 2013 Canonical Ltd.
0e28707
+ *
0e28707
+ * This program is free software; you can redistribute it and/or modify
0e28707
+ * it under the terms of the GNU Lesser General Public License as
0e28707
+ * published by the Free Software Foundation; either version 2 of the
0e28707
+ * License, or (at your option) any later version.
0e28707
+ *
0e28707
+ * This program is distributed in the hope that it will be useful, but
0e28707
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
0e28707
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0e28707
+ * Lesser General Public License for more details.
0e28707
+ *
0e28707
+ * You should have received a copy of the GNU Lesser General Public License
0e28707
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0e28707
+ *
0e28707
+ * Author: Lars Uebernickel <lars.uebernickel@canonical.com>
0e28707
+ */
0e28707
+
0e28707
+#ifndef __BUS_WATCH_NAMESPACE_H__
0e28707
+#define __BUS_WATCH_NAMESPACE_H__
0e28707
+
0e28707
+#include <gio/gio.h>
0e28707
+
0e28707
+guint       bus_watch_namespace         (GBusType                  bus_type,
0e28707
+                                         const gchar              *name_space,
0e28707
+                                         GBusNameAppearedCallback  appeared_handler,
0e28707
+                                         GBusNameVanishedCallback  vanished_handler,
0e28707
+                                         gpointer                  user_data,
0e28707
+                                         GDestroyNotify            user_data_destroy);
0e28707
+
0e28707
+void        bus_unwatch_namespace       (guint id);
0e28707
+
0e28707
+#endif
0e28707
diff -uNrp a/plugins/media-keys/csd-media-keys-manager.c b/plugins/media-keys/csd-media-keys-manager.c
0e28707
--- a/plugins/media-keys/csd-media-keys-manager.c	2013-11-03 15:50:04.000000000 +0000
0e28707
+++ b/plugins/media-keys/csd-media-keys-manager.c	2013-11-04 11:14:29.158446609 +0000
0e28707
@@ -44,6 +44,7 @@
0e28707
 #include <gudev/gudev.h>
0e28707
 #endif
0e28707
 
0e28707
+#include "mpris-controller.h"
0e28707
 #include "cinnamon-settings-profile.h"
0e28707
 #include "csd-marshal.h"
0e28707
 #include "csd-media-keys-manager.h"
0e28707
@@ -186,6 +187,8 @@ struct CsdMediaKeysManagerPrivate
0e28707
 
0e28707
         guint            start_idle_id;
0e28707
 
0e28707
+        MprisController *mpris_controller;
0e28707
+
0e28707
         /* Ubuntu notifications */
0e28707
         NotifyNotification *volume_notification;
0e28707
         NotifyNotification *brightness_notification;
0e28707
@@ -1281,12 +1284,14 @@ csd_media_player_key_pressed (CsdMediaKe
0e28707
         have_listeners = (manager->priv->media_players != NULL);
0e28707
 
0e28707
         if (!have_listeners) {
0e28707
+                if (!mpris_controller_key (manager->priv->mpris_controller, key)) {
0e28707
                 /* Popup a dialog with an (/) icon */
0e28707
                 dialog_init (manager);
0e28707
                 csd_osd_window_set_action_custom (CSD_OSD_WINDOW (manager->priv->dialog),
0e28707
                                                          "action-unavailable-symbolic",
0e28707
                                                          FALSE);
0e28707
                 dialog_show (manager);
0e28707
+                 }
0e28707
                 return TRUE;
0e28707
         }
0e28707
 
0e28707
@@ -2136,6 +2141,9 @@ start_media_keys_idle_cb (CsdMediaKeysMa
0e28707
         g_free(sound);
0e28707
         g_object_unref (settings);
0e28707
 
0e28707
+        g_debug ("Starting mpris controller");
0e28707
+        manager->priv->mpris_controller = mpris_controller_new ();
0e28707
+
0e28707
         cinnamon_settings_profile_end (NULL);
0e28707
 
0e28707
         manager->priv->start_idle_id = 0;
0e28707
@@ -2258,6 +2266,11 @@ csd_media_keys_manager_stop (CsdMediaKey
0e28707
                 priv->power_keyboard_proxy = NULL;
0e28707
         }
0e28707
 
0e28707
+        if (priv->mpris_controller) {
0e28707
+                g_object_unref (priv->mpris_controller);
0e28707
+                priv->mpris_controller = NULL;
0e28707
+        }
0e28707
+
0e28707
         if (priv->upower_proxy) {
0e28707
                 g_object_unref (priv->upower_proxy);
0e28707
                 priv->upower_proxy = NULL;
0e28707
diff -uNrp a/plugins/media-keys/Makefile.am b/plugins/media-keys/Makefile.am
0e28707
--- a/plugins/media-keys/Makefile.am	2013-11-03 15:50:04.000000000 +0000
0e28707
+++ b/plugins/media-keys/Makefile.am	2013-11-04 10:43:23.308286574 +0000
0e28707
@@ -28,6 +28,10 @@ libmedia_keys_la_SOURCES = 		\
0e28707
 	csd-osd-window.h		\
0e28707
 	csd-osd-window-private.h	\
0e28707
 	shortcuts-list.h		\
0e28707
+	bus-watch-namespace.c		\
0e28707
+	bus-watch-namespace.h		\
0e28707
+	mpris-controller.c		\
0e28707
+	mpris-controller.h		\
0e28707
 	$(BUILT_SOURCES)		\
0e28707
 	$(NULL)
0e28707
 
0e28707
@@ -101,6 +105,10 @@ csd_test_media_keys_SOURCES =			\
0e28707
 	csd-osd-window.c			\
0e28707
 	csd-osd-window.h			\
0e28707
 	csd-osd-window-private.h		\
0e28707
+	bus-watch-namespace.c			\
0e28707
+	bus-watch-namespace.h			\
0e28707
+	mpris-controller.c		        \
0e28707
+	mpris-controller.h	        	\
0e28707
 	test-media-keys.c			\
0e28707
 	$(BUILT_SOURCES)			\
0e28707
 	$(NULL)
0e28707
diff -uNrp a/plugins/media-keys/mpris-controller.c b/plugins/media-keys/mpris-controller.c
0e28707
--- a/plugins/media-keys/mpris-controller.c	1970-01-01 01:00:00.000000000 +0100
0e28707
+++ b/plugins/media-keys/mpris-controller.c	2013-11-04 10:58:55.607910850 +0000
0e28707
@@ -0,0 +1,209 @@
0e28707
+/*
0e28707
+ * Copyright © 2013 Intel Corporation.
0e28707
+ *
0e28707
+ * This program is free software; you can redistribute it and/or modify it
0e28707
+ * under the terms and conditions of the GNU Lesser General Public License,
0e28707
+ * version 2.1, as published by the Free Software Foundation.
0e28707
+ *
0e28707
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
0e28707
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
0e28707
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
0e28707
+ * details.
0e28707
+ *
0e28707
+ * You should have received a copy of the GNU Lesser General Public License
0e28707
+ * along with this program; if not, see <http://www.gnu.org/licenses>
0e28707
+ *
0e28707
+ * Author: Michael Wood <michael.g.wood@intel.com>
0e28707
+ */
0e28707
+
0e28707
+#include "mpris-controller.h"
0e28707
+#include "bus-watch-namespace.h"
0e28707
+#include <gio/gio.h>
0e28707
+
0e28707
+G_DEFINE_TYPE (MprisController, mpris_controller, G_TYPE_OBJECT)
0e28707
+
0e28707
+#define CONTROLLER_PRIVATE(o) \
0e28707
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), MPRIS_TYPE_CONTROLLER, MprisControllerPrivate))
0e28707
+
0e28707
+struct _MprisControllerPrivate
0e28707
+{
0e28707
+  GCancellable *cancellable;
0e28707
+  GDBusProxy *mpris_client_proxy;
0e28707
+  guint namespace_watcher_id;
0e28707
+  GSList *other_players;
0e28707
+  gboolean connecting;
0e28707
+};
0e28707
+
0e28707
+
0e28707
+static void
0e28707
+mpris_controller_dispose (GObject *object)
0e28707
+{
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (object)->priv;
0e28707
+
0e28707
+  g_clear_object (&priv->cancellable);
0e28707
+  g_clear_object (&priv->mpris_client_proxy);
0e28707
+
0e28707
+  if (priv->namespace_watcher_id)
0e28707
+    {
0e28707
+      bus_unwatch_namespace (priv->namespace_watcher_id);
0e28707
+      priv->namespace_watcher_id = 0;
0e28707
+    }
0e28707
+
0e28707
+  if (priv->other_players)
0e28707
+    {
0e28707
+      g_slist_free_full (priv->other_players, g_free);
0e28707
+      priv->other_players = NULL;
0e28707
+    }
0e28707
+
0e28707
+  G_OBJECT_CLASS (mpris_controller_parent_class)->dispose (object);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_proxy_call_done (GObject      *object,
0e28707
+                       GAsyncResult *res,
0e28707
+                       gpointer      user_data)
0e28707
+{
0e28707
+  GError *error = NULL;
0e28707
+  GVariant *ret;
0e28707
+
0e28707
+  if (!(ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (object), res, &error)))
0e28707
+    {
0e28707
+      g_warning ("Error calling method %s", error->message);
0e28707
+      g_clear_error (&error);
0e28707
+      return;
0e28707
+    }
0e28707
+  g_variant_unref (ret);
0e28707
+}
0e28707
+
0e28707
+gboolean
0e28707
+mpris_controller_key (MprisController *self, const gchar *key)
0e28707
+{
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (self)->priv;
0e28707
+
0e28707
+  if (!priv->mpris_client_proxy)
0e28707
+    return FALSE;
0e28707
+
0e28707
+  g_debug ("calling %s over dbus to mpris client %s",
0e28707
+           key, g_dbus_proxy_get_name (priv->mpris_client_proxy));
0e28707
+  g_dbus_proxy_call (priv->mpris_client_proxy,
0e28707
+                     key, NULL, 0, -1, priv->cancellable,
0e28707
+                     mpris_proxy_call_done,
0e28707
+                     NULL);
0e28707
+  return TRUE;
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_proxy_ready_cb (GObject      *object,
0e28707
+                      GAsyncResult *res,
0e28707
+                      gpointer      user_data)
0e28707
+{
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (user_data)->priv;
0e28707
+  GError *error = NULL;
0e28707
+
0e28707
+  priv->mpris_client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
0e28707
+
0e28707
+  if (!priv->mpris_client_proxy)
0e28707
+    g_warning ("Error connecting to mpris interface %s", error->message);
0e28707
+
0e28707
+  g_clear_error (&error);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+start_mpris_proxy (MprisController *self, const gchar *name)
0e28707
+{
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (self)->priv;
0e28707
+
0e28707
+  g_debug ("Creating proxy for for %s", name);
0e28707
+  g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
0e28707
+                            0,
0e28707
+                            NULL,
0e28707
+                            name,
0e28707
+                            "/org/mpris/MediaPlayer2",
0e28707
+                            "org.mpris.MediaPlayer2.Player",
0e28707
+                            priv->cancellable,
0e28707
+                            mpris_proxy_ready_cb,
0e28707
+                            self);
0e28707
+  priv->connecting = TRUE;
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_player_appeared (GDBusConnection *connection,
0e28707
+                       const gchar     *name,
0e28707
+                       const gchar     *name_owner,
0e28707
+                       gpointer         user_data)
0e28707
+{
0e28707
+  MprisController *self = user_data;
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (self)->priv;
0e28707
+
0e28707
+  if (priv->mpris_client_proxy == NULL && !priv->connecting)
0e28707
+    start_mpris_proxy (self, name);
0e28707
+  else
0e28707
+    self->priv->other_players = g_slist_prepend (self->priv->other_players, g_strdup (name));
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_player_vanished (GDBusConnection *connection,
0e28707
+                       const gchar     *name,
0e28707
+                       gpointer         user_data)
0e28707
+{
0e28707
+  MprisController *self = user_data;
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (self)->priv;
0e28707
+
0e28707
+  if (priv->mpris_client_proxy &&
0e28707
+      g_strcmp0 (name, g_dbus_proxy_get_name (priv->mpris_client_proxy)) == 0)
0e28707
+    {
0e28707
+      g_clear_object (&priv->mpris_client_proxy);
0e28707
+
0e28707
+      /* take the next one if there's one */
0e28707
+      if (self->priv->other_players && !priv->connecting)
0e28707
+        {
0e28707
+          GSList *first;
0e28707
+          gchar *name;
0e28707
+
0e28707
+          first = self->priv->other_players;
0e28707
+          name = first->data;
0e28707
+
0e28707
+          start_mpris_proxy (self, name);
0e28707
+
0e28707
+          self->priv->other_players = self->priv->other_players->next;
0e28707
+          g_free (name);
0e28707
+          g_slist_free_1 (first);
0e28707
+        }
0e28707
+    }
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_controller_constructed (GObject *object)
0e28707
+{
0e28707
+  MprisControllerPrivate *priv = MPRIS_CONTROLLER (object)->priv;
0e28707
+
0e28707
+  priv->namespace_watcher_id = bus_watch_namespace (G_BUS_TYPE_SESSION,
0e28707
+                                                    "org.mpris.MediaPlayer2",
0e28707
+                                                    mpris_player_appeared,
0e28707
+                                                    mpris_player_vanished,
0e28707
+                                                    MPRIS_CONTROLLER (object),
0e28707
+                                                    NULL);
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_controller_class_init (MprisControllerClass *klass)
0e28707
+{
0e28707
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
0e28707
+
0e28707
+  g_type_class_add_private (klass, sizeof (MprisControllerPrivate));
0e28707
+
0e28707
+  object_class->constructed = mpris_controller_constructed;
0e28707
+  object_class->dispose = mpris_controller_dispose;
0e28707
+}
0e28707
+
0e28707
+static void
0e28707
+mpris_controller_init (MprisController *self)
0e28707
+{
0e28707
+  self->priv = CONTROLLER_PRIVATE (self);
0e28707
+}
0e28707
+
0e28707
+MprisController *
0e28707
+mpris_controller_new (void)
0e28707
+{
0e28707
+  return g_object_new (MPRIS_TYPE_CONTROLLER, NULL);
0e28707
+}
0e28707
diff -uNrp a/plugins/media-keys/mpris-controller.h b/plugins/media-keys/mpris-controller.h
0e28707
--- a/plugins/media-keys/mpris-controller.h	1970-01-01 01:00:00.000000000 +0100
0e28707
+++ b/plugins/media-keys/mpris-controller.h	2013-11-04 10:58:55.607910850 +0000
0e28707
@@ -0,0 +1,56 @@
0e28707
+/*
0e28707
+ * Copyright © 2013 Intel Corporation.
0e28707
+ *
0e28707
+ * This program is free software; you can redistribute it and/or modify it
0e28707
+ * under the terms and conditions of the GNU Lesser General Public License,
0e28707
+ * version 2.1, as published by the Free Software Foundation.
0e28707
+ *
0e28707
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
0e28707
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
0e28707
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
0e28707
+ * details.
0e28707
+ *
0e28707
+ * You should have received a copy of the GNU Lesser General Public License
0e28707
+ * along with this program; if not, see <http://www.gnu.org/licenses>
0e28707
+ *
0e28707
+ * Author: Michael Wood <michael.g.wood@intel.com>
0e28707
+ */
0e28707
+
0e28707
+#ifndef __MPRIS_CONTROLLER_H__
0e28707
+#define __MPRIS_CONTROLLER_H__
0e28707
+
0e28707
+#include <glib-object.h>
0e28707
+
0e28707
+G_BEGIN_DECLS
0e28707
+
0e28707
+#define MPRIS_TYPE_CONTROLLER mpris_controller_get_type()
0e28707
+#define MPRIS_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MPRIS_TYPE_CONTROLLER, MprisController))
0e28707
+#define MPRIS_CONTROLLER_CLASS(klass)  (G_TYPE_CHECK_CLASS_CAST ((klass), MPRIS_TYPE_CONTROLLER, MprisControllerClass))
0e28707
+#define MPRIS_IS_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MPRIS_TYPE_CONTROLLER))
0e28707
+#define MPRIS_IS_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MPRIS_TYPE_CONTROLLER))
0e28707
+#define MPRIS_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MPRIS_TYPE_CONTROLLER, MprisControllerClass))
0e28707
+
0e28707
+typedef struct _MprisController MprisController;
0e28707
+typedef struct _MprisControllerClass MprisControllerClass;
0e28707
+typedef struct _MprisControllerPrivate MprisControllerPrivate;
0e28707
+
0e28707
+struct _MprisController
0e28707
+{
0e28707
+  GObject parent;
0e28707
+
0e28707
+  MprisControllerPrivate *priv;
0e28707
+};
0e28707
+
0e28707
+struct _MprisControllerClass
0e28707
+{
0e28707
+  GObjectClass parent_class;
0e28707
+};
0e28707
+
0e28707
+GType mpris_controller_get_type (void) G_GNUC_CONST;
0e28707
+
0e28707
+MprisController *mpris_controller_new (void);
0e28707
+gboolean         mpris_controller_key (MprisController *self, const gchar *key);
0e28707
+
0e28707
+G_END_DECLS
0e28707
+
0e28707
+#endif /* __MPRIS_CONTROLLER_H__ */