xref: /original-bsd/games/boggle/boggle/timer.c (revision f737e041)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Barry Brachman.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)timer.c	8.2 (Berkeley) 02/22/94";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/time.h>
17 
18 #include <curses.h>
19 #include <setjmp.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 
23 #include "bog.h"
24 #include "extern.h"
25 
26 static int waitch __P((long));
27 
28 /*
29  * Update the display of the remaining time while waiting for a character
30  * If time runs out do a longjmp() to the game controlling routine, returning
31  * non-zero; oth. return the character
32  * Leave the cursor where it was initially
33  */
34 int
35 timerch()
36 {
37 	extern int tlimit;
38 	extern long start_t;
39 	extern jmp_buf env;
40 	long prevt, t;
41 	int col, remaining, row;
42 
43 	getyx(stdscr, row, col);
44 	prevt = 0L;
45 	for (;;) {
46 		if (waitch(1000L) == 1)
47 			break;
48 		time(&t);
49 		if (t == prevt)
50 			continue;
51 		prevt = t;
52 		remaining = tlimit - (int) (t - start_t);
53 		if (remaining < 0) {
54 			longjmp(env, 1);
55 			/*NOTREACHED*/
56 		}
57 		move(TIMER_LINE, TIMER_COL);
58 		printw("%d:%02d", remaining / 60, remaining % 60);
59 		move(row, col);
60 		refresh();
61 	}
62 	return (getch() & 0177);
63 }
64 
65 /*
66  * Wait up to 'delay' microseconds for input to appear
67  * Returns 1 if input is ready, 0 oth.
68  */
69 static int
70 waitch(delay)
71 	long delay;
72 {
73 	fd_set fdbits;
74 	struct timeval duration;
75 
76 	duration.tv_sec = 0;
77 	duration.tv_usec = delay;
78 	FD_ZERO(&fdbits);
79 	FD_SET(STDIN_FILENO, &fdbits);
80 	return (select(32, &fdbits, NULL, NULL, &duration));
81 }
82 
83 void
84 delay(tenths)
85 	int tenths;
86 {
87 	struct timeval duration;
88 
89 	duration.tv_usec = (tenths % 10 ) * 100000L;
90 	duration.tv_sec = (long) (tenths / 10);
91 	select(32, 0, 0, 0, &duration);
92 }
93