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;
36 void clear_mpd_stats(struct information *current_info)
38 if (current_info->mpd.artist == NULL)
39 current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE);
40 if (current_info->mpd.album == NULL)
41 current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
42 if (current_info->mpd.title == NULL)
43 current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
44 if (current_info->mpd.random == NULL)
45 current_info->mpd.random = malloc(TEXT_BUFFER_SIZE);
46 if (current_info->mpd.repeat == NULL)
47 current_info->mpd.repeat = malloc(TEXT_BUFFER_SIZE);
48 if (current_info->mpd.track == NULL)
49 current_info->mpd.track = malloc(TEXT_BUFFER_SIZE);
50 if (current_info->mpd.status == NULL)
51 current_info->mpd.status = malloc(TEXT_BUFFER_SIZE);
52 if (current_info->mpd.name == NULL)
53 current_info->mpd.name = malloc(TEXT_BUFFER_SIZE);
54 if (current_info->mpd.file == NULL)
55 current_info->mpd.file = malloc(TEXT_BUFFER_SIZE);
57 *current_info->mpd.name=0;
58 *current_info->mpd.file=0;
59 *current_info->mpd.artist=0;
60 *current_info->mpd.album=0;
61 *current_info->mpd.title=0;
62 *current_info->mpd.random=0;
63 *current_info->mpd.repeat=0;
64 *current_info->mpd.track=0;
65 *current_info->mpd.status=0;
66 current_info->mpd.bitrate = 0;
67 current_info->mpd.progress = 0;
68 current_info->mpd.elapsed = 0;
69 current_info->mpd.length = 0;
72 void *update_mpd(void)
74 struct information *current_info = &info;
76 if (!current_info->conn) {
77 current_info->conn = mpd_newConnection(current_info->mpd.host, current_info->mpd.port, 10);
79 if (strlen(current_info->mpd.password) > 1) {
80 mpd_sendPasswordCommand(current_info->conn,
81 current_info->mpd.password);
82 mpd_finishCommand(current_info->conn);
85 if (current_info->conn->error || current_info->conn == NULL) {
86 //ERR("%MPD error: s\n", current_info->conn->errorStr);
87 mpd_closeConnection(current_info->conn);
88 current_info->conn = 0;
90 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
91 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
95 timed_thread_lock(mpd_timed_thread);
97 mpd_InfoEntity *entity;
98 mpd_sendCommandListOkBegin(current_info->conn);
99 mpd_sendStatusCommand(current_info->conn);
100 mpd_sendCurrentSongCommand(current_info->conn);
101 mpd_sendCommandListEnd(current_info->conn);
102 if ((status = mpd_getStatus(current_info->conn)) == NULL) {
103 //ERR("MPD error: %s\n", current_info->conn->errorStr);
104 mpd_closeConnection(current_info->conn);
105 current_info->conn = 0;
107 strncpy(current_info->mpd.status, "MPD not responding", TEXT_BUFFER_SIZE - 1);
108 timed_thread_unlock(mpd_timed_thread);
109 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
113 current_info->mpd.volume = status->volume;
115 //printf("error: %s\n", status->error);
117 if (status->state == MPD_STATUS_STATE_PLAY) {
118 strncpy(current_info->mpd.status, "Playing",
119 TEXT_BUFFER_SIZE - 1);
121 if (status->state == MPD_STATUS_STATE_STOP) {
122 strncpy(current_info->mpd.status, "Stopped",
123 TEXT_BUFFER_SIZE - 1);
125 if (status->state == MPD_STATUS_STATE_PAUSE) {
126 strncpy(current_info->mpd.status, "Paused",
127 TEXT_BUFFER_SIZE - 1);
129 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
130 // current_info was already cleaned up by clear_mpd_stats()
132 if (status->state == MPD_STATUS_STATE_PLAY ||
133 status->state == MPD_STATUS_STATE_PAUSE) {
134 current_info->mpd.bitrate = status->bitRate;
135 current_info->mpd.progress =
136 (float) status->elapsedTime / status->totalTime;
137 current_info->mpd.elapsed = status->elapsedTime;
138 current_info->mpd.length = status->totalTime;
139 if (status->random == 0) {
140 strcpy(current_info->mpd.random, "Off");
141 } else if (status->random == 1) {
142 strcpy(current_info->mpd.random, "On");
144 *current_info->mpd.random=0;
146 if (status->repeat == 0) {
147 strcpy(current_info->mpd.repeat, "Off");
148 } else if (status->repeat == 1) {
149 strcpy(current_info->mpd.repeat, "On");
151 *current_info->mpd.repeat=0;
155 if (current_info->conn->error) {
156 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
157 mpd_closeConnection(current_info->conn);
158 current_info->conn = 0;
159 timed_thread_unlock(mpd_timed_thread);
160 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
164 mpd_nextListOkCommand(current_info->conn);
166 while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
167 mpd_Song *song = entity->info.song;
168 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
169 mpd_freeInfoEntity(entity);
174 strncpy(current_info->mpd.artist, song->artist,
175 TEXT_BUFFER_SIZE - 1);
177 *current_info->mpd.artist=0;
180 strncpy(current_info->mpd.album, song->album,
181 TEXT_BUFFER_SIZE - 1);
183 *current_info->mpd.album=0;
186 strncpy(current_info->mpd.title, song->title,
187 TEXT_BUFFER_SIZE - 1);
189 *current_info->mpd.title=0;
192 strncpy(current_info->mpd.track, song->track,
193 TEXT_BUFFER_SIZE - 1);
195 *current_info->mpd.track=0;
198 strncpy(current_info->mpd.name, song->name,
199 TEXT_BUFFER_SIZE - 1);
201 *current_info->mpd.name=0;
204 strncpy(current_info->mpd.file,
205 song->file, TEXT_BUFFER_SIZE - 1);
207 *current_info->mpd.file=0;
209 if (entity != NULL) {
210 mpd_freeInfoEntity(entity);
214 if (entity != NULL) {
215 mpd_freeInfoEntity(entity);
219 timed_thread_unlock(mpd_timed_thread);
220 if (current_info->conn->error) {
221 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
222 mpd_closeConnection(current_info->conn);
223 current_info->conn = 0;
224 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
228 mpd_finishCommand(current_info->conn);
229 if (current_info->conn->error) {
230 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
231 mpd_closeConnection(current_info->conn);
232 current_info->conn = 0;
233 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);
236 mpd_freeStatus(status);
237 /* if (current_info->conn) {
238 mpd_closeConnection(current_info->conn);
239 current_info->conn = 0;
241 if (timed_thread_test(mpd_timed_thread)) timed_thread_exit(mpd_timed_thread);