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