mpd: outsource printing code
authorPhil Sutter <phil@nwl.cc>
Mon, 9 Nov 2009 21:11:06 +0000 (22:11 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 23:01:33 +0000 (00:01 +0100)
In order to make life a bit easier, the struct mpd_s field "volume" has
been renamed to just "vol" to match the object's name (mpd_vol).

Although format_media_player_time() is probably meant to be used by all
supported media players, it's currently being used by mpd only. So for
now this function can reside statically in mpd.c

src/conky.c
src/mpd.c
src/mpd.h

index 3e3e7de..9b6b14c 100644 (file)
@@ -721,34 +721,6 @@ void parse_conky_vars(struct text_object *root, const char *txt,
        generate_text_internal(p, p_max_size, *root, cur);
 }
 
        generate_text_internal(p, p_max_size, *root, cur);
 }
 
-static inline void format_media_player_time(char *buf, const int size,
-               int seconds)
-{
-       int days, hours, minutes;
-
-       if (times_in_seconds()) {
-               snprintf(buf, size, "%d", seconds);
-               return;
-       }
-
-       days = seconds / (24 * 60 * 60);
-       seconds %= (24 * 60 * 60);
-       hours = seconds / (60 * 60);
-       seconds %= (60 * 60);
-       minutes = seconds / 60;
-       seconds %= 60;
-
-       if (days > 0) {
-               snprintf(buf, size, "%i days %i:%02i:%02i", days,
-                               hours, minutes, seconds);
-       } else if (hours > 0) {
-               snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
-                               seconds);
-       } else {
-               snprintf(buf, size, "%i:%02i", minutes, seconds);
-       }
-}
-
 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
  * multiline objects like $exec work with $align[rc] and friends
  */
 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
  * multiline objects like $exec work with $align[rc] and friends
  */
@@ -1988,81 +1960,48 @@ void generate_text_internal(char *p, int p_max_size,
 #endif /* __FreeBSD__ __OpenBSD__ */
 
 #ifdef MPD
 #endif /* __FreeBSD__ __OpenBSD__ */
 
 #ifdef MPD
-#define mpd_printf(fmt, val) \
-       snprintf(p, p_max_size, fmt, mpd_get_info()->val)
-#define mpd_sprintf(val) { \
-       if (!obj->data.i || obj->data.i > p_max_size) \
-               mpd_printf("%s", val); \
-       else \
-               snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
-}
                        OBJ(mpd_title)
                        OBJ(mpd_title)
-                               mpd_sprintf(title);
+                               print_mpd_title(obj, p, p_max_size);
                        OBJ(mpd_artist)
                        OBJ(mpd_artist)
-                               mpd_sprintf(artist);
+                               print_mpd_artist(obj, p, p_max_size);
                        OBJ(mpd_album)
                        OBJ(mpd_album)
-                               mpd_sprintf(album);
+                               print_mpd_album(obj, p, p_max_size);
                        OBJ(mpd_random)
                        OBJ(mpd_random)
-                               mpd_printf("%s", random);
+                               print_mpd_random(obj, p, p_max_size);
                        OBJ(mpd_repeat)
                        OBJ(mpd_repeat)
-                               mpd_printf("%s", repeat);
+                               print_mpd_repeat(obj, p, p_max_size);
                        OBJ(mpd_track)
                        OBJ(mpd_track)
-                               mpd_sprintf(track);
+                               print_mpd_track(obj, p, p_max_size);
                        OBJ(mpd_name)
                        OBJ(mpd_name)
-                               mpd_sprintf(name);
+                               print_mpd_name(obj, p, p_max_size);
                        OBJ(mpd_file)
                        OBJ(mpd_file)
-                               mpd_sprintf(file);
+                               print_mpd_file(obj, p, p_max_size);
                        OBJ(mpd_vol)
                        OBJ(mpd_vol)
-                               mpd_printf("%d", volume);
+                               print_mpd_vol(obj, p, p_max_size);
                        OBJ(mpd_bitrate)
                        OBJ(mpd_bitrate)
-                               mpd_printf("%d", bitrate);
+                               print_mpd_bitrate(obj, p, p_max_size);
                        OBJ(mpd_status)
                        OBJ(mpd_status)
