xref: /dragonfly/games/boggle/boggle/timer.c (revision 11da14fe)
1*11da14feSzrj /*	$OpenBSD: timer.c,v 1.15 2016/08/27 02:11:27 guenther Exp $	*/
2*11da14feSzrj /*	$NetBSD: timer.c,v 1.3 1995/04/24 12:22:45 cgd Exp $	*/
3*11da14feSzrj 
4*11da14feSzrj /*-
5*11da14feSzrj  * Copyright (c) 1993
6*11da14feSzrj  *	The Regents of the University of California.  All rights reserved.
7*11da14feSzrj  *
8*11da14feSzrj  * This code is derived from software contributed to Berkeley by
9*11da14feSzrj  * Barry Brachman.
10*11da14feSzrj  *
11*11da14feSzrj  * Redistribution and use in source and binary forms, with or without
12*11da14feSzrj  * modification, are permitted provided that the following conditions
13*11da14feSzrj  * are met:
14*11da14feSzrj  * 1. Redistributions of source code must retain the above copyright
15*11da14feSzrj  *    notice, this list of conditions and the following disclaimer.
16*11da14feSzrj  * 2. Redistributions in binary form must reproduce the above copyright
17*11da14feSzrj  *    notice, this list of conditions and the following disclaimer in the
18*11da14feSzrj  *    documentation and/or other materials provided with the distribution.
19*11da14feSzrj  * 3. Neither the name of the University nor the names of its contributors
20*11da14feSzrj  *    may be used to endorse or promote products derived from this software
21*11da14feSzrj  *    without specific prior written permission.
22*11da14feSzrj  *
23*11da14feSzrj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*11da14feSzrj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*11da14feSzrj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*11da14feSzrj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*11da14feSzrj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*11da14feSzrj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*11da14feSzrj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*11da14feSzrj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*11da14feSzrj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*11da14feSzrj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*11da14feSzrj  * SUCH DAMAGE.
34*11da14feSzrj  */
35*11da14feSzrj 
36*11da14feSzrj #include <sys/select.h>
37*11da14feSzrj #include <curses.h>
38*11da14feSzrj #include <setjmp.h>
39*11da14feSzrj #include <time.h>
40*11da14feSzrj #include <unistd.h>
41*11da14feSzrj 
42*11da14feSzrj #include "extern.h"
43*11da14feSzrj 
44*11da14feSzrj static int waitch(long);
45*11da14feSzrj 
46*11da14feSzrj /*
47*11da14feSzrj  * Update the display of the remaining time while waiting for a character
48*11da14feSzrj  * If time runs out do a longjmp() to the game controlling routine, returning
49*11da14feSzrj  * non-zero; oth. return the character
50*11da14feSzrj  * Leave the cursor where it was initially
51*11da14feSzrj  */
52*11da14feSzrj int
timerch(void)53*11da14feSzrj timerch(void)
54*11da14feSzrj {
55*11da14feSzrj 	time_t prevt, t;
56*11da14feSzrj 	int col, remaining, row;
57*11da14feSzrj 
58*11da14feSzrj 	getyx(stdscr, row, col);
59*11da14feSzrj 	prevt = 0L;
60*11da14feSzrj 	for (;;) {
61*11da14feSzrj 		if (waitch(1000L) == 1)
62*11da14feSzrj 			break;
63*11da14feSzrj 		time(&t);
64*11da14feSzrj 		if (t == prevt)
65*11da14feSzrj 			continue;
66*11da14feSzrj 		prevt = t;
67*11da14feSzrj 		remaining = tlimit - (int) (t - start_t);
68*11da14feSzrj 		if (remaining < 0) {
69*11da14feSzrj 			longjmp(env, 1);
70*11da14feSzrj 		}
71*11da14feSzrj 		move(TIMER_LINE, TIMER_COL);
72*11da14feSzrj 		printw("%d:%02d", remaining / 60, remaining % 60);
73*11da14feSzrj 		move(row, col);
74*11da14feSzrj 		refresh();
75*11da14feSzrj 	}
76*11da14feSzrj 	return (inputch());
77*11da14feSzrj }
78*11da14feSzrj 
79*11da14feSzrj /*
80*11da14feSzrj  * Wait up to 'delay' microseconds for input to appear
81*11da14feSzrj  * Returns 1 if input is ready, 0 oth.
82*11da14feSzrj  */
83*11da14feSzrj static int
waitch(long delay)84*11da14feSzrj waitch(long delay)
85*11da14feSzrj {
86*11da14feSzrj 	fd_set fdbits;
87*11da14feSzrj 	struct timeval duration;
88*11da14feSzrj 
89*11da14feSzrj 	duration.tv_sec = 0;
90*11da14feSzrj 	duration.tv_usec = delay;
91*11da14feSzrj 	FD_ZERO(&fdbits);
92*11da14feSzrj 	FD_SET(STDIN_FILENO, &fdbits);
93*11da14feSzrj 	return (select(STDIN_FILENO+1, &fdbits, NULL, NULL, &duration));
94*11da14feSzrj }
95*11da14feSzrj 
96*11da14feSzrj void
delay(int tenths)97*11da14feSzrj delay(int tenths)
98*11da14feSzrj {
99*11da14feSzrj 	struct timeval duration;
100*11da14feSzrj 
101*11da14feSzrj 	duration.tv_usec = (tenths % 10 ) * 100000L;
102*11da14feSzrj 	duration.tv_sec = tenths / 10;
103*11da14feSzrj 	select(0, 0, 0, 0, &duration);
104*11da14feSzrj }
105