]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/gdc.c
6bf3830145ed789efe8ab887747513827dda18f1
[ncurses.git] / test / gdc.c
1 /*
2  * Grand digital clock for curses compatible terminals
3  * Usage: gdc [-s] [n]   -- run for n seconds (default infinity)
4  * Flags: -s: scroll
5  *
6  * modified 10-18-89 for curses (jrl)
7  * 10-18-89 added signal handling
8  *
9  * $Id: gdc.c,v 1.8 1997/01/19 00:59:28 tom Exp $
10  */
11
12 #include <test.priv.h>
13
14 #include <time.h>
15 #include <signal.h>
16 #include <string.h>
17
18 #define YBASE   10
19 #define XBASE   10
20 #define XLENGTH 54
21 #define YDEPTH  5
22
23 /* it won't be */
24 static time_t now; /* yeah! */
25 static struct tm *tm;
26
27 static short disp[11] = {
28         075557, 011111, 071747, 071717, 055711,
29         074717, 074757, 071111, 075757, 075717, 002020
30 };
31 static long old[6], next[6], new[6], mask;
32 static char scrol;
33
34 static int sigtermed = 0;
35
36 static int hascolor = 0;
37
38 static void set(int, int);
39 static void standt(int);
40 static void movto(int, int);
41
42 static
43 RETSIGTYPE sighndl(int signo)
44 {
45         signal(signo, sighndl);
46         sigtermed=signo;
47 }
48
49 int
50 main(int argc, char *argv[])
51 {
52 long t, a;
53 int i, j, s, k;
54 int n = 0;
55
56         signal(SIGINT,sighndl);
57         signal(SIGTERM,sighndl);
58         signal(SIGKILL,sighndl);
59
60         initscr();
61         cbreak();
62         noecho();
63         nodelay(stdscr, 1);
64
65         hascolor = has_colors();
66
67         if(hascolor) {
68                 int bg = COLOR_BLACK;
69                 start_color();
70 #ifdef NCURSES_VERSION
71                 if (use_default_colors() == OK)
72                         bg = -1;
73 #endif
74                 init_pair(1, COLOR_BLACK, COLOR_RED);
75                 init_pair(2, COLOR_RED,   bg);
76                 init_pair(3, COLOR_WHITE, bg);
77                 attrset(COLOR_PAIR(2));
78         }
79
80         clear();
81         refresh();
82         while(--argc > 0) {
83                 if(**++argv == '-')
84                         scrol = 1;
85                 else
86                         n = atoi(*argv);
87         }
88
89         if(hascolor) {
90                 attrset(COLOR_PAIR(3));
91
92                 mvaddch(YBASE - 1,  XBASE - 1, ACS_ULCORNER);
93                 hline(ACS_HLINE, XLENGTH);
94                 mvaddch(YBASE - 1,  XBASE + XLENGTH, ACS_URCORNER);
95
96                 mvaddch(YBASE + YDEPTH,  XBASE - 1, ACS_LLCORNER);
97                 hline(ACS_HLINE, XLENGTH);
98                 mvaddch(YBASE + YDEPTH,  XBASE + XLENGTH, ACS_LRCORNER);
99
100                 move(YBASE,  XBASE - 1);
101                 vline(ACS_VLINE, YDEPTH);
102
103                 move(YBASE,  XBASE + XLENGTH);
104                 vline(ACS_VLINE, YDEPTH);
105
106                 attrset(COLOR_PAIR(2));
107         }
108         do {
109                 char    buf[30];
110
111                 mask = 0;
112                 time(&now);
113                 tm = localtime(&now);
114                 set(tm->tm_sec%10, 0);
115                 set(tm->tm_sec/10, 4);
116                 set(tm->tm_min%10, 10);
117                 set(tm->tm_min/10, 14);
118                 set(tm->tm_hour%10, 20);
119                 set(tm->tm_hour/10, 24);
120                 set(10, 7);
121                 set(10, 17);
122                 for(k=0; k<6; k++) {
123                         if(scrol) {
124                                 for(i=0; i<5; i++)
125                                         new[i] = (new[i]&~mask) | (new[i+1]&mask);
126                                 new[5] = (new[5]&~mask) | (next[k]&mask);
127                         } else
128                                 new[k] = (new[k]&~mask) | (next[k]&mask);
129                         next[k] = 0;
130                         for(s=1; s>=0; s--) {
131                                 standt(s);
132                                 for(i=0; i<6; i++) {
133                                         if((a = (new[i]^old[i])&(s ? new : old)[i]) != 0) {
134                                                 for(j=0,t=1<<26; t; t>>=1,j++) {
135                                                         if(a&t) {
136                                                                 if(!(a&(t<<1))) {
137                                                                         movto(YBASE + i, XBASE + 2*j);
138                                                                 }
139                                                                 addstr("  ");
140                                                         }
141                                                 }
142                                         }
143                                         if(!s) {
144                                                 old[i] = new[i];
145                                         }
146                                 }
147                                 if(!s) {
148                                         refresh();
149                                 }
150                         }
151                 }
152
153                 /* this depends on the detailed format of ctime(3) */
154                 (void) strcpy(buf, ctime(&now));
155                 (void) strcpy(buf + 10, buf + 19);
156                 mvaddstr(16, 30, buf);
157
158                 movto(6, 0);
159                 refresh();
160                 sleep(1);
161                 while(wgetch(stdscr) != ERR)
162                         continue;
163                 if (sigtermed) {
164                         standend();
165                         clear();
166                         refresh();
167                         endwin();
168                         fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
169                         return EXIT_FAILURE;
170                 }
171         } while(--n);
172         standend();
173         clear();
174         refresh();
175         endwin();
176         return EXIT_SUCCESS;
177 }
178
179 static void
180 set(int t, int n)
181 {
182 int i, m;
183
184         m = 7<<n;
185         for(i=0; i<5; i++) {
186                 next[i] |= ((disp[t]>>(4-i)*3)&07)<<n;
187                 mask |= (next[i]^old[i])&m;
188         }
189         if(mask&m)
190                 mask |= m;
191 }
192
193 static void
194 standt(int on)
195 {
196         if (on) {
197                 if(hascolor) {
198                         attron(COLOR_PAIR(1));
199                 } else {
200                         attron(A_STANDOUT);
201                 }
202         } else {
203                 if(hascolor) {
204                         attron(COLOR_PAIR(2));
205                 } else {
206                         attroff(A_STANDOUT);
207                 }
208         }
209 }
210
211 static void
212 movto(int line, int col)
213 {
214         move(line, col);
215 }