Code

screen: move code to paint_top_window()
[ncmpc.git] / src / screen.c
index 4abb35a429e0af46b3a45bfdc67bb96f9917bea1..1f7b7a6ef69f7a028a837da5442699c41d51f5bc 100644 (file)
@@ -1,27 +1,27 @@
 /* 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
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "screen.h"
 #include "screen_interface.h"
 #include "screen_list.h"
 #include "screen_utils.h"
-#include "screen_message.h"
+#include "screen_status.h"
 #include "config.h"
 #include "i18n.h"
 #include "charset.h"
@@ -39,6 +39,7 @@
 #include "screen_keydef.h"
 #include "screen_lyrics.h"
 #include "screen_outputs.h"
+#include "screen_chat.h"
 
 #include <mpd/client.h>
 
@@ -131,43 +132,48 @@ static void
 screen_next_mode(struct mpdclient *c, int offset)
 {
        int max = g_strv_length(options.screen_list);
-       int current, next;
-       const struct screen_functions *sf;
 
        /* find current screen */
-       current = find_configured_screen(screen_get_name(mode_fn));
-       next = current + offset;
+       int current = find_configured_screen(screen_get_name(mode_fn));
+       int next = current + offset;
        if (next<0)
                next = max-1;
        else if (next>=max)
                next = 0;
 
-       sf = screen_lookup_name(options.screen_list[next]);
+       const struct screen_functions *sf =
+               screen_lookup_name(options.screen_list[next]);
        if (sf != NULL)
                screen_switch(sf, c);
 }
 
 static void
-paint_top_window(const char *header, const struct mpdclient *c)
+paint_top_window(const struct mpdclient *c)
 {
-       title_bar_paint(&screen.title_bar, header, c->status);
+       const char *title =
+#ifndef NCMPC_MINI
+               screen.welcome_source_id == 0 &&
+#endif
+               mode_fn->get_title != NULL
+               ? mode_fn->get_title(screen.buf, screen.buf_size)
+               : "";
+       assert(title != NULL);
+
+       title_bar_paint(&screen.title_bar, title, c->status);
 }
 
 static void
 update_progress_window(struct mpdclient *c, bool repaint)
 {
-       unsigned elapsed, duration;
-
-       if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
+       unsigned elapsed;
+       if (c->status == NULL)
+               elapsed = 0;
+       else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
                elapsed = seek_target_time;
-       else if (c->status != NULL)
-               elapsed = mpd_status_get_elapsed_time(c->status);
        else
-               elapsed = 0;
+               elapsed = mpd_status_get_elapsed_time(c->status);
 
-       duration = c->status != NULL &&
-               (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
-                mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
+       unsigned duration = mpdclient_is_playing(c)
                ? mpd_status_get_total_time(c->status)
                : 0;
 
@@ -204,11 +210,14 @@ screen_resize(struct mpdclient *c)
 {
        if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
                screen_exit();
-               fprintf(stderr, "%s", _("Error: Screen too small"));
+               fprintf(stderr, "%s\n", _("Error: Screen too small"));
                exit(EXIT_FAILURE);
        }
-
+#ifdef PDCURSES
+       resize_term(LINES, COLS);
+#else 
        resizeterm(LINES, COLS);
+#endif
 
        screen.cols = COLS;
        screen.rows = LINES;
@@ -219,16 +228,13 @@ screen_resize(struct mpdclient *c)
        screen.main_window.cols = screen.cols;
        screen.main_window.rows = screen.rows-4;
        wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
-       wclear(screen.main_window.w);
 
        /* progress window */
        progress_bar_resize(&screen.progress_bar, screen.cols,
                            screen.rows - 2, 0);
-       progress_bar_paint(&screen.progress_bar);
 
        /* status window */
        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);
@@ -250,14 +256,9 @@ welcome_timer_callback(gpointer data)
 {
        struct mpdclient *c = data;
 
-#ifndef NCMPC_MINI
        screen.welcome_source_id = 0;
-#endif
 
-       paint_top_window(mode_fn->get_title != NULL
-                        ? mode_fn->get_title(screen.buf, screen.buf_size)
-                        : "",
-                        c);
+       paint_top_window(c);
        doupdate();
 
        return false;
@@ -282,8 +283,8 @@ screen_init(struct mpdclient *c)
 #ifndef NCMPC_MINI
        if (options.welcome_screen_list)
                screen.welcome_source_id =
-                       g_timeout_add(SCREEN_WELCOME_TIME * 1000,
-                                     welcome_timer_callback, c);
+                       g_timeout_add_seconds(SCREEN_WELCOME_TIME,
+                                             welcome_timer_callback, c);
 #endif
 
        /* create top window */
@@ -292,17 +293,17 @@ screen_init(struct mpdclient *c)
        /* create main window */
        window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
 
-       //  leaveok(screen.main_window.w, TRUE); temporary disabled
+       if (!options.hardware_cursor)
+               leaveok(screen.main_window.w, TRUE);
+
        keypad(screen.main_window.w, TRUE);
 
        /* create progress window */
        progress_bar_init(&screen.progress_bar, screen.cols,
                          screen.rows - 2, 0);
-       progress_bar_paint(&screen.progress_bar);
 
        /* create status window */
        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) {
@@ -317,8 +318,6 @@ screen_init(struct mpdclient *c)
        }
 #endif
 
-       doupdate();
-
        /* initialize screens */
        screen_list_init(screen.main_window.w,
                         screen.main_window.cols, screen.main_window.rows);
@@ -327,19 +326,11 @@ screen_init(struct mpdclient *c)
                mode_fn->open(c);
 }
 