-                               mpd_printf("%s", status);
+                               print_mpd_status(obj, p, p_max_size);
                        OBJ(mpd_elapsed) {
                        OBJ(mpd_elapsed) {
-                               format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
+                               print_mpd_elapsed(obj, p, p_max_size);
                        }
                        OBJ(mpd_length) {
                        }
                        OBJ(mpd_length) {
-                               format_media_player_time(p, p_max_size, mpd_get_info()->length);
+                               print_mpd_length(obj, p, p_max_size);
                        }
                        OBJ(mpd_percent) {
                        }
                        OBJ(mpd_percent) {
-                               percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
+                               print_mpd_percent(obj, p, p_max_size);
                        }
                        OBJ(mpd_bar) {
                        }
                        OBJ(mpd_bar) {
-#ifdef X11
-                               if(output_methods & TO_X) {
-                                       new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
-                               } else
-#endif /* X11 */
-                                       new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
+                               print_mpd_bar(obj, p, p_max_size);
                        }
                        OBJ(mpd_smart) {
                        }
                        OBJ(mpd_smart) {
-                               struct mpd_s *mpd = mpd_get_info();
-                               int len = obj->data.i;
-                               if (len == 0 || len > p_max_size)
-                                       len = p_max_size;
-
-                               memset(p, 0, p_max_size);
-                               if (mpd->artist && *mpd->artist &&
-                                   mpd->title && *mpd->title) {
-                                       snprintf(p, len, "%s - %s", mpd->artist,
-                                               mpd->title);
-                               } else if (mpd->title && *mpd->title) {
-                                       snprintf(p, len, "%s", mpd->title);
-                               } else if (mpd->artist && *mpd->artist) {
-                                       snprintf(p, len, "%s", mpd->artist);
-                               } else if (mpd->file && *mpd->file) {
-                                       snprintf(p, len, "%s", mpd->file);
-                               } else {
-                                       *p = 0;
-                               }
+                               print_mpd_smart(obj, p, p_max_size);
                        }
                        OBJ(if_mpd_playing) {
                                if (!mpd_get_info()->is_playing) {
                                        DO_JUMP;
                                }
                        }
                        }
                        OBJ(if_mpd_playing) {
                                if (!mpd_get_info()->is_playing) {
                                        DO_JUMP;
                                }
                        }
-#undef mpd_sprintf
-#undef mpd_printf
 #endif
 
 #ifdef MOC
 #endif
 
 #ifdef MOC
index 2bfdf18..e7a5673 100644 (file)
--- a/src/mpd.c
+++ b/src/mpd.c
@@ -30,6 +30,7 @@
 #include "conky.h"
 #include "logging.h"
 #include "timed_thread.h"
 #include "conky.h"
 #include "logging.h"
 #include "timed_thread.h"
+#include "timeinfo.h"
 #include "libmpdclient.h"
 #include "mpd.h"
 
 #include "libmpdclient.h"
 #include "mpd.h"
 
@@ -195,7 +196,7 @@ static void *update_mpd_thread(void *arg)
                        continue;
                }
 
                        continue;
                }
 
-               mpd_info.volume = status->volume;
+               mpd_info.vol = status->volume;
                if (status->random == 0) {
                        mpd_info.random = "Off";
                } else if (status->random == 1) {
                if (status->random == 0) {
                        mpd_info.random = "Off";
                } else if (status->random == 1) {
@@ -318,3 +319,103 @@ static void *update_mpd_thread(void *arg)
        /* never reached */
 }
 
        /* never reached */
 }
 
