getch.c revision 1.49 1 /* $NetBSD: getch.c,v 1.49 2007/08/28 12:28:18 blymn Exp $ */
2
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
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.
18 *
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.
30 */
31
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.49 2007/08/28 12:28:18 blymn Exp $");
38 #endif
39 #endif /* not lint */
40
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"
48
49 short state; /* state of the inkey function */
50
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}
202 };
203 /* Number of TC entries .... */
204 static const int num_tcs = (sizeof(tc) / sizeof(struct tcdata));
205
206 int ESCDELAY = 300; /* Delay in ms between keys for esc seq's */
207
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 */
213
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);
223
224 /*
225 * Free the storage associated with the given keymap
226 */
227 void
228 _cursesi_free_keymap(keymap_t *map)
229 {
230 int i;
231
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);
238 }
239 }
240
241 /* now free any allocated keymap structs */
242 for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) {
243 free(map->key[i]);
244 }
245
246 free(map->key);
247 free(map);
248 }
249
250
251 /*
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.
257 */
258 static key_entry_t *
259 add_new_key(keymap_t *current, char chr, int key_type, int symbol)
260 {
261 key_entry_t *the_key;
262 int i, ki;
263
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;
275
276 /* make sure we have room in the key array first */
277 if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
278 {
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);
286 }
287
288 the_key = new_key();
289 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
290 current->key[ki + i] = &the_key[i];
291 }
292 }
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;
297 }
298
299 current->count++;
300
301 /* point at the current key array element to use */
302 the_key = current->key[ki];
303
304 the_key->type = key_type;
305
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;
315
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;
324
325 default:
326 fprintf(stderr, "add_new_key: bad type passed\n");
327 exit(1);
328 }
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]];
335 }
336
337 return the_key;
338 }
339
340 /*
341 * Delete the given key symbol from the key mappings for the screen.
342 *
343 */
344 void
345 delete_key_sequence(keymap_t *current, int key_type)
346 {
347 key_entry_t *key;
348 int i;
349
350 /*
351 * we need to iterate over all the keys as there may be
352 * multiple instances of the leaf symbol.
353 */
354 for (i = 0; i < MAX_CHAR; i++) {
355 if (current->mapping[i] < 0)
356 continue; /* no mapping for the key, next! */
357
358 key = current->key[current->mapping[i]];
359
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)) {
368 /*
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.
373 */
374 current->mapping[i] = - current->mapping[i];
375 current->count--;
376 }
377 }
378 }
379
380 /*
381 * Add the sequence of characters given in sequence as the key mapping
382 * for the given key symbol.
383 */
384 void
385 add_key_sequence(SCREEN *screen, char *sequence, int key_type)
386 {
387 key_entry_t *tmp_key;
388 keymap_t *current;
389 int length, j, key_ent;
390
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);
398
399 /*
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.
408 */
409 if (length == 0)
410 length = 1;
411
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);
415
416 /* index into the key array - it's
417 clearer if we stash this */
418 key_ent = current->mapping[(unsigned char) sequence[j]];
419
420 current->key[key_ent] = tmp_key;
421
422 /* next key uses this map... */
423 current = current->key[key_ent]->value.next;
424 }
425
426 /*
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.
430 */
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;
434 }
435
436 /*
437 * Init_getch - initialise all the pointers & structures needed to make
438 * getch work in keypad mode.
439 *
440 */
441 void
442 __init_getch(SCREEN *screen)
443 {
444 char entry[1024], *p;
445 int i;
446 size_t limit;
447 #ifdef DEBUG
448 int k, length;
449 #endif
450
451 /* init the inkey state variable */
452 state = INKEY_NORM;
453
454 /* init the base keymap */
455 screen->base_keymap = new_keymap();
456
457 /* key input buffer pointers */
458 start = end = working = 0;
459
460 /* now do the termcap snarfing ... */
461
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);
477 }
478
479 }
480 }
481
482
483 /*
484 * new_keymap - allocates & initialises a new keymap structure. This
485 * function returns a pointer to the new keymap.
486 *
487 */
488 static keymap_t *
489 new_keymap(void)
490 {
491 int i;
492 keymap_t *new_map;
493
494 if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
495 perror("Inkey: Cannot allocate new keymap");
496 exit(2);
497 }
498
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 */
503 }
504
505 /* key array will be allocated when first key is added */
506 new_map->key = NULL;
507
508 return new_map;
509 }
510
511 /*
512 * new_key - allocates & initialises a new key entry. This function returns
513 * a pointer to the newly allocated key entry.
514 *
515 */
516 static key_entry_t *
517 new_key(void)
518 {
519 key_entry_t *new_one;
520 int i;
521
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);
526 }
527
528 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
529 new_one[i].type = 0;
530 new_one[i].value.next = NULL;
531 }
532
533 return new_one;
534 }
535
536 /*
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.
539 *
540 */
541
542 wchar_t
543 inkey(int to, int delay)
544 {
545 wchar_t k;
546 int c, mapping;
547 keymap_t *current = _cursesi_screen->base_keymap;
548 FILE *infd = _cursesi_screen->infd;
549
550 k = 0; /* XXX gcc -Wuninitialized */
551
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 = getchar();
561 if (_cursesi_screen->resized) {
562 if (c != -1)
563 ungetch(c);
564 _cursesi_screen->resized = 0;
565 clearerr(infd);
566 return KEY_RESIZE;
567 }
568 if (c == EOF) {
569 clearerr(infd);
570 return ERR;
571 }
572
573 if (delay && (__notimeout() == ERR))
574 return ERR;
575
576 k = (wchar_t) c;
577 #ifdef DEBUG
578 __CTRACE(__CTRACE_INPUT,
579 "inkey (state normal) got '%s'\n", unctrl(k));
580 #endif
581
582 working = start;
583 inbuf[working] = k;
584 INC_POINTER(working);
585 end = working;
586 state = INKEY_ASSEMBLING; /* go to the assembling
587 * state now */
588 } else if (state == INKEY_BACKOUT) {
589 k = inbuf[working];
590 INC_POINTER(working);
591 if (working == end) { /* see if we have run
592 * out of keys in the
593 * backlog */
594
595 /* if we have then switch to assembling */
596 state = INKEY_ASSEMBLING;
597 }
598 } else if (state == INKEY_ASSEMBLING) {
599 /* assembling a key sequence */
600 if (delay) {
601 if (__timeout(to ? (ESCDELAY / 100) : delay)
602 == ERR)
603 return ERR;
604 } else {
605 if (to && (__timeout(ESCDELAY / 100) == ERR))
606 return ERR;
607 }
608
609 c = getchar();
610 if (_cursesi_screen->resized) {
611 if (c != -1)
612 ungetch(c);
613 _cursesi_screen->resized = 0;
614 clearerr(infd);
615 return KEY_RESIZE;
616 }
617 if (c == -1 || ferror(infd)) {
618 clearerr(infd);
619 return ERR;
620 }
621
622 if ((to || delay) && (__notimeout() == ERR))
623 return ERR;
624
625 k = (wchar_t) c;
626 #ifdef DEBUG
627 __CTRACE(__CTRACE_INPUT,
628 "inkey (state assembling) got '%s'\n", unctrl(k));
629 #endif
630 if (feof(infd)) { /* inter-char timeout,
631 * start backing out */
632 clearerr(infd);
633 if (start == end)
634 /* no chars in the buffer, restart */
635 goto reread;
636
637 k = inbuf[start];
638 state = INKEY_TIMEOUT;
639 } else {
640 inbuf[working] = k;
641 INC_POINTER(working);
642 end = working;
643 }
644 } else {
645 fprintf(stderr, "Inkey state screwed - exiting!!!");
646 exit(2);
647 }
648
649 /*
650 * Check key has no special meaning and we have not
651 * timed out and the key has not been disabled
652 */
653 mapping = current->mapping[k];
654 if (((state == INKEY_TIMEOUT) || (mapping < 0))
655 || ((current->key[mapping]->type == KEYMAP_LEAF)
656 && (current->key[mapping]->enable == FALSE))) {
657 /* return the first key we know about */
658 k = inbuf[start];
659
660 INC_POINTER(start);
661 working = start;
662
663 if (start == end) { /* only one char processed */
664 state = INKEY_NORM;
665 } else {/* otherwise we must have more than one char
666 * to backout */
667 state = INKEY_BACKOUT;
668 }
669 return k;
670 } else { /* must be part of a multikey sequence */
671 /* check for completed key sequence */
672 if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
673 start = working; /* eat the key sequence
674 * in inbuf */
675
676 /* check if inbuf empty now */
677 if (start == end) {
678 /* if it is go back to normal */
679 state = INKEY_NORM;
680 } else {
681 /* otherwise go to backout state */
682 state = INKEY_BACKOUT;
683 }
684
685 /* return the symbol */
686 return current->key[current->mapping[k]]->value.symbol;
687
688 } else {
689 /*
690 * Step on to next part of the multi-key
691 * sequence.
692 */
693 current = current->key[current->mapping[k]]->value.next;
694 }
695 }
696 }
697 }
698
699 #ifndef _CURSES_USE_MACROS
700 /*
701 * getch --
702 * Read in a character from stdscr.
703 */
704 int
705 getch(void)
706 {
707 return wgetch(stdscr);
708 }
709
710 /*
711 * mvgetch --
712 * Read in a character from stdscr at the given location.
713 */
714 int
715 mvgetch(int y, int x)
716 {
717 return mvwgetch(stdscr, y, x);
718 }
719
720 /*
721 * mvwgetch --
722 * Read in a character from stdscr at the given location in the
723 * given window.
724 */
725 int
726 mvwgetch(WINDOW *win, int y, int x)
727 {
728 if (wmove(win, y, x) == ERR)
729 return ERR;
730
731 return wgetch(win);
732 }
733
734 #endif
735
736 /*
737 * keyok --
738 * Set the enable flag for a keysym, if the flag is false then
739 * getch will not return this keysym even if the matching key sequence
740 * is seen.
741 */
742 int
743 keyok(int key_type, bool flag)
744 {
745 int result = ERR;
746
747 do_keyok(_cursesi_screen->base_keymap, key_type, flag, &result);
748 return result;
749 }
750
751 /*
752 * do_keyok --
753 * Does the actual work for keyok, we need to recurse through the
754 * keymaps finding the passed key symbol.
755 */
756 void
757 do_keyok(keymap_t *current, int key_type, bool flag, int *retval)
758 {
759 key_entry_t *key;
760 int i;
761
762 /*
763 * we need to iterate over all the keys as there may be
764 * multiple instances of the leaf symbol.
765 */
766 for (i = 0; i < MAX_CHAR; i++) {
767 if (current->mapping[i] < 0)
768 continue; /* no mapping for the key, next! */
769
770 key = current->key[current->mapping[i]];
771
772 if (key->type == KEYMAP_MULTI)
773 do_keyok(key->value.next, key_type, flag, retval);
774 else if ((key->type == KEYMAP_LEAF)
775 && (key->value.symbol == key_type)) {
776 key->enable = flag;
777 *retval = OK; /* we found at least one instance, ok */
778 }
779 }
780 }
781
782 /*
783 * define_key --
784 * Add a custom mapping of a key sequence to key symbol.
785 *
786 */
787 int
788 define_key(char *sequence, int symbol)
789 {
790
791 if (symbol <= 0)
792 return ERR;
793
794 if (sequence == NULL)
795 delete_key_sequence(_cursesi_screen->base_keymap, symbol);
796 else
797 add_key_sequence(_cursesi_screen, sequence, symbol);
798
799 return OK;
800 }
801
802 /*
803 * wgetch --
804 * Read in a character from the window.
805 */
806 int
807 wgetch(WINDOW *win)
808 {
809 int inp, weset;
810 int c;
811 FILE *infd = _cursesi_screen->infd;
812
813 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
814 && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
815 && __echoit)
816 return (ERR);
817
818 if (is_wintouched(win))
819 wrefresh(win);
820 #ifdef DEBUG
821 __CTRACE(__CTRACE_INPUT, "wgetch: __echoit = %d, "
822 "__rawmode = %d, __nl = %d, flags = %#.4x\n",
823 __echoit, __rawmode, _cursesi_screen->nl, win->flags);
824 #endif
825 if (__echoit && !__rawmode) {
826 cbreak();
827 weset = 1;
828 } else
829 weset = 0;
830
831 __save_termios();
832
833 if (win->flags & __KEYPAD) {
834 switch (win->delay)
835 {
836 case -1:
837 inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
838 break;
839 case 0:
840 if (__nodelay() == ERR) {
841 __restore_termios();
842 return ERR;
843 }
844 inp = inkey(0, 0);
845 break;
846 default:
847 inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
848 break;
849 }
850 } else {
851 switch (win->delay)
852 {
853 case -1:
854 if (__delay() == ERR) {
855 __restore_termios();
856 return ERR;
857 }
858 break;
859 case 0:
860 if (__nodelay() == ERR) {
861 __restore_termios();
862 return ERR;
863 }
864 break;
865 default:
866 if (__timeout(win->delay) == ERR) {
867 __restore_termios();
868 return ERR;
869 }
870 break;
871 }
872
873 c = getchar();
874 if (_cursesi_screen->resized) {
875 if (c != -1)
876 ungetch(c);
877 _cursesi_screen->resized = 0;
878 clearerr(infd);
879 __restore_termios();
880 return KEY_RESIZE;
881 }
882 if (feof(infd)) {
883 clearerr(infd);
884 __restore_termios();
885 return ERR; /* we have timed out */
886 }
887
888 if (ferror(infd)) {
889 clearerr(infd);
890 inp = ERR;
891 } else {
892 inp = c;
893 }
894 }
895 #ifdef DEBUG
896 if (inp > 255)
897 /* we have a key symbol - treat it differently */
898 /* XXXX perhaps __unctrl should be expanded to include
899 * XXXX the keysyms in the table....
900 */
901 __CTRACE(__CTRACE_INPUT, "wgetch assembled keysym 0x%x\n", inp);
902 else
903 __CTRACE(__CTRACE_INPUT, "wgetch got '%s'\n", unctrl(inp));
904 #endif
905 if (win->delay > -1) {
906 if (__delay() == ERR) {
907 __restore_termios();
908 return ERR;
909 }
910 }
911
912 __restore_termios();
913
914 if (__echoit)
915 waddch(win, (chtype) inp);
916
917 if (weset)
918 nocbreak();
919
920 if (_cursesi_screen->nl && inp == 13)
921 inp = 10;
922
923 return ((inp < 0) || (inp == ERR) ? ERR : inp);
924 }
925
926 /*
927 * ungetch --
928 * Put the character back into the input queue.
929 */
930 int
931 ungetch(int c)
932 {
933 return ((ungetc(c, _cursesi_screen->infd) == EOF) ? ERR : OK);
934 }
935