| 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-draw.h" |
| 29 |
|
| 30 |
#include "applet-xmms.h" |
| 31 |
|
| 32 |
|
| 33 |
//Structure et données necessaires |
| 34 |
enum { |
| 35 |
INFO_STATUS = 0, |
| 36 |
INFO_TRACK_IN_PLAYLIST, |
| 37 |
INFO_TIME_ELAPSED_IN_SEC, |
| 38 |
INFO_TIME_ELAPSED, |
| 39 |
INFO_TOTAL_TIME_IN_SEC, |
| 40 |
INFO_TOTAL_TIME, |
| 41 |
INFO_NOW_TITLE, |
| 42 |
NB_INFO |
| 43 |
} AppletInfoEnum; |
| 44 |
|
| 45 |
static char *s_cTmpFile = NULL; |
| 46 |
static int s_pLineNumber[NB_INFO] = {2,4,5,6,7,8,12}; |
| 47 |
|
| 48 |
//Les Fonctions |
| 49 |
static void cd_xmms_free_data (void) { //Permet de libéré la mémoire prise par notre controleur |
| 50 |
cd_debug (""); |
| 51 |
g_free (s_cTmpFile); |
| 52 |
s_cTmpFile = NULL; |
| 53 |
} |
| 54 |
|
| 55 |
static void cd_xmms_control (MyPlayerControl pControl, const gchar *cFile) { //Permet d'effectuer les actions de bases sur le lecteur |
| 56 |
GError *erreur = NULL; |
| 57 |
|
| 58 |
if (pControl != PLAYER_JUMPBOX && pControl != PLAYER_SHUFFLE && pControl != PLAYER_REPEAT && pControl != PLAYER_ENQUEUE) { |
| 59 |
g_free (myData.cRawTitle); |
| 60 |
myData.cRawTitle = NULL; //Reset the title to detect it for sure ;) |
| 61 |
} |
| 62 |
const gchar *cCommand = NULL; |
| 63 |
gchar *cCommand2 = NULL; |
| 64 |
|
| 65 |
switch (pControl) { |
| 66 |
case PLAYER_PREVIOUS : |
| 67 |
cCommand = "xmms -r"; |
| 68 |
break; |
| 69 |
case PLAYER_PLAY_PAUSE : |
| 70 |
cCommand = "xmms -t"; |
| 71 |
break; |
| 72 |
case PLAYER_STOP : |
| 73 |
cCommand = "xmms -s"; |
| 74 |
break; |
| 75 |
case PLAYER_NEXT : |
| 76 |
cCommand = "xmms -f"; |
| 77 |
break; |
| 78 |
case PLAYER_JUMPBOX : |
| 79 |
cCommand = "xmms -j"; |
| 80 |
break; |
| 81 |
case PLAYER_SHUFFLE : |
| 82 |
cCommand = "xmms -S"; |
| 83 |
break; |
| 84 |
case PLAYER_REPEAT : |
| 85 |
cCommand = "xmms -R"; |
| 86 |
break; |
| 87 |
case PLAYER_ENQUEUE : |
| 88 |
if (cFile != NULL) |
| 89 |
cCommand2 = g_strdup_printf ("xmms -e %s", cFile); |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
cd_debug ("Handler XMMS: will use '%s'", cCommand?cCommand:cCommand2); |
| 94 |
g_spawn_command_line_async (cCommand?cCommand:cCommand2, &erreur); |
| 95 |
g_free (cCommand2); |
| 96 |
|
| 97 |
if (erreur != NULL) { |
| 98 |
cd_warning ("MP : when trying to execute command : %s", erreur->message); |
| 99 |
g_error_free (erreur); |
| 100 |
//CD_APPLET_MAKE_TEMPORARY_EMBLEM_CLASSIC (CAIRO_DOCK_EMBLEM_ERROR, CAIRO_DOCK_EMBLEM_UPPER_LEFT, 5000); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
//Fonction de lecture du tuyau. |
| 105 |
static void cd_xmms_read_data (void) { |
| 106 |
s_cTmpFile = g_strdup_printf("/tmp/xmms-info_%s.0",g_getenv ("USER")); |
| 107 |
|
| 108 |
gchar *cContent = NULL; |
| 109 |
gchar *cQuickInfo = NULL; |
| 110 |
gsize length=0; |
| 111 |
GError *erreur = NULL; |
| 112 |
g_file_get_contents (s_cTmpFile, &cContent, &length, &erreur); |
| 113 |
if (erreur != NULL) { |
| 114 |
cd_warning ("MP : %s", erreur->message); |
| 115 |
g_error_free (erreur); |
| 116 |
myData.iPlayingStatus = PLAYER_NONE; |
| 117 |
//cd_musicplayer_player_none (); |
| 118 |
} |
| 119 |
else { |
| 120 |
gchar **cInfopipesList = g_strsplit(cContent, "\n", -1); |
| 121 |
g_free (cContent); |
| 122 |
gchar *cOneInfopipe; |
| 123 |
myData.iTrackNumber = -1; |
| 124 |
myData.iCurrentTime = -1; |
| 125 |
myData.iSongLength = -1; |
| 126 |
int i; |
| 127 |
for (i = 0; cInfopipesList[i] != NULL; i ++) { |
| 128 |
cOneInfopipe = cInfopipesList[i]; |
| 129 |
if (i == s_pLineNumber[INFO_STATUS]) { |
| 130 |
gchar *str = strchr (cOneInfopipe, ' '); |
| 131 |
if (str != NULL) { |
| 132 |
str ++; |
| 133 |
while (*str == ' ') |
| 134 |
str ++; |
| 135 |
if ((strcmp (str, "Playing") == 0) || (strcmp (str, "playing") == 0)) |
| 136 |
myData.iPlayingStatus = PLAYER_PLAYING; |
| 137 |
else if ((strcmp (str, "Paused") == 0) || (strcmp (str, "paused") == 0)) |
| 138 |
myData.iPlayingStatus = PLAYER_PAUSED; |
| 139 |
else if ((strcmp (str, "Stopped") == 0) || (strcmp (str, "stopped") == 0)) |
| 140 |
myData.iPlayingStatus = PLAYER_STOPPED; |
| 141 |
else |
| 142 |
myData.iPlayingStatus = PLAYER_BROKEN; |
| 143 |
} |
| 144 |
else |
| 145 |
myData.iPlayingStatus = PLAYER_BROKEN; |
| 146 |
} |
| 147 |
else if (i == s_pLineNumber[INFO_TRACK_IN_PLAYLIST]) { |
| 148 |
if (myConfig.iQuickInfoType == MY_APPLET_TRACK) { |
| 149 |
gchar *str = strchr (cOneInfopipe, ':'); |
| 150 |
if (str != NULL) { |
| 151 |
str ++; |
| 152 |
while (*str == ' ') |
| 153 |
str ++; |
| 154 |
myData.iTrackNumber = atoi (str); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
else if (i == s_pLineNumber[INFO_TIME_ELAPSED_IN_SEC]) { |
| 159 |
if (myConfig.iQuickInfoType == MY_APPLET_TIME_ELAPSED || myConfig.iQuickInfoType == MY_APPLET_TIME_LEFT) { |
| 160 |
gchar *str = strchr (cOneInfopipe, ' '); |
| 161 |
if (str != NULL) { |
| 162 |
str ++; |
| 163 |
while (*str == ' ') |
| 164 |
str ++; |
| 165 |
if (*str != 'N') |
| 166 |
myData.iCurrentTime = atoi(str) * 1e-3; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
else if (i == s_pLineNumber[INFO_TIME_ELAPSED]) { |
| 171 |
if ((myConfig.iQuickInfoType == MY_APPLET_TIME_ELAPSED || myConfig.iQuickInfoType == MY_APPLET_TIME_LEFT) && myData.iCurrentTime == -1) { |
| 172 |
gchar *str = strchr (cOneInfopipe, ' '); |
| 173 |
if (str != NULL) { |
| 174 |
str ++; |
| 175 |
while (*str == ' ') |
| 176 |
str ++; |
| 177 |
gchar *str2 = strchr (str, ':'); |
| 178 |
if (str2 == NULL) { // pas de minutes. |
| 179 |
myData.iCurrentTime = atoi(str); |
| 180 |
} |
| 181 |
else { |
| 182 |
*str2 = '\0'; |
| 183 |
myData.iCurrentTime = atoi(str2+1) + 60*atoi (str); // prions pour qu'ils n'ecrivent jamais les heures ... xD |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
else if (i == s_pLineNumber[INFO_TOTAL_TIME_IN_SEC]) { |
| 189 |
if (myConfig.iQuickInfoType == MY_APPLET_TIME_LEFT) { |
| 190 |
gchar *str = strchr (cOneInfopipe, ' '); |
| 191 |
if (str != NULL) { |
| 192 |
str ++; |
| 193 |
while (*str == ' ') |
| 194 |
str ++; |
| 195 |
if (*str != 'N') |
| 196 |
myData.iSongLength = atoi(str) * 1e-3; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
else if (i == s_pLineNumber[INFO_TOTAL_TIME]) { |
| 201 |
if (myConfig.iQuickInfoType == MY_APPLET_TIME_LEFT && myData.iSongLength == -1) { |
| 202 |
gchar *str = strchr (cOneInfopipe, ' '); |
| 203 |
if (str != NULL) { |
| 204 |
str ++; |
| 205 |
while (*str == ' ') |
| 206 |
str ++; |
| 207 |
gchar *str2 = strchr (str, ':'); |
| 208 |
if (str2 == NULL) { // pas de minutes. |
| 209 |
myData.iSongLength = atoi(str); |
| 210 |
} |
| 211 |
else { |
| 212 |
*str2 = '\0'; |
| 213 |
myData.iSongLength = atoi(str2+1) + 60*atoi (str); // prions pour qu'ils n'ecrivent jamais les heures ... |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
else if (i == s_pLineNumber[INFO_NOW_TITLE]) { |
| 219 |
gchar *str = strchr (cOneInfopipe, ':'); |
| 220 |
if (str != NULL) { |
| 221 |
str ++; |
| 222 |
while (*str == ' ') |
| 223 |
str ++; |
| 224 |
if ((strcmp(str, "(null)") != 0) && (myData.cRawTitle == NULL || strcmp(str, myData.cRawTitle) != 0)) { |
| 225 |
g_free (myData.cRawTitle); |
| 226 |
myData.cRawTitle = g_strdup (str); |
| 227 |
cd_message ("On a changé de son! (%s)", myData.cRawTitle); |
| 228 |
//cd_musicplayer_change_desklet_data(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} // fin de parcours des lignes. |
| 233 |
g_strfreev (cInfopipesList); |
| 234 |
} |
| 235 |
|
| 236 |
g_free (s_cTmpFile); |
| 237 |
s_cTmpFile = NULL; |
| 238 |
} |
| 239 |
|
| 240 |
void cd_musicplayer_register_xmms_handler (void) { //On enregistre notre lecteurs |
| 241 |
MusicPlayerHandler *pXMMS = g_new0 (MusicPlayerHandler, 1); |
| 242 |
pXMMS->read_data = cd_xmms_read_data; |
| 243 |
pXMMS->free_data = cd_xmms_free_data; |
| 244 |
pXMMS->configure = NULL; //Cette fonction permettera de préparer le controleur |
| 245 |
//Pour les lecteurs utilisants dbus, c'est elle qui connectera le dock aux services des lecteurs etc.. |
| 246 |
pXMMS->control = cd_xmms_control; |
| 247 |
pXMMS->appclass = "xmms"; |
| 248 |
pXMMS->name = "XMMS"; |
| 249 |
pXMMS->iPlayerControls = PLAYER_PREVIOUS | PLAYER_PLAY_PAUSE | PLAYER_NEXT | PLAYER_STOP | PLAYER_JUMPBOX | PLAYER_SHUFFLE | PLAYER_ENQUEUE | PLAYER_REPEAT; |
| 250 |
pXMMS->launch = "xmms"; |
| 251 |
pXMMS->bSeparateAcquisition = TRUE; |
| 252 |
cd_musicplayer_register_my_handler (pXMMS); |
| 253 |
} |