Experimental RSS code.
[monky] / src / mpd.c
1 /*
2  * mpd.c: MPD stuff for Conky
3  *
4  * $Id$
5  */
6
7 #include "conky.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "libmpdclient.h"
12
13
14 void update_mpd()
15 {
16         struct information *current_info = &info;
17         if (current_info->conn == NULL) {
18                 current_info->conn = mpd_newConnection(current_info->mpd.host, current_info->mpd.port, 10);
19         }
20         if (strlen(current_info->mpd.password) > 1) {
21                 mpd_sendPasswordCommand(current_info->conn,
22                                         current_info->mpd.password);
23                 mpd_finishCommand(current_info->conn);
24         }
25         if (current_info->conn->error) {
26                 //ERR("%MPD error: s\n", current_info->conn->errorStr);
27                 mpd_closeConnection(current_info->conn);
28                 current_info->conn = 0;
29                 if (current_info->mpd.artist == NULL)
30                         current_info->mpd.artist =
31                             malloc(TEXT_BUFFER_SIZE);
32                 if (current_info->mpd.album == NULL)
33                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
34                 if (current_info->mpd.title == NULL)
35                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
36                 if (current_info->mpd.random == NULL)
37                         current_info->mpd.random =
38                             malloc(TEXT_BUFFER_SIZE);
39                 if (current_info->mpd.repeat == NULL)
40                         current_info->mpd.repeat =
41                             malloc(TEXT_BUFFER_SIZE);
42                 if (current_info->mpd.track == NULL)
43                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
44                 if (current_info->mpd.status == NULL)
45                         current_info->mpd.status =
46                             malloc(TEXT_BUFFER_SIZE);
47                 if (current_info->mpd.name == NULL)
48                         current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
49                 if (current_info->mpd.file == NULL)
50                         current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
51                 *current_info->mpd.name=0;
52                 *current_info->mpd.file=0;
53                 *current_info->mpd.artist=0;
54                 *current_info->mpd.album=0;
55                 *current_info->mpd.title=0;
56                 *current_info->mpd.random=0;
57                 *current_info->mpd.repeat=0;
58                 *current_info->mpd.track=0;
59                 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
60                 current_info->mpd.bitrate = 0;
61                 current_info->mpd.progress = 0;
62                 current_info->mpd.elapsed = 0;
63                 current_info->mpd.length = 0;
64                 return;
65         }
66
67         mpd_Status *status;
68         mpd_InfoEntity *entity;
69         mpd_sendCommandListOkBegin(current_info->conn);
70         mpd_sendStatusCommand(current_info->conn);
71         mpd_sendCurrentSongCommand(current_info->conn);
72         mpd_sendCommandListEnd(current_info->conn);
73         if ((status = mpd_getStatus(current_info->conn)) == NULL) {
74                 //ERR("MPD error: %s\n", current_info->conn->errorStr);
75                 mpd_closeConnection(current_info->conn);
76                 current_info->conn = 0;
77                 if (current_info->mpd.artist == NULL)
78                         current_info->mpd.artist =
79                             malloc(TEXT_BUFFER_SIZE);
80                 if (current_info->mpd.album == NULL)
81                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
82                 if (current_info->mpd.title == NULL)
83                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
84                 if (current_info->mpd.random == NULL)
85                         current_info->mpd.random =
86                             malloc(TEXT_BUFFER_SIZE);
87                 if (current_info->mpd.repeat == NULL)
88                         current_info->mpd.repeat =
89                             malloc(TEXT_BUFFER_SIZE);
90                 if (current_info->mpd.track == NULL)
91                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
92                 if (current_info->mpd.status == NULL)
93                         current_info->mpd.status = malloc(TEXT_BUFFER_SIZE);
94                 if (current_info->mpd.name == NULL)
95                         current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
96                 if (current_info->mpd.file == NULL)
97                         current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
98                 *current_info->mpd.name=0;
99                 *current_info->mpd.file=0;
100                 *current_info->mpd.artist=0;
101                 *current_info->mpd.album=0;
102                 *current_info->mpd.title=0;
103                 *current_info->mpd.random=0;
104                 *current_info->mpd.repeat=0;
105                 *current_info->mpd.track=0;
106                 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
107                 current_info->mpd.bitrate = 0;
108                 current_info->mpd.progress = 0;
109                 current_info->mpd.elapsed = 0;
110                 current_info->mpd.length = 0;
111                 return;
112         }
113         current_info->mpd.volume = status->volume;
114         //if (status->error)
115         //printf("error: %s\n", status->error);
116
117         if (status->state == MPD_STATUS_STATE_PLAY) {
118                 if (current_info->mpd.status == NULL)
119                         current_info->mpd.status =
120                             malloc(TEXT_BUFFER_SIZE);
121                 strncpy(current_info->mpd.status, "Playing",
122                         TEXT_BUFFER_SIZE - 1);
123         }
124         if (status->state == MPD_STATUS_STATE_STOP) {
125                 current_info->mpd.bitrate = 0;
126                 current_info->mpd.progress = 0;
127                 current_info->mpd.elapsed = 0;
128                 current_info->mpd.length = 0;
129                 if (current_info->mpd.artist == NULL)
130                         current_info->mpd.artist =
131                             malloc(TEXT_BUFFER_SIZE);
132                 if (current_info->mpd.album == NULL)
133                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
134                 if (current_info->mpd.title == NULL)
135                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
136                 if (current_info->mpd.random == NULL)
137                         current_info->mpd.random =
138                             malloc(TEXT_BUFFER_SIZE);
139                 if (current_info->mpd.repeat == NULL)
140                         current_info->mpd.repeat =
141                             malloc(TEXT_BUFFER_SIZE);
142                 if (current_info->mpd.track == NULL)
143                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
144                 if (current_info->mpd.status == NULL)
145                         current_info->mpd.status =
146                             malloc(TEXT_BUFFER_SIZE);
147                 if (current_info->mpd.name == NULL)
148                         current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
149                 if (current_info->mpd.file == NULL)
150                         current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
151                 *current_info->mpd.name=0;
152                 *current_info->mpd.file=0;
153                 *current_info->mpd.artist=0;
154                 *current_info->mpd.album=0;
155                 *current_info->mpd.title=0;
156                 *current_info->mpd.random=0;
157                 *current_info->mpd.repeat=0;
158                 *current_info->mpd.track=0;
159                 strncpy(current_info->mpd.status, "Stopped",
160                         TEXT_BUFFER_SIZE - 1);
161         }
162         if (status->state == MPD_STATUS_STATE_PAUSE) {
163                 if (current_info->mpd.status == NULL)
164                         current_info->mpd.status =
165                             malloc(TEXT_BUFFER_SIZE);
166                 strncpy(current_info->mpd.status, "Paused",
167                         TEXT_BUFFER_SIZE - 1);
168         }
169         if (status->state == MPD_STATUS_STATE_UNKNOWN) {
170                 current_info->mpd.bitrate = 0;
171                 current_info->mpd.progress = 0;
172                 current_info->mpd.elapsed = 0;
173                 current_info->mpd.length = 0;
174                 if (current_info->mpd.artist == NULL)
175                         current_info->mpd.artist =
176                             malloc(TEXT_BUFFER_SIZE);
177                 if (current_info->mpd.album == NULL)
178                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
179                 if (current_info->mpd.title == NULL)
180                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
181                 if (current_info->mpd.random == NULL)
182                         current_info->mpd.random =
183                             malloc(TEXT_BUFFER_SIZE);
184                 if (current_info->mpd.repeat == NULL)
185                         current_info->mpd.repeat =
186                             malloc(TEXT_BUFFER_SIZE);
187                 if (current_info->mpd.track == NULL)
188                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
189                 if (current_info->mpd.status == NULL)
190                         current_info->mpd.status =
191                             malloc(TEXT_BUFFER_SIZE);
192                 if (current_info->mpd.name == NULL)
193                         current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
194                 if (current_info->mpd.file == NULL)
195                         current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
196                 *current_info->mpd.name=0;
197                 *current_info->mpd.file=0;
198                 *current_info->mpd.artist=0;
199                 *current_info->mpd.album=0;
200                 *current_info->mpd.title=0;
201                 *current_info->mpd.random=0;
202                 *current_info->mpd.repeat=0;
203                 *current_info->mpd.track=0;
204                 *current_info->mpd.status=0;
205         }
206         if (status->state == MPD_STATUS_STATE_PLAY ||
207             status->state == MPD_STATUS_STATE_PAUSE) {
208                 current_info->mpd.bitrate = status->bitRate;
209                 current_info->mpd.progress =
210                     (float) status->elapsedTime / status->totalTime;
211                 current_info->mpd.elapsed = status->elapsedTime;
212                 current_info->mpd.length = status->totalTime;
213                 if (current_info->mpd.random == NULL)
214                         current_info->mpd.random =
215                             malloc(TEXT_BUFFER_SIZE);
216                 if (current_info->mpd.repeat == NULL)
217                         current_info->mpd.repeat =
218                             malloc(TEXT_BUFFER_SIZE);
219                 if (status->random == 0) {
220                         strcpy(current_info->mpd.random, "Off");
221                 } else if (status->random == 1) {
222                         strcpy(current_info->mpd.random, "On");
223                 } else {
224                         *current_info->mpd.random=0;
225                 }
226                 if (status->repeat == 0) {
227                         strcpy(current_info->mpd.repeat, "Off");
228                 } else if (status->repeat == 1) {
229                         strcpy(current_info->mpd.repeat, "On");
230                 } else {
231                         *current_info->mpd.repeat=0;
232                 }
233         }
234
235         if (current_info->conn->error) {
236                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
237                 mpd_closeConnection(current_info->conn);
238                 current_info->conn = 0;
239                 return;
240         }
241
242         mpd_nextListOkCommand(current_info->conn);
243
244         while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
245                 mpd_Song *song = entity->info.song;
246                 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
247                         mpd_freeInfoEntity(entity);
248                         continue;
249                 }
250
251                 if (current_info->mpd.artist == NULL)
252                         current_info->mpd.artist =
253                             malloc(TEXT_BUFFER_SIZE);
254                 if (current_info->mpd.album == NULL)
255                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
256                 if (current_info->mpd.title == NULL)
257                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
258                 if (current_info->mpd.track == NULL)
259                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
260                 if (current_info->mpd.name == NULL)
261                         current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
262                 if (current_info->mpd.file == NULL)
263                         current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
264                 if (song->artist) {
265                         strncpy(current_info->mpd.artist, song->artist,
266                                 TEXT_BUFFER_SIZE - 1);
267                 } else {
268                         *current_info->mpd.artist=0;
269                 }
270                 if (song->album) {
271                         strncpy(current_info->mpd.album, song->album,
272                                 TEXT_BUFFER_SIZE - 1);
273                 } else {
274                         *current_info->mpd.album=0;
275                 }
276                 if (song->title) {
277                         strncpy(current_info->mpd.title, song->title,
278                                 TEXT_BUFFER_SIZE - 1);
279                 } else {
280                         *current_info->mpd.title=0;
281                 }
282                 if (song->track) {
283                         strncpy(current_info->mpd.track, song->track,
284                                 TEXT_BUFFER_SIZE - 1);
285                 } else {
286                         *current_info->mpd.track=0;
287                 }
288                 if (song->name) {
289                         strncpy(current_info->mpd.name, song->name,
290                                 TEXT_BUFFER_SIZE - 1);
291                 } else {
292                         *current_info->mpd.name=0;
293                 }
294                 if (song->file) {
295                         strncpy(current_info->mpd.file,
296                                 song->file, TEXT_BUFFER_SIZE - 1);
297                 } else {
298                         *current_info->mpd.file=0;
299                 }
300                 if (entity != NULL) {
301                         mpd_freeInfoEntity(entity);
302                         entity = NULL;
303                 }
304         }
305         if (entity != NULL) {
306                 mpd_freeInfoEntity(entity);
307                 entity = NULL;
308         }
309
310         if (current_info->conn->error) {
311                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
312                 mpd_closeConnection(current_info->conn);
313                 current_info->conn = 0;
314                 return;
315         }
316
317         mpd_finishCommand(current_info->conn);
318         if (current_info->conn->error) {
319                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
320                 mpd_closeConnection(current_info->conn);
321                 current_info->conn = 0;
322                 return;
323         }
324         mpd_freeStatus(status);
325 //      mpd_closeConnection(current_info->conn);
326 }