+static inline void format_media_player_time(char *buf, const int size,
+               int seconds)
+{
+       int days, hours, minutes;
+
+       if (times_in_seconds()) {
+               snprintf(buf, size, "%d", seconds);
+               return;
+       }
+
+       days = seconds / (24 * 60 * 60);
+       seconds %= (24 * 60 * 60);
+       hours = seconds / (60 * 60);
+       seconds %= (60 * 60);
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, size, "%i days %i:%02i:%02i", days,
+                               hours, minutes, seconds);
+       } else if (hours > 0) {
+               snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
+                               seconds);
+       } else {
+               snprintf(buf, size, "%i:%02i", minutes, seconds);
+       }
+}
+
+void print_mpd_elapsed(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
+}
+
+void print_mpd_length(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       format_media_player_time(p, p_max_size, mpd_get_info()->length);
+}
+
+void print_mpd_percent(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
+}
+
+void print_mpd_bar(struct text_object *obj, char *p, int p_max_size)
+{
+#ifdef X11
+       if(output_methods & TO_X) {
+               new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
+       } else
+#endif /* X11 */
+               new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
+}
+
+void print_mpd_smart(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mpd_s *mpd = mpd_get_info();
+       int len = obj->data.i;
+       if (len == 0 || len > p_max_size)
+               len = p_max_size;
+
+       memset(p, 0, p_max_size);
+       if (mpd->artist && *mpd->artist &&
+                       mpd->title && *mpd->title) {
+               snprintf(p, len, "%s - %s", mpd->artist,
+                               mpd->title);
+       } else if (mpd->title && *mpd->title) {
+               snprintf(p, len, "%s", mpd->title);
+       } else if (mpd->artist && *mpd->artist) {
+               snprintf(p, len, "%s", mpd->artist);
+       } else if (mpd->file && *mpd->file) {
+               snprintf(p, len, "%s", mpd->file);
+       } else {
+               *p = 0;
+       }
+}
+
+#define MPD_PRINT_GENERATOR(name, fmt) \
+void print_mpd_##name(struct text_object *obj, char *p, int p_max_size) \
+{ \
+       if (obj->data.i && obj->data.i < p_max_size) \
+               p_max_size = obj->data.i; \
+       snprintf(p, p_max_size, fmt, mpd_get_info()->name); \
+}
+
+MPD_PRINT_GENERATOR(title, "%s")
+MPD_PRINT_GENERATOR(artist, "%s")
+MPD_PRINT_GENERATOR(album, "%s")
+MPD_PRINT_GENERATOR(random, "%s")
+MPD_PRINT_GENERATOR(repeat, "%s")
+MPD_PRINT_GENERATOR(track, "%s")
+MPD_PRINT_GENERATOR(name, "%s")
+MPD_PRINT_GENERATOR(file, "%s")
+MPD_PRINT_GENERATOR(vol, "%d")
+MPD_PRINT_GENERATOR(bitrate, "%d")
+MPD_PRINT_GENERATOR(status, "%s")
+
+#undef MPD_PRINT_GENERATOR
index da6a13c..94d0d29 100644 (file)
--- a/src/mpd.h
+++ b/src/mpd.h
@@ -3,8 +3,6 @@
 #ifndef MPD_H_
 #define MPD_H_
 
 #ifndef MPD_H_
 #define MPD_H_
 
-//#include "conky.h"
-
 struct mpd_s {
        char *title;
        char *artist;
 struct mpd_s {
        char *title;
        char *artist;
@@ -16,7 +14,7 @@ struct mpd_s {
        char *name;
        char *file;
        int is_playing;
        char *name;
        char *file;
        int is_playing;
-       int volume;
+       int vol;
        float progress;
        int bitrate;
        int length;
        float progress;
        int bitrate;
        int length;
@@ -35,4 +33,21 @@ struct mpd_s *mpd_get_info(void);
 void free_mpd(void);
 void update_mpd(void);
 
 void free_mpd(void);
 void update_mpd(void);
 
+void print_mpd_elapsed(struct text_object *, char *, int);
+void print_mpd_length(struct text_object *, char *, int);
+void print_mpd_percent(struct text_object *, char *, int);
+void print_mpd_bar(struct text_object *, char *, int);
+void print_mpd_smart(struct text_object *, char *, int);
+void print_mpd_title(struct text_object *, char *, int);
+void print_mpd_artist(struct text_object *, char *, int);
+void print_mpd_album(struct text_object *, char *, int);
+void print_mpd_random(struct text_object *, char *, int);
+void print_mpd_repeat(struct text_object *, char *, int);
+void print_mpd_track(struct text_object *, char *, int);
+void print_mpd_name(struct text_object *, char *, int);
+void print_mpd_file(struct text_object *, char *, int);
+void print_mpd_vol(struct text_object *, char *, int);
+void print_mpd_bitrate(struct text_object *, char *, int);
+void print_mpd_status(struct text_object *, char *, int);
+
 #endif /*MPD_H_*/
 #endif /*MPD_H_*/