Sync usage with man page.
[netbsd-mini2440.git] / lib / libcurses / getch.c
blob59e9618d3fa82debbd38e32d91b51fa51a3d7ea1
1 /* $NetBSD: getch.c,v 1.53 2009/11/01 22:11:27 dsl Exp $ */
3 /*
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
9 * are met:
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
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)getch.c 8.2 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: getch.c,v 1.53 2009/11/01 22:11:27 dsl Exp $");
38 #endif
39 #endif /* not lint */
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include "curses.h"
46 #include "curses_private.h"
47 #include "keymap.h"
49 short state; /* state of the inkey function */
51 static const struct tcdata tc[] = {
52 {"!1", KEY_SSAVE},
53 {"!2", KEY_SSUSPEND},
54 {"!3", KEY_SUNDO},
55 {"#1", KEY_SHELP},
56 {"#2", KEY_SHOME},
57 {"#3", KEY_SIC},
58 {"#4", KEY_SLEFT},
59 {"%0", KEY_REDO},
60 {"%1", KEY_HELP},
61 {"%2", KEY_MARK},
62 {"%3", KEY_MESSAGE},
63 {"%4", KEY_MOVE},
64 {"%5", KEY_NEXT},
65 {"%6", KEY_OPEN},
66 {"%7", KEY_OPTIONS},
67 {"%8", KEY_PREVIOUS},
68 {"%9", KEY_PRINT},
69 {"%a", KEY_SMESSAGE},
70 {"%b", KEY_SMOVE},
71 {"%c", KEY_SNEXT},
72 {"%d", KEY_SOPTIONS},
73 {"%e", KEY_SPREVIOUS},
74 {"%f", KEY_SPRINT},
75 {"%g", KEY_SREDO},
76 {"%h", KEY_SREPLACE},
77 {"%i", KEY_SRIGHT},
78 {"%j", KEY_SRSUME},
79 {"&0", KEY_SCANCEL},
80 {"&1", KEY_REFERENCE},
81 {"&2", KEY_REFRESH},
82 {"&3", KEY_REPLACE},
83 {"&4", KEY_RESTART},
84 {"&5", KEY_RESUME},
85 {"&6", KEY_SAVE},
86 {"&7", KEY_SUSPEND},
87 {"&8", KEY_UNDO},
88 {"&9", KEY_SBEG},
89 {"*0", KEY_SFIND},
90 {"*1", KEY_SCOMMAND},
91 {"*2", KEY_SCOPY},
92 {"*3", KEY_SCREATE},
93 {"*4", KEY_SDC},
94 {"*5", KEY_SDL},
95 {"*6", KEY_SELECT},
96 {"*7", KEY_SEND},
97 {"*8", KEY_SEOL},
98 {"*9", KEY_SEXIT},
99 {"@0", KEY_FIND},
100 {"@1", KEY_BEG},
101 {"@2", KEY_CANCEL},
102 {"@3", KEY_CLOSE},
103 {"@4", KEY_COMMAND},
104 {"@5", KEY_COPY},
105 {"@6", KEY_CREATE},
106 {"@7", KEY_END},
107 {"@8", KEY_ENTER},
108 {"@9", KEY_EXIT},
109 {"F1", KEY_F(11)},
110 {"F2", KEY_F(12)},
111 {"F3", KEY_F(13)},
112 {"F4", KEY_F(14)},
113 {"F5", KEY_F(15)},
114 {"F6", KEY_F(16)},
115 {"F7", KEY_F(17)},
116 {"F8", KEY_F(18)},
117 {"F9", KEY_F(19)},
118 {"FA", KEY_F(20)},
119 {"FB", KEY_F(21)},
120 {"FC", KEY_F(22)},
121 {"FD", KEY_F(23)},
122 {"FE", KEY_F(24)},
123 {"FF", KEY_F(25)},
124 {"FG", KEY_F(26)},
125 {"FH", KEY_F(27)},
126 {"FI", KEY_F(28)},
127 {"FJ", KEY_F(29)},
128 {"FK", KEY_F(30)},
129 {"FL", KEY_F(31)},
130 {"FM", KEY_F(32)},
131 {"FN", KEY_F(33)},
132 {"FO", KEY_F(34)},
133 {"FP", KEY_F(35)},
134 {"FQ", KEY_F(36)},
135 {"FR", KEY_F(37)},
136 {"FS", KEY_F(38)},
137 {"FT", KEY_F(39)},
138 {"FU", KEY_F(40)},
139 {"FV", KEY_F(41)},
140 {"FW", KEY_F(42)},
141 {"FX", KEY_F(43)},
142 {"FY", KEY_F(44)},
143 {"FZ", KEY_F(45)},
144 {"Fa", KEY_F(46)},
145 {"Fb", KEY_F(47)},
146 {"Fc", KEY_F(48)},
147 {"Fd", KEY_F(49)},
148 {"Fe", KEY_F(50)},
149 {"Ff", KEY_F(51)},
150 {"Fg", KEY_F(52)},
151 {"Fh", KEY_F(53)},
152 {"Fi", KEY_F(54)},
153 {"Fj", KEY_F(55)},
154 {"Fk", KEY_F(56)},
155 {"Fl", KEY_F(57)},
156 {"Fm", KEY_F(58)},
157 {"Fn", KEY_F(59)},
158 {"Fo", KEY_F(60)},
159 {"Fp", KEY_F(61)},
160 {"Fq", KEY_F(62)},
161 {"Fr", KEY_F(63)},
162 {"K1", KEY_A1},
163 {"K2", KEY_B2},
164 {"K3", KEY_A3},
165 {"K4", KEY_C1},
166 {"K5", KEY_C3},
167 {"Km", KEY_MOUSE},
168 {"k0", KEY_F0},
169 {"k1", KEY_F(1)},
170 {"k2", KEY_F(2)},
171 {"k3", KEY_F(3)},
172 {"k4", KEY_F(4)},
173 {"k5", KEY_F(5)},
174 {"k6", KEY_F(6)},
175 {"k7", KEY_F(7)},
176 {"k8", KEY_F(8)},
177 {"k9", KEY_F(9)},
178 {"k;", KEY_F(10)},
179 {"kA", KEY_IL},
180 {"ka", KEY_CATAB},
181 {"kB", KEY_BTAB},
182 {"kb", KEY_BACKSPACE},
183 {"kC", KEY_CLEAR},
184 {"kD", KEY_DC},
185 {"kd", KEY_DOWN},
186 {"kE", KEY_EOL},
187 {"kF", KEY_SF},
188 {"kH", KEY_LL},
189 {"kh", KEY_HOME},
190 {"kI", KEY_IC},
191 {"kL", KEY_DL},
192 {"kl", KEY_LEFT},
193 {"kM", KEY_EIC},
194 {"kN", KEY_NPAGE},
195 {"kP", KEY_PPAGE},
196 {"kR", KEY_SR},
197 {"kr", KEY_RIGHT},
198 {"kS", KEY_EOS},
199 {"kT", KEY_STAB},
200 {"kt", KEY_CTAB},
201 {"ku", KEY_UP}
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 */
208 /* Key buffer */
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,
217 int symbol);
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
227 void
228 _cursesi_free_keymap(keymap_t *map)
230 int i;
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) {
243 free(map->key[i]);
246 free(map->key);
247 free(map);
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
256 * new keymap entry.
258 static key_entry_t *
259 add_new_key(keymap_t *current, char chr, int key_type, int symbol)
261 key_entry_t *the_key;
262 int i, ki;
264 #ifdef DEBUG
265 __CTRACE(__CTRACE_MISC,
266 "Adding character %s of type %d, symbol 0x%x\n",
267 unctrl(chr), key_type, symbol);
268 #endif
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 */
274 ki = current->count;
276 /* make sure we have room in the key array first */
277 if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
279 if ((current->key =
280 realloc(current->key,
281 ki * sizeof(key_entry_t *)
282 + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
283 fprintf(stderr,
284 "Could not malloc for key entry\n");
285 exit(1);
288 the_key = new_key();
289 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
290 current->key[ki + i] = &the_key[i];
293 } else {
294 /* the mapping was used but freed, reuse it */
295 ki = - current->mapping[(unsigned char) chr];
296 current->mapping[(unsigned char) chr] = ki;
299 current->count++;
301 /* point at the current key array element to use */
302 the_key = current->key[ki];
304 the_key->type = key_type;
306 switch (key_type) {
307 case KEYMAP_MULTI:
308 /* need for next key */
309 #ifdef DEBUG
310 __CTRACE(__CTRACE_MISC, "Creating new keymap\n");
311 #endif
312 the_key->value.next = new_keymap();
313 the_key->enable = TRUE;
314 break;
316 case KEYMAP_LEAF:
317 /* the associated symbol for the key */
318 #ifdef DEBUG
319 __CTRACE(__CTRACE_MISC, "Adding leaf key\n");
320 #endif
321 the_key->value.symbol = symbol;
322 the_key->enable = TRUE;
323 break;
325 default:
326 fprintf(stderr, "add_new_key: bad type passed\n");
327 exit(1);
329 } else {
330 /* the key is already known - just return the address. */
331 #ifdef DEBUG
332 __CTRACE(__CTRACE_MISC, "Keymap already known\n");
333 #endif
334 the_key = current->key[current->mapping[(unsigned char) chr]];
337 return the_key;
341 * Delete the given key symbol from the key mappings for the screen.
344 void
345 delete_key_sequence(keymap_t *current, int key_type)
347 key_entry_t *key;
348 int i;
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];
375 current->count--;
381 * Add the sequence of characters given in sequence as the key mapping
382 * for the given key symbol.
384 void
385 add_key_sequence(SCREEN *screen, char *sequence, int key_type)
387 key_entry_t *tmp_key;
388 keymap_t *current;
389 int length, j, key_ent;
391 #ifdef DEBUG
392 __CTRACE(__CTRACE_MISC, "add_key_sequence: add key sequence: %s(%s)\n",
393 sequence, keyname(key_type));
394 #endif /* DEBUG */
395 current = screen->base_keymap; /* always start with
396 * base keymap. */
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.
409 if (length == 0)
410 length = 1;
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,
432 key_type);
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.
441 void
442 __init_getch(SCREEN *screen)
444 char entry[1024], *p;
445 int i;
446 size_t limit;
447 #ifdef DEBUG
448 int k, length;
449 #endif
451 /* init the inkey state variable */
452 state = INKEY_NORM;
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++) {
463 p = entry;
464 limit = 1023;
465 if (t_getstr(screen->cursesi_genbuf, tc[i].name,
466 &p, &limit) != (char *) NULL) {
467 #ifdef DEBUG
468 __CTRACE(__CTRACE_INIT,
469 "Processing termcap entry %s, sequence ",
470 tc[i].name);
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");
475 #endif
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.
488 static keymap_t *
489 new_keymap(void)
491 int i;
492 keymap_t *new_map;
494 if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
495 perror("Inkey: Cannot allocate new keymap");
496 exit(2);
499 /* Initialise the new map */
500 new_map->count = 0;
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 */
506 new_map->key = NULL;
508 return new_map;
512 * new_key - allocates & initialises a new key entry. This function returns
513 * a pointer to the newly allocated key entry.
516 static key_entry_t *
517 new_key(void)
519 key_entry_t *new_one;
520 int i;
522 if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
523 == NULL) {
524 perror("inkey: Cannot allocate new key entry chunk");
525 exit(2);
528 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
529 new_one[i].type = 0;
530 new_one[i].value.next = NULL;
533 return new_one;
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.
542 wchar_t
543 inkey(int to, int delay)
545 wchar_t k;
546 int c, mapping;
547 keymap_t *current = _cursesi_screen->base_keymap;
548 FILE *infd = _cursesi_screen->infd;
550 k = 0; /* XXX gcc -Wuninitialized */
552 #ifdef DEBUG
553 __CTRACE(__CTRACE_INPUT, "inkey (%d, %d)\n", to, delay);
554 #endif
555 for (;;) { /* loop until we get a complete key sequence */
556 reread:
557 if (state == INKEY_NORM) {
558 if (delay && __timeout(delay) == ERR)
559 return ERR;
560 c = fgetc(infd);
561 if (c == EOF) {
562 clearerr(infd);
563 return ERR;
566 if (delay && (__notimeout() == ERR))
567 return ERR;
569 k = (wchar_t) c;
570 #ifdef DEBUG
571 __CTRACE(__CTRACE_INPUT,
572 "inkey (state normal) got '%s'\n", unctrl(k));
573 #endif
575 working = start;
576 inbuf[working] = k;
577 INC_POINTER(working);
578 end = working;
579 state = INKEY_ASSEMBLING; /* go to the assembling
580 * state now */
581 } else if (state == INKEY_BACKOUT) {
582 k = inbuf[working];
583 INC_POINTER(working);
584 if (working == end) { /* see if we have run
585 * out of keys in the
586 * backlog */
588 /* if we have then switch to assembling */
589 state = INKEY_ASSEMBLING;
591 } else if (state == INKEY_ASSEMBLING) {
592 /* assembling a key sequence */
593 if (delay) {
594 if (__timeout(to ? (ESCDELAY / 100) : delay)
595 == ERR)
596 return ERR;
597 } else {
598 if (to && (__timeout(ESCDELAY / 100) == ERR))
599 return ERR;
602 c = fgetc(infd);
603 if (ferror(infd)) {
604 clearerr(infd);
605 return ERR;
608 if ((to || delay) && (__notimeout() == ERR))
609 return ERR;
611 #ifdef DEBUG
612 __CTRACE(__CTRACE_INPUT,
613 "inkey (state assembling) got '%s'\n", unctrl(k));
614 #endif
615 if (feof(infd) || c == -1) { /* inter-char timeout,
616 * start backing out */
617 clearerr(infd);
618 if (start == end)
619 /* no chars in the buffer, restart */
620 goto reread;
622 k = inbuf[start];
623 state = INKEY_TIMEOUT;
624 } else {
625 k = (wchar_t) c;
626 inbuf[working] = k;
627 INC_POINTER(working);
628 end = working;
630 } else {
631 fprintf(stderr, "Inkey state screwed - exiting!!!");
632 exit(2);
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 */
644 k = inbuf[start];
646 INC_POINTER(start);
647 working = start;
649 if (start == end) { /* only one char processed */
650 state = INKEY_NORM;
651 } else {/* otherwise we must have more than one char
652 * to backout */
653 state = INKEY_BACKOUT;
655 return k;
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
660 * in inbuf */
662 /* check if inbuf empty now */
663 if (start == end) {
664 /* if it is go back to normal */
665 state = INKEY_NORM;
666 } else {
667 /* otherwise go to backout state */
668 state = INKEY_BACKOUT;
671 /* return the symbol */
672 return current->key[current->mapping[k]]->value.symbol;
674 } else {
676 * Step on to next part of the multi-key
677 * sequence.
679 current = current->key[current->mapping[k]]->value.next;
685 #ifndef _CURSES_USE_MACROS
687 * getch --
688 * Read in a character from stdscr.
691 getch(void)
693 return wgetch(stdscr);
697 * mvgetch --
698 * Read in a character from stdscr at the given location.
701 mvgetch(int y, int x)
703 return mvwgetch(stdscr, y, x);
707 * mvwgetch --
708 * Read in a character from stdscr at the given location in the
709 * given window.
712 mvwgetch(WINDOW *win, int y, int x)
714 if (wmove(win, y, x) == ERR)
715 return ERR;
717 return wgetch(win);
720 #endif
723 * keyok --
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
726 * is seen.
729 keyok(int key_type, bool flag)
731 int result = ERR;
733 do_keyok(_cursesi_screen->base_keymap, key_type, flag, &result);
734 return result;
738 * do_keyok --
739 * Does the actual work for keyok, we need to recurse through the
740 * keymaps finding the passed key symbol.
742 void
743 do_keyok(keymap_t *current, int key_type, bool flag, int *retval)
745 key_entry_t *key;
746 int i;
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)) {
762 key->enable = flag;
763 *retval = OK; /* we found at least one instance, ok */
769 * define_key --
770 * Add a custom mapping of a key sequence to key symbol.
774 define_key(char *sequence, int symbol)
777 if (symbol <= 0)
778 return ERR;
780 if (sequence == NULL)
781 delete_key_sequence(_cursesi_screen->base_keymap, symbol);
782 else
783 add_key_sequence(_cursesi_screen, sequence, symbol);
785 return OK;
789 * wgetch --
790 * Read in a character from the window.
793 wgetch(WINDOW *win)
795 int inp, weset;
796 int c;
797 FILE *infd = _cursesi_screen->infd;
799 #ifdef DEBUG
800 __CTRACE(__CTRACE_INPUT, "wgetch: win(%p)\n", win);
801 #endif
802 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
803 && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
804 && __echoit)
805 return (ERR);
807 if (is_wintouched(win))
808 wrefresh(win);
809 #ifdef DEBUG
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);
813 #endif
814 if (_cursesi_screen->resized) {
815 _cursesi_screen->resized = 0;
816 #ifdef DEBUG
817 __CTRACE(__CTRACE_INPUT, "wgetch returning KEY_RESIZE\n");
818 #endif
819 return KEY_RESIZE;
821 if (_cursesi_screen->unget_pos) {
822 #ifdef DEBUG
823 __CTRACE(__CTRACE_INPUT, "wgetch returning char at %d\n",
824 _cursesi_screen->unget_pos);
825 #endif
826 _cursesi_screen->unget_pos--;
827 c = _cursesi_screen->unget_list[_cursesi_screen->unget_pos];
828 if (__echoit)
829 waddch(win, (chtype) c);
830 return c;
832 if (__echoit && !__rawmode) {
833 cbreak();
834 weset = 1;
835 } else
836 weset = 0;
838 __save_termios();
840 if (win->flags & __KEYPAD) {
841 switch (win->delay)
843 case -1:
844 inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
845 break;
846 case 0:
847 if (__nodelay() == ERR)
848 return ERR;
849 inp = inkey(0, 0);
850 break;
851 default:
852 inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
853 break;
855 } else {
856 switch (win->delay)
858 case -1:
859 if (__delay() == ERR)
860 return ERR;
861 break;
862 case 0:
863 if (__nodelay() == ERR)
864 return ERR;
865 break;
866 default:
867 if (__timeout(win->delay) == ERR)
868 return ERR;
869 break;
872 c = fgetc(infd);
873 if (feof(infd)) {
874 clearerr(infd);
875 __restore_termios();
876 return ERR; /* we have timed out */
879 if (ferror(infd)) {
880 clearerr(infd);
881 inp = ERR;
882 } else {
883 inp = c;
886 #ifdef DEBUG
887 if (inp > 255)
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);
893 else
894 __CTRACE(__CTRACE_INPUT, "wgetch got '%s'\n", unctrl(inp));
895 #endif
896 if (win->delay > -1) {
897 if (__delay() == ERR)
898 return ERR;
901 __restore_termios();
903 if (__echoit)
904 waddch(win, (chtype) inp);
906 if (weset)
907 nocbreak();
909 if (_cursesi_screen->nl && inp == 13)
910 inp = 10;
912 return ((inp < 0) || (inp == ERR) ? ERR : inp);
916 * ungetch --
917 * Put the character back into the input queue.
920 ungetch(int c)
922 return __unget((wint_t) c);
926 * __unget --
927 * Do the work for ungetch() and unget_wch();
930 __unget(wint_t c)
932 wchar_t *p;
933 int len;
935 #ifdef DEBUG
936 __CTRACE(__CTRACE_INPUT, "__unget(%x)\n", c);
937 #endif
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
947 - 1] = c;
948 _cursesi_screen->unget_pos =
949 _cursesi_screen->unget_len;
950 return OK;
951 } else {
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++;
960 return OK;