From 63fc23f1c83f52eb47b5511e97bb1fe7cf7d82d5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 2 Aug 2017 09:51:31 +0200 Subject: [PATCH] screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT --- src/command.h | 1 - src/keyboard.c | 24 ++++++++++++++---- src/main.c | 10 ++++++++ src/ncmpc.h | 9 +++++++ src/screen.c | 55 ++++++++++++++++++++++-------------------- src/screen.h | 8 +++--- src/screen_artist.c | 18 ++++++++++++++ src/screen_browser.c | 22 ++++++----------- src/screen_browser.h | 10 ++++++-- src/screen_file.c | 17 +++++++++++++ src/screen_interface.h | 10 ++++++++ src/screen_queue.c | 16 +++++------- src/screen_search.c | 18 ++++++++++++++ 13 files changed, 157 insertions(+), 61 deletions(-) diff --git a/src/command.h b/src/command.h index 1d6b6ba..aa794c6 100644 --- a/src/command.h +++ b/src/command.h @@ -83,7 +83,6 @@ typedef enum { CMD_LIST_SCROLL_DOWN_LINE, CMD_LIST_SCROLL_UP_HALF, CMD_LIST_SCROLL_DOWN_HALF, - CMD_MOUSE_EVENT, CMD_SCREEN_UPDATE, CMD_SCREEN_PREVIOUS, CMD_SCREEN_NEXT, diff --git a/src/keyboard.c b/src/keyboard.c index e2234f3..86f2afe 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -38,11 +38,6 @@ gcc_pure static command_t translate_key(int key) { -#ifdef HAVE_GETMOUSE - if (key == KEY_MOUSE) - return CMD_MOUSE_EVENT; -#endif - return get_key_command(key); } @@ -55,6 +50,25 @@ keyboard_event(gcc_unused GIOChannel *source, if (ignore_key(key)) return true; +#ifdef HAVE_GETMOUSE + if (key == KEY_MOUSE) { + MEVENT event; + + /* retrieve the mouse event from curses */ +#ifdef PDCURSES + nc_getmouse(&event); +#else + getmouse(&event); +#endif + + begin_input_event(); + do_mouse_event(event.x, event.y, event.bstate); + end_input_event(); + + return true; + } +#endif + command_t cmd = translate_key(key); if (cmd == CMD_NONE) return true; diff --git a/src/main.c b/src/main.c index 11e46e4..eccb5a0 100644 --- a/src/main.c +++ b/src/main.c @@ -280,6 +280,16 @@ do_input_event(command_t cmd) return true; } +#ifdef HAVE_GETMOUSE + +void +do_mouse_event(int x, int y, mmask_t bstate) +{ + screen_mouse(mpd, x, y, bstate); +} + +#endif + #ifndef NCMPC_MINI /** * Check the configured key bindings for errors, and display a status diff --git a/src/ncmpc.h b/src/ncmpc.h index 2ca79ad..2cc778f 100644 --- a/src/ncmpc.h +++ b/src/ncmpc.h @@ -22,6 +22,10 @@ #include "command.h" +#ifdef HAVE_GETMOUSE +#include "ncmpc_curses.h" +#endif + #include void begin_input_event(void); @@ -33,4 +37,9 @@ void end_input_event(void); bool do_input_event(command_t cmd); +#ifdef HAVE_GETMOUSE +void +do_mouse_event(int x, int y, mmask_t bstate); +#endif + #endif /* NCMPC_H */ diff --git a/src/screen.c b/src/screen.c index 6d2610a..abd241d 100644 --- a/src/screen.c +++ b/src/screen.c @@ -206,32 +206,6 @@ screen_update(struct mpdclient *c) 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) { @@ -325,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 diff --git a/src/screen.h b/src/screen.h index 8a91d46..3d7bc39 100644 --- a/src/screen.h +++ b/src/screen.h @@ -75,6 +75,11 @@ screen_paint(struct mpdclient *c, bool main_dirty); void screen_update(struct mpdclient *c); void screen_cmd(struct mpdclient *c, command_t cmd); +#ifdef HAVE_GETMOUSE +bool +screen_mouse(struct mpdclient *c, int x, int y, mmask_t bstate); +#endif + void screen_switch(const struct screen_functions *sf, struct mpdclient *c); void @@ -86,7 +91,4 @@ screen_is_visible(const struct screen_functions *sf) return sf == screen.current_page; } -int -screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row); - #endif diff --git a/src/screen_artist.c b/src/screen_artist.c index 2190cbb..672c93f 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -740,6 +740,21 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd) return false; } +#ifdef HAVE_GETMOUSE +static bool +screen_artist_mouse(struct mpdclient *c, int x, int y, mmask_t bstate) +{ + if (browser_mouse(&browser, c, x, y, bstate)) { + if (screen_is_visible(&screen_artist)) + screen_artist_paint(); + + return true; + } + + return false; +} +#endif + const struct screen_functions screen_artist = { .init = screen_artist_init, .exit = screen_artist_quit, @@ -748,5 +763,8 @@ const struct screen_functions screen_artist = { .paint = screen_artist_paint, .update = screen_artist_update, .cmd = screen_artist_cmd, +#ifdef HAVE_GETMOUSE + .mouse = screen_artist_mouse, +#endif .get_title = screen_artist_get_title, }; diff --git a/src/screen_browser.c b/src/screen_browser.c index c38ae9e..e0c37cb 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -351,16 +351,15 @@ browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c) } #ifdef HAVE_GETMOUSE -static int -browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c) + +bool +browser_mouse(struct screen_browser *browser, + struct mpdclient *c, gcc_unused int x, int row, mmask_t bstate) { - int row; unsigned prev_selected = browser->lw->selected; - unsigned long bstate; - if (screen_get_mouse_event(c, &bstate, &row) || - list_window_mouse(browser->lw, bstate, row)) - return 1; + if (list_window_mouse(browser->lw, bstate, row)) + return true; list_window_set_cursor(browser->lw, browser->lw->start + row); @@ -372,8 +371,9 @@ browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c) browser_handle_select(browser, c); } - return 1; + return true; } + #endif static void @@ -408,12 +408,6 @@ browser_cmd(struct screen_browser *browser, screen_browser_paint_callback, browser); return true; -#ifdef HAVE_GETMOUSE - case CMD_MOUSE_EVENT: - browser_handle_mouse_event(browser, c); - return true; -#endif - #ifdef ENABLE_SONG_SCREEN case CMD_SCREEN_SONG: song = browser_get_selected_song(browser); diff --git a/src/screen_browser.h b/src/screen_browser.h index e972bd5..82d6ffa 100644 --- a/src/screen_browser.h +++ b/src/screen_browser.h @@ -62,11 +62,17 @@ screen_browser_paint_directory(WINDOW *w, unsigned width, void screen_browser_paint(const struct screen_browser *browser); -struct filelist_entry * -browser_get_selected_entry(const struct screen_browser *browser); +#ifdef HAVE_GETMOUSE +bool +browser_mouse(struct screen_browser *browser, + struct mpdclient *c, int x, int y, mmask_t bstate); +#endif bool browser_cmd(struct screen_browser *browser, struct mpdclient *c, command_t cmd); +struct filelist_entry * +browser_get_selected_entry(const struct screen_browser *browser); + #endif diff --git a/src/screen_file.c b/src/screen_file.c index b23a1c8..d55684c 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -380,6 +380,20 @@ screen_file_cmd(struct mpdclient *c, command_t cmd) return false; } +#ifdef HAVE_GETMOUSE +static bool +screen_file_mouse(struct mpdclient *c, int x, int y, mmask_t bstate) +{ + if (browser_mouse(&browser, c, x, y, bstate)) { + if (screen_is_visible(&screen_browse)) + screen_file_paint(); + return true; + } + + return false; +} +#endif + const struct screen_functions screen_browse = { .init = screen_file_init, .exit = screen_file_exit, @@ -388,6 +402,9 @@ const struct screen_functions screen_browse = { .paint = screen_file_paint, .update = screen_file_update, .cmd = screen_file_cmd, +#ifdef HAVE_GETMOUSE + .mouse = screen_file_mouse, +#endif .get_title = screen_file_get_title, }; diff --git a/src/screen_interface.h b/src/screen_interface.h index 8296242..e11813a 100644 --- a/src/screen_interface.h +++ b/src/screen_interface.h @@ -45,6 +45,16 @@ struct screen_functions { */ bool (*cmd)(struct mpdclient *c, command_t cmd); +#ifdef HAVE_GETMOUSE + /** + * Handle a mouse event. + * + * @return true if the event was handled (and should not be + * handled by the ncmpc core) + */ + bool (*mouse)(struct mpdclient *c, int x, int y, mmask_t bstate); +#endif + const char *(*get_title)(char *s, size_t size); }; diff --git a/src/screen_queue.c b/src/screen_queue.c index dd27df0..5e43b03 100644 --- a/src/screen_queue.c +++ b/src/screen_queue.c @@ -443,12 +443,10 @@ screen_queue_update(struct mpdclient *c) #ifdef HAVE_GETMOUSE static bool -handle_mouse_event(struct mpdclient *c) +screen_queue_mouse(struct mpdclient *c, gcc_unused int x, int row, + mmask_t bstate) { - unsigned long bstate; - int row; - if (screen_get_mouse_event(c, &bstate, &row) || - list_window_mouse(lw, bstate, row)) { + if (list_window_mouse(lw, bstate, row)) { screen_queue_paint(); return true; } @@ -539,11 +537,6 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd) screen_queue_paint(); return true; -#ifdef HAVE_GETMOUSE - case CMD_MOUSE_EVENT: - return handle_mouse_event(c); -#endif - #ifdef ENABLE_SONG_SCREEN case CMD_SCREEN_SONG: if (screen_queue_selected_song() != NULL) { @@ -692,5 +685,8 @@ const struct screen_functions screen_queue = { .paint = screen_queue_paint, .update = screen_queue_update, .cmd = screen_queue_cmd, +#ifdef HAVE_GETMOUSE + .mouse = screen_queue_mouse, +#endif .get_title = screen_queue_title, }; diff --git a/src/screen_search.c b/src/screen_search.c index 30be533..4ba3c82 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -462,6 +462,21 @@ screen_search_cmd(struct mpdclient *c, command_t cmd) return false; } +#ifdef HAVE_GETMOUSE +static bool +screen_search_mouse(struct mpdclient *c, int x, int y, mmask_t bstate) +{ + if (browser_mouse(&browser, c, x, y, bstate)) { + if (screen_is_visible(&screen_search)) + screen_search_paint(); + + return true; + } + + return false; +} +#endif + const struct screen_functions screen_search = { .init = screen_search_init, .exit = screen_search_quit, @@ -470,5 +485,8 @@ const struct screen_functions screen_search = { .paint = screen_search_paint, .update = screen_search_update, .cmd = screen_search_cmd, +#ifdef HAVE_GETMOUSE + .mouse = screen_search_mouse, +#endif .get_title = screen_search_get_title, }; -- 2.30.2