xref: /titanic_41/usr/src/lib/libcurses/screen/ttimeout.c (revision 5fbb41393be5d63f75952b1d72d4df2642d22557)
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 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 #include	<fcntl.h>
47 
48 /*
49  * Set the current time-out period for getting input.
50  *
51  * delay:	< 0 for infinite delay (blocking read)
52  * 		= 0 for no delay read
53  * 		> 0 to specify a period in millisecs to wait
54  *		for input, then return '\0' if none comes
55  */
56 
57 static void	_setblock(int), _settimeout(int);
58 
59 int
60 ttimeout(int delay)
61 {
62 	if (cur_term->_inputfd < 0)
63 		return (ERR);
64 
65 	if (delay < 0)
66 		delay = -1;
67 
68 	if (cur_term->_delay == delay)
69 		goto good;
70 
71 #ifdef	SYSV
72 	if (delay > 0) {
73 		if (cur_term->_delay < 0)
74 			_setblock(-delay);
75 		_settimeout(delay);
76 	} else
77 		if ((delay + cur_term->_delay) == -1)
78 			_setblock(delay);
79 		else {
80 			if (delay < 0)
81 				_setblock(delay);
82 			_settimeout(delay);
83 		}
84 #else	/* SYSV */
85 	cbreak();
86 #endif	/* SYSV */
87 
88 	cur_term->_delay = delay;
89 good:
90 	return (OK);
91 }
92 
93 #ifdef	SYSV
94 /* set the terminal to nodelay (==0) or block(<0) */
95 static	void
96 _setblock(int block)
97 {
98 	int	flags = fcntl(cur_term->_inputfd, F_GETFL, 0);
99 
100 	if (block < 0)
101 		flags &= ~O_NDELAY;
102 	else
103 		flags |= O_NDELAY;
104 
105 	(void) fcntl(cur_term->_inputfd, F_SETFL, flags);
106 }
107 
108 /*
109  * set the terminal to timeout in t milliseconds,
110  * rounded up to the nearest 10th of a second.
111  */
112 static	void
113 _settimeout(int num)
114 {
115 	PROGTTYS.c_lflag &= ~ICANON;
116 	if (num > 0) {
117 		PROGTTYS.c_cc[VMIN] = 0;
118 		PROGTTYS.c_cc[VTIME] = (num > 25500) ? 255 : (num + 99) / 100;
119 		cur_term->_fl_rawmode = 3;
120 	} else {
121 		PROGTTYS.c_cc[VMIN] = 1;
122 		PROGTTYS.c_cc[VTIME] = 0;
123 		cur_term->_fl_rawmode = 1;
124 	}
125 	(void) reset_prog_mode();
126 }
127 #endif	/* SYSV */
128