summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcurses/screen/chkinput.c
blob: 152daf6c818f4d5d419759fe43ae9171d82dc314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*LINTLIBRARY*/

/*
 * chkinput()
 *
 * Routine to check to see if any input is waiting.
 * It returns a 1 if there is input waiting, and a zero if
 * no input is waiting.
 *
 * This function replaces system calls to ioctl() with the FIONREAD
 * parameter. It enables curses to stop a screen refresh whenever
 * a character is input.
 * Standard BTL UNIX 4.0 or 5.0 does not handle FIONREAD.
 * Changes have been made to 'wrefresh.c' to
 * call this routine as "_inputpending = chkinput()".
 * (delay.c and getch.c also use FIONREAD for nodelay, select and fast peek,
 * but these routines have not been changed).
 *
 * Philip N. Crusius - July 20, 1983
 * Modified to handle various systems March 9, 1984 by Mark Horton.
 */

#include	<unistd.h>
#include	<sys/types.h>
#include	"curses_inc.h"

#ifdef	FIONREAD
#define	HAVE_CHK

int
_chkinput(void)
{
	int	i;

	ioctl(SP->check_fd, FIONREAD, &i);
	return (i > 0);
}
#endif	/* FIONREAD */

#ifdef	SELECT
#ifndef	HAVE_CHK
#define	HAVE_CHK

int
_chkinput(void)
{
	int ifds, ofds, efds, n;
	struct timeval tv;

	ifds = 1 << SP->check_fd;
	ofds = efds = 0;
	tv.tv_sec = tv.t_usec = 0;
	n = select(20, &ifds, &ofds, &efds, &tv);
	return (n > 0);
}
#endif	/* HAVE_CHK */
#endif	/* SELECT */

#ifndef	HAVE_CHK
#ifdef	SYSV

int
_chkinput(void)
{
	unsigned	char	c;	/* character input */

	/*
	 * Only check typeahead if the user is using our input
	 * routines. This is because the read below will put
	 * stuff into the inputQ that will never be read and the
	 * screen will never get updated from now on.
	 * This code should GO AWAY when a poll() or FIONREAD can
	 * be done on the file descriptor as then the check
	 * will be non-destructive.
	 */

	if (!cur_term->fl_typeahdok ||
	    (cur_term->_chars_on_queue == INP_QSIZE) ||
	    (cur_term->_check_fd < 0)) {
		goto bad;
	}

	/* If input is waiting in curses queue, return (TRUE). */

	if ((int)cur_term->_chars_on_queue > 0) {
#ifdef	DEBUG
		if (outf) {
			(void) fprintf(outf, "Found a character on the input "
			    "queue\n");
			_print_queue();
		}
#endif	/* DEBUG */
		goto good;
	}

	if (read(cur_term->_check_fd, (char *)&c, 1) > 0) {
#ifdef	DEBUG
		if (outf) {
			(void) fprintf(outf, "Reading ahead\n");
		}
#endif	/* DEBUG */
		/*
		 * A character was waiting.  Put it at the end
		 * of the curses queue and return 1 to show that
		 * input is waiting.
		 */
#ifdef	DEBUG
		if (outf)
			_print_queue();
#endif	/* DEBUG */
		cur_term->_input_queue[cur_term->_chars_on_queue++] = c;
good:
		return (TRUE);
	} else {
		/* No input was waiting so return 0. */
#ifdef	DEBUG
		if (outf)
			(void) fprintf(outf, "No input waiting\n");
#endif	/* DEBUG */
bad:
		return (FALSE);
	}
}
#else	/* SYSV */
int
_chkinput()
{
	return (FALSE);
}
#endif	/* SYSV */
#endif	/* HAVE_CHK */

#ifdef	DEBUG
void
_print_queue()	/* FOR DEBUG ONLY */
{
	int		i, j = cur_term->_chars_on_queue;
	chtype		*inputQ = cur_term->_input_queue;

	if (outf)
		for (i = 0; i < j; i++)
			(void) fprintf(outf, "inputQ[%d] = %c\n", i, inputQ[i]);
}
#endif	/* DEBUG */