summaryrefslogtreecommitdiffstats
path: root/plugins/media-keys/mpris-controller.c
blob: d60d362ec1ab932fb36aef3f34f9be67b9a72c36 (plain)
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
/*
 * Copyright © 2013 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>
 *
 * Author: Michael Wood <michael.g.wood@intel.com>
 */

#include "mpris-controller.h"
#include "bus-watch-namespace.h"
#include <gio/gio.h>

enum {
  PROP_0,
  PROP_HAS_ACTIVE_PLAYER
};

struct _MprisController
{
  GObject parent;

  GCancellable *cancellable;
  GDBusProxy *mpris_client_proxy;
  guint namespace_watcher_id;
  GSList *other_proxies;
};

G_DEFINE_TYPE (MprisController, mpris_controller, G_TYPE_OBJECT)

static void
mpris_controller_dispose (GObject *object)
{
  MprisController *self = MPRIS_CONTROLLER (object);

  g_clear_object (&self->cancellable);
  g_clear_object (&self->mpris_client_proxy);

  if (self->namespace_watcher_id)
    {
      bus_unwatch_namespace (self->namespace_watcher_id);
      self->namespace_watcher_id = 0;
    }

  g_slist_free_full (g_steal_pointer (&self->other_proxies), g_object_unref);

  G_OBJECT_CLASS (mpris_controller_parent_class)->dispose (object);
}

static void
mpris_proxy_call_done (GObject      *object,
                       GAsyncResult *res,
                       gpointer      user_data)
{
  GError *error = NULL;
  GVariant *ret;

  if (!(ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (object), res, &error)))
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error calling method %s", error->message);
      g_clear_error (&error);
      return;
    }
  g_variant_unref (ret);
}

