mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-01-15 19:56:55 +00:00
Make mpd vars use the new update system
This commit is contained in:
parent
ba9be3fa18
commit
f3dfb21859
68
src/core.cc
68
src/core.cc
@ -1393,82 +1393,48 @@ struct text_object *construct_text_object(char *s, const char *arg, long
|
|||||||
else \
|
else \
|
||||||
NORM_ERR(#name ": invalid length argument"); \
|
NORM_ERR(#name ": invalid length argument"); \
|
||||||
}
|
}
|
||||||
END OBJ(mpd_artist, &update_mpd)
|
END OBJ(mpd_artist, 0)
|
||||||
mpd_set_maxlen(mpd_artist);
|
mpd_set_maxlen(mpd_artist);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_artist;
|
obj->callbacks.print = &print_mpd_artist;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_title, 0)
|
||||||
END OBJ(mpd_title, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_title);
|
mpd_set_maxlen(mpd_title);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_title;
|
obj->callbacks.print = &print_mpd_title;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_random, 0)
|
||||||
END OBJ(mpd_random, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_random;
|
obj->callbacks.print = &print_mpd_random;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_repeat, 0)
|
||||||
END OBJ(mpd_repeat, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_repeat;
|
obj->callbacks.print = &print_mpd_repeat;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_elapsed, 0)
|
||||||
END OBJ(mpd_elapsed, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_elapsed;
|
obj->callbacks.print = &print_mpd_elapsed;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_length, 0)
|
||||||
END OBJ(mpd_length, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_length;
|
obj->callbacks.print = &print_mpd_length;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_track, 0)
|
||||||
END OBJ(mpd_track, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_track);
|
mpd_set_maxlen(mpd_track);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_track;
|
obj->callbacks.print = &print_mpd_track;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_name, 0)
|
||||||
END OBJ(mpd_name, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_name);
|
mpd_set_maxlen(mpd_name);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_name;
|
obj->callbacks.print = &print_mpd_name;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_file, 0)
|
||||||
END OBJ(mpd_file, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_file);
|
mpd_set_maxlen(mpd_file);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_file;
|
obj->callbacks.print = &print_mpd_file;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_percent, 0)
|
||||||
END OBJ(mpd_percent, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.percentage = &mpd_percentage;
|
obj->callbacks.percentage = &mpd_percentage;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_album, 0)
|
||||||
END OBJ(mpd_album, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_album);
|
mpd_set_maxlen(mpd_album);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_album;
|
obj->callbacks.print = &print_mpd_album;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_vol, 0)
|
||||||
END OBJ(mpd_vol, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_vol;
|
obj->callbacks.print = &print_mpd_vol;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_bitrate, 0)
|
||||||
END OBJ(mpd_bitrate, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_bitrate;
|
obj->callbacks.print = &print_mpd_bitrate;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_status, 0)
|
||||||
END OBJ(mpd_status, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_status;
|
obj->callbacks.print = &print_mpd_status;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_bar, 0)
|
||||||
END OBJ(mpd_bar, &update_mpd)
|
|
||||||
scan_bar(obj, arg, 1);
|
scan_bar(obj, arg, 1);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.barval = &mpd_barval;
|
obj->callbacks.barval = &mpd_barval;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ(mpd_smart, 0)
|
||||||
END OBJ(mpd_smart, &update_mpd)
|
|
||||||
mpd_set_maxlen(mpd_smart);
|
mpd_set_maxlen(mpd_smart);
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.print = &print_mpd_smart;
|
obj->callbacks.print = &print_mpd_smart;
|
||||||
obj->callbacks.free = &free_mpd;
|
END OBJ_IF(if_mpd_playing, 0)
|
||||||
END OBJ_IF(if_mpd_playing, &update_mpd)
|
|
||||||
init_mpd();
|
|
||||||
obj->callbacks.iftest = &check_mpd_playing;
|
obj->callbacks.iftest = &check_mpd_playing;
|
||||||
obj->callbacks.free = &free_mpd;
|
|
||||||
#undef mpd_set_maxlen
|
#undef mpd_set_maxlen
|
||||||
#endif /* BUILD_MPD */
|
#endif /* BUILD_MPD */
|
||||||
#ifdef BUILD_MOC
|
#ifdef BUILD_MOC
|
||||||
|
192
src/mpd.cc
192
src/mpd.cc
@ -27,6 +27,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "conky.h"
|
#include "conky.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
@ -34,6 +35,7 @@
|
|||||||
#include "timeinfo.h"
|
#include "timeinfo.h"
|
||||||
#include "libmpdclient.h"
|
#include "libmpdclient.h"
|
||||||
#include "mpd.h"
|
#include "mpd.h"
|
||||||
|
#include "update-cb.hh"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -117,93 +119,50 @@ namespace {
|
|||||||
conky::range_config_setting<in_port_t> mpd_port("mpd_port", 1, 65535, 6600, false);
|
conky::range_config_setting<in_port_t> mpd_port("mpd_port", 1, 65535, 6600, false);
|
||||||
mpd_host_setting mpd_host;
|
mpd_host_setting mpd_host;
|
||||||
mpd_password_setting mpd_password;
|
mpd_password_setting mpd_password;
|
||||||
}
|
|
||||||
|
|
||||||
/* global mpd information */
|
struct mpd_result {
|
||||||
static struct {
|
std::string title;
|
||||||
char *title;
|
std::string artist;
|
||||||
char *artist;
|
std::string album;
|
||||||
char *album;
|
std::string status;
|
||||||
const char *status;
|
std::string random;
|
||||||
const char *random;
|
std::string repeat;
|
||||||
const char *repeat;
|
std::string track;
|
||||||
char *track;
|
std::string name;
|
||||||
char *name;
|
std::string file;
|
||||||
char *file;
|
|
||||||
int is_playing;
|
int is_playing;
|
||||||
int vol;
|
int vol;
|
||||||
float progress;
|
float progress;
|
||||||
int bitrate;
|
int bitrate;
|
||||||
int length;
|
int length;
|
||||||
int elapsed;
|
int elapsed;
|
||||||
} mpd_info;
|
};
|
||||||
|
|
||||||
/* number of users of the above struct */
|
class mpd_cb: public conky::callback<mpd_result> {
|
||||||
static int refcount = 0;
|
typedef conky::callback<mpd_result> Base;
|
||||||
|
|
||||||
void init_mpd(void)
|
mpd_Connection *conn;
|
||||||
{
|
|
||||||
if (!(refcount++)) /* first client */
|
|
||||||
memset(&mpd_info, 0, sizeof(mpd_info));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void clear_mpd(void)
|
protected:
|
||||||
{
|
virtual void work();
|
||||||
free_and_zero(mpd_info.title);
|
|
||||||
free_and_zero(mpd_info.artist);
|
|
||||||
free_and_zero(mpd_info.album);
|
|
||||||
/* do not free() the const char *status! */
|
|
||||||
/* do not free() the const char *random! */
|
|
||||||
/* do not free() the const char *repeat! */
|
|
||||||
free_and_zero(mpd_info.track);
|
|
||||||
free_and_zero(mpd_info.name);
|
|
||||||
free_and_zero(mpd_info.file);
|
|
||||||
memset(&mpd_info, 0, sizeof(mpd_info));
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_mpd(struct text_object *obj)
|
public:
|
||||||
{
|
mpd_cb(uint32_t period)
|
||||||
(void)obj;
|
: Base(period, false, Tuple()), conn(NULL)
|
||||||
|
{}
|
||||||
|
|
||||||
if (!(--refcount)) /* last client */
|
~mpd_cb()
|
||||||
clear_mpd();
|
{
|
||||||
}
|
if(conn)
|
||||||
|
mpd_closeConnection(conn);
|
||||||
static void update_mpd_thread(thread_handle &handle);
|
|
||||||
|
|
||||||
int update_mpd(void)
|
|
||||||
{
|
|
||||||
static timed_thread_ptr thread;
|
|
||||||
|
|
||||||
if (thread)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
thread = timed_thread::create(std::bind(update_mpd_thread, std::placeholders::_1),
|
|
||||||
std::chrono::microseconds(long(music_player_interval.get(*state) * 1000000)) );
|
|
||||||
if (!thread) {
|
|
||||||
NORM_ERR("Failed to create MPD timed thread");
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
return 0;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
/* stringMAXdup dups at most text_buffer_size bytes */
|
void mpd_cb::work()
|
||||||
#define strmdup(x) strndup(x, text_buffer_size.get(*state) - 1)
|
{
|
||||||
|
|
||||||
#define SONGSET(x) { \
|
|
||||||
free(mpd_info.x); \
|
|
||||||
if(song->x) \
|
|
||||||
mpd_info.x = strmdup(song->x); \
|
|
||||||
else \
|
|
||||||
mpd_info.x = strmdup(emptystr); \
|
|
||||||
}
|
|
||||||
|
|
||||||
bool mpd_process(thread_handle &handle)
|
|
||||||
{
|
|
||||||
static mpd_Connection *conn = NULL;
|
|
||||||
mpd_Status *status;
|
mpd_Status *status;
|
||||||
mpd_InfoEntity *entity;
|
mpd_InfoEntity *entity;
|
||||||
const char *emptystr = "";
|
mpd_result mpd_info;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!conn)
|
if (!conn)
|
||||||
@ -214,14 +173,10 @@ bool mpd_process(thread_handle &handle)
|
|||||||
mpd_finishCommand(conn);
|
mpd_finishCommand(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
if (conn->error) {
|
||||||
std::lock_guard<std::mutex> lock(handle.mutex());
|
|
||||||
|
|
||||||
if (conn->error || conn == NULL) {
|
|
||||||
NORM_ERR("MPD error: %s\n", conn->errorStr);
|
NORM_ERR("MPD error: %s\n", conn->errorStr);
|
||||||
mpd_closeConnection(conn);
|
mpd_closeConnection(conn);
|
||||||
conn = 0;
|
conn = 0;
|
||||||
clear_mpd();
|
|
||||||
|
|
||||||
mpd_info.status = "MPD not responding";
|
mpd_info.status = "MPD not responding";
|
||||||
break;
|
break;
|
||||||
@ -232,7 +187,6 @@ bool mpd_process(thread_handle &handle)
|
|||||||
NORM_ERR("MPD error: %s\n", conn->errorStr);
|
NORM_ERR("MPD error: %s\n", conn->errorStr);
|
||||||
mpd_closeConnection(conn);
|
mpd_closeConnection(conn);
|
||||||
conn = 0;
|
conn = 0;
|
||||||
clear_mpd();
|
|
||||||
|
|
||||||
mpd_info.status = "MPD not responding";
|
mpd_info.status = "MPD not responding";
|
||||||
}
|
}
|
||||||
@ -275,7 +229,6 @@ bool mpd_process(thread_handle &handle)
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
mpd_info.status = "";
|
mpd_info.status = "";
|
||||||
clear_mpd();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,13 +261,12 @@ bool mpd_process(thread_handle &handle)
|
|||||||
mpd_freeInfoEntity(entity);
|
mpd_freeInfoEntity(entity);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SONGSET(artist);
|
mpd_info.artist = song->artist;
|
||||||
SONGSET(album);
|
mpd_info.album = song->album;
|
||||||
SONGSET(title);
|
mpd_info.title = song->title;
|
||||||
SONGSET(track);
|
mpd_info.track = song->track;
|
||||||
SONGSET(name);
|
mpd_info.name = song->name;
|
||||||
SONGSET(file);
|
mpd_info.file = song->file;
|
||||||
#undef SONGSET
|
|
||||||
if (entity != NULL) {
|
if (entity != NULL) {
|
||||||
mpd_freeInfoEntity(entity);
|
mpd_freeInfoEntity(entity);
|
||||||
entity = NULL;
|
entity = NULL;
|
||||||
@ -328,7 +280,6 @@ bool mpd_process(thread_handle &handle)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conn->error) {
|
if (conn->error) {
|
||||||
// fprintf(stderr, "%s\n", conn->errorStr);
|
// fprintf(stderr, "%s\n", conn->errorStr);
|
||||||
@ -343,13 +294,15 @@ bool mpd_process(thread_handle &handle)
|
|||||||
conn = 0;
|
conn = 0;
|
||||||
} */
|
} */
|
||||||
} while (0);
|
} while (0);
|
||||||
return !handle.test(0);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void update_mpd_thread(thread_handle &handle)
|
mpd_result get_mpd()
|
||||||
{
|
{
|
||||||
while (mpd_process(handle)) ;
|
uint32_t period = std::max(
|
||||||
/* never reached */
|
std::lround(music_player_interval.get(*state)/active_update_interval()), 1l
|
||||||
|
);
|
||||||
|
return conky::register_cb<mpd_cb>(period)->get_result_copy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void format_media_player_time(char *buf, const int size,
|
static inline void format_media_player_time(char *buf, const int size,
|
||||||
@ -383,44 +336,43 @@ static inline void format_media_player_time(char *buf, const int size,
|
|||||||
void print_mpd_elapsed(struct text_object *obj, char *p, int p_max_size)
|
void print_mpd_elapsed(struct text_object *obj, char *p, int p_max_size)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
format_media_player_time(p, p_max_size, mpd_info.elapsed);
|
format_media_player_time(p, p_max_size, get_mpd().elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_mpd_length(struct text_object *obj, char *p, int p_max_size)
|
void print_mpd_length(struct text_object *obj, char *p, int p_max_size)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
format_media_player_time(p, p_max_size, mpd_info.length);
|
format_media_player_time(p, p_max_size, get_mpd().length);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t mpd_percentage(struct text_object *obj)
|
uint8_t mpd_percentage(struct text_object *obj)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
return round_to_int(mpd_info.progress * 100.0f);
|
return round_to_int(get_mpd().progress * 100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
double mpd_barval(struct text_object *obj)
|
double mpd_barval(struct text_object *obj)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
return mpd_info.progress;
|
return get_mpd().progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
|
void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
|
||||||
{
|
{
|
||||||
|
const mpd_result &mpd_info = get_mpd();
|
||||||
int len = obj->data.i;
|
int len = obj->data.i;
|
||||||
if (len == 0 || len > p_max_size)
|
if (len == 0 || len > p_max_size)
|
||||||
len = p_max_size;
|
len = p_max_size;
|
||||||
|
|
||||||
memset(p, 0, p_max_size);
|
memset(p, 0, p_max_size);
|
||||||
if (mpd_info.artist && *mpd_info.artist &&
|
if (mpd_info.artist.size() && mpd_info.title.size()) {
|
||||||
mpd_info.title && *mpd_info.title) {
|
snprintf(p, len, "%s - %s", mpd_info.artist.c_str(), mpd_info.title.c_str());
|
||||||
snprintf(p, len, "%s - %s", mpd_info.artist,
|
} else if (get_mpd().title.size()) {
|
||||||
mpd_info.title);
|
snprintf(p, len, "%s", mpd_info.title.c_str());
|
||||||
} else if (mpd_info.title && *mpd_info.title) {
|
} else if (mpd_info.artist.size()) {
|
||||||
snprintf(p, len, "%s", mpd_info.title);
|
snprintf(p, len, "%s", mpd_info.artist.c_str());
|
||||||
} else if (mpd_info.artist && *mpd_info.artist) {
|
} else if (mpd_info.file.size()) {
|
||||||
snprintf(p, len, "%s", mpd_info.artist);
|
snprintf(p, len, "%s", mpd_info.file.c_str());
|
||||||
} else if (mpd_info.file && *mpd_info.file) {
|
|
||||||
snprintf(p, len, "%s", mpd_info.file);
|
|
||||||
} else {
|
} else {
|
||||||
*p = 0;
|
*p = 0;
|
||||||
}
|
}
|
||||||
@ -429,27 +381,27 @@ void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
|
|||||||
int check_mpd_playing(struct text_object *obj)
|
int check_mpd_playing(struct text_object *obj)
|
||||||
{
|
{
|
||||||
(void)obj;
|
(void)obj;
|
||||||
return mpd_info.is_playing;
|
return get_mpd().is_playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MPD_PRINT_GENERATOR(name, fmt) \
|
#define MPD_PRINT_GENERATOR(name, fmt, acc) \
|
||||||
void print_mpd_##name(struct text_object *obj, char *p, int p_max_size) \
|
void print_mpd_##name(struct text_object *obj, char *p, int p_max_size) \
|
||||||
{ \
|
{ \
|
||||||
if (obj->data.i && obj->data.i < p_max_size) \
|
if (obj->data.i && obj->data.i < p_max_size) \
|
||||||
p_max_size = obj->data.i; \
|
p_max_size = obj->data.i; \
|
||||||
snprintf(p, p_max_size, fmt, mpd_info.name); \
|
snprintf(p, p_max_size, fmt, get_mpd().name acc); \
|
||||||
}
|
}
|
||||||
|
|
||||||
MPD_PRINT_GENERATOR(title, "%s")
|
MPD_PRINT_GENERATOR(title, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(artist, "%s")
|
MPD_PRINT_GENERATOR(artist, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(album, "%s")
|
MPD_PRINT_GENERATOR(album, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(random, "%s")
|
MPD_PRINT_GENERATOR(random, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(repeat, "%s")
|
MPD_PRINT_GENERATOR(repeat, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(track, "%s")
|
MPD_PRINT_GENERATOR(track, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(name, "%s")
|
MPD_PRINT_GENERATOR(name, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(file, "%s")
|
MPD_PRINT_GENERATOR(file, "%s", .c_str())
|
||||||
MPD_PRINT_GENERATOR(vol, "%d")
|
MPD_PRINT_GENERATOR(vol, "%d", )
|
||||||
MPD_PRINT_GENERATOR(bitrate, "%d")
|
MPD_PRINT_GENERATOR(bitrate, "%d", )
|
||||||
MPD_PRINT_GENERATOR(status, "%s")
|
MPD_PRINT_GENERATOR(status, "%s", .c_str())
|
||||||
|
|
||||||
#undef MPD_PRINT_GENERATOR
|
#undef MPD_PRINT_GENERATOR
|
||||||
|
@ -27,10 +27,6 @@
|
|||||||
#define MPD_H_
|
#define MPD_H_
|
||||||
|
|
||||||
/* text object functions */
|
/* text object functions */
|
||||||
void init_mpd(void);
|
|
||||||
void free_mpd(struct text_object *);
|
|
||||||
int update_mpd(void);
|
|
||||||
|
|
||||||
void print_mpd_elapsed(struct text_object *, char *, int);
|
void print_mpd_elapsed(struct text_object *, char *, int);
|
||||||
void print_mpd_length(struct text_object *, char *, int);
|
void print_mpd_length(struct text_object *, char *, int);
|
||||||
uint8_t mpd_percentage(struct text_object *);
|
uint8_t mpd_percentage(struct text_object *);
|
||||||
|
Loading…
Reference in New Issue
Block a user