Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
[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         return get_key_command(key);
42 }
44 static gboolean
45 keyboard_event(gcc_unused GIOChannel *source,
46                gcc_unused GIOCondition condition,
47                gcc_unused gpointer data)
48 {
49         int key = wgetch(screen.main_window.w);
50         if (ignore_key(key))
51                 return true;
53 #ifdef HAVE_GETMOUSE
54         if (key == KEY_MOUSE) {
55                 MEVENT event;
57                 /* retrieve the mouse event from curses */
58 #ifdef PDCURSES
59                 nc_getmouse(&event);
60 #else
61                 getmouse(&event);
62 #endif
64                 begin_input_event();
65                 do_mouse_event(event.x, event.y, event.bstate);
66                 end_input_event();
68                 return true;
69         }
70 #endif
72         command_t cmd = translate_key(key);
73         if (cmd == CMD_NONE)
74                 return true;
76         begin_input_event();
78         if (!do_input_event(cmd))
79                 return FALSE;
81         end_input_event();
82         return TRUE;
83 }
85 void
86 keyboard_init(void)
87 {
88         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
89         g_io_add_watch(channel, G_IO_IN, keyboard_event, NULL);
90         g_io_channel_unref(channel);
91 }
93 void
94 keyboard_unread(int key)
95 {
96         if (ignore_key(key))
97                 return;
99         command_t cmd = translate_key(key);
100         if (cmd != CMD_NONE)
101                 do_input_event(cmd);