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.
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. */
32 #include <sys/types.h>
39 #include "layout.h" /* winch_flag */
42 #include "cons.saver.h"
45 #include "../vfs/gc.h"
48 #ifdef HAVE_TEXTMODE_X11_SUPPORT
53 # if defined(__GLIBC__) && (__GLIBC__ < 2)
54 # include <linux/termios.h> /* TIOCLINUX */
58 # include <sys/ioctl.h>
59 #endif /* __linux__ */
63 # include <sys/ioctl.h>
64 #endif /* __CYGWIN__ */
69 # include <sys/dcmd_chr.h>
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;
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 */
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
100 /* This holds all the key definitions */
101 static key_def
*keys
= NULL
;
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
{
113 struct SelectList
*next
;
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
*);
121 ph_ov_f ph_input_group
;
122 ph_pqc_f ph_query_cursor
;
125 static SelectList
*select_list
= NULL
;
127 void add_select_channel (int fd
, select_fn callback
, void *info
)
131 new = g_new (SelectList
, 1);
133 new->callback
= callback
;
135 new->next
= select_list
;
139 void delete_select_channel (int fd
)
141 SelectList
*p
= select_list
;
142 SelectList
*p_prev
= NULL
;
150 p_prev
->next
= p_next
;
152 select_list
= p_next
;
164 inline static int add_selects (fd_set
*select_set
)
169 if (disabled_channels
)
172 for (p
= select_list
; p
; p
= p
->next
) {
173 FD_SET (p
->fd
, select_set
);
180 static void check_selects (fd_set
*select_set
)
185 if (disabled_channels
)
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
);
200 void channels_down (void)
205 void channels_up (void)
207 if (!disabled_channels
)
208 fputs ("Error: channels_up called with disabled_channels = 0\n",
213 typedef const struct {
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
},
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
},
316 { KEY_M_SHIFT
| KEY_PPAGE
, ESC_STR
"[5;2~", MCKEY_NOACTION
},
317 { KEY_M_SHIFT
| KEY_NPAGE
, ESC_STR
"[6;2~", MCKEY_NOACTION
},
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
},
407 define_sequences (key_define_t
*kd
)
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
;
423 if (!getenv ("DISPLAY"))
426 x11_display
= mc_XOpenDisplay (0);
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 */
439 const char *term
= getenv ("TERM");
440 char *kt
= getenv ("KEYBOARD_KEY_TIMEOUT_US");
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 */
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 ();
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;
479 #ifdef HAVE_TEXTMODE_X11_SUPPORT
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)
494 input_fd
= SLang_TT_Read_FD
;
500 xmouse_get_event (Gpm_Event
*ev
)
503 static struct timeval tv1
= { 0, 0 }; /* Force first click as single */
504 static struct timeval tv2
;
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 */
514 /* There seems to be no way of knowing which button was released */
515 /* So we assume all the buttons were released */
519 ev
->type
= GPM_UP
| (GPM_SINGLE
<< clicks
);
525 /* Bogus event, maybe mouse wheel */
529 if (btn
>= 32 && btn
<= 34) {
536 if (tv1
.tv_sec
&& (DIF_TIME (tv1
,tv2
) < double_click_speed
)) {
544 ev
->buttons
= GPM_B_LEFT
;
547 ev
->buttons
= GPM_B_MIDDLE
;
550 ev
->buttons
= GPM_B_RIGHT
;
553 ev
->buttons
= GPM_B_UP
;
557 ev
->buttons
= GPM_B_DOWN
;
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);
581 if (attach
) attach
->child
= p
;
585 p
->child
= p
->next
= NULL
;
589 p
->action
= MCKEY_NOACTION
;
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
)
603 seq_append
= seq_buffer
;
605 if (seq_append
== &(seq_buffer
[SEQ_BUFFER_LEN
-2]))
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
)
620 if (strlen (seq
) > SEQ_BUFFER_LEN
-1)
623 for (base
= keys
; (base
!= 0) && *seq
; ) {
624 if (*seq
== base
->ch
) {
625 if (base
->child
== 0) {
627 base
->child
= create_sequence (seq
+1, code
, action
);
630 /* The sequence matches an existing one. */
632 base
->action
= action
;
643 base
->next
= create_sequence (seq
, code
, action
);
650 /* Attempt to redefine a sequence with a shorter sequence. */
654 keys
= create_sequence (seq
, code
, action
);
658 static int *pending_keys
;
660 /* Apply corrections for the keycode generated in get_key_code() */
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 */
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 */
683 /* This is reported to be useful on AIX */
684 if (c
== KEY_SCANCEL
)
687 /* Convert Shift+Tab and Ctrl+Tab to Back Tab */
688 if ((c
== '\t') && (mod
& (KEY_M_SHIFT
| KEY_M_CTRL
))) {
693 /* F0 is the same as F10 for out purposes */
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') {
710 if ((c
== 127) && (mod
==0)) /* Add Ctrl/Alt/Shift-BackSpace */
712 mod
|= get_modifier();
716 if ((c
=='0') && (mod
==0)) /* Add Shift-Insert on key pad */
718 if ((qmod
& KEY_M_SHIFT
) == KEY_M_SHIFT
)
725 if ((c
=='.') && (mod
==0)) /* Add Shift-Del on key pad */
727 if ((qmod
& KEY_M_SHIFT
) == KEY_M_SHIFT
)
733 #endif /* __QNXNTO__ */
735 /* Unrecognized 0177 is delete (preserve Ctrl) */
740 /* Unrecognized Ctrl-d is delete */
741 if (c
== (31 & 'd')) {
746 /* Unrecognized Ctrl-h is backspace */
747 if (c
== (31 & 'h')) {
752 /* Shift+BackSpace is backspace */
753 if (c
== KEY_BACKSPACE
&& (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
)) {
762 /* Remove Shift information from function keys */
763 if (c
>= KEY_F (1) && c
<= KEY_F (20)) {
767 if (!alternate_plus_minus
)
772 case KEY_KP_SUBTRACT
:
775 case KEY_KP_MULTIPLY
:
783 int get_key_code (int no_delay
)
786 static key_def
*this = NULL
, *parent
;
787 static struct timeval esctime
= { -1, -1 };
788 static int lastnodelay
= -1;
790 if (no_delay
!= lastnodelay
) {
792 lastnodelay
= no_delay
;
797 int d
= *pending_keys
++;
799 if (!*pending_keys
) {
803 if (d
== ESC_CHAR
&& pending_keys
) {
804 d
= ALT(*pending_keys
++);
807 if ((d
> 127 && d
< 256) && use_8th_bit_as_meta
)
810 return correct_key_code (d
);
815 nodelay (stdscr
, TRUE
);
818 #if defined(USE_NCURSES) && defined(KEY_RESIZE)
820 goto nodelay_try_again
;
823 nodelay (stdscr
, FALSE
);
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)
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;
838 if (current
.tv_sec
< timeout
.tv_sec
)
840 if (current
.tv_sec
== timeout
.tv_sec
&&
841 current
.tv_usec
< timeout
.tv_usec
)
844 pending_keys
= seq_append
= NULL
;
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. */
854 pending_keys
= seq_buffer
;
861 /* Search the key on the root */
862 if (!no_delay
|| this == NULL
) {
866 if ((c
> 127 && c
< 256) && use_8th_bit_as_meta
) {
869 /* The first sequence defined starts with esc */
877 if (!push_char (c
)) {
878 pending_keys
= seq_buffer
;
883 if (parent
->action
== MCKEY_ESCAPE
&& old_esc_mode
) {
887 /* Shouldn't happen */
888 fputs ("Internal error\n", stderr
);
891 goto nodelay_try_again
;
894 c
= xgetch_second ();
896 pending_keys
= seq_append
= NULL
;
902 goto nodelay_try_again
;
906 /* We got a complete match, return and reset search */
909 pending_keys
= seq_append
= NULL
;
912 return correct_key_code (code
);
918 if (parent
!= NULL
&& parent
->action
== MCKEY_ESCAPE
) {
920 /* Convert escape-digits to F-keys */
928 pending_keys
= seq_append
= 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
936 pending_keys
= seq_buffer
;
942 return correct_key_code (c
);
945 /* If set timeout is set, then we wait 0.1 seconds, else, we block */
947 try_channels (int set_timeout
)
949 struct timeval timeout
;
950 static fd_set select_set
;
951 struct timeval
*timeptr
;
956 FD_ZERO (&select_set
);
957 FD_SET (input_fd
, &select_set
); /* Add stdin */
958 maxfdp
= max (add_selects (&select_set
), input_fd
);
962 timeout
.tv_usec
= 100000;
967 v
= select (maxfdp
+ 1, &select_set
, NULL
, NULL
, timeptr
);
969 check_selects (&select_set
);
970 if (FD_ISSET (input_fd
, &select_set
))
976 /* Workaround for System V Curses vt100 bug */
977 static int getch_with_delay (void)
981 /* This routine could be used on systems without mouse support,
982 so we need to do the select check :-( */
987 /* Try to get a character */
988 c
= get_key_code (0);
991 /* Failed -> wait 0.1 secs and try again */
994 /* Success -> return the character */
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
)
1006 static int flag
; /* Return value from select */
1008 static struct Gpm_Event ev
; /* Mouse event */
1010 struct timeval timeout
;
1011 struct timeval
*time_addr
= NULL
;
1012 static int dirty
= 3;
1014 if ((dirty
== 3) || is_idle ()) {
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
1028 show_mouse_pointer (event
->x
, event
->y
);
1033 /* Repeat if using mouse */
1034 while (mouse_enabled
&& !pending_keys
) {
1038 FD_ZERO (&select_set
);
1039 FD_SET (input_fd
, &select_set
);
1040 maxfdp
= max (add_selects (&select_set
), input_fd
);
1043 if (use_mouse_p
== MOUSE_GPM
) {
1045 /* Connection to gpm broken, possibly gpm has died */
1047 use_mouse_p
= MOUSE_NONE
;
1050 FD_SET (gpm_fd
, &select_set
);
1051 maxfdp
= max (maxfdp
, gpm_fd
);
1057 timeout
.tv_usec
= mou_auto_repeat
* 1000;
1060 time_addr
= &timeout
;
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
;
1077 if (!block
|| winch_flag
) {
1078 time_addr
= &timeout
;
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.
1094 if (!block
|| winch_flag
)
1096 vfs_timeout_handler ();
1098 if (flag
== -1 && errno
== EINTR
)
1101 check_selects (&select_set
);
1103 if (FD_ISSET (input_fd
, &select_set
))
1106 if (use_mouse_p
== MOUSE_GPM
&& gpm_fd
> 0
1107 && FD_ISSET (gpm_fd
, &select_set
)) {
1113 #endif /* !HAVE_LIBGPM */
1116 flag
= is_wintouched (stdscr
);
1117 untouchwin (stdscr
);
1118 #endif /* !HAVE_SLANG */
1119 c
= block
? getch_with_delay () : get_key_code (1);
1124 #endif /* !HAVE_SLANG */
1126 if (c
== MCKEY_MOUSE
1129 #endif /* KEY_MOUSE */
1132 xmouse_get_event (event
);
1142 /* Returns a key press, mouse events are discarded */
1149 while ((key
= get_event (&ev
, 0, 1)) == EV_NONE
)
1154 static int xgetch_second (void)
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
);
1167 nodelay (stdscr
, FALSE
);
1172 learn_store_key (char *buffer
, char **p
, int c
)
1174 if (*p
- buffer
> 253)
1176 if (c
== ESC_CHAR
) {
1179 } else if (c
< ' ') {
1181 *(*p
)++ = c
+ 'a' - 1;
1182 } else if (c
== '^') {
1189 char *learn_key (void)
1191 /* LEARN_TIMEOUT in usec */
1192 #define LEARN_TIMEOUT 200000
1195 struct timeval endtime
;
1196 struct timeval timeout
;
1201 keypad(stdscr
, FALSE
); /* disable intepreting keys by ncurses */
1204 c
= getch (); /* Sanity check, should be unnecessary */
1205 learn_store_key (buffer
, &p
, c
);
1207 endtime
.tv_usec
+= LEARN_TIMEOUT
;
1208 if (endtime
.tv_usec
> 1000000) {
1209 endtime
.tv_usec
-= 1000000;
1212 nodelay (stdscr
, TRUE
);
1214 while ((c
= getch ()) == -1) {
1216 timeout
.tv_usec
= endtime
.tv_usec
- timeout
.tv_usec
;
1217 if (timeout
.tv_usec
< 0)
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
);
1229 learn_store_key (buffer
, &p
, c
);
1231 keypad(stdscr
, TRUE
);
1232 nodelay (stdscr
, FALSE
);
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)*/
1241 numeric_keypad_mode (void)
1243 if (console_flag
|| xterm_flag
) {
1244 fputs ("\033>", stdout
);
1250 application_keypad_mode (void)
1252 if (console_flag
|| xterm_flag
) {
1253 fputs ("\033=", 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.
1268 struct timeval timeout
;
1270 FD_ZERO (&select_set
);
1271 FD_SET (input_fd
, &select_set
);
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
);
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.
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
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
;
1319 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
1322 if (in_photon
== 0) {
1323 /* First time here, let's load Photon library and attach
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");
1332 (ph_ov_f
) dlsym (ph_handle
, "PhInputGroup");
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);
1345 /* We do not have Photon running. Assume we are in text
1347 if (in_photon
== -1) {
1349 (fileno (stdin
), DCMD_CHR_LINESTATUS
, &mod_status
,
1350 sizeof (int), NULL
) == -1)
1352 shift_ext_status
= mod_status
& 0xffffff00UL
;
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
;
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)
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__ */
1391 static void k_dispose (key_def
*k
)
1395 k_dispose (k
->child
);
1396 k_dispose (k
->next
);
1400 static void s_dispose (SelectList
*sel
)
1405 s_dispose (sel
->next
);
1412 s_dispose (select_list
);
1414 #ifdef HAVE_TEXTMODE_X11_SUPPORT
1416 mc_XCloseDisplay (x11_display
);