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/>.
30 void init_mpd_stats(struct mpd_s *mpd)
32 if (mpd->artist == NULL) {
33 mpd->artist = malloc(text_buffer_size);
35 if (mpd->album == NULL) {
36 mpd->album = malloc(text_buffer_size);
38 if (mpd->title == NULL) {
39 mpd->title = malloc(text_buffer_size);
41 if (mpd->random == NULL) {
42 mpd->random = malloc(text_buffer_size);
44 if (mpd->repeat == NULL) {
45 mpd->repeat = malloc(text_buffer_size);
47 if (mpd->track == NULL) {
48 mpd->track = malloc(text_buffer_size);
50 if (mpd->status == NULL) {
51 mpd->status = malloc(text_buffer_size);
53 if (mpd->name == NULL) {
54 mpd->name = malloc(text_buffer_size);
56 if (mpd->file == NULL) {
57 mpd->file = malloc(text_buffer_size);
62 void free_mpd_vars(struct mpd_s *mpd)
101 mpd_closeConnection(mpd->conn);
106 void clear_mpd_stats(struct mpd_s *mpd)
124 void *update_mpd(void *arg)
129 CRIT_ERR("update_mpd called with a null argument!");
132 mpd = (struct mpd_s *) arg;
136 mpd_InfoEntity *entity;
139 mpd->conn = mpd_newConnection(mpd->host,
142 if (strlen(mpd->password) > 1) {
143 mpd_sendPasswordCommand(mpd->conn,
145 mpd_finishCommand(mpd->conn);
148 timed_thread_lock(mpd->timed_thread);
150 if (mpd->conn->error || mpd->conn == NULL) {
151 ERR("MPD error: %s\n", mpd->conn->errorStr);
152 mpd_closeConnection(mpd->conn);
154 clear_mpd_stats(mpd);
156 strncpy(mpd->status, "MPD not responding",
157 text_buffer_size - 1);
158 timed_thread_unlock(mpd->timed_thread);
159 if (timed_thread_test(mpd->timed_thread, 0)) {
160 timed_thread_exit(mpd->timed_thread);
165 mpd_sendStatusCommand(mpd->conn);
166 if ((status = mpd_getStatus(mpd->conn)) == NULL) {
167 ERR("MPD error: %s\n", mpd->conn->errorStr);
168 mpd_closeConnection(mpd->conn);
170 clear_mpd_stats(mpd);
172 strncpy(mpd->status, "MPD not responding",
173 text_buffer_size - 1);
174 timed_thread_unlock(mpd->timed_thread);
175 if (timed_thread_test(mpd->timed_thread, 0)) {
176 timed_thread_exit(mpd->timed_thread);
180 mpd_finishCommand(mpd->conn);
181 if (mpd->conn->error) {
182 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
183 mpd_closeConnection(mpd->conn);
185 timed_thread_unlock(mpd->timed_thread);
186 if (timed_thread_test(mpd->timed_thread, 0)) {
187 timed_thread_exit(mpd->timed_thread);
192 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(mpd->status, "Playing", text_buffer_size - 1);
200 if (status->state == MPD_STATUS_STATE_STOP) {
201 clear_mpd_stats(mpd);
202 strncpy(mpd->status, "Stopped", text_buffer_size - 1);
204 if (status->state == MPD_STATUS_STATE_PAUSE) {
205 strncpy(mpd->status, "Paused", text_buffer_size - 1);
207 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
208 clear_mpd_stats(mpd);
211 if (status->state == MPD_STATUS_STATE_PLAY
212 || status->state == MPD_STATUS_STATE_PAUSE) {
214 mpd->bitrate = status->bitRate;
215 mpd->progress = (float) status->elapsedTime /
217 mpd->elapsed = status->elapsedTime;
218 mpd->length = status->totalTime;
219 if (status->random == 0) {
220 strcpy(mpd->random, "Off");
221 } else if (status->random == 1) {
222 strcpy(mpd->random, "On");
226 if (status->repeat == 0) {
227 strcpy(mpd->repeat, "Off");
228 } else if (status->repeat == 1) {
229 strcpy(mpd->repeat, "On");
235 if (mpd->conn->error) {
236 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
237 mpd_closeConnection(mpd->conn);
239 timed_thread_unlock(mpd->timed_thread);
240 if (timed_thread_test(mpd->timed_thread, 0)) {
241 timed_thread_exit(mpd->timed_thread);
246 mpd_sendCurrentSongCommand(mpd->conn);
247 while ((entity = mpd_getNextInfoEntity(mpd->conn))) {
248 mpd_Song *song = entity->info.song;
250 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
251 mpd_freeInfoEntity(entity);
256 strncpy(mpd->artist, song->artist,
257 text_buffer_size - 1);
262 strncpy(mpd->album, song->album,
263 text_buffer_size - 1);
268 strncpy(mpd->title, song->title,
269 text_buffer_size - 1);
274 strncpy(mpd->track, song->track,
275 text_buffer_size - 1);
280 strncpy(mpd->name, song->name,
281 text_buffer_size - 1);
286 strncpy(mpd->file, song->file,
287 text_buffer_size - 1);
291 if (entity != NULL) {
292 mpd_freeInfoEntity(entity);
296 if (entity != NULL) {
297 mpd_freeInfoEntity(entity);
300 mpd_finishCommand(mpd->conn);
301 if (mpd->conn->error) {
302 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
303 mpd_closeConnection(mpd->conn);
305 timed_thread_unlock(mpd->timed_thread);
306 if (timed_thread_test(mpd->timed_thread, 0)) {
307 timed_thread_exit(mpd->timed_thread);
312 timed_thread_unlock(mpd->timed_thread);
313 if (mpd->conn->error) {
314 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
315 mpd_closeConnection(mpd->conn);
317 if (timed_thread_test(mpd->timed_thread, 0)) {
318 timed_thread_exit(mpd->timed_thread);
323 mpd_freeStatus(status);
325 mpd_closeConnection(mpd->conn);
328 if (timed_thread_test(mpd->timed_thread, 0)) {
329 timed_thread_exit(mpd->timed_thread);