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-2008 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 /* global mpd information */
39 static struct mpd_s mpd_info;
41 /* number of users of the above struct */
42 static int refcount = 0;
44 void mpd_set_host(const char *host)
46 snprintf(mpd_host, 128, "%s", host);
48 void mpd_set_password(const char *password)
50 snprintf(mpd_password, 128, "%s", password);
52 void mpd_clear_password(void)
56 int mpd_set_port(const char *port)
60 val = strtol(port, 0, 0);
61 if (val < 1 || val > 0xffff)
69 if (!(refcount++)) /* first client */
70 memset(&mpd_info, 0, sizeof(struct mpd_s));
75 struct mpd_s *mpd_get_info(void)
80 static void clear_mpd(void)
82 #define xfree(x) if (x) free(x)
83 xfree(mpd_info.title);
84 xfree(mpd_info.artist);
85 xfree(mpd_info.album);
86 /* do not free() the const char *status! */
87 /* do not free() the const char *random! */
88 /* do not free() the const char *repeat! */
89 xfree(mpd_info.track);
93 memset(&mpd_info, 0, sizeof(struct mpd_s));
98 if (!(--refcount)) /* last client */
102 static void *update_mpd_thread(void *) __attribute__((noreturn));
104 void update_mpd(void)
107 static timed_thread *thread = NULL;
112 interval = info.music_player_interval * 1000000;
113 thread = timed_thread_create(&update_mpd_thread, &thread, interval);
115 ERR("Failed to create MPD timed thread");
118 timed_thread_register(thread, &thread);
119 if (timed_thread_run(thread))
120 ERR("Failed to run MPD timed thread");
123 /* stringMAXdup dups at most text_buffer_size bytes */
124 #define strmdup(x) strndup(x, text_buffer_size - 1)
126 static void *update_mpd_thread(void *arg)
128 static mpd_Connection *conn = NULL;
130 mpd_InfoEntity *entity;
131 timed_thread *me = *(timed_thread **)arg;
132 const char *emptystr = "";
136 conn = mpd_newConnection(mpd_host, mpd_port, 10);
139 mpd_sendPasswordCommand(conn, mpd_password);
140 mpd_finishCommand(conn);
143 timed_thread_lock(me);
145 if (conn->error || conn == NULL) {
146 ERR("MPD error: %s\n", conn->errorStr);
147 mpd_closeConnection(conn);
151 mpd_info.status = "MPD not responding";
152 timed_thread_unlock(me);
153 if (timed_thread_test(me, 0)) {
154 timed_thread_exit(me);
159 mpd_sendStatusCommand(conn);
160 if ((status = mpd_getStatus(conn)) == NULL) {
161 ERR("MPD error: %s\n", conn->errorStr);
162 mpd_closeConnection(conn);
166 mpd_info.status = "MPD not responding";
167 timed_thread_unlock(me);
168 if (timed_thread_test(me, 0)) {
169 timed_thread_exit(me);
173 mpd_finishCommand(conn);
175 // fprintf(stderr, "%s\n", conn->errorStr);
176 mpd_closeConnection(conn);
178 timed_thread_unlock(me);
179 if (timed_thread_test(me, 0)) {
180 timed_thread_exit(me);
185 mpd_info.volume = status->volume;
186 /* if (status->error) {
187 printf("error: %s\n", status->error);
190 switch(status->state) {
191 case MPD_STATUS_STATE_PLAY:
192 mpd_info.status = "Playing";
194 case MPD_STATUS_STATE_STOP:
195 mpd_info.status = "Stopped";
197 case MPD_STATUS_STATE_PAUSE:
198 mpd_info.status = "Paused";
201 mpd_info.status = "";
206 if (status->state == MPD_STATUS_STATE_PLAY ||
207 status->state == MPD_STATUS_STATE_PAUSE) {
208 mpd_info.is_playing = 1;
209 mpd_info.bitrate = status->bitRate;
210 mpd_info.progress = (float) status->elapsedTime /
212 mpd_info.elapsed = status->elapsedTime;
213 mpd_info.length = status->totalTime;
214 if (status->random == 0) {
215 mpd_info.random = "Off";
216 } else if (status->random == 1) {
217 mpd_info.random = "On";
219 mpd_info.random = "";
221 if (status->repeat == 0) {
222 mpd_info.repeat = "Off";
223 } else if (status->repeat == 1) {
224 mpd_info.repeat = "On";
226 mpd_info.repeat = "";
231 // fprintf(stderr, "%s\n", conn->errorStr);
232 mpd_closeConnection(conn);
234 timed_thread_unlock(me);
235 if (timed_thread_test(me, 0)) {
236 timed_thread_exit(me);
241 mpd_sendCurrentSongCommand(conn);
242 while ((entity = mpd_getNextInfoEntity(conn))) {
243 mpd_Song *song = entity->info.song;
245 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
246 mpd_freeInfoEntity(entity);
249 #define SONGSET(x) { \
252 mpd_info.x = strmdup(song->x); \
254 mpd_info.x = strmdup(emptystr); \
263 if (entity != NULL) {
264 mpd_freeInfoEntity(entity);
268 mpd_finishCommand(conn);
270 // fprintf(stderr, "%s\n", conn->errorStr);
271 mpd_closeConnection(conn);
273 timed_thread_unlock(me);
274 if (timed_thread_test(me, 0)) {
275 timed_thread_exit(me);
280 timed_thread_unlock(me);
282 // fprintf(stderr, "%s\n", conn->errorStr);
283 mpd_closeConnection(conn);
285 if (timed_thread_test(me, 0)) {
286 timed_thread_exit(me);
291 mpd_freeStatus(status);
293 mpd_closeConnection(conn);
296 if (timed_thread_test(me, 0)) {
297 timed_thread_exit(me);