2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
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/>.
32 #include "libmpdclient.h"
34 timed_thread *mpd_timed_thread = NULL;
35 void clear_mpd_stats(struct information *current_info);
37 void init_mpd_stats(struct information *current_info)
39 if (current_info->mpd.artist == NULL)
40 current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE);
41 if (current_info->mpd.album == NULL)
42 current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
43 if (current_info->mpd.title == NULL)
44 current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
45 if (current_info->mpd.random == NULL)
46 current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
47 if (current_info->mpd.repeat == NULL)
48 current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
49 if (current_info->mpd.track == NULL)
50 current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
51 if (current_info->mpd.status == NULL)
52 current_info->mpd.status = malloc(TEXT_BUFFER_SIZE);
53 if (current_info->mpd.name == NULL)
54 current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
55 if (current_info->mpd.file == NULL)
56 current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
57 clear_mpd_stats(current_info);
60 void clear_mpd_stats(struct information *current_info)
62 *current_info->mpd.name=0;
63 *current_info->mpd.file=0;
64 *current_info->mpd.artist=0;
65 *current_info->mpd.album=0;
66 *current_info->mpd.title=0;
67 *current_info->mpd.random=0;
68 *current_info->mpd.repeat=0;
69 *current_info->mpd.track=0;
70 *current_info->mpd.status=0;
71 current_info->mpd.bitrate = 0;
72 current_info->mpd.progress = 0;
73 current_info->mpd.elapsed = 0;
74 current_info->mpd.length = 0;
77 void *update_mpd(void)
79 struct information *current_info = &info;
81 if (!current_info->conn) {
82 current_info->conn = mpd_newConnection(current_info->mpd.host, current_info->mpd.port, 10);
84 if (strlen(current_info->mpd.password) > 1) {
85 mpd_sendPasswordCommand(current_info->conn,
86 current_info->mpd.password);
87 mpd_finishCommand(current_info->conn);
90 timed_thread_lock(mpd_timed_thread);
91 clear_mpd_stats(current_info);
93 if (current_info->conn->error || current_info->conn == NULL) {
94 //ERR("%MPD error: s\n", current_info->conn->errorStr);
95 mpd_closeConnection(current_info->conn);
96 current_info->conn = 0;
98 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
99 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
104 mpd_InfoEntity *entity;
105 mpd_sendStatusCommand(current_info->conn);
106 if ((status = mpd_getStatus(current_info->conn)) == NULL) {
107 //ERR("MPD error: %s\n", current_info->conn->errorStr);
108 mpd_closeConnection(current_info->conn);
109 current_info->conn = 0;
111 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
112 timed_thread_unlock(mpd_timed_thread);
113 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
116 mpd_finishCommand(current_info->conn);
117 if (current_info->conn->error) {
118 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
119 mpd_closeConnection(current_info->conn);
120 current_info->conn = 0;
121 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
125 current_info->mpd.volume = status->volume;
127 //printf("error: %s\n", status->error);
129 if (status->state == MPD_STATUS_STATE_PLAY) {
130 strncpy(current_info->mpd.status, "Playing",
131 TEXT_BUFFER_SIZE - 1);
133 if (status->state == MPD_STATUS_STATE_STOP) {
134 strncpy(current_info->mpd.status, "Stopped",
135 TEXT_BUFFER_SIZE - 1);
137 if (status->state == MPD_STATUS_STATE_PAUSE) {
138 strncpy(current_info->mpd.status, "Paused",
139 TEXT_BUFFER_SIZE - 1);
141 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
142 // current_info was already cleaned up by clear_mpd_stats()
144 if (status->state == MPD_STATUS_STATE_PLAY ||
145 status->state == MPD_STATUS_STATE_PAUSE) {
146 current_info->mpd.bitrate = status->bitRate;
147 current_info->mpd.progress =
148 (float) status->elapsedTime / status->totalTime;
149 current_info->mpd.elapsed = status->elapsedTime;
150 current_info->mpd.length = status->totalTime;
151 if (status->random == 0) {
152 strcpy(current_info->mpd.random, "Off");
153 } else if (status->random == 1) {
154 strcpy(current_info->mpd.random, "On");
156 *current_info->mpd.random=0;
158 if (status->repeat == 0) {
159 strcpy(current_info->mpd.repeat, "Off");
160 } else if (status->repeat == 1) {
161 strcpy(current_info->mpd.repeat, "On");
163 *current_info->mpd.repeat=0;
167 if (current_info->conn->error) {
168 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
169 mpd_closeConnection(current_info->conn);
170 current_info->conn = 0;
171 timed_thread_unlock(mpd_timed_thread);
172 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
176 mpd_sendCurrentSongCommand(current_info->conn);
177 while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
178 mpd_Song *song = entity->info.song;
179 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
180 mpd_freeInfoEntity(entity);
185 strncpy(current_info->mpd.artist, song->artist,
186 TEXT_BUFFER_SIZE - 1);
188 *current_info->mpd.artist=0;
191 strncpy(current_info->mpd.album, song->album,
192 TEXT_BUFFER_SIZE - 1);
194 *current_info->mpd.album=0;
197 strncpy(current_info->mpd.title, song->title,
198 TEXT_BUFFER_SIZE - 1);
200 *current_info->mpd.title=0;
203 strncpy(current_info->mpd.track, song->track,
204 TEXT_BUFFER_SIZE - 1);
206 *current_info->mpd.track=0;
209 strncpy(current_info->mpd.name, song->name,
210 TEXT_BUFFER_SIZE - 1);
212 *current_info->mpd.name=0;
215 strncpy(current_info->mpd.file,
216 song->file, TEXT_BUFFER_SIZE - 1);
218 *current_info->mpd.file=0;
220 if (entity != NULL) {
221 mpd_freeInfoEntity(entity);
225 if (entity != NULL) {
226 mpd_freeInfoEntity(entity);
229 mpd_finishCommand(current_info->conn);
230 if (current_info->conn->error) {
231 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
232 mpd_closeConnection(current_info->conn);
233 current_info->conn = 0;
234 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
238 timed_thread_unlock(mpd_timed_thread);
239 if (current_info->conn->error) {
240 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
241 mpd_closeConnection(current_info->conn);
242 current_info->conn = 0;
243 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
247 mpd_freeStatus(status);
248 /* if (current_info->conn) {
249 mpd_closeConnection(current_info->conn);
250 current_info->conn = 0;
252 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);