moved stuff
[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         }
75         if (status->state == MPD_STATUS_STATE_PAUSE) {
76                 current_info->mpd.status = "Paused";
77         }
78         if (status->state == MPD_STATUS_STATE_UNKNOWN) {
79                 current_info->mpd.status = "Unknown";
80                 current_info->mpd.bitrate = 0;
81                 current_info->mpd.progress = 0;
82                 current_info->mpd.elapsed = 0;
83                 current_info->mpd.length = 0;
84         }
85         if (status->state == MPD_STATUS_STATE_PLAY ||
86             status->state == MPD_STATUS_STATE_PAUSE) {
87                 current_info->mpd.bitrate = status->bitRate;
88                 current_info->mpd.progress =
89                     (float) status->elapsedTime / status->totalTime;
90                 current_info->mpd.elapsed = status->elapsedTime;
91                 current_info->mpd.length = status->totalTime;
92         }
93
94
95         if (current_info->conn->error) {
96                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
97                 mpd_closeConnection(current_info->conn);
98                 return;
99         }
100
101         mpd_nextListOkCommand(current_info->conn);
102
103         while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
104                 mpd_Song *song = entity->info.song;
105                 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
106                         mpd_freeInfoEntity(entity);
107                         continue;
108                 }
109
110                 if (current_info->mpd.artist == NULL)
111                         current_info->mpd.artist =
112                             malloc(TEXT_BUFFER_SIZE);
113                 if (current_info->mpd.album == NULL)
114                         current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
115                 if (current_info->mpd.title == NULL)
116                         current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
117                 if (song->artist) {
118                         strcpy(current_info->mpd.artist, song->artist);
119                 } else {
120                         strcpy(current_info->mpd.artist, "Unknown");
121                 }
122                 if (song->album) {
123                         strcpy(current_info->mpd.album, song->album);
124                 } else {
125                         strcpy(current_info->mpd.album, "Unknown");
126                 }
127                 if (song->title) {
128                         strcpy(current_info->mpd.title, song->title);
129                 } else {
130                         strcpy(current_info->mpd.title, "Unknown");
131                 }
132                 if (entity != NULL) {
133                         mpd_freeInfoEntity(entity);
134                 }
135         }
136         if (entity != NULL) {
137                 mpd_freeInfoEntity(entity);
138         }
139
140         if (current_info->conn->error) {
141                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
142                 mpd_closeConnection(current_info->conn);
143                 return;
144         }
145
146         mpd_finishCommand(current_info->conn);
147         if (current_info->conn->error) {
148                 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
149                 mpd_closeConnection(current_info->conn);
150                 return;
151         }
152         mpd_freeStatus(status);
153         mpd_closeConnection(current_info->conn);
154 }