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