[5c058d50] | 1 | /* $OpenBSD: input.c,v 1.12 2005/04/13 02:33:08 deraadt Exp $ */
|
---|
| 2 | /* $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $ */
|
---|
| 3 |
|
---|
| 4 | /*-
|
---|
| 5 | * Copyright (c) 1992, 1993
|
---|
| 6 | * The Regents of the University of California. All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * This code is derived from software contributed to Berkeley by
|
---|
| 9 | * Chris Torek and Darren F. Provine.
|
---|
| 10 | *
|
---|
| 11 | * Redistribution and use in source and binary forms, with or without
|
---|
| 12 | * modification, are permitted provided that the following conditions
|
---|
| 13 | * are met:
|
---|
| 14 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer.
|
---|
| 16 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 17 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 18 | * documentation and/or other materials provided with the distribution.
|
---|
| 19 | * 3. Neither the name of the University nor the names of its contributors
|
---|
| 20 | * may be used to endorse or promote products derived from this software
|
---|
| 21 | * without specific prior written permission.
|
---|
| 22 | *
|
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 33 | * SUCH DAMAGE.
|
---|
| 34 | *
|
---|
| 35 | * @(#)input.c 8.1 (Berkeley) 5/31/93
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /** @addtogroup top
|
---|
| 39 | * @{
|
---|
| 40 | */
|
---|
| 41 | /** @file
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | /*
|
---|
| 45 | * Top input.
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | #include <sys/types.h>
|
---|
| 49 | #include <sys/time.h>
|
---|
| 50 | #include <stdio.h>
|
---|
| 51 |
|
---|
| 52 | #include <stdlib.h>
|
---|
| 53 | #include <errno.h>
|
---|
| 54 | #include <unistd.h>
|
---|
| 55 | #include <str.h>
|
---|
| 56 |
|
---|
| 57 | #include "input.h"
|
---|
| 58 |
|
---|
| 59 | #include <async.h>
|
---|
| 60 | #include <vfs/vfs.h>
|
---|
| 61 | #include <io/console.h>
|
---|
| 62 | #include <ipc/console.h>
|
---|
| 63 |
|
---|
[79edc36] | 64 | #define USEC_COUNT 1000000
|
---|
[5c058d50] | 65 |
|
---|
| 66 | /* return true iff the given timeval is positive */
|
---|
| 67 | #define TV_POS(tv) \
|
---|
| 68 | ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
|
---|
| 69 |
|
---|
| 70 | /* subtract timeval `sub' from `res' */
|
---|
| 71 | #define TV_SUB(res, sub) \
|
---|
| 72 | (res)->tv_sec -= (sub)->tv_sec; \
|
---|
| 73 | (res)->tv_usec -= (sub)->tv_usec; \
|
---|
| 74 | if ((res)->tv_usec < 0) { \
|
---|
| 75 | (res)->tv_usec += 1000000; \
|
---|
| 76 | (res)->tv_sec--; \
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /* We will use a hack here - if lastchar is non-zero, it is
|
---|
| 80 | * the last character read. We will somehow simulate the select
|
---|
| 81 | * semantics.
|
---|
| 82 | */
|
---|
| 83 | static aid_t getchar_inprog = 0;
|
---|
| 84 | static char lastchar = '\0';
|
---|
| 85 |
|
---|
| 86 | /*
|
---|
| 87 | * Do a `read wait': select for reading from stdin, with timeout *tvp.
|
---|
| 88 | * On return, modify *tvp to reflect the amount of time spent waiting.
|
---|
| 89 | * It will be positive only if input appeared before the time ran out;
|
---|
| 90 | * otherwise it will be zero or perhaps negative.
|
---|
| 91 | *
|
---|
| 92 | * If tvp is nil, wait forever, but return if select is interrupted.
|
---|
| 93 | *
|
---|
| 94 | * Return 0 => no input, 1 => can read() from stdin
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
| 97 | int rwait(struct timeval *tvp)
|
---|
| 98 | {
|
---|
| 99 | struct timeval starttv, endtv, *s;
|
---|
| 100 | static ipc_call_t charcall;
|
---|
[96b02eb9] | 101 | sysarg_t rc;
|
---|
[5c058d50] | 102 |
|
---|
| 103 | /*
|
---|
| 104 | * Someday, select() will do this for us.
|
---|
| 105 | * Just in case that day is now, and no one has
|
---|
| 106 | * changed this, we use a temporary.
|
---|
| 107 | */
|
---|
| 108 | if (tvp) {
|
---|
| 109 | (void) gettimeofday(&starttv, NULL);
|
---|
| 110 | endtv = *tvp;
|
---|
| 111 | s = &endtv;
|
---|
| 112 | } else
|
---|
| 113 | s = NULL;
|
---|
| 114 |
|
---|
| 115 | if (!lastchar) {
|
---|
| 116 | again:
|
---|
| 117 | if (!getchar_inprog) {
|
---|
| 118 | getchar_inprog = async_send_0(fphone(stdin),
|
---|
| 119 | CONSOLE_GET_EVENT, &charcall);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | if (!s)
|
---|
| 123 | async_wait_for(getchar_inprog, &rc);
|
---|
| 124 | else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) {
|
---|
| 125 | tvp->tv_sec = 0;
|
---|
| 126 | tvp->tv_usec = 0;
|
---|
| 127 | return (0);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | getchar_inprog = 0;
|
---|
| 131 | if (rc) {
|
---|
| 132 | printf("End of file, bug?\n");
|
---|
| 133 | exit(1);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if (IPC_GET_ARG1(charcall) == KEY_RELEASE)
|
---|
| 137 | goto again;
|
---|
| 138 |
|
---|
| 139 | lastchar = IPC_GET_ARG4(charcall);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (tvp) {
|
---|
| 143 | /* since there is input, we may not have timed out */
|
---|
| 144 | (void) gettimeofday(&endtv, NULL);
|
---|
| 145 | TV_SUB(&endtv, &starttv);
|
---|
| 146 | TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return 1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /*
|
---|
| 153 | * `sleep' for the current turn time (using select).
|
---|
| 154 | * Eat any input that might be available.
|
---|
| 155 | */
|
---|
[79edc36] | 156 | void tsleep(unsigned int sec)
|
---|
[5c058d50] | 157 | {
|
---|
| 158 | struct timeval tv;
|
---|
| 159 |
|
---|
| 160 | tv.tv_sec = 0;
|
---|
[79edc36] | 161 | tv.tv_usec = sec * USEC_COUNT;
|
---|
[5c058d50] | 162 | while (TV_POS(&tv))
|
---|
| 163 | if (rwait(&tv)) {
|
---|
| 164 | lastchar = '\0';
|
---|
| 165 | } else
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /*
|
---|
| 170 | * getchar with timeout.
|
---|
| 171 | */
|
---|
[79edc36] | 172 | int tgetchar(unsigned int sec)
|
---|
[5c058d50] | 173 | {
|
---|
| 174 | static struct timeval timeleft;
|
---|
| 175 | char c;
|
---|
| 176 |
|
---|
| 177 | /*
|
---|
[79edc36] | 178 | * Reset timeleft to USEC_COUNT whenever it is not positive.
|
---|
[5c058d50] | 179 | * In any case, wait to see if there is any input. If so,
|
---|
| 180 | * take it, and update timeleft so that the next call to
|
---|
| 181 | * tgetchar() will not wait as long. If there is no input,
|
---|
| 182 | * make timeleft zero or negative, and return -1.
|
---|
| 183 | *
|
---|
| 184 | * Most of the hard work is done by rwait().
|
---|
| 185 | */
|
---|
| 186 | if (!TV_POS(&timeleft)) {
|
---|
| 187 | timeleft.tv_sec = 0;
|
---|
[79edc36] | 188 | timeleft.tv_usec = sec * USEC_COUNT;
|
---|
[5c058d50] | 189 | }
|
---|
| 190 |
|
---|
| 191 | if (!rwait(&timeleft))
|
---|
| 192 | return -1;
|
---|
| 193 |
|
---|
| 194 | c = lastchar;
|
---|
| 195 | lastchar = '\0';
|
---|
| 196 | return ((int) (unsigned char) c);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /** @}
|
---|
| 200 | */
|
---|