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
|
|
|
|
*
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (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-12-09 23:35:49 +00:00
|
|
|
*/
|
2006-03-16 18:29:23 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include "conky.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
2008-12-18 12:37:53 +00:00
|
|
|
#include "timed_thread.h"
|
|
|
|
#include "libmpdclient.h"
|
|
|
|
#include "mpd.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
/* server connection data */
|
|
|
|
static char mpd_host[128];
|
|
|
|
static char mpd_password[128];
|
|
|
|
static int mpd_port;
|
|
|
|
|
2009-06-14 23:15:04 +00:00
|
|
|
/* this is >0 if the current password was set from MPD_HOST */
|
|
|
|
static int mpd_environment_password = 0;
|
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
/* global mpd information */
|
|
|
|
static struct mpd_s mpd_info;
|
|
|
|
|
|
|
|
/* number of users of the above struct */
|
|
|
|
static int refcount = 0;
|
|
|
|
|
|
|
|
void mpd_set_host(const char *host)
|
2007-08-05 04:47:21 +00:00
|
|
|
{
|
2008-12-18 12:37:53 +00:00
|
|
|
snprintf(mpd_host, 128, "%s", host);
|
2009-06-14 23:15:04 +00:00
|
|
|
|
|
|
|
if (mpd_environment_password) {
|
|
|
|
/* for security, dont use environment password when user specifies host in config */
|
|
|
|
mpd_clear_password();
|
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
}
|
2009-06-14 23:15:04 +00:00
|
|
|
void mpd_set_password(const char *password, int from_environment)
|
2008-12-18 12:37:53 +00:00
|
|
|
{
|
|
|
|
snprintf(mpd_password, 128, "%s", password);
|
2009-06-14 23:15:04 +00:00
|
|
|
mpd_environment_password = from_environment;
|
2008-12-18 12:37:53 +00:00
|
|
|
}
|
|
|
|
void mpd_clear_password(void)
|
|
|
|
{
|
|
|
|
*mpd_password = '\0';
|
2009-06-14 23:15:04 +00:00
|
|
|
mpd_environment_password = 0;
|
2008-12-18 12:37:53 +00:00
|
|
|
}
|
|
|
|
int mpd_set_port(const char *port)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = strtol(port, 0, 0);
|
|
|
|
if (val < 1 || val > 0xffff)
|
|
|
|
return 1;
|
|
|
|
mpd_port = val;
|
|
|
|
return 0;
|
2007-08-31 23:45:41 +00:00
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
void init_mpd(void)
|
2008-03-19 22:28:23 +00:00
|
|
|
{
|
2008-12-18 12:37:53 +00:00
|
|
|
if (!(refcount++)) /* first client */
|
|
|
|
memset(&mpd_info, 0, sizeof(struct mpd_s));
|
|
|
|
|
|
|
|
refcount++;
|
2008-03-19 22:28:23 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
struct mpd_s *mpd_get_info(void)
|
2007-08-31 23:45:41 +00:00
|
|
|
{
|
2008-12-18 12:37:53 +00:00
|
|
|
return &mpd_info;
|
2007-08-05 04:47:21 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
static void clear_mpd(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-12-18 12:37:53 +00:00
|
|
|
#define xfree(x) if (x) free(x)
|
|
|
|
xfree(mpd_info.title);
|
|
|
|
xfree(mpd_info.artist);
|
|
|
|
xfree(mpd_info.album);
|
|
|
|
/* do not free() the const char *status! */
|
|
|
|
/* do not free() the const char *random! */
|
|
|
|
/* do not free() the const char *repeat! */
|
|
|
|
xfree(mpd_info.track);
|
|
|
|
xfree(mpd_info.name);
|
|
|
|
xfree(mpd_info.file);
|
|
|
|
#undef xfree
|
|
|
|
memset(&mpd_info, 0, sizeof(struct mpd_s));
|
|
|
|
}
|
2008-06-19 06:17:53 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
void free_mpd(void)
|
|
|
|
{
|
|
|
|
if (!(--refcount)) /* last client */
|
|
|
|
clear_mpd();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *update_mpd_thread(void *) __attribute__((noreturn));
|
|
|
|
|
|
|
|
void update_mpd(void)
|
|
|
|
{
|
|
|
|
int interval;
|
|
|
|
static timed_thread *thread = NULL;
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
return;
|
|
|
|
|
|
|
|
interval = info.music_player_interval * 1000000;
|
|
|
|
thread = timed_thread_create(&update_mpd_thread, &thread, interval);
|
|
|
|
if (!thread) {
|
|
|
|
ERR("Failed to create MPD timed thread");
|
|
|
|
return;
|
2008-03-29 05:14:35 +00:00
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
timed_thread_register(thread, &thread);
|
|
|
|
if (timed_thread_run(thread))
|
|
|
|
ERR("Failed to run MPD timed thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stringMAXdup dups at most text_buffer_size bytes */
|
|
|
|
#define strmdup(x) strndup(x, text_buffer_size - 1)
|
2008-03-29 05:14:35 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
static void *update_mpd_thread(void *arg)
|
|
|
|
{
|
|
|
|
static mpd_Connection *conn = NULL;
|
|
|
|
mpd_Status *status;
|
|
|
|
mpd_InfoEntity *entity;
|
|
|
|
timed_thread *me = *(timed_thread **)arg;
|
2009-02-18 05:42:08 +00:00
|
|
|
const char *emptystr = "";
|
2008-06-14 18:41:12 +00:00
|
|
|
|
2007-08-30 17:21:30 +00:00
|
|
|
while (1) {
|
2008-12-18 12:37:53 +00:00
|
|
|
if (!conn)
|
|
|
|
conn = mpd_newConnection(mpd_host, mpd_port, 10);
|
|
|
|
|
|
|
|
if (*mpd_password) {
|
|
|
|
mpd_sendPasswordCommand(conn, mpd_password);
|
|
|
|
mpd_finishCommand(conn);
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
timed_thread_lock(me);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
if (conn->error || conn == NULL) {
|
|
|
|
ERR("MPD error: %s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
clear_mpd();
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.status = "MPD not responding";
|
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
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
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_sendStatusCommand(conn);
|
|
|
|
if ((status = mpd_getStatus(conn)) == NULL) {
|
|
|
|
ERR("MPD error: %s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
clear_mpd();
|
|
|
|
|
|
|
|
mpd_info.status = "MPD not responding";
|
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-01-31 03:32:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_finishCommand(conn);
|
|
|
|
if (conn->error) {
|
|
|
|
// fprintf(stderr, "%s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-08-31 18:32:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.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
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
switch(status->state) {
|
|
|
|
case MPD_STATUS_STATE_PLAY:
|
|
|
|
mpd_info.status = "Playing";
|
|
|
|
break;
|
|
|
|
case MPD_STATUS_STATE_STOP:
|
|
|
|
mpd_info.status = "Stopped";
|
|
|
|
break;
|
|
|
|
case MPD_STATUS_STATE_PAUSE:
|
|
|
|
mpd_info.status = "Paused";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mpd_info.status = "";
|
|
|
|
clear_mpd();
|
|
|
|
break;
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
|
|
|
|
if (status->state == MPD_STATUS_STATE_PLAY ||
|
|
|
|
status->state == MPD_STATUS_STATE_PAUSE) {
|
|
|
|
mpd_info.is_playing = 1;
|
|
|
|
mpd_info.bitrate = status->bitRate;
|
|
|
|
mpd_info.progress = (float) status->elapsedTime /
|
2008-02-20 20:30:45 +00:00
|
|
|
status->totalTime;
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.elapsed = status->elapsedTime;
|
|
|
|
mpd_info.length = status->totalTime;
|
2007-08-30 17:21:30 +00:00
|
|
|
if (status->random == 0) {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.random = "Off";
|
2007-08-30 17:21:30 +00:00
|
|
|
} else if (status->random == 1) {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.random = "On";
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.random = "";
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
if (status->repeat == 0) {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.repeat = "Off";
|
2007-08-30 17:21:30 +00:00
|
|
|
} else if (status->repeat == 1) {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.repeat = "On";
|
2007-08-30 17:21:30 +00:00
|
|
|
} else {
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_info.repeat = "";
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
2009-06-15 00:02:58 +00:00
|
|
|
} else {
|
|
|
|
mpd_info.is_playing = 0;
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
if (conn->error) {
|
|
|
|
// fprintf(stderr, "%s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_sendCurrentSongCommand(conn);
|
|
|
|
while ((entity = mpd_getNextInfoEntity(conn))) {
|
2007-08-30 17:21:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-03-23 22:42:59 +00:00
|
|
|
#define SONGSET(x) { \
|
|
|
|
free(mpd_info.x); \
|
|
|
|
if(song->x) \
|
|
|
|
mpd_info.x = strmdup(song->x); \
|
|
|
|
else \
|
|
|
|
mpd_info.x = strmdup(emptystr); \
|
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
SONGSET(artist);
|
|
|
|
SONGSET(album);
|
|
|
|
SONGSET(title);
|
|
|
|
SONGSET(track);
|
|
|
|
SONGSET(name);
|
|
|
|
SONGSET(file);
|
|
|
|
#undef SONGSET
|
2007-08-30 17:21:30 +00:00
|
|
|
if (entity != NULL) {
|
|
|
|
mpd_freeInfoEntity(entity);
|
|
|
|
entity = NULL;
|
|
|
|
}
|
2006-01-31 03:32:26 +00:00
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
mpd_finishCommand(conn);
|
|
|
|
if (conn->error) {
|
|
|
|
// fprintf(stderr, "%s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-18 12:37:53 +00:00
|
|
|
timed_thread_unlock(me);
|
|
|
|
if (conn->error) {
|
|
|
|
// fprintf(stderr, "%s\n", conn->errorStr);
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
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-12-18 12:37:53 +00:00
|
|
|
/* if (conn) {
|
|
|
|
mpd_closeConnection(conn);
|
|
|
|
conn = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
} */
|
2008-12-18 12:37:53 +00:00
|
|
|
if (timed_thread_test(me, 0)) {
|
|
|
|
timed_thread_exit(me);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
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
|
|
|
}
|
2008-12-18 12:37:53 +00:00
|
|
|
|