2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A/* Copyright (c) 1988 AT&T */ 2N/A/* All Rights Reserved */ 2N/A * University Copyright- Copyright (c) 1982, 1986, 1988 2N/A * The Regents of the University of California 2N/A * All Rights Reserved 2N/A * University Acknowledgment- Portions of this document are derived from 2N/A * software developed by the University of California, Berkeley, and its 2N/A * Read a key typed from the terminal 2N/A * interpret: = 0 for single-char key only 2N/A * = 1 for matching function key and macro patterns. 2N/A * = 2 same as 1 but no time-out for funckey matching. 2N/A * Register the fact that getch is being used so 2N/A * that typeahead checking can be done. 2N/A * This code should GO AWAY when a poll() or FIONREAD can 2N/A * be done on the file descriptor as then the check 2N/A * will be non-destructive. 2N/A /* decode an ungetch()'d character */ 2N/A /* (*chars_onQ)++; MR */ 2N/A * interpret is set to 0 so that down below we don't 2N/A * drop into getkey since we already know there can't be 2N/A * a key that starts with -1. Also, we don't want to 2N/A * access funckeystarter[-1]. 2N/A#
endif /* FIONREAD */ 2N/A /* Check for arrow and function keys */ 2N/A /* Collapse the input queue to remove the escape */ 2N/A /* sequence from the stack. */ 2N/A#
endif /* !FIONREAD */ 2N/A * This algorithm is a "learning" algorithm. The premise is 2N/A * that keys used once are like to be used again and again. 2N/A * Since the time for a linear search of the table is so 2N/A * expensive, we move keys that are found up to the top of 2N/A * the list, making the access to a repeated key very fast and 2N/A * keys that have been used before close to the top. 2N/A /* partial match? peek ahead. */ 2N/A * Since -2 signifies a timeout we don't really 2N/A * want to put it on the queue so we decrement 2N/A * We have to decrement one because key will be 2N/A * incremented at the bottom of the out loop. 2N/A /* read the mouse status information */ 2N/A rc = -
3;
/* NOT IMPLEMENTED */ 2N/A if (
rc == -
1)
/* read error */ 2N/A else if (
rc == -
2 ||
rc == -
3)
/* timeout */ 2N/A else if (
rc == 0)
/* report mouse pos */ 2N/A /* mouse button event */ 2N/A /* We found it! Read in any chars left in _sends */ 2N/A /* move key to top of ordered list */ 2N/A * If we're below the last ordered key, swap next unordered 2N/A * key with this one and ripple from there. 2N/A /* ripple the ordered keys down */ 2N/A * SS-mouse support: if mouse button event 2N/A * occured on top of the soft label, we may 2N/A * have to return the function key corresponding 2N/A * to that soft label 2N/A/* this function tries to read in information that follows KEY_MOUSE: */ 2N/A/* the first character identifies what button is involved (1,2,or 3) */ 2N/A/* if the first character is 0, we are dealing with report_mouse_pos */ 2N/A * The routine returns the following: 2N/A * -3: not a mouse button event 2N/A * -2: read timed out 2N/A * -1: the read failed 2N/A * [0, 1, 2, 3] - the first character in the mouse event 2N/A /* the first character should be 0, 1, 2, or 4 */ 2N/A /* read error or timeout */ 2N/A /* if the character is 1, 2, or 3 it must be followed by */ 2N/A /* P, R, C, D, or T */ 2N/A /* read X and Y coordinates of the mouse */ 2N/A for (j = 0; j <
2; j++) {
2N/A if (
c1 >=
' ' &&
c1 <=
'~') {
/* ascii char */ 2N/A /* read complete mouse event: update the Mouse_status structure */ 2N/A * Fast peek key. Like getchar but if the right flags are set, times out 2N/A * quickly if there is nothing waiting, returning -1. 2N/A * f is an output stdio descriptor, we read from the fileno. 2N/A * We wait for long enough for a terminal to send another character 2N/A * (at 15cps repeat rate, this is 67 ms, I'm using 100ms to allow 2N/A * a bit of a fudge factor) and time out more quickly. 2N/A * -2 is returned if we time out, -1 is returned if interrupted, and the 2N/A * character is returned otherwise. 2N/A * Traditional implementation. The best resolution we have is 1 second, 2N/A * so we set a 1 second alarm and try to read. If we fail for 1 second, 2N/A * we assume there is no key waiting. Problem here is that 1 second is 2N/A * too long; people can type faster than this. 2N/A * Another possible implementation of changing VMIN/VTIME before and 2N/A * after each read does not work because the tty driver's timeout 2N/A * mechanism is too unreliable when the timeouts are changed too quickly. 2N/A/* The following line causes a lint warning for "dummy" which is not used. */ 2N/A /* turn off any user alarms and set our own */ 2N/A * This code is to take care of the possibility of 2N/A * the process getting swapped out in the middle of 2N/A * read() call above. The interrupt will cause the 2N/A * read() call to retur, even if a character is really 2N/A * on the clist. So we do a non-blocking read() to make 2N/A * sure that there really isn't a character there. 2N/A /* restore the user alarms */ 2N/A if (
rc ==
1)
/* got a character */ 2N/A else /* EOF or got interrupted */ 2N/A * If we have the select system call, we can do much better than the 2N/A * traditional method. Even if we don't have the real 4.2BSD select, we 2N/A * can emulate it with napms and FIONREAD. napms might be done with only 2N/A * 1 second resolution, but this is no worse than what we have in the 2N/A * traditional implementation. 2N/A return (
rc ==
1 ? c : -
1);
2N/A#
endif /* FIONREAD */ 2N/A * Plain peekchar function. Nothing fancy. This is just like _fpk 2N/A * but will wait forever rather than time out. 2N/A * SS-mouse: check if this mouse button event should map into 2N/A /* first determine if this mouse button event should be */ 2N/A /* mapped into function key */