Debian Codegraph @

musicPlayer/src/applet-banshee.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-banshee.h"
32
33 /*
34 service : org.bansheeproject.Banshee
35
36 <interface name="org.bansheeproject.Banshee.PlayerEngine">
37 <method name="Open">
38 <arg name="uri" direction="in" type="s" />
39 </method>
40 <method name="Close" />
41 <method name="Pause" />
42 <method name="Play" />
43 <method name="TogglePlaying" />
44 <signal name="EventChanged">
45 <arg name="evnt" direction="out" type="s" />
46 <arg name="message" direction="out" type="s" />
47 <arg name="bufferingPercent" direction="out" type="d" />
48 </signal>
49 <signal name="StateChanged">
50 <arg name="state" direction="out" type="s" />
51 </signal>
52 <property name="CurrentTrack" type="a{sv}" access="read" />
53 <property name="CurrentUri" type="s" access="read" />
54 <property name="CurrentState" type="s" access="read" />
55 <property name="LastState" type="s" access="read" />
56 <property name="Volume" type="q" access="readwrite" />
57 <property name="Position" type="u" access="readwrite" />
58 <property name="CanSeek" type="b" access="read" />
59 <property name="CanPause" type="b" access="read" />
60 <property name="Length" type="u" access="read" />
61 </interface>
62
63 interface : org.bansheeproject.Banshee.PlaybackController
64 path : /org/bansheeproject/Banshee/PlaybackController
65 Next
66 Previous
67 PlaybackShuffleMode ShuffleMode { get; set; }
68 PlaybackRepeatMode RepeatMode { get; set; }
69
70 interface : org.bansheeproject.Banshee.PlayerEngine
71 path : /org/bansheeproject/Banshee/PlayerEngine
72 event DBusPlayerEventHandler EventChanged;
73 event DBusPlayerStateHandler StateChanged;
74
75 void Open (string uri);
76
77 void Pause ();
78 void Play ();
79 void TogglePlaying ();
80
81 IDictionary CurrentTrack { get; } artist,name,album,track-number
82 string CurrentUri { get; }
83
84 string CurrentState { get; }
85
86 uint Position { get; set; }
87 uint Length { get; }
88
89 cover cache : "$HOME/.cache/album-art/`GetCurrentTrack["artwork-id"])`.jpg"
90 */
91
92
93 static inline gboolean _extract_playing_status (const gchar *cCurrentState)
94 {
95 myData.pPreviousPlayingStatus = myData.iPlayingStatus;
96 if (cCurrentState == NULL)
97 return FALSE;
98 if (strcmp (cCurrentState, "playing") == 0)
99 myData.iPlayingStatus = PLAYER_PLAYING;
100 else if (strcmp (cCurrentState, "paused") == 0)
101 myData.iPlayingStatus = PLAYER_PAUSED;
102 else if (strcmp (cCurrentState, "idle") == 0)
103 {
104 myData.iPlayingStatus = PLAYER_STOPPED;
105 return FALSE; // on ne redessine pas ici, on va attendre la prochaine iteration de read_data. En effet, le lecteur passe par l'etat "idle" a chaque chgt de chanson, alors qu'il n'est manifestement pas a l'arret.
106 }
107 else // loading, loaded, etc => on considere que le lecteur va jouer la chanson des qu'il aura fini de la charger, on se place donc direct en mode "playing".
108 {
109 myData.iPlayingStatus = PLAYER_PLAYING;
110 return FALSE; // inutile de redessiner pour ces etats.
111 }
112 return TRUE;
113 }
114
115 /* Teste si Audacious joue de la musique ou non
116 */
117 static void _banshee_getPlaying (void)
118 {
119 cd_message ("");
120 gchar *cCurrentState = cairo_dock_dbus_get_string (myData.dbus_proxy_player, "GetCurrentState");
121 _extract_playing_status (cCurrentState);
122 g_free (cCurrentState);
123 }
124
125 /* Renvoie le temps ecoule en secondes..
126 */
127 static void _banshee_get_time_elapsed (void)
128 {
129 cd_message ("");
130 myData.iCurrentTime = cairo_dock_dbus_get_uinteger (myData.dbus_proxy_player, "GetPosition") / 1000;
131 }
132
133 static inline void _extract_metadata (GHashTable *data_list)
134 {
135 GValue *value;
136 g_free (myData.cArtist);
137 value = (GValue *) g_hash_table_lookup (data_list, "artist");
138 if (value != NULL && G_VALUE_HOLDS_STRING (value))
139 myData.cArtist = g_strdup (g_value_get_string(value));
140 else
141 myData.cArtist = NULL;
142
143 g_free (myData.cAlbum);
144 value = (GValue *) g_hash_table_lookup (data_list, "album");
145 if (value != NULL && G_VALUE_HOLDS_STRING (value))
146 myData.cAlbum = g_strdup (g_value_get_string(value));
147 else
148 myData.cAlbum = NULL;
149
150 g_free (myData.cTitle);
151 value = (GValue *) g_hash_table_lookup (data_list, "name");
152 if (value != NULL && G_VALUE_HOLDS_STRING (value))
153 myData.cTitle = g_strdup (g_value_get_string (value));
154 else
155 myData.cTitle = NULL;
156
157 value = (GValue *) g_hash_table_lookup(data_list, "track-number");
158 if (value != NULL && G_VALUE_HOLDS_INT(value))
159 myData.iTrackNumber = g_value_get_int(value);
160 else
161 myData.iTrackNumber = 0;
162
163 value = (GValue *) g_hash_table_lookup(data_list, "length");
164 if (value != NULL && G_VALUE_HOLDS_INT(value))
165 myData.iSongLength = g_value_get_int(value);
166 else
167 myData.iSongLength = cairo_dock_dbus_get_uinteger (myData.dbus_proxy_player, "GetLength") / 1000;
168
169 g_free (myData.cPlayingUri);
170 myData.cPlayingUri = cairo_dock_dbus_get_string (myData.dbus_proxy_player, "GetCurrentUri");
171
172 const gchar *cString = NULL;
173 value = (GValue *) g_hash_table_lookup(data_list, "artwork-id");
174 if (value != NULL && G_VALUE_HOLDS_STRING(value))
175 cString = g_value_get_string(value);
176 cd_debug ("MP : got cover path from Banshee : '%s'", cString);
177 gchar *cCoverPath = (cString ? g_strdup_printf ("%s/.cache/album-art/%s.jpg", g_getenv ("HOME"), cString) : NULL);
178 if (cString && ! g_file_test (cCoverPath, G_FILE_TEST_EXISTS)) // path has changed :-/
179 {
180 g_free (cCoverPath);
181 cCoverPath = g_strdup_printf ("%s/.cache/media-art/%s.jpg", g_getenv ("HOME"), cString);
182 }
183 cd_musicplayer_set_cover_path (cCoverPath);
184 g_free (cCoverPath);
185 }
186
187 /* Recupere les infos de la chanson courante, y compris le chemin de la couverture (la telecharge au besoin).
188 */
189 static void cd_banshee_getSongInfos (void)
190 {
191 GHashTable *data_list = NULL;
192
193 if (dbus_g_proxy_call (myData.dbus_proxy_player, "GetCurrentTrack", NULL, G_TYPE_INVALID,
194 MP_DBUS_TYPE_SONG_METADATA,
195 &data_list,
196 G_TYPE_INVALID))
197 {
198 _extract_metadata (data_list);
199
200 g_hash_table_destroy (data_list);
201 }
202 else
203 {
204 cd_warning (" can't get song properties");
205 g_free (myData.cPlayingUri);
206 myData.cPlayingUri = NULL;
207 g_free (myData.cTitle);
208 myData.cTitle = NULL;
209 g_free (myData.cAlbum);
210 myData.cAlbum = NULL;
211 g_free (myData.cCoverPath);
212 myData.cCoverPath = NULL;
213 }
214 myData.cRawTitle = g_strdup_printf ("%s - %s", myData.cArtist, myData.cTitle);
215 }
216
217 static void cd_banshee_getCoverPath (void)
218 {
219 cd_debug ("MP - %s ()", __func__);
220 GHashTable *data_list = NULL;
221 GValue *value;
222 GError *erreur = NULL;
223
224 dbus_g_proxy_call (myData.dbus_proxy_player, "GetCurrentTrack", &erreur, G_TYPE_INVALID,
225 MP_DBUS_TYPE_SONG_METADATA,
226 &data_list,
227 G_TYPE_INVALID);
228 if (erreur != NULL)
229 {
230 cd_warning (erreur->message);
231 g_error_free (erreur);
232 myData.iPlayingStatus = PLAYER_NONE;
233 }
234 else
235 {
236 const gchar *cString = NULL;
237 value = (GValue *) g_hash_table_lookup(data_list, "artwork-id");
238 if (value != NULL && G_VALUE_HOLDS_STRING(value))
239 cString = g_value_get_string(value);
240 cd_debug ("MP - => got cover path from Banshee : '%s'", cString);
241 gchar *cCoverPath = g_strdup_printf ("%s/.cache/album-art/300/%s.jpg", g_getenv ("HOME"), cString);
242 if (! g_file_test (cCoverPath, G_FILE_TEST_EXISTS))
243 {
244 g_free (cCoverPath);
245 cCoverPath = g_strdup_printf ("%s/.cache/album-art/%s.jpg", g_getenv ("HOME"), cString);
246 if (! g_file_test (cCoverPath, G_FILE_TEST_EXISTS)) // path has changed :-/
247 {
248 g_free (cCoverPath);
249 cCoverPath = g_strdup_printf ("%s/.cache/media-art/300/%s.jpg", g_getenv ("HOME"), cString);
250 if (! g_file_test (cCoverPath, G_FILE_TEST_EXISTS))
251 {
252 g_free (cCoverPath);
253 cCoverPath = g_strdup_printf ("%s/.cache/media-art/%s.jpg", g_getenv ("HOME"), cString);
254 }
255 }
256 }
257 cd_musicplayer_set_cover_path (cString);
258 g_free (cCoverPath);
259 g_hash_table_destroy (data_list);
260 }
261 }
262 /* Fonction executée à chaque changement de musique.
263 */
264 static void onChangeSong(DBusGProxy *player_proxy, const gchar *cEvent, const gchar *cMessage, double fBufferingPercent, gpointer data)
265 {
266 CD_APPLET_ENTER;
267 cd_debug ("MP : %s (%s, %s, %.2f)", __func__, cEvent, cMessage, fBufferingPercent);
268 if (cMessage != NULL)
269 {
270 if (strcmp (cMessage, "startofstream") == 0) // new song
271 {
272 cd_banshee_getSongInfos ();
273 }
274 else if (strcmp (cMessage, "trackinfoupdated") == 0) // song update, in practice only the cover can be updated.
275 {
276 cd_debug ("MP - trackinfoupdated\n");
277 if (myData.cCoverPath == NULL) // the player didn't provide a cover the first time, see if it does now.
278 {
279 cd_debug ("MP - aucune pochette, on regarde si Banshee a mieux a nous proposer\n");
280 cd_banshee_getCoverPath ();
281 CD_APPLET_LEAVE ();
282 }
283 }
284 else
285 CD_APPLET_LEAVE ();
286 }
287 else
288 {
289 cd_debug ("MP - message vide !\n");
290 g_free (myData.cPlayingUri);
291 myData.cPlayingUri = NULL;
292 g_free (myData.cArtist);
293 myData.cArtist = NULL;
294 g_free (myData.cAlbum);
295 myData.cAlbum = NULL;
296 g_free (myData.cTitle);
297 myData.cTitle = NULL;
298 g_free (myData.cCoverPath);
299 myData.cCoverPath = NULL;
300 myData.iSongLength = 0;
301 myData.iTrackNumber = 0;
302 myData.cover_exist = FALSE;
303 }
304 cd_musicplayer_update_icon ();
305 CD_APPLET_LEAVE ();
306 }
307
308 static void g_cclosure_marshal_VOID__STRING_STRING_DOUBLE (GClosure *closure,
309 GValue *return_value,
310 guint n_param_values,
311 const GValue *param_values,
312 gpointer invocation_hint,
313 gpointer marshal_data)
314 {
315 cd_debug ("MP - %s ()", __func__);
316 const GValue *value;
317 const gchar *cEvent = NULL;
318 const gchar *cMessage = NULL;
319 double fBufferingPercent=0.;
320
321 value = &param_values[0];
322 if (value != NULL && G_VALUE_HOLDS_STRING (value))
323 cEvent = g_value_get_string (value);
324
325 value = &param_values[1];
326 if (value != NULL && G_VALUE_HOLDS_STRING (value))
327 cMessage = g_value_get_string (value);
328
329 value = &param_values[2];
330 if (value != NULL && G_VALUE_HOLDS_DOUBLE (value))
331 fBufferingPercent = g_value_get_double (value);
332
333 onChangeSong (NULL, cEvent, cMessage, fBufferingPercent, marshal_data);
334 }
335
336
337 /* Fonction executée à chaque changement play/pause
338 */
339 static void onChangePlaying(DBusGProxy *player_proxy, const gchar *cCurrentStatus, gpointer data)
340 {
341 CD_APPLET_ENTER;
342 cd_debug ("MP : %s (%s)", __func__, cCurrentStatus);
343 gboolean bStateChanged = _extract_playing_status (cCurrentStatus);
344 if (! bStateChanged)
345 CD_APPLET_LEAVE ();
346 //return ;
347
348 if (myData.iPlayingStatus == PLAYER_PLAYING)
349 cd_musicplayer_relaunch_handler ();
350 if(! myData.cover_exist && myData.cPlayingUri != NULL)
351 {
352 cd_musicplayer_apply_status_surface (myData.iPlayingStatus);
353 }
354 else
355 {
356 CD_APPLET_REDRAW_MY_ICON;
357 }
358 CD_APPLET_LEAVE ();
359 }
360
361
362
363 ////////////////////////////
364 // Definition du backend. //
365 ////////////////////////////
366
367 /* Controle du lecteur.
368 */
369 static void cd_banshee_control (MyPlayerControl pControl, const char *file)
370 {
371 switch (pControl)
372 {
373 case PLAYER_PREVIOUS :
374 dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "Previous",
375 G_TYPE_BOOLEAN, FALSE, // FALSE <=> dont play it now
376 G_TYPE_INVALID,
377 G_TYPE_INVALID);
378 break;
379
380 case PLAYER_PLAY_PAUSE :
381 cairo_dock_dbus_call (myData.dbus_proxy_player, "TogglePlaying");
382 break;
383
384 case PLAYER_NEXT :
385 dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "Next",
386 G_TYPE_BOOLEAN, FALSE, // FALSE <=> dont play it now
387 G_TYPE_INVALID,
388 G_TYPE_INVALID);
389 break;
390
391 case PLAYER_SHUFFLE :
392 {
393 gboolean bShuffle = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetShuffleMode");
394 cd_debug ("MP - bShuffle : %d", bShuffle);
395 dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "SetShuffleMode",
396 G_TYPE_INT, ! bShuffle,
397 G_TYPE_INVALID,
398 G_TYPE_INVALID);
399 }
400 break;
401
402 case PLAYER_REPEAT :
403 {
404 int iRepeat = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetRepeatMode");
405 cd_debug ("MP - iRepeat : %d", iRepeat);
406 dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "SetRepeatMode",
407 G_TYPE_INT, (iRepeat+1)%3,
408 G_TYPE_INVALID,
409 G_TYPE_INVALID);
410 }
411 break;
412
413 default :
414 return;
415 }
416 }
417
418
419 /* Recupere le temps ecoule chaque seconde (pas de signal pour ca).
420 */
421 static void cd_banshee_get_data (void)
422 {
423 if (myData.iPlayingStatus == PLAYER_PLAYING)
424 {
425 _banshee_get_time_elapsed();
426 if (myData.iCurrentTime < 0)
427 myData.iPlayingStatus = PLAYER_STOPPED;
428 }
429 else if (myData.iPlayingStatus != PLAYER_PAUSED) // en pause le temps reste constant.
430 {
431 myData.iCurrentTime = 0;
432 if (myData.iPlayingStatus == PLAYER_STOPPED && myData.pPreviousPlayingStatus != PLAYER_STOPPED) /// utile ?...
433 {
434 myData.pPreviousPlayingStatus = PLAYER_STOPPED;
435 cd_musicplayer_apply_status_surface (PLAYER_NONE);
436 g_free (myData.cCoverPath);
437 myData.cCoverPath = NULL;
438 }
439 }
440 }
441
442 /* Initialise le backend de BA.
443 */
444 static void cd_banshee_start (void)
445 {
446 // register to the signals
447 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "StateChanged",
448 G_TYPE_STRING,
449 G_TYPE_INVALID);
450 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "StateChanged",
451 G_CALLBACK(onChangePlaying), NULL, NULL);
452
453 dbus_g_object_register_marshaller(g_cclosure_marshal_VOID__STRING_STRING_DOUBLE,
454 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE ,G_TYPE_INVALID);
455 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "EventChanged",
456 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, // MP_DBUS_TYPE_SONG_METADATA
457 G_TYPE_INVALID);
458 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "EventChanged",
459 G_CALLBACK(onChangeSong), NULL, NULL);
460
461 // get the current state.
462 _banshee_getPlaying ();
463 cd_banshee_getSongInfos ();
464 cd_musicplayer_update_icon ();
465 }
466
467 void cd_musicplayer_register_banshee_handler (void)
468 {
469 MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1);
470 pHandler->name = "Banshee";
471 pHandler->get_data = cd_banshee_get_data; // recupere le temps ecoule car on n'a pas de signal pour ca.
472 pHandler->stop = NULL;
473 pHandler->start = cd_banshee_start; // renseigne les proprietes DBus et se connecte au bus.
474 pHandler->control = cd_banshee_control;
475 pHandler->get_cover = NULL;
476 pHandler->cCoverDir = g_strdup_printf ("%s/.cache/media-art", g_getenv ("HOME"));
477
478 pHandler->cMprisService = "org.bansheeproject.Banshee";
479 pHandler->path = "/org/bansheeproject/Banshee/PlaybackController";
480 pHandler->interface = "org.bansheeproject.Banshee.PlaybackController";
481 pHandler->path2 = "/org/bansheeproject/Banshee/PlayerEngine";
482 pHandler->interface2 = "org.bansheeproject.Banshee.PlayerEngine";
483
484 pHandler->appclass = "banshee"; // en fait la vraie classe est plus compliquee (Mono oblige), mais le dock sait extraire ca.
485 pHandler->launch = "banshee";
486 pHandler->cMpris2Service = "org.mpris.MediaPlayer2.banshee";
487 pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT | PLAYER_SHUFFLE | PLAYER_REPEAT | PLAYER_ENQUEUE;
488 pHandler->bSeparateAcquisition = FALSE;
489 pHandler->iLevel = PLAYER_GOOD; // n'a besoin d'une boucle que pour afficher le temps ecoule.
490 cd_musicplayer_register_my_handler (pHandler);
491 }
492