xref: /illumos-gate/usr/src/lib/libcurses/screen/chkinput.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 /*
43  * chkinput()
44  *
45  * Routine to check to see if any input is waiting.
46  * It returns a 1 if there is input waiting, and a zero if
47  * no input is waiting.
48  *
49  * This function replaces system calls to ioctl() with the FIONREAD
50  * parameter. It enables curses to stop a screen refresh whenever
51  * a character is input.
52  * Standard BTL UNIX 4.0 or 5.0 does not handle FIONREAD.
53  * Changes have been made to 'wrefresh.c' to
54  * call this routine as "_inputpending = chkinput()".
55  * (delay.c and getch.c also use FIONREAD for nodelay, select and fast peek,
56  * but these routines have not been changed).
57  *
58  * Philip N. Crusius - July 20, 1983
59  * Modified to handle various systems March 9, 1984 by Mark Horton.
60  */
61 
62 #include	<unistd.h>
63 #include	<sys/types.h>
64 #include	"curses_inc.h"
65 
66 #ifdef	FIONREAD
67 #define	HAVE_CHK
68 
69 int
70 _chkinput(void)
71 {
72 	int	i;
73 
74 	ioctl(SP->check_fd, FIONREAD, &i);
75 	return (i > 0);
76 }
77 #endif	/* FIONREAD */
78 
79 #ifdef	SELECT
80 #ifndef	HAVE_CHK
81 #define	HAVE_CHK
82 
83 int
84 _chkinput(void)
85 {
86 	int ifds, ofds, efds, n;
87 	struct timeval tv;
88 
89 	ifds = 1 << SP->check_fd;
90 	ofds = efds = 0;
91 	tv.tv_sec = tv.t_usec = 0;
92 	n = select(20, &ifds, &ofds, &efds, &tv);
93 	return (n > 0);
94 }
95 #endif	/* HAVE_CHK */
96 #endif	/* SELECT */
97 
98 #ifndef	HAVE_CHK
99 #ifdef	SYSV
100 
101 int
102 _chkinput(void)
103 {
104 	unsigned	char	c;	/* character input */
105 
106 	/*
107 	 * Only check typeahead if the user is using our input
108 	 * routines. This is because the read below will put
109 	 * stuff into the inputQ that will never be read and the
110 	 * screen will never get updated from now on.
111 	 * This code should GO AWAY when a poll() or FIONREAD can
112 	 * be done on the file descriptor as then the check
113 	 * will be non-destructive.
114 	 */
115 
116 	if (!cur_term->fl_typeahdok ||
117 	    (cur_term->_chars_on_queue == INP_QSIZE) ||
118 	    (cur_term->_check_fd < 0)) {
119 		goto bad;
120 	}
121 
122 	/* If input is waiting in curses queue, return (TRUE). */
123 
124 	if ((int)cur_term->_chars_on_queue > 0) {
125 #ifdef	DEBUG
126 		if (outf) {
127 			(void) fprintf(outf, "Found a character on the input "
128 			    "queue\n");
129 			_print_queue();
130 		}
131 #endif	/* DEBUG */
132 		goto good;
133 	}
134 
135 	if (read(cur_term->_check_fd, (char *)&c, 1) > 0) {
136 #ifdef	DEBUG
137 		if (outf) {
138 			(void) fprintf(outf, "Reading ahead\n");
139 		}
140 #endif	/* DEBUG */
141 		/*
142 		 * A character was waiting.  Put it at the end
143 		 * of the curses queue and return 1 to show that
144 		 * input is waiting.
145 		 */
146 #ifdef	DEBUG
147 		if (outf)
148 			_print_queue();
149 #endif	/* DEBUG */
150 		cur_term->_input_queue[cur_term->_chars_on_queue++] = c;
151 good:
152 		return (TRUE);
153 	} else {
154 		/* No input was waiting so return 0. */
155 #ifdef	DEBUG
156 		if (outf)
157 			(void) fprintf(outf, "No input waiting\n");
158 #endif	/* DEBUG */
159 bad:
160 		return (FALSE);
161 	}
162 }
163 #else	/* SYSV */
164 int
165 _chkinput()
166 {
167 	return (FALSE);
168 }
169 #endif	/* SYSV */
170 #endif	/* HAVE_CHK */
171 
172 #ifdef	DEBUG
173 void
174 _print_queue()	/* FOR DEBUG ONLY */
175 {
176 	int		i, j = cur_term->_chars_on_queue;
177 	chtype		*inputQ = cur_term->_input_queue;
178 
179 	if (outf)
180 		for (i = 0; i < j; i++)
181 			(void) fprintf(outf, "inputQ[%d] = %c\n", i, inputQ[i]);
182 }
183 #endif	/* DEBUG */
184