ifnet: Add comment for ringmap
[dragonfly.git] / games / grdc / grdc.c
blob2c9df85b7dd7c5457dc08417c01235eced745d09
1 /*
2 * Grand digital clock for curses compatible terminals
3 * Usage: grdc [-s] [-d msecs] [n] -- run for n seconds (default infinity)
4 * Flags: -s: scroll (default scroll duration 120msec)
5 * -d msecs: specify scroll duration (implies -s)
7 * modified 10-18-89 for curses (jrl)
8 * 10-18-89 added signal handling
9 * 03-23-04 added centering, scroll delay (cap)
11 * $FreeBSD: src/games/grdc/grdc.c,v 1.8.2.1 2001/10/02 11:51:49 ru Exp $
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
23 #define XLENGTH 58
24 #define YDEPTH 7
26 time_t now;
27 struct tm *tm;
29 short disp[11] = {
30 075557, 011111, 071747, 071717, 055711,
31 074717, 074757, 071111, 075757, 075717, 002020
33 long old[6], next[6], new[6], mask;
35 volatile sig_atomic_t sigtermed;
37 int hascolor = 0;
38 long int scroll_msecs = 120;
39 int xbase, ybase, xmax, ymax;
41 static void set(int, int);
42 static void standt(int);
43 static void sighndl(int);
44 static void usage(void);
45 static void draw_row(int, int);
46 static void snooze(long int);
48 void
49 sighndl(int signo)
51 sigtermed = signo;
54 int
55 main(int argc, char **argv)
57 int i, s, k;
58 int n;
59 int ch;
60 int scrol;
61 int forever;
63 n = scrol = 0;
64 forever = 1;
66 while ((ch = getopt(argc, argv, "d:s")) != -1)
67 switch (ch) {
68 case 'd':
69 scroll_msecs = atol(optarg);
70 if (scroll_msecs < 0)
71 errx(1, "scroll duration may not be negative");
72 /* FALLTHROUGH */
73 case 's':
74 scrol = 1;
75 break;
76 case '?':
77 default:
78 usage();
79 /* NOTREACHED */
81 argc -= optind;
82 argv += optind;
84 if (argc > 1) {
85 usage();
86 /* NOTREACHED */
89 if (argc > 0) {
90 n = atoi(*argv);
91 forever = 0;
94 initscr();
96 getmaxyx(stdscr, ymax, xmax);
97 if (ymax < YDEPTH + 2 || xmax < XLENGTH + 4) {
98 endwin();
99 errx(1, "terminal too small");
101 xbase = (xmax - XLENGTH) / 2 + 2;
102 ybase = (ymax - YDEPTH) / 2 + 1;
104 signal(SIGINT, sighndl);
105 signal(SIGTERM, sighndl);
106 signal(SIGHUP, sighndl);
108 cbreak();
109 noecho();
110 curs_set(0);
112 hascolor = has_colors();
114 if (hascolor) {
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));
122 clear();
123 refresh();
125 if (hascolor) {
126 attrset(COLOR_PAIR(3));
128 mvaddch(ybase - 2, xbase - 3, ACS_ULCORNER);
129 hline(ACS_HLINE, XLENGTH);
130 mvaddch(ybase - 2, xbase - 2 + XLENGTH, ACS_URCORNER);
132 mvaddch(ybase + YDEPTH - 1, xbase - 3, ACS_LLCORNER);
133 hline(ACS_HLINE, XLENGTH);
134 mvaddch(ybase + YDEPTH - 1, xbase - 2 + XLENGTH, ACS_LRCORNER);
136 move(ybase - 1, xbase - 3);
137 vline(ACS_VLINE, YDEPTH);
139 move(ybase - 1, xbase - 2 + XLENGTH);
140 vline(ACS_VLINE, YDEPTH);
142 attrset(COLOR_PAIR(2));
143 refresh();
145 do {
146 mask = 0;
147 time(&now);
148 tm = localtime(&now);
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);
155 set(10, 7);
156 set(10, 17);
157 for(k = 0; k < 6; k++) {
158 if (scrol) {
159 snooze(scroll_msecs / 6);
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);
164 } else
165 new[k] = (new[k] & ~mask) | (next[k] & mask);
166 next[k] = 0;
167 for (s = 1; s >= 0; s--) {
168 standt(s);
169 for (i = 0; i < 6; i++) {
170 draw_row(i, s);
172 if (!s) {
173 move(ybase, 0);
174 refresh();
178 move(ybase, 0);
179 refresh();
180 snooze(1000 - (scrol ? scroll_msecs : 0));
181 } while (forever ? 1 : --n);
182 standend();
183 clear();
184 refresh();
185 endwin();
186 return(0);
189 void
190 snooze(long int msecs)
192 struct timespec ts;
194 ts.tv_sec = 0;
195 ts.tv_nsec = 1000000 * msecs;
197 nanosleep(&ts, NULL);
199 if (sigtermed) {
200 standend();
201 clear();
202 refresh();
203 endwin();
204 errx(1, "terminated by signal %d", (int)sigtermed);
208 void
209 draw_row(int i, int s)
211 long a, t;
212 int j;
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))) {
218 move(ybase + i, xbase + 2 * j);
220 addstr(" ");
224 if (!s) {
225 old[i] = new[i];
229 void
230 set(int t, int n)
232 int i, m;
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;
239 if (mask & m)
240 mask |= m;
243 void
244 standt(int on)
246 if (on) {
247 if (hascolor) {
248 attron(COLOR_PAIR(1));
249 } else {
250 attron(A_STANDOUT);
252 } else {
253 if (hascolor) {
254 attron(COLOR_PAIR(2));
255 } else {
256 attroff(A_STANDOUT);
261 void
262 usage(void)
264 fprintf(stderr, "usage: grdc [-s] [-d msecs] [n]\n");
265 exit(1);