Code

keyboard: add keyboard_unread()
[ncmpc.git] / src / keyboard.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "keyboard.h"
21 #include "command.h"
22 #include "ncmpc.h"
23 #include "ncmpc_curses.h"
24 #include "Compiler.h"
26 #include <glib.h>
28 #include <unistd.h>
30 static command_t
31 get_keyboard_command(void)
32 {
33         int key = wgetch(stdscr);
34         if (key == ERR || key == '\0')
35                 return CMD_NONE;
37 #ifdef HAVE_GETMOUSE
38         if (key == KEY_MOUSE)
39                 return CMD_MOUSE_EVENT;
40 #endif
42         return get_key_command(key);
43 }
45 static gboolean
46 keyboard_event(gcc_unused GIOChannel *source,
47                gcc_unused GIOCondition condition,
48                gcc_unused gpointer data)
49 {
50         begin_input_event();
52         command_t cmd = get_keyboard_command();
53         if (cmd != CMD_NONE)
54                 if (!do_input_event(cmd))
55                         return FALSE;
57         end_input_event();
58         return TRUE;
59 }
61 void
62 keyboard_init(void)
63 {
64         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
65         g_io_add_watch(channel, G_IO_IN, keyboard_event, NULL);
66         g_io_channel_unref(channel);
67 }
69 void
70 keyboard_unread(int key)
71 {
72         ungetch(key);
74         command_t cmd = get_keyboard_command();
75         if (cmd != CMD_NONE)
76                 do_input_event(cmd);
77 }