Code

keyboard: move code to ignore_key()
[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 "screen.h"
25 #include "Compiler.h"
27 #include <glib.h>
29 #include <unistd.h>
31 static bool
32 ignore_key(int key)
33 {
34         return key == ERR || key == '\0';
35 }
37 gcc_pure
38 static command_t
39 translate_key(int key)
40 {
41         if (ignore_key(key))
42                 return CMD_NONE;
44 #ifdef HAVE_GETMOUSE
45         if (key == KEY_MOUSE)
46                 return CMD_MOUSE_EVENT;
47 #endif
49         return get_key_command(key);
50 }
52 static command_t
53 get_keyboard_command(void)
54 {
55         return translate_key(wgetch(screen.main_window.w));
56 }
58 static gboolean
59 keyboard_event(gcc_unused GIOChannel *source,
60                gcc_unused GIOCondition condition,
61                gcc_unused gpointer data)
62 {
63         begin_input_event();
65         command_t cmd = get_keyboard_command();
66         if (cmd != CMD_NONE)
67                 if (!do_input_event(cmd))
68                         return FALSE;
70         end_input_event();
71         return TRUE;
72 }
74 void
75 keyboard_init(void)
76 {
77         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
78         g_io_add_watch(channel, G_IO_IN, keyboard_event, NULL);
79         g_io_channel_unref(channel);
80 }
82 void
83 keyboard_unread(int key)
84 {
85         command_t cmd = translate_key(key);
86         if (cmd != CMD_NONE)
87                 do_input_event(cmd);
88 }