1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * chkinput()
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * Routine to check to see if any input is waiting.
46*7c478bd9Sstevel@tonic-gate  * It returns a 1 if there is input waiting, and a zero if
47*7c478bd9Sstevel@tonic-gate  * no input is waiting.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * This function replaces system calls to ioctl() with the FIONREAD
50*7c478bd9Sstevel@tonic-gate  * parameter. It enables curses to stop a screen refresh whenever
51*7c478bd9Sstevel@tonic-gate  * a character is input.
52*7c478bd9Sstevel@tonic-gate  * Standard BTL UNIX 4.0 or 5.0 does not handle FIONREAD.
53*7c478bd9Sstevel@tonic-gate  * Changes have been made to 'wrefresh.c' to
54*7c478bd9Sstevel@tonic-gate  * call this routine as "_inputpending = chkinput()".
55*7c478bd9Sstevel@tonic-gate  * (delay.c and getch.c also use FIONREAD for nodelay, select and fast peek,
56*7c478bd9Sstevel@tonic-gate  * but these routines have not been changed).
57*7c478bd9Sstevel@tonic-gate  *
58*7c478bd9Sstevel@tonic-gate  * Philip N. Crusius - July 20, 1983
59*7c478bd9Sstevel@tonic-gate  * Modified to handle various systems March 9, 1984 by Mark Horton.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
63*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
64*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #ifdef	FIONREAD
67*7c478bd9Sstevel@tonic-gate #define	HAVE_CHK
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate int
_chkinput(void)70*7c478bd9Sstevel@tonic-gate _chkinput(void)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	int	i;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	ioctl(SP->check_fd, FIONREAD, &i);
75*7c478bd9Sstevel@tonic-gate 	return (i > 0);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate #endif	/* FIONREAD */
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #ifdef	SELECT
80*7c478bd9Sstevel@tonic-gate #ifndef	HAVE_CHK
81*7c478bd9Sstevel@tonic-gate #define	HAVE_CHK
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate int
_chkinput(void)84*7c478bd9Sstevel@tonic-gate _chkinput(void)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	int ifds, ofds, efds, n;
87*7c478bd9Sstevel@tonic-gate 	struct timeval tv;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	ifds = 1 << SP->check_fd;
90*7c478bd9Sstevel@tonic-gate 	ofds = efds = 0;
91*7c478bd9Sstevel@tonic-gate 	tv.tv_sec = tv.t_usec = 0;
92*7c478bd9Sstevel@tonic-gate 	n = select(20, &ifds, &ofds, &efds, &tv);
93*7c478bd9Sstevel@tonic-gate 	return (n > 0);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate #endif	/* HAVE_CHK */
96*7c478bd9Sstevel@tonic-gate #endif	/* SELECT */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate #ifndef	HAVE_CHK
99*7c478bd9Sstevel@tonic-gate #ifdef	SYSV
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate int
_chkinput(void)102*7c478bd9Sstevel@tonic-gate _chkinput(void)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	unsigned	char	c;	/* character input */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	/*
107*7c478bd9Sstevel@tonic-gate 	 * Only check typeahead if the user is using our input
108*7c478bd9Sstevel@tonic-gate 	 * routines. This is because the read below will put
109*7c478bd9Sstevel@tonic-gate 	 * stuff into the inputQ that will never be read and the
110*7c478bd9Sstevel@tonic-gate 	 * screen will never get updated from now on.
111*7c478bd9Sstevel@tonic-gate 	 * This code should GO AWAY when a poll() or FIONREAD can
112*7c478bd9Sstevel@tonic-gate 	 * be done on the file descriptor as then the check
113*7c478bd9Sstevel@tonic-gate 	 * will be non-destructive.
114*7c478bd9Sstevel@tonic-gate 	 */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (!cur_term->fl_typeahdok ||
117*7c478bd9Sstevel@tonic-gate 	    (cur_term->_chars_on_queue == INP_QSIZE) ||
118*7c478bd9Sstevel@tonic-gate 	    (cur_term->_check_fd < 0)) {
119*7c478bd9Sstevel@tonic-gate 		goto bad;
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/* If input is waiting in curses queue, return (TRUE). */
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if ((int)cur_term->_chars_on_queue > 0) {
125*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
126*7c478bd9Sstevel@tonic-gate 		if (outf) {
127*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "Found a character on the input "
128*7c478bd9Sstevel@tonic-gate 			    "queue\n");
129*7c478bd9Sstevel@tonic-gate 			_print_queue();
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
132*7c478bd9Sstevel@tonic-gate 		goto good;
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (read(cur_term->_check_fd, (char *)&c, 1) > 0) {
136*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
137*7c478bd9Sstevel@tonic-gate 		if (outf) {
138*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "Reading ahead\n");
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
141*7c478bd9Sstevel@tonic-gate 		/*
142*7c478bd9Sstevel@tonic-gate 		 * A character was waiting.  Put it at the end
143*7c478bd9Sstevel@tonic-gate 		 * of the curses queue and return 1 to show that
144*7c478bd9Sstevel@tonic-gate 		 * input is waiting.
145*7c478bd9Sstevel@tonic-gate 		 */
146*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
147*7c478bd9Sstevel@tonic-gate 		if (outf)
148*7c478bd9Sstevel@tonic-gate 			_print_queue();
149*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
150*7c478bd9Sstevel@tonic-gate 		cur_term->_input_queue[cur_term->_chars_on_queue++] = c;
151*7c478bd9Sstevel@tonic-gate good:
152*7c478bd9Sstevel@tonic-gate 		return (TRUE);
153*7c478bd9Sstevel@tonic-gate 	} else {
154*7c478bd9Sstevel@tonic-gate 		/* No input was waiting so return 0. */
155*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
156*7c478bd9Sstevel@tonic-gate 		if (outf)
157*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "No input waiting\n");
158*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
159*7c478bd9Sstevel@tonic-gate bad:
160*7c478bd9Sstevel@tonic-gate 		return (FALSE);
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
164*7c478bd9Sstevel@tonic-gate int
_chkinput()165*7c478bd9Sstevel@tonic-gate _chkinput()
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	return (FALSE);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
170*7c478bd9Sstevel@tonic-gate #endif	/* HAVE_CHK */
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
173*7c478bd9Sstevel@tonic-gate void
_print_queue()174*7c478bd9Sstevel@tonic-gate _print_queue()	/* FOR DEBUG ONLY */
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	int		i, j = cur_term->_chars_on_queue;
177*7c478bd9Sstevel@tonic-gate 	chtype		*inputQ = cur_term->_input_queue;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (outf)
180*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < j; i++)
181*7c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "inputQ[%d] = %c\n", i, inputQ[i]);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
184