Code

keyboard: implement keyboard_unread() without ungetch()
[ncmpc.git] / src / keyboard.c
index 5bd43e573ab3e173b26542ccbf676418068cf778..d990e3691e7854d011672527062f9d51fc7ede70 100644 (file)
 #include "keyboard.h"
 #include "command.h"
 #include "ncmpc.h"
+#include "ncmpc_curses.h"
 #include "Compiler.h"
 
 #include <glib.h>
 
 #include <unistd.h>
 
+gcc_pure
+static command_t
+translate_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);
+}
+
+static command_t
+get_keyboard_command(void)
+{
+       return translate_key(wgetch(stdscr));
+}
+
 static gboolean
 keyboard_event(gcc_unused GIOChannel *source,
               gcc_unused GIOCondition condition,
@@ -49,3 +71,11 @@ keyboard_init(void)
        g_io_add_watch(channel, G_IO_IN, keyboard_event, NULL);
        g_io_channel_unref(channel);
 }
+
+void
+keyboard_unread(int key)
+{
+       command_t cmd = translate_key(key);
+       if (cmd != CMD_NONE)
+               do_input_event(cmd);
+}