[e9a3c52] | 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 |
|
---|
[b2951e2] | 38 | /** @addtogroup tetris
|
---|
[ebe70f1] | 39 | * @{
|
---|
[b2951e2] | 40 | */
|
---|
| 41 | /** @file
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[e9a3c52] | 44 | /*
|
---|
| 45 | * Tetris input.
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | #include <sys/types.h>
|
---|
| 49 | #include <sys/time.h>
|
---|
[86029498] | 50 | #include <stdio.h>
|
---|
[e9a3c52] | 51 |
|
---|
| 52 | #include <errno.h>
|
---|
| 53 | #include <unistd.h>
|
---|
| 54 | #include <string.h>
|
---|
| 55 |
|
---|
| 56 | #include "input.h"
|
---|
| 57 | #include "tetris.h"
|
---|
| 58 |
|
---|
[c0e674a] | 59 | #include <async.h>
|
---|
[0c25c10] | 60 | #include <vfs/vfs.h>
|
---|
| 61 | #include <io/console.h>
|
---|
[d3e6935] | 62 | #include <ipc/console.h>
|
---|
[c0e674a] | 63 |
|
---|
[e9a3c52] | 64 | /* return true iff the given timeval is positive */
|
---|
| 65 | #define TV_POS(tv) \
|
---|
| 66 | ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
|
---|
| 67 |
|
---|
| 68 | /* subtract timeval `sub' from `res' */
|
---|
| 69 | #define TV_SUB(res, sub) \
|
---|
| 70 | (res)->tv_sec -= (sub)->tv_sec; \
|
---|
| 71 | (res)->tv_usec -= (sub)->tv_usec; \
|
---|
| 72 | if ((res)->tv_usec < 0) { \
|
---|
| 73 | (res)->tv_usec += 1000000; \
|
---|
| 74 | (res)->tv_sec--; \
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[5052046] | 77 | /* We will use a hack here - if lastchar is non-zero, it is
|
---|
| 78 | * the last character read. We will somehow simulate the select
|
---|
| 79 | * semantics.
|
---|
| 80 | */
|
---|
[c0e674a] | 81 | static aid_t getchar_inprog = 0;
|
---|
[5052046] | 82 | static char lastchar = '\0';
|
---|
[c0e674a] | 83 |
|
---|
[e9a3c52] | 84 | /*
|
---|
| 85 | * Do a `read wait': select for reading from stdin, with timeout *tvp.
|
---|
| 86 | * On return, modify *tvp to reflect the amount of time spent waiting.
|
---|
| 87 | * It will be positive only if input appeared before the time ran out;
|
---|
| 88 | * otherwise it will be zero or perhaps negative.
|
---|
| 89 | *
|
---|
| 90 | * If tvp is nil, wait forever, but return if select is interrupted.
|
---|
| 91 | *
|
---|
| 92 | * Return 0 => no input, 1 => can read() from stdin
|
---|
[5052046] | 93 | *
|
---|
[e9a3c52] | 94 | */
|
---|
[ebe70f1] | 95 | int rwait(struct timeval *tvp)
|
---|
[e9a3c52] | 96 | {
|
---|
| 97 | struct timeval starttv, endtv, *s;
|
---|
[5052046] | 98 | static ipc_call_t charcall;
|
---|
[59ed572] | 99 | ipcarg_t rc;
|
---|
[ebe70f1] | 100 |
|
---|
[e9a3c52] | 101 | /*
|
---|
| 102 | * Someday, select() will do this for us.
|
---|
| 103 | * Just in case that day is now, and no one has
|
---|
| 104 | * changed this, we use a temporary.
|
---|
| 105 | */
|
---|
| 106 | if (tvp) {
|
---|
[86029498] | 107 | (void) gettimeofday(&starttv, NULL);
|
---|
[e9a3c52] | 108 | endtv = *tvp;
|
---|
| 109 | s = &endtv;
|
---|
| 110 | } else
|
---|
| 111 | s = NULL;
|
---|
[ebe70f1] | 112 |
|
---|
[5052046] | 113 | if (!lastchar) {
|
---|
[3d360758] | 114 | again:
|
---|
[4e4052b] | 115 | if (!getchar_inprog) {
|
---|
[0c25c10] | 116 | getchar_inprog = async_send_0(fphone(stdin),
|
---|
| 117 | CONSOLE_GET_EVENT, &charcall);
|
---|
[4e4052b] | 118 | }
|
---|
[ebe70f1] | 119 |
|
---|
[0c25c10] | 120 | if (!s)
|
---|
[86029498] | 121 | async_wait_for(getchar_inprog, &rc);
|
---|
| 122 | else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) {
|
---|
[5052046] | 123 | tvp->tv_sec = 0;
|
---|
| 124 | tvp->tv_usec = 0;
|
---|
| 125 | return (0);
|
---|
| 126 | }
|
---|
[ebe70f1] | 127 |
|
---|
[5052046] | 128 | getchar_inprog = 0;
|
---|
[ebe70f1] | 129 | if (rc)
|
---|
[5052046] | 130 | stop("end of file, help");
|
---|
[ebe70f1] | 131 |
|
---|
[0c25c10] | 132 | if (IPC_GET_ARG1(charcall) == KEY_RELEASE)
|
---|
[3d360758] | 133 | goto again;
|
---|
[ebe70f1] | 134 |
|
---|
[fa09449] | 135 | lastchar = IPC_GET_ARG4(charcall);
|
---|
[e9a3c52] | 136 | }
|
---|
[ebe70f1] | 137 |
|
---|
[e9a3c52] | 138 | if (tvp) {
|
---|
| 139 | /* since there is input, we may not have timed out */
|
---|
[86029498] | 140 | (void) gettimeofday(&endtv, NULL);
|
---|
[e9a3c52] | 141 | TV_SUB(&endtv, &starttv);
|
---|
[ebe70f1] | 142 | TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */
|
---|
[e9a3c52] | 143 | }
|
---|
[ebe70f1] | 144 |
|
---|
| 145 | return 1;
|
---|
[e9a3c52] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /*
|
---|
| 149 | * `sleep' for the current turn time (using select).
|
---|
| 150 | * Eat any input that might be available.
|
---|
| 151 | */
|
---|
[ebe70f1] | 152 | void tsleep(void)
|
---|
[e9a3c52] | 153 | {
|
---|
| 154 | struct timeval tv;
|
---|
[ebe70f1] | 155 |
|
---|
[e9a3c52] | 156 | tv.tv_sec = 0;
|
---|
| 157 | tv.tv_usec = fallrate;
|
---|
| 158 | while (TV_POS(&tv))
|
---|
[5052046] | 159 | if (rwait(&tv)) {
|
---|
| 160 | lastchar = '\0';
|
---|
| 161 | } else
|
---|
[e9a3c52] | 162 | break;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | * getchar with timeout.
|
---|
| 167 | */
|
---|
[ebe70f1] | 168 | int tgetchar(void)
|
---|
[e9a3c52] | 169 | {
|
---|
| 170 | static struct timeval timeleft;
|
---|
| 171 | char c;
|
---|
[ebe70f1] | 172 |
|
---|
[e9a3c52] | 173 | /*
|
---|
| 174 | * Reset timeleft to fallrate whenever it is not positive.
|
---|
| 175 | * In any case, wait to see if there is any input. If so,
|
---|
| 176 | * take it, and update timeleft so that the next call to
|
---|
| 177 | * tgetchar() will not wait as long. If there is no input,
|
---|
| 178 | * make timeleft zero or negative, and return -1.
|
---|
| 179 | *
|
---|
| 180 | * Most of the hard work is done by rwait().
|
---|
| 181 | */
|
---|
| 182 | if (!TV_POS(&timeleft)) {
|
---|
[ebe70f1] | 183 | faster(); /* go faster */
|
---|
[e9a3c52] | 184 | timeleft.tv_sec = 0;
|
---|
| 185 | timeleft.tv_usec = fallrate;
|
---|
| 186 | }
|
---|
[ebe70f1] | 187 |
|
---|
[e9a3c52] | 188 | if (!rwait(&timeleft))
|
---|
[ebe70f1] | 189 | return -1;
|
---|
| 190 |
|
---|
[5052046] | 191 | c = lastchar;
|
---|
| 192 | lastchar = '\0';
|
---|
[ebe70f1] | 193 | return ((int) (unsigned char) c);
|
---|
[e9a3c52] | 194 | }
|
---|
[b2951e2] | 195 |
|
---|
| 196 | /** @}
|
---|
| 197 | */
|
---|