-void
-screen_paint(struct mpdclient *c)
+static void
+screen_refresh(struct mpdclient *c, bool main_dirty)
 {
-       const char *title = NULL;
-
-       if (mode_fn->get_title != NULL)
-               title = mode_fn->get_title(screen.buf, screen.buf_size);
-
-       /* paint the title/header window */
-       if( title )
-               paint_top_window(title, c);
-       else
-               paint_top_window("", c);
+       /* update title/header window */
+       paint_top_window(c);
 
        /* paint the bottom window */
 
@@ -348,9 +339,11 @@ screen_paint(struct mpdclient *c)
 
        /* paint the main window */
 
-       wclear(screen.main_window.w);
-       if (mode_fn->paint != NULL)
-               mode_fn->paint();
+       if (main_dirty) {
+               wclear(screen.main_window.w);
+               if (mode_fn->paint != NULL)
+                       mode_fn->paint();
+       }
 
        /* move the cursor to the origin */
 
@@ -363,6 +356,12 @@ screen_paint(struct mpdclient *c)
        doupdate();
 }
 
+void
+screen_paint(struct mpdclient *c)
+{
+       screen_refresh(c, true);
+}
+
 void
 screen_update(struct mpdclient *c)
 {
@@ -430,36 +429,13 @@ screen_update(struct mpdclient *c)
            mpdclient_is_connected(c))
                screen_status_printf(_("Database updated"));
        was_connected = mpdclient_is_connected(c);
-
-       /* update title/header window */
-       if (screen.welcome_source_id != 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);
-       } else
-               paint_top_window("", c);
-
-       /* update progress window */
-       update_progress_window(c, false);
-
-       /* update status window */
-       status_bar_paint(&screen.status_bar, c->status, c->song);
 
        /* update the main window */
        if (mode_fn->update != NULL)
                mode_fn->update(c);
 
-       /* move the cursor to the origin */
-
-       if (!options.hardware_cursor)
-               wmove(screen.main_window.w, 0, 0);
-
-       wnoutrefresh(screen.main_window.w);
-
-       /* tell curses to update */
-       doupdate();
+       screen_refresh(c, false);
 }
 
 #ifdef HAVE_GETMOUSE
@@ -468,8 +444,12 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
 {
        MEVENT event;
 
-       /* retrieve the mouse event from ncurses */
+       /* retrieve the mouse event from curses */
+#ifdef PDCURSES
+       nc_getmouse(&event);
+#else
        getmouse(&event);
+#endif
        /* calculate the selected row in the list window */
        *row = event.y - screen.title_bar.window.rows;
        /* copy button state bits */
@@ -562,10 +542,15 @@ screen_cmd(struct mpdclient *c, command_t cmd)
        case CMD_SCREEN_OUTPUTS:
                screen_switch(&screen_outputs, c);
                break;
+#endif
+#ifdef ENABLE_CHAT_SCREEN
+       case CMD_SCREEN_CHAT:
+               screen_switch(&screen_chat, c);
+               break;
+#endif
        case CMD_SCREEN_SWAP:
                screen_swap(c, NULL);
                break;
-#endif
 
        default:
                break;