Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
authorMax Kellermann <max.kellermann@gmail.com>
Wed, 2 Aug 2017 07:51:31 +0000 (09:51 +0200)
committerMax Kellermann <max.kellermann@gmail.com>
Wed, 2 Aug 2017 07:51:31 +0000 (09:51 +0200)
13 files changed:
src/command.h
src/keyboard.c
src/main.c
src/ncmpc.h
src/screen.c
src/screen.h
src/screen_artist.c
src/screen_browser.c
src/screen_browser.h
src/screen_file.c
src/screen_interface.h
src/screen_queue.c
src/screen_search.c

index 1d6b6babcf8721b02ed1f97a0017d368bab2ba18..aa794c64cded5415e9c5cd521cb5c3eaddf6b00d 100644 (file)
@@ -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,
index e2234f3cee8a7e2373cce1d9af6da82b385f1164..86f2afe1fe84616dd0828b1ea3c6a747e9e1de10 100644 (file)
@@ -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;
index 11e46e4a698bef6c81717468ed20ea72cc446e44..eccb5a004c8a114f98966270447278dcbdbf439d 100644 (file)
@@ -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
index 2ca79ad55b2f1e07f024743aeae312a4c20c4e4b..2cc778f416890ccd0778e0167ab30d7954a87feb 100644 (file)
 
 #include "command.h"
 
+#ifdef HAVE_GETMOUSE
+#include "ncmpc_curses.h"
+#endif
+
 #include <stdbool.h>
 
 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 */
index 6d2610ad1770e461bd72a503ada7a016407bec3e..abd241d16e14e84981ed686fe7e12d3a99442627 100644 (file)
@@ -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
index 8a91d46279254a7205a16af18d899196de787d2e..3d7bc3922f0e8ac2d45565d65f5eceb3bb933fa1 100644 (file)
@@ -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
index 2190cbbff54a7f3b4aed0068a47f9cb7d4c52df0..672c93f33c366f0b35f206d86e369911bf07dd19 100644 (file)
@@ -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,
 };
index c38ae9ea81490f539723a4e7e6dd2e8da0fc76d8..e0c37cbc2e0a48a04e1a66ee3be030b6aad6786a 100644 (file)
@@ -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);
index e972bd5855d64ee9f4aa8e03167dbbf621009406..82d6ffa4afe78bc9ade37321790e4d1eada094a5 100644 (file)
@@ -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
index b23a1c84a1363126d3a55aeb2d119997407500da..d55684c3d06cd86d6641e236a9026350516af0d1 100644 (file)
@@ -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,
 };
 
index 82962420f8c064ce11a90ae3e14e6e7b2910d31d..e11813a10f88ca1395ae773d63d9164faf2758da 100644 (file)
@@ -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);
 };
 
index dd27df04cce48fd6c1fd5a6ea9dc581aed72021c..5e43b03a490767f4fe5d16ff9ed32d3aaf25410e 100644 (file)
@@ -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,
 };
index 30be533baf66d63875173637ce0f0a5858e8035a..4ba3c826b9ad52669f5b8e4515d92354b73a3c9a 100644 (file)
@@ -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,
 };