1*176f6ce7Stedu /* $OpenBSD: input.c,v 1.19 2017/08/13 02:12:16 tedu Exp $ */
2e0232237Sniklas /* $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1992, 1993
6df930be7Sderaadt * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * This code is derived from software contributed to Berkeley by
9df930be7Sderaadt * Chris Torek and Darren F. Provine.
10df930be7Sderaadt *
11df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt * modification, are permitted provided that the following conditions
13df930be7Sderaadt * are met:
14df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt * documentation and/or other materials provided with the distribution.
197a09557bSmillert * 3. Neither the name of the University nor the names of its contributors
20df930be7Sderaadt * may be used to endorse or promote products derived from this software
21df930be7Sderaadt * without specific prior written permission.
22df930be7Sderaadt *
23df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt * SUCH DAMAGE.
34df930be7Sderaadt *
35df930be7Sderaadt * @(#)input.c 8.1 (Berkeley) 5/31/93
36df930be7Sderaadt */
37df930be7Sderaadt
38df930be7Sderaadt /*
39df930be7Sderaadt * Tetris input.
40df930be7Sderaadt */
41df930be7Sderaadt
4279af1000Sguenther #include <sys/time.h>
43*176f6ce7Stedu
44df930be7Sderaadt #include <errno.h>
455e640057Sderaadt #include <poll.h>
46*176f6ce7Stedu #include <time.h>
472010f3c8Smestre #include <unistd.h>
48df930be7Sderaadt
49df930be7Sderaadt #include "input.h"
50df930be7Sderaadt #include "tetris.h"
51df930be7Sderaadt
52*176f6ce7Stedu /* return true iff the given timespec is positive */
53*176f6ce7Stedu #define TS_POS(ts) \
54*176f6ce7Stedu ((ts)->tv_sec > 0 || ((ts)->tv_sec == 0 && (ts)->tv_nsec > 0))
55df930be7Sderaadt
56df930be7Sderaadt /*
57*176f6ce7Stedu * Do a `read wait': poll for reading from stdin, with timeout *limit.
58*176f6ce7Stedu * On return, subtract the time spent waiting from *limit.
59df930be7Sderaadt * It will be positive only if input appeared before the time ran out;
60df930be7Sderaadt * otherwise it will be zero or perhaps negative.
61df930be7Sderaadt *
62*176f6ce7Stedu * If limit is NULL, wait forever, but return if poll is interrupted.
63df930be7Sderaadt *
64*176f6ce7Stedu * Return 0 => no input, 1 => can read() from stdin, -1 => interrupted
65df930be7Sderaadt */
66df930be7Sderaadt int
rwait(struct timespec * limit)67*176f6ce7Stedu rwait(struct timespec *limit)
68df930be7Sderaadt {
69*176f6ce7Stedu struct timespec start, end, elapsed;
705e640057Sderaadt struct pollfd pfd[1];
71f26eb57bSderaadt
725e640057Sderaadt pfd[0].fd = STDIN_FILENO;
735e640057Sderaadt pfd[0].events = POLLIN;
74*176f6ce7Stedu
75*176f6ce7Stedu if (limit != NULL)
76*176f6ce7Stedu clock_gettime(CLOCK_MONOTONIC, &start);
77*176f6ce7Stedu again:
78*176f6ce7Stedu switch (ppoll(pfd, 1, limit, NULL)) {
79df930be7Sderaadt case -1:
80*176f6ce7Stedu if (limit == NULL)
81df930be7Sderaadt return (-1);
82df930be7Sderaadt if (errno == EINTR)
83df930be7Sderaadt goto again;
847215c05fStedu stop("poll failed, help");
85df930be7Sderaadt case 0: /* timed out */
86*176f6ce7Stedu timespecclear(limit);
87df930be7Sderaadt return (0);
88df930be7Sderaadt }
89*176f6ce7Stedu if (limit != NULL) {
90*176f6ce7Stedu /* we have input, so subtract the elapsed time from *limit */
91*176f6ce7Stedu clock_gettime(CLOCK_MONOTONIC, &end);
92*176f6ce7Stedu timespecsub(&end, &start, &elapsed);
93*176f6ce7Stedu timespecsub(limit, &elapsed, limit);
94df930be7Sderaadt }
95df930be7Sderaadt return (1);
96df930be7Sderaadt }
97df930be7Sderaadt
98df930be7Sderaadt /*
99*176f6ce7Stedu * `sleep' for the current turn time and eat any
100*176f6ce7Stedu * input that becomes available.
101df930be7Sderaadt */
102df930be7Sderaadt void
tsleep(void)103ff8320a7Sderaadt tsleep(void)
104df930be7Sderaadt {
105*176f6ce7Stedu struct timespec ts;
106df930be7Sderaadt char c;
107df930be7Sderaadt
108*176f6ce7Stedu ts.tv_sec = 0;
109*176f6ce7Stedu ts.tv_nsec = fallrate;
110*176f6ce7Stedu while (TS_POS(&ts))
111*176f6ce7Stedu if (rwait(&ts) && read(STDIN_FILENO, &c, 1) != 1)
112df930be7Sderaadt break;
113df930be7Sderaadt }
114df930be7Sderaadt
115df930be7Sderaadt /*
116df930be7Sderaadt * getchar with timeout.
117df930be7Sderaadt */
118df930be7Sderaadt int
tgetchar(void)119ff8320a7Sderaadt tgetchar(void)
120df930be7Sderaadt {
121*176f6ce7Stedu static struct timespec timeleft;
122df930be7Sderaadt char c;
123df930be7Sderaadt
124df930be7Sderaadt /*
125df930be7Sderaadt * Reset timeleft to fallrate whenever it is not positive.
126df930be7Sderaadt * In any case, wait to see if there is any input. If so,
127df930be7Sderaadt * take it, and update timeleft so that the next call to
128df930be7Sderaadt * tgetchar() will not wait as long. If there is no input,
129df930be7Sderaadt * make timeleft zero or negative, and return -1.
130df930be7Sderaadt *
131df930be7Sderaadt * Most of the hard work is done by rwait().
132df930be7Sderaadt */
133*176f6ce7Stedu if (!TS_POS(&timeleft)) {
134df930be7Sderaadt faster(); /* go faster */
135df930be7Sderaadt timeleft.tv_sec = 0;
136*176f6ce7Stedu timeleft.tv_nsec = fallrate;
137df930be7Sderaadt }
138df930be7Sderaadt if (!rwait(&timeleft))
139df930be7Sderaadt return (-1);
14054dea640Sderaadt if (read(STDIN_FILENO, &c, 1) != 1)
141df930be7Sderaadt stop("end of file, help");
142df930be7Sderaadt return ((int)(unsigned char)c);
143df930be7Sderaadt }
144