build - Significantly improve parallel buildworld times
[dragonfly.git] / games / grdc / grdc.c
CommitLineData
984263bc
MD
1/*
2 * Grand digital clock for curses compatible terminals
180ac9b3
CP
3 * Usage: grdc [-s] [-d msecs] [n] -- run for n seconds (default infinity)
4 * Flags: -s: scroll (default scroll duration 120msec)
6693db17 5 * -d msecs: specify scroll duration (implies -s)
984263bc
MD
6 *
7 * modified 10-18-89 for curses (jrl)
8 * 10-18-89 added signal handling
180ac9b3 9 * 03-23-04 added centering, scroll delay (cap)
984263bc
MD
10 *
11 * $FreeBSD: src/games/grdc/grdc.c,v 1.8.2.1 2001/10/02 11:51:49 ru Exp $
12 */
13
14#include <err.h>
15#include <time.h>
16#include <signal.h>
17#include <ncurses.h>
18#include <stdlib.h>
19#ifndef NONPOSIX
20#include <unistd.h>
21#endif
22
984263bc
MD
23#define XLENGTH 58
24#define YDEPTH 7
25
9aeb24d3 26time_t now;
984263bc
MD
27struct tm *tm;
28
29short disp[11] = {
30 075557, 011111, 071747, 071717, 055711,
31 074717, 074757, 071111, 075757, 075717, 002020
32};
33long old[6], next[6], new[6], mask;
34
35volatile sig_atomic_t sigtermed;
36
37int hascolor = 0;
180ac9b3
CP
38long int scroll_msecs = 120;
39int xbase, ybase, xmax, ymax;
984263bc 40
9aeb24d3
CP
41static void set(int, int);
42static void standt(int);
43static void sighndl(int);
44static void usage(void);
45static void draw_row(int, int);
180ac9b3 46static void snooze(long int);
984263bc 47
c11b8fa9
SW
48void
49sighndl(int signo)
984263bc 50{
9aeb24d3 51 sigtermed = signo;
984263bc
MD
52}
53
54int
9aeb24d3 55main(int argc, char **argv)
984263bc 56{
9aeb24d3 57 int i, s, k;
6693db17 58 int n;
9aeb24d3
CP
59 int ch;
60 int scrol;
6693db17 61 int forever;
984263bc 62
6693db17
SW
63 n = scrol = 0;
64 forever = 1;
984263bc 65
180ac9b3 66 while ((ch = getopt(argc, argv, "d:s")) != -1)
9aeb24d3 67 switch (ch) {
180ac9b3
CP
68 case 'd':
69 scroll_msecs = atol(optarg);
70 if (scroll_msecs < 0)
71 errx(1, "scroll duration may not be negative");
72 /* FALLTHROUGH */
9aeb24d3
CP
73 case 's':
74 scrol = 1;
75 break;
76 case '?':
77 default:
78 usage();
79 /* NOTREACHED */
80 }
984263bc
MD
81 argc -= optind;
82 argv += optind;
83
84 if (argc > 1) {
85 usage();
86 /* NOTREACHED */
87 }
88
180ac9b3 89 if (argc > 0) {
984263bc 90 n = atoi(*argv);
180ac9b3
CP
91 forever = 0;
92 }
984263bc
MD
93
94 initscr();
95
180ac9b3
CP
96 getmaxyx(stdscr, ymax, xmax);
97 if (ymax < YDEPTH + 2 || xmax < XLENGTH + 4) {
98 endwin();
99 errx(1, "terminal too small");
100 }
101 xbase = (xmax - XLENGTH) / 2 + 2;
102 ybase = (ymax - YDEPTH) / 2 + 1;
103
9aeb24d3
CP
104 signal(SIGINT, sighndl);
105 signal(SIGTERM, sighndl);
106 signal(SIGHUP, sighndl);
984263bc
MD
107
108 cbreak();
109 noecho();
110 curs_set(0);
111
112 hascolor = has_colors();
113
9aeb24d3 114 if (hascolor) {
984263bc
MD
115 start_color();
116 init_pair(1, COLOR_BLACK, COLOR_RED);
117 init_pair(2, COLOR_RED, COLOR_BLACK);
118 init_pair(3, COLOR_WHITE, COLOR_BLACK);
119 attrset(COLOR_PAIR(2));
120 }
121
122 clear();
123 refresh();
124
9aeb24d3 125 if (hascolor) {
984263bc
MD
126 attrset(COLOR_PAIR(3));
127
180ac9b3 128 mvaddch(ybase - 2, xbase - 3, ACS_ULCORNER);
984263bc 129 hline(ACS_HLINE, XLENGTH);
180ac9b3 130 mvaddch(ybase - 2, xbase - 2 + XLENGTH, ACS_URCORNER);
984263bc 131
180ac9b3 132 mvaddch(ybase + YDEPTH - 1, xbase - 3, ACS_LLCORNER);
984263bc 133 hline(ACS_HLINE, XLENGTH);
180ac9b3 134 mvaddch(ybase + YDEPTH - 1, xbase - 2 + XLENGTH, ACS_LRCORNER);
984263bc 135
180ac9b3 136 move(ybase - 1, xbase - 3);
984263bc
MD
137 vline(ACS_VLINE, YDEPTH);
138
180ac9b3 139 move(ybase - 1, xbase - 2 + XLENGTH);
984263bc
MD
140 vline(ACS_VLINE, YDEPTH);
141
142 attrset(COLOR_PAIR(2));
180ac9b3 143 refresh();
984263bc
MD
144 }
145 do {
146 mask = 0;
147 time(&now);
148 tm = localtime(&now);
9aeb24d3
CP
149 set(tm->tm_sec % 10, 0);
150 set(tm->tm_sec / 10, 4);
151 set(tm->tm_min % 10, 10);
152 set(tm->tm_min / 10, 14);
153 set(tm->tm_hour % 10, 20);
154 set(tm->tm_hour / 10, 24);
984263bc
MD
155 set(10, 7);
156 set(10, 17);
9aeb24d3
CP
157 for(k = 0; k < 6; k++) {
158 if (scrol) {
180ac9b3 159 snooze(scroll_msecs / 6);
9aeb24d3
CP
160 for(i = 0; i < 5; i++)
161 new[i] = (new[i] & ~mask) |
162 (new[i+1] & mask);
163 new[5] = (new[5] & ~mask) | (next[k] & mask);
984263bc 164 } else
9aeb24d3 165 new[k] = (new[k] & ~mask) | (next[k] & mask);
984263bc 166 next[k] = 0;
9aeb24d3 167 for (s = 1; s >= 0; s--) {
984263bc 168 standt(s);
9aeb24d3
CP
169 for (i = 0; i < 6; i++) {
170 draw_row(i, s);
984263bc 171 }
9aeb24d3 172 if (!s) {
180ac9b3 173 move(ybase, 0);
984263bc
MD
174 refresh();
175 }
176 }
177 }
180ac9b3 178 move(ybase, 0);
984263bc 179 refresh();
180ac9b3
CP
180 snooze(1000 - (scrol ? scroll_msecs : 0));
181 } while (forever ? 1 : --n);
984263bc
MD
182 standend();
183 clear();
184 refresh();
185 endwin();
186 return(0);
187}
188
180ac9b3
CP
189void
190snooze(long int msecs)
191{
192 struct timespec ts;
6693db17 193
180ac9b3
CP
194 ts.tv_sec = 0;
195 ts.tv_nsec = 1000000 * msecs;
196
197 nanosleep(&ts, NULL);
198
199 if (sigtermed) {
200 standend();
201 clear();
202 refresh();
203 endwin();
204 errx(1, "terminated by signal %d", (int)sigtermed);
205 }
206}
207
9aeb24d3
CP
208void
209draw_row(int i, int s)
210{
211 long a, t;
212 int j;
213
214 if ((a = (new[i] ^ old[i]) & (s ? new : old)[i]) != 0) {
215 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
216 if (a & t) {
217 if (!(a & (t << 1))) {
180ac9b3 218 move(ybase + i, xbase + 2 * j);
9aeb24d3
CP
219 }
220 addstr(" ");
221 }
222 }
223 }
224 if (!s) {
225 old[i] = new[i];
226 }
227}
228
984263bc
MD
229void
230set(int t, int n)
231{
9aeb24d3 232 int i, m;
984263bc 233
9aeb24d3
CP
234 m = 7 << n;
235 for (i = 0; i < 5; i++) {
236 next[i] |= ((disp[t] >> (4 - i) * 3) & 07) << n;
237 mask |= (next[i] ^ old[i]) & m;
984263bc 238 }
9aeb24d3 239 if (mask & m)
984263bc
MD
240 mask |= m;
241}
242
243void
244standt(int on)
245{
246 if (on) {
9aeb24d3 247 if (hascolor) {
984263bc
MD
248 attron(COLOR_PAIR(1));
249 } else {
250 attron(A_STANDOUT);
251 }
252 } else {
9aeb24d3 253 if (hascolor) {
984263bc
MD
254 attron(COLOR_PAIR(2));
255 } else {
256 attroff(A_STANDOUT);
257 }
258 }
259}
260
984263bc
MD
261void
262usage(void)
263{
180ac9b3 264 fprintf(stderr, "usage: grdc [-s] [-d msecs] [n]\n");
984263bc
MD
265 exit(1);
266}