Code

utils: move format_duration_*() to time_format.c
[ncmpc.git] / src / status_bar.c
index ddffb8ee060f7c5ee577541ab89da0deebcc2953..fc63d20e49e0d4fa4b50df1e5ac9b63e9da523db 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
 #include "charset.h"
 #include "strfsong.h"
 #include "player_command.h"
-#include "utils.h"
+#include "time_format.h"
 
 #include <mpd/client.h>
 
 #include <assert.h>
 #include <string.h>
 
-#ifndef NCMPC_MINI
-static gboolean
-scroll_timer_callback(gpointer data)
+void
+status_bar_init(struct status_bar *p, unsigned width, int y, int x)
 {
-       struct status_bar *p = data;
+       window_init(&p->window, 1, width, y, x);
 
-       p->scroll_source_id = 0;
+       leaveok(p->window.w, false);
+       keypad(p->window.w, true);
 
-       hscroll_step(&p->hscroll);
-       status_bar_paint(p, p->prev_status, p->prev_song);
-       doupdate();
-       return false;
-}
+       p->message_source_id = 0;
+
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_init(&p->hscroll, p->window.w, options.scroll_sep);
+
+       p->prev_status = NULL;
+       p->prev_song = NULL;
 #endif
+}
 
-static gboolean
-status_bar_clear_message(gpointer data)
+void
+status_bar_deinit(struct status_bar *p)
 {
-       struct status_bar *p = data;
-       WINDOW *w = p->window.w;
+       delwin(p->window.w);
 
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_clear(&p->hscroll);
+#endif
+}
+
+void
+status_bar_clear_message(struct status_bar *p)
+{
        assert(p != NULL);
-       assert(p->message_source_id != 0);
 
-       p->message_source_id = 0;
+       if (p->message_source_id != 0) {
+               g_source_remove(p->message_source_id);
+               p->message_source_id = 0;
+       }
+
+       WINDOW *w = p->window.w;
 
        wmove(w, 0, 0);
        wclrtoeol(w);
        wrefresh(w);
+}
 
-       return false;
+#ifndef NCMPC_MINI
+
+static void
+format_bitrate(char *p, size_t max_length, const struct mpd_status *status)
+{
+       if (options.visible_bitrate && mpd_status_get_kbit_rate(status) > 0)
+               g_snprintf(p, max_length,
+                          " [%d kbps]",
+                          mpd_status_get_kbit_rate(status));
+       else
+               p[0] = '\0';
 }
 
