Completely removed MHL stuff
[midnight-commander.git] / src / key.c
blob6d300261241501686c67e24306c9020cd35f6554
1 /* Keyboard support routines.
3 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 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 #include <config.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
35 #include "global.h"
36 #include "tty.h"
37 #include "mouse.h"
38 #include "key.h"
39 #include "layout.h" /* winch_flag */
40 #include "main.h"
41 #include "win.h"
42 #include "cons.saver.h"
44 #ifdef USE_VFS
45 #include "../vfs/gc.h"
46 #endif
48 #ifdef HAVE_TEXTMODE_X11_SUPPORT
49 # include "x11conn.h"
50 #endif
52 #ifdef __linux__
53 # if defined(__GLIBC__) && (__GLIBC__ < 2)
54 # include <linux/termios.h> /* TIOCLINUX */
55 # else
56 # include <termios.h>
57 # endif
58 # include <sys/ioctl.h>
59 #endif /* __linux__ */
61 #ifdef __CYGWIN__
62 # include <termios.h>
63 # include <sys/ioctl.h>
64 #endif /* __CYGWIN__ */
66 #ifdef __QNXNTO__
67 # include <dlfcn.h>
68 # include <Ph.h>
69 # include <sys/dcmd_chr.h>
70 #endif
72 #define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *)NULL))
73 #define DIF_TIME(t1,t2) ((t2.tv_sec -t1.tv_sec) *1000+ \
74 (t2.tv_usec-t1.tv_usec)/1000)
76 /* Linux console keyboard modifiers */
77 #define SHIFT_PRESSED (1 << 0)
78 #define ALTR_PRESSED (1 << 1)
79 #define CONTROL_PRESSED (1 << 2)
80 #define ALTL_PRESSED (1 << 3)
82 int mou_auto_repeat = 100;
83 int double_click_speed = 250;
84 int old_esc_mode = 0;
85 /* timeout for old_esc_mode in usec */
86 int keyboard_key_timeout = 1000000; /* settable via env */
88 int use_8th_bit_as_meta = 0;
90 typedef struct key_def {
91 char ch; /* Holds the matching char code */
92 int code; /* The code returned, valid if child == NULL */
93 struct key_def *next;
94 struct key_def *child; /* sequence continuation */
95 int action; /* optional action to be done. Now used only
96 to mark that we are just after the first
97 Escape */
98 } key_def;
100 /* This holds all the key definitions */
101 static key_def *keys = NULL;
103 static int input_fd;
104 static int disabled_channels = 0; /* Disable channels checking */
105 static int xgetch_second (void);
106 static int get_modifier (void);
108 /* File descriptor monitoring add/remove routines */
109 typedef struct SelectList {
110 int fd;
111 select_fn callback;
112 void *info;
113 struct SelectList *next;
114 } SelectList;
116 #ifdef __QNXNTO__
117 typedef int (*ph_dv_f) (void *, void *);
118 typedef int (*ph_ov_f) (void *);
119 typedef int (*ph_pqc_f) (unsigned short, PhCursorInfo_t *);
120 ph_dv_f ph_attach;
121 ph_ov_f ph_input_group;
122 ph_pqc_f ph_query_cursor;
123 #endif
125 static SelectList *select_list = NULL;
127 void add_select_channel (int fd, select_fn callback, void *info)
129 SelectList *new;
131 new = g_new (SelectList, 1);
132 new->fd = fd;
133 new->callback = callback;
134 new->info = info;
135 new->next = select_list;
136 select_list = new;
139 void delete_select_channel (int fd)
141 SelectList *p = select_list;
142 SelectList *p_prev = NULL;
143 SelectList *p_next;
145 while (p) {
146 if (p->fd == fd) {
147 p_next = p->next;
149 if (p_prev)
150 p_prev->next = p_next;
151 else
152 select_list = p_next;
154 g_free (p);
155 p = p_next;
156 continue;
159 p_prev = p;
160 p = p->next;
164 inline static int add_selects (fd_set *select_set)
166 SelectList *p;
167 int top_fd = 0;
169 if (disabled_channels)
170 return 0;
172 for (p = select_list; p; p = p->next) {
173 FD_SET (p->fd, select_set);
174 if (p->fd > top_fd)
175 top_fd = p->fd;
177 return top_fd;
180 static void check_selects (fd_set *select_set)
182 SelectList *p;
183 gboolean retry;
185 if (disabled_channels)
186 return;
188 do {
189 retry = FALSE;
190 for (p = select_list; p; p = p->next)
191 if (FD_ISSET (p->fd, select_set)) {
192 FD_CLR (p->fd, select_set);
193 (*p->callback)(p->fd, p->info);
194 retry = TRUE;
195 break;
197 } while (retry);
200 void channels_down (void)
202 disabled_channels++;
205 void channels_up (void)
207 if (!disabled_channels)
208 fputs ("Error: channels_up called with disabled_channels = 0\n",
209 stderr);
210 disabled_channels--;
213 typedef const struct {
214 int code;
215 const char *seq;
216 int action;
217 } key_define_t;
219 /* Broken terminfo and termcap databases on xterminals */
220 static key_define_t xterm_key_defines [] = {
221 { KEY_F(1), ESC_STR "OP", MCKEY_NOACTION },
222 { KEY_F(2), ESC_STR "OQ", MCKEY_NOACTION },
223 { KEY_F(3), ESC_STR "OR", MCKEY_NOACTION },
224 { KEY_F(4), ESC_STR "OS", MCKEY_NOACTION },
225 { KEY_F(1), ESC_STR "[11~", MCKEY_NOACTION },
226 { KEY_F(2), ESC_STR "[12~", MCKEY_NOACTION },
227 { KEY_F(3), ESC_STR "[13~", MCKEY_NOACTION },
228 { KEY_F(4), ESC_STR "[14~", MCKEY_NOACTION },
229 { KEY_F(5), ESC_STR "[15~", MCKEY_NOACTION },
230 { KEY_F(6), ESC_STR "[17~", MCKEY_NOACTION },
231 { KEY_F(7), ESC_STR "[18~", MCKEY_NOACTION },
232 { KEY_F(8), ESC_STR "[19~", MCKEY_NOACTION },
233 { KEY_F(9), ESC_STR "[20~", MCKEY_NOACTION },
234 { KEY_F(10), ESC_STR "[21~", MCKEY_NOACTION },
236 /* old xterm Shift-arrows */
237 { KEY_M_SHIFT | KEY_UP, ESC_STR "O2A", MCKEY_NOACTION },
238 { KEY_M_SHIFT | KEY_DOWN, ESC_STR "O2B", MCKEY_NOACTION },
239 { KEY_M_SHIFT | KEY_RIGHT, ESC_STR "O2C", MCKEY_NOACTION },
240 { KEY_M_SHIFT | KEY_LEFT, ESC_STR "O2D", MCKEY_NOACTION },
242 /* new xterm Shift-arrows */
243 { KEY_M_SHIFT | KEY_UP, ESC_STR "[1;2A", MCKEY_NOACTION },
244 { KEY_M_SHIFT | KEY_DOWN, ESC_STR "[1;2B", MCKEY_NOACTION },
245 { KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[1;2C", MCKEY_NOACTION },
246 { KEY_M_SHIFT | KEY_LEFT, ESC_STR "[1;2D", MCKEY_NOACTION },
248 /* more xterm keys with modifiers */
249 { KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5;5~", MCKEY_NOACTION },
250 { KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6;5~", MCKEY_NOACTION },
251 { KEY_M_CTRL | KEY_IC, ESC_STR "[2;5~", MCKEY_NOACTION },
252 { KEY_M_CTRL | KEY_DC, ESC_STR "[3;5~", MCKEY_NOACTION },
253 { KEY_M_CTRL | KEY_HOME, ESC_STR "[1;5H", MCKEY_NOACTION },
254 { KEY_M_CTRL | KEY_END, ESC_STR "[1;5F", MCKEY_NOACTION },
255 { KEY_M_SHIFT | KEY_HOME, ESC_STR "[1;2H", MCKEY_NOACTION },
256 { KEY_M_SHIFT | KEY_END, ESC_STR "[1;2F", MCKEY_NOACTION },
257 { KEY_M_CTRL | KEY_UP, ESC_STR "[1;5A", MCKEY_NOACTION },
258 { KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;5B", MCKEY_NOACTION },
259 { KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;5C", MCKEY_NOACTION },
260 { KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;5D", MCKEY_NOACTION },
261 { KEY_M_SHIFT | KEY_IC, ESC_STR "[2;2~", MCKEY_NOACTION },
262 { KEY_M_SHIFT | KEY_DC, ESC_STR "[3;2~", MCKEY_NOACTION },
263 { KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[1;6A", MCKEY_NOACTION },
264 { KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[1;6B", MCKEY_NOACTION },
265 { KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[1;6C", MCKEY_NOACTION },
266 { KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[1;6D", MCKEY_NOACTION },
268 /* rxvt keys with modifiers */
269 { KEY_M_SHIFT | KEY_UP, ESC_STR "[a", MCKEY_NOACTION },
270 { KEY_M_SHIFT | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION },
271 { KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION },
272 { KEY_M_SHIFT | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION },
273 { KEY_M_CTRL | KEY_UP, ESC_STR "Oa", MCKEY_NOACTION },
274 { KEY_M_CTRL | KEY_DOWN, ESC_STR "Ob", MCKEY_NOACTION },
275 { KEY_M_CTRL | KEY_RIGHT, ESC_STR "Oc", MCKEY_NOACTION },
276 { KEY_M_CTRL | KEY_LEFT, ESC_STR "Od", MCKEY_NOACTION },
277 { KEY_M_CTRL | KEY_PPAGE, ESC_STR "[5^", MCKEY_NOACTION },
278 { KEY_M_CTRL | KEY_NPAGE, ESC_STR "[6^", MCKEY_NOACTION },
279 { KEY_M_CTRL | KEY_HOME, ESC_STR "[7^", MCKEY_NOACTION },
280 { KEY_M_CTRL | KEY_END, ESC_STR "[8^", MCKEY_NOACTION },
281 { KEY_M_SHIFT | KEY_HOME, ESC_STR "[7$", MCKEY_NOACTION },
282 { KEY_M_SHIFT | KEY_END, ESC_STR "[8$", MCKEY_NOACTION },
283 { KEY_M_CTRL | KEY_IC, ESC_STR "[2^", MCKEY_NOACTION },
284 { KEY_M_CTRL | KEY_DC, ESC_STR "[3^", MCKEY_NOACTION },
285 { KEY_M_SHIFT | KEY_DC, ESC_STR "[3$", MCKEY_NOACTION },
287 /* konsole keys with modifiers */
288 { KEY_M_SHIFT | KEY_HOME, ESC_STR "O2H", MCKEY_NOACTION },
289 { KEY_M_SHIFT | KEY_END, ESC_STR "O2F", MCKEY_NOACTION },
291 /* gnome-terminal */
292 { KEY_M_SHIFT | KEY_UP, ESC_STR "[2A", MCKEY_NOACTION },
293 { KEY_M_SHIFT | KEY_DOWN, ESC_STR "[2B", MCKEY_NOACTION },
294 { KEY_M_SHIFT | KEY_RIGHT, ESC_STR "[2C", MCKEY_NOACTION },
295 { KEY_M_SHIFT | KEY_LEFT, ESC_STR "[2D", MCKEY_NOACTION },
296 { KEY_M_CTRL | KEY_UP, ESC_STR "[5A", MCKEY_NOACTION },
297 { KEY_M_CTRL | KEY_DOWN, ESC_STR "[5B", MCKEY_NOACTION },
298 { KEY_M_CTRL | KEY_RIGHT, ESC_STR "[5C", MCKEY_NOACTION },
299 { KEY_M_CTRL | KEY_LEFT, ESC_STR "[5D", MCKEY_NOACTION },
300 { KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "[6A", MCKEY_NOACTION },
301 { KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "[6B", MCKEY_NOACTION },
302 { KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "[6C", MCKEY_NOACTION },
303 { KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "[6D", MCKEY_NOACTION },
305 /* gnome-terminal - application mode */
306 { KEY_M_CTRL | KEY_UP, ESC_STR "O5A", MCKEY_NOACTION },
307 { KEY_M_CTRL | KEY_DOWN, ESC_STR "O5B", MCKEY_NOACTION },
308 { KEY_M_CTRL | KEY_RIGHT, ESC_STR "O5C", MCKEY_NOACTION },
309 { KEY_M_CTRL | KEY_LEFT, ESC_STR "O5D", MCKEY_NOACTION },
310 { KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, ESC_STR "O6A", MCKEY_NOACTION },
311 { KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, ESC_STR "O6B", MCKEY_NOACTION },
312 { KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, ESC_STR "O6C", MCKEY_NOACTION },
313 { KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, ESC_STR "O6D", MCKEY_NOACTION },
315 /* iTerm */
316 { KEY_M_SHIFT | KEY_PPAGE, ESC_STR "[5;2~", MCKEY_NOACTION },
317 { KEY_M_SHIFT | KEY_NPAGE, ESC_STR "[6;2~", MCKEY_NOACTION },
319 /* keypad keys */
320 { KEY_IC, ESC_STR "Op", MCKEY_NOACTION },
321 { KEY_DC, ESC_STR "On", MCKEY_NOACTION },
322 { '/', ESC_STR "Oo", MCKEY_NOACTION },
323 { '\n', ESC_STR "OM", MCKEY_NOACTION },
325 { 0, 0, MCKEY_NOACTION },
328 /* qansi-m terminals have a much more key combinatios,
329 which are undefined in termcap/terminfo */
330 static key_define_t qansi_key_defines[] =
332 /* qansi-m terminal */
333 {KEY_M_CTRL | KEY_NPAGE, ESC_STR "[u", MCKEY_NOACTION}, /* Ctrl-PgDown */
334 {KEY_M_CTRL | KEY_PPAGE, ESC_STR "[v", MCKEY_NOACTION}, /* Ctrl-PgUp */
335 {KEY_M_CTRL | KEY_HOME, ESC_STR "[h", MCKEY_NOACTION}, /* Ctrl-Home */
336 {KEY_M_CTRL | KEY_END, ESC_STR "[y", MCKEY_NOACTION}, /* Ctrl-End */
337 {KEY_M_CTRL | KEY_IC, ESC_STR "[`", MCKEY_NOACTION}, /* Ctrl-Insert */
338 {KEY_M_CTRL | KEY_DC, ESC_STR "[p", MCKEY_NOACTION}, /* Ctrl-Delete */
339 {KEY_M_CTRL | KEY_LEFT, ESC_STR "[d", MCKEY_NOACTION}, /* Ctrl-Left */
340 {KEY_M_CTRL | KEY_RIGHT, ESC_STR "[c", MCKEY_NOACTION}, /* Ctrl-Right */
341 {KEY_M_CTRL | KEY_DOWN, ESC_STR "[b", MCKEY_NOACTION}, /* Ctrl-Down */
342 {KEY_M_CTRL | KEY_UP, ESC_STR "[a", MCKEY_NOACTION}, /* Ctrl-Up */
343 {KEY_M_CTRL | KEY_KP_ADD, ESC_STR "[s", MCKEY_NOACTION}, /* Ctrl-Gr-Plus */
344 {KEY_M_CTRL | KEY_KP_SUBTRACT, ESC_STR "[t", MCKEY_NOACTION}, /* Ctrl-Gr-Minus */
345 {KEY_M_CTRL | '\t', ESC_STR "[z", MCKEY_NOACTION}, /* Ctrl-Tab */
346 {KEY_M_SHIFT | '\t', ESC_STR "[Z", MCKEY_NOACTION}, /* Shift-Tab */
347 {KEY_M_CTRL | KEY_F(1), ESC_STR "[1~", MCKEY_NOACTION}, /* Ctrl-F1 */
348 {KEY_M_CTRL | KEY_F(2), ESC_STR "[2~", MCKEY_NOACTION}, /* Ctrl-F2 */
349 {KEY_M_CTRL | KEY_F(3), ESC_STR "[3~", MCKEY_NOACTION}, /* Ctrl-F3 */
350 {KEY_M_CTRL | KEY_F(4), ESC_STR "[4~", MCKEY_NOACTION}, /* Ctrl-F4 */
351 {KEY_M_CTRL | KEY_F(5), ESC_STR "[5~", MCKEY_NOACTION}, /* Ctrl-F5 */
352 {KEY_M_CTRL | KEY_F(6), ESC_STR "[6~", MCKEY_NOACTION}, /* Ctrl-F6 */
353 {KEY_M_CTRL | KEY_F(7), ESC_STR "[7~", MCKEY_NOACTION}, /* Ctrl-F7 */
354 {KEY_M_CTRL | KEY_F(8), ESC_STR "[8~", MCKEY_NOACTION}, /* Ctrl-F8 */
355 {KEY_M_CTRL | KEY_F(9), ESC_STR "[9~", MCKEY_NOACTION}, /* Ctrl-F9 */
356 {KEY_M_CTRL | KEY_F(10), ESC_STR "[10~", MCKEY_NOACTION}, /* Ctrl-F10 */
357 {KEY_M_CTRL | KEY_F(11), ESC_STR "[11~", MCKEY_NOACTION}, /* Ctrl-F11 */
358 {KEY_M_CTRL | KEY_F(12), ESC_STR "[12~", MCKEY_NOACTION}, /* Ctrl-F12 */
359 {KEY_M_ALT | KEY_F(1), ESC_STR "[17~", MCKEY_NOACTION}, /* Alt-F1 */
360 {KEY_M_ALT | KEY_F(2), ESC_STR "[18~", MCKEY_NOACTION}, /* Alt-F2 */
361 {KEY_M_ALT | KEY_F(3), ESC_STR "[19~", MCKEY_NOACTION}, /* Alt-F3 */
362 {KEY_M_ALT | KEY_F(4), ESC_STR "[20~", MCKEY_NOACTION}, /* Alt-F4 */
363 {KEY_M_ALT | KEY_F(5), ESC_STR "[21~", MCKEY_NOACTION}, /* Alt-F5 */
364 {KEY_M_ALT | KEY_F(6), ESC_STR "[22~", MCKEY_NOACTION}, /* Alt-F6 */
365 {KEY_M_ALT | KEY_F(7), ESC_STR "[23~", MCKEY_NOACTION}, /* Alt-F7 */
366 {KEY_M_ALT | KEY_F(8), ESC_STR "[24~", MCKEY_NOACTION}, /* Alt-F8 */
367 {KEY_M_ALT | KEY_F(9), ESC_STR "[25~", MCKEY_NOACTION}, /* Alt-F9 */
368 {KEY_M_ALT | KEY_F(10), ESC_STR "[26~", MCKEY_NOACTION}, /* Alt-F10 */
369 {KEY_M_ALT | KEY_F(11), ESC_STR "[27~", MCKEY_NOACTION}, /* Alt-F11 */
370 {KEY_M_ALT | KEY_F(12), ESC_STR "[28~", MCKEY_NOACTION}, /* Alt-F12 */
371 {KEY_M_ALT | 'a', ESC_STR "Na", MCKEY_NOACTION}, /* Alt-a */
372 {KEY_M_ALT | 'b', ESC_STR "Nb", MCKEY_NOACTION}, /* Alt-b */
373 {KEY_M_ALT | 'c', ESC_STR "Nc", MCKEY_NOACTION}, /* Alt-c */
374 {KEY_M_ALT | 'd', ESC_STR "Nd", MCKEY_NOACTION}, /* Alt-d */
375 {KEY_M_ALT | 'e', ESC_STR "Ne", MCKEY_NOACTION}, /* Alt-e */
376 {KEY_M_ALT | 'f', ESC_STR "Nf", MCKEY_NOACTION}, /* Alt-f */
377 {KEY_M_ALT | 'g', ESC_STR "Ng", MCKEY_NOACTION}, /* Alt-g */
378 {KEY_M_ALT | 'i', ESC_STR "Ni", MCKEY_NOACTION}, /* Alt-i */
379 {KEY_M_ALT | 'j', ESC_STR "Nj", MCKEY_NOACTION}, /* Alt-j */
380 {KEY_M_ALT | 'k', ESC_STR "Nk", MCKEY_NOACTION}, /* Alt-k */
381 {KEY_M_ALT | 'l', ESC_STR "Nl", MCKEY_NOACTION}, /* Alt-l */
382 {KEY_M_ALT | 'm', ESC_STR "Nm", MCKEY_NOACTION}, /* Alt-m */
383 {KEY_M_ALT | 'n', ESC_STR "Nn", MCKEY_NOACTION}, /* Alt-n */
384 {KEY_M_ALT | 'o', ESC_STR "No", MCKEY_NOACTION}, /* Alt-o */
385 {KEY_M_ALT | 'p', ESC_STR "Np", MCKEY_NOACTION}, /* Alt-p */
386 {KEY_M_ALT | 'q', ESC_STR "Nq", MCKEY_NOACTION}, /* Alt-r */
387 {KEY_M_ALT | 's', ESC_STR "Ns", MCKEY_NOACTION}, /* Alt-s */
388 {KEY_M_ALT | 't', ESC_STR "Nt", MCKEY_NOACTION}, /* Alt-t */
389 {KEY_M_ALT | 'u', ESC_STR "Nu", MCKEY_NOACTION}, /* Alt-u */
390 {KEY_M_ALT | 'v', ESC_STR "Nv", MCKEY_NOACTION}, /* Alt-v */
391 {KEY_M_ALT | 'w', ESC_STR "Nw", MCKEY_NOACTION}, /* Alt-w */
392 {KEY_M_ALT | 'x', ESC_STR "Nx", MCKEY_NOACTION}, /* Alt-x */
393 {KEY_M_ALT | 'y', ESC_STR "Ny", MCKEY_NOACTION}, /* Alt-y */
394 {KEY_M_ALT | 'z', ESC_STR "Nz", MCKEY_NOACTION}, /* Alt-z */
395 {KEY_KP_SUBTRACT, ESC_STR "[S", MCKEY_NOACTION}, /* Gr-Minus */
396 {KEY_KP_ADD, ESC_STR "[T", MCKEY_NOACTION}, /* Gr-Plus */
397 {0, NULL, MCKEY_NOACTION},
400 static key_define_t mc_default_keys [] = {
401 { ESC_CHAR, ESC_STR, MCKEY_ESCAPE },
402 { ESC_CHAR, ESC_STR ESC_STR, MCKEY_NOACTION },
403 { 0, NULL, MCKEY_NOACTION },
406 static void
407 define_sequences (key_define_t *kd)
409 int i;
411 for (i = 0; kd[i].code != 0; i++)
412 define_sequence(kd[i].code, kd[i].seq, kd[i].action);
415 #ifdef HAVE_TEXTMODE_X11_SUPPORT
417 static Display *x11_display;
418 static Window x11_window;
420 static void
421 init_key_x11 (void)
423 if (!getenv ("DISPLAY"))
424 return;
426 x11_display = mc_XOpenDisplay (0);
428 if (x11_display)
429 x11_window = DefaultRootWindow (x11_display);
431 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
434 /* This has to be called before slang_init or whatever routine
435 calls any define_sequence */
436 void
437 init_key (void)
439 const char *term = getenv ("TERM");
440 char *kt = getenv ("KEYBOARD_KEY_TIMEOUT_US");
441 if (kt != NULL)
442 keyboard_key_timeout = atoi (kt);
444 /* This has to be the first define_sequence */
445 /* So, we can assume that the first keys member has ESC */
446 define_sequences (mc_default_keys);
448 /* Terminfo on irix does not have some keys */
449 if (xterm_flag
450 || (term != NULL
451 && (strncmp (term, "iris-ansi", 9) == 0
452 || strncmp (term, "xterm", 5) == 0
453 || strncmp (term, "rxvt", 4) == 0
454 || strcmp (term, "screen") == 0)))
455 define_sequences (xterm_key_defines);
457 /* load some additional keys (e.g. direct Alt-? support) */
458 load_xtra_key_defines ();
460 #ifdef __QNX__
461 if (term && strncmp (term, "qnx", 3) == 0) {
462 /* Modify the default value of use_8th_bit_as_meta: we would
463 * like to provide a working mc for a newbie who knows nothing
464 * about [Options|Display bits|Full 8 bits input]...
466 * Don't use 'meta'-bit, when we are dealing with a
467 * 'qnx*'-type terminal: clear the default value!
468 * These terminal types use 0xFF as an escape character,
469 * so use_8th_bit_as_meta==1 must not be enabled!
471 * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
472 * is not used now (doesn't even depend on use_8th_bit_as_meta
473 * as in mc-3.1.2)...GREAT!...no additional code is required!]
475 use_8th_bit_as_meta = 0;
477 #endif /* __QNX__ */
479 #ifdef HAVE_TEXTMODE_X11_SUPPORT
480 init_key_x11 ();
481 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
483 /* Load the qansi-m key definitions
484 if we are running under the qansi-m terminal */
485 if (term != NULL && (strncmp (term, "qansi-m", 7) == 0)) {
486 define_sequences (qansi_key_defines);
490 /* This has to be called after SLang_init_tty/slint_init */
491 void init_key_input_fd (void)
493 #ifdef HAVE_SLANG
494 input_fd = SLang_TT_Read_FD;
495 #endif
499 static void
500 xmouse_get_event (Gpm_Event *ev)
502 int btn;
503 static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
504 static struct timeval tv2;
505 static int clicks;
506 static int last_btn = 0;
508 /* Decode Xterm mouse information to a GPM style event */
510 /* Variable btn has following meaning: */
511 /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
512 btn = getch () - 32;
514 /* There seems to be no way of knowing which button was released */
515 /* So we assume all the buttons were released */
517 if (btn == 3) {
518 if (last_btn) {
519 ev->type = GPM_UP | (GPM_SINGLE << clicks);
520 ev->buttons = 0;
521 last_btn = 0;
522 GET_TIME (tv1);
523 clicks = 0;
524 } else {
525 /* Bogus event, maybe mouse wheel */
526 ev->type = 0;
528 } else {
529 if (btn >= 32 && btn <= 34) {
530 btn -= 32;
531 ev->type = GPM_DRAG;
532 } else
533 ev->type = GPM_DOWN;
535 GET_TIME (tv2);
536 if (tv1.tv_sec && (DIF_TIME (tv1,tv2) < double_click_speed)) {
537 clicks++;
538 clicks %= 3;
539 } else
540 clicks = 0;
542 switch (btn) {
543 case 0:
544 ev->buttons = GPM_B_LEFT;
545 break;
546 case 1:
547 ev->buttons = GPM_B_MIDDLE;
548 break;
549 case 2:
550 ev->buttons = GPM_B_RIGHT;
551 break;
552 case 64:
553 ev->buttons = GPM_B_UP;
554 clicks = 0;
555 break;
556 case 65:
557 ev->buttons = GPM_B_DOWN;
558 clicks = 0;
559 break;
560 default:
561 /* Nothing */
562 ev->type = 0;
563 ev->buttons = 0;
564 break;
566 last_btn = ev->buttons;
568 /* Coordinates are 33-based */
569 /* Transform them to 1-based */
570 ev->x = getch () - 32;
571 ev->y = getch () - 32;
574 static key_def *create_sequence (const char *seq, int code, int action)
576 key_def *base, *p, *attach;
578 for (base = attach = NULL; *seq; seq++) {
579 p = g_new (key_def, 1);
580 if (!base) base = p;
581 if (attach) attach->child = p;
583 p->ch = *seq;
584 p->code = code;
585 p->child = p->next = NULL;
586 if (!seq[1])
587 p->action = action;
588 else
589 p->action = MCKEY_NOACTION;
590 attach = p;
592 return base;
595 /* The maximum sequence length (32 + null terminator) */
596 #define SEQ_BUFFER_LEN 33
597 static int seq_buffer [SEQ_BUFFER_LEN];
598 static int *seq_append = 0;
600 static int push_char (int c)
602 if (!seq_append)
603 seq_append = seq_buffer;
605 if (seq_append == &(seq_buffer [SEQ_BUFFER_LEN-2]))
606 return 0;
607 *(seq_append++) = c;
608 *seq_append = 0;
609 return 1;
613 * Return 1 on success, 0 on error.
614 * An error happens if SEQ is a beginning of an existing longer sequence.
616 int define_sequence (int code, const char *seq, int action)
618 key_def *base;
620 if (strlen (seq) > SEQ_BUFFER_LEN-1)
621 return 0;
623 for (base = keys; (base != 0) && *seq; ) {
624 if (*seq == base->ch) {
625 if (base->child == 0) {
626 if (*(seq+1)) {
627 base->child = create_sequence (seq+1, code, action);
628 return 1;
629 } else {
630 /* The sequence matches an existing one. */
631 base->code = code;
632 base->action = action;
633 return 1;
635 } else {
636 base = base->child;
637 seq++;
639 } else {
640 if (base->next)
641 base = base->next;
642 else {
643 base->next = create_sequence (seq, code, action);
644 return 1;
649 if (!*seq) {
650 /* Attempt to redefine a sequence with a shorter sequence. */
651 return 0;
654 keys = create_sequence (seq, code, action);
655 return 1;
658 static int *pending_keys;
660 /* Apply corrections for the keycode generated in get_key_code() */
661 static int
662 correct_key_code (int code)
664 unsigned int c = code & ~KEY_M_MASK; /* code without modifier */
665 unsigned int mod = code & KEY_M_MASK; /* modifier */
666 #ifdef __QNXNTO__
667 unsigned int qmod; /* bunch of the QNX console
668 modifiers needs unchanged */
669 #endif /* __QNXNTO__ */
672 * Add key modifiers directly from X11 or OS.
673 * Ordinary characters only get modifiers from sequences.
675 if (c < 32 || c >= 256) {
676 mod |= get_modifier ();
679 /* This is needed if the newline is reported as carriage return */
680 if (c == '\r')
681 c = '\n';
683 /* This is reported to be useful on AIX */
684 if (c == KEY_SCANCEL)
685 c = '\t';
687 /* Convert Shift+Tab and Ctrl+Tab to Back Tab */
688 if ((c == '\t') && (mod & (KEY_M_SHIFT | KEY_M_CTRL))) {
689 c = KEY_BTAB;
690 mod = 0;
693 /* F0 is the same as F10 for out purposes */
694 if (c == KEY_F (0))
695 c = KEY_F (10);
698 * We are not interested if Ctrl was pressed when entering control
699 * characters, so assume that it was. When checking for such keys,
700 * XCTRL macro should be used. In some cases, we are interested,
701 * e.g. to distinguish Ctrl-Enter from Enter.
703 if (c < 32 && c != ESC_CHAR && c != '\t' && c != '\n') {
704 mod |= KEY_M_CTRL;
707 #ifdef __QNXNTO__
708 qmod=get_modifier();
710 if ((c == 127) && (mod==0)) /* Add Ctrl/Alt/Shift-BackSpace */
712 mod |= get_modifier();
713 c = KEY_BACKSPACE;
716 if ((c=='0') && (mod==0)) /* Add Shift-Insert on key pad */
718 if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
720 mod = KEY_M_SHIFT;
721 c = KEY_IC;
725 if ((c=='.') && (mod==0)) /* Add Shift-Del on key pad */
727 if ((qmod & KEY_M_SHIFT) == KEY_M_SHIFT)
729 mod = KEY_M_SHIFT;
730 c = KEY_DC;
733 #endif /* __QNXNTO__ */
735 /* Unrecognized 0177 is delete (preserve Ctrl) */
736 if (c == 0177) {
737 c = KEY_BACKSPACE;
740 /* Unrecognized Ctrl-d is delete */
741 if (c == (31 & 'd')) {
742 c = KEY_DC;
743 mod &= ~KEY_M_CTRL;
746 /* Unrecognized Ctrl-h is backspace */
747 if (c == (31 & 'h')) {
748 c = KEY_BACKSPACE;
749 mod &= ~KEY_M_CTRL;
752 /* Shift+BackSpace is backspace */
753 if (c == KEY_BACKSPACE && (mod & KEY_M_SHIFT)) {
754 mod &= ~KEY_M_SHIFT;
757 /* Convert Shift+Fn to F(n+10) */
758 if (c >= KEY_F (1) && c <= KEY_F (10) && (mod & KEY_M_SHIFT)) {
759 c += 10;
762 /* Remove Shift information from function keys */
763 if (c >= KEY_F (1) && c <= KEY_F (20)) {
764 mod &= ~KEY_M_SHIFT;
767 if (!alternate_plus_minus)
768 switch (c) {
769 case KEY_KP_ADD:
770 c = '+';
771 break;
772 case KEY_KP_SUBTRACT:
773 c = '-';
774 break;
775 case KEY_KP_MULTIPLY:
776 c = '*';
777 break;
780 return (mod | c);
783 int get_key_code (int no_delay)
785 int c;
786 static key_def *this = NULL, *parent;
787 static struct timeval esctime = { -1, -1 };
788 static int lastnodelay = -1;
790 if (no_delay != lastnodelay) {
791 this = NULL;
792 lastnodelay = no_delay;
795 pend_send:
796 if (pending_keys) {
797 int d = *pending_keys++;
798 check_pend:
799 if (!*pending_keys) {
800 pending_keys = 0;
801 seq_append = 0;
803 if (d == ESC_CHAR && pending_keys) {
804 d = ALT(*pending_keys++);
805 goto check_pend;
807 if ((d > 127 && d < 256) && use_8th_bit_as_meta)
808 d = ALT(d & 0x7f);
809 this = NULL;
810 return correct_key_code (d);
813 nodelay_try_again:
814 if (no_delay) {
815 nodelay (stdscr, TRUE);
817 c = getch ();
818 #if defined(USE_NCURSES) && defined(KEY_RESIZE)
819 if (c == KEY_RESIZE)
820 goto nodelay_try_again;
821 #endif
822 if (no_delay) {
823 nodelay (stdscr, FALSE);
824 if (c == -1) {
825 if (this != NULL && parent != NULL &&
826 parent->action == MCKEY_ESCAPE && old_esc_mode) {
827 struct timeval current, timeout;
829 if (esctime.tv_sec == -1)
830 return -1;
831 GET_TIME (current);
832 timeout.tv_sec = keyboard_key_timeout / 1000000 + esctime.tv_sec;
833 timeout.tv_usec = keyboard_key_timeout % 1000000 + esctime.tv_usec;
834 if (timeout.tv_usec > 1000000) {
835 timeout.tv_usec -= 1000000;
836 timeout.tv_sec++;
838 if (current.tv_sec < timeout.tv_sec)
839 return -1;
840 if (current.tv_sec == timeout.tv_sec &&
841 current.tv_usec < timeout.tv_usec)
842 return -1;
843 this = NULL;
844 pending_keys = seq_append = NULL;
845 return ESC_CHAR;
847 return -1;
849 } else if (c == -1) {
850 /* Maybe we got an incomplete match.
851 This we do only in delay mode, since otherwise
852 getch can return -1 at any time. */
853 if (seq_append) {
854 pending_keys = seq_buffer;
855 goto pend_send;
857 this = NULL;
858 return -1;
861 /* Search the key on the root */
862 if (!no_delay || this == NULL) {
863 this = keys;
864 parent = NULL;
866 if ((c > 127 && c < 256) && use_8th_bit_as_meta) {
867 c &= 0x7f;
869 /* The first sequence defined starts with esc */
870 parent = keys;
871 this = keys->child;
874 while (this) {
875 if (c == this->ch) {
876 if (this->child) {
877 if (!push_char (c)) {
878 pending_keys = seq_buffer;
879 goto pend_send;
881 parent = this;
882 this = this->child;
883 if (parent->action == MCKEY_ESCAPE && old_esc_mode) {
884 if (no_delay) {
885 GET_TIME (esctime);
886 if (this == NULL) {
887 /* Shouldn't happen */
888 fputs ("Internal error\n", stderr);
889 exit (1);
891 goto nodelay_try_again;
893 esctime.tv_sec = -1;
894 c = xgetch_second ();
895 if (c == -1) {
896 pending_keys = seq_append = NULL;
897 this = NULL;
898 return ESC_CHAR;
900 } else {
901 if (no_delay)
902 goto nodelay_try_again;
903 c = getch ();
905 } else {
906 /* We got a complete match, return and reset search */
907 int code;
909 pending_keys = seq_append = NULL;
910 code = this->code;
911 this = NULL;
912 return correct_key_code (code);
914 } else {
915 if (this->next)
916 this = this->next;
917 else {
918 if (parent != NULL && parent->action == MCKEY_ESCAPE) {
920 /* Convert escape-digits to F-keys */
921 if (isdigit(c))
922 c = KEY_F (c - '0');
923 else if (c == ' ')
924 c = ESC_CHAR;
925 else
926 c = ALT(c);
928 pending_keys = seq_append = NULL;
929 this = NULL;
930 return correct_key_code (c);
932 /* Did not find a match or {c} was changed in the if above,
933 so we have to return everything we had skipped
935 push_char (c);
936 pending_keys = seq_buffer;
937 goto pend_send;
941 this = NULL;
942 return correct_key_code (c);
945 /* If set timeout is set, then we wait 0.1 seconds, else, we block */
946 static void
947 try_channels (int set_timeout)
949 struct timeval timeout;
950 static fd_set select_set;
951 struct timeval *timeptr;
952 int v;
953 int maxfdp;
955 while (1) {
956 FD_ZERO (&select_set);
957 FD_SET (input_fd, &select_set); /* Add stdin */
958 maxfdp = max (add_selects (&select_set), input_fd);
960 if (set_timeout) {
961 timeout.tv_sec = 0;
962 timeout.tv_usec = 100000;
963 timeptr = &timeout;
964 } else
965 timeptr = 0;
967 v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
968 if (v > 0) {
969 check_selects (&select_set);
970 if (FD_ISSET (input_fd, &select_set))
971 return;
976 /* Workaround for System V Curses vt100 bug */
977 static int getch_with_delay (void)
979 int c;
981 /* This routine could be used on systems without mouse support,
982 so we need to do the select check :-( */
983 while (1) {
984 if (!pending_keys)
985 try_channels (0);
987 /* Try to get a character */
988 c = get_key_code (0);
989 if (c != -1)
990 break;
991 /* Failed -> wait 0.1 secs and try again */
992 try_channels (1);
994 /* Success -> return the character */
995 return c;
998 /* Returns a character read from stdin with appropriate interpretation */
999 /* Also takes care of generated mouse events */
1000 /* Returns EV_MOUSE if it is a mouse event */
1001 /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
1003 get_event (struct Gpm_Event *event, int redo_event, int block)
1005 int c;
1006 static int flag; /* Return value from select */
1007 #ifdef HAVE_LIBGPM
1008 static struct Gpm_Event ev; /* Mouse event */
1009 #endif
1010 struct timeval timeout;
1011 struct timeval *time_addr = NULL;
1012 static int dirty = 3;
1014 if ((dirty == 3) || is_idle ()) {
1015 mc_refresh ();
1016 doupdate ();
1017 dirty = 1;
1018 } else
1019 dirty++;
1021 vfs_timeout_handler ();
1023 /* Ok, we use (event->x < 0) to signal that the event does not contain
1024 a suitable position for the mouse, so we can't use show_mouse_pointer
1025 on it.
1027 if (event->x > 0) {
1028 show_mouse_pointer (event->x, event->y);
1029 if (!redo_event)
1030 event->x = -1;
1033 /* Repeat if using mouse */
1034 while (mouse_enabled && !pending_keys) {
1035 int maxfdp;
1036 fd_set select_set;
1038 FD_ZERO (&select_set);
1039 FD_SET (input_fd, &select_set);
1040 maxfdp = max (add_selects (&select_set), input_fd);
1042 #ifdef HAVE_LIBGPM
1043 if (use_mouse_p == MOUSE_GPM) {
1044 if (gpm_fd < 0) {
1045 /* Connection to gpm broken, possibly gpm has died */
1046 mouse_enabled = 0;
1047 use_mouse_p = MOUSE_NONE;
1048 break;
1049 } else {
1050 FD_SET (gpm_fd, &select_set);
1051 maxfdp = max (maxfdp, gpm_fd);
1054 #endif
1056 if (redo_event) {
1057 timeout.tv_usec = mou_auto_repeat * 1000;
1058 timeout.tv_sec = 0;
1060 time_addr = &timeout;
1061 } else {
1062 int seconds;
1064 if ((seconds = vfs_timeouts ())) {
1065 /* the timeout could be improved and actually be
1066 * the number of seconds until the next vfs entry
1067 * timeouts in the stamp list.
1070 timeout.tv_sec = seconds;
1071 timeout.tv_usec = 0;
1072 time_addr = &timeout;
1073 } else
1074 time_addr = NULL;
1077 if (!block || winch_flag) {
1078 time_addr = &timeout;
1079 timeout.tv_sec = 0;
1080 timeout.tv_usec = 0;
1082 enable_interrupt_key ();
1083 flag = select (maxfdp + 1, &select_set, NULL, NULL, time_addr);
1084 disable_interrupt_key ();
1086 /* select timed out: it could be for any of the following reasons:
1087 * redo_event -> it was because of the MOU_REPEAT handler
1088 * !block -> we did not block in the select call
1089 * else -> 10 second timeout to check the vfs status.
1091 if (flag == 0) {
1092 if (redo_event)
1093 return EV_MOUSE;
1094 if (!block || winch_flag)
1095 return EV_NONE;
1096 vfs_timeout_handler ();
1098 if (flag == -1 && errno == EINTR)
1099 return EV_NONE;
1101 check_selects (&select_set);
1103 if (FD_ISSET (input_fd, &select_set))
1104 break;
1105 #ifdef HAVE_LIBGPM
1106 if (use_mouse_p == MOUSE_GPM && gpm_fd > 0
1107 && FD_ISSET (gpm_fd, &select_set)) {
1108 Gpm_GetEvent (&ev);
1109 Gpm_FitEvent (&ev);
1110 *event = ev;
1111 return EV_MOUSE;
1113 #endif /* !HAVE_LIBGPM */
1115 #ifndef HAVE_SLANG
1116 flag = is_wintouched (stdscr);
1117 untouchwin (stdscr);
1118 #endif /* !HAVE_SLANG */
1119 c = block ? getch_with_delay () : get_key_code (1);
1121 #ifndef HAVE_SLANG
1122 if (flag)
1123 touchwin (stdscr);
1124 #endif /* !HAVE_SLANG */
1126 if (c == MCKEY_MOUSE
1127 #ifdef KEY_MOUSE
1128 || c == KEY_MOUSE
1129 #endif /* KEY_MOUSE */
1131 /* Mouse event */
1132 xmouse_get_event (event);
1133 if (event->type)
1134 return EV_MOUSE;
1135 else
1136 return EV_NONE;
1139 return c;
1142 /* Returns a key press, mouse events are discarded */
1143 int mi_getch ()
1145 Gpm_Event ev;
1146 int key;
1148 ev.x = -1;
1149 while ((key = get_event (&ev, 0, 1)) == EV_NONE)
1151 return key;
1154 static int xgetch_second (void)
1156 fd_set Read_FD_Set;
1157 int c;
1158 struct timeval timeout;
1160 timeout.tv_sec = keyboard_key_timeout / 1000000;
1161 timeout.tv_usec = keyboard_key_timeout % 1000000;
1162 nodelay (stdscr, TRUE);
1163 FD_ZERO (&Read_FD_Set);
1164 FD_SET (input_fd, &Read_FD_Set);
1165 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
1166 c = getch ();
1167 nodelay (stdscr, FALSE);
1168 return c;
1171 static void
1172 learn_store_key (char *buffer, char **p, int c)
1174 if (*p - buffer > 253)
1175 return;
1176 if (c == ESC_CHAR) {
1177 *(*p)++ = '\\';
1178 *(*p)++ = 'e';
1179 } else if (c < ' ') {
1180 *(*p)++ = '^';
1181 *(*p)++ = c + 'a' - 1;
1182 } else if (c == '^') {
1183 *(*p)++ = '^';
1184 *(*p)++ = '^';
1185 } else
1186 *(*p)++ = (char) c;
1189 char *learn_key (void)
1191 /* LEARN_TIMEOUT in usec */
1192 #define LEARN_TIMEOUT 200000
1194 fd_set Read_FD_Set;
1195 struct timeval endtime;
1196 struct timeval timeout;
1197 int c;
1198 char buffer [256];
1199 char *p = buffer;
1201 keypad(stdscr, FALSE); /* disable intepreting keys by ncurses */
1202 c = getch ();
1203 while (c == -1)
1204 c = getch (); /* Sanity check, should be unnecessary */
1205 learn_store_key (buffer, &p, c);
1206 GET_TIME (endtime);
1207 endtime.tv_usec += LEARN_TIMEOUT;
1208 if (endtime.tv_usec > 1000000) {
1209 endtime.tv_usec -= 1000000;
1210 endtime.tv_sec++;
1212 nodelay (stdscr, TRUE);
1213 for (;;) {
1214 while ((c = getch ()) == -1) {
1215 GET_TIME (timeout);
1216 timeout.tv_usec = endtime.tv_usec - timeout.tv_usec;
1217 if (timeout.tv_usec < 0)
1218 timeout.tv_sec++;
1219 timeout.tv_sec = endtime.tv_sec - timeout.tv_sec;
1220 if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) {
1221 FD_ZERO (&Read_FD_Set);
1222 FD_SET (input_fd, &Read_FD_Set);
1223 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
1224 } else
1225 break;
1227 if (c == -1)
1228 break;
1229 learn_store_key (buffer, &p, c);
1231 keypad(stdscr, TRUE);
1232 nodelay (stdscr, FALSE);
1233 *p = 0;
1234 return g_strdup (buffer);
1237 /* xterm and linux console only: set keypad to numeric or application
1238 mode. Only in application keypad mode it's possible to distinguish
1239 the '+' key and the '+' on the keypad ('*' and '-' ditto)*/
1240 void
1241 numeric_keypad_mode (void)
1243 if (console_flag || xterm_flag) {
1244 fputs ("\033>", stdout);
1245 fflush (stdout);
1249 void
1250 application_keypad_mode (void)
1252 if (console_flag || xterm_flag) {
1253 fputs ("\033=", stdout);
1254 fflush (stdout);
1260 * Check if we are idle, i.e. there are no pending keyboard or mouse
1261 * events. Return 1 is idle, 0 is there are pending events.
1264 is_idle (void)
1266 int maxfdp;
1267 fd_set select_set;
1268 struct timeval timeout;
1270 FD_ZERO (&select_set);
1271 FD_SET (input_fd, &select_set);
1272 maxfdp = input_fd;
1273 #ifdef HAVE_LIBGPM
1274 if (use_mouse_p == MOUSE_GPM && mouse_enabled && gpm_fd > 0) {
1275 FD_SET (gpm_fd, &select_set);
1276 maxfdp = max (maxfdp, gpm_fd);
1278 #endif
1279 timeout.tv_sec = 0;
1280 timeout.tv_usec = 0;
1281 return (select (maxfdp + 1, &select_set, 0, 0, &timeout) <= 0);
1286 * Get modifier state (shift, alt, ctrl) for the last key pressed.
1287 * We are assuming that the state didn't change since the key press.
1288 * This is only correct if get_modifier() is called very fast after
1289 * the input was received, so that the user didn't release the
1290 * modifier keys yet.
1292 static int
1293 get_modifier (void)
1295 int result = 0;
1296 #ifdef __QNXNTO__
1297 int mod_status, shift_ext_status;
1298 static int in_photon = 0;
1299 static int ph_ig = 0;
1300 PhCursorInfo_t cursor_info;
1301 #endif /* __QNXNTO__ */
1303 #ifdef HAVE_TEXTMODE_X11_SUPPORT
1304 if (x11_window) {
1305 Window root, child;
1306 int root_x, root_y;
1307 int win_x, win_y;
1308 unsigned int mask;
1310 mc_XQueryPointer (x11_display, x11_window, &root, &child, &root_x,
1311 &root_y, &win_x, &win_y, &mask);
1313 if (mask & ShiftMask)
1314 result |= KEY_M_SHIFT;
1315 if (mask & ControlMask)
1316 result |= KEY_M_CTRL;
1317 return result;
1319 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
1320 #ifdef __QNXNTO__
1322 if (in_photon == 0) {
1323 /* First time here, let's load Photon library and attach
1324 to Photon */
1325 in_photon = -1;
1326 if (getenv ("PHOTON2_PATH") != NULL) {
1327 /* QNX 6.x has no support for RTLD_LAZY */
1328 void *ph_handle = dlopen ("/usr/lib/libph.so", RTLD_NOW);
1329 if (ph_handle != NULL) {
1330 ph_attach = (ph_dv_f) dlsym (ph_handle, "PhAttach");
1331 ph_input_group =
1332 (ph_ov_f) dlsym (ph_handle, "PhInputGroup");
1333 ph_query_cursor =
1334 (ph_pqc_f) dlsym (ph_handle, "PhQueryCursor");
1335 if ((ph_attach != NULL) && (ph_input_group != NULL)
1336 && (ph_query_cursor != NULL)) {
1337 if ((*ph_attach) (0, 0)) { /* Attached */
1338 ph_ig = (*ph_input_group) (0);
1339 in_photon = 1;
1345 /* We do not have Photon running. Assume we are in text
1346 console or xterm */
1347 if (in_photon == -1) {
1348 if (devctl
1349 (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status,
1350 sizeof (int), NULL) == -1)
1351 return 0;
1352 shift_ext_status = mod_status & 0xffffff00UL;
1353 mod_status &= 0x7f;
1354 if (mod_status & _LINESTATUS_CON_ALT)
1355 result |= KEY_M_ALT;
1356 if (mod_status & _LINESTATUS_CON_CTRL)
1357 result |= KEY_M_CTRL;
1358 if ((mod_status & _LINESTATUS_CON_SHIFT)
1359 || (shift_ext_status & 0x00000800UL))
1360 result |= KEY_M_SHIFT;
1361 } else {
1362 (*ph_query_cursor) (ph_ig, &cursor_info);
1363 if (cursor_info.key_mods & 0x04)
1364 result |= KEY_M_ALT;
1365 if (cursor_info.key_mods & 0x02)
1366 result |= KEY_M_CTRL;
1367 if (cursor_info.key_mods & 0x01)
1368 result |= KEY_M_SHIFT;
1370 #endif /* __QNXNTO__ */
1372 #if defined __linux__ || (defined __CYGWIN__ && defined TIOCLINUX)
1374 unsigned char modifiers = 6;
1376 if (ioctl (0, TIOCLINUX, &modifiers) < 0)
1377 return 0;
1379 /* Translate Linux modifiers into mc modifiers */
1380 if (modifiers & SHIFT_PRESSED)
1381 result |= KEY_M_SHIFT;
1382 if (modifiers & (ALTL_PRESSED | ALTR_PRESSED))
1383 result |= KEY_M_ALT;
1384 if (modifiers & CONTROL_PRESSED)
1385 result |= KEY_M_CTRL;
1387 #endif /* !__linux__ */
1388 return result;
1391 static void k_dispose (key_def *k)
1393 if (!k)
1394 return;
1395 k_dispose (k->child);
1396 k_dispose (k->next);
1397 g_free (k);
1400 static void s_dispose (SelectList *sel)
1402 if (!sel)
1403 return;
1405 s_dispose (sel->next);
1406 g_free (sel);
1409 void done_key ()
1411 k_dispose (keys);
1412 s_dispose (select_list);
1414 #ifdef HAVE_TEXTMODE_X11_SUPPORT
1415 if (x11_display)
1416 mc_XCloseDisplay (x11_display);
1417 #endif