Update MPD elapsed/progress when stopped.
[monky] / src / mpd.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26
27 #include "conky.h"
28 #include "logging.h"
29 #include "timed_thread.h"
30 #include "libmpdclient.h"
31 #include "mpd.h"
32
33 /* server connection data */
34 static char mpd_host[128];
35 static char mpd_password[128];
36 static int mpd_port;
37
38 /* this is >0 if the current password was set from MPD_HOST */
39 static int mpd_environment_password = 0;
40
41 /* global mpd information */
42 static struct mpd_s mpd_info;
43
44 /* number of users of the above struct */
45 static int refcount = 0;
46
47 void mpd_set_host(const char *host)
48 {
49         snprintf(mpd_host, 128, "%s", host);
50
51         if (mpd_environment_password) {
52                 /* for security, dont use environment password when user specifies host in config */
53                 mpd_clear_password();
54         }
55 }
56 void mpd_set_password(const char *password, int from_environment)
57 {
58         snprintf(mpd_password, 128, "%s", password);
59         mpd_environment_password = from_environment;
60 }
61 void mpd_clear_password(void)
62 {
63         *mpd_password = '\0';
64         mpd_environment_password = 0;
65 }
66 int mpd_set_port(const char *port)
67 {
68         int val;
69
70         val = strtol(port, 0, 0);
71         if (val < 1 || val > 0xffff)
72                 return 1;
73         mpd_port = val;
74         return 0;
75 }
76
77 void init_mpd(void)
78 {
79         if (!(refcount++))      /* first client */
80                 memset(&mpd_info, 0, sizeof(struct mpd_s));
81
82         refcount++;
83 }
84
85 struct mpd_s *mpd_get_info(void)
86 {
87         return &mpd_info;
88 }
89
90 static void clear_mpd(void)
91 {
92 #define xfree(x) if (x) free(x)
93         xfree(mpd_info.title);
94         xfree(mpd_info.artist);
95         xfree(mpd_info.album);
96         /* do not free() the const char *status! */
97         /* do not free() the const char *random! */
98         /* do not free() the const char *repeat! */
99         xfree(mpd_info.track);
100         xfree(mpd_info.name);
101         xfree(mpd_info.file);
102 #undef xfree
103         memset(&mpd_info, 0, sizeof(struct mpd_s));
104 }
105
106 void free_mpd(void)
107 {
108         if (!(--refcount))      /* last client */
109                 clear_mpd();
110 }
111
112 static void *update_mpd_thread(void *) __attribute__((noreturn));
113
114 void update_mpd(void)
115 {
116         int interval;
117         static timed_thread *thread = NULL;
118
119         if (thread)
120                 return;
121
122         interval = info.music_player_interval * 1000000;
123         thread = timed_thread_create(&update_mpd_thread, &thread, interval);
124         if (!thread) {
125                 ERR("Failed to create MPD timed thread");
126                 return;
127         }
128         timed_thread_register(thread, &thread);
129         if (timed_thread_run(thread))
130                 ERR("Failed to run MPD timed thread");
131 }
132
133 /* stringMAXdup dups at most text_buffer_size bytes */
134 #define strmdup(x) strndup(x, text_buffer_size - 1)
135
136 static void *update_mpd_thread(void *arg)
137 {
138         static mpd_Connection *conn = NULL;
139         mpd_Status *status;
140         mpd_InfoEntity *entity;
141         timed_thread *me = *(timed_thread **)arg;
142         const char *emptystr = "";
143
144         while (1) {
145                 if (!conn)
146                         conn = mpd_newConnection(mpd_host, mpd_port, 10);
147
148                 if (*mpd_password) {
149                         mpd_sendPasswordCommand(conn, mpd_password);
150                         mpd_finishCommand(conn);
151                 }
152
153                 timed_thread_lock(me);
154
155                 if (conn->error || conn == NULL) {
156                         ERR("MPD error: %s\n", conn->errorStr);
157                         mpd_closeConnection(conn);
158                         conn = 0;
159                         clear_mpd();
160
161                         mpd_info.status = "MPD not responding";
162                         timed_thread_unlock(me);
163                         if (timed_thread_test(me, 0)) {
164                                 timed_thread_exit(me);
165                         }
166                         continue;
167                 }
168
169                 mpd_sendStatusCommand(conn);
170                 if ((status = mpd_getStatus(conn)) == NULL) {
171                         ERR("MPD error: %s\n", conn->errorStr);
172                         mpd_closeConnection(conn);
173                         conn = 0;
174                         clear_mpd();
175
176                         mpd_info.status = "MPD not responding";
177                         timed_thread_unlock(me);
178                         if (timed_thread_test(me, 0)) {
179                                 timed_thread_exit(me);
180                         }
181                         continue;
182                 }
183                 mpd_finishCommand(conn);
184                 if (conn->error) {
185                         // fprintf(stderr, "%s\n", conn->errorStr);
186                         mpd_closeConnection(conn);
187                         conn = 0;
188                         timed_thread_unlock(me);
189                         if (timed_thread_test(me, 0)) {
190                                 timed_thread_exit(me);
191                         }
192                         continue;
193                 }
194
195                 mpd_info.volume = status->volume;
196                 /* if (status->error) {
197                         printf("error: %s\n", status->error);
198                 } */
199
200                 switch (status->state) {
201                         case MPD_STATUS_STATE_PLAY:
202                                 mpd_info.status = "Playing";
203                                 break;
204                         case MPD_STATUS_STATE_STOP:
205                                 mpd_info.status = "Stopped";
206                                 break;
207                         case MPD_STATUS_STATE_PAUSE:
208                                 mpd_info.status = "Paused";
209                                 break;
210                         default:
211                                 mpd_info.status = "";
212                                 clear_mpd();
213                                 break;
214                 }
215
216                 if (status->state == MPD_STATUS_STATE_STOP) {
217                         mpd_info.progress = (float) status->elapsedTime /
218                                 status->totalTime;
219                         mpd_info.elapsed = status->elapsedTime;
220                 } else if (status->state == MPD_STATUS_STATE_PLAY ||
221                     status->state == MPD_STATUS_STATE_PAUSE) {
222                         mpd_info.is_playing = 1;
223                         mpd_info.bitrate = status->bitRate;
224                         mpd_info.progress = (float) status->elapsedTime /
225                                 status->totalTime;
226                         mpd_info.elapsed = status->elapsedTime;
227                         mpd_info.length = status->totalTime;
228                         if (status->random == 0) {
229                                 mpd_info.random = "Off";
230                         } else if (status->random == 1) {
231                                 mpd_info.random = "On";
232                         } else {
233                                 mpd_info.random = "";
234                         }
235                         if (status->repeat == 0) {
236                                 mpd_info.repeat = "Off";
237                         } else if (status->repeat == 1) {
238                                 mpd_info.repeat = "On";
239                         } else {
240                                 mpd_info.repeat = "";
241                         }
242                 } else {
243                         mpd_info.is_playing = 0;
244                 }
245
246                 if (conn->error) {
247                         // fprintf(stderr, "%s\n", conn->errorStr);
248                         mpd_closeConnection(conn);
249                         conn = 0;
250                         timed_thread_unlock(me);
251                         if (timed_thread_test(me, 0)) {
252                                 timed_thread_exit(me);
253                         }
254                         continue;
255                 }
256
257                 mpd_sendCurrentSongCommand(conn);
258                 while ((entity = mpd_getNextInfoEntity(conn))) {
259                         mpd_Song *song = entity->info.song;
260
261                         if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
262                                 mpd_freeInfoEntity(entity);
263                                 continue;
264                         }
265 #define SONGSET(x) {                            \
266         free(mpd_info.x);                       \
267         if(song->x)                             \
268                 mpd_info.x = strmdup(song->x);  \
269         else                                    \
270                 mpd_info.x = strmdup(emptystr); \
271 }
272                         SONGSET(artist);
273                         SONGSET(album);
274                         SONGSET(title);
275                         SONGSET(track);
276                         SONGSET(name);
277                         SONGSET(file);
278 #undef SONGSET
279                         if (entity != NULL) {
280                                 mpd_freeInfoEntity(entity);
281                                 entity = NULL;
282                         }
283                 }
284                 mpd_finishCommand(conn);
285                 if (conn->error) {
286                         // fprintf(stderr, "%s\n", conn->errorStr);
287                         mpd_closeConnection(conn);
288                         conn = 0;
289                         timed_thread_unlock(me);
290                         if (timed_thread_test(me, 0)) {
291                                 timed_thread_exit(me);
292                         }
293                         continue;
294                 }
295
296                 timed_thread_unlock(me);
297                 if (conn->error) {
298                         // fprintf(stderr, "%s\n", conn->errorStr);
299                         mpd_closeConnection(conn);
300                         conn = 0;
301                         if (timed_thread_test(me, 0)) {
302                                 timed_thread_exit(me);
303                         }
304                         continue;
305                 }
306
307                 mpd_freeStatus(status);
308                 /* if (conn) {
309                         mpd_closeConnection(conn);
310                         conn = 0;
311                 } */
312                 if (timed_thread_test(me, 0)) {
313                         timed_thread_exit(me);
314                 }
315                 continue;
316         }
317         /* never reached */
318 }
319