59fe5c1d586f756895c2abfb883352eba23bd965
[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-2008 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 /* global mpd information */
39 static struct mpd_s mpd_info;
40
41 /* number of users of the above struct */
42 static int refcount = 0;
43
44 void mpd_set_host(const char *host)
45 {
46         snprintf(mpd_host, 128, "%s", host);
47 }
48 void mpd_set_password(const char *password)
49 {
50         snprintf(mpd_password, 128, "%s", password);
51 }
52 void mpd_clear_password(void)
53 {
54         *mpd_password = '\0';
55 }
56 int mpd_set_port(const char *port)
57 {
58         int val;
59
60         val = strtol(port, 0, 0);
61         if (val < 1 || val > 0xffff)
62                 return 1;
63         mpd_port = val;
64         return 0;
65 }
66
67 void init_mpd(void)
68 {
69         if (!(refcount++))      /* first client */
70                 memset(&mpd_info, 0, sizeof(struct mpd_s));
71
72         refcount++;
73 }
74
75 struct mpd_s *mpd_get_info(void)
76 {
77         return &mpd_info;
78 }
79
80 static void clear_mpd(void)
81 {
82 #define xfree(x) if (x) free(x)
83         xfree(mpd_info.title);
84         xfree(mpd_info.artist);
85         xfree(mpd_info.album);
86         /* do not free() the const char *status! */
87         /* do not free() the const char *random! */
88         /* do not free() the const char *repeat! */
89         xfree(mpd_info.track);
90         xfree(mpd_info.name);
91         xfree(mpd_info.file);
92 #undef xfree
93         memset(&mpd_info, 0, sizeof(struct mpd_s));
94 }
95
96 void free_mpd(void)
97 {
98         if (!(--refcount))      /* last client */
99                 clear_mpd();
100 }
101
102 static void *update_mpd_thread(void *) __attribute__((noreturn));
103
104 void update_mpd(void)
105 {
106         int interval;
107         static timed_thread *thread = NULL;
108
109         if (thread)
110                 return;
111
112         interval = info.music_player_interval * 1000000;
113         thread = timed_thread_create(&update_mpd_thread, &thread, interval);
114         if (!thread) {
115                 ERR("Failed to create MPD timed thread");
116                 return;
117         }
118         timed_thread_register(thread, &thread);
119         if (timed_thread_run(thread))
120                 ERR("Failed to run MPD timed thread");
121 }
122
123 /* stringMAXdup dups at most text_buffer_size bytes */
124 #define strmdup(x) strndup(x, text_buffer_size - 1)
125
126 static void *update_mpd_thread(void *arg)
127 {
128         static mpd_Connection *conn = NULL;
129         mpd_Status *status;
130         mpd_InfoEntity *entity;
131         timed_thread *me = *(timed_thread **)arg;
132         const char *emptystr = "";
133
134         while (1) {
135                 if (!conn)
136                         conn = mpd_newConnection(mpd_host, mpd_port, 10);
137
138                 if (*mpd_password) {
139                         mpd_sendPasswordCommand(conn, mpd_password);
140                         mpd_finishCommand(conn);
141                 }
142
143                 timed_thread_lock(me);
144
145                 if (conn->error || conn == NULL) {
146                         ERR("MPD error: %s\n", conn->errorStr);
147                         mpd_closeConnection(conn);
148                         conn = 0;
149                         clear_mpd();
150
151                         mpd_info.status = "MPD not responding";
152                         timed_thread_unlock(me);
153                         if (timed_thread_test(me, 0)) {
154                                 timed_thread_exit(me);
155                         }
156                         continue;
157                 }
158
159                 mpd_sendStatusCommand(conn);
160                 if ((status = mpd_getStatus(conn)) == NULL) {
161                         ERR("MPD error: %s\n", conn->errorStr);
162                         mpd_closeConnection(conn);
163                         conn = 0;
164                         clear_mpd();
165
166                         mpd_info.status = "MPD not responding";
167                         timed_thread_unlock(me);
168                         if (timed_thread_test(me, 0)) {
169                                 timed_thread_exit(me);
170                         }
171                         continue;
172                 }
173                 mpd_finishCommand(conn);
174                 if (conn->error) {
175                         // fprintf(stderr, "%s\n", conn->errorStr);
176                         mpd_closeConnection(conn);
177                         conn = 0;
178                         timed_thread_unlock(me);
179                         if (timed_thread_test(me, 0)) {
180                                 timed_thread_exit(me);
181                         }
182                         continue;
183                 }
184
185                 mpd_info.volume = status->volume;
186                 /* if (status->error) {
187                         printf("error: %s\n", status->error);
188                 } */
189
190                 switch(status->state) {
191                         case MPD_STATUS_STATE_PLAY:
192                                 mpd_info.status = "Playing";
193                                 break;
194                         case MPD_STATUS_STATE_STOP:
195                                 mpd_info.status = "Stopped";
196                                 break;
197                         case MPD_STATUS_STATE_PAUSE:
198                                 mpd_info.status = "Paused";
199                                 break;
200                         default:
201                                 mpd_info.status = "";
202                                 clear_mpd();
203                                 break;
204                 }
205
206                 if (status->state == MPD_STATUS_STATE_PLAY ||
207                     status->state == MPD_STATUS_STATE_PAUSE) {
208                         mpd_info.is_playing = 1;
209                         mpd_info.bitrate = status->bitRate;
210                         mpd_info.progress = (float) status->elapsedTime /
211                                 status->totalTime;
212                         mpd_info.elapsed = status->elapsedTime;
213                         mpd_info.length = status->totalTime;
214                         if (status->random == 0) {
215                                 mpd_info.random = "Off";
216                         } else if (status->random == 1) {
217                                 mpd_info.random = "On";
218                         } else {
219                                 mpd_info.random = "";
220                         }
221                         if (status->repeat == 0) {
222                                 mpd_info.repeat = "Off";
223                         } else if (status->repeat == 1) {
224                                 mpd_info.repeat = "On";
225                         } else {
226                                 mpd_info.repeat = "";
227                         }
228                 }
229
230                 if (conn->error) {
231                         // fprintf(stderr, "%s\n", conn->errorStr);
232                         mpd_closeConnection(conn);
233                         conn = 0;
234                         timed_thread_unlock(me);
235                         if (timed_thread_test(me, 0)) {
236                                 timed_thread_exit(me);
237                         }
238                         continue;
239                 }
240
241                 mpd_sendCurrentSongCommand(conn);
242                 while ((entity = mpd_getNextInfoEntity(conn))) {
243                         mpd_Song *song = entity->info.song;
244
245                         if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
246                                 mpd_freeInfoEntity(entity);
247                                 continue;
248                         }
249 #define SONGSET(x) {                            \
250         free(mpd_info.x);                       \
251         if(song->x)                             \
252                 mpd_info.x = strmdup(song->x);  \
253         else                                    \
254                 mpd_info.x = strmdup(emptystr); \
255 }
256                         SONGSET(artist);
257                         SONGSET(album);
258                         SONGSET(title);
259                         SONGSET(track);
260                         SONGSET(name);
261                         SONGSET(file);
262 #undef SONGSET
263                         if (entity != NULL) {
264                                 mpd_freeInfoEntity(entity);
265                                 entity = NULL;
266                         }
267                 }
268                 mpd_finishCommand(conn);
269                 if (conn->error) {
270                         // fprintf(stderr, "%s\n", conn->errorStr);
271                         mpd_closeConnection(conn);
272                         conn = 0;
273                         timed_thread_unlock(me);
274                         if (timed_thread_test(me, 0)) {
275                                 timed_thread_exit(me);
276                         }
277                         continue;
278                 }
279
280                 timed_thread_unlock(me);
281                 if (conn->error) {
282                         // fprintf(stderr, "%s\n", conn->errorStr);
283                         mpd_closeConnection(conn);
284                         conn = 0;
285                         if (timed_thread_test(me, 0)) {
286                                 timed_thread_exit(me);
287                         }
288                         continue;
289                 }
290
291                 mpd_freeStatus(status);
292                 /* if (conn) {
293                         mpd_closeConnection(conn);
294                         conn = 0;
295                 } */
296                 if (timed_thread_test(me, 0)) {
297                         timed_thread_exit(me);
298                 }
299                 continue;
300         }
301         /* never reached */
302 }
303