get_wch.c revision 1.25 1 /* $NetBSD: get_wch.c,v 1.25 2021/09/06 07:03:49 rin 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.25 2021/09/06 07:03:49 rin Exp $");
40 #endif /* not lint */
41
42 #include <errno.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include "curses.h"
48 #include "curses_private.h"
49 #include "keymap.h"
50
51 static short wstate; /* state of the wcinkey function */
52 extern short _cursesi_state; /* storage declared in getch.c */
53
54 /* prototypes for private functions */
55 static int inkey(wchar_t *wc, int to, int delay);
56 static wint_t __fgetwc_resize(FILE *infd, bool *resized);
57
58 /*
59 * __init_get_wch - initialise all the pointers & structures needed to make
60 * get_wch work in keypad mode.
61 *
62 */
63 void
64 __init_get_wch(SCREEN *screen)
65 {
66 wstate = INKEY_NORM;
67 memset(&screen->cbuf, 0, sizeof(screen->cbuf));
68 screen->cbuf_head = screen->cbuf_tail = screen->cbuf_cur = 0;
69 }
70
71
72 /*
73 * inkey - do the work to process keyboard input, check for multi-key
74 * sequences and return the appropriate symbol if we get a match.
75 *
76 */
77 static int
78 inkey(wchar_t *wc, int to, int delay)
79 {
80 wchar_t k = 0;
81 int c, mapping, ret = 0;
82 size_t mlen = 0;
83 keymap_t *current = _cursesi_screen->base_keymap;
84 FILE *infd = _cursesi_screen->infd;
85 int *start = &_cursesi_screen->cbuf_head,
86 *working = &_cursesi_screen->cbuf_cur,
87 *end = &_cursesi_screen->cbuf_tail;
88 char *inbuf = &_cursesi_screen->cbuf[ 0 ];
89
90 __CTRACE(__CTRACE_INPUT, "inkey (%p, %d, %d)\n", wc, to, delay);
91 for (;;) { /* loop until we get a complete key sequence */
92 if (wstate == INKEY_NORM) {
93 if (delay && __timeout(delay) == ERR)
94 return ERR;
95 c = __fgetc_resize(infd);
96 if (c == ERR || c == KEY_RESIZE) {
97 clearerr(infd);
98 return c;
99 }
100
101 if (delay && (__notimeout() == ERR))
102 return ERR;
103
104 k = (wchar_t)c;
105 __CTRACE(__CTRACE_INPUT,
106 "inkey (wstate normal) got '%s'\n", unctrl(k));
107
108 inbuf[*end] = k;
109 *end = (*end + 1) % MAX_CBUF_SIZE;
110 *working = *start;
111 wstate = INKEY_ASSEMBLING; /* go to assembling state */
112 __CTRACE(__CTRACE_INPUT,
113 "inkey: NORM=>ASSEMBLING: start(%d), "
114 "current(%d), end(%d)\n", *start, *working, *end);
115 } else if (wstate == INKEY_BACKOUT) {
116 k = inbuf[*working];
117 *working = (*working + 1) % MAX_CBUF_SIZE;
118 if (*working == *end) { /* see if run out of keys */
119 /* if so, switch to assembling */
120 wstate = INKEY_ASSEMBLING;
121 __CTRACE(__CTRACE_INPUT,
122 "inkey: BACKOUT=>ASSEMBLING, start(%d), "
123 "current(%d), end(%d)\n",
124 *start, *working, *end);
125 }
126 } else if (wstate == INKEY_ASSEMBLING) {
127 /* assembling a key sequence */
128 if (delay) {
129 if (__timeout(to ? (ESCDELAY / 100) : delay)
130 == ERR)
131 return ERR;
132 } else {
133 if (to && (__timeout(ESCDELAY / 100) == ERR))
134 return ERR;
135 }
136
137 c = __fgetc_resize(infd);
138 if (ferror(infd)) {
139 clearerr(infd);
140 return c;
141 }
142
143 if ((to || delay) && (__notimeout() == ERR))
144 return ERR;
145
146 k = (wchar_t)c;
147 __CTRACE(__CTRACE_INPUT,
148 "inkey (wstate assembling) got '%s'\n", unctrl(k));
149 if (feof(infd)) { /* inter-char T/O, start backout */
150 clearerr(infd);
151 if (*start == *end)
152 /* no chars in the buffer, restart */
153 continue;
154
155 k = inbuf[*start];
156 wstate = INKEY_TIMEOUT;
157 __CTRACE(__CTRACE_INPUT,
158 "inkey: ASSEMBLING=>TIMEOUT, start(%d), "
159 "current(%d), end(%d)\n",
160 *start, *working, *end);
161 } else {
162 inbuf[*end] = k;
163 *working = *end;
164 *end = (*end + 1) % MAX_CBUF_SIZE;
165 __CTRACE(__CTRACE_INPUT,
166 "inkey: ASSEMBLING: start(%d), "
167 "current(%d), end(%d)",
168 *start, *working, *end);
169 }
170 } else if (wstate == INKEY_WCASSEMBLING) {
171 /* assembling a wide-char sequence */
172 if (delay) {
173 if (__timeout(to ? (ESCDELAY / 100) : delay)
174 == ERR)
175 return ERR;
176 } else {
177 if (to && (__timeout(ESCDELAY / 100) == ERR))
178 return ERR;
179 }
180
181 c = __fgetc_resize(infd);
182 if (ferror(infd)) {
183 clearerr(infd);
184 return c;
185 }
186
187 if ((to || delay) && (__notimeout() == ERR))
188 return ERR;
189
190 k = (wchar_t)c;
191 __CTRACE(__CTRACE_INPUT,
192 "inkey (wstate wcassembling) got '%s'\n",
193 unctrl(k));
194 if (feof(infd)) { /* inter-char T/O, start backout */
195 clearerr(infd);
196 if (*start == *end)
197 /* no chars in the buffer, restart */
198 continue;
199
200 *wc = inbuf[*start];
201 *working = *start = (*start +1) % MAX_CBUF_SIZE;
202 if (*start == *end) {
203 _cursesi_state = wstate = INKEY_NORM;
204 __CTRACE(__CTRACE_INPUT,
205 "inkey: WCASSEMBLING=>NORM, "
206 "start(%d), current(%d), end(%d)",
207 *start, *working, *end);
208 } else {
209 _cursesi_state = wstate = INKEY_BACKOUT;
210 __CTRACE(__CTRACE_INPUT,
211 "inkey: WCASSEMBLING=>BACKOUT, "
212 "start(%d), current(%d), end(%d)",
213 *start, *working, *end);
214 }
215 return OK;
216 } else {
217 /* assembling wide characters */
218 inbuf[*end] = k;
219 *working = *end;
220 *end = (*end + 1) % MAX_CBUF_SIZE;
221 __CTRACE(__CTRACE_INPUT,
222 "inkey: WCASSEMBLING[head(%d), "
223 "urrent(%d), tail(%d)]\n",
224 *start, *working, *end);
225 ret = (int)mbrtowc(wc, inbuf + (*working), 1,
226 &_cursesi_screen->sp);
227 __CTRACE(__CTRACE_INPUT,
228 "inkey: mbrtowc returns %d, wc(%x)\n",
229 ret, *wc);
230 if (ret == -2) {
231 *working = (*working+1) % MAX_CBUF_SIZE;
232 continue;
233 }
234 if ( ret == 0 )
235 ret = 1;
236 if ( ret == -1 ) {
237 /* return the 1st character we know */
238 *wc = inbuf[*start];
239 *working = *start = (*start + 1) % MAX_CBUF_SIZE;
240 __CTRACE(__CTRACE_INPUT,
241 "inkey: Invalid wide char(%x) "
242 "[head(%d), current(%d), "
243 "tail(%d)]\n",
244 *wc, *start, *working, *end);
245 } else { /* > 0 */
246 /* return the wide character */
247 *start = *working
248 = (*working + ret)%MAX_CBUF_SIZE;
249 __CTRACE(__CTRACE_INPUT,
250 "inkey: Wide char found(%x) "
251 "[head(%d), current(%d), "
252 "tail(%d)]\n",
253 *wc, *start, *working, *end);
254 }
255
256 if (*start == *end) {
257 /* only one char processed */
258 _cursesi_state = wstate = INKEY_NORM;
259 __CTRACE(__CTRACE_INPUT,
260 "inkey: WCASSEMBLING=>NORM, "
261 "start(%d), current(%d), end(%d)",
262 *start, *working, *end);
263 } else {
264 /* otherwise we must have more than
265 * one char to backout */
266 _cursesi_state = wstate = INKEY_BACKOUT;
267 __CTRACE(__CTRACE_INPUT,
268 "inkey: WCASSEMBLING=>BACKOUT, "
269 "start(%d), current(%d), end(%d)",
270 *start, *working, *end);
271 }
272 return OK;
273 }
274 } else {
275 fprintf(stderr, "Inkey wstate screwed - exiting!!!");
276 exit(2);
277 }
278
279 /*
280 * Check key has no special meaning and we have not
281 * timed out and the key has not been disabled
282 */
283 mapping = current->mapping[k];
284 if (((wstate == INKEY_TIMEOUT) || (mapping < 0))
285 || ((current->key[mapping]->type
286 == KEYMAP_LEAF)
287 && (current->key[mapping]->enable == FALSE)))
288 {
289 /* wide-character specific code */
290 __CTRACE(__CTRACE_INPUT,
291 "inkey: Checking for wide char\n");
292 mbrtowc( NULL, NULL, 1, &_cursesi_screen->sp );
293 *working = *start;
294 mlen = *end > *working ?
295 *end - *working : MAX_CBUF_SIZE - *working;
296 if (!mlen)
297 return ERR;
298 __CTRACE(__CTRACE_INPUT,
299 "inkey: Check wide char[head(%d), "
300 "current(%d), tail(%d), mlen(%ld)]\n",
301 *start, *working, *end, (long) mlen);
302 ret = (int)mbrtowc(wc, inbuf + (*working), mlen,
303 &_cursesi_screen->sp);
304 __CTRACE(__CTRACE_INPUT,
305 "inkey: mbrtowc returns %d, wc(%x)\n", ret, *wc);
306 if (ret == -2 && *end < *working) {
307 /* second half of a wide character */
308 *working = 0;
309 mlen = *end;
310 if (mlen)
311 ret = (int)mbrtowc(wc, inbuf, mlen,
312 &_cursesi_screen->sp);
313 }
314 if (ret == -2 && wstate != INKEY_TIMEOUT) {
315 *working = (*working + (int) mlen)
316 % MAX_CBUF_SIZE;
317 wstate = INKEY_WCASSEMBLING;
318 continue;
319 }
320 if (ret == 0)
321 ret = 1;
322 if (ret == -1) {
323 /* return the first key we know about */
324 *wc = inbuf[*start];
325 *working = *start
326 = (*start + 1) % MAX_CBUF_SIZE;
327 __CTRACE(__CTRACE_INPUT,
328 "inkey: Invalid wide char(%x)[head(%d), "
329 "current(%d), tail(%d)]\n",
330 *wc, *start, *working, *end);
331 } else { /* > 0 */
332 /* return the wide character */
333 *start = *working
334 = (*working + ret) % MAX_CBUF_SIZE;
335 __CTRACE(__CTRACE_INPUT,
336 "inkey: Wide char found(%x)[head(%d), "
337 "current(%d), tail(%d)]\n",
338 *wc, *start, *working, *end);
339 }
340
341 if (*start == *end) { /* only one char processed */
342 _cursesi_state = wstate = INKEY_NORM;
343 __CTRACE(__CTRACE_INPUT,
344 "inkey: Empty cbuf=>NORM, "
345 "start(%d), current(%d), end(%d)\n",
346 *start, *working, *end);
347 } else {
348 /* otherwise we must have more than one
349 * char to backout */
350 _cursesi_state = wstate = INKEY_BACKOUT;
351 __CTRACE(__CTRACE_INPUT,
352 "inkey: Non-empty cbuf=>BACKOUT, "
353 "start(%d), current(%d), end(%d)\n",
354 *start, *working, *end);
355 }
356 return OK;
357 } else { /* must be part of a multikey sequence */
358 /* check for completed key sequence */
359 if (current->key[current->mapping[k]]->type
360 == KEYMAP_LEAF) {
361 /* eat the key sequence in cbuf */
362 *start = *working = ( *working + 1 )
363 % MAX_CBUF_SIZE;
364
365 /* check if inbuf empty now */
366 __CTRACE(__CTRACE_INPUT,
367 "inkey: Key found(%s)\n",
368 key_name(current->key[mapping]->value.symbol));
369 if (*start == *end) {
370 /* if it is go back to normal */
371 _cursesi_state = wstate = INKEY_NORM;
372 __CTRACE(__CTRACE_INPUT,
373 "[inkey]=>NORM, start(%d), "
374 "current(%d), end(%d)",
375 *start, *working, *end);
376 } else {
377 /* otherwise go to backout state */
378 _cursesi_state = wstate = INKEY_BACKOUT;
379 __CTRACE(__CTRACE_INPUT,
380 "[inkey]=>BACKOUT, start(%d), "
381 "current(%d), end(%d)",
382 *start, *working, *end );
383 }
384
385 /* return the symbol */
386 *wc = current->key[mapping]->value.symbol;
387 return KEY_CODE_YES;
388 } else {
389 /* Step to next part of multi-key sequence */
390 current = current->key[current->mapping[k]]->value.next;
391 }
392 }
393 }
394 }
395
396 /*
397 * get_wch --
398 * Read in a wide character from stdscr.
399 */
400 int
401 get_wch(wint_t *ch)
402 {
403 return wget_wch(stdscr, ch);
404 }
405
406 /*
407 * mvget_wch --
408 * Read in a character from stdscr at the given location.
409 */
410 int
411 mvget_wch(int y, int x, wint_t *ch)
412 {
413 return mvwget_wch(stdscr, y, x, ch);
414 }
415
416 /*
417 * mvwget_wch --
418 * Read in a character from stdscr at the given location in the
419 * given window.
420 */
421 int
422 mvwget_wch(WINDOW *win, int y, int x, wint_t *ch)
423 {
424 if (wmove(win, y, x) == ERR)
425 return ERR;
426
427 return wget_wch(win, ch);
428 }
429
430 /*
431 * wget_wch --
432 * Read in a wide character from the window.
433 */
434 int
435 wget_wch(WINDOW *win, wint_t *ch)
436 {
437 int ret, weset;
438 int c;
439 FILE *infd = _cursesi_screen->infd;
440 cchar_t wc;
441 wchar_t inp, ws[2];
442
443 if (!(win->flags & __SCROLLOK)
444 && (win->flags & __FULLWIN)
445 && win->curx == win->maxx - 1
446 && win->cury == win->maxy - 1
447 && __echoit)
448 return ERR;
449
450 if (!(win->flags & __ISPAD) && is_wintouched(win))
451 wrefresh(win);
452 __CTRACE(__CTRACE_INPUT, "wget_wch: __echoit = %d, "
453 "__rawmode = %d, __nl = %d, flags = %#.4x\n",
454 __echoit, __rawmode, _cursesi_screen->nl, win->flags);
455 if (_cursesi_screen->resized) {
456 resizeterm(LINES, COLS);
457 _cursesi_screen->resized = 0;
458 *ch = KEY_RESIZE;
459 return KEY_CODE_YES;
460 }
461 if (_cursesi_screen->unget_pos) {
462 __CTRACE(__CTRACE_INPUT, "wget_wch returning char at %d\n",
463 _cursesi_screen->unget_pos);
464 _cursesi_screen->unget_pos--;
465 *ch = _cursesi_screen->unget_list[_cursesi_screen->unget_pos];
466 if (__echoit) {
467 ws[0] = *ch, ws[1] = L'\0';
468 setcchar(&wc, ws, win->wattr, 0, NULL);
469 wadd_wch(win, &wc);
470 }
471 return KEY_CODE_YES;
472 }
473 if (__echoit && !__rawmode) {
474 cbreak();
475 weset = 1;
476 } else
477 weset = 0;
478
479 __save_termios();
480
481 if (win->flags & __KEYPAD) {
482 switch (win->delay) {
483 case -1:
484 ret = inkey(&inp,
485 win->flags & __NOTIMEOUT ? 0 : 1, 0);
486 break;
487 case 0:
488 if (__nodelay() == ERR)
489 return ERR;
490 ret = inkey(&inp, 0, 0);
491 break;
492 default:
493 ret = inkey(&inp,
494 win->flags & __NOTIMEOUT ? 0 : 1,
495 win->delay);
496 break;
497 }
498 if ( ret == ERR )
499 return ERR;
500 } else {
501 bool resized;
502
503 switch (win->delay) {
504 case -1:
505 break;
506 case 0:
507 if (__nodelay() == ERR)
508 return ERR;
509 break;
510 default:
511 if (__timeout(win->delay) == ERR)
512 return ERR;
513 break;
514 }
515
516 c = __fgetwc_resize(infd, &resized);
517 if (c == WEOF) {
518 clearerr(infd);
519 __restore_termios();
520 if (resized) {
521 *ch = KEY_RESIZE;
522 return KEY_CODE_YES;
523 } else
524 return ERR;
525 } else {
526 ret = c;
527 inp = c;
528 }
529 }
530 #ifdef DEBUG
531 if (inp > 255)
532 /* we have a key symbol - treat it differently */
533 /* XXXX perhaps __unctrl should be expanded to include
534 * XXXX the keysyms in the table....
535 */
536 __CTRACE(__CTRACE_INPUT, "wget_wch assembled keysym 0x%x\n",
537 inp);
538 else
539 __CTRACE(__CTRACE_INPUT, "wget_wch got '%s'\n", unctrl(inp));
540 #endif
541 if (win->delay > -1) {
542 if (__delay() == ERR)
543 return ERR;
544 }
545
546 __restore_termios();
547
548 if (__echoit) {
549 if ( ret == KEY_CODE_YES ) {
550 /* handle [DEL], [BS], and [LEFT] */
551 if ( win->curx &&
552 ( inp == KEY_DC ||
553 inp == KEY_BACKSPACE ||
554 inp == KEY_LEFT )) {
555 wmove( win, win->cury, win->curx - 1);
556 wdelch( win );
557 }
558 } else {
559 ws[ 0 ] = inp, ws[ 1 ] = L'\0';
560 setcchar( &wc, ws, win->wattr, 0, NULL );
561 wadd_wch( win, &wc );
562 }
563 }
564
565 if (weset)
566 nocbreak();
567
568 if (_cursesi_screen->nl && inp == 13)
569 inp = 10;
570
571 *ch = inp;
572
573 if ( ret == KEY_CODE_YES )
574 return KEY_CODE_YES;
575 return inp < 0 ? ERR : OK;
576 }
577
578 /*
579 * unget_wch --
580 * Put the wide character back into the input queue.
581 */
582 int
583 unget_wch(const wchar_t c)
584 {
585 return __unget((wint_t)c);
586 }
587
588 /*
589 * __fgetwc_resize --
590 * Any call to fgetwc(3) should use this function instead.
591 */
592 static wint_t
593 __fgetwc_resize(FILE *infd, bool *resized)
594 {
595 wint_t c;
596
597 c = fgetwc(infd);
598 if (c != WEOF)
599 return c;
600
601 if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized)
602 return ERR;
603 __CTRACE(__CTRACE_INPUT, "__fgetwc_resize returning KEY_RESIZE\n");
604 resizeterm(LINES, COLS);
605 _cursesi_screen->resized = 0;
606 *resized = true;
607 return c;
608 }
609