HAMMER VFS - REDO implementation base code part 3/many (addendum)
[dragonfly.git] / games / grdc / grdc.c
blobc3571c02cb569b9171d81119839e573fdce2a4f0
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 $
12 * $DragonFly: src/games/grdc/grdc.c,v 1.6 2007/04/18 18:32:12 swildner Exp $
15 #include <err.h>
16 #include <time.h>
17 #include <signal.h>
18 #include <ncurses.h>
19 #include <stdlib.h>
20 #ifndef NONPOSIX
21 #include <unistd.h>
22 #endif
23 #include <time.h>
25 #define XLENGTH 58
26 #define YDEPTH 7
28 time_t now;
29 struct tm *tm;
31 short disp[11] = {
32 075557, 011111, 071747, 071717, 055711,
33 074717, 074757, 071111, 075757, 075717, 002020
35 long old[6], next[6], new[6], mask;
37 volatile sig_atomic_t sigtermed;
39 int hascolor = 0;
40 long int scroll_msecs = 120;
41 int xbase, ybase, xmax, ymax;
43 static void set(int, int);
44 static void standt(int);
45 static void sighndl(int);
46 static void usage(void);
47 static void draw_row(int, int);
48 static void snooze(long int);
50 void
51 sighndl(int signo)
53 sigtermed = signo;
56 int
57 main(int argc, char **argv)
59 int i, s, k;
60 int n;
61 int ch;
62 int scrol;
63 int forever;
65 n = scrol = 0;
66 forever = 1;
68 while ((ch = getopt(argc, argv, "d:s")) != -1)
69 switch (ch) {
70 case 'd':
71 scroll_msecs = atol(optarg);
72 if (scroll_msecs < 0)
73 errx(1, "scroll duration may not be negative");
74 /* FALLTHROUGH */
75 case 's':
76 scrol = 1;
77 break;
78 case '?':
79 default:
80 usage();
81 /* NOTREACHED */
83 argc -= optind;
84 argv += optind;
86 if (argc > 1) {
87 usage();
88 /* NOTREACHED */
91 if (argc > 0) {
92 n = atoi(*argv);
93 forever = 0;
96 initscr();
98 getmaxyx(stdscr, ymax, xmax);
99 if (ymax < YDEPTH + 2 || xmax < XLENGTH + 4) {
100 endwin();
101 errx(1, "terminal too small");
103 xbase = (xmax - XLENGTH) / 2 + 2;
104 ybase = (ymax - YDEPTH) / 2 + 1;
106 signal(SIGINT, sighndl);
107 signal(SIGTERM, sighndl);
108 signal(SIGHUP, sighndl);
110 cbreak();
111 noecho();
112 curs_set(0);
114 hascolor = has_colors();
116 if (hascolor) {
117 start_color();
118 init_pair(1, COLOR_BLACK, COLOR_RED);
119 init_pair(2, COLOR_RED, COLOR_BLACK);
120 init_pair(3, COLOR_WHITE, COLOR_BLACK);
121 attrset(COLOR_PAIR(2));
124 clear();
125 refresh();
127 if (hascolor) {
128 attrset(COLOR_PAIR(3));
130 mvaddch(ybase - 2, xbase - 3, ACS_ULCORNER);
131 hline(ACS_HLINE, XLENGTH);
132 mvaddch(ybase - 2, xbase - 2 + XLENGTH, ACS_URCORNER);
134 mvaddch(ybase + YDEPTH - 1, xbase - 3, ACS_LLCORNER);
135 hline(ACS_HLINE, XLENGTH);
136 mvaddch(ybase + YDEPTH - 1, xbase - 2 + XLENGTH, ACS_LRCORNER);
138 move(ybase - 1, xbase - 3);
139 vline(ACS_VLINE, YDEPTH);
141 move(ybase - 1, xbase - 2 + XLENGTH);
142 vline(ACS_VLINE, YDEPTH);
144 attrset(COLOR_PAIR(2));
145 refresh();
147 do {
148 mask = 0;
149 time(&now);
150 tm = localtime(&now);
151 set(tm->tm_sec % 10, 0);
152 set(tm->tm_sec / 10, 4);
153 set(tm->tm_min % 10, 10);
154 set(tm->tm_min / 10, 14);
155 set(tm->tm_hour % 10, 20);
156 set(tm->tm_hour / 10, 24);
157 set(10, 7);
158 set(10, 17);
159 for(k = 0; k < 6; k++) {
160 if (scrol) {
161 snooze(scroll_msecs / 6);
162 for(i = 0; i < 5; i++)
163 new[i] = (new[i] & ~mask) |
164 (new[i+1] & mask);
165 new[5] = (new[5] & ~mask) | (next[k] & mask);
166 } else
167 new[k] = (new[k] & ~mask) | (next[k] & mask);
168 next[k] = 0;
169 for (s = 1; s >= 0; s--) {
170 standt(s);
171 for (i = 0; i < 6; i++) {
172 draw_row(i, s);
174 if (!s) {
175 move(ybase, 0);
176 refresh();
180 move(ybase, 0);
181 refresh();
182 snooze(1000 - (scrol ? scroll_msecs : 0));
183 } while (forever ? 1 : --n);
184 standend();
185 clear();
186 refresh();
187 endwin();
188 return(0);
191 void
192 snooze(long int msecs)
194 struct timespec ts;
196 ts.tv_sec = 0;
197 ts.tv_nsec = 1000000 * msecs;
199 nanosleep(&ts, NULL);
201 if (sigtermed) {
202 standend();
203 clear();
204 refresh();
205 endwin();
206 errx(1, "terminated by signal %d", (int)sigtermed);
210 void
211 draw_row(int i, int s)
213 long a, t;
214 int j;
216 if ((a = (new[i] ^ old[i]) & (s ? new : old)[i]) != 0) {
217 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
218 if (a & t) {
219 if (!(a & (t << 1))) {
220 move(ybase + i, xbase + 2 * j);
222 addstr(" ");
226 if (!s) {
227 old[i] = new[i];
231 void
232 set(int t, int n)
234 int i, m;
236 m = 7 << n;
237 for (i = 0; i < 5; i++) {
238 next[i] |= ((disp[t] >> (4 - i) * 3) & 07) << n;
239 mask |= (next[i] ^ old[i]) & m;
241 if (mask & m)
242 mask |= m;
245 void
246 standt(int on)
248 if (on) {
249 if (hascolor) {
250 attron(COLOR_PAIR(1));
251 } else {
252 attron(A_STANDOUT);
254 } else {
255 if (hascolor) {
256 attron(COLOR_PAIR(2));
257 } else {
258 attroff(A_STANDOUT);
263 void
264 usage(void)
266 fprintf(stderr, "usage: grdc [-s] [-d msecs] [n]\n");
267 exit(1);