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 void init_mpd_stats(struct mpd_s *mpd)
31 if (mpd->artist == NULL) {
32 mpd->artist = malloc(text_buffer_size);
34 if (mpd->album == NULL) {
35 mpd->album = malloc(text_buffer_size);
37 if (mpd->title == NULL) {
38 mpd->title = malloc(text_buffer_size);
40 if (mpd->random == NULL) {
41 mpd->random = malloc(text_buffer_size);
43 if (mpd->repeat == NULL) {
44 mpd->repeat = malloc(text_buffer_size);
46 if (mpd->track == NULL) {
47 mpd->track = malloc(text_buffer_size);
49 if (mpd->status == NULL) {
50 mpd->status = malloc(text_buffer_size);
52 if (mpd->name == NULL) {
53 mpd->name = malloc(text_buffer_size);
55 if (mpd->file == NULL) {
56 mpd->file = malloc(text_buffer_size);
61 void free_mpd_vars(struct mpd_s *mpd)
100 mpd_closeConnection(mpd->conn);
105 void clear_mpd_stats(struct mpd_s *mpd)
123 void *update_mpd(void *arg)
128 CRIT_ERR("update_mpd called with a null argument!");
131 mpd = (struct mpd_s *) arg;
135 mpd_InfoEntity *entity;
138 mpd->conn = mpd_newConnection(mpd->host,
141 if (strlen(mpd->password) > 1) {
142 mpd_sendPasswordCommand(mpd->conn,
144 mpd_finishCommand(mpd->conn);
147 timed_thread_lock(mpd->timed_thread);
149 if (mpd->conn->error || mpd->conn == NULL) {
150 ERR("MPD error: %s\n", mpd->conn->errorStr);
151 mpd_closeConnection(mpd->conn);
153 clear_mpd_stats(mpd);
155 strncpy(mpd->status, "MPD not responding",
156 text_buffer_size - 1);
157 timed_thread_unlock(mpd->timed_thread);
158 if (timed_thread_test(mpd->timed_thread, 0)) {
159 timed_thread_exit(mpd->timed_thread);
164 mpd_sendStatusCommand(mpd->conn);
165 if ((status = mpd_getStatus(mpd->conn)) == NULL) {
166 ERR("MPD error: %s\n", mpd->conn->errorStr);
167 mpd_closeConnection(mpd->conn);
169 clear_mpd_stats(mpd);
171 strncpy(mpd->status, "MPD not responding",
172 text_buffer_size - 1);
173 timed_thread_unlock(mpd->timed_thread);
174 if (timed_thread_test(mpd->timed_thread, 0)) {
175 timed_thread_exit(mpd->timed_thread);
179 mpd_finishCommand(mpd->conn);
180 if (mpd->conn->error) {
181 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
182 mpd_closeConnection(mpd->conn);
184 timed_thread_unlock(mpd->timed_thread);
185 if (timed_thread_test(mpd->timed_thread, 0)) {
186 timed_thread_exit(mpd->timed_thread);
191 mpd->volume = status->volume;
192 /* if (status->error) {
193 printf("error: %s\n", status->error);
196 if (status->state == MPD_STATUS_STATE_PLAY) {
197 strncpy(mpd->status, "Playing", text_buffer_size - 1);
199 if (status->state == MPD_STATUS_STATE_STOP) {
200 clear_mpd_stats(mpd);
201 strncpy(mpd->status, "Stopped", text_buffer_size - 1);
203 if (status->state == MPD_STATUS_STATE_PAUSE) {
204 strncpy(mpd->status, "Paused", text_buffer_size - 1);
206 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
207 clear_mpd_stats(mpd);
210 if (status->state == MPD_STATUS_STATE_PLAY
211 || status->state == MPD_STATUS_STATE_PAUSE) {
213 mpd->bitrate = status->bitRate;
214 mpd->progress = (float) status->elapsedTime /
216 mpd->elapsed = status->elapsedTime;
217 mpd->length = status->totalTime;
218 if (status->random == 0) {
219 strcpy(mpd->random, "Off");
220 } else if (status->random == 1) {
221 strcpy(mpd->random, "On");
225 if (status->repeat == 0) {
226 strcpy(mpd->repeat, "Off");
227 } else if (status->repeat == 1) {
228 strcpy(mpd->repeat, "On");
234 if (mpd->conn->error) {
235 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
236 mpd_closeConnection(mpd->conn);
238 timed_thread_unlock(mpd->timed_thread);
239 if (timed_thread_test(mpd->timed_thread, 0)) {
240 timed_thread_exit(mpd->timed_thread);
245 mpd_sendCurrentSongCommand(mpd->conn);
246 while ((entity = mpd_getNextInfoEntity(mpd->conn))) {
247 mpd_Song *song = entity->info.song;
249 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
250 mpd_freeInfoEntity(entity);
255 strncpy(mpd->artist, song->artist,
256 text_buffer_size - 1);
261 strncpy(mpd->album, song->album,
262 text_buffer_size - 1);
267 strncpy(mpd->title, song->title,
268 text_buffer_size - 1);
273 strncpy(mpd->track, song->track,
274 text_buffer_size - 1);
279 strncpy(mpd->name, song->name,
280 text_buffer_size - 1);
285 strncpy(mpd->file, song->file,
286 text_buffer_size - 1);
290 if (entity != NULL) {
291 mpd_freeInfoEntity(entity);
295 if (entity != NULL) {
296 mpd_freeInfoEntity(entity);
299 mpd_finishCommand(mpd->conn);
300 if (mpd->conn->error) {
301 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
302 mpd_closeConnection(mpd->conn);
304 timed_thread_unlock(mpd->timed_thread);
305 if (timed_thread_test(mpd->timed_thread, 0)) {
306 timed_thread_exit(mpd->timed_thread);
311 timed_thread_unlock(mpd->timed_thread);
312 if (mpd->conn->error) {
313 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
314 mpd_closeConnection(mpd->conn);
316 if (timed_thread_test(mpd->timed_thread, 0)) {
317 timed_thread_exit(mpd->timed_thread);
322 mpd_freeStatus(status);
324 mpd_closeConnection(mpd->conn);
327 if (timed_thread_test(mpd->timed_thread, 0)) {
328 timed_thread_exit(mpd->timed_thread);