]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 6.3 - patch 20221203
[ncurses.git] / test / tclock.c
1 /* $Id: tclock.c,v 1.46 2022/12/04 00:40:11 tom Exp $ */
2
3 #define NEED_TIME_H
4 #include <test.priv.h>
5
6 #if HAVE_MATH_H && HAVE_MATH_FUNCS
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(int ok)
111 {
112     static const char *msg[] =
113     {
114         "Usage: tclock [options]"
115         ,""
116         ,USAGE_COMMON
117         ,"Options:"
118 #if HAVE_USE_DEFAULT_COLORS
119         ," -d       invoke use_default_colors"
120 #endif
121     };
122     size_t n;
123
124     for (n = 0; n < SIZEOF(msg); n++)
125         fprintf(stderr, "%s\n", msg[n]);
126
127     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
128 }
129 /* *INDENT-OFF* */
130 VERSION_COMMON()
131 /* *INDENT-ON* */
132
133 int
134 main(int argc, char *argv[])
135 {
136     int i, cx, cy;
137     double cr, mradius, hradius, mangle, hangle;
138     double sangle, sradius, hours;
139     int hdx, hdy;
140     int mdx, mdy;
141     int sdx, sdy;
142     int ch;
143     int lastbeep = -1;
144     bool odd = FALSE;
145     time_t tim;
146     struct tm *t;
147     char szChar[20];
148     char *text;
149     short my_bg = COLOR_BLACK;
150 #if HAVE_GETTIMEOFDAY
151     struct timeval current;
152 #endif
153     double fraction = 0.0;
154 #if HAVE_USE_DEFAULT_COLORS
155     bool d_option = FALSE;
156 #endif
157
158     while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
159         switch (ch) {
160 #if HAVE_USE_DEFAULT_COLORS
161         case 'd':
162             d_option = TRUE;
163             break;
164 #endif
165         case OPTS_VERSION:
166             show_version(argv);
167             ExitProgram(EXIT_SUCCESS);
168         default:
169             usage(ch == OPTS_USAGE);
170             /* NOTREACHED */
171         }
172     }
173     if (optind < argc)
174         usage(FALSE);
175
176     setlocale(LC_ALL, "");
177
178     initscr();
179     noecho();
180     cbreak();
181     nodelay(stdscr, TRUE);
182     curs_set(0);
183
184     if (has_colors()) {
185         start_color();
186 #if HAVE_USE_DEFAULT_COLORS
187         if (d_option && (use_default_colors() == OK))
188             my_bg = -1;
189 #endif
190         init_pair(1, COLOR_RED, my_bg);
191         init_pair(2, COLOR_MAGENTA, my_bg);
192         init_pair(3, COLOR_GREEN, my_bg);
193         init_pair(4, COLOR_WHITE, COLOR_BLUE);
194     }
195 #ifdef KEY_RESIZE
196     keypad(stdscr, TRUE);
197   restart:
198 #endif
199     cx = (COLS - 1) / 2;        /* 39 */
200     cy = LINES / 2;             /* 12 */
201     if (cx / ASPECT < cy)
202         cr = cx / ASPECT;
203     else
204         cr = cy;
205     sradius = (5 * cr) / 6;     /* 10 */
206     mradius = (3 * cr) / 4;     /* 9 */
207     hradius = cr / 2;           /* 6 */
208
209     for (i = 0; i < 12; i++) {
210         sangle = (i + 1) * (2.0 * PI) / 12.0;
211         sdx = A2X(sangle, sradius);
212         sdy = A2Y(sangle, sradius);
213         _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
214
215         MvAddStr(cy - sdy, cx + sdx, szChar);
216     }
217
218     MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
219
220     sradius = (4 * sradius) / 5;
221     for (;;) {
222         napms(100);
223
224         tim = time(0);
225         t = localtime(&tim);
226
227         hours = (t->tm_hour + (t->tm_min / 60.0));
228         if (hours > 12.0)
229             hours -= 12.0;
230
231         mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
232         mdx = A2X(mangle, mradius);
233         mdy = A2Y(mangle, mradius);
234
235         hangle = ((hours) * (2.0 * PI) / 12.0);
236         hdx = A2X(hangle, hradius);
237         hdy = A2Y(hangle, hradius);
238
239 #if HAVE_GETTIMEOFDAY
240         gettimeofday(&current, 0);
241         fraction = ((double) current.tv_usec / 1.0e6);
242 #endif
243         sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
244         sdx = A2X(sangle, sradius);
245         sdy = A2Y(sangle, sradius);
246
247         dline(3, cx, cy, cx + mdx, cy - mdy, '#');
248
249         (void) attrset(A_REVERSE);
250         dline(2, cx, cy, cx + hdx, cy - hdy, '.');
251         attroff(A_REVERSE);
252
253         if (has_colors())
254             (void) attrset(AttrArg(COLOR_PAIR(1), 0));
255
256         dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
257
258         if (has_colors())
259             (void) attrset(AttrArg(COLOR_PAIR(0), 0));
260
261         text = ctime(&tim);
262         MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
263         refresh();
264         if ((t->tm_sec % 5) == 0
265             && t->tm_sec != lastbeep) {
266             lastbeep = t->tm_sec;
267             if (has_colors()) {
268                 odd = !odd;
269                 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
270             }
271             beep();
272         }
273
274         if ((ch = getch()) != ERR) {
275 #ifdef KEY_RESIZE
276             if (ch == KEY_RESIZE) {
277                 flash();
278                 erase();
279                 wrefresh(curscr);
280                 goto restart;
281             }
282 #endif
283             break;
284         }
285
286         dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
287         dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
288         dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
289
290     }
291
292     stop_curses();
293     ExitProgram(EXIT_SUCCESS);
294 }
295 #else
296 int
297 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
298 {
299     printf("This program requires the header math.h and trignometric functions\n");
300     ExitProgram(EXIT_FAILURE);
301 }
302 #endif