+#endif /* !NCMPC_MINI */
+
 void
 status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                 const struct mpd_song *song)
 {
        WINDOW *w = p->window.w;
-       enum mpd_state state;
-       int elapsedTime = 0;
-#ifdef NCMPC_MINI
-       static char bitrate[1];
-#else
-       char bitrate[16];
-#endif
-       const char *str = NULL;
-       int x = 0;
        char buffer[p->window.cols * 4 + 1];
 
 #ifndef NCMPC_MINI
@@ -92,9 +112,10 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
        wclrtoeol(w);
        colors_use(w, COLOR_STATUS_BOLD);
 
-       state = status == NULL ? MPD_STATE_UNKNOWN
+       enum mpd_state state = status == NULL ? MPD_STATE_UNKNOWN
                : mpd_status_get_state(status);
 
+       const char *str = NULL;
        switch (state) {
        case MPD_STATE_PLAY:
                str = _("Playing:");
@@ -107,6 +128,7 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                break;
        }
 
+       int x = 0;
        if (str) {
                waddstr(w, str);
                x += utf8_width(str) + 1;
@@ -114,30 +136,28 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
 
        /* create time string */
        if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
+               int elapsedTime = seek_id >= 0 &&
+                       seek_id == mpd_status_get_song_id(status)
+                       ? (unsigned)seek_target_time
+                       : mpd_status_get_elapsed_time(status);
                int total_time = mpd_status_get_total_time(status);
-               if (total_time > 0) {
+               if (elapsedTime > 0 || total_time > 0) {
+#ifdef NCMPC_MINI
+                       static const char bitrate[1];
+#else
+                       char bitrate[16];
+#endif
                        char elapsed_string[32], duration_string[32];
 
                        /*checks the conf to see whether to display elapsed or remaining time */
-                       if(!strcmp(options.timedisplay_type,"elapsed"))
-                               elapsedTime = mpd_status_get_elapsed_time(status);
-                       else if(!strcmp(options.timedisplay_type,"remaining"))
-                               elapsedTime = total_time -
-                                       mpd_status_get_elapsed_time(status);
-
-                       if (song != NULL &&
-                           seek_id == (int)mpd_song_get_id(song))
-                               elapsedTime = seek_target_time;
+                       if (options.display_remaining_time)
+                               elapsedTime = elapsedTime < total_time
+                                       ? total_time - elapsedTime
+                                       : 0;
 
                        /* display bitrate if visible-bitrate is true */
 #ifndef NCMPC_MINI
-                       if (options.visible_bitrate) {
-                               g_snprintf(bitrate, 16,
-                                          " [%d kbps]",
-                                          mpd_status_get_kbit_rate(status));
-                       } else {
-                               bitrate[0] = '\0';
-                       }
+                       format_bitrate(bitrate, sizeof(bitrate), status);
 #endif
 
                        /* write out the time */
@@ -152,21 +172,13 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                                   bitrate, elapsed_string, duration_string);
 #ifndef NCMPC_MINI
                } else {
-                       g_snprintf(buffer, sizeof(buffer),
-                                  " [%d kbps]",
-                                  mpd_status_get_kbit_rate(status));
+                       format_bitrate(buffer, sizeof(buffer), status);
+#else
+                       buffer[0] = 0;
 #endif
                }
-#ifndef NCMPC_MINI
        } else {
-               if (options.display_time) {
-                       time_t timep;
-
-                       time(&timep);
-                       strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
-               } else
-#endif
-                       buffer[0] = 0;
+               buffer[0] = 0;
        }
 
        /* display song */
@@ -186,28 +198,19 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                /* scroll if the song name is to long */
 #ifndef NCMPC_MINI
                if (options.scroll && utf8_width(songname) > (unsigned)width) {
-                       char *tmp = strscroll(&p->hscroll, songname,
-                                             options.scroll_sep, width);
-
-                       g_strlcpy(songname, tmp, sizeof(songname));
-                       g_free(tmp);
-
-                       if (p->scroll_source_id == 0)
-                               p->scroll_source_id =
-                                       g_timeout_add(1000,
-                                                     scroll_timer_callback,
-                                                     p);
-               } else if (p->scroll_source_id != 0) {
-                       g_source_remove(p->scroll_source_id);
-                       p->scroll_source_id = 0;
+                       hscroll_set(&p->hscroll, x, 0, width, songname);
+                       hscroll_draw(&p->hscroll);
+               } else {
+                       if (options.scroll)
+                               hscroll_clear(&p->hscroll);
+                       mvwaddstr(w, 0, x, songname);
                }
-#endif
-               //mvwaddnstr(w, 0, x, songname, width);
+#else
                mvwaddstr(w, 0, x, songname);
+#endif
 #ifndef NCMPC_MINI
-       } else if (p->scroll_source_id != 0) {
-               g_source_remove(p->scroll_source_id);
-               p->scroll_source_id = 0;
+       } else if (options.scroll) {
+               hscroll_clear(&p->hscroll);
 #endif
        }
 
@@ -229,11 +232,27 @@ status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
        mvwin(p->window.w, y, x);
 }
 
+static gboolean
+status_bar_clear_message_cb(gpointer data)
+{
+       struct status_bar *p = data;
+       assert(p->message_source_id != 0);
+       p->message_source_id = 0;
+
+       status_bar_clear_message(p);
+       return false;
+}
+
 void
 status_bar_message(struct status_bar *p, const char *msg)
 {
        WINDOW *w = p->window.w;
 
+#ifndef NCMPC_MINI
+       if (options.scroll)
+               hscroll_clear(&p->hscroll);
+#endif
+
        wmove(w, 0, 0);
        wclrtoeol(w);
        colors_use(w, COLOR_STATUS_ALERT);
@@ -242,6 +261,6 @@ status_bar_message(struct status_bar *p, const char *msg)
 
        if (p->message_source_id != 0)
                g_source_remove(p->message_source_id);
-       p->message_source_id = g_timeout_add(options.status_message_time * 1000,
-                                            status_bar_clear_message, p);
+       p->message_source_id = g_timeout_add_seconds(options.status_message_time,
+                                                    status_bar_clear_message_cb, p);
 }