]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/gdc.c
ncurses 6.5 - patch 20240622
[ncurses.git] / test / gdc.c
1 /****************************************************************************
2  * Copyright 2019-2022,2024 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * Grand digital clock for curses compatible terminals
31  * Usage: gdc [-s] [-t hh:mm:ss] [n] -- run for n seconds (default infinity)
32  * Flags: -s: scroll
33  *
34  * modified 10-18-89 for curses (jrl)
35  * 10-18-89 added signal handling
36  *
37  * $Id: gdc.c,v 1.58 2024/06/22 22:27:52 tom Exp $
38  */
39
40 #include <test.priv.h>
41
42 #include <time.h>
43
44 #define YBASE   10
45 #define XBASE   10
46 #define XLENGTH 54
47 #define YDEPTH  5
48
49 #define PAIR_DIGITS 1
50 #define PAIR_OTHERS 2
51 #define PAIR_FRAMES 3
52
53 static short disp[11] =
54 {
55     075557, 011111, 071747, 071717, 055711,
56     074717, 074757, 071111, 075757, 075717, 002020
57 };
58 static long older[6], next[6], newer[6], mask;
59
60 static int sigtermed = 0;
61 static bool redirected = FALSE;
62 static bool hascolor = FALSE;
63 static bool hascustomtime = FALSE;
64
65 static void
66 sighndl(int signo)
67 {
68     signal(signo, sighndl);
69     sigtermed = signo;
70     if (redirected) {
71         stop_curses();
72         ExitProgram(EXIT_FAILURE);
73     }
74 }
75
76 static void
77 check_term(void)
78 {
79     if (sigtermed) {
80         (void) standend();
81         stop_curses();
82         fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
83         ExitProgram(EXIT_FAILURE);
84     }
85 }
86
87 static void
88 drawbox(bool scrolling)
89 {
90     chtype bottom[XLENGTH + 1];
91
92     if (hascolor)
93         (void) attrset(AttrArg(COLOR_PAIR(PAIR_FRAMES), 0));
94
95     MvAddCh(YBASE - 1, XBASE - 1, ACS_ULCORNER);
96     hline(ACS_HLINE, XLENGTH);
97     MvAddCh(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
98
99     MvAddCh(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
100     if ((mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH)) != ERR) {
101         int n;
102         for (n = 0; n < XLENGTH; n++) {
103             if (!scrolling)
104                 bottom[n] &= ~A_COLOR;
105             bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
106         }
107         (void) mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
108     }
109     MvAddCh(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
110
111     move(YBASE, XBASE - 1);
112     vline(ACS_VLINE, YDEPTH);
113
114     move(YBASE, XBASE + XLENGTH);
115     vline(ACS_VLINE, YDEPTH);
116
117     if (hascolor)
118         (void) attrset(AttrArg(COLOR_PAIR(PAIR_OTHERS), 0));
119 }
120
121 static void
122 standt(int on)
123 {
124     if (on) {
125         if (hascolor) {
126             attron(COLOR_PAIR(PAIR_DIGITS));
127         } else {
128             attron(A_STANDOUT);
129         }
130     } else {
131         if (hascolor) {
132             attron(COLOR_PAIR(PAIR_OTHERS));
133         } else {
134             attroff(A_STANDOUT);
135         }
136     }
137 }
138
139 static void
140 set(int t, int n)
141 {
142     int i, m;
143
144     m = 7 << n;
145     for (i = 0; i < 5; i++) {
146         next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
147         mask |= (next[i] ^ older[i]) & m;
148     }
149     if (mask & m)
150         mask |= m;
151 }
152
153 static void
154 usage(int ok)
155 {
156     static const char *msg[] =
157     {
158         "usage: gdc [-dns] -[t HH:MM:SS] [COUNT]"
159         ,""
160         ,"Display a digital clock, running indefinitely or for COUNT"
161         " seconds."
162         ,""
163         ,USAGE_COMMON
164         ,"Options:"
165 #if HAVE_USE_DEFAULT_COLORS
166         ," -d           uses the terminal's default background color"
167 #endif
168         ," -n           reads input from /dev/null"
169         ," -s           scrolls each digit into place"
170         ," -t HH:MM:SS  starts clock at specified time"
171     };
172     unsigned j;
173     for (j = 0; j < SIZEOF(msg); j++)
174         fprintf(stderr, "%s\n", msg[j]);
175     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
176 }
177
178 static time_t
179 parse_time(const char *value)
180 {
181     int hh, mm, ss;
182     int check;
183     time_t result;
184     char c;
185     struct tm *tm;
186
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) {
189             usage(FALSE);
190         }
191     }
192
193     if ((hh < 0) || (hh >= 24) ||
194         (mm < 0) || (mm >= 60) ||
195         (ss < 0) || (ss >= 60)) {
196         usage(FALSE);
197     }
198
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)
204             break;
205         result += 3600;
206     }
207
208     if (tm->tm_hour != hh) {
209         fprintf(stderr, "Cannot find local time for %s!\n", value);
210         usage(FALSE);
211     }
212     return result;
213 }
214 /* *INDENT-OFF* */
215 VERSION_COMMON()
216 /* *INDENT-ON* */
217
218 int
219 main(int argc, char *argv[])
220 {
221     time_t now;
222     struct tm *tm;
223     long t, a;
224     int i, j, s, k, ch;
225     int count = 0;
226     FILE *ofp = stdout;
227     FILE *ifp = stdin;
228     bool smooth = FALSE;
229     bool stages = FALSE;
230     time_t starts = 0;
231 #if HAVE_USE_DEFAULT_COLORS
232     bool d_option = FALSE;
233 #endif
234
235     setlocale(LC_ALL, "");
236
237     while ((ch = getopt(argc, argv, OPTS_COMMON "dnst:")) != -1) {
238         switch (ch) {
239 #if HAVE_USE_DEFAULT_COLORS
240         case 'd':
241             d_option = TRUE;
242             break;
243 #endif
244         case 'n':
245             ifp = fopen("/dev/null", "r");
246             redirected = TRUE;
247             break;
248         case 's':
249             smooth = TRUE;
250             break;
251         case 't':
252             hascustomtime = TRUE;
253             starts = parse_time(optarg);
254             break;
255         case OPTS_VERSION:
256             show_version(argv);
257             ExitProgram(EXIT_SUCCESS);
258         default:
259             usage(ch == OPTS_USAGE);
260             /* NOTREACHED */
261         }
262     }
263     if (optind < argc) {
264         count = atoi(argv[optind++]);
265         assert(count >= 0);
266         if (optind < argc)
267             usage(FALSE);
268     }
269
270     InitAndCatch({
271         if (redirected) {
272             char *name = getenv("TERM");
273             if (name == 0
274                 || newterm(name, ofp, ifp) == 0) {
275                 fprintf(stderr, "cannot open terminal\n");
276                 ExitProgram(EXIT_FAILURE);
277             }
278         } else {
279             initscr();
280         }
281     }
282     ,sighndl);
283
284     cbreak();
285     noecho();
286     nodelay(stdscr, 1);
287     curs_set(0);
288
289     hascolor = has_colors();
290
291     if (hascolor) {
292         short bg = COLOR_BLACK;
293         start_color();
294 #if HAVE_USE_DEFAULT_COLORS
295         if (d_option && (use_default_colors() == OK))
296             bg = -1;
297 #endif
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));
302     }
303
304   restart:
305     for (j = 0; j < 5; j++)
306         older[j] = newer[j] = next[j] = 0;
307
308     clear();
309     drawbox(FALSE);
310
311     do {
312         char buf[40];
313
314         if (starts != 0) {
315             now = ++starts;
316         } else {
317             time(&now);
318         }
319         tm = localtime(&now);
320
321         mask = 0;
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);
328         set(10, 7);
329         set(10, 17);
330
331         for (k = 0; k < 6; k++) {
332             if (smooth) {
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);
336             } else {
337                 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
338             }
339             next[k] = 0;
340             for (s = 1; s >= 0; s--) {
341                 standt(s);
342                 for (i = 0; i < 6; i++) {
343                     if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
344                         != 0) {
345                         for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
346                             if (a & t) {
347                                 if (!(a & (t << 1))) {
348                                     move(YBASE + i, XBASE + 2 * j);
349                                 }
350                                 addstr("  ");
351                             }
352                         }
353                     }
354                     if (!s) {
355                         older[i] = newer[i];
356                     }
357                 }
358                 if (!s) {
359                     if (smooth)
360                         drawbox(TRUE);
361                     refresh();
362                     /*
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
371                      */
372                     if (smooth)
373                         napms(85);
374                     if (stages) {
375                         stages = FALSE;
376                         switch (wgetch(stdscr)) {
377                         case 'q':
378                             count = 1;
379                             break;
380                         case 'S':
381                             stages = TRUE;
382                             /* FALLTHRU */
383                         case 's':
384                             nodelay(stdscr, FALSE);
385                             break;
386                         case ' ':
387                             nodelay(stdscr, TRUE);
388                             break;
389 #ifdef KEY_RESIZE
390                         case KEY_RESIZE:
391 #endif
392                         case '?':
393                             goto restart;
394                         case ERR:
395                             check_term();
396                             /* FALLTHRU */
397                         default:
398                             continue;
399                         }
400                     }
401                 }
402             }
403         }
404
405         if (!hascustomtime) {
406             /* this depends on the detailed format of ctime(3) */
407             _nc_STRNCPY(buf, ctime(&now), (size_t) 30);
408             {
409                 char *d2 = buf + 10;
410                 char *s2 = buf + 19;
411                 while ((*d2++ = *s2++) != '\0') ;
412             }
413             MvAddStr(16, 30, buf);
414         }
415
416         move(6, 0);
417         drawbox(FALSE);
418         refresh();
419
420         /*
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.
424          */
425         if (smooth)
426             napms(500);
427         else
428             napms(1000);
429
430         /*
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.
436          */
437         stages = FALSE;
438         switch (wgetch(stdscr)) {
439         case 'q':
440             count = 1;
441             break;
442         case 'S':
443             stages = TRUE;
444             /* FALLTHRU */
445         case 's':
446             nodelay(stdscr, FALSE);
447             break;
448         case ' ':
449             nodelay(stdscr, TRUE);
450             break;
451 #ifdef KEY_RESIZE
452         case KEY_RESIZE:
453 #endif
454         case '?':
455             goto restart;
456         case ERR:
457             check_term();
458             /* FALLTHRU */
459         default:
460             continue;
461         }
462     } while (--count);
463     (void) standend();
464     stop_curses();
465     ExitProgram(EXIT_SUCCESS);
466 }