~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to xmms/src/applet-infopipe.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
Import upstream version 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Rémy Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
 
26
Fabrice Rey (fabounet@users.berlios.de)
 
27
 
 
28
******************************************************************************/
 
29
#define _BSD_SOURCE
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
#include <unistd.h>
 
33
#include <stdio.h>
 
34
#include <glib/gstdio.h>
 
35
 
 
36
#include "applet-struct.h"
 
37
#include "applet-infopipe.h"
 
38
#include "applet-draw.h"
 
39
 
 
40
 
 
41
static char  *s_cTmpFile = NULL;
 
42
 
 
43
enum {
 
44
        INFO_STATUS = 0,
 
45
        INFO_TRACK_IN_PLAYLIST,
 
46
        INFO_TIME_ELAPSED_IN_SEC,
 
47
        INFO_TIME_ELAPSED,
 
48
        INFO_TOTAL_TIME_IN_SEC,
 
49
        INFO_TOTAL_TIME,
 
50
        INFO_NOW_TITLE,
 
51
        NB_INFO
 
52
} AppletInfoEnum;
 
53
 
 
54
static int s_pLineNumber[MY_NB_PLAYERS][NB_INFO] = {
 
55
        {2,4,5,6,7,8,12} ,
 
56
        {0,1,2,3,4,5,6} ,
 
57
        {0,1,2,3,4,5,6} ,
 
58
        {0,1,2,3,4,5,6} ,
 
59
};
 
60
 
 
61
 
 
62
//Fonction de lecture du tuyau.
 
63
void cd_xmms_read_pipe (CairoDockModuleInstance *myApplet)
 