gboolean
mpris_controller_key (MprisController *self, const gchar *key)
{
  g_return_val_if_fail (MPRIS_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

  if (!self->mpris_client_proxy)
    return FALSE;

  if (g_strcmp0 (key, "Play") == 0)
    key = "PlayPause";

  g_debug ("calling %s over dbus to mpris client %s",
           key, g_dbus_proxy_get_name (self->mpris_client_proxy));
  g_dbus_proxy_call (self->mpris_client_proxy,
                     key, NULL, 0, -1, self->cancellable,
                     mpris_proxy_call_done,
                     NULL);
  return TRUE;
}

gboolean
mpris_controller_seek (MprisController *self, gint64 offset)
{
  g_return_val_if_fail (MPRIS_IS_CONTROLLER (self), FALSE);

  if (!self->mpris_client_proxy)
    return FALSE;

  g_debug ("calling Seek over dbus to mpris client %s",
           g_dbus_proxy_get_name (self->mpris_client_proxy));
  g_dbus_proxy_call (self->mpris_client_proxy,
                     "Seek", g_variant_new ("(x)", offset, NULL),
                     G_DBUS_CALL_FLAGS_NONE, -1, self->cancellable,
                     mpris_proxy_call_done,
                     NULL);
  return TRUE;
}

static GDBusProxy *
get_props_proxy (GDBusProxy   *proxy,
                 GCancellable *cancellable)
{
  GDBusProxy *props = NULL;
  g_autoptr(GError) error = NULL;

  g_return_val_if_fail (proxy != NULL, NULL);

  props = g_dbus_proxy_new_sync (g_dbus_proxy_get_connection (proxy),
                                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                                 NULL,
                                 g_dbus_proxy_get_name (proxy),
                                 g_dbus_proxy_get_object_path (proxy),
                                 "org.freedesktop.DBus.Properties",
                                 cancellable,
                                 &error);
  if (!props) {
    g_debug ("Could not get properties proxy for %s: %s",
             g_dbus_proxy_get_interface_name (proxy),
             error->message);
    return NULL;
  }

  return props;
}

gboolean
mpris_controller_toggle (MprisController *self, const gchar *property)
{
  g_return_val_if_fail (MPRIS_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (property != NULL, FALSE);

  if (!self->mpris_client_proxy)
    return FALSE;

  if (g_str_equal (property, "LoopStatus")) {
    g_autoptr(GDBusProxy) props = NULL;
    g_autoptr(GVariant) loop_status;
    const gchar *status_str, *new_status;

    loop_status = g_dbus_proxy_get_cached_property (self->mpris_client_proxy, "LoopStatus");
    if (!loop_status)
      return FALSE;
    if (!g_variant_is_of_type (loop_status, G_VARIANT_TYPE_STRING))
      return FALSE;
    status_str = g_variant_get_string (loop_status, NULL);
    if (g_str_equal (status_str, "Playlist"))
      new_status = "None";
    else
      new_status = "Playlist";

    props = get_props_proxy (self->mpris_client_proxy, self->cancellable);
    if (!props)
      return FALSE;
    g_dbus_proxy_call (props,
                       "Set",
                       g_variant_new_parsed ("('org.mpris.MediaPlayer2.Player', 'LoopStatus', %v)",
                                             g_variant_new_string (new_status)),
                       G_DBUS_CALL_FLAGS_NONE,
                       -1,
                       self->cancellable,
                       mpris_proxy_call_done, NULL);
  } else if (g_str_equal (property, "Shuffle")) {
    g_autoptr(GDBusProxy) props = NULL;
    g_autoptr(GVariant) shuffle_status;
    gboolean status;

    shuffle_status = g_dbus_proxy_get_cached_property (self->mpris_client_proxy, "Shuffle");
    if (!shuffle_status)
      return FALSE;
    if (!g_variant_is_of_type (shuffle_status, G_VARIANT_TYPE_BOOLEAN))
      return FALSE;
    status = g_variant_get_boolean (shuffle_status);

    props = get_props_proxy (self->mpris_client_proxy, self->cancellable);
    if (!props)
      return FALSE;
    g_dbus_proxy_call (props,
                       "Set",
                       g_variant_new_parsed ("('org.mpris.MediaPlayer2.Player', 'Shuffle', %v)",
                                             g_variant_new_boolean (!status)),
                       G_DBUS_CALL_FLAGS_NONE,
                       -1,
                       self->cancellable,
                       mpris_proxy_call_done, NULL);
  }

  g_debug ("Unhandled toggle property '%s'", property);

  return TRUE;
}

static gboolean
mpris_client_is_playing (GDBusProxy *proxy)
{
  g_autoptr(GVariant) playback_status;
  const gchar *status_str;

  playback_status = g_dbus_proxy_get_cached_property (proxy, "PlaybackStatus");
  if (!playback_status)
    return FALSE;

  if (!g_variant_is_of_type (playback_status, G_VARIANT_TYPE_STRING))
    return FALSE;

  status_str = g_variant_get_string (playback_status, NULL);
  return g_strcmp0 (status_str, "Playing") == 0;
}

static void
mpris_client_notify_name_owner_cb (GDBusProxy      *proxy,
                                   GParamSpec      *pspec,
                                   MprisController *self)
{
  g_autofree gchar *name_owner = NULL;
  GSList *first;

  /* Owner changed, but the proxy is still valid. */
  name_owner = g_dbus_proxy_get_name_owner (proxy);
  if (name_owner)
    return;

  if (proxy == self->mpris_client_proxy)
    {
      g_debug ("Clearing the current MPRIS client proxy");
      g_clear_object (&self->mpris_client_proxy);

      if ((first = self->other_proxies))
        {
          self->mpris_client_proxy = first->data;
          self->other_proxies = first->next;
          g_slist_free_1 (first);

          g_debug ("Falling back to MPRIS client %s",
                   g_dbus_proxy_get_name (self->mpris_client_proxy));
        }
      else
        {
          g_object_notify (G_OBJECT (self), "has-active-player");
        }
    }
  else
    {
      g_debug ("Forgetting MPRIS client %s", g_dbus_proxy_get_name (proxy));
      self->other_proxies = g_slist_remove (self->other_proxies, proxy);
      g_object_unref (proxy);
    }
}

static void
mpris_client_properties_changed_cb (GDBusProxy *proxy,
                                    GVariant   *changed_properties,
                                    GStrv       invalidated_properties,
                                    gpointer    user_data)
{
  MprisController *self = MPRIS_CONTROLLER (user_data);
  GDBusProxy *current_proxy;

  current_proxy = self->mpris_client_proxy;
  if (current_proxy == proxy)
    return;

  if (current_proxy && mpris_client_is_playing (current_proxy))
    return;

  if (mpris_client_is_playing (proxy))
    {
      g_debug ("Switching to MPRIS client %s because it is playing",
               g_dbus_proxy_get_name (proxy));

      self->other_proxies = g_slist_remove (self->other_proxies, proxy);

      if (current_proxy)
        self->other_proxies = g_slist_prepend (self->other_proxies, current_proxy);

      self->mpris_client_proxy = proxy;

      if (!current_proxy)
        g_object_notify (user_data, "has-active-player");
    }
}

static void
mpris_proxy_ready_cb (GObject      *object,
                      GAsyncResult *res,
                      gpointer      user_data)
{
  MprisController *self = MPRIS_CONTROLLER (user_data);
  GError *error = NULL;
  GDBusProxy *proxy;
  const gchar *name;

  proxy = g_dbus_proxy_new_for_bus_finish (res, &error);

  if (!proxy)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Error connecting to MPRIS interface: %s", error->message);
      g_clear_error (&error);
      return;
    }

  g_signal_connect (proxy, "notify::g-name-owner",
                    G_CALLBACK (mpris_client_notify_name_owner_cb), user_data);

  g_signal_connect (proxy, "g-properties-changed",
                    G_CALLBACK (mpris_client_properties_changed_cb), user_data);

  name = g_dbus_proxy_get_name (proxy);

  if (self->mpris_client_proxy)
    {
      if (mpris_client_is_playing (self->mpris_client_proxy))
        {
          g_debug ("Remembering %s for later because the current MPRIS client is playing",
                   name);
          self->other_proxies = g_slist_prepend (self->other_proxies, proxy);
          return;
        }

      g_debug ("Remembering the current MPRIS client for later");
      self->other_proxies =
        g_slist_prepend (self->other_proxies, self->mpris_client_proxy);
    }

  g_debug ("Switching to MPRIS client %s because it just appeared", name);

  self->mpris_client_proxy = proxy;

  g_object_notify (user_data, "has-active-player");
}

static void
start_mpris_proxy (MprisController *self, const gchar *name)
{
  g_debug ("Creating proxy for %s", name);
  g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
                            0,
                            NULL,
                            name,
                            "/org/mpris/MediaPlayer2",
                            "org.mpris.MediaPlayer2.Player",
                            self->cancellable,
                            mpris_proxy_ready_cb,
                            self);
}

