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)
122 void *update_mpd(void *arg)
125 CRIT_ERR("update_mpd called with a null argument!");
128 struct mpd_s *mpd = (struct mpd_s *) arg;
132 mpd_InfoEntity *entity;
135 mpd->conn = mpd_newConnection(mpd->host,
138 if (strlen(mpd->password) > 1) {
139 mpd_sendPasswordCommand(mpd->conn,
141 mpd_finishCommand(mpd->conn);
144 timed_thread_lock(mpd->timed_thread);
146 if (mpd->conn->error || mpd->conn == NULL) {
147 ERR("MPD error: %s\n", mpd->conn->errorStr);
148 mpd_closeConnection(mpd->conn);
150 clear_mpd_stats(mpd);
152 strncpy(mpd->status, "MPD not responding",
153 text_buffer_size - 1);
154 timed_thread_unlock(mpd->timed_thread);
155 if (timed_thread_test(mpd->timed_thread)) {
156 timed_thread_exit(mpd->timed_thread);
161 mpd_sendStatusCommand(mpd->conn);
162 if ((status = mpd_getStatus(mpd->conn)) == NULL) {
163 ERR("MPD error: %s\n", mpd->conn->errorStr);
164 mpd_closeConnection(mpd->conn);
166 clear_mpd_stats(mpd);
168 strncpy(mpd->status, "MPD not responding",
169 text_buffer_size - 1);
170 timed_thread_unlock(mpd->timed_thread);
171 if (timed_thread_test(mpd->timed_thread)) {
172 timed_thread_exit(mpd->timed_thread);
176 mpd_finishCommand(mpd->conn);
177 if (mpd->conn->error) {
178 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
179 mpd_closeConnection(mpd->conn);
181 timed_thread_unlock(mpd->timed_thread);
182 if (timed_thread_test(mpd->timed_thread)) {
183 timed_thread_exit(mpd->timed_thread);
188 mpd->volume = status->volume;
189 /* if (status->error) {
190 printf("error: %s\n", status->error);
193 if (status->state == MPD_STATUS_STATE_PLAY) {
194 strncpy(mpd->status, "Playing", text_buffer_size - 1);
196 if (status->state == MPD_STATUS_STATE_STOP) {
197 clear_mpd_stats(mpd);
198 strncpy(mpd->status, "Stopped", text_buffer_size - 1);
200 if (status->state == MPD_STATUS_STATE_PAUSE) {
201 strncpy(mpd->status, "Paused", text_buffer_size - 1);
203 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
204 clear_mpd_stats(mpd);
207 if (status->state == MPD_STATUS_STATE_PLAY
208 || status->state == MPD_STATUS_STATE_PAUSE) {
209 mpd->bitrate = status->bitRate;
210 mpd->progress = (float) status->elapsedTime /
212 mpd->elapsed = status->elapsedTime;
213 mpd->length = status->totalTime;
214 if (status->random == 0) {
215 strcpy(mpd->random, "Off");
216 } else if (status->random == 1) {
217 strcpy(mpd->random, "On");
221 if (status->repeat == 0) {
222 strcpy(mpd->repeat, "Off");
223 } else if (status->repeat == 1) {
224 strcpy(mpd->repeat, "On");
230 if (mpd->conn->error) {
231 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
232 mpd_closeConnection(mpd->conn);
234 timed_thread_unlock(mpd->timed_thread);
235 if (timed_thread_test(mpd->timed_thread)) {
236 timed_thread_exit(mpd->timed_thread);
241 mpd_sendCurrentSongCommand(mpd->conn);
242 while ((entity = mpd_getNextInfoEntity(mpd->conn))) {
243 mpd_Song *song = entity->info.song;
245 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
246 mpd_freeInfoEntity(entity);
251 strncpy(mpd->artist, song->artist,
252 text_buffer_size - 1);
257 strncpy(mpd->album, song->album,
258 text_buffer_size - 1);
263 strncpy(mpd->title, song->title,
264 text_buffer_size - 1);
269 strncpy(mpd->track, song->track,
270 text_buffer_size - 1);
275 strncpy(mpd->name, song->name,
276 text_buffer_size - 1);
281 strncpy(mpd->file, song->file,
282 text_buffer_size - 1);
286 if (entity != NULL) {
287 mpd_freeInfoEntity(entity);
291 if (entity != NULL) {
292 mpd_freeInfoEntity(entity);
295 mpd_finishCommand(mpd->conn);
296 if (mpd->conn->error) {
297 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
298 mpd_closeConnection(mpd->conn);
300 timed_thread_unlock(mpd->timed_thread);
301 if (timed_thread_test(mpd->timed_thread)) {
302 timed_thread_exit(mpd->timed_thread);
307 timed_thread_unlock(mpd->timed_thread);
308 if (mpd->conn->error) {
309 // fprintf(stderr, "%s\n", mpd->conn->errorStr);
310 mpd_closeConnection(mpd->conn);
312 if (timed_thread_test(mpd->timed_thread)) {
313 timed_thread_exit(mpd->timed_thread);
318 mpd_freeStatus(status);
320 mpd_closeConnection(mpd->conn);
323 if (timed_thread_test(mpd->timed_thread)) {
324 timed_thread_exit(mpd->timed_thread);