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