2 * Copyright (c) 2002 Amos Shapir.
5 * Grand digital clock for curses compatible terminals
7 * Usage: grdc [-st] [-d msecs] [n] -- run for n seconds (default infinity)
8 * Flags: -s: scroll (default scroll duration 120msec)
9 * -t: show time in 12-hour format
10 * -d msecs: specify scroll duration (implies -s)
12 * modified 10-18-89 for curses (jrl)
13 * 10-18-89 added signal handling
14 * 03-23-04 added centering, scroll delay (cap)
16 * $FreeBSD: src/games/grdc/grdc.c,v 1.8.2.1 2001/10/02 11:51:49 ru Exp $
32 static int hascolor
= 0;
33 static int xbase
, ybase
, xmax
, ymax
;
34 static long old
[6], next
[6], new[6], mask
;
35 static volatile sig_atomic_t sigtermed
;
37 static void cleanup(void);
38 static void draw_row(int, int);
39 static void set(int, int);
40 static void sighndl(int);
41 static int snooze(long);
42 static void standt(int);
43 static void usage(void) __dead2
;
52 main(int argc
, char **argv
)
55 bool t12
, scrol
, forever
;
56 long scroll_msecs
, delay_msecs
;
57 struct timespec now
, scroll_ts
;
66 while ((ch
= getopt(argc
, argv
, "d:st")) != -1) {
69 scroll_msecs
= atol(optarg
);
71 errx(1, "scroll duration may not be negative");
100 getmaxyx(stdscr
, ymax
, xmax
);
101 if (ymax
< YDEPTH
+ 2 || xmax
< XLENGTH
+ 4) {
103 errx(1, "terminal too small");
105 xbase
= (xmax
- XLENGTH
) / 2 + 2;
106 ybase
= (ymax
- YDEPTH
) / 2 + 1;
108 signal(SIGINT
, sighndl
);
109 signal(SIGTERM
, sighndl
);
110 signal(SIGHUP
, sighndl
);
116 hascolor
= has_colors();
120 init_pair(1, COLOR_BLACK
, COLOR_RED
);
121 init_pair(2, COLOR_RED
, COLOR_BLACK
);
122 init_pair(3, COLOR_WHITE
, COLOR_BLACK
);
123 attrset(COLOR_PAIR(2));
130 attrset(COLOR_PAIR(3));
132 mvaddch(ybase
- 2, xbase
- 3, ACS_ULCORNER
);
133 hline(ACS_HLINE
, XLENGTH
);
134 mvaddch(ybase
- 2, xbase
- 2 + XLENGTH
, ACS_URCORNER
);
136 mvaddch(ybase
+ YDEPTH
- 1, xbase
- 3, ACS_LLCORNER
);
137 hline(ACS_HLINE
, XLENGTH
);
138 mvaddch(ybase
+ YDEPTH
- 1, xbase
- 2 + XLENGTH
, ACS_LRCORNER
);
140 move(ybase
- 1, xbase
- 3);
141 vline(ACS_VLINE
, YDEPTH
);
143 move(ybase
- 1, xbase
- 2 + XLENGTH
);
144 vline(ACS_VLINE
, YDEPTH
);
146 attrset(COLOR_PAIR(2));
150 scroll_ts
.tv_sec
= scroll_msecs
/ 1000;
151 scroll_ts
.tv_nsec
= 1000000 * (scroll_msecs
% 1000);
154 clock_gettime(CLOCK_REALTIME_FAST
, &now
);
156 timespecadd(&now
, &scroll_ts
, &now
);
157 tm
= localtime(&now
.tv_sec
);
160 set(tm
->tm_sec
% 10, 0);
161 set(tm
->tm_sec
/ 10, 4);
162 set(tm
->tm_min
% 10, 10);
163 set(tm
->tm_min
/ 10, 14);
165 if (tm
->tm_hour
== 0) {
167 mvaddstr(ybase
- 1, xbase
, "AM");
168 } else if (tm
->tm_hour
< 12) {
169 mvaddstr(ybase
- 1, xbase
, "AM");
170 } else if (tm
->tm_hour
== 12) {
171 mvaddstr(ybase
- 1, xbase
, "PM");
174 mvaddstr(ybase
- 1, xbase
, "PM");
177 set(tm
->tm_hour
% 10, 20);
178 set(tm
->tm_hour
/ 10, 24);
182 for (k
= 0; k
< 6; k
++) {
184 if (snooze(scroll_msecs
/ 6) == 1)
186 for(i
= 0; i
< 5; i
++) {
187 new[i
] = (new[i
] & ~mask
) |
190 new[5] = (new[5] & ~mask
) | (next
[k
] & mask
);
192 new[k
] = (new[k
] & ~mask
) | (next
[k
] & mask
);
195 for (s
= 1; s
>= 0; s
--) {
197 for (i
= 0; i
< 6; i
++)
208 clock_gettime(CLOCK_REALTIME_FAST
, &now
);
209 delay_msecs
= 1000 - now
.tv_nsec
/ 1000000;
210 /* want scrolling to end on the second */
212 delay_msecs
-= scroll_msecs
;
213 if (delay_msecs
> 0) {
214 if (snooze(delay_msecs
) == 1)
217 } while (forever
? 1 : --n
);
228 static struct pollfd pfd
= {
238 ts
.tv_nsec
= 1000000 * msecs
;
240 ts
.tv_sec
= msecs
/ 1000;
241 ts
.tv_nsec
= 1000000 * (msecs
% 1000);
244 rv
= ppoll(&pfd
, 1, &ts
, NULL
);
253 errx(1, "terminated by signal %d", (int)sigtermed
);
269 draw_row(int i
, int s
)
274 if ((a
= (new[i
] ^ old
[i
]) & (s
? new : old
)[i
]) != 0) {
275 for (j
= 0, t
= 1 << 26; t
; t
>>= 1, j
++) {
277 if (!(a
& (t
<< 1))) {
278 move(ybase
+ i
, xbase
+ 2 * j
);
292 static short disp
[11] = {
293 075557, 011111, 071747, 071717, 055711,
294 074717, 074757, 071111, 075757, 075717, 002020
299 for (i
= 0; i
< 5; i
++) {
300 next
[i
] |= ((disp
[t
] >> (4 - i
) * 3) & 07) << n
;
301 mask
|= (next
[i
] ^ old
[i
]) & m
;
312 attron(COLOR_PAIR(1));
318 attron(COLOR_PAIR(2));
328 fprintf(stderr
, "usage: grdc [-st] [-d msecs] [n]\n");