Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
[ncmpc.git] / src / screen.c
index 781b68f22eb9e99c2d06b2590dce1990548041a6..abd241d16e14e84981ed686fe7e12d3a99442627 100644 (file)
 #include "screen.h"
 #include "screen_interface.h"
 #include "screen_list.h"
-#include "screen_utils.h"
 #include "screen_status.h"
 #include "config.h"
 #include "i18n.h"
 #include "charset.h"
 #include "mpdclient.h"
-#include "utils.h"
 #include "options.h"
-#include "colors.h"
 #include "player_command.h"
 #include "screen_help.h"
 #include "screen_queue.h"
 #include <unistd.h>
 #include <string.h>
 #include <time.h>
-#include <locale.h>
-
-#ifndef NCMPC_MINI
-/** welcome message time [s] */
-static const GTime SCREEN_WELCOME_TIME = 10;
-#endif
-
-/* minimum window size */
-static const int SCREEN_MIN_COLS = 14;
-static const int SCREEN_MIN_ROWS = 5;
 
 /* screens */
 
-struct screen screen;
-static const struct screen_functions *mode_fn = &screen_queue;
-static const struct screen_functions *mode_fn_prev = &screen_queue;
+struct screen screen = {
+       .current_page = &screen_queue,
+};
 
-gboolean
-screen_is_visible(const struct screen_functions *sf)
-{
-       return sf == mode_fn;
-}
+static const struct screen_functions *mode_fn_prev = &screen_queue;
 
 void
 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
 {
        assert(sf != NULL);
 
-       if (sf == mode_fn)
+       if (sf == screen.current_page)
                return;
 
-       mode_fn_prev = mode_fn;
+       mode_fn_prev = screen.current_page;
 
        /* close the old mode */
-       if (mode_fn->close != NULL)
-               mode_fn->close();
+       if (screen.current_page->close != NULL)
+               screen.current_page->close();
 
        /* get functions for the new mode */
-       mode_fn = sf;
+       screen.current_page = sf;
 
        /* open the new mode */
-       if (mode_fn->open != NULL)
-               mode_fn->open(c);
+       if (sf->open != NULL)
+               sf->open(c);
 
-       screen_paint(c);
+       screen_paint(c, true);
 }
 
 void
@@ -134,7 +117,7 @@ screen_next_mode(struct mpdclient *c, int offset)
        int max = g_strv_length(options.screen_list);
 
        /* find current screen */
-       int current = find_configured_screen(screen_get_name(mode_fn));
+       int current = find_configured_screen(screen_get_name(screen.current_page));
        int next = current + offset;
        if (next<0)
                next = max-1;
@@ -147,223 +130,6 @@ screen_next_mode(struct mpdclient *c, int offset)
                screen_switch(sf, c);
 }
 
