Code

moved struct screen_functions to screen_interface.h
[ncmpc.git] / src / screen.c
index bfba41befec02897ae01897c14fcdd44ae5ad613..d9e7fdbe86ed0510a61ffd7d856e652b2e678a0b 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "screen.h"
+#include "screen_interface.h"
 #include "screen_list.h"
 #include "screen_utils.h"
 #include "config.h"
 #include "utils.h"
 #include "options.h"
 #include "colors.h"
-#include "strfsong.h"
 #include "player_command.h"
 
-#ifndef NCMPC_MINI
-#include "hscroll.h"
-#endif
-
 #include <mpd/client.h>
 
 #include <stdlib.h>
@@ -146,99 +142,6 @@ screen_next_mode(struct mpdclient *c, int offset)
                screen_switch(sf, c);
 }
 
-#ifndef NCMPC_MINI
-static void
-print_hotkey(WINDOW *w, command_t cmd, const char *label)
-{
-       colors_use(w, COLOR_TITLE_BOLD);
-       waddstr(w, get_key_names(cmd, FALSE));
-       colors_use(w, COLOR_TITLE);
-       waddch(w, ':');
-       waddstr(w, label);
-       waddch(w, ' ');
-       waddch(w, ' ');
-}
-#endif
-
-static inline int
-get_volume(const struct mpd_status *status)
-{
-       return status != NULL
-               ? mpd_status_get_volume(status)
-               : MPD_STATUS_NO_VOLUME;
-}
-
-static void
-paint_top_window2(const char *header, struct mpdclient *c)
-{
-       int volume;
-       char flags[5];
-       WINDOW *w = screen.top_window.w;
-       char buf[32];
-
-       if (header[0]) {
-               colors_use(w, COLOR_TITLE_BOLD);
-               mvwaddstr(w, 0, 0, header);
-#ifndef NCMPC_MINI
-       } else {
-#ifdef ENABLE_HELP_SCREEN
-               print_hotkey(w, CMD_SCREEN_HELP, _("Help"));
-#endif
-               print_hotkey(w, CMD_SCREEN_PLAY, _("Playlist"));
-               print_hotkey(w, CMD_SCREEN_FILE, _("Browse"));
-#ifdef ENABLE_ARTIST_SCREEN
-               print_hotkey(w, CMD_SCREEN_ARTIST, _("Artist"));
-#endif
-#ifdef ENABLE_SEARCH_SCREEN
-               print_hotkey(w, CMD_SCREEN_SEARCH, _("Search"));
-#endif
-#ifdef ENABLE_LYRICS_SCREEN
-               print_hotkey(w, CMD_SCREEN_LYRICS, _("Lyrics"));
-#endif
-#ifdef ENABLE_OUTPUTS_SCREEN
-               print_hotkey(w, CMD_SCREEN_OUTPUTS, _("Outputs"));
-#endif
-#endif
-       }
-
-       volume = get_volume(c->status);
-       if (volume == MPD_STATUS_NO_VOLUME)
-               g_snprintf(buf, 32, _("Volume n/a"));
-       else
-               g_snprintf(buf, 32, _("Volume %d%%"), volume);
-
-       colors_use(w, COLOR_TITLE);
-       mvwaddstr(w, 0, screen.top_window.cols - utf8_width(buf), buf);
-
-       flags[0] = 0;
-       if (c->status != NULL) {
-               if (mpd_status_get_repeat(c->status))
-                       g_strlcat(flags, "r", sizeof(flags));
-               if (mpd_status_get_random(c->status))
-                       g_strlcat(flags, "z", sizeof(flags));
-               if (mpd_status_get_single(c->status))
-                       g_strlcat(flags, "s", sizeof(flags));
-               if (mpd_status_get_consume(c->status))
-                       g_strlcat(flags, "c", sizeof(flags));
-               if (mpd_status_get_crossfade(c->status))
-                       g_strlcat(flags, "x", sizeof(flags));
-               if (mpd_status_get_update_id(c->status) != 0)
-                       g_strlcat(flags, "U", sizeof(flags));
-       }
-
-       colors_use(w, COLOR_LINE);
-       mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
-       if (flags[0]) {
-               wmove(w,1,screen.top_window.cols-strlen(flags)-3);
-               waddch(w, '[');
-               colors_use(w, COLOR_LINE_BOLD);
-               waddstr(w, flags);
-               colors_use(w, COLOR_LINE);
-               waddch(w, ']');
-       }
-       wnoutrefresh(w);
-}
-
 static inline int
 volume_length(int volume)
 {
@@ -252,31 +155,9 @@ volume_length(int volume)
 }
 
 static void
