1 /****************************************************************************
2 * Copyright 2019-2022,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * Grand digital clock for curses compatible terminals
31 * Usage: gdc [-s] [-t hh:mm:ss] [n] -- run for n seconds (default infinity)
34 * modified 10-18-89 for curses (jrl)
35 * 10-18-89 added signal handling
37 * $Id: gdc.c,v 1.58 2024/06/22 22:27:52 tom Exp $
40 #include <test.priv.h>
53 static short disp[11] =
55 075557, 011111, 071747, 071717, 055711,
56 074717, 074757, 071111, 075757, 075717, 002020
58 static long older[6], next[6], newer[6], mask;
60 static int sigtermed = 0;
61 static bool redirected = FALSE;
62 static bool hascolor = FALSE;
63 static bool hascustomtime = FALSE;
68 signal(signo, sighndl);
72 ExitProgram(EXIT_FAILURE);
82 fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
83 ExitProgram(EXIT_FAILURE);
88 drawbox(bool scrolling)
90 chtype bottom[XLENGTH + 1];
93 (void) attrset(AttrArg(COLOR_PAIR(PAIR_FRAMES), 0));
95 MvAddCh(YBASE - 1, XBASE - 1, ACS_ULCORNER);
96 hline(ACS_HLINE, XLENGTH);
97 MvAddCh(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
99 MvAddCh(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
100 if ((mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH)) != ERR) {
102 for (n = 0; n < XLENGTH; n++) {
104 bottom[n] &= ~A_COLOR;
105 bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
107 (void) mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
109 MvAddCh(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
111 move(YBASE, XBASE - 1);
112 vline(ACS_VLINE, YDEPTH);
114 move(YBASE, XBASE + XLENGTH);
115 vline(ACS_VLINE, YDEPTH);
118 (void) attrset(AttrArg(COLOR_PAIR(PAIR_OTHERS), 0));
126 attron(COLOR_PAIR(PAIR_DIGITS));
132 attron(COLOR_PAIR(PAIR_OTHERS));
145 for (i = 0; i < 5; i++) {
146 next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
147 mask |= (next[i] ^ older[i]) & m;
156 static const char *msg[] =
158 "usage: gdc [-dns] -[t HH:MM:SS] [COUNT]"
160 ,"Display a digital clock, running indefinitely or for COUNT"
165 #if HAVE_USE_DEFAULT_COLORS
166 ," -d uses the terminal's default background color"
168 ," -n reads input from /dev/null"
169 ," -s scrolls each digit into place"
170 ," -t HH:MM:SS starts clock at specified time"
173 for (j = 0; j < SIZEOF(msg); j++)
174 fprintf(stderr, "%s\n", msg[j]);
175 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
179 parse_time(const char *value)
187 if (sscanf(value, "%d:%d:%d%c", &hh, &mm, &ss, &c) != 3) {
188 if (sscanf(value, "%02d%02d%02d%c", &hh, &mm, &ss, &c) != 3) {
193 if ((hh < 0) || (hh >= 24) ||
194 (mm < 0) || (mm >= 60) ||
195 (ss < 0) || (ss >= 60)) {
199 /* adjust so that the localtime in the main loop will give usable time */
200 result = (hh * 3600) + ((mm * 60) + ss);
201 for (check = 0; check < 24; ++check) {
202 tm = localtime(&result);
203 if (tm->tm_hour == hh)
208 if (tm->tm_hour != hh) {
209 fprintf(stderr, "Cannot find local time for %s!\n", value);
219 main(int argc, char *argv[])
231 #if HAVE_USE_DEFAULT_COLORS
232 bool d_option = FALSE;
235 setlocale(LC_ALL, "");
237 while ((ch = getopt(argc, argv, OPTS_COMMON "dnst:")) != -1) {
239 #if HAVE_USE_DEFAULT_COLORS
245 ifp = fopen("/dev/null", "r");
252 hascustomtime = TRUE;
253 starts = parse_time(optarg);
257 ExitProgram(EXIT_SUCCESS);
259 usage(ch == OPTS_USAGE);
264 count = atoi(argv[optind++]);
272 char *name = getenv("TERM");
274 || newterm(name, ofp, ifp) == 0) {
275 fprintf(stderr, "cannot open terminal\n");
276 ExitProgram(EXIT_FAILURE);
289 hascolor = has_colors();
292 short bg = COLOR_BLACK;
294 #if HAVE_USE_DEFAULT_COLORS
295 if (d_option && (use_default_colors() == OK))
298 init_pair(PAIR_DIGITS, COLOR_BLACK, COLOR_RED);
299 init_pair(PAIR_OTHERS, COLOR_RED, bg);
300 init_pair(PAIR_FRAMES, COLOR_WHITE, bg);
301 (void) attrset(AttrArg(COLOR_PAIR(PAIR_OTHERS), 0));
305 for (j = 0; j < 5; j++)
306 older[j] = newer[j] = next[j] = 0;
319 tm = localtime(&now);
322 set(tm->tm_sec % 10, 0);
323 set(tm->tm_sec / 10, 4);
324 set(tm->tm_min % 10, 10);
325 set(tm->tm_min / 10, 14);
326 set(tm->tm_hour % 10, 20);
327 set(tm->tm_hour / 10, 24);
331 for (k = 0; k < 6; k++) {
333 for (i = 0; i < 5; i++)
334 newer[i] = (newer[i] & ~mask) | (newer[i + 1] & mask);
335 newer[5] = (newer[5] & ~mask) | (next[k] & mask);
337 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
340 for (s = 1; s >= 0; s--) {
342 for (i = 0; i < 6; i++) {
343 if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
345 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
347 if (!(a & (t << 1))) {
348 move(YBASE + i, XBASE + 2 * j);
363 * If we're scrolling, space out the refreshes to fake
364 * movement. That's 7 frames, or 6 intervals, which would
365 * be 166 msec if we spread it out over a second. It looks
366 * better (but will work on a slow terminal, e.g., less
367 * than 9600bd) to squeeze that into a half-second, and use
368 * half of 170 msec to ensure that the program doesn't eat
369 * a lot of time when asking what time it is, at the top of
370 * this loop -T.Dickey
376 switch (wgetch(stdscr)) {
384 nodelay(stdscr, FALSE);
387 nodelay(stdscr, TRUE);
405 if (!hascustomtime) {
406 /* this depends on the detailed format of ctime(3) */
407 _nc_STRNCPY(buf, ctime(&now), (size_t) 30);
411 while ((*d2++ = *s2++) != '\0') ;
413 MvAddStr(16, 30, buf);
421 * If we're not smooth-scrolling, wait 1000 msec (1 sec). Use napms()
422 * rather than sleep() because the latter does odd things on some
423 * systems, e.g., suspending output as well.
431 * This is a safe way to check if we're interrupted - making the signal
432 * handler set a flag that we can check. Since we're running
433 * nodelay(), the wgetch() call returns immediately, and in particular
434 * will return an error if interrupted. This works only if we can
435 * read from the input, of course.
438 switch (wgetch(stdscr)) {
446 nodelay(stdscr, FALSE);
449 nodelay(stdscr, TRUE);
465 ExitProgram(EXIT_SUCCESS);