Ticket #1836: fixed wrong shortcuts in menu items.
[kaloumi3.git] / src / tty / key.c
blob15c8d84d757e3589dd02983df73c0c609c681899
1 /* Keyboard support routines.
3 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
6 Written by: 1994, 1995 Miguel de Icaza.
7 1994, 1995 Janne Kukonlehto.
8 1995 Jakub Jelinek.
9 1997 Norbert Warmuth
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /** \file key.c
26 * \brief Source: keyboard support routines
29 #include <config.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
40 #include "../../src/global.h"
42 #include "../../src/tty/tty.h"
43 #include "../../src/tty/tty-internal.h" /* mouse_enabled */
44 #include "../../src/tty/mouse.h"
45 #include "../../src/tty/key.h"
46 #include "../../src/tty/win.h" /* xterm_flag */
48 #include "../../src/main.h"
49 #include "../../src/layout.h" /* winch_flag, mc_refresh() */
50 #include "../../src/cons.saver.h"
51 #include "../../src/strutil.h" /* str_casecmp */
53 #ifdef USE_VFS
54 #include "../../vfs/gc.h"
55 #endif
57 #ifdef HAVE_TEXTMODE_X11_SUPPORT
58 #include "../src/tty/x11conn.h"
59 #endif
61 #ifdef __linux__
62 #if defined(__GLIBC__) && (__GLIBC__ < 2)
63 # include <linux/termios.h> /* TIOCLINUX */
64 #elif defined HAVE_TERMIOS_H
65 # include <termios.h>
66 #endif
67 #include <sys/ioctl.h>
68 #endif /* __linux__ */
70 #ifdef __CYGWIN__
71 #include <termios.h>
72 #include <sys/ioctl.h>
73 #endif /* __CYGWIN__ */
75 #ifdef __QNXNTO__
76 #include <dlfcn.h>
77 #include <Ph.h>
78 #include <sys/dcmd_chr.h>
79 #endif /* __QNXNTO__ */
81 /*** global variables **************************************************/
83 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\.
84 and M-- and keypad + / - */
85 int alternate_plus_minus = 0;
87 int mou_auto_repeat = 100;
88 int double_click_speed = 250;
89 int old_esc_mode = 0;
90 int use_8th_bit_as_meta = 0;
92 /* This table is a mapping between names and the constants we use
93 * We use this to allow users to define alternate definitions for
94 * certain keys that may be missing from the terminal database
96 const key_code_name_t key_name_conv_tab[] = {
97 /* KEY_F(0) is not here, since we are mapping it to f10, so there is no reason
98 to define f0 as well. Also, it makes Learn keys a bunch of problems :( */
99 { KEY_F (1), "f1", N_("Function key 1"), "F1" },
100 { KEY_F (2), "f2", N_("Function key 2"), "F2" },
101 { KEY_F (3), "f3", N_("Function key 3"), "F3" },
102 { KEY_F (4), "f4", N_("Function key 4"), "F4" },
103 { KEY_F (5), "f5", N_("Function key 5"), "F5" },
104 { KEY_F (6), "f6", N_("Function key 6"), "F6" },
105 { KEY_F (7), "f7", N_("Function key 7"), "F7" },
106 { KEY_F (8), "f8", N_("Function key 8"), "F8" },
107 { KEY_F (9), "f9", N_("Function key 9"), "F9" },
108 { KEY_F (10), "f10", N_("Function key 10"), "F10" },
109 { KEY_F (11), "f11", N_("Function key 11"), "F11" },
110 { KEY_F (12), "f12", N_("Function key 12"), "F12" },
111 { KEY_F (13), "f13", N_("Function key 13"), "F13" },
112 { KEY_F (14), "f14", N_("Function key 14"), "F14" },
113 { KEY_F (15), "f15", N_("Function key 15"), "F15" },
114 { KEY_F (16), "f16", N_("Function key 16"), "F16" },
115 { KEY_F (17), "f17", N_("Function key 17"), "F17" },
116 { KEY_F (18), "f18", N_("Function key 18"), "F18" },
117 { KEY_F (19), "f19", N_("Function key 19"), "F19" },
118 { KEY_F (20), "f20", N_("Function key 20"), "F20" },
119 { KEY_BACKSPACE, "backspace", N_("Backspace key"), "Backspace" },
120 { KEY_END, "end", N_("End key"), "End" },
121 { KEY_UP, "up", N_("Up arrow key"), "Up" },
122 { KEY_DOWN, "down", N_("Down arrow key"), "Down" },
123 { KEY_LEFT, "left", N_("Left arrow key"), "Left" },
124 { KEY_RIGHT, "right", N_("Right arrow key"), "Right" },
125 { KEY_HOME, "home", N_("Home key"), "Home" },
126 { KEY_NPAGE, "pgdn", N_("Page Down key"), "PgDn" },
127 { KEY_PPAGE, "pgup", N_("Page Up key"), "PgUp" },
128 { KEY_IC, "insert", N_("Insert key"), "Ins" },
129 { KEY_DC, "delete", N_("Delete key"), "Del" },
130 { ALT ('\t'), "complete", N_("Completion/M-tab"), "Meta-Tab" },
131 { KEY_KP_ADD, "kpplus", N_("+ on keypad"), "+" },
132 { KEY_KP_SUBTRACT, "kpminus", N_("- on keypad"), "-" },
133 { (int) '/', "kpslash", N_("Slash on keypad"), "/" },
134 { KEY_KP_MULTIPLY, "kpasterisk", N_("* on keypad"), "*" },
136 /* From here on, these won't be shown in Learn keys (no space) */
137 { ESC_CHAR, "escape", N_("Escape key"), "Esc" },
138 { KEY_LEFT, "kpleft", N_("Left arrow keypad"), "Left" },
139 { KEY_RIGHT, "kpright", N_("Right arrow keypad"), "Right" },
140 { KEY_UP, "kpup", N_("Up arrow keypad"), "Up" },
141 { KEY_DOWN, "kpdown", N_("Down arrow keypad"), "Down" },
142 { KEY_HOME, "kphome", N_("Home on keypad"), "Home" },
143 { KEY_END, "kpend", N_("End on keypad"), "End" },
144 { KEY_NPAGE, "kpnpage", N_("Page Down keypad"), "PgDn" },
145 { KEY_PPAGE, "kpppage", N_("Page Up keypad"), "PgUp" },
146 { KEY_IC, "kpinsert", N_("Insert on keypad"), "Ins" },
147 { KEY_DC, "kpdelete", N_("Delete on keypad"), "Del" },
148 { (int) '\n', "kpenter", N_("Enter on keypad"), "Enter" },
149 { KEY_F (21), "f21", N_("Function key 21"), "F21" },
150 { KEY_F (22), "f22", N_("Function key 22"), "F22" },
151 { KEY_F (23), "f23", N_("Function key 23"), "F23" },
152 { KEY_F (24), "f24", N_("Function key 24"), "F24" },
154 /* Alternative label */
155 { ESC_CHAR, "esc", N_("Escape key"), "Esc" },
156 { KEY_BACKSPACE, "bs", N_("Backspace key"), "Bakspace" },
157 { KEY_IC, "ins", N_("Insert key"), "Ins" },
158 { KEY_DC, "del", N_("Delete key"), "Del" },
159 { (int) '+', "plus", N_("Plus"), "+" },
160 { (int) '-', "minus", N_("Minus"), "-" },
161 { (int) '*', "asterisk", N_("Asterisk"), "*" },
162 { (int) '.', "dot", N_("Dot"), "." },
163 { (int) '<', "lt", N_("Less than"), "<" },
164 { (int) '>', "gt", N_("Great than"), ">" },
165 { (int) '=', "equal", N_("Equal"), "=" },
166 { (int) ',', "comma", N_("Comma"), "," },
167 { (int) '\'', "apostrophe", N_("Apostrophe"), "\'" },
168 { (int) ':', "colon", N_("Colon"), ":" },
169 { (int) '!', "exclamation", N_("Exclamation mark"), "!" },
170 { (int) '?', "question", N_("Question mark"), "?" },
171 { (int) '&', "ampersand", N_("Ampersand"), "&" },
172 { (int) '$', "dollar", N_("Dollar sign"), "$" },
173 { (int) '"', "quota", N_("Quotation mark"), "\"" },
174 { (int) '^', "caret", N_("Caret"), "^" },
175 { (int) '~', "tilda", N_("Tilda"), "~" },
176 { (int) '`', "prime", N_("Prime"), "`" },
177 { (int) '_', "underline", N_("Underline"), "_" },
178 { (int) '_', "understrike", N_("Understrike"), "_" },
179 { (int) '|', "pipe", N_("Pipe"), "|" },
180 { (int) '\n', "enter", N_("Enter"), "Enter" },
181 { (int) '\t', "tab", N_("Tab key"), "Tab" },
182 { (int) ' ', "space", N_("Space key"), "Space" },
183 { (int) '/', "slash", N_("Slash key"), "/" },
184 { (int) '\\', "backslash", N_("Backslash key"), "\\" },
185 { (int) '#', "number", N_("Number sign #"), "#" },
186 { (int) '#', "hash", N_("Number sign #"), "#" },
188 /* meta keys */
189 { KEY_M_CTRL, "control", N_("Ctrl"), "C" },
190 { KEY_M_CTRL, "ctrl", N_("Ctrl"), "C" },
191 { KEY_M_ALT, "meta", N_("Alt"), "M" },
192 { KEY_M_ALT, "alt", N_("Alt"), "M" },
193 { KEY_M_ALT, "ralt", N_("Alt"), "M" },
194 { KEY_M_SHIFT, "shift", N_("Shift"), "S" },
196 { 0, NULL, NULL, NULL }
200 /*** file scope macro definitions **************************************/
202 #define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *) NULL))
203 #define DIF_TIME(t1, t2) ((t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec)/1000)
205 /* The maximum sequence length (32 + null terminator) */
206 #define SEQ_BUFFER_LEN 33
208 /*** file scope type declarations **************************************/
210 /* Linux console keyboard modifiers */
211 typedef enum {
212 SHIFT_PRESSED = (1 << 0),
213 ALTR_PRESSED = (1 << 1),
214 CONTROL_PRESSED = (1 << 2),
215 ALTL_PRESSED = (1 << 3)
216 } mod_pressed_t;
218 typedef struct key_def {
219 char ch; /* Holds the matching char code */
220 int code; /* The code returned, valid if child == NULL */
221 struct key_def *next;
222 struct key_def *child; /* sequence continuation */
223 int action; /* optional action to be done. Now used only
224 to mark that we are just after the first
225 Escape */
226 } key_def;
228 typedef struct {
229 int code;
230 const char *seq;
231 int action;
232 } key_define_t;
234 /* File descriptor monitoring add/remove routines */
235 typedef struct SelectList {
236 int fd;
237 select_fn callback;
238 void *info;
239 struct SelectList *next;
240 } SelectList;
242 #ifdef __QNXNTO__
243 typedef int (*ph_dv_f) (void *, void *);
244 typedef int (*ph_ov_f) (void *);
245 typedef int (*ph_pqc_f) (unsigned short, PhCursorInfo_t *);
246 #endif
248 /*** file scope variables **********************************************/
250 static key_define_t mc_default_keys[] = {
251 {ESC_CHAR, ESC_STR, MCKEY_ESCAPE},
252 {ESC_CHAR, ESC_STR ESC_STR, MCKEY_NOACTION},
253 {0, NULL, MCKEY_NOACTION},
256 /* Broken terminfo and termcap databases on xterminals */
257 static key_define_t xterm_key_defines[] = {
258 {KEY_F (1), ESC_STR "OP", MCKEY_NOACTION},
259 {KEY_F (2), ESC_STR "OQ", MCKEY_NOACTION},
260 {KEY_F (3), ESC_STR "OR", MCKEY_NOACTION},
261 {KEY_F (4), ESC_STR "OS", MCKEY_NOACTION},
262 {KEY_F (1), ESC_STR "[11~", MCKEY_NOACTION},
263 {KEY_F (2), ESC_STR "[12~", MCKEY_NOACTION},
264 {KEY_F (3), ESC_STR "[13~", MCKEY_NOACTION},
265 {KEY_F (4), ESC_STR "[14~", MCKEY_NOACTION},
266 {KEY_F (5), ESC_STR "[15~", MCKEY_NOACTION},
267 {KEY_F (6), ESC_STR "[17~", MCKEY_NOACTION},
268 {KEY_F (7), ESC_STR "[18~", MCKEY_NOACTION},
269 {KEY_F (8), ESC_STR "[19~", MCKEY_NOACTION},
270 {KEY_F (9), ESC_STR "[20~", MCKEY_NOACTION},
271 {KEY_F (10), ESC_STR "[21~", MCKEY_NOACTION},
273 /* old xterm Shift-arrows */
274 {KEY_M_SHIFT | KEY_UP, ESC_STR "O2A", MCKEY_NOACTION},
275 {KEY_M_SHIFT | KEY_DOWN, ESC_STR "O2B", MCKEY_NOACTION},
276 {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "O2C", MCKEY_NOACTION},
277 {KEY_M_SHIFT | KEY_LEFT, ESC_STR "O2D", MCKEY_NOACTION},
279 /* new xterm Shift-arrows */
280 {KEY_M_SHIFT | KEY_UP, ESC_STR "[1;2A", MCKEY_NOACTION},
281 {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[1;2B", MCKEY_NOACTION},
282 {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[1;2C", MCKEY_NOACTION},
283 {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[1;2D", MCKEY_NOACTION},
285 /* more xterm keys with modifiers */
286 {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5;5~", MCKEY_NOACTION},
287 {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6;5~", MCKEY_NOACTION},
288 {KEY_M_CTRL | KEY_IC, ESC_STR "[2;5~", MCKEY_NOACTION},
289 {KEY_M_CTRL | KEY_DC, ESC_STR "[3;5~", MCKEY_NOACTION},
290 {KEY_M_CTRL | KEY_HOME, ESC_STR "[1;5H", MCKEY_NOACTION},
291 {KEY_M_CTRL | KEY_END, ESC_STR "[1;5F", MCKEY_NOACTION},
292 {KEY_M_SHIFT | KEY_HOME, ESC_STR "[1;2H", MCKEY_NOACTION},
293 {KEY_M_SHIFT | KEY_END, ESC_STR "[1;2F", MCKEY_NOACTION},
294 {KEY_M_CTRL | KEY_UP, ESC_STR "[1;5A", MCKEY_NOACTION},
295 {KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;5B", MCKEY_NOACTION},
296 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;5C", MCKEY_NOACTION},
297 {KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;5D", MCKEY_NOACTION},
298 {KEY_M_SHIFT | KEY_IC, ESC_STR "[2;2~", MCKEY_NOACTION},
299 {KEY_M_SHIFT | KEY_DC, ESC_STR "[3;2~", MCKEY_NOACTION},
300 {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[1;6A", MCKEY_NOACTION},
301 {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;6B", MCKEY_NOACTION},
302 {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;6C", MCKEY_NOACTION},
303 {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;6D", MCKEY_NOACTION},
305 /* putty */
306 {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[[1;6A", MCKEY_NOACTION},
307 {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[[1;6B", MCKEY_NOACTION},
308 {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[[1;6C", MCKEY_NOACTION},
309 {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[[1;6D", MCKEY_NOACTION},
311 /* putty alt-arrow keys */
312 /* removed as source esc esc esc trouble */
314 { KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "OA", MCKEY_NOACTION },
315 { KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "OB", MCKEY_NOACTION },
316 { KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "OC", MCKEY_NOACTION },
317 { KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "OD", MCKEY_NOACTION },
318 { KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[5~", MCKEY_NOACTION },
319 { KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[6~", MCKEY_NOACTION },
320 { KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1~", MCKEY_NOACTION },
321 { KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[4~", MCKEY_NOACTION },
323 { KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR ESC_STR "[1;2A", MCKEY_NOACTION },
324 { KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR ESC_STR "[1;2B", MCKEY_NOACTION },
325 { KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR ESC_STR "[1;2C", MCKEY_NOACTION },
326 { KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR ESC_STR "[1;2D", MCKEY_NOACTION },
328 { KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR ESC_STR "[[5;5~", MCKEY_NOACTION },
329 { KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR ESC_STR "[[6;5~", MCKEY_NOACTION },
330 { KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR ESC_STR "[1;5H", MCKEY_NOACTION },
331 { KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR ESC_STR "[1;5F", MCKEY_NOACTION },
333 /* xterm alt-arrow keys */
334 {KEY_M_ALT | KEY_UP, ESC_STR "[1;3A", MCKEY_NOACTION},
335 {KEY_M_ALT | KEY_DOWN, ESC_STR "[1;3B", MCKEY_NOACTION},
336 {KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;3C", MCKEY_NOACTION},
337 {KEY_M_ALT | KEY_LEFT, ESC_STR "[1;3D", MCKEY_NOACTION},
338 {KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;3~", MCKEY_NOACTION},
339 {KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;3~", MCKEY_NOACTION},
340 {KEY_M_ALT | KEY_HOME, ESC_STR "[1~", MCKEY_NOACTION},
341 {KEY_M_ALT | KEY_END, ESC_STR "[4~", MCKEY_NOACTION},
342 {KEY_M_CTRL | KEY_M_ALT | KEY_UP, ESC_STR "[1;7A", MCKEY_NOACTION},
343 {KEY_M_CTRL | KEY_M_ALT | KEY_DOWN, ESC_STR "[1;7B", MCKEY_NOACTION},
344 {KEY_M_CTRL | KEY_M_ALT | KEY_RIGHT, ESC_STR "[1;7C", MCKEY_NOACTION},
345 {KEY_M_CTRL | KEY_M_ALT | KEY_LEFT, ESC_STR "[1;7D", MCKEY_NOACTION},
346 {KEY_M_CTRL | KEY_M_ALT | KEY_PPAGE, ESC_STR "[5;7~", MCKEY_NOACTION},
347 {KEY_M_CTRL | KEY_M_ALT | KEY_NPAGE, ESC_STR "[6;7~", MCKEY_NOACTION},
348 {KEY_M_CTRL | KEY_M_ALT | KEY_HOME, ESC_STR "OH", MCKEY_NOACTION},
349 {KEY_M_CTRL | KEY_M_ALT | KEY_END, ESC_STR "OF", MCKEY_NOACTION},
351 /* rxvt keys with modifiers */
352 {KEY_M_SHIFT | KEY_UP, ESC_STR "[a", MCKEY_NOACTION},
353 {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION},
354 {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION},
355 {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION},
356 {KEY_M_CTRL | KEY_UP, ESC_STR "Oa", MCKEY_NOACTION},
357 {KEY_M_CTRL | KEY_DOWN, ESC_STR "Ob", MCKEY_NOACTION},
358 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "Oc", MCKEY_NOACTION},
359 {KEY_M_CTRL | KEY_LEFT, ESC_STR "Od", MCKEY_NOACTION},
360 {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5^", MCKEY_NOACTION},
361 {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6^", MCKEY_NOACTION},
362 {KEY_M_CTRL | KEY_HOME, ESC_STR "[7^", MCKEY_NOACTION},
363 {KEY_M_CTRL | KEY_END, ESC_STR "[8^", MCKEY_NOACTION},
364 {KEY_M_SHIFT | KEY_HOME, ESC_STR "[7$", MCKEY_NOACTION},
365 {KEY_M_SHIFT | KEY_END, ESC_STR "[8$", MCKEY_NOACTION},
366 {KEY_M_CTRL | KEY_IC, ESC_STR "[2^", MCKEY_NOACTION},
367 {KEY_M_CTRL | KEY_DC, ESC_STR "[3^", MCKEY_NOACTION},
368 {KEY_M_SHIFT | KEY_DC, ESC_STR "[3$", MCKEY_NOACTION},
370 /* konsole keys with modifiers */
371 {KEY_M_SHIFT | KEY_HOME, ESC_STR "O2H", MCKEY_NOACTION},
372 {KEY_M_SHIFT | KEY_END, ESC_STR "O2F", MCKEY_NOACTION},
374 /* gnome-terminal */
375 {KEY_M_SHIFT | KEY_UP, ESC_STR "[2A", MCKEY_NOACTION},
376 {KEY_M_SHIFT | KEY_DOWN, ESC_STR "[2B", MCKEY_NOACTION},
377 {KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[2C", MCKEY_NOACTION},
378 {KEY_M_SHIFT | KEY_LEFT, ESC_STR "[2D", MCKEY_NOACTION},
379 {KEY_M_CTRL | KEY_UP, ESC_STR "[5A", MCKEY_NOACTION},
380 {KEY_M_CTRL | KEY_DOWN, ESC_STR "[5B", MCKEY_NOACTION},
381 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[5C", MCKEY_NOACTION},
382 {KEY_M_CTRL | KEY_LEFT, ESC_STR "[5D", MCKEY_NOACTION},
383 {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[6A", MCKEY_NOACTION},
384 {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[6B", MCKEY_NOACTION},
385 {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[6C", MCKEY_NOACTION},
386 {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[6D", MCKEY_NOACTION},
388 /* gnome-terminal - application mode */
389 {KEY_M_CTRL | KEY_UP, ESC_STR "O5A", MCKEY_NOACTION},
390 {KEY_M_CTRL | KEY_DOWN, ESC_STR "O5B", MCKEY_NOACTION},
391 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "O5C", MCKEY_NOACTION},
392 {KEY_M_CTRL | KEY_LEFT, ESC_STR "O5D", MCKEY_NOACTION},
393 {KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "O6A", MCKEY_NOACTION},
394 {KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "O6B", MCKEY_NOACTION},
395 {KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "O6C", MCKEY_NOACTION},
396 {KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "O6D", MCKEY_NOACTION},
398 /* iTerm */
399 {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[5;2~", MCKEY_NOACTION},
400 {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[6;2~", MCKEY_NOACTION},
402 /* putty */
403 {KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[[5;53~", MCKEY_NOACTION},
404 {KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[[6;53~", MCKEY_NOACTION},
406 /* keypad keys */
407 {KEY_IC, ESC_STR "Op", MCKEY_NOACTION},
408 {KEY_DC, ESC_STR "On", MCKEY_NOACTION},
409 {'/', ESC_STR "Oo", MCKEY_NOACTION},
410 {'\n', ESC_STR "OM", MCKEY_NOACTION},
412 {0, NULL, MCKEY_NOACTION},
415 /* qansi-m terminals have a much more key combinatios,
416 which are undefined in termcap/terminfo */
417 static key_define_t qansi_key_defines[] = {
418 /* qansi-m terminal */
419 {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[u", MCKEY_NOACTION}, /* Ctrl-PgDown */
420 {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[v", MCKEY_NOACTION}, /* Ctrl-PgUp */
421 {KEY_M_CTRL | KEY_HOME, ESC_STR "[h", MCKEY_NOACTION}, /* Ctrl-Home */
422 {KEY_M_CTRL | KEY_END, ESC_STR "[y", MCKEY_NOACTION}, /* Ctrl-End */
423 {KEY_M_CTRL | KEY_IC, ESC_STR "[`", MCKEY_NOACTION}, /* Ctrl-Insert */
424 {KEY_M_CTRL | KEY_DC, ESC_STR "[p", MCKEY_NOACTION}, /* Ctrl-Delete */
425 {KEY_M_CTRL | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION}, /* Ctrl-Left */
426 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION}, /* Ctrl-Right */
427 {KEY_M_CTRL | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION}, /* Ctrl-Down */
428 {KEY_M_CTRL | KEY_UP, ESC_STR "[a", MCKEY_NOACTION}, /* Ctrl-Up */
429 {KEY_M_CTRL | KEY_KP_ADD, ESC_STR "[s", MCKEY_NOACTION}, /* Ctrl-Gr-Plus */
430 {KEY_M_CTRL | KEY_KP_SUBTRACT, ESC_STR "[t", MCKEY_NOACTION}, /* Ctrl-Gr-Minus */
431 {KEY_M_CTRL | '\t', ESC_STR "[z", MCKEY_NOACTION}, /* Ctrl-Tab */
432 {KEY_M_SHIFT | '\t', ESC_STR "[Z", MCKEY_NOACTION}, /* Shift-Tab */
433 {KEY_M_CTRL | KEY_F (1), ESC_STR "[1~", MCKEY_NOACTION}, /* Ctrl-F1 */
434 {KEY_M_CTRL | KEY_F (2), ESC_STR "[2~", MCKEY_NOACTION}, /* Ctrl-F2 */
435 {KEY_M_CTRL | KEY_F (3), ESC_STR "[3~", MCKEY_NOACTION}, /* Ctrl-F3 */
436 {KEY_M_CTRL | KEY_F (4), ESC_STR "[4~", MCKEY_NOACTION}, /* Ctrl-F4 */
437 {KEY_M_CTRL | KEY_F (5), ESC_STR "[5~", MCKEY_NOACTION}, /* Ctrl-F5 */
438 {KEY_M_CTRL | KEY_F (6), ESC_STR "[6~", MCKEY_NOACTION}, /* Ctrl-F6 */
439 {KEY_M_CTRL | KEY_F (7), ESC_STR "[7~", MCKEY_NOACTION}, /* Ctrl-F7 */
440 {KEY_M_CTRL | KEY_F (8), ESC_STR "[8~", MCKEY_NOACTION}, /* Ctrl-F8 */
441 {KEY_M_CTRL | KEY_F (9), ESC_STR "[9~", MCKEY_NOACTION}, /* Ctrl-F9 */
442 {KEY_M_CTRL | KEY_F (10), ESC_STR "[10~", MCKEY_NOACTION}, /* Ctrl-F10 */
443 {KEY_M_CTRL | KEY_F (11), ESC_STR "[11~", MCKEY_NOACTION}, /* Ctrl-F11 */
444 {KEY_M_CTRL | KEY_F (12), ESC_STR "[12~", MCKEY_NOACTION}, /* Ctrl-F12 */
445 {KEY_M_ALT | KEY_F (1), ESC_STR "[17~", MCKEY_NOACTION}, /* Alt-F1 */
446 {KEY_M_ALT | KEY_F (2), ESC_STR "[18~", MCKEY_NOACTION}, /* Alt-F2 */
447 {KEY_M_ALT | KEY_F (3), ESC_STR "[19~", MCKEY_NOACTION}, /* Alt-F3 */
448 {KEY_M_ALT | KEY_F (4), ESC_STR "[20~", MCKEY_NOACTION}, /* Alt-F4 */
449 {KEY_M_ALT | KEY_F (5), ESC_STR "[21~", MCKEY_NOACTION}, /* Alt-F5 */
450 {KEY_M_ALT | KEY_F (6), ESC_STR "[22~", MCKEY_NOACTION}, /* Alt-F6 */
451 {KEY_M_ALT | KEY_F (7), ESC_STR "[23~", MCKEY_NOACTION}, /* Alt-F7 */
452 {KEY_M_ALT | KEY_F (8), ESC_STR "[24~", MCKEY_NOACTION}, /* Alt-F8 */
453 {KEY_M_ALT | KEY_F (9), ESC_STR "[25~", MCKEY_NOACTION}, /* Alt-F9 */
454 {KEY_M_ALT | KEY_F (10), ESC_STR "[26~", MCKEY_NOACTION}, /* Alt-F10 */
455 {KEY_M_ALT | KEY_F (11), ESC_STR "[27~", MCKEY_NOACTION}, /* Alt-F11 */
456 {KEY_M_ALT | KEY_F (12), ESC_STR "[28~", MCKEY_NOACTION}, /* Alt-F12 */
457 {KEY_M_ALT | 'a', ESC_STR "Na", MCKEY_NOACTION}, /* Alt-a */
458 {KEY_M_ALT | 'b', ESC_STR "Nb", MCKEY_NOACTION}, /* Alt-b */
459 {KEY_M_ALT | 'c', ESC_STR "Nc", MCKEY_NOACTION}, /* Alt-c */
460 {KEY_M_ALT | 'd', ESC_STR "Nd", MCKEY_NOACTION}, /* Alt-d */
461 {KEY_M_ALT | 'e', ESC_STR "Ne", MCKEY_NOACTION}, /* Alt-e */
462 {KEY_M_ALT | 'f', ESC_STR "Nf", MCKEY_NOACTION}, /* Alt-f */
463 {KEY_M_ALT | 'g', ESC_STR "Ng", MCKEY_NOACTION}, /* Alt-g */
464 {KEY_M_ALT | 'h', ESC_STR "Nh", MCKEY_NOACTION}, /* Alt-h */
465 {KEY_M_ALT | 'i', ESC_STR "Ni", MCKEY_NOACTION}, /* Alt-i */
466 {KEY_M_ALT | 'j', ESC_STR "Nj", MCKEY_NOACTION}, /* Alt-j */
467 {KEY_M_ALT | 'k', ESC_STR "Nk", MCKEY_NOACTION}, /* Alt-k */
468 {KEY_M_ALT | 'l', ESC_STR "Nl", MCKEY_NOACTION}, /* Alt-l */
469 {KEY_M_ALT | 'm', ESC_STR "Nm", MCKEY_NOACTION}, /* Alt-m */
470 {KEY_M_ALT | 'n', ESC_STR "Nn", MCKEY_NOACTION}, /* Alt-n */
471 {KEY_M_ALT | 'o', ESC_STR "No", MCKEY_NOACTION}, /* Alt-o */
472 {KEY_M_ALT | 'p', ESC_STR "Np", MCKEY_NOACTION}, /* Alt-p */
473 {KEY_M_ALT | 'q', ESC_STR "Nq", MCKEY_NOACTION}, /* Alt-q */
474 {KEY_M_ALT | 'r', ESC_STR "Nr", MCKEY_NOACTION}, /* Alt-r */
475 {KEY_M_ALT | 's', ESC_STR "Ns", MCKEY_NOACTION}, /* Alt-s */
476 {KEY_M_ALT | 't', ESC_STR "Nt", MCKEY_NOACTION}, /* Alt-t */
477 {KEY_M_ALT | 'u', ESC_STR "Nu", MCKEY_NOACTION}, /* Alt-u */
478 {KEY_M_ALT | 'v', ESC_STR "Nv", MCKEY_NOACTION}, /* Alt-v */
479 {KEY_M_ALT | 'w', ESC_STR "Nw", MCKEY_NOACTION}, /* Alt-w */
480 {KEY_M_ALT | 'x', ESC_STR "Nx", MCKEY_NOACTION}, /* Alt-x */
481 {KEY_M_ALT | 'y', ESC_STR "Ny", MCKEY_NOACTION}, /* Alt-y */
482 {KEY_M_ALT | 'z', ESC_STR "Nz", MCKEY_NOACTION}, /* Alt-z */
483 {KEY_KP_SUBTRACT, ESC_STR "[S", MCKEY_NOACTION}, /* Gr-Minus */
484 {KEY_KP_ADD, ESC_STR "[T", MCKEY_NOACTION}, /* Gr-Plus */
485 {0, NULL, MCKEY_NOACTION},
488 /* timeout for old_esc_mode in usec */
489 static int keyboard_key_timeout = 1000000; /* settable via env */
491 /* This holds all the key definitions */
492 static key_def *keys = NULL;
494 static int input_fd;
495 static int disabled_channels = 0; /* Disable channels checking */
497 static SelectList *select_list = NULL;
499 static int seq_buffer[SEQ_BUFFER_LEN];
500 static int *seq_append = NULL;
502 static int *pending_keys = NULL;
504 #ifdef __QNXNTO__
505 ph_dv_f ph_attach;
506 ph_ov_f ph_input_group;
507 ph_pqc_f ph_query_cursor;
508 #endif
510 #ifdef HAVE_TEXTMODE_X11_SUPPORT
511 static Display *x11_display;
512 static Window x11_window;
513 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
515 /*** file scope functions **********************************************/
517 static int
518 add_selects (fd_set * select_set)
520 int top_fd = 0;
522 if (disabled_channels == 0) {
523 SelectList *p;
525 for (p = select_list; p != NULL; p = p->next) {
526 FD_SET (p->fd, select_set);
527 if (p->fd > top_fd)
528 top_fd = p->fd;
532 return top_fd;
535 static void
536 check_selects (fd_set * select_set)
538 if (disabled_channels == 0) {
539 gboolean retry;
541 do {
542 SelectList *p;
544 retry = FALSE;
545 for (p = select_list; p; p = p->next)
546 if (FD_ISSET (p->fd, select_set)) {
547 FD_CLR (p->fd, select_set);
548 (*p->callback) (p->fd, p->info);
549 retry = TRUE;
550 break;
552 } while (retry);
556 /* If set timeout is set, then we wait 0.1 seconds, else, we block */
557 static void
558 try_channels (int set_timeout)
560 struct timeval timeout;
561 static fd_set select_set;
562 struct timeval *timeptr;
563 int v;
564 int maxfdp;
566 while (1) {
567 FD_ZERO (&select_set);
568 FD_SET (input_fd, &select_set); /* Add stdin */
569 maxfdp = max (add_selects (&select_set), input_fd);
571 timeptr = NULL;
572 if (set_timeout) {
573 timeout.tv_sec = 0;
574 timeout.tv_usec = 100000;
575 timeptr = &timeout;
578 v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
579 if (v > 0) {
580 check_selects (&select_set);
581 if (FD_ISSET (input_fd, &select_set))
582 break;
587 static key_def *
588 create_sequence (const char *seq, int code, int action)
590 key_def *base, *p, *attach;
592 for (base = attach = NULL; *seq; seq++) {
593 p = g_new (key_def, 1);
594 if (base == NULL)
595 base = p;
596 if (attach != NULL)
597 attach->child = p;
599 p->ch = *seq;
600 p->code = code;
601 p->child = p->next = NULL;
602 if (seq[1] == '\0')
603 p->action = action;
604 else
605 p->action = MCKEY_NOACTION;
606 attach = p;
608 return base;
611 static void
612 define_sequences (const key_define_t * kd)
614 int i;
616 for (i = 0; kd[i].code != 0; i++)
617 define_sequence (kd[i].code, kd[i].seq, kd[i].action);
620 static void
621 init_key_x11 (void)
623 #ifdef HAVE_TEXTMODE_X11_SUPPORT
624 if (getenv ("DISPLAY") != NULL) {
625 x11_display = mc_XOpenDisplay (0);
627 if (x11_display != NULL)
628 x11_window = DefaultRootWindow (x11_display);
630 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
633 /* Workaround for System V Curses vt100 bug */
634 static int
635 getch_with_delay (void)
637 int c;
639 /* This routine could be used on systems without mouse support,
640 so we need to do the select check :-( */
641 while (1) {
642 if (pending_keys == NULL)
643 try_channels (0);
645 /* Try to get a character */
646 c = get_key_code (0);
647 if (c != -1)
648 break;
649 /* Failed -> wait 0.1 secs and try again */
650 try_channels (1);
652 /* Success -> return the character */
653 return c;
656 static void
657 xmouse_get_event (Gpm_Event * ev)
659 int btn;
660 static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
661 static struct timeval tv2;
662 static int clicks = 0;
663 static int last_btn = 0;
665 /* Decode Xterm mouse information to a GPM style event */
667 /* Variable btn has following meaning: */
668 /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
669 btn = tty_lowlevel_getch () - 32;
671 /* There seems to be no way of knowing which button was released */
672 /* So we assume all the buttons were released */
674 if (btn == 3) {
675 if (last_btn != 0) {
676 if ((last_btn & (GPM_B_UP | GPM_B_DOWN)) != 0) {
677 /* FIXME: DIRTY HACK */
678 /* don't generate GPM_UP after mouse wheel */
679 /* need for menu event handling */
680 ev->type = 0;
681 tv1.tv_sec = 0;
682 tv1.tv_usec = 0;
683 } else {
684 ev->type = GPM_UP | (GPM_SINGLE << clicks);
685 GET_TIME (tv1);
687 ev->buttons = 0;
688 last_btn = 0;
689 clicks = 0;
690 } else {
691 /* Bogus event, maybe mouse wheel */
692 ev->type = 0;
694 } else {
695 if (btn >= 32 && btn <= 34) {
696 btn -= 32;
697 ev->type = GPM_DRAG;
698 } else
699 ev->type = GPM_DOWN;
701 GET_TIME (tv2);
702 if (tv1.tv_sec && (DIF_TIME (tv1, tv2) < double_click_speed)) {
703 clicks++;
704 clicks %= 3;
705 } else
706 clicks = 0;
708 switch (btn) {
709 case 0:
710 ev->buttons = GPM_B_LEFT;
711 break;
712 case 1:
713 ev->buttons = GPM_B_MIDDLE;
714 break;
715 case 2:
716 ev->buttons = GPM_B_RIGHT;
717 break;
718 case 64:
719 ev->buttons = GPM_B_UP;
720 clicks = 0;
721 break;
722 case 65:
723 ev->buttons = GPM_B_DOWN;
724 clicks = 0;
725 break;
726 default:
727 /* Nothing */
728 ev->type = 0;
729 ev->buttons = 0;
730 break;
732 last_btn = ev->buttons;
734 /* Coordinates are 33-based */
735 /* Transform them to 1-based */
736 ev->x = tty_lowlevel_getch () - 32;
737 ev->y = tty_lowlevel_getch () - 32;
741 * Get modifier state (shift, alt, ctrl) for the last key pressed.
742 * We are assuming that the state didn't change since the key press.
743 * This is only correct if get_modifier() is called very fast after
744 * the input was received, so that the user didn't release the
745 * modifier keys yet.
747 static int
748 get_modifier (void)
750 int result = 0;
751 #ifdef __QNXNTO__
752 int mod_status, shift_ext_status;
753 static int in_photon = 0;
754 static int ph_ig = 0;
755 PhCursorInfo_t cursor_info;
756 #endif /* __QNXNTO__ */
758 #ifdef HAVE_TEXTMODE_X11_SUPPORT
759 if (x11_window != 0) {
760 Window root, child;
761 int root_x, root_y;
762 int win_x, win_y;
763 unsigned int mask;
765 mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
766 &root_y, &win_x, &win_y, &mask);
768 if (mask & ShiftMask)
769 result |= KEY_M_SHIFT;
770 if (mask & ControlMask)
771 result |= KEY_M_CTRL;
772 return result;
774 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
775 #ifdef __QNXNTO__
777 if (in_photon == 0) {
778 /* First time here, let's load Photon library and attach
779 to Photon */
780 in_photon = -1;
781 if (getenv ("PHOTON2_PATH") != NULL) {
782 /* QNX 6.x has no support for RTLD_LAZY */
783 void *ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
784 if (ph_handle != NULL) {
785 ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
786 ph_input_group = (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
787 ph_query_cursor = (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
788 if ((ph_attach != NULL) && (ph_input_group != NULL)
789 && (ph_query_cursor != NULL)) {
790 if ((*ph_attach) (0, 0)) { /* Attached */
791 ph_ig = (*ph_input_group) (0);
792 in_photon = 1;
798 /* We do not have Photon running. Assume we are in text
799 console or xterm */
800 if (in_photon == -1) {
801 if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (int), NULL) == -1)
802 return 0;
803 shift_ext_status = mod_status & 0xffffff00UL;
804 mod_status &= 0x7f;
805 if (mod_status & _LINESTATUS_CON_ALT)
806 result |= KEY_M_ALT;
807 if (mod_status & _LINESTATUS_CON_CTRL)
808 result |= KEY_M_CTRL;
809 if ((mod_status & _LINESTATUS_CON_SHIFT)
810 || (shift_ext_status & 0x00000800UL))
811 result |= KEY_M_SHIFT;
812 } else {
813 (*ph_query_cursor) (ph_ig, &cursor_info);
814 if (cursor_info.key_mods & 0x04)
815 result |= KEY_M_ALT;
816 if (cursor_info.key_mods & 0x02)
817 result |= KEY_M_CTRL;
818 if (cursor_info.key_mods & 0x01)
819 result |= KEY_M_SHIFT;
821 #endif /* __QNXNTO__ */
823 #if defined __linux__ || (defined __CYGWIN__ && defined TIOCLINUX)
825 unsigned char modifiers = 6;
827 if (ioctl (0, TIOCLINUX, &modifiers) < 0)
828 return 0;
830 /* Translate Linux modifiers into mc modifiers */
831 if (modifiers & SHIFT_PRESSED)
832 result |= KEY_M_SHIFT;
833 if (modifiers & (ALTL_PRESSED | ALTR_PRESSED))
834 result |= KEY_M_ALT;
835 if (modifiers & CONTROL_PRESSED)
836 result |= KEY_M_CTRL;
838 #endif /* !__linux__ */
839 return result;
842 static gboolean
843 push_char (int c)
845 gboolean ret = FALSE;
847 if (seq_append == NULL)
848 seq_append = seq_buffer;
850 if (seq_append != &(seq_buffer[SEQ_BUFFER_LEN - 2])) {
851 *(seq_append++) = c;
852 *seq_append = 0;
853 ret = TRUE;
856 return ret;
859 /* Apply corrections for the keycode generated in get_key_code() */
860 static int
861 correct_key_code (int code)
863 unsigned int c = code & ~KEY_M_MASK; /* code without modifier */
864 unsigned int mod = code & KEY_M_MASK; /* modifier */
865 #ifdef __QNXNTO__
866 unsigned int qmod; /* bunch of the QNX console
867 modifiers needs unchanged */
868 #endif /* __QNXNTO__ */
871 * Add key modifiers directly from X11 or OS.
872 * Ordinary characters only get modifiers from sequences.
874 if (c < 32 || c >= 256) {
875 mod |= get_modifier ();
878 /* This is needed if the newline is reported as carriage return */
879 if (c == '\r')
880 c = '\n';
882 /* This is reported to be useful on AIX */
883 if (c == KEY_SCANCEL)
884 c = '\t';
886 /* Convert Shift+Tab and Ctrl+Tab to Back Tab */
887 if ((c == '\t') && (mod & (KEY_M_SHIFT | KEY_M_CTRL))) {
888 c = KEY_BTAB;
889 mod = 0;
892 /* F0 is the same as F10 for out purposes */
893 if (c == KEY_F (0))
894 c = KEY_F (10);
897 * We are not interested if Ctrl was pressed when entering control
898 * characters, so assume that it was. When checking for such keys,
899 * XCTRL macro should be used. In some cases, we are interested,
900 * e.g. to distinguish Ctrl-Enter from Enter.
902 if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n') {
903 mod |= KEY_M_CTRL;
905 #ifdef __QNXNTO__
906 qmod = get_modifier ();
908 if ((c == 127) && (mod == 0)) { /* Add Ctrl/Alt/Shift-BackSpace */
909 mod |= get_modifier ();
910 c = KEY_BACKSPACE;
913 if ((c == '0') && (mod == 0)) { /* Add Shift-Insert on key pad */
914 if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) {
915 mod = KEY_M_SHIFT;
916 c = KEY_IC;
920 if ((c == '.') && (mod == 0)) { /* Add Shift-Del on key pad */
921 if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT) {
922 mod = KEY_M_SHIFT;
923 c = KEY_DC;
926 #endif /* __QNXNTO__ */
928 /* Unrecognized 0177 is delete (preserve Ctrl) */
929 if (c == 0177) {
930 c = KEY_BACKSPACE;
933 /* Unrecognized Ctrl-d is delete */
934 if (c == (31 & 'd')) {
935 c = KEY_DC;
936 mod &= ~KEY_M_CTRL;
939 /* Unrecognized Ctrl-h is backspace */
940 if (c == (31 & 'h')) {
941 c = KEY_BACKSPACE;
942 mod &= ~KEY_M_CTRL;
945 /* Shift+BackSpace is backspace */
946 if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT)) {
947 mod &= ~KEY_M_SHIFT;
950 /* Convert Shift+Fn to F(n+10) */
951 if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT)) {
952 c += 10;
955 /* Remove Shift information from function keys */
956 if (c >= KEY_F (1) && c <= KEY_F (20)) {
957 mod &= ~KEY_M_SHIFT;
960 if (!alternate_plus_minus)
961 switch (c) {
962 case KEY_KP_ADD:
963 c = '+';
964 break;
965 case KEY_KP_SUBTRACT:
966 c = '-';
967 break;
968 case KEY_KP_MULTIPLY:
969 c = '*';
970 break;
973 return (mod | c);
976 static int
977 xgetch_second (void)
979 fd_set Read_FD_Set;
980 int c;
981 struct timeval timeout;
983 timeout.tv_sec = keyboard_key_timeout / 1000000;
984 timeout.tv_usec = keyboard_key_timeout % 1000000;
985 tty_nodelay (TRUE);
986 FD_ZERO (&Read_FD_Set);
987 FD_SET (input_fd, &Read_FD_Set);
988 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
989 c = tty_lowlevel_getch ();
990 tty_nodelay (FALSE);
991 return c;
994 static void
995 learn_store_key (char *buffer, char **p, int c)
997 if (*p - buffer > 253)
998 return;
999 if (c == ESC_CHAR) {
1000 *(*p)++ = '\\';
1001 *(*p)++ = 'e';
1002 } else if (c < ' ') {
1003 *(*p)++ = '^';
1004 *(*p)++ = c + 'a' - 1;
1005 } else if (c == '^') {
1006 *(*p)++ = '^';
1007 *(*p)++ = '^';
1008 } else
1009 *(*p)++ = (char) c;
1012 static void
1013 k_dispose (key_def * k)
1015 if (k != NULL) {
1016 k_dispose (k->child);
1017 k_dispose (k->next);
1018 g_free (k);
1022 static void
1023 s_dispose (SelectList * sel)
1025 if (sel != NULL) {
1026 s_dispose (sel->next);
1027 g_free (sel);
1031 /*** public functions **************************************************/
1033 /* This has to be called before init_slang or whatever routine
1034 calls any define_sequence */
1035 void
1036 init_key (void)
1038 const char *term = getenv ("TERM");
1039 const char *kt = getenv ("KEYBOARD_KEY_TIMEOUT_US");
1040 if (kt != NULL)
1041 keyboard_key_timeout = atoi (kt);
1043 /* This has to be the first define_sequence */
1044 /* So, we can assume that the first keys member has ESC */
1045 define_sequences (mc_default_keys);
1047 /* Terminfo on irix does not have some keys */
1048 if (xterm_flag
1049 || (term != NULL
1050 && (strncmp (term, "iris-ansi", 9) == 0
1051 || strncmp (term, "xterm", 5) == 0
1052 || strncmp (term, "rxvt", 4) == 0 || strcmp (term, "screen") == 0)))
1053 define_sequences (xterm_key_defines);
1055 /* load some additional keys (e.g. direct Alt-? support) */
1056 load_xtra_key_defines ();
1058 #ifdef __QNX__
1059 if ((term != NULL) && (strncmp (term, "qnx", 3) == 0)) {
1060 /* Modify the default value of use_8th_bit_as_meta: we would
1061 * like to provide a working mc for a newbie who knows nothing
1062 * about [Options|Display bits|Full 8 bits input]...
1064 * Don't use 'meta'-bit, when we are dealing with a
1065 * 'qnx*'-type terminal: clear the default value!
1066 * These terminal types use 0xFF as an escape character,
1067 * so use_8th_bit_as_meta==1 must not be enabled!
1069 * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
1070 * is not used now (doesn't even depend on use_8th_bit_as_meta
1071 * as in mc-3.1.2)...GREAT!...no additional code is required!]
1073 use_8th_bit_as_meta = 0;
1075 #endif /* __QNX__ */
1077 init_key_x11 ();
1079 /* Load the qansi-m key definitions
1080 if we are running under the qansi-m terminal */
1081 if (term != NULL && (strncmp (term, "qansi-m", 7) == 0))
1082 define_sequences (qansi_key_defines);
1085 /* This has to be called after SLang_init_tty/slint_init */
1086 void
1087 init_key_input_fd (void)
1089 #ifdef HAVE_SLANG
1090 input_fd = SLang_TT_Read_FD;
1091 #endif
1094 void
1095 done_key (void)
1097 k_dispose (keys);
1098 s_dispose (select_list);
1100 #ifdef HAVE_TEXTMODE_X11_SUPPORT
1101 if (x11_display)
1102 mc_XCloseDisplay (x11_display);
1103 #endif
1106 void
1107 add_select_channel (int fd, select_fn callback, void *info)
1109 SelectList *new;
1111 new = g_new (SelectList, 1);
1112 new->fd = fd;
1113 new->callback = callback;
1114 new->info = info;
1115 new->next = select_list;
1116 select_list = new;
1119 void
1120 delete_select_channel (int fd)
1122 SelectList *p = select_list;
1123 SelectList *p_prev = NULL;
1124 SelectList *p_next;
1126 while (p != NULL)
1127 if (p->fd == fd) {
1128 p_next = p->next;
1130 if (p_prev != NULL)
1131 p_prev->next = p_next;
1132 else
1133 select_list = p_next;
1135 g_free (p);
1136 p = p_next;
1137 } else {
1138 p_prev = p;
1139 p = p->next;
1143 void
1144 channels_up (void)
1146 if (disabled_channels == 0)
1147 fputs ("Error: channels_up called with disabled_channels = 0\n", stderr);
1148 disabled_channels--;
1151 void
1152 channels_down (void)
1154 disabled_channels++;
1158 * Common handler for standard movement keys in a text area. Provided
1159 * functions are called with the "data" argument. backfn and forfn also
1160 * get an argument indicating how many lines to scroll. Return MSG_HANDLED
1161 * if the key was handled, MSG_NOT_HANDLED otherwise.
1163 cb_ret_t
1164 check_movement_keys (int key, int page_size, void *data, move_fn backfn,
1165 move_fn forfn, move_fn topfn, move_fn bottomfn)
1167 switch (key) {
1168 case KEY_UP:
1169 case XCTRL ('p'):
1170 (*backfn) (data, 1);
1171 break;
1173 case KEY_DOWN:
1174 case XCTRL ('n'):
1175 (*forfn) (data, 1);
1176 break;
1178 case KEY_PPAGE:
1179 case ALT ('v'):
1180 (*backfn) (data, page_size - 1);
1181 break;
1183 case KEY_NPAGE:
1184 case XCTRL ('v'):
1185 (*forfn) (data, page_size - 1);
1186 break;
1188 case KEY_HOME:
1189 case KEY_M_CTRL | KEY_HOME:
1190 case KEY_M_CTRL | KEY_PPAGE:
1191 case KEY_A1:
1192 case ALT ('<'):
1193 (*topfn) (data, 0);
1194 break;
1196 case KEY_END:
1197 case KEY_M_CTRL | KEY_END:
1198 case KEY_M_CTRL | KEY_NPAGE:
1199 case KEY_C1:
1200 case ALT ('>'):
1201 (*bottomfn) (data, 0);
1202 break;
1204 case 'b':
1205 case KEY_BACKSPACE:
1206 (*backfn) (data, page_size - 1);
1207 break;
1209 case ' ':
1210 (*forfn) (data, page_size - 1);
1211 break;
1213 case 'u':
1214 (*backfn) (data, page_size / 2);
1215 break;
1217 case 'd':
1218 (*forfn) (data, page_size / 2);
1219 break;
1221 case 'g':
1222 (*topfn) (data, 0);
1223 break;
1225 case 'G':
1226 (*bottomfn) (data, 0);
1227 break;
1229 default:
1230 return MSG_NOT_HANDLED;
1232 return MSG_HANDLED;
1236 static const size_t key_name_conv_tab_size = sizeof (key_name_conv_tab) /
1237 sizeof (key_name_conv_tab[0]) - 1;
1238 static key_code_name_t key_name_conv_tab_sorted [sizeof (key_name_conv_tab) /
1239 sizeof (key_name_conv_tab[0]) - 1];
1241 static int
1242 key_code_name_comparator (const void *p1, const void *p2)
1244 const key_code_name_t *n1 = (const key_code_name_t *) p1;
1245 const key_code_name_t *n2 = (const key_code_name_t *) p2;
1247 return str_casecmp (n1->name, n2->name);
1250 static void
1251 sort_key_name_conv_tab (void)
1253 static gboolean has_been_sorted = FALSE;
1255 if (!has_been_sorted) {
1256 memcpy (key_name_conv_tab_sorted, key_name_conv_tab,
1257 sizeof (key_name_conv_tab_sorted));
1258 qsort (key_name_conv_tab_sorted,
1259 key_name_conv_tab_size, sizeof (key_name_conv_tab_sorted[0]),
1260 &key_code_name_comparator);
1261 has_been_sorted = TRUE;
1265 static int
1266 lookup_keyname (const char *keyname, int *lc_index)
1268 if (keyname[0] != '\0') {
1269 const key_code_name_t key = { 0, keyname, NULL, NULL };
1270 key_code_name_t *res;
1272 if (keyname[1] == '\0') {
1273 *lc_index = -1;
1274 return (int) keyname[0];
1277 sort_key_name_conv_tab ();
1279 res = bsearch (&key, key_name_conv_tab_sorted,
1280 key_name_conv_tab_size,
1281 sizeof (key_name_conv_tab_sorted[0]),
1282 key_code_name_comparator);
1284 if (res != NULL) {
1285 *lc_index = (int) (res - key_name_conv_tab_sorted);
1286 return res->code;
1290 *lc_index = -1;
1291 return 0;
1294 /* Return the code associated with the symbolic name keyname */
1295 long
1296 lookup_key (const char *keyname, char **label)
1298 char **lc_keys, **p;
1299 int k = -1;
1300 int key = 0;
1301 int lc_index = -1;
1303 int use_meta = -1;
1304 int use_ctrl = -1;
1305 int use_shift = -1;
1307 if (keyname == NULL)
1308 return 0;
1310 keyname = g_strstrip (g_strdup (keyname));
1311 p = lc_keys = g_strsplit_set (keyname, "-+ ", -1);
1312 g_free ((char *) keyname);
1314 while ((p != NULL) && (*p != NULL)) {
1315 if ((*p)[0] != '\0') {
1316 int idx;
1318 key = lookup_keyname (g_strstrip (*p), &idx);
1320 if (key == KEY_M_ALT)
1321 use_meta = idx;
1322 else if (key == KEY_M_CTRL)
1323 use_ctrl = idx;
1324 else if (key == KEY_M_SHIFT)
1325 use_shift = idx;
1326 else {
1327 k = key;
1328 lc_index = idx;
1329 break;
1333 p++;
1336 g_strfreev (lc_keys);
1338 /* output */
1339 if (k <= 0)
1340 return 0;
1343 if (label != NULL) {
1344 GString *s;
1346 s = g_string_new ("");
1348 if (use_meta != -1) {
1349 g_string_append (s, key_name_conv_tab_sorted[use_meta].shortcut);
1350 g_string_append_c (s, '-');
1352 if (use_ctrl != -1) {
1353 g_string_append (s, key_name_conv_tab_sorted[use_ctrl].shortcut);
1354 g_string_append_c (s, '-');
1356 if (use_shift != -1) {
1357 if (k < 127)
1358 g_string_append_c (s, (gchar) g_ascii_toupper ((gchar) k));
1359 else {
1360 g_string_append (s, key_name_conv_tab_sorted[use_shift].shortcut);
1361 g_string_append_c (s, '-');
1362 g_string_append (s, key_name_conv_tab_sorted[lc_index].shortcut);
1364 } else if (k < 128) {
1365 if ((k >= 'A') || (lc_index < 0)
1366 || (key_name_conv_tab_sorted[lc_index].shortcut == NULL))
1367 g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) k));
1368 else
1369 g_string_append (s, key_name_conv_tab_sorted[lc_index].shortcut);
1370 } else if ((lc_index != -1) && (key_name_conv_tab_sorted[lc_index].shortcut != NULL))
1371 g_string_append (s, key_name_conv_tab_sorted[lc_index].shortcut);
1372 else
1373 g_string_append_c (s, (gchar) g_ascii_tolower ((gchar) key));
1375 *label = g_string_free (s, FALSE);
1378 if (use_shift != -1) {
1379 if (k < 127 && k > 31 )
1380 k = g_ascii_toupper ((gchar) k);
1381 else
1382 k |= KEY_M_SHIFT;
1385 if (use_ctrl != -1) {
1386 if (k < 256)
1387 k = XCTRL (k);
1388 else
1389 k |= KEY_M_CTRL;
1392 if (use_meta != -1)
1393 k = ALT (k);
1395 return (long) k;
1399 * Return TRUE on success, FALSE on error.
1400 * An error happens if SEQ is a beginning of an existing longer sequence.
1402 gboolean
1403 define_sequence (int code, const char *seq, int action)
1405 key_def *base;
1407 if (strlen (seq) > SEQ_BUFFER_LEN - 1)
1408 return FALSE;
1410 for (base = keys; (base != NULL) && (*seq != '\0');)
1411 if (*seq == base->ch) {
1412 if (base->child == 0) {
1413 if (*(seq + 1) != '\0')
1414 base->child = create_sequence (seq + 1, code, action);
1415 else {
1416 /* The sequence matches an existing one. */
1417 base->code = code;
1418 base->action = action;
1420 return TRUE;
1423 base = base->child;
1424 seq++;
1425 } else {
1426 if (base->next)
1427 base = base->next;
1428 else {
1429 base->next = create_sequence (seq, code, action);
1430 return TRUE;
1434 if (*seq == '\0') {
1435 /* Attempt to redefine a sequence with a shorter sequence. */
1436 return FALSE;
1439 keys = create_sequence (seq, code, action);
1440 return TRUE;
1444 * Check if we are idle, i.e. there are no pending keyboard or mouse
1445 * events. Return 1 is idle, 0 is there are pending events.
1447 gboolean
1448 is_idle (void)
1450 int maxfdp;
1451 fd_set select_set;
1452 struct timeval timeout;
1454 FD_ZERO (&select_set);
1455 FD_SET (input_fd, &select_set);
1456 maxfdp = input_fd;
1457 #ifdef HAVE_LIBGPM
1458 if (mouse_enabled && (use_mouse_p == MOUSE_GPM) && (gpm_fd > 0)) {
1459 FD_SET (gpm_fd, &select_set);
1460 maxfdp = max (maxfdp, gpm_fd);
1462 #endif
1463 timeout.tv_sec = 0;
1464 timeout.tv_usec = 0;
1465 return (select (maxfdp + 1, &select_set, 0, 0, &timeout) <= 0);
1469 get_key_code (int no_delay)
1471 int c;
1472 static key_def *this = NULL, *parent;
1473 static struct timeval esctime = { -1, -1 };
1474 static int lastnodelay = -1;
1476 if (no_delay != lastnodelay) {
1477 this = NULL;
1478 lastnodelay = no_delay;
1481 pend_send:
1482 if (pending_keys != NULL) {
1483 int d = *pending_keys++;
1484 check_pend:
1485 if (*pending_keys == 0) {
1486 pending_keys = NULL;
1487 seq_append = NULL;
1489 if ((d == ESC_CHAR) && (pending_keys != NULL)) {
1490 d = ALT (*pending_keys++);
1491 goto check_pend;
1493 if ((d > 127 && d < 256) && use_8th_bit_as_meta)
1494 d = ALT (d & 0x7f);
1495 this = NULL;
1496 return correct_key_code (d);
1499 nodelay_try_again:
1500 if (no_delay)
1501 tty_nodelay (TRUE);
1503 c = tty_lowlevel_getch ();
1504 #if (defined(USE_NCURSES) || defined(USE_NCURSESW)) && defined(KEY_RESIZE)
1505 if (c == KEY_RESIZE)
1506 goto nodelay_try_again;
1507 #endif
1508 if (no_delay) {
1509 tty_nodelay (FALSE);
1510 if (c == -1) {
1511 if (this != NULL && parent != NULL && parent->action == MCKEY_ESCAPE && old_esc_mode) {
1512 struct timeval current, timeout;
1514 if (esctime.tv_sec == -1)
1515 return -1;
1516 GET_TIME (current);
1517 timeout.tv_sec = keyboard_key_timeout / 1000000 + esctime.tv_sec;
1518 timeout.tv_usec = keyboard_key_timeout % 1000000 + esctime.tv_usec;
1519 if (timeout.tv_usec > 1000000) {
1520 timeout.tv_usec -= 1000000;
1521 timeout.tv_sec++;
1523 if (current.tv_sec < timeout.tv_sec)
1524 return -1;
1525 if (current.tv_sec == timeout.tv_sec && current.tv_usec < timeout.tv_usec)
1526 return -1;
1527 this = NULL;
1528 pending_keys = seq_append = NULL;
1529 return ESC_CHAR;
1531 return -1;
1533 } else if (c == -1) {
1534 /* Maybe we got an incomplete match.
1535 This we do only in delay mode, since otherwise
1536 tty_lowlevel_getch can return -1 at any time. */
1537 if (seq_append != NULL) {
1538 pending_keys = seq_buffer;
1539 goto pend_send;
1541 this = NULL;
1542 return -1;
1545 /* Search the key on the root */
1546 if (!no_delay || this == NULL) {
1547 this = keys;
1548 parent = NULL;
1550 if ((c > 127 && c < 256) && use_8th_bit_as_meta) {
1551 c &= 0x7f;
1553 /* The first sequence defined starts with esc */
1554 parent = keys;
1555 this = keys->child;
1558 while (this != NULL) {
1559 if (c == this->ch) {
1560 if (this->child) {
1561 if (!push_char (c)) {
1562 pending_keys = seq_buffer;
1563 goto pend_send;
1565 parent = this;
1566 this = this->child;
1567 if (parent->action == MCKEY_ESCAPE && old_esc_mode) {
1568 if (no_delay) {
1569 GET_TIME (esctime);
1570 if (this == NULL) {
1571 /* Shouldn't happen */
1572 fputs ("Internal error\n", stderr);
1573 exit (1);
1575 goto nodelay_try_again;
1577 esctime.tv_sec = -1;
1578 c = xgetch_second ();
1579 if (c == -1) {
1580 pending_keys = seq_append = NULL;
1581 this = NULL;
1582 return ESC_CHAR;
1584 } else {
1585 if (no_delay)
1586 goto nodelay_try_again;
1587 c = tty_lowlevel_getch ();
1589 } else {
1590 /* We got a complete match, return and reset search */
1591 int code;
1593 pending_keys = seq_append = NULL;
1594 code = this->code;
1595 this = NULL;
1596 return correct_key_code (code);
1598 } else {
1599 if (this->next != NULL)
1600 this = this->next;
1601 else {
1602 if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) {
1603 /* Convert escape-digits to F-keys */
1604 if (g_ascii_isdigit (c))
1605 c = KEY_F (c - '0');
1606 else if (c == ' ')
1607 c = ESC_CHAR;
1608 else
1609 c = ALT (c);
1611 pending_keys = seq_append = NULL;
1612 this = NULL;
1613 return correct_key_code (c);
1615 /* Did not find a match or {c} was changed in the if above,
1616 so we have to return everything we had skipped
1618 push_char (c);
1619 pending_keys = seq_buffer;
1620 goto pend_send;
1624 this = NULL;
1625 return correct_key_code (c);
1628 /* Returns a character read from stdin with appropriate interpretation */
1629 /* Also takes care of generated mouse events */
1630 /* Returns EV_MOUSE if it is a mouse event */
1631 /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
1633 tty_get_event (struct Gpm_Event *event, gboolean redo_event, gboolean block)
1635 int c;
1636 static int flag = 0; /* Return value from select */
1637 #ifdef HAVE_LIBGPM
1638 static struct Gpm_Event ev; /* Mouse event */
1639 #endif
1640 struct timeval timeout;
1641 struct timeval *time_addr = NULL;
1642 static int dirty = 3;
1644 if ((dirty == 3) || is_idle ()) {
1645 mc_refresh ();
1646 dirty = 1;
1647 } else
1648 dirty++;
1650 vfs_timeout_handler ();
1652 /* Ok, we use (event->x < 0) to signal that the event does not contain
1653 a suitable position for the mouse, so we can't use show_mouse_pointer
1654 on it.
1656 if (event->x > 0) {
1657 show_mouse_pointer (event->x, event->y);
1658 if (!redo_event)
1659 event->x = -1;
1662 /* Repeat if using mouse */
1663 while (pending_keys == NULL) {
1664 int maxfdp;
1665 fd_set select_set;
1667 FD_ZERO (&select_set);
1668 FD_SET (input_fd, &select_set);
1669 maxfdp = max (add_selects (&select_set), input_fd);
1671 #ifdef HAVE_LIBGPM
1672 if (mouse_enabled && (use_mouse_p == MOUSE_GPM)) {
1673 if (gpm_fd < 0) {
1674 /* Connection to gpm broken, possibly gpm has died */
1675 mouse_enabled = FALSE;
1676 use_mouse_p = MOUSE_NONE;
1677 break;
1680 FD_SET (gpm_fd, &select_set);
1681 maxfdp = max (maxfdp, gpm_fd);
1683 #endif
1685 if (redo_event) {
1686 timeout.tv_usec = mou_auto_repeat * 1000;
1687 timeout.tv_sec = 0;
1689 time_addr = &timeout;
1690 } else {
1691 int seconds;
1693 seconds = vfs_timeouts ();
1694 time_addr = NULL;
1696 if (seconds != 0) {
1697 /* the timeout could be improved and actually be
1698 * the number of seconds until the next vfs entry
1699 * timeouts in the stamp list.
1702 timeout.tv_sec = seconds;
1703 timeout.tv_usec = 0;
1704 time_addr = &timeout;
1708 if (!block || winch_flag) {
1709 time_addr = &timeout;
1710 timeout.tv_sec = 0;
1711 timeout.tv_usec = 0;
1714 tty_enable_interrupt_key ();
1715 flag = select (maxfdp + 1, &select_set, NULL, NULL, time_addr);
1716 tty_disable_interrupt_key ();
1718 /* select timed out: it could be for any of the following reasons:
1719 * redo_event -> it was because of the MOU_REPEAT handler
1720 * !block -> we did not block in the select call
1721 * else -> 10 second timeout to check the vfs status.
1723 if (flag == 0) {
1724 if (redo_event)
1725 return EV_MOUSE;
1726 if (!block || winch_flag)
1727 return EV_NONE;
1728 vfs_timeout_handler ();
1730 if (flag == -1 && errno == EINTR)
1731 return EV_NONE;
1733 check_selects (&select_set);
1735 if (FD_ISSET (input_fd, &select_set))
1736 break;
1737 #ifdef HAVE_LIBGPM
1738 if (mouse_enabled && use_mouse_p == MOUSE_GPM
1739 && gpm_fd > 0 && FD_ISSET (gpm_fd, &select_set)) {
1740 Gpm_GetEvent (&ev);
1741 Gpm_FitEvent (&ev);
1742 *event = ev;
1743 return EV_MOUSE;
1745 #endif /* !HAVE_LIBGPM */
1748 #ifndef HAVE_SLANG
1749 flag = is_wintouched (stdscr);
1750 untouchwin (stdscr);
1751 #endif /* !HAVE_SLANG */
1752 c = block ? getch_with_delay () : get_key_code (1);
1754 #ifndef HAVE_SLANG
1755 if (flag > 0)
1756 tty_touch_screen ();
1757 #endif /* !HAVE_SLANG */
1759 if (mouse_enabled && (c == MCKEY_MOUSE
1760 #ifdef KEY_MOUSE
1761 || c == KEY_MOUSE
1762 #endif /* KEY_MOUSE */
1763 )) {
1764 /* Mouse event */
1765 xmouse_get_event (event);
1766 return (event->type != 0) ? EV_MOUSE : EV_NONE;
1769 return c;
1772 /* Returns a key press, mouse events are discarded */
1774 tty_getch (void)
1776 Gpm_Event ev;
1777 int key;
1779 ev.x = -1;
1780 while ((key = tty_get_event (&ev, FALSE, TRUE)) == EV_NONE);
1781 return key;
1784 char *
1785 learn_key (void)
1787 /* LEARN_TIMEOUT in usec */
1788 #define LEARN_TIMEOUT 200000
1790 fd_set Read_FD_Set;
1791 struct timeval endtime;
1792 struct timeval timeout;
1793 int c;
1794 char buffer[256];
1795 char *p = buffer;
1797 tty_keypad (FALSE); /* disable intepreting keys by ncurses */
1798 c = tty_lowlevel_getch ();
1799 while (c == -1)
1800 c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
1801 learn_store_key (buffer, &p, c);
1802 GET_TIME (endtime);
1803 endtime.tv_usec += LEARN_TIMEOUT;
1804 if (endtime.tv_usec > 1000000) {
1805 endtime.tv_usec -= 1000000;
1806 endtime.tv_sec++;
1808 tty_nodelay (TRUE);
1809 for (;;) {
1810 while ((c = tty_lowlevel_getch ()) == -1) {
1811 GET_TIME (timeout);
1812 timeout.tv_usec = endtime.tv_usec - timeout.tv_usec;
1813 if (timeout.tv_usec < 0)
1814 timeout.tv_sec++;
1815 timeout.tv_sec = endtime.tv_sec - timeout.tv_sec;
1816 if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) {
1817 FD_ZERO (&Read_FD_Set);
1818 FD_SET (input_fd, &Read_FD_Set);
1819 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
1820 } else
1821 break;
1823 if (c == -1)
1824 break;
1825 learn_store_key (buffer, &p, c);
1827 tty_keypad (TRUE);
1828 tty_nodelay (FALSE);
1829 *p = '\0';
1830 return g_strdup (buffer);
1831 #undef LEARN_TIMEOUT
1834 /* xterm and linux console only: set keypad to numeric or application
1835 mode. Only in application keypad mode it's possible to distinguish
1836 the '+' key and the '+' on the keypad ('*' and '-' ditto)*/
1837 void
1838 numeric_keypad_mode (void)
1840 if (console_flag || xterm_flag) {
1841 fputs ("\033>", stdout);
1842 fflush (stdout);
1846 void
1847 application_keypad_mode (void)
1849 if (console_flag || xterm_flag) {
1850 fputs ("\033=", stdout);
1851 fflush (stdout);