-paint_top_window(const char *header, struct mpdclient *c, int full_repaint)
+paint_top_window(const char *header, const struct mpdclient *c)
 {
-       static int prev_volume = -1;
-       static unsigned prev_header_len = -1;
-       WINDOW *w = screen.top_window.w;
-
-       if (prev_header_len != utf8_width(header)) {
-               prev_header_len = utf8_width(header);
-               full_repaint = 1;
-       }
-
-       if (c->status &&
-           volume_length(prev_volume) !=
-           volume_length(mpd_status_get_volume(c->status)))
-               full_repaint = 1;
-
-       if (full_repaint) {
-               wmove(w, 0, 0);
-               wclrtoeol(w);
-       }
-
-       if ((c->status != NULL &&
-            prev_volume != mpd_status_get_volume(c->status)) ||
-           full_repaint)
-               paint_top_window2(header, c);
+       title_bar_paint(&screen.title_bar, header, c->status);
 }
 
 static void
@@ -300,146 +181,6 @@ paint_progress_window(struct mpdclient *c)
                progress_bar_paint(&screen.progress_bar);
 }
 
-static void
-paint_status_window(struct mpdclient *c)
-{
-       WINDOW *w = screen.status_window.w;
-       const struct mpd_status *status = c->status;
-       enum mpd_state state;
-       const struct mpd_song *song = c->song;
-       int elapsedTime = 0;
-#ifdef NCMPC_MINI
-       static char bitrate[1];
-#else
-       char bitrate[16];
-#endif
-       const char *str = NULL;
-       int x = 0;
-
-       if( time(NULL) - screen.status_timestamp <= options.status_message_time )
-               return;
-
-       wmove(w, 0, 0);
-       wclrtoeol(w);
-       colors_use(w, COLOR_STATUS_BOLD);
-
-       state = status == NULL ? MPD_STATE_UNKNOWN
-               : mpd_status_get_state(status);
-
-       switch (state) {
-       case MPD_STATE_PLAY:
-               str = _("Playing:");
-               break;
-       case MPD_STATE_PAUSE:
-               str = _("[Paused]");
-               break;
-       case MPD_STATE_STOP:
-       default:
-               break;
-       }
-
-       if (str) {
-               waddstr(w, str);
-               x += utf8_width(str) + 1;
-       }
-
-       /* create time string */
-       memset(screen.buf, 0, screen.buf_size);
-       if (IS_PLAYING(state) || IS_PAUSED(state)) {
-               int total_time = mpd_status_get_total_time(status);
-               if (total_time > 0) {
-                       /*checks the conf to see whether to display elapsed or remaining time */
-                       if(!strcmp(options.timedisplay_type,"elapsed"))
-                               elapsedTime = mpd_status_get_elapsed_time(c->status);
-                       else if(!strcmp(options.timedisplay_type,"remaining"))
-                               elapsedTime = total_time -
-                                       mpd_status_get_elapsed_time(c->status);
-
-                       if (c->song != NULL &&
-                           seek_id == (int)mpd_song_get_id(c->song))
-                               elapsedTime = seek_target_time;
-
-                       /* 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';
-                       }
-#endif
-
-                       /*write out the time, using hours if time over 60 minutes*/
-                       if (total_time > 3600) {
-                               g_snprintf(screen.buf, screen.buf_size,
-                                          "%s [%i:%02i:%02i/%i:%02i:%02i]",
-                                          bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
-                                          total_time / 3600,
-                                          (total_time % 3600)/60,
-                                          total_time % 60);
-                       } else {
-                               g_snprintf(screen.buf, screen.buf_size,
-                                          "%s [%i:%02i/%i:%02i]",
-                                          bitrate, elapsedTime/60, elapsedTime%60,
-                                          total_time / 60, total_time % 60);
-                       }
-#ifndef NCMPC_MINI
-               } else {
-                       g_snprintf(screen.buf, screen.buf_size,
-                                  " [%d kbps]",
-                                  mpd_status_get_kbit_rate(status));
-#endif
-               }
-#ifndef NCMPC_MINI
-       } else {
-               if (options.display_time) {
-                       time_t timep;
-
-                       time(&timep);
-                       strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
-               }
-#endif
-       }
-
-       /* display song */
-       if (IS_PLAYING(state) || IS_PAUSED(state)) {
-               char songname[MAX_SONGNAME_LENGTH];
-#ifndef NCMPC_MINI
-               int width = COLS - x - utf8_width(screen.buf);
-#endif
-
-               if (song)
-                       strfsong(songname, MAX_SONGNAME_LENGTH,
-                                options.status_format, song);
-               else
-                       songname[0] = '\0';
-
-               colors_use(w, COLOR_STATUS);
-               /* scroll if the song name is to long */
-#ifndef NCMPC_MINI
-               if (options.scroll && utf8_width(songname) > (unsigned)width) {
-                       static  scroll_state_t st = { 0, 0 };
-                       char *tmp = strscroll(songname, options.scroll_sep, width, &st);
-
-                       g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
-                       g_free(tmp);
-               }
-#endif
-               //mvwaddnstr(w, 0, x, songname, width);
-               mvwaddstr(w, 0, x, songname);
-       }
-
-       /* display time string */
-       if (screen.buf[0]) {
-               x = screen.status_window.cols - strlen(screen.buf);
-               colors_use(w, COLOR_STATUS_TIME);
-               mvwaddstr(w, 0, x, screen.buf);
-       }
-
-       wnoutrefresh(w);
-}
-
 void
 screen_exit(void)
 {
@@ -452,10 +193,10 @@ screen_exit(void)
        g_free(screen.buf);
        g_free(screen.findbuf);
 
-       delwin(screen.top_window.w);
+       title_bar_deinit(&screen.title_bar);
        delwin(screen.main_window.w);
-       delwin(screen.progress_bar.window.w);
-       delwin(screen.status_window.w);
+       progress_bar_deinit(&screen.progress_bar);
+       status_bar_deinit(&screen.status_bar);
 }
 
 void
