]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/tclock.c
ncurses 6.0 - patch 20160528
[ncurses.git] / test / tclock.c
1 /* $Id: tclock.c,v 1.34 2014/08/02 16:37:03 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 int
120 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
121 {
122     int i, cx, cy;
123     double cr, mradius, hradius, mangle, hangle;
124     double sangle, sradius, hours;
125     int hdx, hdy;
126     int mdx, mdy;
127     int sdx, sdy;
128     int ch;
129     int lastbeep = -1;
130     bool odd = FALSE;
131     time_t tim;
132     struct tm *t;
133     char szChar[10];
134     char *text;
135     short my_bg = COLOR_BLACK;
136 #if HAVE_GETTIMEOFDAY
137     struct timeval current;
138 #endif
139     double fraction = 0.0;
140
141     setlocale(LC_ALL, "");
142
143     initscr();
144     noecho();
145     cbreak();
146     nodelay(stdscr, TRUE);
147     curs_set(0);
148
149     if (has_colors()) {
150         start_color();
151 #if HAVE_USE_DEFAULT_COLORS
152         if (use_default_colors() == OK)
153             my_bg = -1;
154 #endif
155         init_pair(1, COLOR_RED, my_bg);
156         init_pair(2, COLOR_MAGENTA, my_bg);
157         init_pair(3, COLOR_GREEN, my_bg);
158         init_pair(4, COLOR_WHITE, COLOR_BLUE);
159     }
160 #ifdef KEY_RESIZE
161     keypad(stdscr, TRUE);
162   restart:
163 #endif
164     cx = (COLS - 1) / 2;        /* 39 */
165     cy = LINES / 2;             /* 12 */
166     if (cx / ASPECT < cy)
167         cr = cx / ASPECT;
168     else
169         cr = cy;
170     sradius = (5 * cr) / 6;     /* 10 */
171     mradius = (3 * cr) / 4;     /* 9 */
172     hradius = cr / 2;           /* 6 */
173
174     for (i = 0; i < 12; i++) {
175         sangle = (i + 1) * (2.0 * PI) / 12.0;
176         sdx = A2X(sangle, sradius);
177         sdy = A2Y(sangle, sradius);
178         sprintf(szChar, "%d", i + 1);
179
180         MvAddStr(cy - sdy, cx + sdx, szChar);
181     }
182
183     MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
184
185     sradius = (4 * sradius) / 5;
186     for (;;) {
187         napms(100);
188
189         tim = time(0);
190         t = localtime(&tim);
191
192         hours = (t->tm_hour + (t->tm_min / 60.0));
193         if (hours > 12.0)
194             hours -= 12.0;
195
196         mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
197         mdx = A2X(mangle, mradius);
198         mdy = A2Y(mangle, mradius);
199
200         hangle = ((hours) * (2.0 * PI) / 12.0);
201         hdx = A2X(hangle, hradius);
202         hdy = A2Y(hangle, hradius);
203
204 #if HAVE_GETTIMEOFDAY
205         gettimeofday(&current, 0);
206         fraction = ((double) current.tv_usec / 1.0e6);
207 #endif
208         sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
209         sdx = A2X(sangle, sradius);
210         sdy = A2Y(sangle, sradius);
211
212         dline(3, cx, cy, cx + mdx, cy - mdy, '#');
213
214         (void) attrset(A_REVERSE);
215         dline(2, cx, cy, cx + hdx, cy - hdy, '.');
216         attroff(A_REVERSE);
217
218         if (has_colors())
219             (void) attrset(AttrArg(COLOR_PAIR(1), 0));
220
221         dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
222
223         if (has_colors())
224             (void) attrset(AttrArg(COLOR_PAIR(0), 0));
225
226         text = ctime(&tim);
227         MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
228         refresh();
229         if ((t->tm_sec % 5) == 0
230             && t->tm_sec != lastbeep) {
231             lastbeep = t->tm_sec;
232             if (has_colors()) {
233                 odd = !odd;
234                 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
235             }
236             beep();
237         }
238
239         if ((ch = getch()) != ERR) {
240 #ifdef KEY_RESIZE
241             if (ch == KEY_RESIZE) {
242                 flash();
243                 erase();
244                 wrefresh(curscr);
245                 goto restart;
246             }
247 #endif
248             break;
249         }
250
251         dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
252         dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
253         dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
254
255     }
256
257     curs_set(1);
258     endwin();
259     ExitProgram(EXIT_SUCCESS);
260 }
261 #else
262 int
263 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
264 {
265     printf("This program requires the development header math.h\n");
266     ExitProgram(EXIT_FAILURE);
267 }
268 #endif