1 /* $NetBSD: getch.c,v 1.53 2009/11/01 22:11:27 dsl Exp $ */
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
37 __RCSID("$NetBSD: getch.c,v 1.53 2009/11/01 22:11:27 dsl Exp $");
46 #include "curses_private.h"
49 short state
; /* state of the inkey function */
51 static const struct tcdata tc
[] = {
73 {"%e", KEY_SPREVIOUS
},
80 {"&1", KEY_REFERENCE
},
182 {"kb", KEY_BACKSPACE
},
203 /* Number of TC entries .... */
204 static const int num_tcs
= (sizeof(tc
) / sizeof(struct tcdata
));
206 int ESCDELAY
= 300; /* Delay in ms between keys for esc seq's */
209 #define INBUF_SZ 16 /* size of key buffer - must be larger than
210 * longest multi-key sequence */
211 static wchar_t inbuf
[INBUF_SZ
];
212 static int start
, end
, working
; /* pointers for manipulating inbuf data */
214 /* prototypes for private functions */
215 static void add_key_sequence(SCREEN
*screen
, char *sequence
, int key_type
);
216 static key_entry_t
*add_new_key(keymap_t
*current
, char ch
, int key_type
,
218 static void delete_key_sequence(keymap_t
*current
, int key_type
);
219 static void do_keyok(keymap_t
*current
, int key_type
, bool flag
, int *retval
);
220 static keymap_t
*new_keymap(void); /* create a new keymap */
221 static key_entry_t
*new_key(void); /* create a new key entry */
222 static wchar_t inkey(int to
, int delay
);
225 * Free the storage associated with the given keymap
228 _cursesi_free_keymap(keymap_t
*map
)
232 /* check for, and free, child keymaps */
233 for (i
= 0; i
< MAX_CHAR
; i
++) {
234 if (map
->mapping
[i
] >= 0) {
235 if (map
->key
[map
->mapping
[i
]]->type
== KEYMAP_MULTI
)
236 _cursesi_free_keymap(
237 map
->key
[map
->mapping
[i
]]->value
.next
);
241 /* now free any allocated keymap structs */
242 for (i
= 0; i
< map
->count
; i
+= KEYMAP_ALLOC_CHUNK
) {
252 * Add a new key entry to the keymap pointed to by current. Entry
253 * contains the character to add to the keymap, type is the type of
254 * entry to add (either multikey or leaf) and symbol is the symbolic
255 * value for a leaf type entry. The function returns a pointer to the
259 add_new_key(keymap_t
*current
, char chr
, int key_type
, int symbol
)
261 key_entry_t
*the_key
;
265 __CTRACE(__CTRACE_MISC
,
266 "Adding character %s of type %d, symbol 0x%x\n",
267 unctrl(chr
), key_type
, symbol
);
269 if (current
->mapping
[(unsigned char) chr
] < 0) {
270 if (current
->mapping
[(unsigned char) chr
] == MAPPING_UNUSED
) {
271 /* first time for this char */
272 current
->mapping
[(unsigned char) chr
] =
273 current
->count
; /* map new entry */
276 /* make sure we have room in the key array first */
277 if ((current
->count
& (KEYMAP_ALLOC_CHUNK
- 1)) == 0)
280 realloc(current
->key
,
281 ki
* sizeof(key_entry_t
*)
282 + KEYMAP_ALLOC_CHUNK
* sizeof(key_entry_t
*))) == NULL
) {
284 "Could not malloc for key entry\n");
289 for (i
= 0; i
< KEYMAP_ALLOC_CHUNK
; i
++) {
290 current
->key
[ki
+ i
] = &the_key
[i
];
294 /* the mapping was used but freed, reuse it */
295 ki
= - current
->mapping
[(unsigned char) chr
];
296 current
->mapping
[(unsigned char) chr
] = ki
;
301 /* point at the current key array element to use */
302 the_key
= current
->key
[ki
];
304 the_key
->type
= key_type
;
308 /* need for next key */
310 __CTRACE(__CTRACE_MISC
, "Creating new keymap\n");
312 the_key
->value
.next
= new_keymap();
313 the_key
->enable
= TRUE
;
317 /* the associated symbol for the key */
319 __CTRACE(__CTRACE_MISC
, "Adding leaf key\n");
321 the_key
->value
.symbol
= symbol
;
322 the_key
->enable
= TRUE
;
326 fprintf(stderr
, "add_new_key: bad type passed\n");
330 /* the key is already known - just return the address. */
332 __CTRACE(__CTRACE_MISC
, "Keymap already known\n");
334 the_key
= current
->key
[current
->mapping
[(unsigned char) chr
]];
341 * Delete the given key symbol from the key mappings for the screen.
345 delete_key_sequence(keymap_t
*current
, int key_type
)
351 * we need to iterate over all the keys as there may be
352 * multiple instances of the leaf symbol.
354 for (i
= 0; i
< MAX_CHAR
; i
++) {
355 if (current
->mapping
[i
] < 0)
356 continue; /* no mapping for the key, next! */
358 key
= current
->key
[current
->mapping
[i
]];
360 if (key
->type
== KEYMAP_MULTI
) {
361 /* have not found the leaf, recurse down */
362 delete_key_sequence(key
->value
.next
, key_type
);
363 /* if we deleted the last key in the map, free */
364 if (key
->value
.next
->count
== 0)
365 _cursesi_free_keymap(key
->value
.next
);
366 } else if ((key
->type
== KEYMAP_LEAF
)
367 && (key
->value
.symbol
== key_type
)) {
369 * delete the mapping by negating the current
370 * index - this "holds" the position in the
371 * allocation just in case we later re-add
372 * the key for that mapping.
374 current
->mapping
[i
] = - current
->mapping
[i
];
381 * Add the sequence of characters given in sequence as the key mapping
382 * for the given key symbol.
385 add_key_sequence(SCREEN
*screen
, char *sequence
, int key_type
)
387 key_entry_t
*tmp_key
;
389 int length
, j
, key_ent
;
392 __CTRACE(__CTRACE_MISC
, "add_key_sequence: add key sequence: %s(%s)\n",
393 sequence
, keyname(key_type
));
395 current
= screen
->base_keymap
; /* always start with
397 length
= (int) strlen(sequence
);
400 * OK - we really should never get a zero length string here, either
401 * the termcap entry is there and it has a value or we are not called
402 * at all. Unfortunately, if someone assigns a termcap string to the
403 * ^@ value we get passed a null string which messes up our length.
404 * So, if we get a null string then just insert a leaf value in
405 * the 0th char position of the root keymap. Note that we are
406 * totally screwed if someone terminates a multichar sequence
407 * with ^@... oh well.
412 for (j
= 0; j
< length
- 1; j
++) {
413 /* add the entry to the struct */
414 tmp_key
= add_new_key(current
, sequence
[j
], KEYMAP_MULTI
, 0);
416 /* index into the key array - it's
417 clearer if we stash this */
418 key_ent
= current
->mapping
[(unsigned char) sequence
[j
]];
420 current
->key
[key_ent
] = tmp_key
;
422 /* next key uses this map... */
423 current
= current
->key
[key_ent
]->value
.next
;
427 * This is the last key in the sequence (it may have been the
428 * only one but that does not matter) this means it is a leaf
429 * key and should have a symbol associated with it.
431 tmp_key
= add_new_key(current
, sequence
[length
- 1], KEYMAP_LEAF
,
433 current
->key
[current
->mapping
[(int)sequence
[length
- 1]]] = tmp_key
;
437 * Init_getch - initialise all the pointers & structures needed to make
438 * getch work in keypad mode.
442 __init_getch(SCREEN
*screen
)
444 char entry
[1024], *p
;
451 /* init the inkey state variable */
454 /* init the base keymap */
455 screen
->base_keymap
= new_keymap();
457 /* key input buffer pointers */
458 start
= end
= working
= 0;
460 /* now do the termcap snarfing ... */
462 for (i
= 0; i
< num_tcs
; i
++) {
465 if (t_getstr(screen
->cursesi_genbuf
, tc
[i
].name
,
466 &p
, &limit
) != (char *) NULL
) {
468 __CTRACE(__CTRACE_INIT
,
469 "Processing termcap entry %s, sequence ",
471 length
= (int) strlen(entry
);
472 for (k
= 0; k
<= length
-1; k
++)
473 __CTRACE(__CTRACE_INIT
, "%s", unctrl(entry
[k
]));
474 __CTRACE(__CTRACE_INIT
, "\n");
476 add_key_sequence(screen
, entry
, tc
[i
].symbol
);
484 * new_keymap - allocates & initialises a new keymap structure. This
485 * function returns a pointer to the new keymap.
494 if ((new_map
= malloc(sizeof(keymap_t
))) == NULL
) {
495 perror("Inkey: Cannot allocate new keymap");
499 /* Initialise the new map */
501 for (i
= 0; i
< MAX_CHAR
; i
++) {
502 new_map
->mapping
[i
] = MAPPING_UNUSED
; /* no mapping for char */
505 /* key array will be allocated when first key is added */
512 * new_key - allocates & initialises a new key entry. This function returns
513 * a pointer to the newly allocated key entry.
519 key_entry_t
*new_one
;
522 if ((new_one
= malloc(KEYMAP_ALLOC_CHUNK
* sizeof(key_entry_t
)))
524 perror("inkey: Cannot allocate new key entry chunk");
528 for (i
= 0; i
< KEYMAP_ALLOC_CHUNK
; i
++) {
530 new_one
[i
].value
.next
= NULL
;
537 * inkey - do the work to process keyboard input, check for multi-key
538 * sequences and return the appropriate symbol if we get a match.
543 inkey(int to
, int delay
)
547 keymap_t
*current
= _cursesi_screen
->base_keymap
;
548 FILE *infd
= _cursesi_screen
->infd
;
550 k
= 0; /* XXX gcc -Wuninitialized */
553 __CTRACE(__CTRACE_INPUT
, "inkey (%d, %d)\n", to
, delay
);
555 for (;;) { /* loop until we get a complete key sequence */
557 if (state
== INKEY_NORM
) {
558 if (delay
&& __timeout(delay
) == ERR
)
566 if (delay
&& (__notimeout() == ERR
))
571 __CTRACE(__CTRACE_INPUT
,
572 "inkey (state normal) got '%s'\n", unctrl(k
));
577 INC_POINTER(working
);
579 state
= INKEY_ASSEMBLING
; /* go to the assembling
581 } else if (state
== INKEY_BACKOUT
) {
583 INC_POINTER(working
);
584 if (working
== end
) { /* see if we have run
588 /* if we have then switch to assembling */
589 state
= INKEY_ASSEMBLING
;
591 } else if (state
== INKEY_ASSEMBLING
) {
592 /* assembling a key sequence */
594 if (__timeout(to
? (ESCDELAY
/ 100) : delay
)
598 if (to
&& (__timeout(ESCDELAY
/ 100) == ERR
))
608 if ((to
|| delay
) && (__notimeout() == ERR
))
612 __CTRACE(__CTRACE_INPUT
,
613 "inkey (state assembling) got '%s'\n", unctrl(k
));
615 if (feof(infd
) || c
== -1) { /* inter-char timeout,
616 * start backing out */
619 /* no chars in the buffer, restart */
623 state
= INKEY_TIMEOUT
;
627 INC_POINTER(working
);
631 fprintf(stderr
, "Inkey state screwed - exiting!!!");
636 * Check key has no special meaning and we have not
637 * timed out and the key has not been disabled
639 mapping
= current
->mapping
[k
];
640 if (((state
== INKEY_TIMEOUT
) || (mapping
< 0))
641 || ((current
->key
[mapping
]->type
== KEYMAP_LEAF
)
642 && (current
->key
[mapping
]->enable
== FALSE
))) {
643 /* return the first key we know about */
649 if (start
== end
) { /* only one char processed */
651 } else {/* otherwise we must have more than one char
653 state
= INKEY_BACKOUT
;
656 } else { /* must be part of a multikey sequence */
657 /* check for completed key sequence */
658 if (current
->key
[current
->mapping
[k
]]->type
== KEYMAP_LEAF
) {
659 start
= working
; /* eat the key sequence
662 /* check if inbuf empty now */
664 /* if it is go back to normal */
667 /* otherwise go to backout state */
668 state
= INKEY_BACKOUT
;
671 /* return the symbol */
672 return current
->key
[current
->mapping
[k
]]->value
.symbol
;
676 * Step on to next part of the multi-key
679 current
= current
->key
[current
->mapping
[k
]]->value
.next
;
685 #ifndef _CURSES_USE_MACROS
688 * Read in a character from stdscr.
693 return wgetch(stdscr
);
698 * Read in a character from stdscr at the given location.
701 mvgetch(int y
, int x
)
703 return mvwgetch(stdscr
, y
, x
);
708 * Read in a character from stdscr at the given location in the
712 mvwgetch(WINDOW
*win
, int y
, int x
)
714 if (wmove(win
, y
, x
) == ERR
)
724 * Set the enable flag for a keysym, if the flag is false then
725 * getch will not return this keysym even if the matching key sequence
729 keyok(int key_type
, bool flag
)
733 do_keyok(_cursesi_screen
->base_keymap
, key_type
, flag
, &result
);
739 * Does the actual work for keyok, we need to recurse through the
740 * keymaps finding the passed key symbol.
743 do_keyok(keymap_t
*current
, int key_type
, bool flag
, int *retval
)
749 * we need to iterate over all the keys as there may be
750 * multiple instances of the leaf symbol.
752 for (i
= 0; i
< MAX_CHAR
; i
++) {
753 if (current
->mapping
[i
] < 0)
754 continue; /* no mapping for the key, next! */
756 key
= current
->key
[current
->mapping
[i
]];
758 if (key
->type
== KEYMAP_MULTI
)
759 do_keyok(key
->value
.next
, key_type
, flag
, retval
);
760 else if ((key
->type
== KEYMAP_LEAF
)
761 && (key
->value
.symbol
== key_type
)) {
763 *retval
= OK
; /* we found at least one instance, ok */
770 * Add a custom mapping of a key sequence to key symbol.
774 define_key(char *sequence
, int symbol
)
780 if (sequence
== NULL
)
781 delete_key_sequence(_cursesi_screen
->base_keymap
, symbol
);
783 add_key_sequence(_cursesi_screen
, sequence
, symbol
);
790 * Read in a character from the window.
797 FILE *infd
= _cursesi_screen
->infd
;
800 __CTRACE(__CTRACE_INPUT
, "wgetch: win(%p)\n", win
);
802 if (!(win
->flags
& __SCROLLOK
) && (win
->flags
& __FULLWIN
)
803 && win
->curx
== win
->maxx
- 1 && win
->cury
== win
->maxy
- 1
807 if (is_wintouched(win
))
810 __CTRACE(__CTRACE_INPUT
, "wgetch: __echoit = %d, "
811 "__rawmode = %d, __nl = %d, flags = %#.4x, delay = %d\n",
812 __echoit
, __rawmode
, _cursesi_screen
->nl
, win
->flags
, win
->delay
);
814 if (_cursesi_screen
->resized
) {
815 _cursesi_screen
->resized
= 0;
817 __CTRACE(__CTRACE_INPUT
, "wgetch returning KEY_RESIZE\n");
821 if (_cursesi_screen
->unget_pos
) {
823 __CTRACE(__CTRACE_INPUT
, "wgetch returning char at %d\n",
824 _cursesi_screen
->unget_pos
);
826 _cursesi_screen
->unget_pos
--;
827 c
= _cursesi_screen
->unget_list
[_cursesi_screen
->unget_pos
];
829 waddch(win
, (chtype
) c
);
832 if (__echoit
&& !__rawmode
) {
840 if (win
->flags
& __KEYPAD
) {
844 inp
= inkey (win
->flags
& __NOTIMEOUT
? 0 : 1, 0);
847 if (__nodelay() == ERR
)
852 inp
= inkey(win
->flags
& __NOTIMEOUT
? 0 : 1, win
->delay
);
859 if (__delay() == ERR
)
863 if (__nodelay() == ERR
)
867 if (__timeout(win
->delay
) == ERR
)
876 return ERR
; /* we have timed out */
888 /* we have a key symbol - treat it differently */
889 /* XXXX perhaps __unctrl should be expanded to include
890 * XXXX the keysyms in the table....
892 __CTRACE(__CTRACE_INPUT
, "wgetch assembled keysym 0x%x\n", inp
);
894 __CTRACE(__CTRACE_INPUT
, "wgetch got '%s'\n", unctrl(inp
));
896 if (win
->delay
> -1) {
897 if (__delay() == ERR
)
904 waddch(win
, (chtype
) inp
);
909 if (_cursesi_screen
->nl
&& inp
== 13)
912 return ((inp
< 0) || (inp
== ERR
) ? ERR
: inp
);
917 * Put the character back into the input queue.
922 return __unget((wint_t) c
);
927 * Do the work for ungetch() and unget_wch();
936 __CTRACE(__CTRACE_INPUT
, "__unget(%x)\n", c
);
938 if (_cursesi_screen
->unget_pos
>= _cursesi_screen
->unget_len
) {
939 len
= _cursesi_screen
->unget_len
+ 32;
940 if ((p
= realloc(_cursesi_screen
->unget_list
,
941 sizeof(wchar_t) * len
)) == NULL
) {
942 /* Can't realloc(), so just lose the oldest entry */
943 memmove(_cursesi_screen
->unget_list
,
944 _cursesi_screen
->unget_list
+ sizeof(wchar_t),
945 _cursesi_screen
->unget_len
- 1);
946 _cursesi_screen
->unget_list
[_cursesi_screen
->unget_len
948 _cursesi_screen
->unget_pos
=
949 _cursesi_screen
->unget_len
;
952 _cursesi_screen
->unget_pos
=
953 _cursesi_screen
->unget_len
;
954 _cursesi_screen
->unget_len
= len
;
955 _cursesi_screen
->unget_list
= p
;
958 _cursesi_screen
->unget_list
[_cursesi_screen
->unget_pos
] = c
;
959 _cursesi_screen
->unget_pos
++;