xref: /onnv-gate/usr/src/lib/libcurses/screen/tgetch.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 2004 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 #include	"curses_inc.h"
45*0Sstevel@tonic-gate #include	<signal.h>
46*0Sstevel@tonic-gate #include	<unistd.h>
47*0Sstevel@tonic-gate #ifdef	DEBUG
48*0Sstevel@tonic-gate #include	<ctype.h>
49*0Sstevel@tonic-gate #endif	/* DEBUG */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * Read a key typed from the terminal
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * interpret:	= 0 for single-char key only
55*0Sstevel@tonic-gate  * 		= 1 for matching function key and macro patterns.
56*0Sstevel@tonic-gate  * 		= 2 same as 1 but no time-out for funckey matching.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static	int _getkey(int, chtype *);
60*0Sstevel@tonic-gate static	int _fpk(void);
61*0Sstevel@tonic-gate static	int _pk(void);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate chtype
64*0Sstevel@tonic-gate tgetch(int interpret)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	int		i = 0, j, collapse = 1;
67*0Sstevel@tonic-gate #define	WAIT3		333
68*0Sstevel@tonic-gate 	chtype		inp;
69*0Sstevel@tonic-gate 	chtype		*inputQ = cur_term->_input_queue;
70*0Sstevel@tonic-gate 	char		*chars_onQ = &(cur_term->_chars_on_queue);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #ifdef	SYSV
73*0Sstevel@tonic-gate 	/*
74*0Sstevel@tonic-gate 	 * Register the fact that getch is being used so
75*0Sstevel@tonic-gate 	 * that typeahead checking can be done.
76*0Sstevel@tonic-gate 	 * This code should GO AWAY when a poll() or FIONREAD can
77*0Sstevel@tonic-gate 	 * be done on the file descriptor as then the check
78*0Sstevel@tonic-gate 	 * will be non-destructive.
79*0Sstevel@tonic-gate 	 */
80*0Sstevel@tonic-gate 	cur_term->fl_typeahdok = TRUE;
81*0Sstevel@tonic-gate #endif	/* SYSV */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/* ask for input */
84*0Sstevel@tonic-gate 	if (cur_term->_ungotten > 0) {
85*0Sstevel@tonic-gate 		cur_term->_ungotten--;
86*0Sstevel@tonic-gate 		/* decode an ungetch()'d character */
87*0Sstevel@tonic-gate 		inp = -inputQ[0];
88*0Sstevel@tonic-gate 	} else {
89*0Sstevel@tonic-gate 		/* Only read a character if there is no typeahead/peekahead. */
90*0Sstevel@tonic-gate 		if (*chars_onQ == 0) {
91*0Sstevel@tonic-gate 			/* (*chars_onQ)++;  MR */
92*0Sstevel@tonic-gate #ifdef	FIONREAD
93*0Sstevel@tonic-gate 			inp = _readchar();
94*0Sstevel@tonic-gate #else	/* FIONREAD */
95*0Sstevel@tonic-gate 			inp = (chtype) _pk();
96*0Sstevel@tonic-gate 			if ((int)inp == ERR) {
97*0Sstevel@tonic-gate 		/*
98*0Sstevel@tonic-gate 		 * interpret is set to 0 so that down below we don't
99*0Sstevel@tonic-gate 		 * drop into getkey since we already know there can't be
100*0Sstevel@tonic-gate 		 * a key that starts with -1.  Also, we don't want to
101*0Sstevel@tonic-gate 		 * access funckeystarter[-1].
102*0Sstevel@tonic-gate 		 */
103*0Sstevel@tonic-gate 				interpret = FALSE;
104*0Sstevel@tonic-gate 			}
105*0Sstevel@tonic-gate #endif	/* FIONREAD */
106*0Sstevel@tonic-gate 			(*chars_onQ)++;
107*0Sstevel@tonic-gate 		} else
108*0Sstevel@tonic-gate 			inp = inputQ[0];
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate #ifdef	DEBUG
111*0Sstevel@tonic-gate 		if (outf)
112*0Sstevel@tonic-gate 			fprintf(outf, "TGETCH read '%s'\n", unctrl(inp));
113*0Sstevel@tonic-gate #endif	/* DEBUG */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		/* Check for arrow and function keys */
116*0Sstevel@tonic-gate 		if (interpret && cur_term->funckeystarter[inp])
117*0Sstevel@tonic-gate 		    collapse = _getkey(interpret - 1, &inp);
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	/* Collapse the input queue to remove the escape */
121*0Sstevel@tonic-gate 	/* sequence from the stack. */
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	j = *chars_onQ;
124*0Sstevel@tonic-gate 	(*chars_onQ) -= collapse;
125*0Sstevel@tonic-gate 	while (collapse < j)
126*0Sstevel@tonic-gate 		inputQ[i++] = inputQ[collapse++];
127*0Sstevel@tonic-gate 	return (inp);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #ifdef	FIONREAD
131*0Sstevel@tonic-gate static	int
132*0Sstevel@tonic-gate _readchar()
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	int		i;
135*0Sstevel@tonic-gate 	unsigned	char	c;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if (cur_term->_delay == 0) {
138*0Sstevel@tonic-gate 		int	arg;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		(void) ioctl(cur_term->_inputfd, FIONREAD, &arg);
141*0Sstevel@tonic-gate #ifdef	DEBUG
142*0Sstevel@tonic-gate 		if (outf)
143*0Sstevel@tonic-gate 			fprintf(outf, "FIONREAD returns %d\n", arg);
144*0Sstevel@tonic-gate #endif	/* DEBUG */
145*0Sstevel@tonic-gate 		if (arg < 1)
146*0Sstevel@tonic-gate 			return (-1);
147*0Sstevel@tonic-gate 	} else
148*0Sstevel@tonic-gate 		if (cur_term->_delay > 0) {
149*0Sstevel@tonic-gate 			char	c;
150*0Sstevel@tonic-gate 			int	infd;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 			infd = 1 << cur_term->_inputfd;
153*0Sstevel@tonic-gate 			t.tv_sec = cur_term->_delay / 1000;
154*0Sstevel@tonic-gate 			t.tv_usec = (cur_term->_delay % 1000) * 1000;
155*0Sstevel@tonic-gate 			i = select(20, &infd, (int *)NULL, (int *)NULL, &t);
156*0Sstevel@tonic-gate 			if (i < 0)
157*0Sstevel@tonic-gate 				return (ERR);
158*0Sstevel@tonic-gate 			i = read(cur_term->_inputfd, &c, 1);
159*0Sstevel@tonic-gate 		} else
160*0Sstevel@tonic-gate 			i = read(cur_term->_inputfd, &c, 1);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate #ifdef	DEBUG
163*0Sstevel@tonic-gate 	if (outf)
164*0Sstevel@tonic-gate 		fprintf(outf, "read from %d returns %d chars, first %o\n",
165*0Sstevel@tonic-gate 		    cur_term->_inputfd, i, c);
166*0Sstevel@tonic-gate #endif	/* DEBUG */
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if (i > 0)
169*0Sstevel@tonic-gate 		return (c);
170*0Sstevel@tonic-gate 	else
171*0Sstevel@tonic-gate 		return (ERR);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate #endif	/* !FIONREAD */
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate #ifdef	DEBUG
176*0Sstevel@tonic-gate extern	char	*_asciify();
177*0Sstevel@tonic-gate #endif	/* DEBUG */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * This algorithm is a "learning" algorithm. The premise is
181*0Sstevel@tonic-gate  * that keys used once are like to be used again and again.
182*0Sstevel@tonic-gate  * Since the time for a linear search of the table is so
183*0Sstevel@tonic-gate  * expensive, we move keys that are found up to the top of
184*0Sstevel@tonic-gate  * the list, making the access to a repeated key very fast and
185*0Sstevel@tonic-gate  * keys that have been used before close to the top.
186*0Sstevel@tonic-gate  */
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate static	int
189*0Sstevel@tonic-gate _getkey(int blockpeek, chtype *inp)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	_KEY_MAP	**kp = cur_term->_keys;
192*0Sstevel@tonic-gate 	int		key, num_keys = cur_term->_ksz;
193*0Sstevel@tonic-gate 	int		i;
194*0Sstevel@tonic-gate 	chtype		*inputQ = cur_term->_input_queue;
195*0Sstevel@tonic-gate 	char		*chars_onQ = &(cur_term->_chars_on_queue),
196*0Sstevel@tonic-gate 			flag = cur_term->funckeystarter[*inp];
197*0Sstevel@tonic-gate 	int		first, collapse = 1;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate #ifdef	DEBUG
201*0Sstevel@tonic-gate 	if (outf)
202*0Sstevel@tonic-gate 		fprintf(outf, "getkey(): looking in linear table, "
203*0Sstevel@tonic-gate 		    "inp=%d\n", *inp);
204*0Sstevel@tonic-gate #endif	/* DEBUG */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (flag & _KEY)
207*0Sstevel@tonic-gate 		key = 0;
208*0Sstevel@tonic-gate 	else {
209*0Sstevel@tonic-gate 		key = cur_term->_first_macro;
210*0Sstevel@tonic-gate 		blockpeek = TRUE;
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	first = key;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	for (; key < num_keys; key++) {
215*0Sstevel@tonic-gate 		if (kp[key]->_sends[0] == *inp) {
216*0Sstevel@tonic-gate 			for (i = 1; i < INP_QSIZE; i++) {
217*0Sstevel@tonic-gate 				/* found it? */
218*0Sstevel@tonic-gate 				if (kp[key]->_sends[i] == '\0')
219*0Sstevel@tonic-gate 					break;
220*0Sstevel@tonic-gate 				/* partial match? peek ahead. */
221*0Sstevel@tonic-gate 				if (*chars_onQ == i) {
222*0Sstevel@tonic-gate 					(*chars_onQ)++;
223*0Sstevel@tonic-gate 					inputQ[i] = (blockpeek) ?
224*0Sstevel@tonic-gate 						_pk() : _fpk();
225*0Sstevel@tonic-gate 					switch ((int)inputQ[i]) {
226*0Sstevel@tonic-gate 					case -2:
227*0Sstevel@tonic-gate 			/*
228*0Sstevel@tonic-gate 			 * Since -2 signifies a timeout we don't really
229*0Sstevel@tonic-gate 			 * want to put it on the queue so we decrement
230*0Sstevel@tonic-gate 			 * our counter.
231*0Sstevel@tonic-gate 			 */
232*0Sstevel@tonic-gate 						(*chars_onQ)--;
233*0Sstevel@tonic-gate #ifdef	DEBUG
234*0Sstevel@tonic-gate 					if (outf)
235*0Sstevel@tonic-gate 						fprintf(outf, "Timed out\n");
236*0Sstevel@tonic-gate #endif	/* DEBUG */
237*0Sstevel@tonic-gate 					if (flag & _MACRO) {
238*0Sstevel@tonic-gate #ifdef	DEBUG
239*0Sstevel@tonic-gate 						if (outf)
240*0Sstevel@tonic-gate 							fprintf(outf,
241*0Sstevel@tonic-gate 							    "Found macro\n");
242*0Sstevel@tonic-gate #endif	/* DEBUG */
243*0Sstevel@tonic-gate 				/*
244*0Sstevel@tonic-gate 				 * We have to decrement one because key will be
245*0Sstevel@tonic-gate 				 * incremented at the bottom of the out loop.
246*0Sstevel@tonic-gate 				 */
247*0Sstevel@tonic-gate 						key = (first = blockpeek =
248*0Sstevel@tonic-gate 						    cur_term->_first_macro) -
249*0Sstevel@tonic-gate 						    1;
250*0Sstevel@tonic-gate 						goto outerloop;
251*0Sstevel@tonic-gate 					}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 					/*FALLTHROUGH*/
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 					case -1:
256*0Sstevel@tonic-gate 						goto ret;
257*0Sstevel@tonic-gate 					}
258*0Sstevel@tonic-gate 				}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 				/* not this one? */
261*0Sstevel@tonic-gate 				if (kp[key]->_sends[i] != inputQ[i])
262*0Sstevel@tonic-gate 					goto outerloop;
263*0Sstevel@tonic-gate 			}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 			/* SS-mouse */
266*0Sstevel@tonic-gate 			if (kp[key]->_keyval == KEY_MOUSE) {
267*0Sstevel@tonic-gate 				MOUSE_STATUS old_mouse;
268*0Sstevel@tonic-gate 				int rc;
269*0Sstevel@tonic-gate 				static int get_xterm_mouse(int, int *);
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 				old_mouse = Mouse_status;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 				/* read the mouse status information	*/
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 				if (mouse_info)
276*0Sstevel@tonic-gate 					rc = -3;	/* NOT IMPLEMENTED */
277*0Sstevel@tonic-gate 				else
278*0Sstevel@tonic-gate 					rc = get_xterm_mouse(blockpeek, &i);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 				if (rc == -1)		/* read error */
281*0Sstevel@tonic-gate 					goto ret;
282*0Sstevel@tonic-gate 				else if (rc == -2 || rc == -3) /* timeout */
283*0Sstevel@tonic-gate 							/* or not mouse */
284*0Sstevel@tonic-gate 					goto outerloop;
285*0Sstevel@tonic-gate 				else if (rc == 0) /* report mouse pos */
286*0Sstevel@tonic-gate 					Mouse_status.changes |= 020;
287*0Sstevel@tonic-gate 				else if (rc >= 1 && rc <= 3)
288*0Sstevel@tonic-gate 					/* mouse button event */
289*0Sstevel@tonic-gate 					Mouse_status.changes =
290*0Sstevel@tonic-gate 					    (((MOUSE_X_POS != old_mouse.x ||
291*0Sstevel@tonic-gate 					    MOUSE_Y_POS != old_mouse.y) << 3) |
292*0Sstevel@tonic-gate 					    ((Mouse_status.button[2] !=
293*0Sstevel@tonic-gate 					    old_mouse.button[2]) << 2) |
294*0Sstevel@tonic-gate 					    ((Mouse_status.button[1] !=
295*0Sstevel@tonic-gate 					    old_mouse.button[1]) << 1) |
296*0Sstevel@tonic-gate 					    (Mouse_status.button[0] !=
297*0Sstevel@tonic-gate 					    old_mouse.button[0]));
298*0Sstevel@tonic-gate 			}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 			/* We found it! Read in any chars left in _sends */
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 			if ((collapse = i) == INP_QSIZE)
303*0Sstevel@tonic-gate 				for (; kp[key]->_sends[i]; i++)
304*0Sstevel@tonic-gate 					(void) _fpk();
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 			/* move key to top of ordered list */
307*0Sstevel@tonic-gate 			if (key != first) {
308*0Sstevel@tonic-gate 				_KEY_MAP	*savekey = kp[key];
309*0Sstevel@tonic-gate 				short		*lorder;
310*0Sstevel@tonic-gate 				int		j;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 				if (key > cur_term->_first_macro)
313*0Sstevel@tonic-gate 				lorder = &(cur_term->_lastmacro_ordered);
314*0Sstevel@tonic-gate 				else
315*0Sstevel@tonic-gate 					lorder = &(cur_term->_lastkey_ordered);
316*0Sstevel@tonic-gate 		/*
317*0Sstevel@tonic-gate 		 * If we're below the last ordered key, swap next unordered
318*0Sstevel@tonic-gate 		 * key with this one and ripple from there.
319*0Sstevel@tonic-gate 		 */
320*0Sstevel@tonic-gate 				if (key > *lorder)
321*0Sstevel@tonic-gate 					kp[key] = kp[(i = ++(*lorder))];
322*0Sstevel@tonic-gate 				else
323*0Sstevel@tonic-gate 					i = key;
324*0Sstevel@tonic-gate 				/* ripple the ordered keys down */
325*0Sstevel@tonic-gate 				for (j = i--; j > first; )
326*0Sstevel@tonic-gate 					kp[j--] = kp[i--];
327*0Sstevel@tonic-gate 				kp[first] = savekey;
328*0Sstevel@tonic-gate 			}
329*0Sstevel@tonic-gate 			*inp = kp[first]->_keyval;
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 			/*
332*0Sstevel@tonic-gate 			 * SS-mouse support: if mouse button event
333*0Sstevel@tonic-gate 			 * occured on top of the soft label, we may
334*0Sstevel@tonic-gate 			 * have to return the function key corresponding
335*0Sstevel@tonic-gate 			 * to that soft label
336*0Sstevel@tonic-gate 			 */
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 			if (*inp == KEY_MOUSE && A_BUTTON_CHANGED &&
339*0Sstevel@tonic-gate 			    (MOUSE_Y_POS == LINES) &&
340*0Sstevel@tonic-gate 			    (SP->slk != (SLK_MAP *) NULL) &&
341*0Sstevel@tonic-gate 			    (SP->_map_mbe_to_key  != 0)) {
342*0Sstevel@tonic-gate 				static void _map_button(chtype *);
343*0Sstevel@tonic-gate 				_map_button(inp);
344*0Sstevel@tonic-gate 			}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 			goto ret;
347*0Sstevel@tonic-gate 		}
348*0Sstevel@tonic-gate outerloop:
349*0Sstevel@tonic-gate 		;
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate ret:
353*0Sstevel@tonic-gate 	/* key not found */
354*0Sstevel@tonic-gate #ifdef	DEBUG
355*0Sstevel@tonic-gate 	if (outf)
356*0Sstevel@tonic-gate 		if (key == num_keys)
357*0Sstevel@tonic-gate 			fprintf(outf, "Did not match anything.\n");
358*0Sstevel@tonic-gate #endif	/* DEBUG */
359*0Sstevel@tonic-gate 	return (collapse);
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate /* SS-mouse */
364*0Sstevel@tonic-gate /* this function tries to read in information that follows KEY_MOUSE: */
365*0Sstevel@tonic-gate /* the first character identifies what button is involved (1,2,or 3)  */
366*0Sstevel@tonic-gate /* if the first character is 0, we are dealing with report_mouse_pos  */
367*0Sstevel@tonic-gate /*
368*0Sstevel@tonic-gate  *	The routine returns the following:
369*0Sstevel@tonic-gate  *		-3:	not a mouse button event
370*0Sstevel@tonic-gate  *		-2:	read timed out
371*0Sstevel@tonic-gate  *		-1:	the read failed
372*0Sstevel@tonic-gate  *		[0, 1, 2, 3] - the first character in the mouse event
373*0Sstevel@tonic-gate  */
374*0Sstevel@tonic-gate static int
375*0Sstevel@tonic-gate get_xterm_mouse(int blockpeek, int *i)
376*0Sstevel@tonic-gate {
377*0Sstevel@tonic-gate 	chtype	*inputQ = cur_term->_input_queue;		/* ??? */
378*0Sstevel@tonic-gate 	/* LINTED */
379*0Sstevel@tonic-gate 	chtype	*chars_onQ = (chtype *) &(cur_term->_chars_on_queue);
380*0Sstevel@tonic-gate 	int	j, mx, my;
381*0Sstevel@tonic-gate 	int	char1, char2, c1, c2;
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	/* the first character should be 0, 1, 2, or 4	*/
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	char1 = (inputQ[(*i)++] = (blockpeek) ? _pk() : _fpk());
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/* read error or timeout	*/
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if (char1 < 0)
390*0Sstevel@tonic-gate 		return (char1);
391*0Sstevel@tonic-gate 	(*chars_onQ)++;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	if (char1 < '0' || char1 > '3')
394*0Sstevel@tonic-gate 		return (-3);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/* if the character is 1, 2, or 3 it must be followed by 	*/
397*0Sstevel@tonic-gate 	/* P, R, C, D, or T						*/
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	if (char1 != '0') {
400*0Sstevel@tonic-gate 		char2 = (inputQ[(*i)++] = (blockpeek) ? _pk() : _fpk());
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		if (char2 < 0)
403*0Sstevel@tonic-gate 			return (char2);
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 		(*chars_onQ)++;
406*0Sstevel@tonic-gate 		if (char2 != 'P' && char2 != 'R' && char2 != 'C' &&
407*0Sstevel@tonic-gate 		    char2 != 'D' && char2 != 'T')
408*0Sstevel@tonic-gate 			return (-3);
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	/* read X  and Y coordinates of the mouse	*/
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	for (j = 0; j < 2; j++) {
414*0Sstevel@tonic-gate 		c1 = (inputQ[(*i)++] = (blockpeek) ? _pk() : _fpk());
415*0Sstevel@tonic-gate 		if (c1 < 0)
416*0Sstevel@tonic-gate 			return (c1);
417*0Sstevel@tonic-gate 		(*chars_onQ)++;
418*0Sstevel@tonic-gate 		if (c1 >= ' ' && c1 <= '~') {	/* ascii char */
419*0Sstevel@tonic-gate 			if (j == 0)
420*0Sstevel@tonic-gate 				mx = c1 - ' ';
421*0Sstevel@tonic-gate 			else
422*0Sstevel@tonic-gate 				my = c1 - ' ';
423*0Sstevel@tonic-gate 		} else if (char1 == 01 || char1 == 02) {   /* ^A || ^B */
424*0Sstevel@tonic-gate 			c2 = (inputQ[(*i)++] = (blockpeek) ? _pk() : _fpk());
425*0Sstevel@tonic-gate 			if (c2 < 0)
426*0Sstevel@tonic-gate 				return (c2);
427*0Sstevel@tonic-gate 			(*chars_onQ)++;
428*0Sstevel@tonic-gate 			if (c2 >= ' ' && c2 <= '~') {
429*0Sstevel@tonic-gate 				if (j == 0)
430*0Sstevel@tonic-gate 					mx = c1 * (c2 - ' ');
431*0Sstevel@tonic-gate 				else
432*0Sstevel@tonic-gate 					my = c1 * (c2 - ' ');
433*0Sstevel@tonic-gate 			} else
434*0Sstevel@tonic-gate 				return (-3);
435*0Sstevel@tonic-gate 		} else
436*0Sstevel@tonic-gate 			return (-3);
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	/* read complete mouse event: update the Mouse_status structure */
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	MOUSE_X_POS = mx;
442*0Sstevel@tonic-gate 	MOUSE_Y_POS = my;
443*0Sstevel@tonic-gate 	j = char1 - '0';
444*0Sstevel@tonic-gate 	if (j != 0) {
445*0Sstevel@tonic-gate 		switch (char2) {
446*0Sstevel@tonic-gate 			case 'P':
447*0Sstevel@tonic-gate 				BUTTON_STATUS(j) = BUTTON_PRESSED;
448*0Sstevel@tonic-gate 				break;
449*0Sstevel@tonic-gate 			case 'R':
450*0Sstevel@tonic-gate 				BUTTON_STATUS(j) = BUTTON_RELEASED;
451*0Sstevel@tonic-gate 				break;
452*0Sstevel@tonic-gate 			case 'C':
453*0Sstevel@tonic-gate 				BUTTON_STATUS(j) = BUTTON_CLICKED;
454*0Sstevel@tonic-gate 				break;
455*0Sstevel@tonic-gate 			case 'D':
456*0Sstevel@tonic-gate 				BUTTON_STATUS(j) = BUTTON_DOUBLE_CLICKED;
457*0Sstevel@tonic-gate 				break;
458*0Sstevel@tonic-gate 			case 'T':
459*0Sstevel@tonic-gate 				BUTTON_STATUS(j) = BUTTON_TRIPLE_CLICKED;
460*0Sstevel@tonic-gate 				break;
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 	return (j);
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate /* SS-mouse-end */
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate /*
469*0Sstevel@tonic-gate  * Fast peek key.  Like getchar but if the right flags are set, times out
470*0Sstevel@tonic-gate  * quickly if there is nothing waiting, returning -1.
471*0Sstevel@tonic-gate  * f is an output stdio descriptor, we read from the fileno.
472*0Sstevel@tonic-gate  * We wait for long enough for a terminal to send another character
473*0Sstevel@tonic-gate  * (at 15cps repeat rate, this is 67 ms, I'm using 100ms to allow
474*0Sstevel@tonic-gate  * a bit of a fudge factor) and time out more quickly.
475*0Sstevel@tonic-gate  * -2 is returned if we time out, -1 is returned if interrupted, and the
476*0Sstevel@tonic-gate  * character is returned otherwise.
477*0Sstevel@tonic-gate  */
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate #ifndef	FIONREAD
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  * Traditional implementation.  The best resolution we have is 1 second,
483*0Sstevel@tonic-gate  * so we set a 1 second alarm and try to read.  If we fail for 1 second,
484*0Sstevel@tonic-gate  * we assume there is no key waiting.  Problem here is that 1 second is
485*0Sstevel@tonic-gate  * too long; people can type faster than this.
486*0Sstevel@tonic-gate  *
487*0Sstevel@tonic-gate  * Another possible implementation of changing VMIN/VTIME before and
488*0Sstevel@tonic-gate  * after each read does not work because the tty driver's timeout
489*0Sstevel@tonic-gate  * mechanism is too unreliable when the timeouts are changed too quickly.
490*0Sstevel@tonic-gate  */
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate static	char	sig_caught;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate static
495*0Sstevel@tonic-gate #ifdef	SIGPOLL	/* Vr3 and beyond */
496*0Sstevel@tonic-gate void
497*0Sstevel@tonic-gate #endif  /* SIGPOLL */
498*0Sstevel@tonic-gate /* The following line causes a lint warning for "dummy" which is not used. */
499*0Sstevel@tonic-gate _catch_alarm(int dummy)
500*0Sstevel@tonic-gate {
501*0Sstevel@tonic-gate 	sig_caught = 1;
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static int
505*0Sstevel@tonic-gate _fpk(void)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	unsigned	char	c;
508*0Sstevel@tonic-gate 	int		infd = cur_term->_inputfd;
509*0Sstevel@tonic-gate 	ssize_t		rc;
510*0Sstevel@tonic-gate #ifdef	SIGPOLL	/* Vr3 and beyond */
511*0Sstevel@tonic-gate 	void	(*oldsig)(int);
512*0Sstevel@tonic-gate #else	/* SIGPOLL */
513*0Sstevel@tonic-gate 	int		(*oldsig)(int);
514*0Sstevel@tonic-gate #endif	/* SIGPOLL */
515*0Sstevel@tonic-gate 	unsigned	int	oldalarm, alarm(unsigned);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	/* turn off any user alarms and set our own */
518*0Sstevel@tonic-gate 	oldalarm = alarm(0);
519*0Sstevel@tonic-gate 	sig_caught = 0;
520*0Sstevel@tonic-gate 	oldsig = signal(SIGALRM, _catch_alarm);
521*0Sstevel@tonic-gate 	(void) alarm(1);
522*0Sstevel@tonic-gate 	rc = read(cur_term->_inputfd, (char *)&c, 1);
523*0Sstevel@tonic-gate 	(void) alarm(0);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	/*
526*0Sstevel@tonic-gate 	 * This code is to take care of the possibility of
527*0Sstevel@tonic-gate 	 * the process getting swapped out in the middle of
528*0Sstevel@tonic-gate 	 * read() call above. The interrupt will cause the
529*0Sstevel@tonic-gate 	 * read() call to retur, even if a character is really
530*0Sstevel@tonic-gate 	 * on the clist. So we do a non-blocking read() to make
531*0Sstevel@tonic-gate 	 * sure that there really isn't a character there.
532*0Sstevel@tonic-gate 	 */
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	if (sig_caught && rc != 1)
535*0Sstevel@tonic-gate 		if (cur_term->_check_fd != -1)
536*0Sstevel@tonic-gate 			rc = read(cur_term->_check_fd, (char *)&c, 1);
537*0Sstevel@tonic-gate 		else {
538*0Sstevel@tonic-gate #include	<fcntl.h>
539*0Sstevel@tonic-gate 			int	fcflags = fcntl(infd, F_GETFL, 0);
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 			(void) fcntl(infd, F_SETFL, fcflags | O_NDELAY);
542*0Sstevel@tonic-gate 			rc = read(infd, (char *)&c, 1);
543*0Sstevel@tonic-gate 			(void) fcntl(infd, F_SETFL, fcflags);
544*0Sstevel@tonic-gate 		}
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	/* restore the user alarms */
547*0Sstevel@tonic-gate 	(void) signal(SIGALRM, oldsig);
548*0Sstevel@tonic-gate 	if (sig_caught && oldalarm > 1)
549*0Sstevel@tonic-gate 	    oldalarm--;
550*0Sstevel@tonic-gate 	(void) alarm(oldalarm);
551*0Sstevel@tonic-gate 	if (rc == 1)			/* got a character */
552*0Sstevel@tonic-gate 	    return (c);
553*0Sstevel@tonic-gate 	else
554*0Sstevel@tonic-gate 	    if (sig_caught)		/* timed out */
555*0Sstevel@tonic-gate 		return (-2);
556*0Sstevel@tonic-gate 	    else			/* EOF or got interrupted */
557*0Sstevel@tonic-gate 		return (-1);
558*0Sstevel@tonic-gate }
559*0Sstevel@tonic-gate #else	/* FIONREAD */
560*0Sstevel@tonic-gate /*
561*0Sstevel@tonic-gate  * If we have the select system call, we can do much better than the
562*0Sstevel@tonic-gate  * traditional method. Even if we don't have the real 4.2BSD select, we
563*0Sstevel@tonic-gate  * can emulate it with napms and FIONREAD.  napms might be done with only
564*0Sstevel@tonic-gate  * 1 second resolution, but this is no worse than what we have in the
565*0Sstevel@tonic-gate  * traditional implementation.
566*0Sstevel@tonic-gate  */
567*0Sstevel@tonic-gate static
568*0Sstevel@tonic-gate _fpk()
569*0Sstevel@tonic-gate {
570*0Sstevel@tonic-gate 	int		infd, rc;
571*0Sstevel@tonic-gate 	int		*outfd, *exfd;
572*0Sstevel@tonic-gate 	unsigned	char	c;
573*0Sstevel@tonic-gate 	struct timeval	t;
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	infd = 1 << cur_term->_inputfd;
576*0Sstevel@tonic-gate 	outfd = exfd = (int *)NULL;
577*0Sstevel@tonic-gate 	t.tv_sec = 0;
578*0Sstevel@tonic-gate 	t.tv_usec = 100000;		/* 100 milliseconds */
579*0Sstevel@tonic-gate 	rc = select(20, &infd, outfd, exfd, &t);
580*0Sstevel@tonic-gate 	if (rc < 0)
581*0Sstevel@tonic-gate 		return (-2);
582*0Sstevel@tonic-gate 	rc = read(fileno(f), &c, 1);
583*0Sstevel@tonic-gate 	return (rc == 1 ? c : -1);
584*0Sstevel@tonic-gate }
585*0Sstevel@tonic-gate #endif	/* FIONREAD */
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate /*
588*0Sstevel@tonic-gate  * Plain peekchar function.  Nothing fancy.  This is just like _fpk
589*0Sstevel@tonic-gate  * but will wait forever rather than time out.
590*0Sstevel@tonic-gate  */
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate static int
593*0Sstevel@tonic-gate _pk(void)
594*0Sstevel@tonic-gate {
595*0Sstevel@tonic-gate 	unsigned	char	c;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	return ((read(cur_term->_inputfd, (char *)&c, 1) == 1) ? c : ERR);
598*0Sstevel@tonic-gate }
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate /*
602*0Sstevel@tonic-gate  * SS-mouse: check if this mouse button event should map into
603*0Sstevel@tonic-gate  * function key
604*0Sstevel@tonic-gate  */
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate static void
608*0Sstevel@tonic-gate _map_button(chtype *inp)
609*0Sstevel@tonic-gate {
610*0Sstevel@tonic-gate 	SLK_MAP *slk = SP->slk;
611*0Sstevel@tonic-gate 	int num = slk->_num;
612*0Sstevel@tonic-gate 	int len = slk->_len;
613*0Sstevel@tonic-gate 	int i;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	/* first determine if this mouse button event should be */
616*0Sstevel@tonic-gate 	/* mapped into function key				*/
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	if (!(SP->_map_mbe_to_key &
619*0Sstevel@tonic-gate 	    ((BUTTON_CHANGED(3) << (10 + BUTTON_STATUS(3))) |
620*0Sstevel@tonic-gate 	    (BUTTON_CHANGED(2) << (5 + BUTTON_STATUS(2)))  |
621*0Sstevel@tonic-gate 	    (BUTTON_CHANGED(1) << BUTTON_STATUS(1)))))
622*0Sstevel@tonic-gate 		return;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
625*0Sstevel@tonic-gate 		if (MOUSE_X_POS < slk->_labx[i])
626*0Sstevel@tonic-gate 			break;
627*0Sstevel@tonic-gate 		if (MOUSE_X_POS > slk->_labx[i] + len)
628*0Sstevel@tonic-gate 			continue;
629*0Sstevel@tonic-gate 		*inp = KEY_F(1) + i;
630*0Sstevel@tonic-gate 		break;
631*0Sstevel@tonic-gate 	}
632*0Sstevel@tonic-gate }
633