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