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