get_wch.c revision 1.1.2.3 1 /* $NetBSD: get_wch.c,v 1.1.2.3 2007/01/21 18:21:13 jdc Exp $ */
2
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved.
6 *
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
9 *
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: get_wch.c,v 1.1.2.3 2007/01/21 18:21:13 jdc Exp $");
40 #endif /* not lint */
41
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include "curses.h"
47 #include "curses_private.h"
48 #include "keymap.h"
49
50 static short wstate; /* state of the wcinkey function */
51
52 /* prototypes for private functions */
53 static int inkey(wchar_t *wc, int to, int delay);
54
55 /*
56 * __init_get_wch - initialise all the pointers & structures needed to make
57 * get_wch work in keypad mode.
58 *
59 */
60 void
61 __init_get_wch(SCREEN *screen)
62 {
63 #ifndef HAVE_WCHAR
64 return;
65 #else
66 /* narrow character initialization */
67 __init_getch( screen );
68
69 /* wide character specific stuff */
70 wstate = INKEY_NORM;
71 memset( &screen->cbuf, 0, MAX_CBUF_SIZE * sizeof( int ));
72 screen->cbuf_head = screen->cbuf_tail = screen->cbuf_cur = 0;
73 #endif /* HAVE_WCHAR */
74 }
75
76
77 /*
78 * inkey - do the work to process keyboard input, check for multi-key
79 * sequences and return the appropriate symbol if we get a match.
80 *
81 */
82
83 int
84 inkey(wchar_t *wc, int to, int delay)
85 {
86 #ifndef HAVE_WCHAR
87 return ERR;
88 #else
89 wchar_t k = 0;
90 int c, mapping, ret = 0;
91 size_t mlen = 0;
92 keymap_t *current = _cursesi_screen->base_keymap;
93 FILE *infd = _cursesi_screen->infd;
94 int *start = &_cursesi_screen->cbuf_head,
95 *working = &_cursesi_screen->cbuf_cur,
96 *end = &_cursesi_screen->cbuf_tail;
97 char *inbuf = &_cursesi_screen->cbuf[ 0 ];
98
99 /* int ESCDELAY = 300;*/ /* Delay in ms between keys for esc seq's */
100 ESCDELAY = 300;
101
102 for (;;) { /* loop until we get a complete key sequence */
103 if (wstate == INKEY_NORM) {
104 if (delay && __timeout(delay) == ERR)
105 return ERR;
106 c = getchar();
107 if (_cursesi_screen->resized) {
108 if (c != WEOF) /* was -1 */
109 unget_wch(c);
110 _cursesi_screen->resized = 0;
111 clearerr(infd);
112 *wc = KEY_RESIZE;
113 return KEY_CODE_YES;
114 }
115 if (c == WEOF) {
116 clearerr(infd);
117 return ERR;
118 }
119
120 if (delay && (__notimeout() == ERR))
121 return ERR;
122
123 k = (wchar_t) c;
124 #ifdef DEBUG
125 __CTRACE(__CTRACE_INPUT,
126 "inkey (wstate normal) got '%s'\n", unctrl(k));
127 #endif
128
129 inbuf[ *end ] = k;
130 *end = ( *end + 1 ) % MAX_CBUF_SIZE;
131 *working = *start;
132 wstate = INKEY_ASSEMBLING; /* go to assembling state */
133 #ifdef DEBUG
134 __CTRACE(__CTRACE_INPUT,
135 "inkey: NORM=>ASSEMBLING: start(%d), "
136 "current(%d), end(%d)\n", *start, *working, *end);
137 #endif /* DEBUG */
138 } else if (wstate == INKEY_BACKOUT) {
139 k = inbuf[*working];
140 *working = ( *working + 1 ) % MAX_CBUF_SIZE;
141 if (*working == *end) { /* see if run out of keys */
142 /* if so, switch to assembling */
143 wstate = INKEY_ASSEMBLING;
144 #ifdef DEBUG
145 __CTRACE(__CTRACE_INPUT,
146 "inkey: BACKOUT=>ASSEMBLING, start(%d), "
147 "current(%d), end(%d)\n",
148 *start, *working, *end);
149 #endif /* DEBUG */
150 }
151 } else if (wstate == INKEY_ASSEMBLING) {
152 /* assembling a key sequence */
153 if (delay) {
154 if (__timeout(to ? (ESCDELAY / 100) : delay)
155 == ERR)
156 return ERR;
157 } else {
158 if (to && (__timeout(ESCDELAY / 100) == ERR))
159 return ERR;
160 }
161
162 c = getchar();
163 if (_cursesi_screen->resized) {
164 if (c != -1)
165 unget_wch(c);
166 _cursesi_screen->resized = 0;
167 clearerr(infd);
168 *wc = KEY_RESIZE;
169 return KEY_CODE_YES;
170 }
171 if (ferror(infd)) {
172 clearerr(infd);
173 return ERR;
174 }
175
176 if ((to || delay) && (__notimeout() == ERR))
177 return ERR;
178
179 k = (wchar_t) c;
180 #ifdef DEBUG
181 __CTRACE(__CTRACE_INPUT,
182 "inkey (wstate assembling) got '%s'\n", unctrl(k));
183 #endif /* DEBUG */
184 if (feof(infd)) { /* inter-char T/O, start backout */
185 clearerr(infd);
186 if (*start == *end)
187 /* no chars in the buffer, restart */
188 continue;
189
190 k = inbuf[*start];
191 wstate = INKEY_TIMEOUT;
192 #ifdef DEBUG
193 __CTRACE(__CTRACE_INPUT,
194 "inkey: ASSEMBLING=>TIMEOUT, start(%d), "
195 "current(%d), end(%d)\n",
196 *start, *working, *end);
197 #endif /* DEBUG */
198 } else {
199 inbuf[ *end ] = k;
200 *working = *end;
201 *end = ( *end + 1 ) % MAX_CBUF_SIZE;
202 #ifdef DEBUG
203 __CTRACE(__CTRACE_INPUT,
204 "inkey: ASSEMBLING: start(%d), "
205 "current(%d), end(%d)",
206 *start, *working, *end);
207 #endif /* DEBUG */
208 }
209 } else if (wstate == INKEY_WCASSEMBLING) {
210 /* assembling a wide char sequence */
211 if (delay) {
212 if (__timeout(to ? (ESCDELAY / 100) : delay)
213 == ERR)
214 return ERR;
215 } else {
216 if (to && (__timeout(ESCDELAY / 100) == ERR))
217 return ERR;
218 }
219
220 c = getchar();
221 if (_cursesi_screen->resized) {
222 if (c != -1)
223 unget_wch(c);
224 _cursesi_screen->resized = 0;
225 clearerr(infd);
226 *wc = KEY_RESIZE;
227 return KEY_CODE_YES;
228 }
229 if (ferror(infd)) {
230 clearerr(infd);
231 return ERR;
232 }
233
234 if ((to || delay) && (__notimeout() == ERR))
235 return ERR;
236
237 k = (wchar_t) c;
238 #ifdef DEBUG
239 __CTRACE(__CTRACE_INPUT,
240 "inkey (wstate wcassembling) got '%s'\n",
241 unctrl(k));
242 #endif
243 if (feof(infd)) { /* inter-char T/O, start backout */
244 clearerr(infd);
245 if (*start == *end)
246 /* no chars in the buffer, restart */
247 continue;
248
249 *wc = inbuf[*start];
250 *working = *start
251 = ( *start + 1 ) % MAX_CBUF_SIZE;
252 if (*start == *end) {
253 state = wstate = INKEY_NORM;
254 #ifdef DEBUG
255 __CTRACE(__CTRACE_INPUT,
256 "inkey: WCASSEMBLING=>NORM, "
257 "start(%d), current(%d), end(%d)",
258 *start, *working, *end);
259 #endif /* DEBUG */
260 } else {
261 state = wstate = INKEY_BACKOUT;
262 #ifdef DEBUG
263 __CTRACE(__CTRACE_INPUT,
264 "inkey: WCASSEMBLING=>BACKOUT, "
265 "start(%d), current(%d), end(%d)",
266 *start, *working, *end);
267 #endif /* DEBUG */
268 }
269 return OK;
270 } else {
271 /* assembling wide characters */
272 inbuf[ *end ] = k;
273 *working = *end;
274 *end = ( *end + 1 ) % MAX_CBUF_SIZE;
275 #ifdef DEBUG
276 __CTRACE(__CTRACE_INPUT,
277 "inkey: WCASSEMBLING[head(%d), "
278 "urrent(%d), tail(%d)]\n",
279 *start, *working, *end);
280 #endif /* DEBUG */
281 ret = mbrtowc( wc, inbuf + (*working), 1,
282 &_cursesi_screen->sp );
283 #ifdef DEBUG
284 __CTRACE(__CTRACE_INPUT,
285 "inkey: mbrtowc returns %d, wc(%x)\n",
286 ret, *wc );
287 #endif /* DEBUG */
288 if ( ret == -2 ) {
289 *working = (*working + 1)
290 % MAX_CBUF_SIZE;
291 continue;
292 }
293 if ( ret == 0 )
294 ret = 1;
295 if ( ret == -1 ) {
296 /* return the 1st character we know */
297 *wc = inbuf[ *start ];
298 *working = *start = ( *start + 1 ) % MAX_CBUF_SIZE;
299 #ifdef DEBUG
300 __CTRACE(__CTRACE_INPUT,
301 "inkey: Invalid wide char(%x) "
302 "[head(%d), current(%d), "
303 "tail(%d)]\n",
304 *wc, *start, *working, *end);
305 #endif /* DEBUG */
306 } else { /* > 0 */
307 /* return the wide character */
308 *start = *working
309 = (*working + ret)%MAX_CBUF_SIZE;
310 #ifdef DEBUG
311 __CTRACE(__CTRACE_INPUT,
312 "inkey: Wide char found(%x) "
313 "[head(%d), current(%d), "
314 "tail(%d)]\n",
315 *wc, *start, *working, *end);
316 #endif /* DEBUG */
317 }
318
319 if (*start == *end) { /* only one char processed */
320 state = wstate = INKEY_NORM;
321 #ifdef DEBUG
322 __CTRACE(__CTRACE_INPUT,
323 "inkey: WCASSEMBLING=>NORM, "
324 "start(%d), current(%d), end(%d)",
325 *start, *working, *end);
326 #endif /* DEBUG */
327 } else {
328 /* otherwise we must have more than one char to backout */
329 state = wstate = INKEY_BACKOUT;
330 #ifdef DEBUG
331 __CTRACE(__CTRACE_INPUT,
332 "inkey: WCASSEMBLING=>BACKOUT, "
333 "start(%d), current(%d), end(%d)",
334 *start, *working, *end);
335 #endif /* DEBUG */
336 }
337 return OK;
338 }
339 } else {
340 fprintf(stderr, "Inkey wstate screwed - exiting!!!");
341 exit(2);
342 }
343
344 /*
345 * Check key has no special meaning and we have not
346 * timed out and the key has not been disabled
347 */
348 mapping = current->mapping[k];
349 if (((wstate == INKEY_TIMEOUT) || (mapping < 0))
350 || ((current->key[mapping]->type
351 == KEYMAP_LEAF)
352 && (current->key[mapping]->enable == FALSE))) {
353 /* wide character specific code */
354 #ifdef DEBUG
355 __CTRACE(__CTRACE_INPUT,
356 "inkey: Checking for wide char\n");
357 #endif /* DEBUG */
358 mbrtowc( NULL, NULL, 1, &_cursesi_screen->sp );
359 *working = *start;
360 mlen = *end > *working ?
361 *end - *working : MAX_CBUF_SIZE - *working;
362 if ( !mlen )
363 return ERR;
364 #ifdef DEBUG
365 __CTRACE(__CTRACE_INPUT,
366 "inkey: Check wide char[head(%d), "
367 "current(%d), tail(%d), mlen(%ld)]\n",
368 *start, *working, *end, (long) mlen);
369 #endif /* DEBUG */
370 ret = mbrtowc( wc, inbuf + (*working), mlen,
371 &_cursesi_screen->sp );
372 #ifdef DEBUG
373 __CTRACE(__CTRACE_INPUT,
374 "inkey: mbrtowc returns %d, wc(%x)\n", ret, *wc);
375 #endif /* DEBUG */
376 if ( ret == -2 && *end < *working ) {
377 /* second half of a wide character */
378 *working = 0;
379 mlen = *end;
380 if ( mlen )
381 ret = mbrtowc( wc, inbuf, mlen,
382 &_cursesi_screen->sp );
383 }
384 if ( ret == -2 && wstate != INKEY_TIMEOUT ) {
385 *working = (*working + mlen) % MAX_CBUF_SIZE;
386 wstate = INKEY_WCASSEMBLING;
387 continue;
388 }
389 if ( ret == 0 )
390 ret = 1;
391 if ( ret == -1 ) {
392 /* return the first key we know about */
393 *wc = inbuf[ *start ];
394 *working = *start
395 = ( *start + 1 ) % MAX_CBUF_SIZE;
396 #ifdef DEBUG
397 __CTRACE(__CTRACE_INPUT,
398 "inkey: Invalid wide char(%x)[head(%d), "
399 "current(%d), tail(%d)]\n",
400 *wc, *start, *working, *end);
401 #endif /* DEBUG */
402 } else { /* > 0 */
403 /* return the wide character */
404 *start = *working
405 = ( *working + ret ) % MAX_CBUF_SIZE;
406 #ifdef DEBUG
407 __CTRACE(__CTRACE_INPUT,
408 "inkey: Wide char found(%x)[head(%d), "
409 "current(%d), tail(%d)]\n",
410 *wc, *start, *working, *end);
411 #endif /* DEBUG */
412 }
413
414 if (*start == *end) { /* only one char processed */
415 state = wstate = INKEY_NORM;
416 #ifdef DEBUG
417 __CTRACE(__CTRACE_INPUT,
418 "inkey: Empty cbuf=>NORM, "
419 "start(%d), current(%d), end(%d)\n",
420 *start, *working, *end);
421 #endif /* DEBUG */
422 } else {
423 /* otherwise we must have more than one char to backout */
424 state = wstate = INKEY_BACKOUT;
425 #ifdef DEBUG
426 __CTRACE(__CTRACE_INPUT,
427 "inkey: Non-empty cbuf=>BACKOUT, "
428 "start(%d), current(%d), end(%d)\n",
429 *start, *working, *end);
430 #endif /* DEBUG */
431 }
432 return OK;
433 } else { /* must be part of a multikey sequence */
434 /* check for completed key sequence */
435 if (current->key[current->mapping[k]]->type
436 == KEYMAP_LEAF) {
437 /* eat the key sequence in cbuf */
438 *start = *working = ( *working + 1 ) % MAX_CBUF_SIZE;
439
440 /* check if inbuf empty now */
441 #ifdef DEBUG
442 __CTRACE(__CTRACE_INPUT,
443 "inkey: Key found(%s)\n",
444 key_name(current->key[mapping]->value.symbol));
445 #endif /* DEBUG */
446 if (*start == *end) {
447 /* if it is go back to normal */
448 state = wstate = INKEY_NORM;
449 #ifdef DEBUG
450 __CTRACE(__CTRACE_INPUT,
451 "[inkey]=>NORM, start(%d), "
452 "current(%d), end(%d)",
453 *start, *working, *end);
454 #endif /* DEBUG */
455 } else {
456 /* otherwise go to backout state */
457 state = wstate = INKEY_BACKOUT;
458 #ifdef DEBUG
459 __CTRACE(__CTRACE_INPUT,
460 "[inkey]=>BACKOUT, start(%d), "
461 "current(%d), end(%d)",
462 *start, *working, *end );
463 #endif /* DEBUG */
464 }
465
466 /* return the symbol */
467 *wc = current->key[mapping]->value.symbol;
468 return KEY_CODE_YES;
469 } else {
470 /* Step to next part of multi-key sequence */
471 current = current->key[current->mapping[k]]->value.next;
472 }
473 }
474 }
475 #endif /* HAVE_WCHAR */
476 }
477
478 /*
479 * get_wch --
480 * Read in a wide character from stdscr.
481 */
482 int
483 get_wch(wint_t *ch)
484 {
485 #ifndef HAVE_WCHAR
486 return ERR;
487 #else
488 return wget_wch(stdscr, ch);
489 #endif /* HAVE_WCHAR */
490 }
491
492 /*
493 * mvget_wch --
494 * Read in a character from stdscr at the given location.
495 */
496 int
497 mvget_wch(int y, int x, wint_t *ch)
498 {
499 #ifndef HAVE_WCHAR
500 return ERR;
501 #else
502 return mvwget_wch(stdscr, y, x, ch);
503 #endif /* HAVE_WCHAR */
504 }
505
506 /*
507 * mvwget_wch --
508 * Read in a character from stdscr at the given location in the
509 * given window.
510 */
511 int
512 mvwget_wch(WINDOW *win, int y, int x, wint_t *ch)
513 {
514 #ifndef HAVE_WCHAR
515 return ERR;
516 #else
517 if (wmove(win, y, x) == ERR)
518 return ERR;
519
520 return wget_wch(win, ch);
521 #endif /* HAVE_WCHAR */
522 }
523
524 /*
525 * wget_wch --
526 * Read in a wide character from the window.
527 */
528 int
529 wget_wch(WINDOW *win, wint_t *ch)
530 {
531 #ifndef HAVE_WCHAR
532 return ERR;
533 #else
534 int ret, weset;
535 int c;
536 FILE *infd = _cursesi_screen->infd;
537 cchar_t wc;
538 wchar_t inp, ws[ 2 ];
539
540 if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
541 && win->curx == win->maxx - 1
542 && win->cury == win->maxy - 1
543 && __echoit)
544 return (ERR);
545
546 if (is_wintouched(win))
547 wrefresh(win);
548 #ifdef DEBUG
549 __CTRACE(__CTRACE_INPUT, "wget_wch: __echoit = %d, "
550 "__rawmode = %d, __nl = %d, flags = %#.4x\n",
551 __echoit, __rawmode, _cursesi_screen->nl, win->flags);
552 #endif
553 if (__echoit && !__rawmode) {
554 cbreak();
555 weset = 1;
556 } else
557 weset = 0;
558
559 __save_termios();
560
561 if (win->flags & __KEYPAD) {
562 switch (win->delay) {
563 case -1:
564 ret = inkey(&inp,
565 win->flags & __NOTIMEOUT ? 0 : 1, 0);
566 break;
567 case 0:
568 if (__nodelay() == ERR) {
569 __restore_termios();
570 return ERR;
571 }
572 ret = inkey(&inp, 0, 0);
573 break;
574 default:
575 ret = inkey(&inp,
576 win->flags & __NOTIMEOUT ? 0 : 1,
577 win->delay);
578 break;
579 }
580 if ( ret == ERR )
581 return ERR;
582 } else {
583 switch (win->delay) {
584 case -1:
585 break;
586 case 0:
587 if (__nodelay() == ERR) {
588 __restore_termios();
589 return ERR;
590 }
591 break;
592 default:
593 if (__timeout(win->delay) == ERR) {
594 __restore_termios();
595 return ERR;
596 }
597 break;
598 }
599
600 c = getwchar();
601 if (_cursesi_screen->resized) {
602 if (c != -1)
603 unget_wch(c);
604 _cursesi_screen->resized = 0;
605 clearerr(infd);
606 __restore_termios();
607 *ch = KEY_RESIZE;
608 return KEY_CODE_YES;
609 }
610 if (feof(infd)) {
611 clearerr(infd);
612 __restore_termios();
613 return ERR; /* we have timed out */
614 }
615
616 if (ferror(infd)) {
617 clearerr(infd);
618 return ERR;
619 } else {
620 ret = c;
621 inp = c;
622 }
623 }
624 #ifdef DEBUG
625 if (inp > 255)
626 /* we have a key symbol - treat it differently */
627 /* XXXX perhaps __unctrl should be expanded to include
628 * XXXX the keysyms in the table....
629 */
630 __CTRACE(__CTRACE_INPUT, "wget_wch assembled keysym 0x%x\n",
631 inp);
632 else
633 __CTRACE(__CTRACE_INPUT, "wget_wch got '%s'\n", unctrl(inp));
634 #endif
635 if (win->delay > -1) {
636 if (__delay() == ERR) {
637 __restore_termios();
638 return ERR;
639 }
640 }
641
642 __restore_termios();
643
644 if (__echoit) {
645 if ( ret == KEY_CODE_YES ) {
646 /* handle [DEL], [BS], and [LEFT] */
647 if ( win->curx &&
648 ( inp == KEY_DC ||
649 inp == KEY_BACKSPACE ||
650 inp == KEY_LEFT )) {
651 wmove( win, win->cury, win->curx - 1 );
652 wdelch( win );
653 }
654 } else {
655 ws[ 0 ] = inp, ws[ 1 ] = L'\0';
656 setcchar( &wc, ws, win->wattr, 0, NULL );
657 wadd_wch( win, &wc );
658 }
659 }
660
661 if (weset)
662 nocbreak();
663
664 if (_cursesi_screen->nl && inp == 13)
665 inp = 10;
666
667 *ch = inp;
668
669 if ( ret == KEY_CODE_YES )
670 return KEY_CODE_YES;
671 return ( inp < 0 ? ERR : OK );
672 #endif /* HAVE_WCHAR */
673 }
674
675 /*
676 * unget_wch --
677 * Put the wide character back into the input queue.
678 */
679 int
680 unget_wch(const wchar_t c)
681 {
682 return
683 ( int )((ungetwc((wint_t)c, _cursesi_screen->infd) == WEOF) ?
684 ERR : OK);
685 }
686