| 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-mpris.h" |
| 32 |
|
| 33 |
#define MP_DBUS_TYPE_PLAYER_STATUS_MPRIS (dbus_g_type_get_struct ("GValueArray", G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INVALID)) |
| 34 |
#define MP_DBUS_TYPE_TRACKLIST_DATA G_TYPE_INT |
| 35 |
|
| 36 |
static DBusGProxyCall *s_pGetSongInfosCall = NULL; |
| 37 |
static DBusGProxyCall *s_pGetStatusCall = NULL; |
| 38 |
static DBusGProxyCall *s_pGetCurrentTrackCall = NULL; |
| 39 |
|
| 40 |
static void cd_mpris_getSongInfos_async (void); |
| 41 |
|
| 42 |
/* |
| 43 |
<node name="/Player"> |
| 44 |
<interface name="org.freedesktop.MediaPlayer"> |
| 45 |
|
| 46 |
<method name="Pause"> |
| 47 |
</method> |
| 48 |
|
| 49 |
<method name="Stop"> |
| 50 |
</method> |
| 51 |
|
| 52 |
<method name="Play"> |
| 53 |
</method> |
| 54 |
|
| 55 |
<method name="Prev"> |
| 56 |
</method> |
| 57 |
|
| 58 |
<method name="Next"> |
| 59 |
</method> |
| 60 |
|
| 61 |
<method name="Repeat"> |
| 62 |
<arg type="b" direction="in"/> |
| 63 |
</method> |
| 64 |
|
| 65 |
<method name="GetStatus"> |
| 66 |
<arg type="(iiii)" direction="out"/> // 0 = Playing, 1 = Paused, 2 = Stopped / 0 = Playing linearly , 1 = Playing randomly / 0 = Go to the next element once the current has finished playing , 1 = Repeat the current / 0 = Stop playing once the last element has been played, 1 = Never give up playing |
| 67 |
|
| 68 |
<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="DBusStatus"/> |
| 69 |
</method> |
| 70 |
|
| 71 |
<method name="VolumeSet"> |
| 72 |
<arg type="i" direction="in"/> |
| 73 |
</method> |
| 74 |
|
| 75 |
<method name="VolumeGet"> |
| 76 |
<arg type="i" direction="out"/> |
| 77 |
</method> |
| 78 |
|
| 79 |
<method name="PositionSet"> |
| 80 |
<arg type="i" direction="in"/> |
| 81 |
</method> |
| 82 |
|
| 83 |
<method name="PositionGet"> |
| 84 |
<arg type="i" direction="out"/> |
| 85 |
</method> |
| 86 |
|
| 87 |
<method name="GetMetadata"> |
| 88 |
<arg type="a{sv}" direction="out"/> |
| 89 |
<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/> |
| 90 |
</method> |
| 91 |
|
| 92 |
<method name="GetCaps"> |
| 93 |
<arg type="i" direction="out" /> |
| 94 |
</method> |
| 95 |
|
| 96 |
<signal name="TrackChange"> |
| 97 |
<arg type="a{sv}"/> |
| 98 |
<annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="QVariantMap"/> |
| 99 |
</signal> |
| 100 |
|
| 101 |
<signal name="StatusChange"> |
| 102 |
<arg type="(iiii)"/> |
| 103 |
<annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="DBusStatus"/> |
| 104 |
</signal> |
| 105 |
|
| 106 |
<signal name="CapsChange"> |
| 107 |
<arg type="i" /> |
| 108 |
</signal> |
| 109 |
|
| 110 |
<!-- NB: Amarok extensions to the mpris spec --> |
| 111 |
<method name="VolumeUp"> |
| 112 |
<arg type="i" drection="in"/> |
| 113 |
</method> |
| 114 |
|
| 115 |
<method name="VolumeDown"> |
| 116 |
<arg type="i" drection="in"/> |
| 117 |
</method> |
| 118 |
|
| 119 |
<method name="Mute"> |
| 120 |
</method> |
| 121 |
|
| 122 |
</interface> |
| 123 |
</node> |
| 124 |
|
| 125 |
<node name="/TrackList"> |
| 126 |
<interface name="org.freedesktop.MediaPlayer"> |
| 127 |
|
| 128 |
<method name="GetMetadata"> |
| 129 |
<arg type="i" direction="in" /> |
| 130 |
<arg type="a{sv}" direction="out" /> |
| 131 |
<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/> |
| 132 |
</method> |
| 133 |
|
| 134 |
<method name="GetCurrentTrack"> // position of current URI in the TrackList. The position of the first URI in the TrackList is 0. |
| 135 |
<arg type="i" direction="out" /> |
| 136 |
</method> |
| 137 |
|
| 138 |
<method name="GetLength"> // Number of elements in the TrackList. |
| 139 |
<arg type="i" direction="out" /> |
| 140 |
</method> |
| 141 |
|
| 142 |
<method name="AddTrack"> |
| 143 |
<arg type="s" direction="in" /> |
| 144 |
<arg type="b" direction="in" /> // TRUE if the item should be played immediately, FALSE otherwise. |
| 145 |
<arg type="i" direction="out" /> |
| 146 |
</method> |
| 147 |
|
| 148 |
<method name="DelTrack"> |
| 149 |
<arg type="i" /> |
| 150 |
</method> |
| 151 |
|
| 152 |
<method name="SetLoop"> |
| 153 |
<arg type="b" /> |
| 154 |
</method> |
| 155 |
|
| 156 |
<method name="SetRandom"> |
| 157 |
<arg type="b" /> |
| 158 |
</method> |
| 159 |
|
| 160 |
<signal name="TrackListChange"> |
| 161 |
<arg type="i" /> |
| 162 |
</signal> |
| 163 |
|
| 164 |
</interface> |
| 165 |
</node> |
| 166 |
*/ |
| 167 |
|
| 168 |
|
| 169 |
///////////////////////////////// |
| 170 |
// Les Fonctions propres a MP. // |
| 171 |
///////////////////////////////// |
| 172 |
static inline void _set_playing_status_mpris (int iStatus) |
| 173 |
{ |
| 174 |
switch (iStatus) |
| 175 |
{ |
| 176 |
case 0: |
| 177 |
myData.iPlayingStatus = PLAYER_PLAYING; |
| 178 |
break; |
| 179 |
case 1: |
| 180 |
myData.iPlayingStatus = PLAYER_PAUSED; |
| 181 |
break; |
| 182 |
case 2: |
| 183 |
default: |
| 184 |
myData.iPlayingStatus = PLAYER_STOPPED; |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static inline int _extract_status_mpris (GValueArray *status, int iStatusIndex) |
| 190 |
{ |
| 191 |
int iStatus; |
| 192 |
GValue *value = g_value_array_get_nth (status, iStatusIndex); |
| 193 |
if (value != NULL && G_VALUE_HOLDS_INT(value)) |
| 194 |
iStatus = g_value_get_int (value); |
| 195 |
else |
| 196 |
iStatus = -1; |
| 197 |
return iStatus; |
| 198 |
} |
| 199 |
|
| 200 |
static int _mpris_get_status (int iStatusIndex) |
| 201 |
{ |
| 202 |
GError *erreur = NULL; |
| 203 |
GValueArray *status; |
| 204 |
dbus_g_proxy_call (myData.dbus_proxy_player, "GetStatus", &erreur, // Audacious's GetStatus() does not comply with MPRIS spec, it returns a single Int32 |
| 205 |
G_TYPE_INVALID, |
| 206 |
MP_DBUS_TYPE_PLAYER_STATUS_MPRIS, &status, |
| 207 |
G_TYPE_INVALID); |
| 208 |
if (erreur != NULL) |
| 209 |
{ |
| 210 |
cd_warning (erreur->message); |
| 211 |
g_error_free (erreur); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
int iStatus = _extract_status_mpris (status, iStatusIndex); |
| 217 |
g_value_array_free (status); |
| 218 |
return iStatus; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
static void _on_got_playing_status (DBusGProxy *proxy, DBusGProxyCall *call_id, GldiModuleInstance *myApplet) |
| 223 |
{ |
| 224 |
cd_debug ("=== %s ()", __func__); |
| 225 |
CD_APPLET_ENTER; |
| 226 |
s_pGetStatusCall = NULL; |
| 227 |
|
| 228 |
int iStatus = -1; |
| 229 |
GValueArray *status = NULL; |
| 230 |
GError *erreur = NULL; |
| 231 |
dbus_g_proxy_end_call (proxy, |
| 232 |
call_id, |
| 233 |
&erreur, |
| 234 |
MP_DBUS_TYPE_PLAYER_STATUS_MPRIS, &status, |
| 235 |
G_TYPE_INVALID); |
| 236 |
if (erreur != NULL) |
| 237 |
{ |
| 238 |
cd_warning ("couldn't get MPRIS status (%s)\n", erreur->message); |
| 239 |
g_error_free (erreur); |
| 240 |
} |
| 241 |
else if (status != NULL) |
| 242 |
{ |
| 243 |
iStatus = _extract_status_mpris (status, 0); |
| 244 |
g_value_array_free (status); |
| 245 |
} |
| 246 |
|
| 247 |
_set_playing_status_mpris (iStatus); |
| 248 |
|
| 249 |
cd_mpris_getSongInfos_async (); |
| 250 |
|
| 251 |
CD_APPLET_LEAVE (); |
| 252 |
} |
| 253 |
void cd_mpris_getPlaying_async (void) |
| 254 |
{ |
| 255 |
if (s_pGetStatusCall != NULL) |
| 256 |
return; |
| 257 |
s_pGetStatusCall = dbus_g_proxy_begin_call (myData.dbus_proxy_player, |
| 258 |
"GetStatus", |
| 259 |
(DBusGProxyCallNotify)_on_got_playing_status, |
| 260 |
myApplet, |
| 261 |
(GDestroyNotify) NULL, |
| 262 |
G_TYPE_INVALID); |
| 263 |
} |
| 264 |
|
| 265 |
int cd_mpris_get_volume (void) |
| 266 |
{ |
| 267 |
GError *erreur = NULL; |
| 268 |
int iVolume; |
| 269 |
dbus_g_proxy_call (myData.dbus_proxy_player, "VolumeGet", &erreur, |
| 270 |
G_TYPE_INVALID, |
| 271 |
G_TYPE_INT, &iVolume, |
| 272 |
G_TYPE_INVALID); |
| 273 |
return iVolume; |
| 274 |
} |
| 275 |
|
| 276 |
void cd_mpris_set_volume (int iVolume) |
| 277 |
{ |
| 278 |
dbus_g_proxy_call_no_reply (myData.dbus_proxy_player, "VolumeSet", |
| 279 |
G_TYPE_INT, iVolume, |
| 280 |
G_TYPE_INVALID, |
| 281 |
G_TYPE_INVALID); |
| 282 |
} |
| 283 |
|
| 284 |
/* Teste si MP joue de la musique ou non |
| 285 |
*/ |
| 286 |
void cd_mpris_getPlaying (void) // sync version, used by Audacious. |
| 287 |
{ |
| 288 |
cd_debug ("%s ()", __func__); |
| 289 |
int iStatus = _mpris_get_status (0); |
| 290 |
_set_playing_status_mpris (iStatus); |
| 291 |
} |
| 292 |
|
| 293 |
/* Teste si MP joue en boucle ou non |
| 294 |
*/ |
| 295 |
static gboolean cd_mpris_is_loop (void) |
| 296 |
{ |
| 297 |
cd_debug ("%s ()", __func__); |
| 298 |
int iStatus = _mpris_get_status (3); // Fourth integer: 0 = Stop playing once the last element has been played, 1 = Never give up playing. |
| 299 |
g_return_val_if_fail (iStatus != -1, FALSE); |
| 300 |
return iStatus; |
| 301 |
} |
| 302 |
|
| 303 |
/* Teste si MP joue aleatoirement ou non |
| 304 |
*/ |
| 305 |
static gboolean cd_mpris_is_shuffle (void) |
| 306 |
{ |
| 307 |
cd_debug ("%s ()", __func__); |
| 308 |
int iStatus = _mpris_get_status (1); // Second interger: 0 = Playing linearly , 1 = Playing randomly. |
| 309 |
g_return_val_if_fail (iStatus != -1, FALSE); |
| 310 |
return iStatus; |
| 311 |
} |
| 312 |
|
| 313 |
/* Renvoie le temps ecoule en secondes.. |
| 314 |
*/ |
| 315 |
void cd_mpris_get_time_elapsed (void) // used by Audacious too. |
| 316 |
{ |
| 317 |
myData.iCurrentTime = cairo_dock_dbus_get_integer (myData.dbus_proxy_player, "PositionGet"); |
| 318 |
if (myData.iCurrentTime > 0) // -1 signifie que la valeur n'a pas pu etre retrouvee (lecteur ferme). |
| 319 |
myData.iCurrentTime /= 1000; |
| 320 |
} |
| 321 |
|
| 322 |
/* Renvoie le numero de la chanson courante dans la playlist. |
| 323 |
*/ |
| 324 |
static void _on_get_current_track (DBusGProxy *proxy, DBusGProxyCall *call_id, gpointer data) |
| 325 |
{ |
| 326 |
s_pGetCurrentTrackCall = NULL; |
| 327 |
dbus_g_proxy_end_call (proxy, |
| 328 |
call_id, |
| 329 |
NULL, |
| 330 |
G_TYPE_INT, |
| 331 |
&myData.iTrackListIndex, |
| 332 |
G_TYPE_INVALID); |
| 333 |
//g_print ("myData.iTrackListIndex <- %d\n", myData.iTrackListIndex); |
| 334 |
if (myConfig.iQuickInfoType == MY_APPLET_TRACK && myData.iTrackListIndex > 0) |
| 335 |
{ |
| 336 |
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%s%d", (myDesklet && myDesklet->container.iWidth >= 64 ? D_("Track") : ""), myData.iTrackListIndex); |
| 337 |
CD_APPLET_REDRAW_MY_ICON; |
| 338 |
} |
| 339 |
} |
| 340 |
static void cd_mpris_get_track_index_async (void) |
| 341 |
{ |
| 342 |
if (s_pGetCurrentTrackCall != NULL) |
| 343 |
return; |
| 344 |
s_pGetCurrentTrackCall = dbus_g_proxy_begin_call (myData.dbus_proxy_shell, "GetCurrentTrack", |
| 345 |
(DBusGProxyCallNotify)_on_get_current_track, |
| 346 |
NULL, |
| 347 |
(GDestroyNotify) NULL, |
| 348 |
G_TYPE_INVALID); |
| 349 |
/**myData.iTrackListIndex = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetCurrentTrack"); |
| 350 |
//g_print ("myData.iTrackListIndex <- %d\n", myData.iTrackListIndex);*/ |
| 351 |
} |
| 352 |
|
| 353 |
static inline int _get_integer_value (GValue *value) |
| 354 |
{ |
| 355 |
if (G_VALUE_HOLDS_INT(value)) |
| 356 |
{ |
| 357 |
return g_value_get_int(value); |
| 358 |
} |
| 359 |
else if (G_VALUE_HOLDS_UINT(value)) |
| 360 |
{ |
| 361 |
return g_value_get_uint(value); |
| 362 |
} |
| 363 |
else if (G_VALUE_HOLDS_INT64(value)) |
| 364 |
{ |
| 365 |
return g_value_get_int64(value); |
| 366 |
} |
| 367 |
else if (G_VALUE_HOLDS_UINT64(value)) |
| 368 |
{ |
| 369 |
return g_value_get_uint64(value); |
| 370 |
} |
| 371 |
else if (G_VALUE_HOLDS_LONG(value)) |
| 372 |
{ |
| 373 |
return g_value_get_long(value); |
| 374 |
} |
| 375 |
else if (G_VALUE_HOLDS_ULONG(value)) |
| 376 |
{ |
| 377 |
return g_value_get_ulong(value); |
| 378 |
} |
| 379 |
else |
| 380 |
return 0; |
| 381 |
} |
| 382 |
static inline void _extract_metadata (GHashTable *data_list) |
| 383 |
{ |
| 384 |
GValue *value; |
| 385 |
const gchar *str; |
| 386 |
g_free (myData.cArtist); |
| 387 |
myData.cArtist = NULL; |
| 388 |
value = (GValue *) g_hash_table_lookup(data_list, "artist"); |
| 389 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 390 |
{ |
| 391 |
str = g_value_get_string(value); |
| 392 |
if (str && *str != '\0') |
| 393 |
myData.cArtist = g_strdup (str); |
| 394 |
} |
| 395 |
cd_message (" cArtist <- %s", myData.cArtist); |
| 396 |
|
| 397 |
g_free (myData.cAlbum); |
| 398 |
myData.cAlbum = NULL; |
| 399 |
value = (GValue *) g_hash_table_lookup(data_list, "album"); |
| 400 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 401 |
{ |
| 402 |
str = g_value_get_string(value); |
| 403 |
if (str && *str != '\0') |
| 404 |
myData.cAlbum = g_strdup (str); |
| 405 |
} |
| 406 |
cd_message (" cAlbum <- %s", myData.cAlbum); |
| 407 |
|
| 408 |
g_free (myData.cTitle); |
| 409 |
myData.cTitle = NULL; |
| 410 |
value = (GValue *) g_hash_table_lookup(data_list, "title"); |
| 411 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 412 |
{ |
| 413 |
str = g_value_get_string(value); |
| 414 |
if (str && *str != '\0') |
| 415 |
myData.cTitle = g_strdup (str); |
| 416 |
} |
| 417 |
cd_message (" cTitle <- %s", myData.cTitle); |
| 418 |
|
| 419 |
value = (GValue *) g_hash_table_lookup(data_list, "tracknumber"); |
| 420 |
if (value == NULL) |
| 421 |
value = (GValue *) g_hash_table_lookup(data_list, "track-number"); // au cas ou |
| 422 |
if (value != NULL) |
| 423 |
{ |
| 424 |
if (G_VALUE_HOLDS_INT (value)) // amarok |
| 425 |
myData.iTrackNumber = g_value_get_int (value); |
| 426 |
else if (G_VALUE_HOLDS_UINT (value)) |
| 427 |
myData.iTrackNumber = g_value_get_uint (value); |
| 428 |
else if (G_VALUE_HOLDS_STRING (value)) // qmmp |
| 429 |
{ |
| 430 |
const gchar *s = g_value_get_string (value); |
| 431 |
myData.iTrackNumber = (s ? atoi(s) : 0); |
| 432 |
} |
| 433 |
} |
| 434 |
else |
| 435 |
myData.iTrackNumber = 0; |
| 436 |
cd_message (" iTrackNumber <- %d", myData.iTrackNumber); |
| 437 |
|
| 438 |
myData.iSongLength = 0; |
| 439 |
value = (GValue *) g_hash_table_lookup(data_list, "mtime"); // duree en ms. |
| 440 |
if (value != NULL) |
| 441 |
{ |
| 442 |
myData.iSongLength = _get_integer_value (value) / 1000; |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
value = (GValue *) g_hash_table_lookup(data_list, "length"); |
| 447 |
if (value == NULL) |
| 448 |
value = (GValue *) g_hash_table_lookup(data_list, "time"); |
| 449 |
if (value != NULL) |
| 450 |
{ |
| 451 |
myData.iSongLength = _get_integer_value (value); |
| 452 |
if (myData.iSongLength > 7200) // on est dans le cas ou "mtime" n'existe pas, donc on n'est pas totalement sur que le lecteur ne nous ai pas refile le temps en ms. On considere qu'une chanson de plus de 2h ca n'existe pas. |
| 453 |
myData.iSongLength /= 1000; |
| 454 |
} |
| 455 |
} |
| 456 |
cd_message (" iSongLength <- %ds", myData.iSongLength); |
| 457 |
|
| 458 |
g_free (myData.cPlayingUri); |
| 459 |
value = (GValue *) g_hash_table_lookup(data_list, "location"); |
| 460 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) myData.cPlayingUri = g_strdup (g_value_get_string(value)); |
| 461 |
else myData.cPlayingUri = NULL; |
| 462 |
cd_message (" cUri <- %s", myData.cPlayingUri); |
| 463 |
|
| 464 |
const gchar *cCoverPath = NULL; |
| 465 |
value = (GValue *) g_hash_table_lookup(data_list, "arturl"); |
| 466 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 467 |
{ |
| 468 |
cCoverPath = g_value_get_string(value); |
| 469 |
} |
| 470 |
cd_musicplayer_set_cover_path (cCoverPath); |
| 471 |
} |
| 472 |
|
| 473 |
static void _on_got_song_infos (DBusGProxy *proxy, DBusGProxyCall *call_id, GldiModuleInstance *myApplet) |
| 474 |
{ |
| 475 |
cd_debug ("=== %s ()", __func__); |
| 476 |
CD_APPLET_ENTER; |
| 477 |
s_pGetSongInfosCall = NULL; |
| 478 |
|
| 479 |
GHashTable *data_list = NULL; |
| 480 |
GError *erreur = NULL; |
| 481 |
dbus_g_proxy_end_call (proxy, |
| 482 |
call_id, |
| 483 |
&erreur, |
| 484 |
MP_DBUS_TYPE_SONG_METADATA, &data_list, |
| 485 |
G_TYPE_INVALID); |
| 486 |
if (erreur != NULL) |
| 487 |
{ |
| 488 |
cd_warning ("couldn't get MPRIS song infos (%s)\n", erreur->message); |
| 489 |
g_error_free (erreur); |
| 490 |
data_list = NULL; |
| 491 |
} |
| 492 |
|
| 493 |
if (data_list != NULL) |
| 494 |
{ |
| 495 |
_extract_metadata (data_list); |
| 496 |
|
| 497 |
g_hash_table_destroy (data_list); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
cd_warning (" can't get song properties"); |
| 502 |
g_free (myData.cPlayingUri); |
| 503 |
myData.cPlayingUri = NULL; |
| 504 |
g_free (myData.cTitle); |
| 505 |
myData.cTitle = NULL; |
| 506 |
g_free (myData.cAlbum); |
| 507 |
myData.cAlbum = NULL; |
| 508 |
g_free (myData.cArtist); |
| 509 |
myData.cArtist = NULL; |
| 510 |
g_free (myData.cCoverPath); |
| 511 |
myData.cCoverPath = NULL; |
| 512 |
myData.iSongLength = 0; |
| 513 |
myData.iTrackNumber = 0; |
| 514 |
myData.cover_exist = FALSE; |
| 515 |
} |
| 516 |
|
| 517 |
cd_musicplayer_update_icon (); |
| 518 |
cd_musicplayer_relaunch_handler (); |
| 519 |
|
| 520 |
CD_APPLET_LEAVE (); |
| 521 |
} |
| 522 |
static void cd_mpris_getSongInfos_async (void) |
| 523 |
{ |
| 524 |
if (s_pGetSongInfosCall != NULL) |
| 525 |
return; |
| 526 |
s_pGetSongInfosCall = dbus_g_proxy_begin_call (myData.dbus_proxy_player, |
| 527 |
"GetMetadata", |
| 528 |
(DBusGProxyCallNotify)_on_got_song_infos, |
| 529 |
myApplet, |
| 530 |
(GDestroyNotify) NULL, |
| 531 |
G_TYPE_INVALID); |
| 532 |
} |
| 533 |
|
| 534 |
///////////////////////////////////// |
| 535 |
// Les callbacks des signaux DBus. // |
| 536 |
///////////////////////////////////// |
| 537 |
|
| 538 |
/* Fonction executee a chaque changement de musique. |
| 539 |
*/ |
| 540 |
static void onChangeSong_mpris(DBusGProxy *player_proxy, GHashTable *metadata, gpointer data) |
| 541 |
{ |
| 542 |
CD_APPLET_ENTER; |
| 543 |
cd_debug ("MP : %s ()", __func__); |
| 544 |
|
| 545 |
if (metadata != NULL) |
| 546 |
{ |
| 547 |
_extract_metadata (metadata); |
| 548 |
myData.iPlayingStatus = PLAYER_PLAYING; // pour les lecteurs bugues comem 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". |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
cd_warning (" no song properties"); |
| 553 |
g_free (myData.cPlayingUri); |
| 554 |
myData.cPlayingUri = NULL; |
| 555 |
g_free (myData.cArtist); |
| 556 |
myData.cArtist = NULL; |
| 557 |
g_free (myData.cAlbum); |
| 558 |
myData.cAlbum = NULL; |
| 559 |
g_free (myData.cTitle); |
| 560 |
myData.cTitle = NULL; |
| 561 |
g_free (myData.cCoverPath); |
| 562 |
myData.cCoverPath = NULL; |
| 563 |
myData.iSongLength = 0; |
| 564 |
myData.iTrackNumber = 0; |
| 565 |
myData.cover_exist = FALSE; |
| 566 |
} |
| 567 |
cd_musicplayer_update_icon (); |
| 568 |
CD_APPLET_LEAVE (); |
| 569 |
} |
| 570 |
|
| 571 |
/* Fonction executee a chaque changement play/pause |
| 572 |
*/ |
| 573 |
void onChangePlaying_mpris (DBusGProxy *player_proxy, GValueArray *status, gpointer data) // used by Audacious too. |
| 574 |
{ |
| 575 |
CD_APPLET_ENTER; |
| 576 |
//cd_debug ("MP : %s (%x)", __func__, status); |
| 577 |
myData.iGetTimeFailed = 0; |
| 578 |
int iStatus = _extract_status_mpris (status, 0); |
| 579 |
_set_playing_status_mpris (iStatus); |
| 580 |
cd_debug ("myData.iPlayingStatus <- %d", myData.iPlayingStatus); |
| 581 |
|
| 582 |
if (myData.iPlayingStatus == PLAYER_PLAYING) // le handler est stoppe lorsque le lecteur ne joue rien. |
| 583 |
cd_musicplayer_relaunch_handler (); |
| 584 |
|
| 585 |
if (myData.iPlayingStatus == PLAYER_STOPPED) |
| 586 |
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL); |
| 587 |
|
| 588 |
if(! myData.cover_exist/** && (myData.cPlayingUri != NULL || myData.cTitle != NULL)*/) |
| 589 |
{ |
| 590 |
cd_musicplayer_apply_status_surface (myData.iPlayingStatus); |
| 591 |
} |
| 592 |
else |
| 593 |
{ |
| 594 |
CD_APPLET_REDRAW_MY_ICON; |
| 595 |
} |
| 596 |
CD_APPLET_LEAVE (); |
| 597 |
} |
| 598 |
|
| 599 |
/* Fonction execute a chaque changement dans la TrackList. |
| 600 |
*/ |
| 601 |
static void onChangeTrackList_mpris (DBusGProxy *player_proxy, gint iNewTrackListLength, gpointer data) |
| 602 |
{ |
| 603 |
CD_APPLET_ENTER; |
| 604 |
cd_debug ("MP : %s (%d)", __func__, iNewTrackListLength); |
| 605 |
myData.iTrackListLength = iNewTrackListLength; |
| 606 |
cd_mpris_get_track_index_async (); |
| 607 |
CD_APPLET_LEAVE (); |
| 608 |
} |
| 609 |
|
| 610 |
//////////////////////////// |
| 611 |
// Definition du backend. // |
| 612 |
//////////////////////////// |
| 613 |
|
| 614 |
/* Permet de liberer la memoire prise par le backend. |
| 615 |
*/ |
| 616 |
static void cd_mpris_stop (void) |
| 617 |
{ |
| 618 |
if (myData.dbus_proxy_player != NULL) |
| 619 |
{ |
| 620 |
if (s_pGetSongInfosCall != NULL) |
| 621 |
{ |
| 622 |
dbus_g_proxy_cancel_call (myData.dbus_proxy_player, s_pGetSongInfosCall); |
| 623 |
s_pGetSongInfosCall = NULL; |
| 624 |
} |
| 625 |
|
| 626 |
if (s_pGetStatusCall != NULL) |
| 627 |
{ |
| 628 |
dbus_g_proxy_cancel_call (myData.dbus_proxy_player, s_pGetStatusCall); |
| 629 |
s_pGetStatusCall = NULL; |
| 630 |
} |
| 631 |
|
| 632 |
dbus_g_proxy_disconnect_signal(myData.dbus_proxy_player, "StatusChange", |
| 633 |
G_CALLBACK(onChangePlaying_mpris), NULL); |
| 634 |
|
| 635 |
dbus_g_proxy_disconnect_signal(myData.dbus_proxy_player, "TrackChange", |
| 636 |
G_CALLBACK(onChangeSong_mpris), NULL); |
| 637 |
} |
| 638 |
if (myData.dbus_proxy_shell!= NULL) |
| 639 |
{ |
| 640 |
if (s_pGetCurrentTrackCall != NULL) |
| 641 |
{ |
| 642 |
dbus_g_proxy_cancel_call (myData.dbus_proxy_player, s_pGetCurrentTrackCall); |
| 643 |
s_pGetCurrentTrackCall = NULL; |
| 644 |
} |
| 645 |
|
| 646 |
dbus_g_proxy_disconnect_signal(myData.dbus_proxy_shell, "TrackListChange", |
| 647 |
G_CALLBACK(onChangeTrackList_mpris), NULL); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
/* Controle du lecteur (permet d'effectuer les actions de bases sur le lecteur). |
| 653 |
*/ |
| 654 |
static void cd_mpris_control (MyPlayerControl pControl, const char* song) |
| 655 |
{ |
| 656 |
gboolean bToggleValue; |
| 657 |
switch (pControl) |
| 658 |
{ |
| 659 |
case PLAYER_PREVIOUS : |
| 660 |
cairo_dock_dbus_call (myData.dbus_proxy_player, "Prev"); |
| 661 |
break; |
| 662 |
|
| 663 |
case PLAYER_STOP : |
| 664 |
cairo_dock_dbus_call (myData.dbus_proxy_player, "Stop"); |
| 665 |
break; |
| 666 |
|
| 667 |
case PLAYER_PLAY_PAUSE : |
| 668 |
if (myData.iPlayingStatus != PLAYER_PLAYING) |
| 669 |
cairo_dock_dbus_call (myData.dbus_proxy_player, "Play"); |
| 670 |
else |
| 671 |
cairo_dock_dbus_call (myData.dbus_proxy_player, "Pause"); |
| 672 |
break; |
| 673 |
|
| 674 |
case PLAYER_NEXT : |
| 675 |
cairo_dock_dbus_call (myData.dbus_proxy_player, "Next"); |
| 676 |
break; |
| 677 |
|
| 678 |
case PLAYER_JUMPBOX : |
| 679 |
|
| 680 |
break; |
| 681 |
|
| 682 |
case PLAYER_SHUFFLE : |
| 683 |
bToggleValue = cd_mpris_is_shuffle (); |
| 684 |
cd_debug ("SetRandom <- %d", !bToggleValue); |
| 685 |
dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "SetRandom", |
| 686 |
G_TYPE_BOOLEAN, !bToggleValue, |
| 687 |
G_TYPE_INVALID); |
| 688 |
break; |
| 689 |
|
| 690 |
case PLAYER_REPEAT : |
| 691 |
bToggleValue = cd_mpris_is_loop (); |
| 692 |
cd_debug ("SetLoop <- %d", !bToggleValue); |
| 693 |
dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "SetLoop", |
| 694 |
G_TYPE_BOOLEAN, !bToggleValue, |
| 695 |
G_TYPE_INVALID); |
| 696 |
break; |
| 697 |
|
| 698 |
case PLAYER_ENQUEUE : |
| 699 |
cd_debug ("enqueue %s", song); |
| 700 |
dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "AddTrack", |
| 701 |
G_TYPE_STRING, song, |
| 702 |
G_TYPE_BOOLEAN, FALSE, |
| 703 |
G_TYPE_INVALID); |
| 704 |
break; |
| 705 |
|
| 706 |
case PLAYER_VOLUME : |
| 707 |
{ |
| 708 |
int iVolume = cd_mpris_get_volume (); // [0, 100] |
| 709 |
if (song && strcmp (song, "up") == 0) |
| 710 |
iVolume += 5; |
| 711 |
else |
| 712 |
iVolume -= 5; |
| 713 |
|
| 714 |
if (iVolume > 100) |
| 715 |
iVolume = 100; |
| 716 |
else if (iVolume < 0) |
| 717 |
iVolume = 0; |
| 718 |
|
| 719 |
cd_mpris_set_volume (iVolume); |
| 720 |
} |
| 721 |
|
| 722 |
default : |
| 723 |
break; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/* Recupere le temps ecoule chaque seconde (pas de signal pour ca). |
| 729 |
*/ |
| 730 |
static void cd_mpris_get_data (void) |
| 731 |
{ |
| 732 |
if (myData.iPlayingStatus == PLAYER_PLAYING) |
| 733 |
{ |
| 734 |
cd_mpris_get_time_elapsed (); |
| 735 |
if (myData.iCurrentTime < 0) // aucune info de temps sur le bus => lecteur ferme. |
| 736 |
{ |
| 737 |
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. |
| 738 |
cd_debug ("failed to get time %d time(s)", myData.iGetTimeFailed); |
| 739 |
if (myData.iGetTimeFailed > 2) |
| 740 |
{ |
| 741 |
cd_debug (" => player is likely closed"); |
| 742 |
myData.iPlayingStatus = PLAYER_NONE; |
| 743 |
myData.iCurrentTime = -2; // le temps etait a -1, on le change pour provoquer un redraw. |
| 744 |
} |
| 745 |
} |
| 746 |
else |
| 747 |
myData.iGetTimeFailed = 0; |
| 748 |
} |
| 749 |
else if (myData.iPlayingStatus != PLAYER_PAUSED) // en pause le temps reste constant. |
| 750 |
{ |
| 751 |
myData.iCurrentTime = 0; |
| 752 |
myData.iGetTimeFailed = 0; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
static void cd_mpris_start (void) |
| 757 |
{ |
| 758 |
// register to the signals |
| 759 |
dbus_g_proxy_add_signal(myData.dbus_proxy_player, "StatusChange", |
| 760 |
MP_DBUS_TYPE_PLAYER_STATUS_MPRIS, |
| 761 |
G_TYPE_INVALID); |
| 762 |
dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "StatusChange", |
| 763 |
G_CALLBACK(onChangePlaying_mpris), NULL, NULL); |
| 764 |
|
| 765 |
dbus_g_proxy_add_signal(myData.dbus_proxy_player, "TrackChange", |
| 766 |
MP_DBUS_TYPE_SONG_METADATA, |
| 767 |
G_TYPE_INVALID); |
| 768 |
dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "TrackChange", |
| 769 |
G_CALLBACK(onChangeSong_mpris), NULL, NULL); |
| 770 |
|
| 771 |
dbus_g_proxy_add_signal(myData.dbus_proxy_shell, "TrackListChange", |
| 772 |
MP_DBUS_TYPE_TRACKLIST_DATA, |
| 773 |
G_TYPE_INVALID); |
| 774 |
dbus_g_proxy_connect_signal(myData.dbus_proxy_shell, "TrackListChange", |
| 775 |
G_CALLBACK(onChangeTrackList_mpris), NULL, NULL); |
| 776 |
|
| 777 |
// get the current state. |
| 778 |
cd_mpris_getPlaying_async (); // will get song infos after playing status. |
| 779 |
/**cd_mpris_getPlaying (); |
| 780 |
cd_mpris_getSongInfos (); |
| 781 |
cd_musicplayer_update_icon (); |
| 782 |
cd_musicplayer_relaunch_handler ();*/ |
| 783 |
} |
| 784 |
|
| 785 |
MusicPlayerHandler *cd_mpris_new_handler (void) |
| 786 |
{ |
| 787 |
MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1); |
| 788 |
pHandler->get_data = cd_mpris_get_data; |
| 789 |
pHandler->stop = cd_mpris_stop; |
| 790 |
pHandler->start = cd_mpris_start; |
| 791 |
pHandler->control = cd_mpris_control; |
| 792 |
pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT | PLAYER_STOP | PLAYER_SHUFFLE | PLAYER_REPEAT | PLAYER_ENQUEUE | PLAYER_VOLUME; |
| 793 |
pHandler->bSeparateAcquisition = FALSE; |
| 794 |
pHandler->iLevel = PLAYER_GOOD; |
| 795 |
|
| 796 |
pHandler->path = "/Player"; |
| 797 |
pHandler->interface = "org.freedesktop.MediaPlayer"; |
| 798 |
pHandler->path2 = "/TrackList"; |
| 799 |
pHandler->interface2 = "org.freedesktop.MediaPlayer"; |
| 800 |
|
| 801 |
return pHandler; |
| 802 |
} |