Extend commit 25680305095bfcedaa46cb017182544183ab743b to the whole cpu object.
[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-2010 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 "timeinfo.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                 NORM_ERR("Failed to create MPD timed thread");
130                 return;
131         }
132         timed_thread_register(thread, &thread);
133         if (timed_thread_run(thread))
134                 NORM_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                         NORM_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                         NORM_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.vol = status->volume;
200                 if (status->random == 0) {
201                         mpd_info.random = "Off";
202                 } else if (status->random == 1) {
203                         mpd_info.random = "On";
204                 } else {
205                         mpd_info.random = "";
206                 }
207                 if (status->repeat == 0) {
208                         mpd_info.repeat = "Off";
209                 } else if (status->repeat == 1) {
210                         mpd_info.repeat = "On";
211                 } else {
212                         mpd_info.repeat = "";
213                 }
214                 /* if (status->error) {
215                         printf("error: %s\n", status->error);
216                 } */
217
218                 switch (status->state) {
219                         case MPD_STATUS_STATE_PLAY:
220                                 mpd_info.status = "Playing";
221                                 break;
222                         case MPD_STATUS_STATE_STOP:
223                                 mpd_info.status = "Stopped";
224                                 break;
225                         case MPD_STATUS_STATE_PAUSE:
226                                 mpd_info.status = "Paused";
227                                 break;
228                         default:
229                                 mpd_info.status = "";
230                                 clear_mpd();
231                                 break;
232                 }
233
234                 if (status->state == MPD_STATUS_STATE_PLAY ||
235                     status->state == MPD_STATUS_STATE_PAUSE) {
236                         mpd_info.is_playing = 1;
237                         mpd_info.bitrate = status->bitRate;
238                         mpd_info.progress = (float) status->elapsedTime /
239                                 status->totalTime;
240                         mpd_info.elapsed = status->elapsedTime;
241                         mpd_info.length = status->totalTime;
242                 } else {
243                         mpd_info.progress = 0;
244                         mpd_info.is_playing = 0;
245                         mpd_info.elapsed = 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
322 static inline void format_media_player_time(char *buf, const int size,
323                 int seconds)
324 {
325         int days, hours, minutes;
326
327         if (times_in_seconds()) {
328                 snprintf(buf, size, "%d", seconds);
329                 return;
330         }
331
332         days = seconds / (24 * 60 * 60);
333         seconds %= (24 * 60 * 60);
334         hours = seconds / (60 * 60);
335         seconds %= (60 * 60);
336         minutes = seconds / 60;
337         seconds %= 60;
338
339         if (days > 0) {
340                 snprintf(buf, size, "%i days %i:%02i:%02i", days,
341                                 hours, minutes, seconds);
342         } else if (hours > 0) {
343                 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
344                                 seconds);
345         } else {
346                 snprintf(buf, size, "%i:%02i", minutes, seconds);
347         }
348 }
349
350 void print_mpd_elapsed(struct text_object *obj, char *p, int p_max_size)
351 {
352         (void)obj;
353         format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
354 }
355
356 void print_mpd_length(struct text_object *obj, char *p, int p_max_size)
357 {
358         (void)obj;
359         format_media_player_time(p, p_max_size, mpd_get_info()->length);
360 }
361
362 void print_mpd_percent(struct text_object *obj, char *p, int p_max_size)
363 {
364         (void)obj;
365         percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
366 }
367
368 void print_mpd_bar(struct text_object *obj, char *p, int p_max_size)
369 {
370         new_bar(obj, p, p_max_size, (int) (mpd_get_info()->progress * 255.0f));
371 }
372
373 void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
374 {
375         struct mpd_s *mpd = mpd_get_info();
376         int len = obj->data.i;
377         if (len == 0 || len > p_max_size)
378                 len = p_max_size;
379
380         memset(p, 0, p_max_size);
381         if (mpd->artist && *mpd->artist &&
382                         mpd->title && *mpd->title) {
383                 snprintf(p, len, "%s - %s", mpd->artist,
384                                 mpd->title);
385         } else if (mpd->title && *mpd->title) {
386                 snprintf(p, len, "%s", mpd->title);
387         } else if (mpd->artist && *mpd->artist) {
388                 snprintf(p, len, "%s", mpd->artist);
389         } else if (mpd->file && *mpd->file) {
390                 snprintf(p, len, "%s", mpd->file);
391         } else {
392                 *p = 0;
393         }
394 }
395
396 #define MPD_PRINT_GENERATOR(name, fmt) \
397 void print_mpd_##name(struct text_object *obj, char *p, int p_max_size) \
398 { \
399         if (obj->data.i && obj->data.i < p_max_size) \
400                 p_max_size = obj->data.i; \
401         snprintf(p, p_max_size, fmt, mpd_get_info()->name); \
402 }
403
404 MPD_PRINT_GENERATOR(title, "%s")
405 MPD_PRINT_GENERATOR(artist, "%s")
406 MPD_PRINT_GENERATOR(album, "%s")
407 MPD_PRINT_GENERATOR(random, "%s")
408 MPD_PRINT_GENERATOR(repeat, "%s")
409 MPD_PRINT_GENERATOR(track, "%s")
410 MPD_PRINT_GENERATOR(name, "%s")
411 MPD_PRINT_GENERATOR(file, "%s")
412 MPD_PRINT_GENERATOR(vol, "%d")
413 MPD_PRINT_GENERATOR(bitrate, "%d")
414 MPD_PRINT_GENERATOR(status, "%s")
415
416 #undef MPD_PRINT_GENERATOR