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