fixed graph width issue
[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                 strcpy(current_info->mpd.artist, "Unknown");
25                 strcpy(current_info->mpd.album, "Unknown");
26                 strcpy(current_info->mpd.title, "Unknown");
27                 current_info->mpd.status = "MPD not responding";
28                 current_info->mpd.bitrate = 0;
29                 current_info->mpd.progress = 0;
30                 current_info->mpd.elapsed = 0;
31                 current_info->mpd.length = 0;
32                 return;
33         }
34
35         mpd_Status *status;
36         mpd_InfoEntity *entity;
37         mpd_sendCommandListOkBegin(current_info->conn);
38         mpd_sendStatusCommand(current_info->conn);
39         mpd_sendCurrentSongCommand(current_info->conn);
40         mpd_sendCommandListEnd(current_info->conn);
41         if ((status = mpd_getStatus(current_info->conn)) == NULL) {
42                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
43                 mpd_closeConnection(current_info->conn);
44                 if (current_info->mpd.artist == NULL)
45                         current_info->mpd.artist =
46                                         malloc(TEXT_BUFFER_SIZE);
47                 if (current_info->mpd.album == NULL)
48                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
49                 if (current_info->mpd.title == NULL)
50                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
51                 strcpy(current_info->mpd.artist, "Unknown");
52                 strcpy(current_info->mpd.album, "Unknown");
53                 strcpy(current_info->mpd.title, "Unknown");
54                 current_info->mpd.status = "MPD not responding";
55                 current_info->mpd.bitrate = 0;
56                 current_info->mpd.progress = 0;
57                 current_info->mpd.elapsed = 0;
58                 current_info->mpd.length = 0;
59                 return;
60         }
61         current_info->mpd.volume = status->volume;
62         //if (status->error)
63         //printf("error: %s\n", status->error);
64
65         if (status->state == MPD_STATUS_STATE_PLAY) {
66                 current_info->mpd.status = "Playing";
67         }
68         if (status->state == MPD_STATUS_STATE_STOP) {
69                 current_info->mpd.status = "Stopped";
70                 current_info->mpd.bitrate = 0;
71                 current_info->mpd.progress = 0;
72                 current_info->mpd.elapsed = 0;
73                 current_info->mpd.length = 0;
74                 if (current_info->mpd.artist == NULL)
75                         current_info->mpd.artist =
76                                         malloc(TEXT_BUFFER_SIZE);
77                 if (current_info->mpd.album == NULL)
78                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
79                 if (current_info->mpd.title == NULL)
80                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
81                 strcpy(current_info->mpd.artist, "Stopped");
82                 strcpy(current_info->mpd.album, "Stopped");
83                 strcpy(current_info->mpd.title, "Stopped");
84         }
85         if (status->state == MPD_STATUS_STATE_PAUSE) {
86                 current_info->mpd.status = "Paused";
87         }
88         if (status->state == MPD_STATUS_STATE_UNKNOWN) {
89                 current_info->mpd.status = "Unknown";
90                 current_info->mpd.bitrate = 0;
91                 current_info->mpd.progress = 0;
92                 current_info->mpd.elapsed = 0;
93                 current_info->mpd.length = 0;
94                 if (current_info->mpd.artist == NULL)
95                         current_info->mpd.artist =
96                                         malloc(TEXT_BUFFER_SIZE);
97                 if (current_info->mpd.album == NULL)
98                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
99                 if (current_info->mpd.title == NULL)
100                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
101                 strcpy(current_info->mpd.artist, "Unknown");
102                 strcpy(current_info->mpd.album, "Unknown");
103                 strcpy(current_info->mpd.title, "Unknown");
104         }
105         if (status->state == MPD_STATUS_STATE_PLAY ||
106                    status->state == MPD_STATUS_STATE_PAUSE) {
107                 current_info->mpd.bitrate = status->bitRate;
108                 current_info->mpd.progress =
109                                 (float) status->elapsedTime / status->totalTime;
110                 current_info->mpd.elapsed = status->elapsedTime;
111                 current_info->mpd.length = status->totalTime;
112                    }
113
114
115                    if (current_info->conn->error) {
116                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
117                            mpd_closeConnection(current_info->conn);
118                            return;
119                    }
120
121                    mpd_nextListOkCommand(current_info->conn);
122
123                    while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
124                            mpd_Song *song = entity->info.song;
125                            if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
126                                    mpd_freeInfoEntity(entity);
127                                    continue;
128                            }
129
130                            if (current_info->mpd.artist == NULL)
131                                    current_info->mpd.artist =
132                                                    malloc(TEXT_BUFFER_SIZE);
133                            if (current_info->mpd.album == NULL)
134                                    current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
135                            if (current_info->mpd.title == NULL)
136                                    current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
137                            if (song->artist) {
138                                    strcpy(current_info->mpd.artist, song->artist);
139                            } else {
140                                    strcpy(current_info->mpd.artist, "Unknown");
141                            }
142                            if (song->album) {
143                                    strcpy(current_info->mpd.album, song->album);
144                            } else {
145                                    strcpy(current_info->mpd.album, "Unknown");
146                            }
147                            if (song->title) {
148                                    strcpy(current_info->mpd.title, song->title);
149                            } else {
150                                    strcpy(current_info->mpd.title, "Unknown");
151                            }
152                            if (entity != NULL) {
153                                    mpd_freeInfoEntity(entity);
154                            }
155                    }
156                    if (entity != NULL) {
157                            mpd_freeInfoEntity(entity);
158                    }
159
160                    if (current_info->conn->error) {
161                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
162                            mpd_closeConnection(current_info->conn);
163                            return;
164                    }
165
166                    mpd_finishCommand(current_info->conn);
167                    if (current_info->conn->error) {
168                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
169                            mpd_closeConnection(current_info->conn);
170                            return;
171                    }
172                    mpd_freeStatus(status);
173                    mpd_closeConnection(current_info->conn);
174 }