xref: /original-bsd/games/boggle/boggle/timer.c (revision e8eb2810)
1 /* vi: set tabstop=4 : */
2 
3 #include "bog.h"
4 
5 #ifdef TIMER
6 
7 #include <setjmp.h>
8 #include <curses.h>
9 #include <stdio.h>
10 
11 static int waitch();
12 
13 /*
14  * Update the display of the remaining time while waiting for a character
15  * If time runs out do a longjmp() to the game controlling routine, returning
16  * non-zero; oth. return the character
17  * Leave the cursor where it was initially
18  */
19 timerch()
20 {
21 	int col, remaining, row;
22 	long prevt, t;
23 	extern int tlimit;
24 	extern long start_t;
25 	extern jmp_buf env;
26 
27 	getyx(stdscr, row, col);
28 	prevt = 0L;
29 	while (1) {
30 		if (waitch(1000L) == 1)
31 			break;
32 		time(&t);
33 		if (t == prevt)
34 			continue;
35 		prevt = t;
36 		remaining = tlimit - (int) (t - start_t);
37 		if (remaining < 0) {
38 			longjmp(env, 1);
39 			/*NOTREACHED*/
40 		}
41 		move(TIMER_LINE, TIMER_COL);
42 		printw("%d:%02d", remaining / 60, remaining % 60);
43 		move(row, col);
44 		refresh();
45 	}
46 	return(getch() & 0177);
47 }
48 
49 /*
50  * Wait up to 'delay' microseconds for input to appear
51  * Returns 1 if input is ready, 0 oth.
52  */
53 
54 #ifdef BSD42
55 
56 #include <sys/time.h>
57 
58 static
59 waitch(delay)
60 long delay;
61 {
62 	int fdbits;
63 	struct timeval duration;
64 
65 	duration.tv_sec = 0L;
66 	duration.tv_usec = delay;
67 	fdbits = 1;
68 	return(select(32, &fdbits, 0, 0, &duration));
69 }
70 
71 delay(tenths)
72 int tenths;
73 {
74 	struct timeval duration;
75 
76 	duration.tv_usec = (tenths % 10 ) * 100000L;
77 	duration.tv_sec = (long) (tenths / 10);
78 	select(32, 0, 0, 0, &duration);
79 }
80 #endif BSD42
81 
82 #ifdef SYSV
83 
84 #include <sys/ioctl.h>
85 
86 /*
87  * This is not too efficient...
88  */
89 static
90 waitch(delay)
91 long delay;
92 {
93 	int nchars;
94 
95 	if (ioctl(fileno(stdin), FIONREAD, &nchars) < 0) {
96 		perror("ioctl():");
97 		cleanup();
98 		exit(1);
99 	}
100 	return(nchars > 0);
101 }
102 
103 /*
104  * Do nothing for the given number of tenths of a second
105  */
106 delay(tenths)
107 int tenths;
108 {
109 	int n;
110 
111 	n = tenths / 10;
112 	if (n == 0)
113 		n == 1;
114 	sleep(n);
115 }
116 
117 #endif SYSV
118 
119 #ifdef ATARI
120 
121 #include <osbind.h>
122 
123 /*
124  * The ST curses turns on the cursor only when a read is performed
125  * Since there's nothing better to do at this point the cursor can
126  * be enabled
127  */
128 static
129 waitch(delay)
130 long delay;
131 {
132 
133 	Bconout(2, '\033');
134 	Bconout(2, 'e');
135 	return(Cconis() == -1);
136 }
137 
138 /*
139  * Do nothing for the given number of tenths of a second
140  */
141 delay(tenths)
142 int tenths;
143 {
144 	int n;
145 
146 	n = tenths / 10;
147 	if (n == 0)
148 		n == 1;
149 	sleep(n);
150 }
151 
152 #endif ATARI
153 
154 #else !TIMER
155 
156 /*
157  * Do nothing for the given number of tenths of a second
158  */
159 delay(tenths)
160 int tenths;
161 {
162 	int n;
163 
164 	n = tenths / 10;
165 	if (n == 0)
166 		n == 1;
167 	sleep(n);
168 }
169 
170 #endif TIMER
171 
172