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