getch.c revision 1.46.6.1 1 /* $NetBSD: getch.c,v 1.46.6.1 2007/01/21 11:38:59 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.46.6.1 2007/01/21 11:38:59 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 int ESCDELAY = 300; /* Delay in ms between keys for esc seq's */
50
51 /* Key buffer */
52 #define INBUF_SZ 16 /* size of key buffer - must be larger than
53 * longest multi-key sequence */
54 static wchar_t inbuf[INBUF_SZ];
55 static int start, end, working; /* pointers for manipulating inbuf data */
56
57 /* prototypes for private functions */
58 static void add_key_sequence(SCREEN *screen, char *sequence, int key_type);
59 static key_entry_t *add_new_key(keymap_t *current, char ch, int key_type,
60 int symbol);
61 static void delete_key_sequence(keymap_t *current, int key_type);
62 static void do_keyok(keymap_t *current, int key_type, bool flag, int *retval);
63 static keymap_t *new_keymap(void); /* create a new keymap */
64 static key_entry_t *new_key(void); /* create a new key entry */
65 static wchar_t inkey(int to, int delay);
66
67 /*
68 * Free the storage associated with the given keymap
69 */
70 void
71 _cursesi_free_keymap(keymap_t *map)
72 {
73 int i;
74
75 /* check for, and free, child keymaps */
76 for (i = 0; i < MAX_CHAR; i++) {
77 if (map->mapping[i] >= 0) {
78 if (map->key[map->mapping[i]]->type == KEYMAP_MULTI)
79 _cursesi_free_keymap(
80 map->key[map->mapping[i]]->value.next);
81 }
82 }
83
84 /* now free any allocated keymap structs */
85 for (i = 0; i < map->count; i += KEYMAP_ALLOC_CHUNK) {
86 free(map->key[i]);
87 }
88
89 free(map->key);
90 free(map);
91 }
92
93
94 /*
95 * Add a new key entry to the keymap pointed to by current. Entry
96 * contains the character to add to the keymap, type is the type of
97 * entry to add (either multikey or leaf) and symbol is the symbolic
98 * value for a leaf type entry. The function returns a pointer to the
99 * new keymap entry.
100 */
101 static key_entry_t *
102 add_new_key(keymap_t *current, char chr, int key_type, int symbol)
103 {
104 key_entry_t *the_key;
105 int i, ki;
106
107 #ifdef DEBUG
108 __CTRACE("Adding character %s of type %d, symbol 0x%x\n", unctrl(chr),
109 key_type, symbol);
110 #endif
111 if (current->mapping[(unsigned char) chr] < 0) {
112 if (current->mapping[(unsigned char) chr] == MAPPING_UNUSED) {
113 /* first time for this char */
114 current->mapping[(unsigned char) chr] =
115 current->count; /* map new entry */
116 ki = current->count;
117
118 /* make sure we have room in the key array first */
119 if ((current->count & (KEYMAP_ALLOC_CHUNK - 1)) == 0)
120 {
121 if ((current->key =
122 realloc(current->key,
123 ki * sizeof(key_entry_t *)
124 + KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t *))) == NULL) {
125 fprintf(stderr,
126 "Could not malloc for key entry\n");
127 exit(1);
128 }
129
130 the_key = new_key();
131 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
132 current->key[ki + i] = &the_key[i];
133 }
134 }
135 } else {
136 /* the mapping was used but freed, reuse it */
137 ki = - current->mapping[(unsigned char) chr];
138 current->mapping[(unsigned char) chr] = ki;
139 }
140
141 current->count++;
142
143 /* point at the current key array element to use */
144 the_key = current->key[ki];
145
146 the_key->type = key_type;
147
148 switch (key_type) {
149 case KEYMAP_MULTI:
150 /* need for next key */
151 #ifdef DEBUG
152 __CTRACE("Creating new keymap\n");
153 #endif
154 the_key->value.next = new_keymap();
155 the_key->enable = TRUE;
156 break;
157
158 case KEYMAP_LEAF:
159 /* the associated symbol for the key */
160 #ifdef DEBUG
161 __CTRACE("Adding leaf key\n");
162 #endif
163 the_key->value.symbol = symbol;
164 the_key->enable = TRUE;
165 break;
166
167 default:
168 fprintf(stderr, "add_new_key: bad type passed\n");
169 exit(1);
170 }
171 } else {
172 /* the key is already known - just return the address. */
173 #ifdef DEBUG
174 __CTRACE("Keymap already known\n");
175 #endif
176 the_key = current->key[current->mapping[(unsigned char) chr]];
177 }
178
179 return the_key;
180 }
181
182 /*
183 * Delete the given key symbol from the key mappings for the screen.
184 *
185 */
186 void
187 delete_key_sequence(keymap_t *current, int key_type)
188 {
189 key_entry_t *key;
190 int i;
191
192 /*
193 * we need to iterate over all the keys as there may be
194 * multiple instances of the leaf symbol.
195 */
196 for (i = 0; i < MAX_CHAR; i++) {
197 if (current->mapping[i] < 0)
198 continue; /* no mapping for the key, next! */
199
200 key = current->key[current->mapping[i]];
201
202 if (key->type == KEYMAP_MULTI) {
203 /* have not found the leaf, recurse down */
204 delete_key_sequence(key->value.next, key_type);
205 /* if we deleted the last key in the map, free */
206 if (key->value.next->count == 0)
207 _cursesi_free_keymap(key->value.next);
208 } else if ((key->type == KEYMAP_LEAF)
209 && (key->value.symbol == key_type)) {
210 /*
211 * delete the mapping by negating the current
212 * index - this "holds" the position in the
213 * allocation just in case we later re-add
214 * the key for that mapping.
215 */
216 current->mapping[i] = - current->mapping[i];
217 current->count--;
218 }
219 }
220 }
221
222 /*
223 * Add the sequence of characters given in sequence as the key mapping
224 * for the given key symbol.
225 */
226 void
227 add_key_sequence(SCREEN *screen, char *sequence, int key_type)
228 {
229 key_entry_t *tmp_key;
230 keymap_t *current;
231 int length, j, key_ent;
232
233 #ifdef DEBUG
234 __CTRACE("[add_key_sequence]add key sequence: %s(%s)\n",
235 sequence, keyname( key_type ));
236 #endif /* DEBUG */
237 current = screen->base_keymap; /* always start with
238 * base keymap. */
239 length = (int) strlen(sequence);
240
241 for (j = 0; j < length - 1; j++) {
242 /* add the entry to the struct */
243 tmp_key = add_new_key(current, sequence[j], KEYMAP_MULTI, 0);
244
245 /* index into the key array - it's
246 clearer if we stash this */
247 key_ent = current->mapping[(unsigned char) sequence[j]];
248
249 current->key[key_ent] = tmp_key;
250
251 /* next key uses this map... */
252 current = current->key[key_ent]->value.next;
253 }
254
255 /*
256 * This is the last key in the sequence (it may have been the
257 * only one but that does not matter) this means it is a leaf
258 * key and should have a symbol associated with it.
259 */
260 tmp_key = add_new_key(current, sequence[length - 1], KEYMAP_LEAF,
261 key_type);
262 current->key[current->mapping[(int)sequence[length - 1]]] = tmp_key;
263 }
264
265 /*
266 * Init_getch - initialise all the pointers & structures needed to make
267 * getch work in keypad mode.
268 *
269 */
270 void
271 __init_getch(SCREEN *screen)
272 {
273 char entry[1024], *p;
274 int i;
275 size_t limit;
276 #ifdef DEBUG
277 int k, length;
278 #endif
279
280 /* init the inkey state variable */
281 state = INKEY_NORM;
282
283 /* init the base keymap */
284 screen->base_keymap = new_keymap();
285
286 /* key input buffer pointers */
287 start = end = working = 0;
288
289 /* now do the termcap snarfing ... */
290
291 for (i = 0; i < num_tcs; i++) {
292 p = entry;
293 limit = 1023;
294 if (t_getstr(screen->cursesi_genbuf, tc[i].name,
295 &p, &limit) != (char *)NULL) {
296 #ifdef DEBUG
297 __CTRACE("Processing termcap entry %s, sequence ",
298 tc[i].name);
299 length = (int) strlen(entry);
300 for (k = 0; k <= length -1; k++)
301 __CTRACE("%s", unctrl(entry[k]));
302 __CTRACE("\n");
303 #endif
304 add_key_sequence(screen, entry, tc[i].symbol);
305 }
306
307 }
308 }
309
310
311 /*
312 * new_keymap - allocates & initialises a new keymap structure. This
313 * function returns a pointer to the new keymap.
314 *
315 */
316 static keymap_t *
317 new_keymap(void)
318 {
319 int i;
320 keymap_t *new_map;
321
322 if ((new_map = malloc(sizeof(keymap_t))) == NULL) {
323 perror("Inkey: Cannot allocate new keymap");
324 exit(2);
325 }
326
327 /* Initialise the new map */
328 new_map->count = 0;
329 for (i = 0; i < MAX_CHAR; i++) {
330 new_map->mapping[i] = MAPPING_UNUSED; /* no mapping for char */
331 }
332
333 /* key array will be allocated when first key is added */
334 new_map->key = NULL;
335
336 return new_map;
337 }
338
339 /*
340 * new_key - allocates & initialises a new key entry. This function returns
341 * a pointer to the newly allocated key entry.
342 *
343 */
344 static key_entry_t *
345 new_key(void)
346 {
347 key_entry_t *new_one;
348 int i;
349
350 if ((new_one = malloc(KEYMAP_ALLOC_CHUNK * sizeof(key_entry_t)))
351 == NULL) {
352 perror("inkey: Cannot allocate new key entry chunk");
353 exit(2);
354 }
355
356 for (i = 0; i < KEYMAP_ALLOC_CHUNK; i++) {
357 new_one[i].type = 0;
358 new_one[i].value.next = NULL;
359 }
360
361 return new_one;
362 }
363
364 /*
365 * inkey - do the work to process keyboard input, check for multi-key
366 * sequences and return the appropriate symbol if we get a match.
367 *
368 */
369
370 wchar_t
371 inkey(int to, int delay)
372 {
373 wchar_t k;
374 int c, mapping;
375 keymap_t *current = _cursesi_screen->base_keymap;
376 FILE *infd = _cursesi_screen->infd;
377
378 k = 0; /* XXX gcc -Wuninitialized */
379
380 #ifdef DEBUG
381 __CTRACE("inkey (%d, %d)\n", to, delay);
382 #endif
383 for (;;) { /* loop until we get a complete key sequence */
384 reread:
385 if (state == INKEY_NORM) {
386 if (delay && __timeout(delay) == ERR)
387 return ERR;
388 c = getchar();
389 if (_cursesi_screen->resized) {
390 if (c != -1)
391 ungetch(c);
392 _cursesi_screen->resized = 0;
393 clearerr(infd);
394 return KEY_RESIZE;
395 }
396 if (c == EOF) {
397 clearerr(infd);
398 return ERR;
399 }
400
401 if (delay && (__notimeout() == ERR))
402 return ERR;
403
404 k = (wchar_t) c;
405 #ifdef DEBUG
406 __CTRACE("inkey (state normal) got '%s'\n", unctrl(k));
407 #endif
408
409 working = start;
410 inbuf[working] = k;
411 INC_POINTER(working);
412 end = working;
413 state = INKEY_ASSEMBLING; /* go to the assembling
414 * state now */
415 } else if (state == INKEY_BACKOUT) {
416 k = inbuf[working];
417 INC_POINTER(working);
418 if (working == end) { /* see if we have run
419 * out of keys in the
420 * backlog */
421
422 /* if we have then switch to assembling */
423 state = INKEY_ASSEMBLING;
424 }
425 } else if (state == INKEY_ASSEMBLING) {
426 /* assembling a key sequence */
427 if (delay) {
428 if (__timeout(to ? (ESCDELAY / 100) : delay)
429 == ERR)
430 return ERR;
431 } else {
432 if (to && (__timeout(ESCDELAY / 100) == ERR))
433 return ERR;
434 }
435
436 c = getchar();
437 if (_cursesi_screen->resized) {
438 if (c != -1)
439 ungetch(c);
440 _cursesi_screen->resized = 0;
441 clearerr(infd);
442 return KEY_RESIZE;
443 }
444 if (c == -1 || ferror(infd)) {
445 clearerr(infd);
446 return ERR;
447 }
448
449 if ((to || delay) && (__notimeout() == ERR))
450 return ERR;
451
452 k = (wchar_t) c;
453 #ifdef DEBUG
454 __CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
455 #endif
456 if (feof(infd)) { /* inter-char timeout,
457 * start backing out */
458 clearerr(infd);
459 if (start == end)
460 /* no chars in the buffer, restart */
461 goto reread;
462
463 k = inbuf[start];
464 state = INKEY_TIMEOUT;
465 } else {
466 inbuf[working] = k;
467 INC_POINTER(working);
468 end = working;
469 }
470 } else {
471 fprintf(stderr, "Inkey state screwed - exiting!!!");
472 exit(2);
473 }
474
475 /*
476 * Check key has no special meaning and we have not
477 * timed out and the key has not been disabled
478 */
479 mapping = current->mapping[k];
480 if (((state == INKEY_TIMEOUT) || (mapping < 0))
481 || ((current->key[mapping]->type == KEYMAP_LEAF)
482 && (current->key[mapping]->enable == FALSE))) {
483 /* return the first key we know about */
484 k = inbuf[start];
485
486 INC_POINTER(start);
487 working = start;
488
489 if (start == end) { /* only one char processed */
490 state = INKEY_NORM;
491 } else {/* otherwise we must have more than one char
492 * to backout */
493 state = INKEY_BACKOUT;
494 }
495 return k;
496 } else { /* must be part of a multikey sequence */
497 /* check for completed key sequence */
498 if (current->key[current->mapping[k]]->type == KEYMAP_LEAF) {
499 start = working; /* eat the key sequence
500 * in inbuf */
501
502 /* check if inbuf empty now */
503 if (start == end) {
504 /* if it is go back to normal */
505 state = INKEY_NORM;
506 } else {
507 /* otherwise go to backout state */
508 state = INKEY_BACKOUT;
509 }
510
511 /* return the symbol */
512 return current->key[current->mapping[k]]->value.symbol;
513
514 } else {
515 /*
516 * Step on to next part of the multi-key
517 * sequence.
518 */
519 current = current->key[current->mapping[k]]->value.next;
520 }
521 }
522 }
523 }
524
525 #ifndef _CURSES_USE_MACROS
526 /*
527 * getch --
528 * Read in a character from stdscr.
529 */
530 int
531 getch(void)
532 {
533 return wgetch(stdscr);
534 }
535
536 /*
537 * mvgetch --
538 * Read in a character from stdscr at the given location.
539 */
540 int
541 mvgetch(int y, int x)
542 {
543 return mvwgetch(stdscr, y, x);
544 }
545
546 /*
547 * mvwgetch --
548 * Read in a character from stdscr at the given location in the
549 * given window.
550 */
551 int
552 mvwgetch(WINDOW *win, int y, int x)
553 {
554 if (wmove(win, y, x) == ERR)
555 return ERR;
556
557 return wgetch(win);
558 }
559
560 #endif
561
562 /*
563 * keyok --
564 * Set the enable flag for a keysym, if the flag is false then
565 * getch will not return this keysym even if the matching key sequence
566 * is seen.
567 */
568 int
569 keyok(int key_type, bool flag)
570 {
571 int result = ERR;
572
573 do_keyok(_cursesi_screen->base_keymap, key_type, flag, &result);
574 return result;
575 }
576
577 /*
578 * do_keyok --
579 * Does the actual work for keyok, we need to recurse through the
580 * keymaps finding the passed key symbol.
581 */
582 void
583 do_keyok(keymap_t *current, int key_type, bool flag, int *retval)
584 {
585 key_entry_t *key;
586 int i;
587
588 /*
589 * we need to iterate over all the keys as there may be
590 * multiple instances of the leaf symbol.
591 */
592 for (i = 0; i < MAX_CHAR; i++) {
593 if (current->mapping[i] < 0)
594 continue; /* no mapping for the key, next! */
595
596 key = current->key[current->mapping[i]];
597
598 if (key->type == KEYMAP_MULTI)
599 do_keyok(key->value.next, key_type, flag, retval);
600 else if ((key->type == KEYMAP_LEAF)
601 && (key->value.symbol == key_type)) {
602 key->enable = flag;
603 *retval = OK; /* we found at least one instance, ok */
604 }
605 }
606 }
607
608 /*
609 * define_key --
610 * Add a custom mapping of a key sequence to key symbol.
611 *
612 */
613 int
614 define_key(char *sequence, int symbol)
615 {
616
617 if (symbol <= 0)
618 return ERR;
619
620 if (sequence == NULL)
621 delete_key_sequence(_cursesi_screen->base_keymap, symbol);
622 else
623 add_key_sequence(_cursesi_screen, sequence, symbol);
624
625 return OK;
626 }
627
628 /*
629 * wgetch --
630 * Read in a character from the window.
631 */
632 int
633 wgetch(WINDOW *win)
634 {
635 int inp, weset;
636 int c;
637 FILE *infd = _cursesi_screen->infd;
638
639 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
640 && win->curx == win->maxx - 1 && win->cury == win->maxy - 1
641 && __echoit)
642 return (ERR);
643
644 if (is_wintouched(win))
645 wrefresh(win);
646 #ifdef DEBUG
647 __CTRACE("wgetch: __echoit = %d, __rawmode = %d, __nl = %d, flags = %#.4x\n",
648 __echoit, __rawmode, _cursesi_screen->nl, win->flags);
649 #endif
650 if (__echoit && !__rawmode) {
651 cbreak();
652 weset = 1;
653 } else
654 weset = 0;
655
656 __save_termios();
657
658 if (win->flags & __KEYPAD) {
659 switch (win->delay)
660 {
661 case -1:
662 inp = inkey (win->flags & __NOTIMEOUT ? 0 : 1, 0);
663 break;
664 case 0:
665 if (__nodelay() == ERR) {
666 __restore_termios();
667 return ERR;
668 }
669 inp = inkey(0, 0);
670 break;
671 default:
672 inp = inkey(win->flags & __NOTIMEOUT ? 0 : 1, win->delay);
673 break;
674 }
675 } else {
676 switch (win->delay)
677 {
678 case -1:
679 if (__delay() == ERR) {
680 __restore_termios();
681 return ERR;
682 }
683 break;
684 case 0:
685 if (__nodelay() == ERR) {
686 __restore_termios();
687 return ERR;
688 }
689 break;
690 default:
691 if (__timeout(win->delay) == ERR) {
692 __restore_termios();
693 return ERR;
694 }
695 break;
696 }
697
698 c = getchar();
699 if (_cursesi_screen->resized) {
700 if (c != -1)
701 ungetch(c);
702 _cursesi_screen->resized = 0;
703 clearerr(infd);
704 __restore_termios();
705 return KEY_RESIZE;
706 }
707 if (feof(infd)) {
708 clearerr(infd);
709 __restore_termios();
710 return ERR; /* we have timed out */
711 }
712
713 if (ferror(infd)) {
714 clearerr(infd);
715 inp = ERR;
716 } else {
717 inp = c;
718 }
719 }
720 #ifdef DEBUG
721 if (inp > 255)
722 /* we have a key symbol - treat it differently */
723 /* XXXX perhaps __unctrl should be expanded to include
724 * XXXX the keysyms in the table....
725 */
726 __CTRACE("wgetch assembled keysym 0x%x\n", inp);
727 else
728 __CTRACE("wgetch got '%s'\n", unctrl(inp));
729 #endif
730 if (win->delay > -1) {
731 if (__delay() == ERR) {
732 __restore_termios();
733 return ERR;
734 }
735 }
736
737 __restore_termios();
738
739 if (__echoit)
740 waddch(win, (chtype) inp);
741
742 if (weset)
743 nocbreak();
744
745 if (_cursesi_screen->nl && inp == 13)
746 inp = 10;
747
748 return ((inp < 0) || (inp == ERR) ? ERR : inp);
749 }
750
751 /*
752 * ungetch --
753 * Put the character back into the input queue.
754 */
755 int
756 ungetch(int c)
757 {
758 return ((ungetc(c, _cursesi_screen->infd) == EOF) ? ERR : OK);
759 }
760