diff --git a/src/conky.c b/src/conky.c index 55c582d7..f4068cd9 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1522,44 +1522,6 @@ void generate_text_internal(char *p, int p_max_size, } #endif -#ifdef MOC -#define MOC_PRINT(t, a) \ - snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a)) - OBJ(moc_state) { - MOC_PRINT(state, "??"); - } - OBJ(moc_file) { - MOC_PRINT(file, "no file"); - } - OBJ(moc_title) { - MOC_PRINT(title, "no title"); - } - OBJ(moc_artist) { - MOC_PRINT(artist, "no artist"); - } - OBJ(moc_song) { - MOC_PRINT(song, "no song"); - } - OBJ(moc_album) { - MOC_PRINT(album, "no album"); - } - OBJ(moc_totaltime) { - MOC_PRINT(totaltime, "0:00"); - } - OBJ(moc_timeleft) { - MOC_PRINT(timeleft, "0:00"); - } - OBJ(moc_curtime) { - MOC_PRINT(curtime, "0:00"); - } - OBJ(moc_bitrate) { - MOC_PRINT(bitrate, "0Kbps"); - } - OBJ(moc_rate) { - MOC_PRINT(rate, "0KHz"); - } -#undef MOC_PRINT -#endif /* MOC */ #ifdef XMMS2 OBJ(xmms2_artist) { snprintf(p, p_max_size, "%s", cur->xmms2.artist); diff --git a/src/core.c b/src/core.c index 776bbeb5..2e5b75da 100644 --- a/src/core.c +++ b/src/core.c @@ -1132,16 +1132,38 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #endif /* MPD */ #ifdef MOC END OBJ(moc_state, &update_moc) + obj->callbacks.print = &print_moc_state; + obj->callbacks.free = &free_moc; END OBJ(moc_file, &update_moc) + obj->callbacks.print = &print_moc_file; + obj->callbacks.free = &free_moc; END OBJ(moc_title, &update_moc) + obj->callbacks.print = &print_moc_title; + obj->callbacks.free = &free_moc; END OBJ(moc_artist, &update_moc) + obj->callbacks.print = &print_moc_artist; + obj->callbacks.free = &free_moc; END OBJ(moc_song, &update_moc) + obj->callbacks.print = &print_moc_song; + obj->callbacks.free = &free_moc; END OBJ(moc_album, &update_moc) + obj->callbacks.print = &print_moc_album; + obj->callbacks.free = &free_moc; END OBJ(moc_totaltime, &update_moc) + obj->callbacks.print = &print_moc_totaltime; + obj->callbacks.free = &free_moc; END OBJ(moc_timeleft, &update_moc) + obj->callbacks.print = &print_moc_timeleft; + obj->callbacks.free = &free_moc; END OBJ(moc_curtime, &update_moc) + obj->callbacks.print = &print_moc_curtime; + obj->callbacks.free = &free_moc; END OBJ(moc_bitrate, &update_moc) + obj->callbacks.print = &print_moc_bitrate; + obj->callbacks.free = &free_moc; END OBJ(moc_rate, &update_moc) + obj->callbacks.print = &print_moc_rate; + obj->callbacks.free = &free_moc; #endif /* MOC */ #ifdef XMMS2 END OBJ(xmms2_artist, &update_xmms2) @@ -1838,21 +1860,6 @@ void free_text_objects(struct text_object *root, int internal) free_mpd(); break; #endif /* MPD */ -#ifdef MOC - case OBJ_moc_state: - case OBJ_moc_file: - case OBJ_moc_title: - case OBJ_moc_artist: - case OBJ_moc_song: - case OBJ_moc_album: - case OBJ_moc_totaltime: - case OBJ_moc_timeleft: - case OBJ_moc_curtime: - case OBJ_moc_bitrate: - case OBJ_moc_rate: - free_moc(); - break; -#endif /* MOC */ case OBJ_include: case OBJ_blink: case OBJ_to_bytes: diff --git a/src/moc.c b/src/moc.c index eca3329f..a8c00cbf 100644 --- a/src/moc.c +++ b/src/moc.c @@ -23,7 +23,8 @@ #include "conky.h" #include "logging.h" -#include "moc.h" +#include "text_object.h" +#include "timed_thread.h" #include #include @@ -31,11 +32,25 @@ #define xfree(x) if (x) free(x); x = 0 -struct moc_s moc; +static struct { + char *state; + char *file; + char *title; + char *artist; + char *song; + char *album; + char *totaltime; + char *timeleft; + char *curtime; + char *bitrate; + char *rate; +} moc; + static timed_thread *moc_thread = NULL; -void free_moc(void) +void free_moc(struct text_object *obj) { + (void)obj; xfree(moc.state); xfree(moc.file); xfree(moc.title); @@ -53,7 +68,7 @@ static void update_infos(void) { FILE *fp; - free_moc(); + free_moc(NULL); fp = popen("mocp -i", "r"); if (!fp) { moc.state = strndup("Can't run 'mocp -i'", text_buffer_size); @@ -137,3 +152,24 @@ void update_moc(void) { run_moc_thread(info.music_player_interval * 100000); } + +#define MOC_PRINT_GENERATOR(type, alt) \ +void print_moc_##type(struct text_object *obj, char *p, int p_max_size) \ +{ \ + (void)obj; \ + snprintf(p, p_max_size, "%s", (moc.type ? moc.type : alt)); \ +} + +MOC_PRINT_GENERATOR(state, "??") +MOC_PRINT_GENERATOR(file, "no file") +MOC_PRINT_GENERATOR(title, "no title") +MOC_PRINT_GENERATOR(artist, "no artist") +MOC_PRINT_GENERATOR(song, "no song") +MOC_PRINT_GENERATOR(album, "no album") +MOC_PRINT_GENERATOR(totaltime, "0:00") +MOC_PRINT_GENERATOR(timeleft, "0:00") +MOC_PRINT_GENERATOR(curtime, "0:00") +MOC_PRINT_GENERATOR(bitrate, "0Kbps") +MOC_PRINT_GENERATOR(rate, "0KHz") + +#undef MOC_PRINT_GENERATOR diff --git a/src/moc.h b/src/moc.h index b1d9cc5d..68e606f6 100644 --- a/src/moc.h +++ b/src/moc.h @@ -23,25 +23,20 @@ #ifndef MOC_H_ #define MOC_H_ -#include "timed_thread.h" - -struct moc_s { - char *state; - char *file; - char *title; - char *artist; - char *song; - char *album; - char *totaltime; - char *timeleft; - char *curtime; - char *bitrate; - char *rate; -}; -extern struct moc_s moc; - void update_moc(void); -void free_moc(void); +void free_moc(struct text_object *); + +void print_moc_state(struct text_object *, char *, int); +void print_moc_file(struct text_object *, char *, int); +void print_moc_title(struct text_object *, char *, int); +void print_moc_artist(struct text_object *, char *, int); +void print_moc_song(struct text_object *, char *, int); +void print_moc_album(struct text_object *, char *, int); +void print_moc_totaltime(struct text_object *, char *, int); +void print_moc_timeleft(struct text_object *, char *, int); +void print_moc_curtime(struct text_object *, char *, int); +void print_moc_bitrate(struct text_object *, char *, int); +void print_moc_rate(struct text_object *, char *, int); #endif /* MOC_H_ */