Debian Codegraph @

musicPlayer/src/applet-mpris2.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <glib/gi18n.h>
24 #include <cairo-dock.h>
25
26 #include "applet-struct.h"
27 #include "applet-musicplayer.h"
28 #include "applet-dbus.h"
29 #include "applet-draw.h"
30 #include "applet-cover.h"
31 #include "applet-mpris2.h"
32
33 static DBusGProxyCall *s_pGetSongInfosCall = NULL;
34 static DBusGProxyCall *s_pGetStatusCall = NULL;
35
36 static void cd_mpris2_getSongInfos_async (void);
37 static gboolean cd_mpris2_is_loop (void);
38 static gboolean cd_mpris2_is_shuffle (void);
39
40 /*
41 Interface MediaPlayer2
42
43 Methods
44 Raise () -> nothing
45 Quit () -> nothing
46
47 Properties
48 CanQuit b Read only
49 CanRaise b Read only
50 HasTrackList b Read only
51 Identity s Read only
52 DesktopEntry s Read only
53 SupportedUriSchemes as Read only
54 SupportedMimeTypes as Read only
55
56 Interface MediaPlayer2.Player
57
58 Methods
59 Next () -> nothing
60 Previous () -> nothing
61 Pause () -> nothing
62 PlayPause () -> nothing
63 Stop () -> nothing
64 Play () -> nothing
65 Seek (x: Offset) -> nothing
66 SetPosition (o: TrackId, x: Position) -> nothing
67 OpenUri (s: Uri) -> nothing
68
69 Signals
70 Seeked (x: Position)
71
72 Properties
73 PlaybackStatus s (Playback_Status) Read only
74 LoopStatus s (Loop_Status) Read/Write
75 Rate d (Playback_Rate) Read/Write
76 Shuffle b Read/Write
77 Metadata a{sv} (Metadata_Map) Read only
78 Volume d (Volume) Read/Write
79 Position x (Time_In_Us) Read only
80 MinimumRate d (Playback_Rate) Read only
81 MaximumRate d (Playback_Rate) Read only
82 CanGoNext b Read only
83 CanGoPrevious b Read only
84 CanPlay b Read only
85 CanPause b Read only
86 CanSeek b Read only
87 CanControl b Read only
88
89 Types
90 Track_Id Simple Type o
91 Playback_Rate Simple Type d
92 Volume Simple Type d
93 Time_In_Us Simple Type x
94 Playback_Status Enum s
95 Loop_Status Enum s
96
97 */
98
99 /////////////////////////////////
100 // Les Fonctions propres a MP. //
101 /////////////////////////////////
102 static gboolean s_bIsLoop = FALSE;
103 static gboolean s_bGotLoopStatus = FALSE;
104 static gboolean s_bIsShuffle = FALSE;
105 static gboolean s_bGotShuffleStatus = FALSE;
106 static gboolean s_bCanRaise = FALSE;
107 static gboolean s_bGotCanRaise = FALSE;
108 static gboolean s_bCanQuit = FALSE;
109 static gboolean s_bGotCanQuit = FALSE;
110
111 static gboolean get_loop_status (void)
112 {
113 if (! s_bGotLoopStatus)
114 {
115 s_bIsLoop = cd_mpris2_is_loop ();
116 s_bGotLoopStatus = TRUE;
117 }
118 return s_bIsLoop;
119 }
120
121 static gboolean get_shuffle_status (void)
122 {
123 if (! s_bGotShuffleStatus)
124 {
125 s_bIsShuffle = cd_mpris2_is_shuffle ();
126 s_bGotShuffleStatus = TRUE;
127 }
128 return s_bIsShuffle;
129 }
130
131 static gboolean _raise (void)
132 {
133 if (! s_bGotCanRaise)
134 {
135 s_bCanRaise = cairo_dock_dbus_get_property_as_boolean_with_timeout (myData.dbus_proxy_shell, "org.mpris.MediaPlayer2", "CanRaise", 1000);
136 cd_debug ("s_bCanRaise : %d", s_bCanRaise);
137 s_bGotCanRaise = TRUE;
138 }
139
140 if (s_bCanRaise)
141 {
142 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Raise");
143 return TRUE;
144 }
145 else
146 {
147 return FALSE;
148 }
149 }
150
151 static gboolean _quit (void)
152 {
153 if (! s_bGotCanQuit)
154 {
155 s_bCanQuit = cairo_dock_dbus_get_property_as_boolean_with_timeout (myData.dbus_proxy_shell, "org.mpris.MediaPlayer2", "CanQuit", 1000);
156 cd_debug ("s_bCanQuit : %d", s_bCanQuit);
157 s_bGotCanQuit = TRUE;
158 }
159
160 if (s_bCanQuit)
161 {
162 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Quit");
163 return TRUE;
164 }
165 else
166 {
167 return FALSE;
168 }
169 }
170
171 static MyPlayerStatus _extract_status (const gchar *cStatus)
172 {
173 if (cStatus == NULL)
174 return PLAYER_BROKEN;
175 if (strcmp (cStatus, "Playing") == 0)
176 return PLAYER_PLAYING;
177 if (strcmp (cStatus, "Paused") == 0)
178 return PLAYER_PAUSED;
179 if (strcmp (cStatus, "Stopped") == 0)
180 return PLAYER_STOPPED;
181 return PLAYER_BROKEN;
182 }
183
184 static void _on_got_playing_status (DBusGProxy *proxy, DBusGProxyCall *call_id, GldiModuleInstance *myApplet)
185 {
186 cd_debug ("=== %s ()", __func__);
187 CD_APPLET_ENTER;
188 s_pGetStatusCall = NULL;
189
190 gchar *cStatus = NULL;
191 GValue v = G_VALUE_INIT;
192 GError *erreur = NULL;
193 dbus_g_proxy_end_call (proxy,
194 call_id,
195 &erreur,
196 G_TYPE_VALUE, &v,
197 G_TYPE_INVALID);
198 if (erreur != NULL)
199 {
200 cd_warning ("couldn't get MPRIS status (%s)\n", erreur->message);
201 g_error_free (erreur);
202 }
203 else
204 {
205 if (G_VALUE_HOLDS_STRING (&v))
206 {
207 cStatus = (gchar*)g_value_get_string (&v);
208 myData.iPlayingStatus = _extract_status (cStatus);
209 g_free (cStatus); // since we don't destroy the value, we destroy its content.
210 }
211 }
212
213 cd_mpris2_getSongInfos_async ();
214
215 CD_APPLET_LEAVE ();
216 }
217 static void cd_mpris2_getPlaying_async (void)
218 {
219 if (s_pGetStatusCall != NULL)
220 return;
221 s_pGetStatusCall = dbus_g_proxy_begin_call (myData.dbus_proxy_player,
222 "Get",
223 (DBusGProxyCallNotify)_on_got_playing_status,
224 myApplet,
225 (GDestroyNotify) NULL,
226 G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
227 G_TYPE_STRING, "PlaybackStatus",
228 G_TYPE_INVALID);
229 }
230
231 static gboolean cd_mpris2_is_loop (void)
232 {
233 gchar *cLoopStatus = cairo_dock_dbus_get_property_as_string_with_timeout (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "LoopStatus", 500);
234 gboolean bLoop = (cLoopStatus != NULL && strcmp (cLoopStatus, "Playlist") == 0);
235 g_free (cLoopStatus);
236 return bLoop;
237 }
238
239 static gboolean cd_mpris2_is_shuffle (void)
240 {
241 return cairo_dock_dbus_get_property_as_boolean_with_timeout (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Shuffle", 500);
242 }
243
244 static void cd_mpris2_get_time_elapsed (void)
245 {
246 GValue v = G_VALUE_INIT;
247 cairo_dock_dbus_get_property_in_value_with_timeout (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Position", &v, 250); // will be call each second, 250ms is already a big timeout, we can wait the next call.
248 if (G_VALUE_HOLDS_INT64 (&v))
249 myData.iCurrentTime = g_value_get_int64 (&v) / 1e6;
250 else if (G_VALUE_HOLDS_UINT64 (&v))
251 myData.iCurrentTime = g_value_get_uint64 (&v) / 1e6;
252 else if (G_VALUE_HOLDS_INT (&v))
253 myData.iCurrentTime = g_value_get_int (&v) / 1e6;
254 else if (G_VALUE_HOLDS_STRING (&v)) // this is bad ! (gmusicbrowser v1.1.7)
255 myData.iCurrentTime = atoi (g_value_get_string (&v)) / 1e6;
256 else
257 {
258 if (G_IS_VALUE(&v)) // when changing song, we don't receive this value => no need to display a warning message each time
259 cd_warning ("wrong type for the 'Position' property, please report this bug to the %s team", myData.pCurrentHandler->appclass);
260 myData.iCurrentTime = -1;
261 }
262 }
263
264 /*static gboolean _can_control (void)
265 {
266 return cairo_dock_dbus_get_property_as_int (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "CanControl");
267 }*/
268
269 static double cd_mpris2_get_volume (void)
270 {
271 return cairo_dock_dbus_get_property_as_double_with_timeout (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Volume", 500);
272 }
273
274 static gboolean _is_a_new_track (const gchar *cTrackID)
275 {
276 cd_message (" TrackId <- %s (was: %s)", cTrackID, myData.cTrackID);
277 if (cairo_dock_strings_differ (myData.cTrackID, cTrackID)) // track has changed.
278 {
279 g_free (myData.cTrackID);
280 myData.cTrackID = g_strdup (cTrackID);
281 return TRUE;
282 }
283 return FALSE;
284 }
285
286 static gboolean _extract_metadata (GHashTable *pMetadata)
287 {
288 gboolean bTrackHasChanged = FALSE;
289 GValue *v;
290 const gchar *str = NULL;
291
292 v = g_hash_table_lookup (pMetadata, "mpris:trackid"); // a string or a dbus object path that uniquely identifies the track within the scope of the playlist
293 if (v != NULL)
294 {
295 if (G_VALUE_HOLDS (v, DBUS_TYPE_G_OBJECT_PATH)) // now this attribute should be of D-Bus type "o"
296 str = (gchar*) g_value_get_boxed (v);
297 else if (G_VALUE_HOLDS_STRING (v)) // but can be a string... e.g. with Rhythmbox
298 str = g_value_get_string (v);
299 bTrackHasChanged = _is_a_new_track (str);
300 }
301
302 v = g_hash_table_lookup (pMetadata, "mpris:length"); // length of the track, in microseconds (signed 64-bit integer)
303 if (v != NULL)
304 {
305 if (G_VALUE_HOLDS_INT64 (v)) // should be a int64
306 myData.iSongLength = g_value_get_int64 (v) / 1000000;
307 else if (G_VALUE_HOLDS_INT (v)) // but some players doesn't respect that... maybe a limitation?
308 myData.iSongLength = g_value_get_int (v) / 1000000;
309 else
310 cd_warning ("Length has a wrong type");
311 cd_debug ("Length: %d", myData.iSongLength);
312 }
313
314 gchar *cOldArtist = myData.cArtist;
315 myData.cArtist = NULL;
316 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:artist");
317 if (v != NULL && G_VALUE_HOLDS(v, G_TYPE_STRV))
318 {
319 gchar **artists = g_value_get_boxed(v);
320 if (artists != NULL)
321 myData.cArtist = g_strjoinv (NULL, artists);
322 }
323 cd_message (" cArtist <- %s", myData.cArtist);
324
325 // maybe the user has renamed the tags of the current song...
326 if (! bTrackHasChanged && cairo_dock_strings_differ (myData.cArtist, cOldArtist))
327 bTrackHasChanged = TRUE;
328 g_free (cOldArtist);
329
330 g_free (myData.cAlbum);
331 myData.cAlbum = NULL;
332 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:album");
333 if (v != NULL && G_VALUE_HOLDS_STRING(v))
334 {
335 str = g_value_get_string(v);
336 if (str && *str != '\0')
337 myData.cAlbum = g_strdup (str);
338 }
339 cd_message (" cAlbum <- %s", myData.cAlbum);
340
341 gchar *cOldTitle = myData.cTitle;
342 myData.cTitle = NULL;
343 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:title");
344 if (v != NULL && G_VALUE_HOLDS_STRING(v))
345 {
346 str = g_value_get_string(v);
347 if (str && *str != '\0')
348 myData.cTitle = g_strdup (str);
349 }
350 cd_message (" cTitle <- %s", myData.cTitle);
351
352 /* some players doesn't support (well) the trackid. Even if this is not our
353 * problem, it can be interesting to also check if the title has changed.
354 */
355 if (! bTrackHasChanged && cairo_dock_strings_differ (myData.cTitle, cOldTitle))
356 bTrackHasChanged = TRUE;
357 g_free (cOldTitle);
358
359 g_free (myData.cPlayingUri);
360 myData.cPlayingUri = NULL;
361 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:url");
362 if (!v)
363 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:uri");
364 if (v != NULL && G_VALUE_HOLDS_STRING(v))
365 {
366 str = g_value_get_string(v);
367 if (str && *str != '\0')
368 myData.cPlayingUri = g_strdup (str);
369 }
370 cd_message (" cUri <- %s", myData.cPlayingUri);
371
372 myData.iTrackNumber = 0; // not really useful, it's the track-number in the album.
373 v = (GValue *) g_hash_table_lookup(pMetadata, "xesam:trackNumber");
374 if (v != NULL && G_VALUE_HOLDS_INT(v))
375 {
376 myData.iTrackNumber = g_value_get_int (v);
377 }
378 cd_message (" iTrackNumber <- %d", myData.iTrackNumber);
379
380 const gchar *cCoverPath = NULL;
381 v = g_hash_table_lookup(pMetadata, "mpris:artUrl");
382 if (v != NULL && G_VALUE_HOLDS_STRING(v))
383 {
384 cCoverPath = g_value_get_string(v);
385 }
386 cd_musicplayer_set_cover_path (cCoverPath); // do it at the end (we have to know the artist and the album if (cCoverPath == NULL))
387
388 /// we miss iTrackListIndex and tracklist-length ...
389
390
391 return bTrackHasChanged;
392 }
393
394 static void _on_got_song_infos (DBusGProxy *proxy, DBusGProxyCall *call_id, GldiModuleInstance *myApplet)
395 {
396 cd_debug ("=== %s ()", __func__);
397 CD_APPLET_ENTER;
398 s_pGetSongInfosCall = NULL;
399
400 GHashTable *pMetadata = NULL;
401 GValue v = G_VALUE_INIT;
402 GError *erreur = NULL;
403 dbus_g_proxy_end_call (proxy,
404 call_id,
405 &erreur,
406 G_TYPE_VALUE, &v,
407 G_TYPE_INVALID);
408 if (erreur != NULL)
409 {
410 cd_warning ("couldn't get MPRIS song infos (%s)\n", erreur->message);
411 g_error_free (erreur);
412 pMetadata = NULL;
413 }
414 else
415 {
416 if (G_VALUE_HOLDS_BOXED (&v))
417 {
418 pMetadata = g_value_get_boxed (&v); // since we don't destroy the value, we'll take care of the hash-table when we're done with it.
419 }
420 }
421 if (pMetadata != NULL)
422 {
423 _extract_metadata (pMetadata);
424
425 g_hash_table_destroy (pMetadata);
426 }
427 else
428 {
429 cd_warning (" can't get song properties");
430 g_free (myData.cPlayingUri);
431 myData.cPlayingUri = NULL;
432 g_free (myData.cTitle);
433 myData.cTitle = NULL;
434 g_free (myData.cAlbum);
435 myData.cAlbum = NULL;
436 g_free (myData.cArtist);
437 myData.cArtist = NULL;
438 g_free (myData.cCoverPath);
439 myData.cCoverPath = NULL;
440 myData.iSongLength = 0;
441 myData.iTrackNumber = 0;
442 myData.cover_exist = FALSE;
443 }
444
445 cd_musicplayer_update_icon ();
446 cd_musicplayer_relaunch_handler ();
447
448 CD_APPLET_LEAVE ();
449 }
450 static void cd_mpris2_getSongInfos_async (void)
451 {
452 if (s_pGetSongInfosCall != NULL)
453 return;
454 s_pGetSongInfosCall = dbus_g_proxy_begin_call (myData.dbus_proxy_player,
455 "Get",
456 (DBusGProxyCallNotify)_on_got_song_infos,
457 myApplet,
458 (GDestroyNotify) NULL,
459 G_TYPE_STRING, "org.mpris.MediaPlayer2.Player",
460 G_TYPE_STRING, "Metadata",
461 G_TYPE_INVALID);
462 }
463
464
465 ////////////////
466 // Callbacks. //
467 ////////////////
468
469 static void on_properties_changed (DBusGProxy *player_proxy, const gchar *cInterface, GHashTable *pChangedProps, const gchar **cInvalidProps, gpointer data)
470 {
471 g_return_if_fail (cInterface != NULL);
472 cd_debug ("");
473 GValue *v;
474 if (strcmp (cInterface, "org.mpris.MediaPlayer2.Player") == 0)
475 {
476 v = g_hash_table_lookup (pChangedProps, "PlaybackStatus");
477 if (v != NULL && G_VALUE_HOLDS_STRING (v)) // status has changed
478 {
479 const gchar *cStatus = g_value_get_string (v); // "Playing", "Paused" or "Stopped"
480 myData.iPlayingStatus = _extract_status (cStatus);
481 cd_debug ("PlaybackStatus: Status: %s, %d", cStatus, myData.iPlayingStatus);
482
483 if (myData.iPlayingStatus == PLAYER_PLAYING) // le handler est stoppe lorsque le lecteur ne joue rien.
484 cd_musicplayer_relaunch_handler ();
485
486 cd_musicplayer_update_icon ();
487 }
488
489 v = g_hash_table_lookup (pChangedProps, "Metadata");
490 if (v != NULL && G_VALUE_HOLDS_BOXED (v))
491 {
492 GHashTable *pMetadata = g_value_get_boxed (v);
493 gboolean bTrackHasChanged = _extract_metadata (pMetadata);
494
495 if (bTrackHasChanged) // new song (song changed or started playing)
496 {
497 myData.iPlayingStatus = PLAYER_PLAYING; // pour les lecteurs bugues comme Exaile qui envoit un statut "stop" au changement de musique sans envoyer de status "play" par la suite. On considere donc que si le lecteur joue une nouvelle musique, c'est qu'il est en "play".
498 cd_musicplayer_update_icon ();
499 }
500 }
501
502 v = g_hash_table_lookup (pChangedProps, "LoopStatus");
503 if (v != NULL && G_VALUE_HOLDS_STRING (v)) // loop status has changed
504 {
505 const gchar *cStatus = g_value_get_string (v); // "Playlist", "None"
506 s_bIsLoop = (cStatus && strcmp (cStatus, "Playlist") == 0);
507 cd_debug ("LoopStatus: %s, %d", cStatus, s_bIsLoop);
508 s_bGotLoopStatus = TRUE;
509 }
510
511 v = g_hash_table_lookup (pChangedProps, "Shuffle");
512 if (v != NULL && G_VALUE_HOLDS_BOOLEAN (v)) // Shuffle status has changed
513 {
514 s_bIsShuffle = g_value_get_boolean (v);
515 cd_debug ("Shuffle: %d", s_bIsShuffle);
516 s_bGotShuffleStatus = TRUE;
517 }
518 }
519 /*else if (strcmp (cInterface, "org.mpris.MediaPlayer2.TrackList") == 0)
520 {
521
522 }*/
523 else
524 cd_debug ("Another interface: %s", cInterface);
525 }
526
527 ////////////////////////
528 // backend definition //
529 ////////////////////////
530
531 /* Permet de liberer la memoire prise par le backend.
532 */
533 static void cd_mpris2_stop (void)
534 {
535 if (myData.dbus_proxy_player != NULL)
536 {
537 if (s_pGetSongInfosCall != NULL)
538 {
539 dbus_g_proxy_cancel_call (myData.dbus_proxy_player, s_pGetSongInfosCall);
540 s_pGetSongInfosCall = NULL;
541 }
542
543 if (s_pGetStatusCall != NULL)
544 {
545 dbus_g_proxy_cancel_call (myData.dbus_proxy_player, s_pGetStatusCall);
546 s_pGetStatusCall = NULL;
547 }
548 }
549 }
550
551
552 /* Controle du lecteur (permet d'effectuer les actions de bases sur le lecteur).
553 */
554 static void cd_mpris2_control (MyPlayerControl pControl, const char* song)
555 {
556 static GValue s_pValue = G_VALUE_INIT;
557 gboolean bToggleValue;
558 switch (pControl)
559 {
560 case PLAYER_PREVIOUS :
561 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Previous");
562 break;
563
564 case PLAYER_STOP :
565 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Stop");
566 break;
567
568 case PLAYER_PLAY_PAUSE :
569 if (myData.iPlayingStatus != PLAYER_PLAYING)
570 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Play");
571 else
572 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Pause");
573 break;
574
575 case PLAYER_NEXT :
576 cairo_dock_dbus_call (myData.dbus_proxy_shell, "Next");
577 break;
578
579 case PLAYER_JUMPBOX :
580
581 break;
582
583 case PLAYER_SHUFFLE :
584 bToggleValue = cd_mpris2_is_shuffle ();
585 cd_debug ("SetRandom <- %d", !bToggleValue);
586 g_value_init (&s_pValue, G_TYPE_BOOLEAN);
587 g_value_set_boolean (&s_pValue, !bToggleValue);
588 cairo_dock_dbus_set_property (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Shuffle", &s_pValue);
589 g_value_unset (&s_pValue);
590 break;
591
592 case PLAYER_REPEAT :
593 bToggleValue = cd_mpris2_is_loop ();
594 cd_debug ("SetLoop <- %d", !bToggleValue);
595 g_value_init (&s_pValue, G_TYPE_STRING);
596 g_value_set_static_string (&s_pValue, bToggleValue ? "None" : "Playlist");
597 cairo_dock_dbus_set_property (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "LoopStatus", &s_pValue);
598 g_value_unset (&s_pValue);
599 break;
600
601 case PLAYER_ENQUEUE :
602 {
603 cd_debug ("enqueue %s", song);
604 GError *erreur = NULL;
605 DBusGProxy *proxy = cairo_dock_create_new_session_proxy ("org.mpris.MediaPlayer2",
606 "/org/mpris/MediaPlayer2",
607 "org.mpris.MediaPlayer2.TrackList");
608 dbus_g_proxy_call (proxy, "AddTrack", &erreur,
609 G_TYPE_INVALID,
610 G_TYPE_STRING, song, // Uri
611 DBUS_TYPE_G_OBJECT_PATH, "", // AfterTrack
612 G_TYPE_BOOLEAN, TRUE, // SetAsCurrent
613 G_TYPE_INVALID);
614 g_object_unref (proxy);
615
616 if (erreur != NULL) // the TrackList interface may not exist.
617 {
618 g_error_free (erreur);
619 erreur = NULL;
620 dbus_g_proxy_call_no_reply (proxy, "OpenUri",
621 G_TYPE_STRING, song,
622 G_TYPE_INVALID);
623 }
624 }
625 break;
626
627 case PLAYER_VOLUME :
628 {
629 double fVolume = cd_mpris2_get_volume (); // [0, 1]
630 if (song && strcmp (song, "up") == 0)
631 fVolume += .05;
632 else
633 fVolume -= .05;
634 if (fVolume > 1) fVolume = 1;
635 if (fVolume < 0) fVolume = 0;
636 cd_debug ("volume <- %f", fVolume);
637 g_value_init (&s_pValue, G_TYPE_DOUBLE);
638 g_value_set_double (&s_pValue, fVolume);
639 cairo_dock_dbus_set_property (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Volume", &s_pValue);
640 g_value_unset (&s_pValue);
641 }
642
643 default :
644 break;
645 }
646 }
647
648
649 /* Recupere le temps ecoule chaque seconde (pas de signal pour ca).
650 */
651 static void cd_mpris2_get_data (void)
652 {
653 if (myData.iPlayingStatus == PLAYER_PLAYING)
654 {
655 cd_mpris2_get_time_elapsed ();
656 if (myData.iCurrentTime < 0) // aucune info de temps sur le bus => lecteur ferme.
657 {
658 myData.iGetTimeFailed ++; // certains lecteurs (qmmp par exemple) envoient le signal 'playing' trop tot lorsqu'on les relance, ils ne fournissent pas de duree tout de suite, et donc l'applet stoppe. On fait donc 3 tentatives avant de declarer le lecteur ferme.
659 cd_debug ("failed to get time %d time(s)", myData.iGetTimeFailed);
660 if (myData.iGetTimeFailed > 2)
661 {
662 cd_debug (" => player is likely closed");
663 myData.iPlayingStatus = PLAYER_NONE;
664 myData.iCurrentTime = -2; // le temps etait a -1, on le change pour provoquer un redraw.
665 }
666 }
667 else
668 myData.iGetTimeFailed = 0;
669 }
670 else if (myData.iPlayingStatus != PLAYER_PAUSED) // en pause le temps reste constant.
671 {
672 myData.iCurrentTime = 0;
673 myData.iGetTimeFailed = 0;
674 }
675 }
676
677 static void _cd_cclosure_marshal_VOID__STRING_HASH_STRV (GClosure *closure,
678 GValue *return_value G_GNUC_UNUSED,
679 guint n_param_values,
680 const GValue *param_values,
681 gpointer invocation_hint G_GNUC_UNUSED,
682 gpointer marshal_data)
683 {
684 typedef void (*GMarshalFunc_VOID__STRING_HASH_STRV) (
685 gpointer data1,
686 gchar *arg_1,
687 GHashTable *arg_2,
688 gchar **arg_3,
689 gpointer data2);
690 register GMarshalFunc_VOID__STRING_HASH_STRV callback;
691 register GCClosure *cc = (GCClosure*) closure;
692 register gpointer data1, data2;
693 g_return_if_fail (n_param_values == 4); // return_value est NULL ici, car la callback ne renvoit rien.
694
695 if (G_CCLOSURE_SWAP_DATA (closure))
696 {
697 data1 = closure->data;
698 data2 = g_value_peek_pointer (param_values + 0);
699 }
700 else
701 {
702 data1 = g_value_peek_pointer (param_values + 0);
703 data2 = closure->data;
704 }
705 callback = (GMarshalFunc_VOID__STRING_HASH_STRV) (marshal_data ? marshal_data : cc->callback);
706
707 g_return_if_fail (callback != NULL);
708 g_return_if_fail (G_VALUE_HOLDS_STRING (param_values + 1));
709 g_return_if_fail (G_VALUE_HOLDS_BOXED (param_values + 2));
710 g_return_if_fail (G_VALUE_HOLDS (param_values + 3, G_TYPE_STRV));
711
712 callback (data1,
713 (char*) g_value_get_string (param_values + 1),
714 (GHashTable*) g_value_get_boxed (param_values + 2),
715 (char**) g_value_get_boxed (param_values + 3),
716 data2);
717 }
718
719 static void cd_mpris2_start (void)
720 {
721 // register to the signals
722 cd_debug ("%s ()", __func__);
723 dbus_g_object_register_marshaller (_cd_cclosure_marshal_VOID__STRING_HASH_STRV,
724 G_TYPE_NONE,
725 G_TYPE_STRING,
726 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
727 G_TYPE_STRV,
728 G_TYPE_INVALID);
729 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "PropertiesChanged",
730 G_TYPE_STRING, // interface
731 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), // dict of (property, new value)
732 G_TYPE_STRV, // array of properties unvalidated
733 G_TYPE_INVALID);
734 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "PropertiesChanged",
735 G_CALLBACK(on_properties_changed), NULL, NULL);
736
737 // get the current state.
738 myData.iTrackListLength = 0;
739 myData.iTrackListIndex = 0;
740 s_bGotLoopStatus = FALSE;
741 s_bGotShuffleStatus = FALSE;
742 cd_mpris2_getPlaying_async (); // will get song infos after playing status.
743 }
744
745
746 void cd_musicplayer_register_mpris2_handler (void)
747 {
748 MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1);
749 pHandler->name = "Mpris2";
750 pHandler->get_data = cd_mpris2_get_data;
751 pHandler->stop = cd_mpris2_stop;
752 pHandler->start = cd_mpris2_start;
753 pHandler->control = cd_mpris2_control;
754 pHandler->get_loop_status = get_loop_status;
755 pHandler->get_shuffle_status = get_shuffle_status;
756 pHandler->raise = _raise;
757 pHandler->quit = _quit;
758 pHandler->bSeparateAcquisition = FALSE;
759 pHandler->iLevel = PLAYER_GOOD;
760
761 pHandler->cMprisService = NULL; // service is left NULL until an actual MPRIS2 player is present.
762 pHandler->path = "/org/mpris/MediaPlayer2";
763 pHandler->interface = DBUS_INTERFACE_PROPERTIES; // "org.mpris.MediaPlayer2.Player"
764 pHandler->path2 = "/org/mpris/MediaPlayer2";
765 pHandler->interface2 = "org.mpris.MediaPlayer2.Player";
766
767 pHandler->appclass = NULL; // will be filled later.
768 pHandler->launch = NULL; // will be filled later.
769 pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT | PLAYER_STOP | PLAYER_SHUFFLE | PLAYER_REPEAT | PLAYER_ENQUEUE | PLAYER_VOLUME;
770 cd_musicplayer_register_my_handler (pHandler);
771 }