]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/gdc.c
ncurses 5.6 - patch 20070128
[ncurses.git] / test / gdc.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * Grand digital clock for curses compatible terminals
30  * Usage: gdc [-s] [n]   -- run for n seconds (default infinity)
31  * Flags: -s: scroll
32  *
33  * modified 10-18-89 for curses (jrl)
34  * 10-18-89 added signal handling
35  *
36  * $Id: gdc.c,v 1.28 2006/05/20 15:37:44 tom Exp $
37  */
38
39 #include <test.priv.h>
40
41 #include <time.h>
42
43 #define YBASE   10
44 #define XBASE   10
45 #define XLENGTH 54
46 #define YDEPTH  5
47
48 #define PAIR_DIGITS 1
49 #define PAIR_OTHERS 2
50 #define PAIR_FRAMES 3
51
52 static short disp[11] =
53 {
54     075557, 011111, 071747, 071717, 055711,
55     074717, 074757, 071111, 075757, 075717, 002020
56 };
57 static long older[6], next[6], newer[6], mask;
58
59 static int sigtermed = 0;
60 static bool redirected = FALSE;
61 static bool hascolor = FALSE;
62
63 static RETSIGTYPE
64 sighndl(int signo)
65 {
66     signal(signo, sighndl);
67     sigtermed = signo;
68     if (redirected) {
69         endwin();
70         ExitProgram(EXIT_FAILURE);
71     }
72 }
73
74 static void
75 drawbox(bool scrolling)
76 {
77     chtype bottom[XLENGTH + 1];
78     int n;
79
80     if (hascolor)
81         attrset(COLOR_PAIR(PAIR_FRAMES));
82
83     mvaddch(YBASE - 1, XBASE - 1, ACS_ULCORNER);
84     hline(ACS_HLINE, XLENGTH);
85     mvaddch(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
86
87     mvaddch(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
88     mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
89     for (n = 0; n < XLENGTH; n++) {
90         if (!scrolling)
91             bottom[n] &= ~A_COLOR;
92         bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
93     }
94     mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
95     mvaddch(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
96
97     move(YBASE, XBASE - 1);
98     vline(ACS_VLINE, YDEPTH);
99
100     move(YBASE, XBASE + XLENGTH);
101     vline(ACS_VLINE, YDEPTH);
102
103     if (hascolor)
104         attrset(COLOR_PAIR(PAIR_OTHERS));
105 }
106
107 static void
108 standt(int on)
109 {
110     if (on) {
111         if (hascolor) {
112             attron(COLOR_PAIR(PAIR_DIGITS));
113         } else {
114             attron(A_STANDOUT);
115         }
116     } else {
117         if (hascolor) {
118             attron(COLOR_PAIR(PAIR_OTHERS));
119         } else {
120             attroff(A_STANDOUT);
121         }
122     }
123 }
124
125 static void
126 set(int t, int n)
127 {
128     int i, m;
129
130     m = 7 << n;
131     for (i = 0; i < 5; i++) {
132         next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
133         mask |= (next[i] ^ older[i]) & m;
134     }
135     if (mask & m)
136         mask |= m;
137 }
138
139 static void
140 usage(void)
141 {
142     static const char *msg[] =
143     {
144         "Usage: gdc [options] [count]"
145         ,""
146         ,"Options:"
147         ,"  -n  redirect input to /dev/null"
148         ,"  -s  scroll each number into place, rather than flipping"
149         ,""
150         ,"If you specify a count, gdc runs for that number of seconds"
151     };
152     unsigned j;
153     for (j = 0; j < SIZEOF(msg); j++)
154         fprintf(stderr, "%s\n", msg[j]);
155     ExitProgram(EXIT_FAILURE);
156 }
157
158 int
159 main(int argc, char *argv[])
160 {
161     time_t now;
162     struct tm *tm;
163     long t, a;
164     int i, j, s, k;
165     int count = 0;
166     FILE *ofp = stdout;
167     FILE *ifp = stdin;
168     bool scrol = FALSE;
169
170     setlocale(LC_ALL, "");
171
172     CATCHALL(sighndl);
173
174     while ((k = getopt(argc, argv, "sn")) != EOF) {
175         switch (k) {
176         case 's':
177             scrol = TRUE;
178             break;
179         case 'n':
180             ifp = fopen("/dev/null", "r");
181             redirected = TRUE;
182             break;
183         default:
184             usage();
185         }
186     }
187     if (optind < argc) {
188         count = atoi(argv[optind++]);
189     }
190     if (optind < argc)
191         usage();
192
193     if (redirected) {
194         char *name = getenv("TERM");
195         if (name == 0
196             || newterm(name, ofp, ifp) == 0) {
197             fprintf(stderr, "cannot open terminal\n");
198             ExitProgram(EXIT_FAILURE);
199         }
200
201     } else {
202         initscr();
203     }
204     cbreak();
205     noecho();
206     nodelay(stdscr, 1);
207     curs_set(0);
208
209     hascolor = has_colors();
210
211     if (hascolor) {
212         int bg = COLOR_BLACK;
213         start_color();
214 #if HAVE_USE_DEFAULT_COLORS
215         if (use_default_colors() == OK)
216             bg = -1;
217 #endif
218         init_pair(PAIR_DIGITS, COLOR_BLACK, COLOR_RED);
219         init_pair(PAIR_OTHERS, COLOR_RED, bg);
220         init_pair(PAIR_FRAMES, COLOR_WHITE, bg);
221         attrset(COLOR_PAIR(PAIR_OTHERS));
222     }
223
224   restart:
225     for (j = 0; j < 5; j++)
226         older[j] = newer[j] = next[j] = 0;
227
228     clear();
229     drawbox(FALSE);
230
231     do {
232         char buf[30];
233
234         time(&now);
235         tm = localtime(&now);
236
237         mask = 0;
238         set(tm->tm_sec % 10, 0);
239         set(tm->tm_sec / 10, 4);
240         set(tm->tm_min % 10, 10);
241         set(tm->tm_min / 10, 14);
242         set(tm->tm_hour % 10, 20);
243         set(tm->tm_hour / 10, 24);
244         set(10, 7);
245         set(10, 17);
246
247         for (k = 0; k < 6; k++) {
248             if (scrol) {
249                 for (i = 0; i < 5; i++)
250                     newer[i] = (newer[i] & ~mask) | (newer[i + 1] & mask);
251                 newer[5] = (newer[5] & ~mask) | (next[k] & mask);
252             } else
253                 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
254             next[k] = 0;
255             for (s = 1; s >= 0; s--) {
256                 standt(s);
257                 for (i = 0; i < 6; i++) {
258                     if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
259                         != 0) {
260                         for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
261                             if (a & t) {
262                                 if (!(a & (t << 1))) {
263                                     move(YBASE + i, XBASE + 2 * j);
264                                 }
265                                 addstr("  ");
266                             }
267                         }
268                     }
269                     if (!s) {
270                         older[i] = newer[i];
271                     }
272                 }
273                 if (!s) {
274                     if (scrol)
275                         drawbox(TRUE);
276                     refresh();
277                     /*
278                      * If we're scrolling, space out the refreshes to fake
279                      * movement.  That's 7 frames, or 6 intervals, which would
280                      * be 166 msec if we spread it out over a second.  It looks
281                      * better (but will work on a slow terminal, e.g., less
282                      * than 9600bd) to squeeze that into a half-second, and use
283                      * half of 170 msec to ensure that the program doesn't eat
284                      * a lot of time when asking what time it is, at the top of
285                      * this loop -T.Dickey
286                      */
287                     if (scrol)
288                         napms(85);
289                 }
290             }
291         }
292
293         /* this depends on the detailed format of ctime(3) */
294         (void) strcpy(buf, ctime(&now));
295         (void) strcpy(buf + 10, buf + 19);
296         mvaddstr(16, 30, buf);
297
298         move(6, 0);
299         drawbox(FALSE);
300         refresh();
301
302         /*
303          * If we're not scrolling, wait 1000 msec (1 sec).  Use napms() rather
304          * than sleep() because the latter does odd things on some systems,
305          * e.g., suspending output as well.
306          */
307         if (scrol)
308             napms(500);
309         else
310             napms(1000);
311
312         /*
313          * This is a safe way to check if we're interrupted - making the signal
314          * handler set a flag that we can check.  Since we're running
315          * nodelay(), the wgetch() call returns immediately, and in particular
316          * will return an error if interrupted.  This works only if we can
317          * read from the input, of course.
318          */
319         switch (wgetch(stdscr)) {
320         case 'q':
321             count = 1;
322             break;
323         case 's':
324             nodelay(stdscr, FALSE);
325             break;
326         case ' ':
327             nodelay(stdscr, TRUE);
328             break;
329 #ifdef KEY_RESIZE
330         case KEY_RESIZE:
331 #endif
332         case '?':
333             goto restart;
334         case ERR:
335             if (sigtermed) {
336                 standend();
337                 endwin();
338                 fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
339                 ExitProgram(EXIT_FAILURE);
340             }
341             /* FALLTHRU */
342         default:
343             continue;
344         }
345     } while (--count);
346     standend();
347     endwin();
348     ExitProgram(EXIT_SUCCESS);
349 }