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-2007 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/>.
31 #include "libmpdclient.h"
33 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(small_text_buffer_size);
42 if (current_info->mpd.album == NULL) {
43 current_info->mpd.album = malloc(small_text_buffer_size);
45 if (current_info->mpd.title == NULL) {
46 current_info->mpd.title = malloc(small_text_buffer_size);
48 if (current_info->mpd.random == NULL) {
49 current_info->mpd.random = malloc(small_text_buffer_size);
51 if (current_info->mpd.repeat == NULL) {
52 current_info->mpd.repeat = malloc(small_text_buffer_size);
54 if (current_info->mpd.track == NULL) {
55 current_info->mpd.track = malloc(small_text_buffer_size);
57 if (current_info->mpd.status == NULL) {
58 current_info->mpd.status = malloc(small_text_buffer_size);
60 if (current_info->mpd.name == NULL) {
61 current_info->mpd.name = malloc(small_text_buffer_size);
63 if (current_info->mpd.file == NULL) {
64 current_info->mpd.file = malloc(small_text_buffer_size);
66 clear_mpd_stats(current_info);
69 void free_mpd_vars(struct information *current_info)
71 if (current_info->mpd.title) {
72 free(current_info->mpd.title);
73 current_info->mpd.title = NULL;
75 if (current_info->mpd.artist) {
76 free(current_info->mpd.artist);
77 current_info->mpd.artist = NULL;
79 if (current_info->mpd.album) {
80 free(current_info->mpd.album);
81 current_info->mpd.album = NULL;
83 if (current_info->mpd.random) {
84 free(current_info->mpd.random);
85 current_info->mpd.random = NULL;
87 if (current_info->mpd.repeat) {
88 free(current_info->mpd.repeat);
89 current_info->mpd.repeat = NULL;
91 if (current_info->mpd.track) {
92 free(current_info->mpd.track);
93 current_info->mpd.track = NULL;
95 if (current_info->mpd.name) {
96 free(current_info->mpd.name);
97 current_info->mpd.name = NULL;
99 if (current_info->mpd.file) {
100 free(current_info->mpd.file);
101 current_info->mpd.file = NULL;
103 if (current_info->mpd.status) {
104 free(current_info->mpd.status);
105 current_info->mpd.status = NULL;
107 if (current_info->conn) {
108 mpd_closeConnection(current_info->conn);
109 current_info->conn = 0;
113 void clear_mpd_stats(struct information *current_info)
115 *current_info->mpd.name = 0;
116 *current_info->mpd.file = 0;
117 *current_info->mpd.artist = 0;
118 *current_info->mpd.album = 0;
119 *current_info->mpd.title = 0;
120 *current_info->mpd.random = 0;
121 *current_info->mpd.repeat = 0;
122 *current_info->mpd.track = 0;
123 *current_info->mpd.status = 0;
124 current_info->mpd.bitrate = 0;
125 current_info->mpd.progress = 0;
126 current_info->mpd.elapsed = 0;
127 current_info->mpd.length = 0;
130 void *update_mpd(void)
132 struct information *current_info = &info;
135 if (!current_info->conn) {
136 current_info->conn = mpd_newConnection(current_info->mpd.host,
137 current_info->mpd.port, 10);
139 if (strlen(current_info->mpd.password) > 1) {
140 mpd_sendPasswordCommand(current_info->conn,
141 current_info->mpd.password);
142 mpd_finishCommand(current_info->conn);
145 timed_thread_lock(mpd_timed_thread);
147 if (current_info->conn->error || current_info->conn == NULL) {
148 // ERR("%MPD error: s\n", current_info->conn->errorStr);
149 mpd_closeConnection(current_info->conn);
150 current_info->conn = 0;
151 clear_mpd_stats(current_info);
153 strncpy(current_info->mpd.status, "MPD not responding",
154 small_text_buffer_size - 1);
155 timed_thread_unlock(mpd_timed_thread);
156 if (timed_thread_test(mpd_timed_thread)) {
157 timed_thread_exit(mpd_timed_thread);
163 mpd_InfoEntity *entity;
165 mpd_sendStatusCommand(current_info->conn);
166 if ((status = mpd_getStatus(current_info->conn)) == NULL) {
167 // ERR("MPD error: %s\n", current_info->conn->errorStr);
168 mpd_closeConnection(current_info->conn);
169 current_info->conn = 0;
170 clear_mpd_stats(current_info);
172 strncpy(current_info->mpd.status, "MPD not responding",
173 small_text_buffer_size - 1);
174 timed_thread_unlock(mpd_timed_thread);
175 if (timed_thread_test(mpd_timed_thread)) {
176 timed_thread_exit(mpd_timed_thread);
180 mpd_finishCommand(current_info->conn);
181 if (current_info->conn->error) {
182 // fprintf(stderr, "%s\n", current_info->conn->errorStr);
183 mpd_closeConnection(current_info->conn);
184 current_info->conn = 0;
185 timed_thread_unlock(mpd_timed_thread);
186 if (timed_thread_test(mpd_timed_thread)) {
187 timed_thread_exit(mpd_timed_thread);
192 current_info->mpd.volume = status->volume;
193 /* if (status->error) {
194 printf("error: %s\n", status->error);
197 if (status->state == MPD_STATUS_STATE_PLAY) {
198 strncpy(current_info->mpd.status, "Playing", small_text_buffer_size - 1);
200 if (status->state == MPD_STATUS_STATE_STOP) {
201 clear_mpd_stats(current_info);
202 strncpy(current_info->mpd.status, "Stopped", small_text_buffer_size - 1);
204 if (status->state == MPD_STATUS_STATE_PAUSE) {
205 strncpy(current_info->mpd.status, "Paused", small_text_buffer_size - 1);
207 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
208 clear_mpd_stats(current_info);
209 *current_info->mpd.status = 0;
211 if (status->state == MPD_STATUS_STATE_PLAY
212 || status->state == MPD_STATUS_STATE_PAUSE) {
213 current_info->mpd.bitrate = status->bitRate;
214 current_info->mpd.progress = (float) status->elapsedTime /
216 current_info->mpd.elapsed = status->elapsedTime;
217 current_info->mpd.length = status->totalTime;
218 if (status->random == 0) {
219 strcpy(current_info->mpd.random, "Off");
220 } else if (status->random == 1) {
221 strcpy(current_info->mpd.random, "On");
223 *current_info->mpd.random = 0;
225 if (status->repeat == 0) {
226 strcpy(current_info->mpd.repeat, "Off");
227 } else if (status->repeat == 1) {
228 strcpy(current_info->mpd.repeat, "On");
230 *current_info->mpd.repeat = 0;
234 if (current_info->conn->error) {
235 // fprintf(stderr, "%s\n", current_info->conn->errorStr);
236 mpd_closeConnection(current_info->conn);
237 current_info->conn = 0;
238 timed_thread_unlock(mpd_timed_thread);
239 if (timed_thread_test(mpd_timed_thread)) {
240 timed_thread_exit(mpd_timed_thread);
245 mpd_sendCurrentSongCommand(current_info->conn);
246 while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
247 mpd_Song *song = entity->info.song;
249 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
250 mpd_freeInfoEntity(entity);
255 strncpy(current_info->mpd.artist, song->artist,
256 small_text_buffer_size - 1);
258 *current_info->mpd.artist = 0;
261 strncpy(current_info->mpd.album, song->album,
262 small_text_buffer_size - 1);
264 *current_info->mpd.album = 0;
267 strncpy(current_info->mpd.title, song->title,
268 small_text_buffer_size - 1);
270 *current_info->mpd.title = 0;
273 strncpy(current_info->mpd.track, song->track,
274 small_text_buffer_size - 1);
276 *current_info->mpd.track = 0;
279 strncpy(current_info->mpd.name, song->name,
280 small_text_buffer_size - 1);
282 *current_info->mpd.name = 0;
285 strncpy(current_info->mpd.file, song->file,
286 small_text_buffer_size - 1);
288 *current_info->mpd.file = 0;
290 if (entity != NULL) {
291 mpd_freeInfoEntity(entity);
295 if (entity != NULL) {
296 mpd_freeInfoEntity(entity);
299 mpd_finishCommand(current_info->conn);
300 if (current_info->conn->error) {
301 // fprintf(stderr, "%s\n", current_info->conn->errorStr);
302 mpd_closeConnection(current_info->conn);
303 current_info->conn = 0;
304 timed_thread_unlock(mpd_timed_thread);
305 if (timed_thread_test(mpd_timed_thread)) {
306 timed_thread_exit(mpd_timed_thread);
311 timed_thread_unlock(mpd_timed_thread);
312 if (current_info->conn->error) {
313 // fprintf(stderr, "%s\n", current_info->conn->errorStr);
314 mpd_closeConnection(current_info->conn);
315 current_info->conn = 0;
316 if (timed_thread_test(mpd_timed_thread)) {
317 timed_thread_exit(mpd_timed_thread);
322 mpd_freeStatus(status);
323 /* if (current_info->conn) {
324 mpd_closeConnection(current_info->conn);
325 current_info->conn = 0;
327 if (timed_thread_test(mpd_timed_thread)) {
328 timed_thread_exit(mpd_timed_thread);