2008-02-20 20:30:45 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
2006-03-16 18:29:23 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
|
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* $Id$ */
|
2006-03-16 18:29:23 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "libmpdclient.h"
|
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
timed_thread *mpd_timed_thread = NULL;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-08-31 23:45:41 +00:00
|
|
|
void clear_mpd_stats(struct information *current_info);
|
2007-08-30 17:21:30 +00:00
|
|
|
|
2007-08-31 23:45:41 +00:00
|
|
|
void init_mpd_stats(struct information *current_info)
|
2007-08-05 04:47:21 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (current_info->mpd.artist == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.artist = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.album == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.album = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.title == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.title = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.random == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.random = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.repeat == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.repeat = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.track == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.track = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.status == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.status = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.name == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.name = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (current_info->mpd.file == NULL) {
|
2008-03-20 21:28:19 +00:00
|
|
|
current_info->mpd.file = malloc(text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-08-31 23:45:41 +00:00
|
|
|
clear_mpd_stats(current_info);
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2008-03-19 22:28:23 +00:00
|
|
|
void free_mpd_vars(struct information *current_info)
|
|
|
|
{
|
|
|
|
if (current_info->mpd.title) {
|
|
|
|
free(current_info->mpd.title);
|
|
|
|
current_info->mpd.title = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.artist) {
|
|
|
|
free(current_info->mpd.artist);
|
|
|
|
current_info->mpd.artist = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.album) {
|
|
|
|
free(current_info->mpd.album);
|
|
|
|
current_info->mpd.album = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.random) {
|
|
|
|
free(current_info->mpd.random);
|
|
|
|
current_info->mpd.random = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.repeat) {
|
|
|
|
free(current_info->mpd.repeat);
|
|
|
|
current_info->mpd.repeat = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.track) {
|
|
|
|
free(current_info->mpd.track);
|
|
|
|
current_info->mpd.track = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.name) {
|
|
|
|
free(current_info->mpd.name);
|
|
|
|
current_info->mpd.name = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.file) {
|
|
|
|
free(current_info->mpd.file);
|
|
|
|
current_info->mpd.file = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->mpd.status) {
|
|
|
|
free(current_info->mpd.status);
|
|
|
|
current_info->mpd.status = NULL;
|
|
|
|
}
|
|
|
|
if (current_info->conn) {
|
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-31 23:45:41 +00:00
|
|
|
void clear_mpd_stats(struct information *current_info)
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.name = 0;
|
|
|
|
*current_info->mpd.file = 0;
|
|
|
|
*current_info->mpd.artist = 0;
|
|
|
|
*current_info->mpd.album = 0;
|
|
|
|
*current_info->mpd.title = 0;
|
|
|
|
*current_info->mpd.random = 0;
|
|
|
|
*current_info->mpd.repeat = 0;
|
|
|
|
*current_info->mpd.track = 0;
|
|
|
|
*current_info->mpd.status = 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
current_info->mpd.bitrate = 0;
|
|
|
|
current_info->mpd.progress = 0;
|
|
|
|
current_info->mpd.elapsed = 0;
|
|
|
|
current_info->mpd.length = 0;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 05:14:35 +00:00
|
|
|
void *update_mpd(void *arg)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2007-08-31 03:37:32 +00:00
|
|
|
struct information *current_info = &info;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2008-03-29 05:14:35 +00:00
|
|
|
if (arg) {
|
|
|
|
/* make gcc happy (unused argument) */
|
|
|
|
}
|
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
while (1) {
|
2008-03-29 06:24:04 +00:00
|
|
|
mpd_Status *status;
|
|
|
|
mpd_InfoEntity *entity;
|
|
|
|
|
2007-08-31 03:37:32 +00:00
|
|
|
if (!current_info->conn) {
|
2008-02-20 20:30:45 +00:00
|
|
|
current_info->conn = mpd_newConnection(current_info->mpd.host,
|
|
|
|
current_info->mpd.port, 10);
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (strlen(current_info->mpd.password) > 1) {
|
|
|
|
mpd_sendPasswordCommand(current_info->conn,
|
2008-02-20 20:30:45 +00:00
|
|
|
current_info->mpd.password);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_finishCommand(current_info->conn);
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-08-31 23:45:41 +00:00
|
|
|
timed_thread_lock(mpd_timed_thread);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2007-08-31 03:37:32 +00:00
|
|
|
if (current_info->conn->error || current_info->conn == NULL) {
|
2008-03-30 01:23:04 +00:00
|
|
|
ERR("MPD error: %s\n", current_info->conn->errorStr);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2007-09-01 20:36:56 +00:00
|
|
|
clear_mpd_stats(current_info);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
strncpy(current_info->mpd.status, "MPD not responding",
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-09-01 18:18:26 +00:00
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_sendStatusCommand(current_info->conn);
|
|
|
|
if ((status = mpd_getStatus(current_info->conn)) == NULL) {
|
2008-03-30 01:23:04 +00:00
|
|
|
ERR("MPD error: %s\n", current_info->conn->errorStr);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2007-09-01 20:36:56 +00:00
|
|
|
clear_mpd_stats(current_info);
|
2007-08-30 17:21:30 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
strncpy(current_info->mpd.status, "MPD not responding",
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2006-01-31 03:32:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 18:32:51 +00:00
|
|
|
mpd_finishCommand(current_info->conn);
|
|
|
|
if (current_info->conn->error) {
|
2008-02-20 20:30:45 +00:00
|
|
|
// fprintf(stderr, "%s\n", current_info->conn->errorStr);
|
2007-08-31 18:32:51 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2007-09-01 18:18:26 +00:00
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-31 18:32:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
current_info->mpd.volume = status->volume;
|
2008-02-20 20:30:45 +00:00
|
|
|
/* if (status->error) {
|
|
|
|
printf("error: %s\n", status->error);
|
|
|
|
} */
|
2007-08-30 17:21:30 +00:00
|
|
|
|
|
|
|
if (status->state == MPD_STATUS_STATE_PLAY) {
|
2008-03-20 21:28:19 +00:00
|
|
|
strncpy(current_info->mpd.status, "Playing", text_buffer_size - 1);
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
if (status->state == MPD_STATUS_STATE_STOP) {
|
2007-10-02 23:51:49 +00:00
|
|
|
clear_mpd_stats(current_info);
|
2008-03-20 21:28:19 +00:00
|
|
|
strncpy(current_info->mpd.status, "Stopped", text_buffer_size - 1);
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
if (status->state == MPD_STATUS_STATE_PAUSE) {
|
2008-03-20 21:28:19 +00:00
|
|
|
strncpy(current_info->mpd.status, "Paused", text_buffer_size - 1);
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
if (status->state == MPD_STATUS_STATE_UNKNOWN) {
|
2007-09-28 19:50:58 +00:00
|
|
|
clear_mpd_stats(current_info);
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.status = 0;
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
if (status->state == MPD_STATUS_STATE_PLAY
|
|
|
|
|| status->state == MPD_STATUS_STATE_PAUSE) {
|
2007-08-30 17:21:30 +00:00
|
|
|
current_info->mpd.bitrate = status->bitRate;
|
2008-02-20 20:30:45 +00:00
|
|
|
current_info->mpd.progress = (float) status->elapsedTime /
|
|
|
|
status->totalTime;
|
2007-08-30 17:21:30 +00:00
|
|
|
current_info->mpd.elapsed = status->elapsedTime;
|
|
|
|
current_info->mpd.length = status->totalTime;
|
|
|
|
if (status->random == 0) {
|
|
|
|
strcpy(current_info->mpd.random, "Off");
|
|
|
|
} else if (status->random == 1) {
|
|
|
|
strcpy(current_info->mpd.random, "On");
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.random = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (status->repeat == 0) {
|
|
|
|
strcpy(current_info->mpd.repeat, "Off");
|
|
|
|
} else if (status->repeat == 1) {
|
|
|
|
strcpy(current_info->mpd.repeat, "On");
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.repeat = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
|
|
|
|
if (current_info->conn->error) {
|
2008-02-20 20:30:45 +00:00
|
|
|
// fprintf(stderr, "%s\n", current_info->conn->errorStr);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-31 18:32:51 +00:00
|
|
|
mpd_sendCurrentSongCommand(current_info->conn);
|
2007-08-30 17:21:30 +00:00
|
|
|
while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
|
|
|
|
mpd_Song *song = entity->info.song;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
|
|
|
|
mpd_freeInfoEntity(entity);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (song->artist) {
|
|
|
|
strncpy(current_info->mpd.artist, song->artist,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.artist = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (song->album) {
|
|
|
|
strncpy(current_info->mpd.album, song->album,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.album = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (song->title) {
|
|
|
|
strncpy(current_info->mpd.title, song->title,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.title = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (song->track) {
|
|
|
|
strncpy(current_info->mpd.track, song->track,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.track = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (song->name) {
|
|
|
|
strncpy(current_info->mpd.name, song->name,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.name = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (song->file) {
|
2008-02-20 20:30:45 +00:00
|
|
|
strncpy(current_info->mpd.file, song->file,
|
2008-03-20 21:28:19 +00:00
|
|
|
text_buffer_size - 1);
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
*current_info->mpd.file = 0;
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (entity != NULL) {
|
|
|
|
mpd_freeInfoEntity(entity);
|
|
|
|
entity = NULL;
|
|
|
|
}
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
|
|
|
if (entity != NULL) {
|
|
|
|
mpd_freeInfoEntity(entity);
|
|
|
|
entity = NULL;
|
|
|
|
}
|
2007-08-31 18:32:51 +00:00
|
|
|
mpd_finishCommand(current_info->conn);
|
2007-08-30 17:21:30 +00:00
|
|
|
if (current_info->conn->error) {
|
2008-02-20 20:30:45 +00:00
|
|
|
// fprintf(stderr, "%s\n", current_info->conn->errorStr);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2007-09-01 18:18:26 +00:00
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2007-08-31 18:32:51 +00:00
|
|
|
timed_thread_unlock(mpd_timed_thread);
|
2007-08-30 17:21:30 +00:00
|
|
|
if (current_info->conn->error) {
|
2008-02-20 20:30:45 +00:00
|
|
|
// fprintf(stderr, "%s\n", current_info->conn->errorStr);
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 18:32:51 +00:00
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_freeStatus(status);
|
2008-02-20 20:30:45 +00:00
|
|
|
/* if (current_info->conn) {
|
2007-08-30 17:21:30 +00:00
|
|
|
mpd_closeConnection(current_info->conn);
|
|
|
|
current_info->conn = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
} */
|
|
|
|
if (timed_thread_test(mpd_timed_thread)) {
|
|
|
|
timed_thread_exit(mpd_timed_thread);
|
|
|
|
}
|
2007-08-31 02:05:02 +00:00
|
|
|
continue;
|
2007-08-27 20:26:58 +00:00
|
|
|
}
|
2008-03-29 11:35:02 +00:00
|
|
|
/* never reached */
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|