Code

screen_queue: move the completion_callback_data_t type down
[ncmpc.git] / src / keyboard.c
index 226306cf7543fc1823fb167b115baa2ad54f7c03..e2234f3cee8a7e2373cce1d9af6da82b385f1164 100644 (file)
 #include "keyboard.h"
 #include "command.h"
 #include "ncmpc.h"
+#include "ncmpc_curses.h"
+#include "screen.h"
 #include "Compiler.h"
 
 #include <glib.h>
 
 #include <unistd.h>
 
+static bool
+ignore_key(int key)
+{
+       return key == ERR || key == '\0';
+}
+
+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);
+}
+
 static gboolean
 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;
+
+       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) != 0)
-                       return FALSE;
+       if (!do_input_event(cmd))
+               return FALSE;
 
        end_input_event();
        return TRUE;
@@ -49,3 +75,14 @@ keyboard_init(void)
        g_io_add_watch(channel, G_IO_IN, keyboard_event, NULL);
        g_io_channel_unref(channel);
 }
+
+void
+keyboard_unread(int key)
+{
+       if (ignore_key(key))
+               return;
+
+       command_t cmd = translate_key(key);
+       if (cmd != CMD_NONE)
+               do_input_event(cmd);
+}