]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 6.2 - patch 20200502
[ncurses.git] / test / tclock.c
1 /* $Id: tclock.c,v 1.41 2020/01/18 16:46:35 tom Exp $ */
2
3 #define NEED_TIME_H
4 #include <test.priv.h>
5
6 #if HAVE_MATH_H
7
8 #include <math.h>
9
10 /*
11   tclock - analog/digital clock for curses.
12   If it gives you joy, then
13   (a) I'm glad
14   (b) you need to get out more :-)
15
16   This program is copyright Howard Jones, September 1994
17   (ha.jones@ic.ac.uk). It may be freely distributed as
18   long as this copyright message remains intact, and any
19   modifications are clearly marked as such. [In fact, if
20   you modify it, I wouldn't mind the modifications back,
21   especially if they add any nice features. A good one
22   would be a precalc table for the 60 hand positions, so
23   that the floating point stuff can be ditched. As I said,
24   it was a 20 hackup minute job.]
25
26   COMING SOON: tfishtank. Be the envy of your mac-owning
27   colleagues.
28 */
29
30 /* To compile: cc -o tclock tclock.c -lcurses -lm */
31
32 #ifndef PI
33 #define PI 3.141592654
34 #endif
35
36 #define sign(_x) (_x<0?-1:1)
37
38 #define ASPECT 2.2
39 #define ROUND(value) ((int)((value) + 0.5))
40
41 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
42 #define A2Y(angle,radius) ROUND(radius * cos(angle))
43
44 /* Plot a point */
45 static void
46 plot(int x, int y, int col)
47 {
48     MvAddCh(y, x, (chtype) col);
49 }
50
51 /* Draw a diagonal(arbitrary) line using Bresenham's algorithm. */
52 static void
53 dline(int pair, int from_x, int from_y, int x2, int y2, int ch)
54 {
55     int dx, dy;
56     int ax, ay;
57     int sx, sy;
58     int x, y;
59     int d;
60
61     if (has_colors())
62         (void) attrset(AttrArg(COLOR_PAIR(pair), 0));
63
64     dx = x2 - from_x;
65     dy = y2 - from_y;
66
67     ax = abs(dx * 2);
68     ay = abs(dy * 2);
69
70     sx = sign(dx);
71     sy = sign(dy);
72
73     x = from_x;
74     y = from_y;
75
76     if (ax > ay) {
77         d = ay - (ax / 2);
78
79         while (1) {
80             plot(x, y, ch);
81             if (x == x2)
82                 return;
83
84             if (d >= 0) {
85                 y += sy;
86                 d -= ax;
87             }
88             x += sx;
89             d += ay;
90         }
91     } else {
92         d = ax - (ay / 2);
93
94         while (1) {
95             plot(x, y, ch);
96             if (y == y2)
97                 return;
98
99             if (d >= 0) {
100                 x += sx;
101                 d -= ay;
102             }
103             y += sy;
104             d += ax;
105         }
106     }
107 }
108
109 static void
110 usage(void)
111 {
112     static const char *msg[] =
113     {
114         "Usage: tclock [options]"
115         ,""
116         ,"Options:"
117 #if HAVE_USE_DEFAULT_COLORS
118         ," -d       invoke use_default_colors"
119 #endif
120     };
121     size_t n;
122
123     for (n = 0; n < SIZEOF(msg); n++)
124         fprintf(stderr, "%s\n", msg[n]);
125
126     ExitProgram(EXIT_FAILURE);
127 }
128
129 int
130 main(int argc, char *argv[])
131 {
132     int i, cx, cy;
133     double cr, mradius, hradius, mangle, hangle;
134     double sangle, sradius, hours;
135     int hdx, hdy;
136     int mdx, mdy;
137     int sdx, sdy;
138     int ch;
139     int lastbeep = -1;
140     bool odd = FALSE;
141     time_t tim;
142     struct tm *t;
143     char szChar[10];
144     char *text;
145     short my_bg = COLOR_BLACK;
146 #if HAVE_GETTIMEOFDAY
147     struct timeval current;
148 #endif
149     double fraction = 0.0;
150 #if HAVE_USE_DEFAULT_COLORS
151     bool d_option = FALSE;
152 #endif
153
154     while ((ch = getopt(argc, argv, "d")) != -1) {
155         switch (ch) {
156 #if HAVE_USE_DEFAULT_COLORS
157         case 'd':
158             d_option = TRUE;
159             break;
160 #endif
161         default:
162             usage();
163             /* NOTREACHED */
164         }
165     }
166     if (optind < argc)
167         usage();
168
169     setlocale(LC_ALL, "");
170
171     initscr();
172     noecho();
173     cbreak();
174     nodelay(stdscr, TRUE);
175     curs_set(0);
176
177     if (has_colors()) {
178         start_color();
179 #if HAVE_USE_DEFAULT_COLORS
180         if (d_option && (use_default_colors() == OK))
181             my_bg = -1;
182 #endif
183         init_pair(1, COLOR_RED, my_bg);
184         init_pair(2, COLOR_MAGENTA, my_bg);
185         init_pair(3, COLOR_GREEN, my_bg);
186         init_pair(4, COLOR_WHITE, COLOR_BLUE);
187     }
188 #ifdef KEY_RESIZE
189     keypad(stdscr, TRUE);
190   restart:
191 #endif
192     cx = (COLS - 1) / 2;        /* 39 */
193     cy = LINES / 2;             /* 12 */
194     if (cx / ASPECT < cy)
195         cr = cx / ASPECT;
196     else
197         cr = cy;
198     sradius = (5 * cr) / 6;     /* 10 */
199     mradius = (3 * cr) / 4;     /* 9 */
200     hradius = cr / 2;           /* 6 */
201
202     for (i = 0; i < 12; i++) {
203         sangle = (i + 1) * (2.0 * PI) / 12.0;
204         sdx = A2X(sangle, sradius);
205         sdy = A2Y(sangle, sradius);
206         _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
207
208         MvAddStr(cy - sdy, cx + sdx, szChar);
209     }
210
211     MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
212
213     sradius = (4 * sradius) / 5;
214     for (;;) {
215         napms(100);
216
217         tim = time(0);
218         t = localtime(&tim);
219
220         hours = (t->tm_hour + (t->tm_min / 60.0));
221         if (hours > 12.0)
222             hours -= 12.0;
223
224         mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
225         mdx = A2X(mangle, mradius);
226         mdy = A2Y(mangle, mradius);
227
228         hangle = ((hours) * (2.0 * PI) / 12.0);
229         hdx = A2X(hangle, hradius);
230         hdy = A2Y(hangle, hradius);
231
232 #if HAVE_GETTIMEOFDAY
233         gettimeofday(&current, 0);
234         fraction = ((double) current.tv_usec / 1.0e6);
235 #endif
236         sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
237         sdx = A2X(sangle, sradius);
238         sdy = A2Y(sangle, sradius);
239
240         dline(3, cx, cy, cx + mdx, cy - mdy, '#');
241
242         (void) attrset(A_REVERSE);
243         dline(2, cx, cy, cx + hdx, cy - hdy, '.');
244         attroff(A_REVERSE);
245
246         if (has_colors())
247             (void) attrset(AttrArg(COLOR_PAIR(1), 0));
248
249         dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
250
251         if (has_colors())
252             (void) attrset(AttrArg(COLOR_PAIR(0), 0));
253
254         text = ctime(&tim);
255         MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
256         refresh();
257         if ((t->tm_sec % 5) == 0
258             && t->tm_sec != lastbeep) {
259             lastbeep = t->tm_sec;
260             if (has_colors()) {
261                 odd = !odd;
262                 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
263             }
264             beep();
265         }
266
267         if ((ch = getch()) != ERR) {
268 #ifdef KEY_RESIZE
269             if (ch == KEY_RESIZE) {
270                 flash();
271                 erase();
272                 wrefresh(curscr);
273                 goto restart;
274             }
275 #endif
276             break;
277         }
278
279         dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
280         dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
281         dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
282
283     }
284
285     stop_curses();
286     ExitProgram(EXIT_SUCCESS);
287 }
288 #else
289 int
290 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
291 {
292     printf("This program requires the development header math.h\n");
293     ExitProgram(EXIT_FAILURE);
294 }
295 #endif