corrected some file operations dialog msgs
[midnight-commander.git] / src / key.c
blob1499dfb81f9ac3c9c53d162c657e5d94fd56ca28
1 /* Keyboard support routines.
3 Copyright (C) 1994,1995 the Free Software Foundation.
5 Written by: 1994, 1995 Miguel de Icaza.
6 1994, 1995 Janne Kukonlehto.
7 1995 Jakub Jelinek.
8 1997 Norbert Warmuth
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <ctype.h>
32 #include <errno.h>
34 #include "global.h"
35 #include "tty.h"
36 #include "mouse.h"
37 #include "key.h"
38 #include "main.h"
39 #include "win.h"
40 #include "cons.saver.h"
41 #include "../vfs/vfs.h"
43 #ifdef HAVE_TEXTMODE_X11_SUPPORT
44 #include <X11/Xlib.h>
45 #endif
47 #ifdef __linux__
48 # if defined(__GLIBC__) && (__GLIBC__ < 2)
49 # include <linux/termios.h> /* This is needed for TIOCLINUX */
50 # else
51 # include <termios.h>
52 # endif
53 # include <sys/ioctl.h>
54 #endif
56 #define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *)NULL))
57 #define DIF_TIME(t1,t2) ((t2.tv_sec -t1.tv_sec) *1000+ \
58 (t2.tv_usec-t1.tv_usec)/1000)
60 /* timeout for old_esc_mode in usec */
61 #define ESCMODE_TIMEOUT 1000000
63 int mou_auto_repeat = 100;
64 int double_click_speed = 250;
65 int old_esc_mode = 0;
67 int use_8th_bit_as_meta = 1;
69 typedef struct key_def {
70 char ch; /* Holds the matching char code */
71 int code; /* The code returned, valid if child == NULL */
72 struct key_def *next;
73 struct key_def *child; /* sequence continuation */
74 int action; /* optional action to be done. Now used only
75 to mark that we are just after the first
76 Escape */
77 } key_def;
79 /* This holds all the key definitions */
80 static key_def *keys = 0;
82 static int input_fd;
83 static int disabled_channels = 0; /* Disable channels checking */
84 static int xgetch_second (void);
86 /* File descriptor monitoring add/remove routines */
87 typedef struct SelectList {
88 int fd;
89 select_fn callback;
90 void *info;
91 struct SelectList *next;
92 } SelectList;
94 static SelectList *select_list = 0;
96 void add_select_channel (int fd, select_fn callback, void *info)
98 SelectList *new;
100 new = g_new (SelectList, 1);
101 new->fd = fd;
102 new->callback = callback;
103 new->info = info;
104 new->next = select_list;
105 select_list = new;
108 void delete_select_channel (int fd)
110 SelectList *p = select_list;
111 SelectList *p_prev = 0;
112 SelectList *p_next;
114 while (p) {
115 if (p->fd == fd) {
116 p_next = p->next;
118 if (p_prev)
119 p_prev->next = p_next;
120 else
121 select_list = p_next;
123 g_free (p);
124 p = p_next;
125 continue;
128 p_prev = p;
129 p = p->next;
133 inline static int add_selects (fd_set *select_set)
135 SelectList *p;
136 int top_fd = 0;
138 if (disabled_channels)
139 return 0;
141 for (p = select_list; p; p = p->next){
142 FD_SET (p->fd, select_set);
143 if (p->fd > top_fd)
144 top_fd = p->fd;
146 return top_fd;
149 static void check_selects (fd_set *select_set)
151 SelectList *p;
153 if (disabled_channels)
154 return;
156 for (p = select_list; p; p = p->next)
157 if (FD_ISSET (p->fd, select_set))
158 (*p->callback)(p->fd, p->info);
161 void channels_down (void)
163 disabled_channels ++;
166 void channels_up (void)
168 if (!disabled_channels)
169 fprintf (stderr,
170 "Error: channels_up called with disabled_channels = 0\n");
171 disabled_channels--;
174 typedef const struct {
175 int code;
176 char *seq;
177 int action;
178 } key_define_t;
180 static key_define_t mc_bindings [] = {
181 { KEY_END, ESC_STR ">", MCKEY_NOACTION },
182 { KEY_HOME, ESC_STR "<", MCKEY_NOACTION },
183 { 0, 0, MCKEY_NOACTION },
186 /* Broken terminfo and termcap databases on xterminals */
187 static key_define_t xterm_key_defines [] = {
188 { KEY_F(1), ESC_STR "OP", MCKEY_NOACTION },
189 { KEY_F(2), ESC_STR "OQ", MCKEY_NOACTION },
190 { KEY_F(3), ESC_STR "OR", MCKEY_NOACTION },
191 { KEY_F(4), ESC_STR "OS", MCKEY_NOACTION },
192 { KEY_F(1), ESC_STR "[11~", MCKEY_NOACTION },
193 { KEY_F(2), ESC_STR "[12~", MCKEY_NOACTION },
194 { KEY_F(3), ESC_STR "[13~", MCKEY_NOACTION },
195 { KEY_F(4), ESC_STR "[14~", MCKEY_NOACTION },
196 { KEY_F(5), ESC_STR "[15~", MCKEY_NOACTION },
197 { KEY_F(6), ESC_STR "[17~", MCKEY_NOACTION },
198 { KEY_F(7), ESC_STR "[18~", MCKEY_NOACTION },
199 { KEY_F(8), ESC_STR "[19~", MCKEY_NOACTION },
200 { KEY_F(9), ESC_STR "[20~", MCKEY_NOACTION },
201 { KEY_F(10), ESC_STR "[21~", MCKEY_NOACTION },
202 { 0, 0, MCKEY_NOACTION },
205 static key_define_t mc_default_keys [] = {
206 { ESC_CHAR, ESC_STR, MCKEY_ESCAPE },
207 { ESC_CHAR, ESC_STR ESC_STR, MCKEY_NOACTION },
208 { 0, 0, MCKEY_NOACTION },
211 static void
212 define_sequences (key_define_t *kd)
214 int i;
216 for (i = 0; kd [i].code; i++)
217 define_sequence(kd [i].code, kd [i].seq, kd [i].action);
220 #ifdef HAVE_TEXTMODE_X11_SUPPORT
221 static Display *x11_display;
222 static Window x11_window;
223 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
225 /* This has to be called before slang_init or whatever routine
226 calls any define_sequence */
227 void init_key (void)
229 char *term = (char *) getenv ("TERM");
231 /* This has to be the first define_sequence */
232 /* So, we can assume that the first keys member has ESC */
233 define_sequences (mc_default_keys);
235 /* Terminfo on irix does not have some keys */
236 if ((!strncmp (term, "iris-ansi", 9)) || (!strncmp (term, "xterm", 5)))
237 define_sequences (xterm_key_defines);
239 define_sequences (mc_bindings);
241 /* load some additional keys (e.g. direct Alt-? support) */
242 load_xtra_key_defines();
244 #ifdef __QNX__
245 if (strncmp(term, "qnx", 3) == 0){
246 /* Modify the default value of use_8th_bit_as_meta: we would
247 * like to provide a working mc for a newbie who knows nothing
248 * about [Options|Display bits|Full 8 bits input]...
250 * Don't use 'meta'-bit, when we are dealing with a
251 * 'qnx*'-type terminal: clear the default value!
252 * These terminal types use 0xFF as an escape character,
253 * so use_8th_bit_as_meta==1 must not be enabled!
255 * [mc-4.1.21+,slint.c/getch(): the DEC_8BIT_HACK stuff
256 * is not used now (doesn't even depend on use_8th_bit_as_meta
257 * as in mc-3.1.2)...GREAT!...no additional code is required!]
259 use_8th_bit_as_meta = 0;
261 #endif /* __QNX__ */
263 #ifdef HAVE_TEXTMODE_X11_SUPPORT
264 x11_display = XOpenDisplay (0);
265 if (x11_display)
266 x11_window = DefaultRootWindow (x11_display);
267 #endif /* HAVE_TEXTMODE_X11_SUPPORT */
270 /* This has to be called after SLang_init_tty/slint_init */
271 void init_key_input_fd (void)
273 #ifdef HAVE_SLANG
274 input_fd = SLang_TT_Read_FD;
275 #endif
279 static void
280 xmouse_get_event (Gpm_Event *ev)
282 int btn;
283 static struct timeval tv1 = { 0, 0 }; /* Force first click as single */
284 static struct timeval tv2;
285 static int clicks;
286 static int last_btn = 0;
288 /* Decode Xterm mouse information to a GPM style event */
290 /* Variable btn has following meaning: */
291 /* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
292 btn = getch () - 32;
294 /* There seems to be no way of knowing which button was released */
295 /* So we assume all the buttons were released */
297 if (btn == 3){
298 if (last_btn) {
299 ev->type = GPM_UP | (GPM_SINGLE << clicks);
300 ev->buttons = 0;
301 last_btn = 0;
302 GET_TIME (tv1);
303 clicks = 0;
304 } else {
305 /* Bogus event, maybe mouse wheel */
306 ev->type = 0;
308 } else {
309 ev->type = GPM_DOWN;
310 GET_TIME (tv2);
311 if (tv1.tv_sec && (DIF_TIME (tv1,tv2) < double_click_speed)){
312 clicks++;
313 clicks %= 3;
314 } else
315 clicks = 0;
317 switch (btn) {
318 case 0:
319 ev->buttons = GPM_B_LEFT;
320 break;
321 case 1:
322 ev->buttons = GPM_B_MIDDLE;
323 break;
324 case 2:
325 ev->buttons = GPM_B_RIGHT;
326 break;
327 default:
328 /* Nothing */
329 ev->type = 0;
330 ev->buttons = 0;
331 break;
333 last_btn = ev->buttons;
335 /* Coordinates are 33-based */
336 /* Transform them to 1-based */
337 ev->x = getch () - 32;
338 ev->y = getch () - 32;
341 static key_def *create_sequence (char *seq, int code, int action)
343 key_def *base, *p, *attach;
345 for (base = attach = NULL; *seq; seq++){
346 p = g_new (key_def, 1);
347 if (!base) base = p;
348 if (attach) attach->child = p;
350 p->ch = *seq;
351 p->code = code;
352 p->child = p->next = NULL;
353 if (!seq[1])
354 p->action = action;
355 else
356 p->action = MCKEY_NOACTION;
357 attach = p;
359 return base;
362 /* The maximum sequence length (32 + null terminator) */
363 #define SEQ_BUFFER_LEN 33
364 static int seq_buffer [SEQ_BUFFER_LEN];
365 static int *seq_append = 0;
367 static int push_char (int c)
369 if (!seq_append)
370 seq_append = seq_buffer;
372 if (seq_append == &(seq_buffer [SEQ_BUFFER_LEN-2]))
373 return 0;
374 *(seq_append++) = c;
375 *seq_append = 0;
376 return 1;
380 * Return 1 on success, 0 on error.
381 * An error happens if SEQ is a beginning of an existing longer sequence.
383 int define_sequence (int code, char *seq, int action)
385 key_def *base;
387 if (strlen (seq) > SEQ_BUFFER_LEN-1)
388 return 0;
390 for (base = keys; (base != 0) && *seq; ){
391 if (*seq == base->ch){
392 if (base->child == 0){
393 if (*(seq+1)){
394 base->child = create_sequence (seq+1, code, action);
395 return 1;
396 } else {
397 /* The sequence matches an existing one. */
398 base->code = code;
399 base->action = action;
400 return 1;
402 } else {
403 base = base->child;
404 seq++;
406 } else {
407 if (base->next)
408 base = base->next;
409 else {
410 base->next = create_sequence (seq, code, action);
411 return 1;
416 if (!*seq) {
417 /* Attempt to redefine a sequence with a shorter sequence. */
418 return 0;
421 keys = create_sequence (seq, code, action);
422 return 1;
425 static int *pending_keys;
427 static int
428 correct_key_code (int c)
430 /* This is needed on some OS that do not support ncurses and */
431 /* do some magic after read()ing the data */
432 if (c == '\r')
433 return '\n';
435 #ifdef IS_AIX
436 if (c == KEY_SCANCEL)
437 return '\t';
438 #endif
440 if (c == KEY_F(0))
441 return KEY_F(10);
443 if (!alternate_plus_minus)
444 switch (c) {
445 case KEY_KP_ADD: c = '+'; break;
446 case KEY_KP_SUBTRACT: c = '-'; break;
447 case KEY_KP_MULTIPLY: c = '*'; break;
450 return c;
453 int get_key_code (int no_delay)
455 int c;
456 static key_def *this = NULL, *parent;
457 static struct timeval esctime = { -1, -1 };
458 static int lastnodelay = -1;
460 if (no_delay != lastnodelay) {
461 this = NULL;
462 lastnodelay = no_delay;
465 pend_send:
466 if (pending_keys){
467 int d = *pending_keys++;
468 check_pend:
469 if (!*pending_keys){
470 pending_keys = 0;
471 seq_append = 0;
473 if (d == ESC_CHAR && pending_keys){
474 d = ALT(*pending_keys++);
475 goto check_pend;
477 if ((d & 0x80) && use_8th_bit_as_meta)
478 d = ALT(d & 0x7f);
479 this = NULL;
480 return correct_key_code (d);
483 nodelay_try_again:
484 if (no_delay) {
485 nodelay (stdscr, TRUE);
487 c = getch ();
488 #if defined(USE_NCURSES) && defined(KEY_RESIZE)
489 if (c == KEY_RESIZE)
490 goto nodelay_try_again;
491 #endif
492 if (no_delay) {
493 nodelay (stdscr, FALSE);
494 if (c == ERR) {
495 if (this != NULL && parent != NULL &&
496 parent->action == MCKEY_ESCAPE && old_esc_mode) {
497 struct timeval current, timeout;
499 if (esctime.tv_sec == -1)
500 return ERR;
501 GET_TIME (current);
502 timeout.tv_sec = ESCMODE_TIMEOUT / 1000000 + esctime.tv_sec;
503 timeout.tv_usec = ESCMODE_TIMEOUT % 1000000 + esctime.tv_usec;
504 if (timeout.tv_usec > 1000000) {
505 timeout.tv_usec -= 1000000;
506 timeout.tv_sec++;
508 if (current.tv_sec < timeout.tv_sec)
509 return ERR;
510 if (current.tv_sec == timeout.tv_sec &&
511 current.tv_usec < timeout.tv_usec)
512 return ERR;
513 this = NULL;
514 pending_keys = seq_append = NULL;
515 return ESC_CHAR;
517 return ERR;
519 } else if (c == ERR){
520 /* Maybe we got an incomplete match.
521 This we do only in delay mode, since otherwise
522 getch can return ERR at any time. */
523 if (seq_append) {
524 pending_keys = seq_buffer;
525 goto pend_send;
527 this = NULL;
528 return ERR;
531 /* Search the key on the root */
532 if (!no_delay || this == NULL) {
533 this = keys;
534 parent = NULL;
536 if ((c & 0x80) && use_8th_bit_as_meta) {
537 c &= 0x7f;
539 /* The first sequence defined starts with esc */
540 parent = keys;
541 this = keys->child;
544 while (this){
545 if (c == this->ch){
546 if (this->child){
547 if (!push_char (c)){
548 pending_keys = seq_buffer;
549 goto pend_send;
551 parent = this;
552 this = this->child;
553 if (parent->action == MCKEY_ESCAPE && old_esc_mode) {
554 if (no_delay) {
555 GET_TIME (esctime);
556 if (this == NULL) {
557 /* Shouldn't happen */
558 fprintf (stderr, "Internal error\n");
559 exit (1);
561 goto nodelay_try_again;
563 esctime.tv_sec = -1;
564 c = xgetch_second ();
565 if (c == ERR) {
566 pending_keys = seq_append = NULL;
567 this = NULL;
568 return ESC_CHAR;
570 } else {
571 if (no_delay)
572 goto nodelay_try_again;
573 c = getch ();
575 } else {
576 /* We got a complete match, return and reset search */
577 int code;
579 pending_keys = seq_append = NULL;
580 code = this->code;
581 this = NULL;
582 return correct_key_code (code);
584 } else {
585 if (this->next)
586 this = this->next;
587 else {
588 if (parent != NULL && parent->action == MCKEY_ESCAPE) {
589 /* This is just to save a lot of define_sequences */
590 if (isalpha(c)
591 || (c == '\n') || (c == '\t') || (c == XCTRL('h'))
592 || (c == KEY_BACKSPACE) || (c == '!') || (c == '\r')
593 || c == 127 || c == '+' || c == '-' || c == '\\'
594 || c == '?')
595 c = ALT(c);
596 else if (isdigit(c))
597 c = KEY_F (c-'0');
598 else if (c == ' ')
599 c = ESC_CHAR;
600 pending_keys = seq_append = NULL;
601 this = NULL;
602 return correct_key_code (c);
604 /* Did not find a match or {c} was changed in the if above,
605 so we have to return everything we had skipped
607 push_char (c);
608 pending_keys = seq_buffer;
609 goto pend_send;
613 this = NULL;
614 return correct_key_code (c);
617 /* If set timeout is set, then we wait 0.1 seconds, else, we block */
618 static void
619 try_channels (int set_timeout)
621 struct timeval timeout;
622 static fd_set select_set;
623 struct timeval *timeptr;
624 int v;
625 int maxfdp;
627 while (1){
628 FD_ZERO (&select_set);
629 FD_SET (input_fd, &select_set); /* Add stdin */
630 maxfdp = max (add_selects (&select_set), input_fd);
632 if (set_timeout){
633 timeout.tv_sec = 0;
634 timeout.tv_usec = 100000;
635 timeptr = &timeout;
636 } else
637 timeptr = 0;
639 v = select (maxfdp + 1, &select_set, NULL, NULL, timeptr);
640 if (v > 0){
641 check_selects (&select_set);
642 if (FD_ISSET (input_fd, &select_set))
643 return;
648 /* Workaround for System V Curses vt100 bug */
649 static int getch_with_delay (void)
651 int c;
653 /* This routine could be used on systems without mouse support,
654 so we need to do the select check :-( */
655 while (1){
656 if (!pending_keys)
657 try_channels (0);
659 /* Try to get a character */
660 c = get_key_code (0);
661 if (c != ERR)
662 break;
663 /* Failed -> wait 0.1 secs and try again */
664 try_channels (1);
666 /* Success -> return the character */
667 return c;
670 extern int max_dirt_limit;
672 /* Returns a character read from stdin with appropriate interpretation */
673 /* Also takes care of generated mouse events */
674 /* Returns EV_MOUSE if it is a mouse event */
675 /* Returns EV_NONE if non-blocking or interrupt set and nothing was done */
677 get_event (Gpm_Event * event, int redo_event, int block)
679 int c;
680 static int flag; /* Return value from select */
681 #ifdef HAVE_LIBGPM
682 static Gpm_Event ev; /* Mouse event */
683 #endif
684 struct timeval timeout;
685 struct timeval *time_addr = NULL;
686 static int dirty = 3;
688 if ((dirty == 3) || is_idle ()) {
689 mc_refresh ();
690 doupdate ();
691 dirty = 1;
692 } else
693 dirty++;
695 vfs_timeout_handler ();
697 /* Ok, we use (event->x < 0) to signal that the event does not contain
698 a suitable position for the mouse, so we can't use show_mouse_pointer
699 on it.
701 if (event->x > 0) {
702 show_mouse_pointer (event->x, event->y);
703 if (!redo_event)
704 event->x = -1;
707 /* Repeat if using mouse */
708 while (mouse_enabled && !pending_keys) {
709 int maxfdp;
710 fd_set select_set;
712 FD_ZERO (&select_set);
713 FD_SET (input_fd, &select_set);
714 maxfdp = max (add_selects (&select_set), input_fd);
716 #ifdef HAVE_LIBGPM
717 if (use_mouse_p == MOUSE_GPM) {
718 if (gpm_fd == -1) {
719 /* Connection to gpm broken, possibly gpm has died */
720 mouse_enabled = 0;
721 use_mouse_p = MOUSE_NONE;
722 break;
724 FD_SET (gpm_fd, &select_set);
725 maxfdp = max (maxfdp, gpm_fd);
727 #endif
729 if (redo_event) {
730 timeout.tv_usec = mou_auto_repeat * 1000;
731 timeout.tv_sec = 0;
733 time_addr = &timeout;
734 } else {
735 int seconds;
737 if ((seconds = vfs_timeouts ())) {
738 /* the timeout could be improved and actually be
739 * the number of seconds until the next vfs entry
740 * timeouts in the stamp list.
743 timeout.tv_sec = seconds;
744 timeout.tv_usec = 0;
745 time_addr = &timeout;
746 } else
747 time_addr = NULL;
750 if (!block) {
751 time_addr = &timeout;
752 timeout.tv_sec = 0;
753 timeout.tv_usec = 0;
755 enable_interrupt_key ();
756 flag = select (maxfdp + 1, &select_set, NULL, NULL, time_addr);
757 disable_interrupt_key ();
759 /* select timed out: it could be for any of the following reasons:
760 * redo_event -> it was because of the MOU_REPEAT handler
761 * !block -> we did not block in the select call
762 * else -> 10 second timeout to check the vfs status.
764 if (flag == 0) {
765 if (redo_event)
766 return EV_MOUSE;
767 if (!block)
768 return EV_NONE;
769 vfs_timeout_handler ();
771 if (flag == -1 && errno == EINTR)
772 return EV_NONE;
774 check_selects (&select_set);
776 if (FD_ISSET (input_fd, &select_set))
777 break;
778 #ifdef HAVE_LIBGPM
779 if (use_mouse_p == MOUSE_GPM && FD_ISSET (gpm_fd, &select_set)) {
780 Gpm_GetEvent (&ev);
781 Gpm_FitEvent (&ev);
782 *event = ev;
783 return EV_MOUSE;
785 #endif /* !HAVE_LIBGPM */
787 #ifndef HAVE_SLANG
788 flag = is_wintouched (stdscr);
789 untouchwin (stdscr);
790 #endif /* !HAVE_SLANG */
791 c = block ? getch_with_delay () : get_key_code (1);
793 #ifndef HAVE_SLANG
794 if (flag)
795 touchwin (stdscr);
796 #endif /* !HAVE_SLANG */
798 if (c == MCKEY_MOUSE
799 #ifdef KEY_MOUSE
800 || c == KEY_MOUSE
801 #endif /* KEY_MOUSE */
803 /* Mouse event */
804 xmouse_get_event (event);
805 if (event->type)
806 return EV_MOUSE;
807 else
808 return EV_NONE;
811 return c;
814 /* Returns a key press, mouse events are discarded */
815 int mi_getch ()
817 Gpm_Event ev;
818 int key;
820 ev.x = -1;
821 while ((key = get_event (&ev, 0, 1)) == EV_NONE)
823 return key;
826 static int xgetch_second (void)
828 fd_set Read_FD_Set;
829 int c;
830 struct timeval timeout;
832 timeout.tv_sec = ESCMODE_TIMEOUT / 1000000;
833 timeout.tv_usec = ESCMODE_TIMEOUT % 1000000;
834 nodelay (stdscr, TRUE);
835 FD_ZERO (&Read_FD_Set);
836 FD_SET (input_fd, &Read_FD_Set);
837 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
838 c = getch ();
839 nodelay (stdscr, FALSE);
840 return c;
843 static void
844 learn_store_key (char *buffer, char **p, int c)
846 if (*p - buffer > 253)
847 return;
848 if (c == ESC_CHAR) {
849 *(*p)++ = '\\';
850 *(*p)++ = 'e';
851 } else if (c < ' ') {
852 *(*p)++ = '^';
853 *(*p)++ = c + 'a' - 1;
854 } else if (c == '^') {
855 *(*p)++ = '^';
856 *(*p)++ = '^';
857 } else
858 *(*p)++ = (char) c;
861 char *learn_key (void)
863 /* LEARN_TIMEOUT in usec */
864 #define LEARN_TIMEOUT 200000
866 fd_set Read_FD_Set;
867 struct timeval endtime;
868 struct timeval timeout;
869 int c;
870 char buffer [256];
871 char *p = buffer;
873 keypad(stdscr, FALSE); /* disable intepreting keys by ncurses */
874 c = getch ();
875 while (c == ERR)
876 c = getch (); /* Sanity check, should be unnecessary */
877 learn_store_key (buffer, &p, c);
878 GET_TIME (endtime);
879 endtime.tv_usec += LEARN_TIMEOUT;
880 if (endtime.tv_usec > 1000000) {
881 endtime.tv_usec -= 1000000;
882 endtime.tv_sec++;
884 nodelay (stdscr, TRUE);
885 for (;;) {
886 while ((c = getch ()) == ERR) {
887 GET_TIME (timeout);
888 timeout.tv_usec = endtime.tv_usec - timeout.tv_usec;
889 if (timeout.tv_usec < 0)
890 timeout.tv_sec++;
891 timeout.tv_sec = endtime.tv_sec - timeout.tv_sec;
892 if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) {
893 FD_ZERO (&Read_FD_Set);
894 FD_SET (input_fd, &Read_FD_Set);
895 select (input_fd + 1, &Read_FD_Set, NULL, NULL, &timeout);
896 } else
897 break;
899 if (c == ERR)
900 break;
901 learn_store_key (buffer, &p, c);
903 keypad(stdscr, TRUE);
904 nodelay (stdscr, FALSE);
905 *p = 0;
906 return g_strdup (buffer);
909 /* xterm and linux console only: set keypad to numeric or application
910 mode. Only in application keypad mode it's possible to distinguish
911 the '+' key and the '+' on the keypad ('*' and '-' ditto)*/
912 void
913 numeric_keypad_mode (void)
915 if (console_flag || xterm_flag) {
916 fprintf (stdout, "\033>");
917 fflush (stdout);
921 void
922 application_keypad_mode (void)
924 if (console_flag || xterm_flag) {
925 fprintf (stdout, "\033=");
926 fflush (stdout);
931 /* A function to check if we're idle.
932 Currently checks only for key presses.
933 We could also check the mouse. */
934 int is_idle (void)
936 /* Check for incoming key presses *
937 * If there are any we say we're busy */
939 fd_set select_set;
940 struct timeval timeout;
941 FD_ZERO (&select_set);
942 FD_SET (0, &select_set);
943 timeout.tv_sec = 0;
944 timeout.tv_usec = 0;
945 select (1, &select_set, 0, 0, &timeout);
946 return ! FD_ISSET (0, &select_set);
951 get_modifier (void)
953 #ifdef HAVE_TEXTMODE_X11_SUPPORT
954 if (x11_display) {
955 Window root, child;
956 int root_x, root_y;
957 int win_x, win_y;
958 unsigned int mask;
959 Bool b;
960 int result = 0;
962 b = XQueryPointer(x11_display, x11_window, &root, &child,
963 &root_x, &root_y,
964 &win_x, &win_y,
965 &mask);
967 if (mask & ShiftMask)
968 result |= SHIFT_PRESSED;
969 if (mask & ControlMask)
970 result |= CONTROL_PRESSED;
971 return result;
972 } else
973 #endif
974 #ifdef __linux__
976 unsigned char modifiers;
978 modifiers = 6;
980 if (ioctl (0, TIOCLINUX, &modifiers) < 0)
981 return 0;
983 return (int) modifiers;
985 #else
986 return 0;
987 #endif
991 ctrl_pressed (void)
993 if (get_modifier () & CONTROL_PRESSED)
994 return 1;
995 else
996 return 0;
999 static void k_dispose (key_def *k)
1001 if (!k)
1002 return;
1003 k_dispose (k->child);
1004 k_dispose (k->next);
1005 g_free (k);
1008 static void s_dispose (SelectList *sel)
1010 if (!sel)
1011 return;
1013 s_dispose (sel->next);
1014 g_free (sel);
1017 void done_key ()
1019 k_dispose (keys);
1020 s_dispose (select_list);
1022 #ifdef HAVE_TEXTMODE_X11_SUPPORT
1023 if (x11_display)
1024 XCloseDisplay (x11_display);
1025 #endif /* HAVE_TEXTMODE_X11_SUPPORT */