input.c revision 1.7
1/* $OpenBSD: input.c,v 1.7 2001/09/27 08:37:33 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. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)input.c 8.1 (Berkeley) 5/31/93 40 */ 41 42/* 43 * Tetris input. 44 */ 45 46#include <sys/types.h> 47#include <sys/time.h> 48 49#include <errno.h> 50#include <unistd.h> 51#include <string.h> 52 53#include "input.h" 54#include "tetris.h" 55 56/* return true iff the given timeval is positive */ 57#define TV_POS(tv) \ 58 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0)) 59 60/* subtract timeval `sub' from `res' */ 61#define TV_SUB(res, sub) \ 62 (res)->tv_sec -= (sub)->tv_sec; \ 63 (res)->tv_usec -= (sub)->tv_usec; \ 64 if ((res)->tv_usec < 0) { \ 65 (res)->tv_usec += 1000000; \ 66 (res)->tv_sec--; \ 67 } 68 69/* 70 * Do a `read wait': select for reading from stdin, with timeout *tvp. 71 * On return, modify *tvp to reflect the amount of time spent waiting. 72 * It will be positive only if input appeared before the time ran out; 73 * otherwise it will be zero or perhaps negative. 74 * 75 * If tvp is nil, wait forever, but return if select is interrupted. 76 * 77 * Return 0 => no input, 1 => can read() from stdin 78 */ 79int 80rwait(tvp) 81 register struct timeval *tvp; 82{ 83 int i; 84 struct timeval starttv, endtv, *s; 85 fd_set fds; 86 87#define NILTZ ((struct timezone *)0) 88 89 /* 90 * Someday, select() will do this for us. 91 * Just in case that day is now, and no one has 92 * changed this, we use a temporary. 93 */ 94 if (tvp) { 95 (void) gettimeofday(&starttv, NILTZ); 96 endtv = *tvp; 97 s = &endtv; 98 } else 99 s = NULL; 100again: 101 FD_ZERO(&fds); 102 FD_SET(STDIN_FILENO, &fds); 103 switch (select(STDIN_FILENO + 1, &fds, (fd_set *)0, (fd_set *)0, s)) { 104 105 case -1: 106 if (tvp == 0) 107 return (-1); 108 if (errno == EINTR) 109 goto again; 110 stop("select failed, help"); 111 /* NOTREACHED */ 112 113 case 0: /* timed out */ 114 tvp->tv_sec = 0; 115 tvp->tv_usec = 0; 116 return (0); 117 } 118 if (tvp) { 119 /* since there is input, we may not have timed out */ 120 (void) gettimeofday(&endtv, NILTZ); 121 TV_SUB(&endtv, &starttv); 122 TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */ 123 } 124 return (1); 125} 126 127/* 128 * `sleep' for the current turn time (using select). 129 * Eat any input that might be available. 130 */ 131void 132tsleep() 133{ 134 struct timeval tv; 135 char c; 136 137 tv.tv_sec = 0; 138 tv.tv_usec = fallrate; 139 while (TV_POS(&tv)) 140 if (rwait(&tv) && read(0, &c, 1) != 1) 141 break; 142} 143 144/* 145 * getchar with timeout. 146 */ 147int 148tgetchar() 149{ 150 static struct timeval timeleft; 151 char c; 152 153 /* 154 * Reset timeleft to fallrate whenever it is not positive. 155 * In any case, wait to see if there is any input. If so, 156 * take it, and update timeleft so that the next call to 157 * tgetchar() will not wait as long. If there is no input, 158 * make timeleft zero or negative, and return -1. 159 * 160 * Most of the hard work is done by rwait(). 161 */ 162 if (!TV_POS(&timeleft)) { 163 faster(); /* go faster */ 164 timeleft.tv_sec = 0; 165 timeleft.tv_usec = fallrate; 166 } 167 if (!rwait(&timeleft)) 168 return (-1); 169 if (read(0, &c, 1) != 1) 170 stop("end of file, help"); 171 return ((int)(unsigned char)c); 172} 173