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