Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
[ncmpc.git] / src / keyboard.c
index da0b5a20e6fd2dcfc6f95666925746591593b277..86f2afe1fe84616dd0828b1ea3c6a747e9e1de10 100644 (file)
 #include "command.h"
 #include "ncmpc.h"
 #include "ncmpc_curses.h"
+#include "screen.h"
 #include "Compiler.h"
 
 #include <glib.h>
 
 #include <unistd.h>
 
-gcc_pure
-static command_t
-translate_key(int key)
+static bool
+ignore_key(int key)
 {
-       if (key == ERR || key == '\0')
-               return CMD_NONE;
-
-#ifdef HAVE_GETMOUSE
-       if (key == KEY_MOUSE)
-               return CMD_MOUSE_EVENT;
-#endif
-
-       return get_key_command(key);
+       return key == ERR || key == '\0';
 }
 
+gcc_pure
 static command_t
-get_keyboard_command(void)
+translate_key(int key)
 {
-       return translate_key(wgetch(stdscr));
+       return get_key_command(key);
 }
 
 static gboolean
@@ -53,12 +46,37 @@ keyboard_event(gcc_unused GIOChannel *source,
               gcc_unused GIOCondition condition,
               gcc_unused gpointer data)
 {
+       int key = wgetch(screen.main_window.w);
+       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;
+
        begin_input_event();
 
-       command_t cmd = get_keyboard_command();
-       if (cmd != CMD_NONE)
-               if (!do_input_event(cmd))
-                       return FALSE;
+       if (!do_input_event(cmd))
+               return FALSE;
 
        end_input_event();
        return TRUE;
@@ -75,9 +93,10 @@ keyboard_init(void)
 void
 keyboard_unread(int key)
 {
-       ungetch(key);
+       if (ignore_key(key))
+               return;
 
-       command_t cmd = get_keyboard_command();
+       command_t cmd = translate_key(key);
        if (cmd != CMD_NONE)
                do_input_event(cmd);
 }