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