xref: /illumos-gate/usr/src/lib/libcurses/screen/wgetch.c (revision 86d949f9497332fe19be6b5d711d265eb957439f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include	<sys/types.h>
43 #include	"curses_inc.h"
44 #ifdef	DEBUG
45 #include	<ctype.h>
46 #endif	/* DEBUG */
47 
48 /*
49  * This routine reads in a character from the window.
50  *
51  * wgetch MUST return an int, not a char, because it can return
52  * things like ERR, meta characters, and function keys > 256.
53  */
54 
55 int
56 wgetch(WINDOW *win)
57 {
58 	int	inp;
59 	bool		weset = FALSE;
60 
61 #ifdef	DEBUG
62 	if (outf) {
63 		fprintf(outf, "WGETCH: SP->fl_echoit = %c\n",
64 		    SP->fl_echoit ? 'T' : 'F');
65 		fprintf(outf, "_use_keypad %d, kp_state %d\n",
66 		    win->_use_keypad, SP->kp_state);
67 		fprintf(outf, "file %x fd %d\n", SP->input_file,
68 		    fileno(SP->input_file));
69 	}
70 #endif	/* DEBUG */
71 
72 	if (SP->fl_echoit && cur_term->_fl_rawmode == 0) {
73 		(void) cbreak();
74 		weset++;
75 	}
76 
77 	/* Make sure we are in proper nodelay state and not */
78 	/* in halfdelay state */
79 	if (cur_term->_delay <= 0 && cur_term->_delay != win->_delay)
80 		(void) ttimeout(win->_delay);
81 
82 	if ((win->_flags & (_WINCHANGED | _WINMOVED)) && !(win->_flags &
83 	    _ISPAD))
84 		(void) wrefresh(win);
85 
86 	if ((cur_term->_ungotten == 0) && (req_for_input)) {
87 		(void) tputs(req_for_input, 1, _outch);
88 		(void) fflush(SP->term_file);
89 	}
90 	inp = (int)tgetch((int)(win->_use_keypad ? 1 + win->_notimeout : 0));
91 
92 	/* echo the key out to the screen */
93 	if (SP->fl_echoit && (inp < 0200) && (inp >= 0) && !(win->_flags &
94 	    _ISPAD))
95 		(void) wechochar(win, (chtype) inp);
96 
97 	/*
98 	 * Do nl() mapping. nl() affects both input and output. Since
99 	 * we turn off input mapping of CR->NL to not affect input
100 	 * virtualization, we do the mapping here in software.
101 	 */
102 	if (inp == '\r' && !SP->fl_nonl)
103 		inp = '\n';
104 
105 	if (weset)
106 		(void) nocbreak();
107 
108 	return (inp);
109 }
110