64
{
 
65
        //\________________________ On determine le pipe.
 
66
        gchar *cPipeCommand = NULL;
 
67
        switch (myConfig.iPlayer) {  // On emule un pipe pour les lecteurs qui n'en ont pas.
 
68
                case MY_XMMS :
 
69
                break ;
 
70
                case MY_AUDACIOUS :  //Il faut émuler le pipe d'audacious par AUDTOOL
 
71
                        cPipeCommand = g_strdup_printf ("bash %s/infoaudacious.sh", MY_APPLET_SHARE_DATA_DIR);
 
72
                break ;
 
73
                case MY_BANSHEE :  //Le pipe est trop lent et cause des freezes... // Il faut émuler le pipe de banshee par le script
 
74
                        cPipeCommand = g_strdup_printf ("bash %s/infobanshee.sh", MY_APPLET_SHARE_DATA_DIR);
 
75
                break ;
 
76
                case MY_EXAILE :  //Le pipe est trop lent, récupération des infos une fois sur deux avec un pique du cpu lors de l'éxécution du script // Il faut émuler le pipe d'audacious par Exaile -q
 
77
                        cPipeCommand = g_strdup_printf ("bash %s/infoexaile.sh", MY_APPLET_SHARE_DATA_DIR);
 
78
                break ;
 
79
                default :
 
80
                break ;
 
81
        }
 
82
        
 
83
        //\________________________ On lit le pipe.
 
84
        gchar *cResult = NULL;
 
85
        if (cPipeCommand != NULL)  // c'est un lecteur avec un pipe emule.
 
86
        {
 
87
                cResult = cairo_dock_launch_command_sync (cPipeCommand);
 
88
                g_free (cPipeCommand);
 
89
        }
 
90
        else if (myConfig.iPlayer == MY_XMMS)  // c'est XMMS, il a deja son propre pipe.
 
91
        {
 
92
                gchar *cPipe = g_strdup_printf ("/tmp/xmms-info_%s.0",g_getenv ("USER"));
 
93
                gsize length=0;
 
94
                GError *erreur = NULL;
 
95
                g_file_get_contents (cPipe, &cResult, &length, &erreur);
 
96
                
 
97
                if (erreur != NULL) {
 
98
                        cd_warning("xmms : %s", erreur->message);
 
99
                        g_error_free(erreur);
 
100
                }
 
101
                
 
102
                g_free (cPipe);
 
103
        }
 
104
        
 
105
        if (cResult == NULL)  // erreur lors de la leture du pipe, on sort.
 
106
        {
 
107
                myData.playingStatus = PLAYER_NONE;
 
108
                cd_xmms_player_none (myApplet);
 
109
                return;
 
110
        }
 
111
        
 
112
        //\________________________ On recupere les donnees.
 
113
        gchar **cInfopipesList = g_strsplit(cResult, "\n", -1);
 
114
        g_free(cResult);
 
115
        
 
116
        gchar *cQuickInfo = NULL;
 
117
        gchar *cOneInfopipe;
 
118
        myData.iTrackNumber = -1;
 
119
        myData.iCurrentTime = -1;
 
120
        myData.iSongLength = -1;
 
121
        int *pLineNumber = s_pLineNumber[myConfig.iPlayer];
 
122
        int i;
 
123
        for (i = 0; cInfopipesList[i] != NULL; i ++) {
 
124
                cOneInfopipe = cInfopipesList[i];
 
125
                if (i == pLineNumber[INFO_STATUS]) {
 
126
                        gchar *str = strchr (cOneInfopipe, ' ');
 
127
                        if (str != NULL) {
 
128
                                str ++;
 
129
                                while (*str == ' ')
 
130
                                        str ++;
 
131
                                if ((strcmp (str, "Playing") == 0) || (strcmp (str, "playing") == 0))
 
132
                                        myData.playingStatus = PLAYER_PLAYING;
 
133
                                else if ((strcmp (str, "Paused") == 0) || (strcmp (str, "paused") == 0))
 
134
                                        myData.playingStatus = PLAYER_PAUSED;
 
135
                                else if ((strcmp (str, "Stopped") == 0) || (strcmp (str, "stopped") == 0))
 
136
                                        myData.playingStatus = PLAYER_STOPPED;
 
137
                                else
 
138
                                        myData.playingStatus = PLAYER_BROKEN;
 
139
                        }
 
140
                        else
 
141
                                myData.playingStatus = PLAYER_BROKEN;
 
142
                }
 
143
                else if (i == pLineNumber[INFO_TRACK_IN_PLAYLIST]) {
 
144
                        if (myConfig.quickInfoType == MY_APPLET_TRACK) {
 
145
                                gchar *str = strchr (cOneInfopipe, ':');
 
146
                                if (str != NULL) {
 
147
                                        str ++;
 
148
                                        while (*str == ' ')
 
149
                                                str ++;
 
150
                                        myData.iTrackNumber = atoi (str);
 
151
                                }
 
152
                        }
 
153
                }
 
154
                else if (i == pLineNumber[INFO_TIME_ELAPSED_IN_SEC]) {
 
155
                        if (myConfig.quickInfoType == MY_APPLET_TIME_ELAPSED || myConfig.quickInfoType == MY_APPLET_TIME_LEFT) {
 
156
                                gchar *str = strchr (cOneInfopipe, ' ');
 
157
                                if (str != NULL) {
 
158
                                        str ++;
 
159
                                        while (*str == ' ')
 
160
                                                str ++;
 
161
                                        if (*str != 'N')
 
162
                                                myData.iCurrentTime = atoi(str) * 1e-3;
 
163
                                }
 
164
                        }
 
165
                }
 
166
                else if (i == pLineNumber[INFO_TIME_ELAPSED]) {
 
167
                        if ((myConfig.quickInfoType == MY_APPLET_TIME_ELAPSED || myConfig.quickInfoType == MY_APPLET_TIME_LEFT) && myData.iCurrentTime == -1) {
 
168
                                gchar *str = strchr (cOneInfopipe, ' ');
 
169
                                if (str != NULL) {
 
170
                                        str ++;
 
171
                                        while (*str == ' ')
 
172
                                                str ++;
 
173
                                        gchar *str2 = strchr (str, ':');
 
174
                                        if (str2 == NULL) { // pas de minutes.
 
175
                                                myData.iCurrentTime = atoi(str);
 
176
                                        }
 
177
                                        else {
 
178
                                                *str2 = '\0';
 
179
                                                myData.iCurrentTime = atoi(str2+1) + 60*atoi (str);  // prions pour qu'ils n'ecrivent jamais les heures ... xD
 
180
                                        }
 
181
                                }
 
182
                        }
 
183
                }
 
184
                else if (i == pLineNumber[INFO_TOTAL_TIME_IN_SEC]) {
 
185
                        if (myConfig.quickInfoType == MY_APPLET_TIME_LEFT) {
 
186
                                gchar *str = strchr (cOneInfopipe, ' ');
 
187
                                if (str != NULL) {
 
188
                                        str ++;
 
189
                                        while (*str == ' ')
 
190
                                                str ++;
 
191
                                        if (*str != 'N')
 
192
                                                myData.iSongLength = atoi(str) * 1e-3;
 
193
                                }
 
194
                        }
 
195
                }
 
196
                else if (i == pLineNumber[INFO_TOTAL_TIME]) {
 
197
                        if (myConfig.quickInfoType == MY_APPLET_TIME_LEFT && myData.iSongLength == -1) {
 
198
                                gchar *str = strchr (cOneInfopipe, ' ');
 
199
                                if (str != NULL) {
 
200
                                        str ++;
 
201
                                        while (*str == ' ')
 
202
                                                str ++;
 
203
                                        gchar *str2 = strchr (str, ':');
 
204
                                        if (str2 == NULL) { // pas de minutes.
 
205
                                                myData.iSongLength = atoi(str);
 
206
                                        }
 
207
                                        else {
 
208
                                                *str2 = '\0';
 
209
                                                myData.iSongLength = atoi(str2+1) + 60*atoi (str);  // prions pour qu'ils n'ecrivent jamais les heures ...
 
210
                                        }
 
211
                                }
 
212
                        }
 
213
                }
 
214
                else if (i == pLineNumber[INFO_NOW_TITLE]) {
 
215
                        gchar *str = strchr (cOneInfopipe, ':');
 
216
                        if (str != NULL) {
 
217
                                str ++;
 
218
                                while (*str == ' ')
 
219
                                        str ++;
 
220
                                if ((strcmp(str," (null)") != 0) && (myData.playingTitle == NULL || strcmp(str, myData.playingTitle) != 0)) {
 
221
                                        g_free (myData.playingTitle);
 
222
                                        myData.playingTitle = g_strdup (str);
 
223
                                        cd_message("On a changé de son! (%s)", myData.playingTitle);
 
224
                                        cd_xmms_change_desklet_data(myApplet);
 
225
                                }
 
226
                        }
 
227
                }
 
228
        }  // fin de parcours des lignes.
 
229
        g_strfreev (cInfopipesList);
 
230
        
 
231
        if (myConfig.iPlayer != MY_XMMS) {
 
232
                g_remove (s_cTmpFile);
 
233
        }
 
234
        g_free (s_cTmpFile);
 
235
        s_cTmpFile = NULL;
 
236
}
 
237