@@ -472,9 +213,7 @@ screen_resize(struct mpdclient *c)
        screen.cols = COLS;
        screen.rows = LINES;
 
-       /* top window */
-       screen.top_window.cols = screen.cols;
-       wresize(screen.top_window.w, 2, screen.cols);
+       title_bar_resize(&screen.title_bar, screen.cols);
 
        /* main window */
        screen.main_window.cols = screen.cols;
@@ -483,16 +222,13 @@ screen_resize(struct mpdclient *c)
        wclear(screen.main_window.w);
 
        /* progress window */
-       screen.progress_bar.window.cols = screen.cols;
-       wresize(screen.progress_bar.window.w, 1, screen.cols);
-       mvwin(screen.progress_bar.window.w, screen.rows-2, 0);
-       progress_bar_resize(&screen.progress_bar);
+       progress_bar_resize(&screen.progress_bar, screen.cols,
+                           screen.rows - 2, 0);
        progress_bar_paint(&screen.progress_bar);
 
        /* status window */
-       screen.status_window.cols = screen.cols;
-       wresize(screen.status_window.w, 1, screen.cols);
-       mvwin(screen.status_window.w, screen.rows-1, 0);
+       status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
+       status_bar_paint(&screen.status_bar, c->status, c->song);
 
        screen.buf_size = screen.cols;
        g_free(screen.buf);
@@ -511,14 +247,7 @@ screen_resize(struct mpdclient *c)
 void
 screen_status_message(const char *msg)
 {
-       WINDOW *w = screen.status_window.w;
-
-       wmove(w, 0, 0);
-       wclrtoeol(w);
-       colors_use(w, COLOR_STATUS_ALERT);
-       waddstr(w, msg);
-       wnoutrefresh(w);
-       screen.status_timestamp = time(NULL);
+       status_bar_message(&screen.status_bar, msg);
 }
 
 void
@@ -551,9 +280,7 @@ screen_init(struct mpdclient *c)
        screen.start_timestamp = time(NULL);
 
        /* create top window */
-       window_init(&screen.top_window, 2, screen.cols, 0, 0);
-       leaveok(screen.top_window.w, TRUE);
-       keypad(screen.top_window.w, TRUE);
+       title_bar_init(&screen.title_bar, screen.cols, 0, 0);
 
        /* create main window */
        window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