static void
mpris_player_appeared (GDBusConnection *connection,
                       const gchar     *name,
                       const gchar     *name_owner,
                       gpointer         user_data)
{
  start_mpris_proxy (MPRIS_CONTROLLER (user_data), name);
}

static void
mpris_controller_constructed (GObject *object)
{
  MprisController *self = MPRIS_CONTROLLER (object);

  self->namespace_watcher_id = bus_watch_namespace (G_BUS_TYPE_SESSION,
                                                    "org.mpris.MediaPlayer2",
                                                    mpris_player_appeared,
                                                    NULL,
                                                    MPRIS_CONTROLLER (object),
                                                    NULL);
}

static void
mpris_controller_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  MprisController *self = MPRIS_CONTROLLER (object);

  switch (prop_id) {
  case PROP_HAS_ACTIVE_PLAYER:
    g_value_set_boolean (value,
                         mpris_controller_get_has_active_player (self));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

static void
mpris_controller_class_init (MprisControllerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = mpris_controller_constructed;
  object_class->dispose = mpris_controller_dispose;
  object_class->get_property = mpris_controller_get_property;

  g_object_class_install_property (object_class,
                                   PROP_HAS_ACTIVE_PLAYER,
                                   g_param_spec_boolean ("has-active-player",
                                                         NULL,
                                                         NULL,
                                                         FALSE,
                                                         G_PARAM_READABLE));
}

static void
mpris_controller_init (MprisController *self)
{
}

gboolean
mpris_controller_get_has_active_player (MprisController *controller)
{
  g_return_val_if_fail (MPRIS_IS_CONTROLLER (controller), FALSE);

  return (controller->mpris_client_proxy != NULL);
}

MprisController *
mpris_controller_new (void)
{
  return g_object_new (MPRIS_TYPE_CONTROLLER, NULL);
}