Debian Codegraph @

musicPlayer/src/applet-quodlibet.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
27 #include "applet-struct.h"
28 #include "applet-musicplayer.h"
29 #include "applet-dbus.h"
30 #include "applet-cover.h"
31 #include "applet-draw.h"
32 #include "applet-quodlibet.h"
33
34 /* cat ~/.quodlibet/current
35 comment=#NIPPONSEI @ IRC.RIZON.NET
36 album=Danzai no Hana ~Guilty Sky~
37 ~#playcount=3
38 ~#bitrate=320000
39 artist=Kosaka Riyu
40 ~#length=261
41 title=Danzai no Hana ~Guilty Sky~
42 ~#rating=0.500000
43 genre=Anime
44 ~#added=1250552346
45 ~filename=/media/mecenie/Claymore_[P-L]/ED_Single/01 - Danzai no Hana ~Guilty Sky~.mp3
46 ~#skipcount=0
47 ~#mtime=1180436036.000000
48 date=2007
49 tracknumber=1
50 ~mountpoint=/media/mecenie
51 ~#laststarted=1250553619
52 ~#lastplayed=1250553881
53 ~format=MP3
54 */
55
56 #define MP_DBUS_TYPE_PLAYER_STATUS G_TYPE_BOOLEAN
57 #define QL_DBUS_TYPE_SONG_METADATA dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)
58 /////////////////////////////////
59 // Les Fonctions propres a QL. //
60 /////////////////////////////////
61
62 static inline void _extract_playing_status (gboolean status)
63 {
64 cd_debug ("%s (%d)", __func__, status);
65
66 if (status)
67 myData.iPlayingStatus = PLAYER_PLAYING;
68 else
69 myData.iPlayingStatus = PLAYER_PAUSED;
70 }
71
72 static void _quodlibet_getPlaying (void)
73 {
74 cd_debug ("%s ()", __func__);
75 GError *erreur = NULL;
76 gboolean status;
77 dbus_g_proxy_call (myData.dbus_proxy_player, "IsPlaying", &erreur,
78 G_TYPE_INVALID,
79 MP_DBUS_TYPE_PLAYER_STATUS, &status,
80 G_TYPE_INVALID);
81 if (erreur != NULL)
82 {
83 cd_warning (erreur->message);
84 g_error_free (erreur);
85 myData.iPlayingStatus = PLAYER_NONE;
86 }
87 else
88 {
89 _extract_playing_status (status);
90 }
91 }
92
93 /* Renvoie le temps ecoule en secondes.
94 */
95 static gint64 cairo_dock_dbus_get_integer64 (DBusGProxy *pDbusProxy, const gchar *cAccessor)
96 {
97 GError *erreur = NULL;
98 gint64 iValue = 0;
99 dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
100 G_TYPE_INVALID,
101 G_TYPE_INT64, &iValue,
102 G_TYPE_INVALID);
103 if (erreur != NULL)
104 {
105 cd_warning (erreur->message);
106 g_error_free (erreur);
107 }
108 return iValue;
109 }
110 static void _quodlibet_get_time_elapsed (void)
111 {
112 myData.iCurrentTime = cairo_dock_dbus_get_integer64 (myData.dbus_proxy_player, "GetPosition")/1000;
113 cd_debug ("MP : current_position <- %i", myData.iCurrentTime);
114 }
115
116 static inline void _extract_metadata (GHashTable *data_list) // album, date, discnumber, discsubtitle, tracknumber, location, organization, labelid, producer, ~filename, ~#length, ~#bitrate, ~#track, ~#disc
117 {
118 const gchar *value;
119 g_free (myData.cArtist);
120 value = (const char *) g_hash_table_lookup (data_list, "artist");
121 if (value != NULL)
122 myData.cArtist = g_strdup (value);
123 else
124 myData.cArtist = NULL;
125 cd_debug (" MP : playing_artist <- '%s'", myData.cArtist);
126
127 g_free (myData.cAlbum);
128 value = (const char *) g_hash_table_lookup (data_list, "album");
129 if (value != NULL)
130 myData.cAlbum = g_strdup (value);
131 else
132 myData.cAlbum = NULL;
133 cd_debug (" MP : playing_album <- '%s'", myData.cAlbum);
134
135 g_free (myData.cTitle);
136 value = (const char *) g_hash_table_lookup (data_list, "title");
137 if (value != NULL)
138 myData.cTitle = g_strdup (value);
139 else
140 myData.cTitle = NULL;
141 cd_debug (" MP : playing_title <- '%s'", myData.cTitle);
142
143 value = (const char *) g_hash_table_lookup (data_list, "tracknumber"); // ~#track ?
144 cd_debug ("MP : tracknumber : '%s'", value);
145 if (value != NULL)
146 myData.iTrackNumber = atoll (value);
147 else
148 myData.iTrackNumber = 0;
149 cd_debug (" MP : playing_track <- %d", myData.iTrackNumber);
150
151 value = (const char *) g_hash_table_lookup (data_list, "~#length");
152 cd_debug ("MP : ~#length : '%s'", value);
153 if (value != NULL)
154 myData.iSongLength = atoll (value);
155 else
156 myData.iSongLength = 0;
157 cd_debug (" MP : playing_duration <- %d", myData.iSongLength);
158
159 g_free (myData.cPlayingUri);
160 value = (const char *) g_hash_table_lookup (data_list, "~filename"); // location ?
161 if (value != NULL)
162 myData.cPlayingUri = g_strdup (value);
163 else
164 myData.cPlayingUri = NULL;
165 cd_debug (" cUri <- %s", myData.cPlayingUri);
166
167 cd_musicplayer_set_cover_path (NULL);
168 }
169
170 static void cd_quodlibet_getSongInfos (void)
171 {
172 GHashTable *data_list = NULL;
173
174 if (dbus_g_proxy_call (myData.dbus_proxy_player, "CurrentSong", NULL,
175 G_TYPE_INVALID,
176 QL_DBUS_TYPE_SONG_METADATA,
177 &data_list,
178 G_TYPE_INVALID))
179 {
180 _extract_metadata (data_list);
181
182 g_hash_table_destroy (data_list);
183 }
184 else
185 {
186 cd_warning ("MP : Can't get song properties");
187 g_free (myData.cPlayingUri);
188 myData.cPlayingUri = NULL;
189 g_free (myData.cTitle);
190 myData.cTitle = NULL;
191 g_free (myData.cAlbum);
192 myData.cAlbum = NULL;
193 g_free (myData.cArtist);
194 myData.cArtist = NULL;
195 g_free (myData.cCoverPath);
196 myData.cCoverPath = NULL;
197 myData.iSongLength = 0;
198 myData.iTrackNumber = 0;
199 myData.cover_exist = FALSE;
200 }
201 }
202
203
204 /////////////////////////////////////
205 // Les callbacks des signaux DBus. //
206 /////////////////////////////////////
207
208 /* Fonction executee à chaque changement de musique.
209 */
210 static void onChangeSong(DBusGProxy *player_proxy, GHashTable *metadata, gpointer data)
211 {
212 CD_APPLET_ENTER;
213 cd_debug ("MP : %s ()", __func__);
214
215 if (metadata != NULL)
216 {
217 _extract_metadata (metadata);
218 }
219 else
220 {
221 g_free (myData.cPlayingUri);
222 myData.cPlayingUri = NULL;
223 g_free (myData.cArtist);
224 myData.cArtist = NULL;
225 g_free (myData.cAlbum);
226 myData.cAlbum = NULL;
227 g_free (myData.cTitle);
228 myData.cTitle = NULL;
229 g_free (myData.cCoverPath);
230 myData.cCoverPath = NULL;
231 myData.iSongLength = 0;
232 myData.iTrackNumber = 0;
233 myData.cover_exist = FALSE;
234 }
235 cd_musicplayer_update_icon ();
236 CD_APPLET_LEAVE ();
237 }
238
239 /* Fonction executee à chaque changement "paused".
240 */
241 static void on_pause (DBusGProxy *player_proxy, gpointer data) // paused
242 {
243 CD_APPLET_ENTER;
244 cd_debug ("MP : %s ()", __func__);
245
246 myData.iPlayingStatus = PLAYER_PAUSED;
247
248 if(! myData.cover_exist && (myData.cPlayingUri != NULL || myData.cTitle != NULL))
249 {
250 if(myData.iPlayingStatus == PLAYER_PLAYING)
251 {
252 cd_musicplayer_apply_status_surface (PLAYER_PLAYING);
253 }
254 else
255 {
256 cd_musicplayer_apply_status_surface (PLAYER_PAUSED);
257 }
258 }
259 else
260 {
261 CD_APPLET_REDRAW_MY_ICON;
262 }
263 CD_APPLET_LEAVE ();
264 }
265 /* Fonction executee à chaque changement "unpaused".
266 */
267 static void on_unpaused (DBusGProxy *player_proxy, gpointer data) // unpaused
268 {
269 CD_APPLET_ENTER;
270 cd_debug ("MP : %s ()", __func__);
271
272 myData.iPlayingStatus = PLAYER_PLAYING;
273
274 cd_musicplayer_relaunch_handler ();
275 if(! myData.cover_exist && (myData.cPlayingUri != NULL || myData.cTitle != NULL))
276 {
277 cd_musicplayer_apply_status_surface (myData.iPlayingStatus);
278 }
279 else
280 {
281 CD_APPLET_REDRAW_MY_ICON;
282 }
283 CD_APPLET_LEAVE ();
284 }
285
286 ////////////////////////////
287 // Definition du backend. //
288 ////////////////////////////
289
290 /* Controle du lecteur
291 */
292 static void cd_quodlibet_control (MyPlayerControl pControl, const char* song)
293 {
294 const gchar *cCommand = NULL;
295
296 switch (pControl) {
297 case PLAYER_PREVIOUS :
298 cCommand = "Previous";
299 break;
300
301 case PLAYER_PLAY_PAUSE :
302 cCommand = "PlayPause";
303 break;
304
305 case PLAYER_NEXT :
306 cCommand = "Next";
307 break;
308
309 default :
310 return;
311 break;
312 }
313
314 if (cCommand != NULL)
315 {
316 cd_debug ("MP : Handler QuodLibet : will use '%s'", cCommand);
317 cairo_dock_dbus_call (myData.dbus_proxy_player, cCommand);
318 }
319 }
320
321 /* Recupere le temps ecoule chaque seconde (pas de signal pour ca).
322 */
323 static void cd_quodlibet_get_data (void)
324 {
325 if (myData.iPlayingStatus == PLAYER_PLAYING)
326 {
327 _quodlibet_get_time_elapsed ();
328 if (myData.iCurrentTime < 0)
329 myData.iPlayingStatus = PLAYER_STOPPED;
330 }
331 else if (myData.iPlayingStatus != PLAYER_PAUSED) // en pause le temps reste constant.
332 myData.iCurrentTime = 0;
333 }
334
335 /* Initialise le backend de QL.
336 */
337 static void cd_quodlibet_start (void)
338 {
339 // register to the signals
340 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "paused",
341 G_TYPE_INVALID);
342 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "paused",
343 G_CALLBACK(on_pause), NULL, NULL);
344
345 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "unpaused",
346 G_TYPE_NONE,
347 G_TYPE_INVALID); // idem.
348 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "unpaused",
349 G_CALLBACK(on_unpaused), NULL, NULL);
350
351 dbus_g_proxy_add_signal(myData.dbus_proxy_player, "song-started",
352 QL_DBUS_TYPE_SONG_METADATA,
353 G_TYPE_INVALID);
354 dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "song-started",
355 G_CALLBACK(onChangeSong), NULL, NULL);
356
357 // get the current state.
358 _quodlibet_getPlaying ();
359 cd_quodlibet_getSongInfos ();
360 cd_musicplayer_update_icon ();
361 }
362
363
364 /* On enregistre notre lecteur.
365 */
366 void cd_musicplayer_register_quodlibet_handler (void)
367 {
368 MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1);
369 pHandler->name = "QuodLibet";
370 pHandler->get_data = cd_quodlibet_get_data;
371 pHandler->stop = NULL;
372 pHandler->start = cd_quodlibet_start;
373 pHandler->control = cd_quodlibet_control;
374 pHandler->get_cover = NULL;
375 pHandler->cCoverDir = NULL; /// il me semble que QL gere les pochettes ...
376
377 pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT;
378 pHandler->appclass = "quodlibet";
379 pHandler->launch = "quodlibet";
380 pHandler->cMprisService = "net.sacredchao.QuodLibet";
381 pHandler->cMpris2Service = "org.mpris.MediaPlayer2.quodlibet";
382 pHandler->path = "/net/sacredchao/QuodLibet";
383 pHandler->interface = "net.sacredchao.QuodLibet";
384 pHandler->path2 = NULL;
385 pHandler->interface2 = NULL;
386
387 pHandler->bSeparateAcquisition = FALSE;
388 pHandler->iLevel = PLAYER_GOOD; // n'a besoin d'une boucle que pour afficher le temps ecoule.
389 cd_musicplayer_register_my_handler (pHandler);
390 }