@@ -562,27 +289,23 @@ screen_init(struct mpdclient *c)
        keypad(screen.main_window.w, TRUE);
 
        /* create progress window */
-       progress_bar_init(&screen.progress_bar, 1, screen.cols,
+       progress_bar_init(&screen.progress_bar, screen.cols,
                          screen.rows - 2, 0);
-
-       leaveok(screen.progress_bar.window.w, TRUE);
+       progress_bar_paint(&screen.progress_bar);
 
        /* create status window */
-       window_init(&screen.status_window, 1, screen.cols,
-                   screen.rows - 1, 0);
-
-       leaveok(screen.status_window.w, FALSE);
-       keypad(screen.status_window.w, TRUE);
+       status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
+       status_bar_paint(&screen.status_bar, c->status, c->song);
 
 #ifdef ENABLE_COLORS
        if (options.enable_colors) {
                /* set background attributes */
                wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
                wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
-               wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
+               wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
                wbkgd(screen.progress_bar.window.w,
                      COLOR_PAIR(COLOR_PROGRESSBAR));
-               wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
+               wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
                colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
        }
 #endif
@@ -607,14 +330,14 @@ screen_paint(struct mpdclient *c)
 
        /* paint the title/header window */
        if( title )
-               paint_top_window(title, c, 1);
+               paint_top_window(title, c);
        else
-               paint_top_window("", c, 1);
+               paint_top_window("", c);
 
        /* paint the bottom window */
 
        paint_progress_window(c);
-       paint_status_window(c);
+       status_bar_paint(&screen.status_bar, c->status, c->song);
 
        /* paint the main window */
 
@@ -643,17 +366,15 @@ screen_update(struct mpdclient *c)
        static bool single;
        static bool consume;
        static unsigned crossfade;
-       static unsigned dbupdate;
 
        /* print a message if mpd status has changed */
-       if (c->status != NULL) {
+       if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
                if (!initialized) {
                        repeat = mpd_status_get_repeat(c->status);
                        random_enabled = mpd_status_get_random(c->status);
                        single = mpd_status_get_single(c->status);
                        consume = mpd_status_get_consume(c->status);
                        crossfade = mpd_status_get_crossfade(c->status);
-                       dbupdate = mpd_status_get_update_id(c->status);
                        initialized = true;
                }
 
@@ -690,38 +411,35 @@ screen_update(struct mpdclient *c)
                        screen_status_printf(_("Crossfade %d seconds"),
                                             mpd_status_get_crossfade(c->status));
 
-               if (dbupdate != 0 &&
-                   dbupdate != mpd_status_get_update_id(c->status)) {
-                       screen_status_printf(_("Database updated"));
-               }
-
                repeat = mpd_status_get_repeat(c->status);
                random_enabled = mpd_status_get_random(c->status);
                single = mpd_status_get_single(c->status);
                consume = mpd_status_get_consume(c->status);
                crossfade = mpd_status_get_crossfade(c->status);
-               dbupdate = mpd_status_get_update_id(c->status);
        }
 
+       if (c->events & MPD_IDLE_DATABASE)
+               screen_status_printf(_("Database updated"));
+
        /* update title/header window */
        if (welcome && options.welcome_screen_list &&
            time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
-               paint_top_window("", c, 0);
+               paint_top_window("", c);
        else
 #endif
        if (mode_fn->get_title != NULL) {
-               paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
+               paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
 #ifndef NCMPC_MINI
                welcome = FALSE;
 #endif
        } else
-               paint_top_window("", c, 0);
+               paint_top_window("", c);
 
        /* update progress window */
        paint_progress_window(c);
 
        /* update status window */
-       paint_status_window(c);
+       status_bar_paint(&screen.status_bar, c->status, c->song);
 
        /* update the main window */
        if (mode_fn->update != NULL)
@@ -747,7 +465,7 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
        /* retrieve the mouse event from ncurses */
        getmouse(&event);
        /* calculate the selected row in the list window */
-       *row = event.y - screen.top_window.rows;
+       *row = event.y - screen.title_bar.window.rows;
        /* copy button state bits */
        *bstate = event.bstate;
        /* if button 2 was pressed switch screen */