d468acb655bece4ee4302172bb19d9592898f64a
[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         current_info->conn =
12                         mpd_newConnection(current_info->mpd.host,
13                                           current_info->mpd.port, 10);
14         if (current_info->conn->error) {
15                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
16                 mpd_closeConnection(current_info->conn);
17                 if (current_info->mpd.artist == NULL)
18                         current_info->mpd.artist =
19                                         malloc(TEXT_BUFFER_SIZE);
20                 if (current_info->mpd.album == NULL)
21                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
22                 if (current_info->mpd.title == NULL)
23                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
24                 if (current_info->mpd.random == NULL)
25                         current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
26                 if (current_info->mpd.repeat == NULL)
27                         current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
28                 if (current_info->mpd.track == NULL)
29                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
30                 strcpy(current_info->mpd.artist, "Unknown");
31                 strcpy(current_info->mpd.album, "Unknown");
32                 strcpy(current_info->mpd.title, "Unknown");
33                 strcpy(current_info->mpd.random, "Unknown");
34                 strcpy(current_info->mpd.repeat, "Unknown");
35                 strcpy(current_info->mpd.track, "Unknown");
36                 current_info->mpd.status = "MPD not responding";
37                 current_info->mpd.bitrate = 0;
38                 current_info->mpd.progress = 0;
39                 current_info->mpd.elapsed = 0;
40                 current_info->mpd.length = 0;
41                 return;
42         }
43
44         mpd_Status *status;
45         mpd_InfoEntity *entity;
46         mpd_sendCommandListOkBegin(current_info->conn);
47         mpd_sendStatusCommand(current_info->conn);
48         mpd_sendCurrentSongCommand(current_info->conn);
49         mpd_sendCommandListEnd(current_info->conn);
50         if ((status = mpd_getStatus(current_info->conn)) == NULL) {
51                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
52                 mpd_closeConnection(current_info->conn);
53                 if (current_info->mpd.artist == NULL)
54                         current_info->mpd.artist =
55                                         malloc(TEXT_BUFFER_SIZE);
56                 if (current_info->mpd.album == NULL)
57                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
58                 if (current_info->mpd.title == NULL)
59                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
60                 if (current_info->mpd.random == NULL)
61                         current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
62                 if (current_info->mpd.track == NULL)
63                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
64                 strcpy(current_info->mpd.artist, "Unknown");
65                 strcpy(current_info->mpd.album, "Unknown");
66                 strcpy(current_info->mpd.title, "Unknown");
67                 strcpy(current_info->mpd.random, "Unknown");
68                 strcpy(current_info->mpd.repeat, "Unknown");
69                 strcpy(current_info->mpd.track, "Unknown");
70                 current_info->mpd.status = "MPD not responding";
71                 current_info->mpd.bitrate = 0;
72                 current_info->mpd.progress = 0;
73                 current_info->mpd.elapsed = 0;
74                 current_info->mpd.length = 0;
75                 current_info->mpd.track = 0;
76                 return;
77         }
78         current_info->mpd.volume = status->volume;
79         //if (status->error)
80         //printf("error: %s\n", status->error);
81
82         if (status->state == MPD_STATUS_STATE_PLAY) {
83                 current_info->mpd.status = "Playing";
84         }
85         if (status->state == MPD_STATUS_STATE_STOP) {
86                 current_info->mpd.status = "Stopped";
87                 current_info->mpd.bitrate = 0;
88                 current_info->mpd.progress = 0;
89                 current_info->mpd.elapsed = 0;
90                 current_info->mpd.length = 0;
91                 current_info->mpd.track = 0;
92                 if (current_info->mpd.artist == NULL)
93                         current_info->mpd.artist =
94                                         malloc(TEXT_BUFFER_SIZE);
95                 if (current_info->mpd.album == NULL)
96                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
97                 if (current_info->mpd.title == NULL)
98                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
99                 if (current_info->mpd.random == NULL)
100                         current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
101                 if (current_info->mpd.repeat == NULL)
102                         current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
103                 if (current_info->mpd.track == NULL)
104                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
105                 strcpy(current_info->mpd.artist, "Stopped");
106                 strcpy(current_info->mpd.album, "Stopped");
107                 strcpy(current_info->mpd.title, "Stopped");
108                 strcpy(current_info->mpd.random, "Stopped");
109                 strcpy(current_info->mpd.repeat, "Stopped");
110                 strcpy(current_info->mpd.track, "Stopped");
111         }
112         if (status->state == MPD_STATUS_STATE_PAUSE) {
113                 current_info->mpd.status = "Paused";
114         }
115         if (status->state == MPD_STATUS_STATE_UNKNOWN) {
116                 current_info->mpd.status = "Unknown";
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                 current_info->mpd.track = 0;
122                 if (current_info->mpd.artist == NULL)
123                         current_info->mpd.artist =
124                                         malloc(TEXT_BUFFER_SIZE);
125                 if (current_info->mpd.album == NULL)
126                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
127                 if (current_info->mpd.title == NULL)
128                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
129                 if (current_info->mpd.random == NULL)
130                         current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
131                 if (current_info->mpd.repeat == NULL)
132                         current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
133                 if (current_info->mpd.track == NULL)
134                         current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
135                 strcpy(current_info->mpd.artist, "Unknown");
136                 strcpy(current_info->mpd.album, "Unknown");
137                 strcpy(current_info->mpd.title, "Unknown");
138                 strcpy(current_info->mpd.random, "Unknown");
139                 strcpy(current_info->mpd.repeat, "Unknown");
140                 strcpy(current_info->mpd.track, "Unknown");
141         }
142         if (status->state == MPD_STATUS_STATE_PLAY ||
143                    status->state == MPD_STATUS_STATE_PAUSE) {
144                 current_info->mpd.bitrate = status->bitRate;
145                 current_info->mpd.progress =
146                                 (float) status->elapsedTime / status->totalTime;
147                 current_info->mpd.elapsed = status->elapsedTime;
148                 current_info->mpd.length = status->totalTime;
149         if (current_info->mpd.random == NULL)
150             current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
151         if (current_info->mpd.repeat == NULL)
152             current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
153         if (status->random == 0) {
154             strcpy(current_info->mpd.random, "Off");
155         } else if (status->random == 1){
156             strcpy(current_info->mpd.random, "On");
157         } else {
158             strcpy(current_info->mpd.random, "Unknown");
159         }
160         if (status->repeat == 0) {
161             strcpy(current_info->mpd.repeat, "Off");
162         } else if (status->repeat == 1){
163             strcpy(current_info->mpd.repeat, "On");
164         } else {
165             strcpy(current_info->mpd.repeat, "Unknown");
166             }
167     }
168         
169         
170         
171                    if (current_info->conn->error) {
172                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
173                            mpd_closeConnection(current_info->conn);
174                            return;
175                    }
176
177                    mpd_nextListOkCommand(current_info->conn);
178
179                    while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
180                            mpd_Song *song = entity->info.song;
181                            if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
182                                    mpd_freeInfoEntity(entity);
183                                    continue;
184                            }
185
186                            if (current_info->mpd.artist == NULL)
187                                    current_info->mpd.artist =
188                                                    malloc(TEXT_BUFFER_SIZE);
189                            if (current_info->mpd.album == NULL)
190                                    current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
191                            if (current_info->mpd.title == NULL)
192                                    current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
193                            if (current_info->mpd.track == NULL)
194                                    current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
195                            if (song->artist) {
196                                    strcpy(current_info->mpd.artist, song->artist);
197                            } else {
198                                    strcpy(current_info->mpd.artist, "Unknown");
199                            }
200                            if (song->album) {
201                                    strcpy(current_info->mpd.album, song->album);
202                            } else {
203                                    strcpy(current_info->mpd.album, "Unknown");
204                            }
205                            if (song->title) {
206                                    strcpy(current_info->mpd.title, song->title);
207                            } else {
208                                    strcpy(current_info->mpd.title, "Unknown");
209                            }
210                            if (song->track) {
211                                    strcpy(current_info->mpd.track, song->track);
212                            } else {
213                                    strcpy(current_info->mpd.track, "Unknown");
214                    }
215                            if (entity != NULL) {
216                                    mpd_freeInfoEntity(entity);
217                            }
218                    }
219                    if (entity != NULL) {
220                            mpd_freeInfoEntity(entity);
221                    }
222
223                    if (current_info->conn->error) {
224                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
225                            mpd_closeConnection(current_info->conn);
226                            return;
227                    }
228
229                    mpd_finishCommand(current_info->conn);
230                    if (current_info->conn->error) {
231                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
232                            mpd_closeConnection(current_info->conn);
233                            return;
234                    }
235                    mpd_freeStatus(status);
236                    mpd_closeConnection(current_info->conn);
237     }