| 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-exaile.h" |
| 33 |
|
| 34 |
/* |
| 35 |
myData.DBus_commands.service = "org.exaile.DBusInterface"; |
| 36 |
myData.DBus_commands.path = "/DBusInterfaceObject"; |
| 37 |
myData.DBus_commands.interface = "org.exaile.DBusInterface"; |
| 38 |
myData.DBus_commands.play = "play_pause"; |
| 39 |
myData.DBus_commands.pause = "play_pause"; |
| 40 |
myData.DBus_commands.stop = "stop"; |
| 41 |
myData.DBus_commands.next = "next_track"; |
| 42 |
myData.DBus_commands.previous = "prev_track"; |
| 43 |
myData.DBus_commands.get_title = "get_title"; |
| 44 |
myData.DBus_commands.get_artist = "get_artist"; |
| 45 |
myData.DBus_commands.get_album = "get_album"; |
| 46 |
myData.DBus_commands.get_cover_path = "get_cover_path"; |
| 47 |
myData.DBus_commands.get_status = "status"; |
| 48 |
myData.DBus_commands.duration = "get_length"; |
| 49 |
myData.DBus_commands.current_position = "current_position"; |
| 50 |
get_rating/set_rating |
| 51 |
*/ |
| 52 |
|
| 53 |
#define MP_DBUS_TYPE_PLAYER_STATUS G_TYPE_STRING |
| 54 |
|
| 55 |
static inline int _get_time_from_string (const gchar *cTime) // mm:ss |
| 56 |
{ |
| 57 |
int s=0, m = atoi (cTime); |
| 58 |
gchar *str = strchr (cTime, ':'); |
| 59 |
if (str) |
| 60 |
{ |
| 61 |
s = atoi (str+1); |
| 62 |
} |
| 63 |
return (60 * m + s); |
| 64 |
} |
| 65 |
static void cd_exaile_getSongInfos(void) |
| 66 |
{ |
| 67 |
gchar *cQuery = cairo_dock_dbus_get_string (myData.dbus_proxy_player, "query"); |
| 68 |
cd_debug ("MP : query : %s", cQuery); // status: playing self: Jiken artist: Yoshihisa Hirano, Hideki Taniuchi album: Death Note Original Soundtrack length: 2:49 position: %4 [0:07] |
| 69 |
if (cQuery == NULL) |
| 70 |
{ |
| 71 |
myData.iPlayingStatus = PLAYER_STOPPED; |
| 72 |
g_free (myData.cPlayingUri); |
| 73 |
myData.cPlayingUri = NULL; |
| 74 |
g_free (myData.cTitle); |
| 75 |
myData.cTitle = NULL; |
| 76 |
g_free (myData.cAlbum); |
| 77 |
myData.cAlbum = NULL; |
| 78 |
g_free (myData.cArtist); |
| 79 |
myData.cArtist = NULL; |
| 80 |
g_free (myData.cCoverPath); |
| 81 |
myData.cCoverPath = NULL; |
| 82 |
return ; |
| 83 |
} |
| 84 |
gchar *str, *str2; |
| 85 |
|
| 86 |
str = g_strstr_len (cQuery, -1, "status:"); |
| 87 |
g_return_if_fail (str != NULL); |
| 88 |
str += 8; |
| 89 |
if (strncmp (str, "playing", 7) == 0) |
| 90 |
myData.iPlayingStatus = PLAYER_PLAYING; |
| 91 |
else if (strncmp (str, "paused", 6) == 0) |
| 92 |
myData.iPlayingStatus = PLAYER_PAUSED; |
| 93 |
else |
| 94 |
myData.iPlayingStatus = PLAYER_STOPPED; |
| 95 |
cd_debug (" iPlayingStatus <- %d", myData.iPlayingStatus); |
| 96 |
if (myData.iPlayingStatus != PLAYER_PLAYING) |
| 97 |
{ |
| 98 |
cd_debug ("exaile ne joue rien, on quitte\n"); |
| 99 |
g_free (cQuery); |
| 100 |
return ; |
| 101 |
} |
| 102 |
|
| 103 |
str = g_strstr_len (str, -1, "self:"); |
| 104 |
g_return_if_fail (str != NULL); |
| 105 |
str += 6; |
| 106 |
str2 = g_strstr_len (str, -1, "artist:"); |
| 107 |
g_return_if_fail (str2 != NULL); |
| 108 |
g_free (myData.cTitle); |
| 109 |
myData.cTitle = g_strndup (str, str2 - str); |
| 110 |
cd_debug (" cTitle <- %s", myData.cTitle); |
| 111 |
|
| 112 |
str = str2 + 8; |
| 113 |
str2 = g_strstr_len (str, -1, "album:"); |
| 114 |
g_return_if_fail (str2 != NULL); |
| 115 |
g_free (myData.cArtist); |
| 116 |
myData.cArtist = g_strndup (str, str2 - str); |
| 117 |
cd_debug (" cArtist <- %s", myData.cArtist); |
| 118 |
|
| 119 |
str = str2 + 7; |
| 120 |
str2 = g_strstr_len (str, -1, "length:"); |
| 121 |
g_return_if_fail (str2 != NULL); |
| 122 |
g_free (myData.cAlbum); |
| 123 |
myData.cAlbum = g_strndup (str, str2 - str); |
| 124 |
cd_debug (" cAlbum <- %s", myData.cAlbum); |
| 125 |
|
| 126 |
str = str2 + 8; |
| 127 |
str2 = g_strstr_len (str, -1, "position:"); |
| 128 |
g_return_if_fail (str2 != NULL); |
| 129 |
myData.iSongLength = _get_time_from_string (str); |
| 130 |
cd_debug (" iSongLength <- %d", myData.iSongLength); |
| 131 |
|
| 132 |
str = str2 + 10; |
| 133 |
str = strchr (str, '['); |
| 134 |
g_return_if_fail (str != NULL); |
| 135 |
myData.iCurrentTime = _get_time_from_string (str+1); |
| 136 |
cd_debug (" iCurrentTime <- %d", myData.iCurrentTime); |
| 137 |
|
| 138 |
g_free (cQuery); |
| 139 |
|
| 140 |
g_free (myData.cRawTitle); |
| 141 |
myData.cRawTitle = g_strdup_printf ("%s - %s", myData.cAlbum, myData.cTitle); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
static void cd_exaile_getCoverPath (void) |
| 146 |
{ |
| 147 |
gchar *cCoverPath = cairo_dock_dbus_get_string (myData.dbus_proxy_player, "get_cover_path"); |
| 148 |
if (g_str_has_suffix (cCoverPath, "nocover.png")) |
| 149 |
{ |
| 150 |
g_free (cCoverPath); |
| 151 |
cCoverPath = NULL; |
| 152 |
} |
| 153 |
if (cCoverPath != NULL) |
| 154 |
cd_debug ("MP : Couverture de exaile : %s", cCoverPath); /// gerer le cas "nocover.jpg" ... |
| 155 |
else |
| 156 |
cd_debug ("MP : Pas de couverture chez exaile\n"); |
| 157 |
cd_musicplayer_set_cover_path (cCoverPath); |
| 158 |
g_free (cCoverPath); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
//////////////////////////// |
| 163 |
// Definition du backend. // |
| 164 |
//////////////////////////// |
| 165 |
|
| 166 |
/* Controle du lecteur |
| 167 |
*/ |
| 168 |
static void cd_exaile_control (MyPlayerControl pControl, const char* file) |
| 169 |
{ |
| 170 |
const gchar *cCommand = NULL; |
| 171 |
|
| 172 |
switch (pControl) { |
| 173 |
case PLAYER_PREVIOUS : |
| 174 |
cCommand = "prev_track"; |
| 175 |
break; |
| 176 |
|
| 177 |
case PLAYER_PLAY_PAUSE : |
| 178 |
cCommand = "play_pause"; |
| 179 |
break; |
| 180 |
|
| 181 |
case PLAYER_NEXT : |
| 182 |
cCommand = "next_track"; |
| 183 |
break; |
| 184 |
|
| 185 |
default : |
| 186 |
return; |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
if (cCommand != NULL) |
| 191 |
{ |
| 192 |
cd_debug ("MP : will use '%s'", cCommand); |
| 193 |
cairo_dock_dbus_call(myData.dbus_proxy_player, cCommand); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* Recupere tout chaque seconde (aucun signal). |
| 198 |
*/ |
| 199 |
static void cd_exaile_get_data (void) |
| 200 |
{ |
| 201 |
cd_debug ("Exaile is running\n"); |
| 202 |
cd_exaile_getSongInfos (); |
| 203 |
if (myData.iPlayingStatus == PLAYER_PLAYING && cairo_dock_strings_differ (myData.cRawTitle, myData.cPreviousRawTitle)) |
| 204 |
cd_exaile_getCoverPath (); |
| 205 |
else if (myData.iPlayingStatus == PLAYER_STOPPED) // en pause le temps et la chanson reste constants. |
| 206 |
{ |
| 207 |
myData.iCurrentTime = 0; |
| 208 |
} |
| 209 |
cd_message (" myData.iCurrentTime <- %d", __func__, myData.iCurrentTime); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
/* Initialise le backend de EX. |
| 214 |
*/ |
| 215 |
static void cd_exaile_start (void) |
| 216 |
{ |
| 217 |
// get the current state. |
| 218 |
cd_exaile_getSongInfos (); |
| 219 |
cd_exaile_getCoverPath (); |
| 220 |
cd_musicplayer_update_icon (); |
| 221 |
} |
| 222 |
|
| 223 |
/* On enregistre notre lecteur. |
| 224 |
*/ |
| 225 |
void cd_musicplayer_register_exaile_handler (void) { //On enregistre notre lecteur |
| 226 |
//cd_debug (""); |
| 227 |
MusicPlayerHandler *pHandler = g_new0 (MusicPlayerHandler, 1); |
| 228 |
pHandler->name = "Exaile"; |
| 229 |
pHandler->get_data = cd_exaile_get_data; |
| 230 |
pHandler->stop = NULL; |
| 231 |
pHandler->start = cd_exaile_start; // we could also let the loop get the state on the first iteration. |
| 232 |
pHandler->control = cd_exaile_control; |
| 233 |
pHandler->get_cover = NULL; |
| 234 |
pHandler->cCoverDir = NULL; /// visiblement il sait gerer les covers, sauf que je l'ai jamais vu en afficher une... |
| 235 |
|
| 236 |
pHandler->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT; |
| 237 |
pHandler->appclass = "exaile.py"; |
| 238 |
pHandler->launch = "exaile"; |
| 239 |
pHandler->cMprisService = "org.exaile.DBusInterface"; |
| 240 |
pHandler->bSeparateAcquisition = FALSE; |
| 241 |
pHandler->iLevel = PLAYER_BAD; // API DBus moisie sans aucun signal. |
| 242 |
|
| 243 |
pHandler->cMprisService = "org.exaile.DBusInterface"; |
| 244 |
pHandler->path = "/DBusInterfaceObject"; |
| 245 |
pHandler->interface = "org.exaile.DBusInterface"; |
| 246 |
pHandler->path2 = NULL; |
| 247 |
pHandler->interface2 = NULL; |
| 248 |
|
| 249 |
cd_musicplayer_register_my_handler (pHandler); |
| 250 |
} |