1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11 * All rights reserved.
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.
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/>.
29 #include "timed_thread.h"
30 #include "libmpdclient.h"
33 /* server connection data */
34 static char mpd_host[128];
35 static char mpd_password[128];
38 /* this is >0 if the current password was set from MPD_HOST */
39 static int mpd_environment_password = 0;
41 /* global mpd information */
42 static struct mpd_s mpd_info;
44 /* number of users of the above struct */
45 static int refcount = 0;
47 void mpd_set_host(const char *host)
49 snprintf(mpd_host, 128, "%s", host);
51 if (mpd_environment_password) {
52 /* for security, dont use environment password when user specifies host in config */
56 void mpd_set_password(const char *password, int from_environment)
58 snprintf(mpd_password, 128, "%s", password);
59 mpd_environment_password = from_environment;
61 void mpd_clear_password(void)
64 mpd_environment_password = 0;
66 int mpd_set_port(const char *port)
70 val = strtol(port, 0, 0);
71 if (val < 1 || val > 0xffff)
79 if (!(refcount++)) /* first client */
80 memset(&mpd_info, 0, sizeof(struct mpd_s));
85 struct mpd_s *mpd_get_info(void)
90 static void clear_mpd(void)
92 #define xfree(x) if (x) free(x)
93 xfree(mpd_info.title);
94 xfree(mpd_info.artist);
95 xfree(mpd_info.album);
96 /* do not free() the const char *status! */
97 /* do not free() the const char *random! */
98 /* do not free() the const char *repeat! */
99 xfree(mpd_info.track);
100 xfree(mpd_info.name);
101 xfree(mpd_info.file);
103 memset(&mpd_info, 0, sizeof(struct mpd_s));
108 if (!(--refcount)) /* last client */
112 static void *update_mpd_thread(void *) __attribute__((noreturn));
114 void update_mpd(void)
117 static timed_thread *thread = NULL;
122 interval = info.music_player_interval * 1000000;
123 thread = timed_thread_create(&update_mpd_thread, &thread, interval);
125 ERR("Failed to create MPD timed thread");
128 timed_thread_register(thread, &thread);
129 if (timed_thread_run(thread))
130 ERR("Failed to run MPD timed thread");
133 /* stringMAXdup dups at most text_buffer_size bytes */
134 #define strmdup(x) strndup(x, text_buffer_size - 1)
136 static void *update_mpd_thread(void *arg)
138 static mpd_Connection *conn = NULL;
140 mpd_InfoEntity *entity;
141 timed_thread *me = *(timed_thread **)arg;
142 const char *emptystr = "";
146 conn = mpd_newConnection(mpd_host, mpd_port, 10);
149 mpd_sendPasswordCommand(conn, mpd_password);
150 mpd_finishCommand(conn);
153 timed_thread_lock(me);
155 if (conn->error || conn == NULL) {
156 ERR("MPD error: %s\n", conn->errorStr);
157 mpd_closeConnection(conn);
161 mpd_info.status = "MPD not responding";
162 timed_thread_unlock(me);
163 if (timed_thread_test(me, 0)) {
164 timed_thread_exit(me);
169 mpd_sendStatusCommand(conn);
170 if ((status = mpd_getStatus(conn)) == NULL) {
171 ERR("MPD error: %s\n", conn->errorStr);
172 mpd_closeConnection(conn);
176 mpd_info.status = "MPD not responding";
177 timed_thread_unlock(me);
178 if (timed_thread_test(me, 0)) {
179 timed_thread_exit(me);
183 mpd_finishCommand(conn);
185 // fprintf(stderr, "%s\n", conn->errorStr);
186 mpd_closeConnection(conn);
188 timed_thread_unlock(me);
189 if (timed_thread_test(me, 0)) {
190 timed_thread_exit(me);
195 mpd_info.volume = status->volume;
196 /* if (status->error) {
197 printf("error: %s\n", status->error);
200 switch (status->state) {
201 case MPD_STATUS_STATE_PLAY:
202 mpd_info.status = "Playing";
204 case MPD_STATUS_STATE_STOP:
205 mpd_info.status = "Stopped";
207 case MPD_STATUS_STATE_PAUSE:
208 mpd_info.status = "Paused";
211 mpd_info.status = "";
216 if (status->state == MPD_STATUS_STATE_STOP) {
217 mpd_info.progress = (float) status->elapsedTime /
219 mpd_info.elapsed = status->elapsedTime;
220 } else if (status->state == MPD_STATUS_STATE_PLAY ||
221 status->state == MPD_STATUS_STATE_PAUSE) {
222 mpd_info.is_playing = 1;
223 mpd_info.bitrate = status->bitRate;
224 mpd_info.progress = (float) status->elapsedTime /
226 mpd_info.elapsed = status->elapsedTime;
227 mpd_info.length = status->totalTime;
228 if (status->random == 0) {
229 mpd_info.random = "Off";
230 } else if (status->random == 1) {
231 mpd_info.random = "On";
233 mpd_info.random = "";
235 if (status->repeat == 0) {
236 mpd_info.repeat = "Off";
237 } else if (status->repeat == 1) {
238 mpd_info.repeat = "On";
240 mpd_info.repeat = "";
243 mpd_info.is_playing = 0;
247 // fprintf(stderr, "%s\n", conn->errorStr);
248 mpd_closeConnection(conn);
250 timed_thread_unlock(me);
251 if (timed_thread_test(me, 0)) {
252 timed_thread_exit(me);
257 mpd_sendCurrentSongCommand(conn);
258 while ((entity = mpd_getNextInfoEntity(conn))) {
259 mpd_Song *song = entity->info.song;
261 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
262 mpd_freeInfoEntity(entity);
265 #define SONGSET(x) { \
268 mpd_info.x = strmdup(song->x); \
270 mpd_info.x = strmdup(emptystr); \
279 if (entity != NULL) {
280 mpd_freeInfoEntity(entity);
284 mpd_finishCommand(conn);
286 // fprintf(stderr, "%s\n", conn->errorStr);
287 mpd_closeConnection(conn);
289 timed_thread_unlock(me);
290 if (timed_thread_test(me, 0)) {
291 timed_thread_exit(me);
296 timed_thread_unlock(me);
298 // fprintf(stderr, "%s\n", conn->errorStr);
299 mpd_closeConnection(conn);
301 if (timed_thread_test(me, 0)) {
302 timed_thread_exit(me);
307 mpd_freeStatus(status);
309 mpd_closeConnection(conn);
312 if (timed_thread_test(me, 0)) {
313 timed_thread_exit(me);