2 * Grand digital clock for curses compatible terminals
3 * Usage: gdc [-s] [n] -- run for n seconds (default infinity)
6 * modified 10-18-89 for curses (jrl)
7 * 10-18-89 added signal handling
9 * $Id: gdc.c,v 1.15 2000/09/02 18:40:39 tom Exp $
12 #include <test.priv.h>
23 static short disp[11] =
25 075557, 011111, 071747, 071717, 055711,
26 074717, 074757, 071111, 075757, 075717, 002020
28 static long older[6], next[6], newer[6], mask;
30 static int sigtermed = 0;
31 static bool redirected = FALSE;
32 static bool hascolor = FALSE;
37 signal(signo, sighndl);
48 chtype bottom[XLENGTH + 1];
52 attrset(COLOR_PAIR(3));
54 mvaddch(YBASE - 1, XBASE - 1, ACS_ULCORNER);
55 hline(ACS_HLINE, XLENGTH);
56 mvaddch(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
58 mvaddch(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
59 mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
60 for (n = 0; n < XLENGTH; n++)
61 bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
62 mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
63 mvaddch(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
65 move(YBASE, XBASE - 1);
66 vline(ACS_VLINE, YDEPTH);
68 move(YBASE, XBASE + XLENGTH);
69 vline(ACS_VLINE, YDEPTH);
72 attrset(COLOR_PAIR(2));
80 attron(COLOR_PAIR(1));
86 attron(COLOR_PAIR(2));
99 for (i = 0; i < 5; i++) {
100 next[i] |= ((disp[t] >> (4 - i) * 3) & 07) << n;
101 mask |= (next[i] ^ older[i]) & m;
110 static const char *msg[] =
112 "Usage: gdc [options] [count]"
115 ," -n redirect input to /dev/null"
116 ," -s scroll each number into place, rather than flipping"
118 ,"If you specify a count, gdc runs for that number of seconds"
121 for (j = 0; j < SIZEOF(msg); j++)
122 fprintf(stderr, "%s\n", msg[j]);
127 main(int argc, char *argv[])
138 signal(SIGINT, sighndl);
139 signal(SIGTERM, sighndl);
140 signal(SIGKILL, sighndl);
142 while ((k = getopt(argc, argv, "sn")) != EOF) {
148 ifp = fopen("/dev/null", "r");
156 count = atoi(argv[optind++]);
162 char *name = getenv("TERM");
164 || newterm(name, ofp, ifp) == 0) {
165 fprintf(stderr, "cannot open terminal\n");
177 hascolor = has_colors();
180 int bg = COLOR_BLACK;
182 #if HAVE_USE_DEFAULT_COLORS
183 if (use_default_colors() == OK)
186 init_pair(1, COLOR_BLACK, COLOR_RED);
187 init_pair(2, COLOR_RED, bg);
188 init_pair(3, COLOR_WHITE, bg);
189 attrset(COLOR_PAIR(2));
193 for (j = 0; j < 5; j++)
194 older[j] = newer[j] = next[j] = 0;
203 tm = localtime(&now);
206 set(tm->tm_sec % 10, 0);
207 set(tm->tm_sec / 10, 4);
208 set(tm->tm_min % 10, 10);
209 set(tm->tm_min / 10, 14);
210 set(tm->tm_hour % 10, 20);
211 set(tm->tm_hour / 10, 24);
215 for (k = 0; k < 6; k++) {
217 for (i = 0; i < 5; i++)
218 newer[i] = (newer[i] & ~mask) | (newer[i + 1] & mask);
219 newer[5] = (newer[5] & ~mask) | (next[k] & mask);
221 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
223 for (s = 1; s >= 0; s--) {
225 for (i = 0; i < 6; i++) {
226 if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
228 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
230 if (!(a & (t << 1))) {
231 move(YBASE + i, XBASE + 2 * j);
246 * If we're scrolling, space out the refreshes to fake
247 * movement. That's 7 frames, or 6 intervals, which would
248 * be 166 msec if we spread it out over a second. It looks
249 * better (but will well on a slow terminal, e.g., less
250 * than 9600bd) to squeeze that into a half-second, and use
251 * half of 170 msec to ensure that the program doesn't eat
252 * a lot of time when asking what time it is, at the top of
261 /* this depends on the detailed format of ctime(3) */
262 (void) strcpy(buf, ctime(&now));
263 (void) strcpy(buf + 10, buf + 19);
264 mvaddstr(16, 30, buf);
271 * If we're not scrolling, wait 1000 msec (1 sec). Use napms() rather
272 * than sleep() because the latter does odd things on some systems,
273 * e.g., suspending output as well.
281 * This is a safe way to check if we're interrupted - making the signal
282 * handler set a flag that we can check. Since we're running
283 * nodelay(), the wgetch() call returns immediately, and in particular
284 * will return an error if interrupted. This works only if we can
285 * read from the input, of course.
287 switch (wgetch(stdscr)) {
292 nodelay(stdscr, FALSE);
295 nodelay(stdscr, TRUE);
306 fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);