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.26 2005/05/28 21:39:39 tom Exp $
12 #include <test.priv.h>
25 static short disp[11] =
27 075557, 011111, 071747, 071717, 055711,
28 074717, 074757, 071111, 075757, 075717, 002020
30 static long older[6], next[6], newer[6], mask;
32 static int sigtermed = 0;
33 static bool redirected = FALSE;
34 static bool hascolor = FALSE;
39 signal(signo, sighndl);
43 ExitProgram(EXIT_FAILURE);
48 drawbox(bool scrolling)
50 chtype bottom[XLENGTH + 1];
54 attrset(COLOR_PAIR(PAIR_FRAMES));
56 mvaddch(YBASE - 1, XBASE - 1, ACS_ULCORNER);
57 hline(ACS_HLINE, XLENGTH);
58 mvaddch(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
60 mvaddch(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
61 mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
62 for (n = 0; n < XLENGTH; n++) {
64 bottom[n] &= ~A_COLOR;
65 bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
67 mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
68 mvaddch(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
70 move(YBASE, XBASE - 1);
71 vline(ACS_VLINE, YDEPTH);
73 move(YBASE, XBASE + XLENGTH);
74 vline(ACS_VLINE, YDEPTH);
77 attrset(COLOR_PAIR(PAIR_OTHERS));
85 attron(COLOR_PAIR(PAIR_DIGITS));
91 attron(COLOR_PAIR(PAIR_OTHERS));
104 for (i = 0; i < 5; i++) {
105 next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
106 mask |= (next[i] ^ older[i]) & m;
115 static const char *msg[] =
117 "Usage: gdc [options] [count]"
120 ," -n redirect input to /dev/null"
121 ," -s scroll each number into place, rather than flipping"
123 ,"If you specify a count, gdc runs for that number of seconds"
126 for (j = 0; j < SIZEOF(msg); j++)
127 fprintf(stderr, "%s\n", msg[j]);
128 ExitProgram(EXIT_FAILURE);
132 main(int argc, char *argv[])
143 setlocale(LC_ALL, "");
145 signal(SIGINT, sighndl);
146 signal(SIGTERM, sighndl);
148 while ((k = getopt(argc, argv, "sn")) != EOF) {
154 ifp = fopen("/dev/null", "r");
162 count = atoi(argv[optind++]);
168 char *name = getenv("TERM");
170 || newterm(name, ofp, ifp) == 0) {
171 fprintf(stderr, "cannot open terminal\n");
172 ExitProgram(EXIT_FAILURE);
183 hascolor = has_colors();
186 int bg = COLOR_BLACK;
188 #if HAVE_USE_DEFAULT_COLORS
189 if (use_default_colors() == OK)
192 init_pair(PAIR_DIGITS, COLOR_BLACK, COLOR_RED);
193 init_pair(PAIR_OTHERS, COLOR_RED, bg);
194 init_pair(PAIR_FRAMES, COLOR_WHITE, bg);
195 attrset(COLOR_PAIR(PAIR_OTHERS));
199 for (j = 0; j < 5; j++)
200 older[j] = newer[j] = next[j] = 0;
209 tm = localtime(&now);
212 set(tm->tm_sec % 10, 0);
213 set(tm->tm_sec / 10, 4);
214 set(tm->tm_min % 10, 10);
215 set(tm->tm_min / 10, 14);
216 set(tm->tm_hour % 10, 20);
217 set(tm->tm_hour / 10, 24);
221 for (k = 0; k < 6; k++) {
223 for (i = 0; i < 5; i++)
224 newer[i] = (newer[i] & ~mask) | (newer[i + 1] & mask);
225 newer[5] = (newer[5] & ~mask) | (next[k] & mask);
227 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
229 for (s = 1; s >= 0; s--) {
231 for (i = 0; i < 6; i++) {
232 if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
234 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
236 if (!(a & (t << 1))) {
237 move(YBASE + i, XBASE + 2 * j);
252 * If we're scrolling, space out the refreshes to fake
253 * movement. That's 7 frames, or 6 intervals, which would
254 * be 166 msec if we spread it out over a second. It looks
255 * better (but will work on a slow terminal, e.g., less
256 * than 9600bd) to squeeze that into a half-second, and use
257 * half of 170 msec to ensure that the program doesn't eat
258 * a lot of time when asking what time it is, at the top of
259 * this loop -T.Dickey
267 /* this depends on the detailed format of ctime(3) */
268 (void) strcpy(buf, ctime(&now));
269 (void) strcpy(buf + 10, buf + 19);
270 mvaddstr(16, 30, buf);
277 * If we're not scrolling, wait 1000 msec (1 sec). Use napms() rather
278 * than sleep() because the latter does odd things on some systems,
279 * e.g., suspending output as well.
287 * This is a safe way to check if we're interrupted - making the signal
288 * handler set a flag that we can check. Since we're running
289 * nodelay(), the wgetch() call returns immediately, and in particular
290 * will return an error if interrupted. This works only if we can
291 * read from the input, of course.
293 switch (wgetch(stdscr)) {
298 nodelay(stdscr, FALSE);
301 nodelay(stdscr, TRUE);
312 fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
313 ExitProgram(EXIT_FAILURE);
322 ExitProgram(EXIT_SUCCESS);