| 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 |
#include "applet-audacious.h" |
| 33 |
|
| 34 |
#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)) |
| 35 |
#define MP_DBUS_TYPE_PLAYER_STATUS G_TYPE_INT |
| 36 |
|
| 37 |
/* |
| 38 |
<node name="/Player"> |
| 39 |
22 <interface name="org.freedesktop.MediaPlayer"> |
| 40 |
23 <method name="Next"> |
| 41 |
24 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 42 |
25 </method> |
| 43 |
26 <method name="Prev"> |
| 44 |
27 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 45 |
28 </method> |
| 46 |
29 <method name="Pause"> |
| 47 |
30 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 48 |
31 </method> |
| 49 |
32 <method name="Stop"> |
| 50 |
33 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 51 |
34 </method> |
| 52 |
35 <method name="Play"> |
| 53 |
36 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 54 |
37 </method> |
| 55 |
38 <method name="Repeat"> |
| 56 |
39 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 57 |
40 <arg type="b" direction="in" /> |
| 58 |
41 </method> |
| 59 |
42 <method name="Quit"> |
| 60 |
43 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 61 |
44 </method> |
| 62 |
45 <method name="GetStatus"> |
| 63 |
46 <arg type="(iiii)" direction="out" /> |
| 64 |
47 </method> |
| 65 |
48 <method name="GetMetadata"> |
| 66 |
49 <arg type="a{sv}" direction="out" /> |
| 67 |
50 </method> |
| 68 |
51 <method name="GetCaps"> |
| 69 |
52 <arg type="i" direction="out" /> |
| 70 |
53 </method> |
| 71 |
54 <method name="VolumeSet"> |
| 72 |
55 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 73 |
56 <arg type="i" direction="in" /> |
| 74 |
57 </method> |
| 75 |
58 <method name="VolumeGet"> |
| 76 |
59 <arg type="i" direction="out" /> |
| 77 |
60 </method> |
| 78 |
61 <method name="PositionSet"> |
| 79 |
62 <annotation name="org.freedesktop.DBus.GLib.NoReply" value=""/> |
| 80 |
63 <arg type="i" direction="in" /> |
| 81 |
64 </method> |
| 82 |
65 <method name="PositionGet"> |
| 83 |
66 <arg type="i" direction="out" /> |
| 84 |
67 </method> |
| 85 |
68 |
| 86 |
69 <signal name="TrackChange"> |
| 87 |
70 <arg type="a{sv}" /> |
| 88 |
71 </signal> |
| 89 |
72 <signal name="StatusChange"> |
| 90 |
73 <arg type="(iiii)" /> |
| 91 |
74 </signal> |
| 92 |
75 <signal name="CapsChange"> |
| 93 |
76 <arg type="i" /> |
| 94 |
77 </signal> |
| 95 |
78 </interface> |
| 96 |
79 </node> |
| 97 |
|
| 98 |
service : org.mpris.audacious |
| 99 |
interface : org.freedesktop.MediaPlayer |
| 100 |
|
| 101 |
path : /Player |
| 102 |
Next |
| 103 |
Prev |
| 104 |
Pause |
| 105 |
Stop |
| 106 |
Play |
| 107 |
Repeat(bool) |
| 108 |
GetStatus(int[4]: status:play=0,pause,stop shuffle=random no_playlist_advance repeat=loop) |
| 109 |
GetMetadata(hash: length title artist album genre codec quality track-number location) |
| 110 |
PositionGet(int=time_in_second) |
| 111 |
|
| 112 |
TrackChange(hash: idem) |
| 113 |
StatusChange(int[4]) |
| 114 |
|
| 115 |
path : /TrackList |
| 116 |
AddTrack(string,bool=play_it_now) |
| 117 |
Random(bool) not yet implemented |
| 118 |
Loop(bool) not yet implemented |
| 119 |
|
| 120 |
service : org.atheme.audacious |
| 121 |
interface : org.atheme.audacious |
| 122 |
|
| 123 |
path : /org/atheme/audacious |
| 124 |
PlaylistVisible(bool=is_pl_win) |
| 125 |
ShowPlaylist(bool=show) |
| 126 |
ToggleShuffle |
| 127 |
ToggleRepeat |
| 128 |
*/ |
| 129 |
|
| 130 |
|
| 131 |
///////////////////////////////// |
| 132 |
// Les Fonctions propres a AU. // |
| 133 |
///////////////////////////////// |
| 134 |
|
| 135 |
static inline void _extract_metadata (GHashTable *data_list) |
| 136 |
{ |
| 137 |
GValue *value; |
| 138 |
const gchar *str; |
| 139 |
g_free (myData.cArtist); |
| 140 |
myData.cArtist = NULL; |
| 141 |
value = (GValue *) g_hash_table_lookup(data_list, "artist"); |
| 142 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 143 |
{ |
| 144 |
str = g_value_get_string(value); |
| 145 |
if (str && *str != '\0') |
| 146 |
myData.cArtist = g_strdup (str); |
| 147 |
} |
| 148 |
cd_message (" cArtist <- %s", myData.cArtist); |
| 149 |
|
| 150 |
g_free (myData.cAlbum); |
| 151 |
myData.cAlbum = NULL; |
| 152 |
value = (GValue *) g_hash_table_lookup(data_list, "album"); |
| 153 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 154 |
{ |
| 155 |
str = g_value_get_string(value); |
| 156 |
if (str && *str != '\0') |
| 157 |
myData.cAlbum = g_strdup (str); |
| 158 |
} |
| 159 |
cd_message (" cAlbum <- %s", myData.cAlbum); |
| 160 |
|
| 161 |
g_free (myData.cTitle); |
| 162 |
myData.cTitle = NULL; |
| 163 |
value = (GValue *) g_hash_table_lookup(data_list, "title"); |
| 164 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) |
| 165 |
{ |
| 166 |
str = g_value_get_string(value); |
| 167 |
if (str && *str != '\0') |
| 168 |
myData.cTitle = g_strdup (str); |
| 169 |
} |
| 170 |
cd_message (" cTitle <- %s", myData.cTitle); |
| 171 |
|
| 172 |
value = (GValue *) g_hash_table_lookup(data_list, "tracknumber"); |
| 173 |
if (value == NULL) |
| 174 |
value = (GValue *) g_hash_table_lookup(data_list, "track-number"); // old parameter. |
| 175 |
if (value != NULL && G_VALUE_HOLDS_INT(value)) |
| 176 |
myData.iTrackNumber = g_value_get_int(value); |
| 177 |
else |
| 178 |
myData.iTrackNumber = 0.; |
| 179 |
cd_message (" iTrackNumber <- %d", myData.iTrackNumber); |
| 180 |
|
| 181 |
value = (GValue *) g_hash_table_lookup(data_list, "mtime"); |
| 182 |
if (value == NULL) |
| 183 |
value = (GValue *) g_hash_table_lookup(data_list, "length"); // old parameter. |
| 184 |
if (value != NULL && G_VALUE_HOLDS_INT(value)) myData.iSongLength = g_value_get_int(value) / 1000; |
| 185 |
else myData.iSongLength = 0; |
| 186 |
cd_message (" iSongLength <- %ds", myData.iSongLength); |
| 187 |
|
| 188 |
g_free (myData.cPlayingUri); |
| 189 |
value = (GValue *) g_hash_table_lookup(data_list, "location"); |
| 190 |
if (value != NULL && G_VALUE_HOLDS_STRING(value)) myData.cPlayingUri = g_strdup (g_value_get_string(value)); |
| 191 |
else myData.cPlayingUri = NULL; |
| 192 |
cd_message (" cUri <- %s", myData.cPlayingUri); |
| 193 |
|
| 194 |
cd_musicplayer_set_cover_path (NULL); |
| 195 |
} |
| 196 |
|
| 197 |
/* Recupere les infos de la chanson courante, y compris le chemin de la couverture (la telecharge au besoin). |
| 198 |
*/ |
| 199 |
static void cd_audacious_getSongInfos (void) |
| 200 |
{ |
| 201 |
GHashTable *data_list = NULL; |
| 202 |
|
| 203 |
if(dbus_g_proxy_call (myData.dbus_proxy_player, "GetMetadata", NULL, |
| 204 |
G_TYPE_INVALID, |
| 205 |
MP_DBUS_TYPE_SONG_METADATA, |
| 206 |
&data_list, |
| 207 |
G_TYPE_INVALID)) |
| 208 |
{ |
| 209 |
_extract_metadata (data_list); |
| 210 |
myData.iTrackListIndex = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetCurrentTrack"); |
| 211 |
myData.iTrackListLength = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetLength"); |
| 212 |
g_hash_table_destroy (data_list); |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
cd_warning (" can't get song properties"); |
| 217 |
g_free (myData.cPlayingUri); |
| 218 |
myData.cPlayingUri = NULL; |
| 219 |
g_free (myData.cTitle); |
| 220 |
myData.cTitle = NULL; |
| 221 |
g_free (myData.cAlbum); |
| 222 |
myData.cAlbum = NULL; |
| 223 |
g_free (myData.cArtist); |
| 224 |
myData.cArtist = NULL; |
| 225 |
g_free (myData.cCoverPath); |
| 226 |
myData.cCoverPath = NULL; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
///////////////////////////////////// |
| 232 |
// Les callbacks des signaux DBus. // |
| 233 |
///////////////////////////////////// |
| 234 |
|
| 235 |
/* Fonction executée à chaque changement de musique. |
| 236 |
*/ |
| 237 |
static void onChangeSong(DBusGProxy *player_proxy, GHashTable *metadata, gpointer data) |
| 238 |
{ |
| 239 |
CD_APPLET_ENTER; |
| 240 |
cd_debug ("MP : %s ()", __func__); |
| 241 |
|
| 242 |
if (metadata != NULL) |
| 243 |
{ |
| 244 |
_extract_metadata (metadata); |
| 245 |
myData.iTrackListIndex = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetCurrentTrack"); |
| 246 |
myData.iTrackListLength = cairo_dock_dbus_get_integer (myData.dbus_proxy_shell, "GetLength"); |
| 247 |
myData.iPlayingStatus = PLAYER_PLAYING; // the current song has changed, means we are playing, so let's update the status now, for the coming update_icon(). |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
g_free (myData.cPlayingUri); |
| 252 |
myData.cPlayingUri = NULL; |
| 253 |
g_free (myData.cArtist); |
| 254 |
myData.cArtist = NULL; |
| 255 |
g_free (myData.cAlbum); |
| 256 |
myData.cAlbum = NULL; |
| 257 |
g_free (myData.cTitle); |
| 258 |
myData.cTitle = NULL; |
| 259 |
g_free (myData.cCoverPath); |
| 260 |
myData.cCoverPath = NULL; |
| 261 |
myData.iSongLength = 0; |
| 262 |
myData.iTrackNumber = 0; |
| 263 |
myData.cover_exist = FALSE; |
| 264 |
} |
| 265 |
cd_musicplayer_update_icon (); |
| 266 |
CD_APPLET_LEAVE (); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
//////////////////////////// |
| 271 |
// Definition du backend. // |
| 272 |
//////////////////////////// |
| 273 |
|
| 274 |
/* Controle du lecteur (permet d'effectuer les actions de bases sur le lecteur). |
| 275 |
*/ |
| 276 |
static void cd_audacious_control (MyPlayerControl pControl, const char* song) |
| 277 |
{ |
| 278 |
const gchar *cCommand = NULL; |
| 279 |
|
| 280 |
switch (pControl) { |
| 281 |
case PLAYER_PREVIOUS : |
| 282 |
cCommand = "Prev"; |
| 283 |
break; |
| 284 |
|
| 285 |
case PLAYER_STOP : |
| 286 |
cCommand = "Stop"; |
| 287 |
break; |
| 288 |
|
| 289 |
case PLAYER_PLAY_PAUSE : |
| 290 |
if (myData.iPlayingStatus != PLAYER_PLAYING) |
| 291 |
cCommand = "Play"; |
| 292 |
else |
| 293 |
cCommand = "Pause"; |
| 294 |
break; |
| 295 |
|
| 296 |
case PLAYER_NEXT : |
| 297 |
cCommand = "Next"; |
| 298 |
break; |
| 299 |
|
| 300 |
case PLAYER_JUMPBOX : |
| 301 |
case PLAYER_SHUFFLE : |
| 302 |
case PLAYER_REPEAT : |
| 303 |
{ |
| 304 |
DBusGProxy *dbus_proxy_atheme = cairo_dock_create_new_session_proxy ( |
| 305 |
"org.atheme.audacious", |
| 306 |
"/org/atheme/audacious", |
| 307 |
"org.atheme.audacious"); // currently buggy ... |
| 308 |
if (dbus_proxy_atheme != NULL) |
| 309 |
{ |
| 310 |
if (pControl == PLAYER_JUMPBOX) |
| 311 |
{ |
| 312 |
cd_debug ("ShowPlaylist\n"); |
| 313 |
cairo_dock_launch_command ("audacious2 --show-jump-box"); |
| 314 |
/*dbus_g_proxy_call_no_reply (dbus_proxy_atheme, "ShowPlaylist", |
| 315 |
G_TYPE_INVALID, |
| 316 |
G_TYPE_BOOLEAN, TRUE, |
| 317 |
G_TYPE_INVALID);*/ |
| 318 |
} |
| 319 |
else if (pControl == PLAYER_SHUFFLE) // a terme, utiliser la methode "Random" du proxy_shell |
| 320 |
{ |
| 321 |
cd_debug ("ToggleShuffle\n"); |
| 322 |
cairo_dock_dbus_call (dbus_proxy_atheme, "ToggleShuffle"); |
| 323 |
} |
| 324 |
else // a terme, utiliser la methode "Loop" du proxy_shell |
| 325 |
{ |
| 326 |
cd_debug ("ToggleRepeat\n"); |
| 327 |
cairo_dock_dbus_call (dbus_proxy_atheme, "ToggleRepeat"); |
| 328 |
} |
| 329 |
g_object_unref (dbus_proxy_atheme); |
| 330 |
} |
| 331 |
else |
| 332 |
cd_warning ("org.atheme.audacious not valid !"); |
| 333 |
} |
| 334 |
break; |
| 335 |
|
| 336 |
case PLAYER_ENQUEUE : |
| 337 |
cd_debug ("enqueue %s", song); |
| 338 |
dbus_g_proxy_call_no_reply (myData.dbus_proxy_shell, "AddTrack", |
| 339 |
G_TYPE_STRING, song, |
| 340 |
G_TYPE_BOOLEAN, FALSE, |
| 341 |
G_TYPE_INVALID); |
| 342 |
break; |
| 343 |
|
| 344 |
case PLAYER_VOLUME : |
| 345 |
{ |
| 346 |
int iVolume = cd_mpris_get_volume (); // [0, 100] |
| 347 |
if (song && strcmp (song, "up") == 0) |
| 348 |
iVolume += 5; |
| 349 |
else |
| 350 |
iVolume -= 5; |
| 351 |
|
| 352 |
if (iVolume > 100) |
| 353 |
iVolume = 100; |
| 354 |
else if (iVolume < 0) |
| 355 |
iVolume = 0; |
| 356 |
|
| 357 |
cd_mpris_set_volume (iVolume); |
| 358 |
} |
| 359 |
|
| 360 |
default : |
| 361 |
return; |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
if (cCommand != NULL) |
| 366 |
{ |
| 367 |
cd_debug ("MP : Handler audacious : will use '%s'", cCommand); |
| 368 |
cairo_dock_dbus_call (myData.dbus_proxy_player, cCommand); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
/* Recupere le temps ecoule chaque seconde (pas de signal pour ca). |
| 374 |
*/ |
| 375 |
static void cd_audacious_get_data (void) |
| 376 |
{ |
| 377 |
if (myData.iPlayingStatus == PLAYER_PLAYING) |
| 378 |
{ |
| 379 |
cd_mpris_get_time_elapsed (); |
| 380 |
if (myData.iCurrentTime < 0) |
| 381 |
myData.iPlayingStatus = PLAYER_STOPPED; |
| 382 |
} |
| 383 |
else if (myData.iPlayingStatus != PLAYER_PAUSED) // en pause le temps reste constant. |
| 384 |
myData.iCurrentTime = 0; |
| 385 |
} |
| 386 |
|
| 387 |
/* Initialise le backend de AU. |
| 388 |
*/ |
| 389 |
static void cd_audacious_start (void) |
| 390 |
{ |
| 391 |
// register to the signals |
| 392 |
dbus_g_proxy_add_signal(myData.dbus_proxy_player, "StatusChange", |
| 393 |
MP_DBUS_TYPE_PLAYER_STATUS_MPRIS, |
| 394 |
G_TYPE_INVALID); |
| 395 |
dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "StatusChange", |
| 396 |
G_CALLBACK(onChangePlaying_mpris), NULL, NULL); |
| 397 |
|
| 398 |
dbus_g_proxy_add_signal(myData.dbus_proxy_player, "TrackChange", |
| 399 |
MP_DBUS_TYPE_SONG_METADATA, |
| 400 |
G_TYPE_INVALID); |
| 401 |
dbus_g_proxy_connect_signal(myData.dbus_proxy_player, "TrackChange", |
| 402 |
G_CALLBACK(onChangeSong), NULL, NULL); // pas de TrackListChange, donc on fera tout dedans. |
| 403 |
|
| 404 |
// get the current state. |
| 405 |
cd_mpris_getPlaying (); ///_audacious_getPlaying_mpris (); |
| 406 |
cd_audacious_getSongInfos (); |
| 407 |
cd_musicplayer_update_icon (); |
| 408 |
} |
| 409 |
|
| 410 |
/* On enregistre notre lecteur. |
| 411 |
*/ |
| 412 |
void cd_musicplayer_register_audacious_handler (void) |
| 413 |
{ |
| 414 |
MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1); |
| 415 |
pHandler->name = "Audacious"; |
| 416 |
pHandler->get_data = cd_audacious_get_data; |
| 417 |
pHandler->stop = NULL; |
| 418 |
pHandler->start = cd_audacious_start; |
| 419 |
pHandler->control = cd_audacious_control; |
| 420 |
pHandler->get_cover = NULL; |
| 421 |
pHandler->cCoverDir = NULL; |
| 422 |
|
| 423 |
// TODO: to check because I think that the class was audacious2 before and not audacious... |
| 424 |
/*gchar *cResult = cairo_dock_launch_command_sync ("which audacious2"); |
| 425 |
if (cResult != NULL && *cResult == '/') |
| 426 |
{ |
| 427 |
cd_debug ("MusicPlayer: Audacious2"); |
| 428 |
pHandler->appclass = "audacious2"; |
| 429 |
pHandler->launch = "audacious2"; |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
cd_debug ("MusicPlayer: Audacious (without 2)"); |
| 434 |
pHandler->appclass = "audacious"; // now it's without this '2' |
| 435 |
pHandler->launch = "audacious"; |
| 436 |
} |
| 437 |
g_free (cResult);*/ |
| 438 |
pHandler->appclass = "audacious"; // desktop file is audasious2.desktop, and command is audacious2, but audacious also exists, and the class is Audacious ... this is really not serious. |
| 439 |
pHandler->launch = "audacious"; |
| 440 |
pHandler->cMprisService = "org.mpris.audacious"; |
| 441 |
pHandler->cMpris2Service = "org.mpris.MediaPlayer2.audacious"; |
| 442 |
pHandler->bSeparateAcquisition = FALSE; |
| 443 |
pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT | PLAYER_STOP | PLAYER_JUMPBOX | PLAYER_SHUFFLE | PLAYER_REPEAT | PLAYER_ENQUEUE | PLAYER_VOLUME; |
| 444 |
pHandler->iLevel = PLAYER_GOOD; |
| 445 |
|
| 446 |
pHandler->cMprisService = "org.mpris.audacious"; |
| 447 |
pHandler->path = "/Player"; |
| 448 |
pHandler->interface = "org.freedesktop.MediaPlayer"; |
| 449 |
pHandler->path2 = "/TrackList"; |
| 450 |
pHandler->interface2 = "org.freedesktop.MediaPlayer"; |
| 451 |
|
| 452 |
cd_musicplayer_register_my_handler(pHandler); |
| 453 |
} |