-static void
-paint_top_window(const char *header, const struct mpdclient *c)
-{
-       title_bar_paint(&screen.title_bar, header, c->status);
-}
-
-static void
-update_progress_window(struct mpdclient *c, bool repaint)
-{
-       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
-               elapsed = mpd_status_get_elapsed_time(c->status);
-
-       unsigned duration = mpdclient_is_playing(c)
-               ? mpd_status_get_total_time(c->status)
-               : 0;
-
-       if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
-           repaint)
-               progress_bar_paint(&screen.progress_bar);
-}
-
-void
-screen_exit(void)
-{
-       if (mode_fn->close != NULL)
-               mode_fn->close();
-
-       screen_list_exit();
-
-       string_list_free(screen.find_history);
-       g_free(screen.buf);
-       g_free(screen.findbuf);
-
-       title_bar_deinit(&screen.title_bar);
-       delwin(screen.main_window.w);
-       progress_bar_deinit(&screen.progress_bar);
-       status_bar_deinit(&screen.status_bar);
-
-#ifndef NCMPC_MINI
-       if (screen.welcome_source_id != 0)
-               g_source_remove(screen.welcome_source_id);
-#endif
-}
-
-void
-screen_resize(struct mpdclient *c)
-{
-       if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
-               screen_exit();
-               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;
-
-       title_bar_resize(&screen.title_bar, screen.cols);
-
-       /* main window */
-       screen.main_window.cols = screen.cols;
-       screen.main_window.rows = screen.rows-4;
-       wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
-
-       /* progress window */
-       progress_bar_resize(&screen.progress_bar, screen.cols,
-                           screen.rows - 2, 0);
-
-       /* status window */
-       status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
-
-       screen.buf_size = screen.cols;
-       g_free(screen.buf);
-       screen.buf = g_malloc(screen.cols);
-
-       /* resize all screens */
-       screen_list_resize(screen.main_window.cols, screen.main_window.rows);
-
-       /* ? - without this the cursor becomes visible with aterm & Eterm */
-       curs_set(1);
-       curs_set(0);
-
-       screen_paint(c);
-}
-
-#ifndef NCMPC_MINI
-static gboolean
-welcome_timer_callback(gpointer data)
-{
-       struct mpdclient *c = data;
-
-       screen.welcome_source_id = 0;
-
-       paint_top_window(mode_fn->get_title != NULL
-                        ? mode_fn->get_title(screen.buf, screen.buf_size)
-                        : "",
-                        c);
-       doupdate();
-
-       return false;
-}
-#endif
-
-void
-screen_init(struct mpdclient *c)
-{
-       if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
-               fprintf(stderr, "%s\n", _("Error: Screen too small"));
-               exit(EXIT_FAILURE);
-       }
-
-       screen.cols = COLS;
-       screen.rows = LINES;
-
-       screen.buf  = g_malloc(screen.cols);
-       screen.buf_size = screen.cols;
-       screen.findbuf = NULL;
-
-#ifndef NCMPC_MINI
-       if (options.welcome_screen_list)
-               screen.welcome_source_id =
-                       g_timeout_add_seconds(SCREEN_WELCOME_TIME,
-                                             welcome_timer_callback, c);
-#endif
-
-       /* create top window */
-       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);
-
-       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);
-
-       /* create status window */
-       status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
-
-#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.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
-               wbkgd(screen.progress_bar.window.w,
-                     COLOR_PAIR(COLOR_PROGRESSBAR));
-               wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
-               colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
-       }
-#endif
-
-       /* initialize screens */
-       screen_list_init(screen.main_window.w,
-                        screen.main_window.cols, screen.main_window.rows);
-
-       if (mode_fn->open != NULL)
-               mode_fn->open(c);
-}
-
-static void
-screen_refresh(struct mpdclient *c, bool main_dirty)
-{
-       /* update title/header window */
-       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);
-       paint_top_window(title, c);
-
-       /* paint the bottom window */
-
-       update_progress_window(c, true);
-       status_bar_paint(&screen.status_bar, c->status, c->song);
-
-       /* paint the main window */
-
-       if (main_dirty) {
-               wclear(screen.main_window.w);
-               if (mode_fn->paint != NULL)
-                       mode_fn->paint();
-       }
-
-       /* 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();
-}
-
-void
-screen_paint(struct mpdclient *c)
-{
-       screen_refresh(c, true);
-}
-
 void
 screen_update(struct mpdclient *c)
 {
@@ -434,38 +200,12 @@ screen_update(struct mpdclient *c)
 #endif
 
        /* update the main window */
-       if (mode_fn->update != NULL)
-               mode_fn->update(c);
+       if (screen.current_page->update != NULL)
+               screen.current_page->update(c);
 
-       screen_refresh(c, false);
+       screen_paint(c, false);
 }
 
-#ifdef HAVE_GETMOUSE
-int
-screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
-{
-       MEVENT event;
-
-       /* 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 */
-       *bstate = event.bstate;
-       /* if button 2 was pressed switch screen */
-       if (event.bstate & BUTTON2_CLICKED) {
-               screen_cmd(c, CMD_SCREEN_NEXT);
-               return 1;
-       }
-
-       return 0;
-}
-#endif
-
 void
 screen_cmd(struct mpdclient *c, command_t cmd)
 {
@@ -476,7 +216,8 @@ screen_cmd(struct mpdclient *c, command_t cmd)
        }
 #endif
 
-       if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
+       if (screen.current_page->cmd != NULL &&
+           screen.current_page->cmd(c, cmd))
                return;
 
        if (handle_player_command(c, cmd))
@@ -496,7 +237,7 @@ screen_cmd(struct mpdclient *c, command_t cmd)
                                     _("Auto center mode: Off"));
                break;
        case CMD_SCREEN_UPDATE:
-               screen_paint(c);
+               screen_paint(c, true);
                break;
        case CMD_SCREEN_PREVIOUS:
                screen_next_mode(c, -1);
@@ -558,3 +299,32 @@ screen_cmd(struct mpdclient *c, command_t cmd)
                break;
        }
 }
+
+#ifdef HAVE_GETMOUSE
+
+static bool
+screen_current_page_mouse(struct mpdclient *c, int x, int y, mmask_t bstate)
+{
+       if (screen.current_page->mouse == NULL)
+               return false;
+
+       y -= screen.title_bar.window.rows;
+       return screen.current_page->mouse(c, x, y, bstate);
+}
+
+bool
+screen_mouse(struct mpdclient *c, int x, int y, mmask_t bstate)
+{
+       if (screen_current_page_mouse(c, x, y, bstate))
+               return true;
+
+       /* if button 2 was pressed switch screen */
+       if (bstate & BUTTON2_CLICKED) {
+               screen_cmd(c, CMD_SCREEN_NEXT);
+               return true;
+       }
+
+       return false;
+}
+
+#endif