xref: /illumos-gate/usr/src/lib/libcurses/screen/wgetch.c (revision eb9a1df2aeb866bf1de4494433b6d7e5fa07b3ae)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 #ifdef	DEBUG
47 #include	<ctype.h>
48 #endif	/* DEBUG */
49 
50 /*
51  * This routine reads in a character from the window.
52  *
53  * wgetch MUST return an int, not a char, because it can return
54  * things like ERR, meta characters, and function keys > 256.
55  */
56 
57 int
58 wgetch(WINDOW *win)
59 {
60 	int	inp;
61 	bool		weset = FALSE;
62 
63 #ifdef	DEBUG
64 	if (outf) {
65 		fprintf(outf, "WGETCH: SP->fl_echoit = %c\n",
66 		    SP->fl_echoit ? 'T' : 'F');
67 		fprintf(outf, "_use_keypad %d, kp_state %d\n",
68 		    win->_use_keypad, SP->kp_state);
69 		fprintf(outf, "file %x fd %d\n", SP->input_file,
70 		    fileno(SP->input_file));
71 	}
72 #endif	/* DEBUG */
73 
74 	if (SP->fl_echoit && cur_term->_fl_rawmode == 0) {
75 		(void) cbreak();
76 		weset++;
77 	}
78 
79 	/* Make sure we are in proper nodelay state and not */
80 	/* in halfdelay state */
81 	if (cur_term->_delay <= 0 && cur_term->_delay != win->_delay)
82 		(void) ttimeout(win->_delay);
83 
84 	if ((win->_flags & (_WINCHANGED | _WINMOVED)) && !(win->_flags &
85 	    _ISPAD))
86 		(void) wrefresh(win);
87 
88 	if ((cur_term->_ungotten == 0) && (req_for_input)) {
89 		(void) tputs(req_for_input, 1, _outch);
90 		(void) fflush(SP->term_file);
91 	}
92 	inp = (int)tgetch((int)(win->_use_keypad ? 1 + win->_notimeout : 0));
93 
94 	/* echo the key out to the screen */
95 	if (SP->fl_echoit && (inp < 0200) && (inp >= 0) && !(win->_flags &
96 	    _ISPAD))
97 		(void) wechochar(win, (chtype) inp);
98 
99 	/*
100 	 * Do nl() mapping. nl() affects both input and output. Since
101 	 * we turn off input mapping of CR->NL to not affect input
102 	 * virtualization, we do the mapping here in software.
103 	 */
104 	if (inp == '\r' && !SP->fl_nonl)
105 		inp = '\n';
106 
107 	if (weset)
108 		(void) nocbreak();
109 
110 	return (inp);
111 }
112