missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / tclock.c
blobfb44725d3209c1244914cda330e0a4ed248f0d82
1 /* $Id: tclock.c,v 1.30 2011/03/22 09:16:00 tom Exp $ */
3 #include <test.priv.h>
5 #if HAVE_MATH_H
7 #include <math.h>
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
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 :-)
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.]
36 COMING SOON: tfishtank. Be the envy of your mac-owning
37 colleagues.
40 /* To compile: cc -o tclock tclock.c -lcurses -lm */
42 #ifndef PI
43 #define PI 3.141592654
44 #endif
46 #define sign(_x) (_x<0?-1:1)
48 #define ASPECT 2.2
49 #define ROUND(value) ((int)((value) + 0.5))
51 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
52 #define A2Y(angle,radius) ROUND(radius * cos(angle))
54 /* Plot a point */
55 static void
56 plot(int x, int y, char col)
58 MvAddCh(y, x, (chtype) col);
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, char ch)
65 int dx, dy;
66 int ax, ay;
67 int sx, sy;
68 int x, y;
69 int d;
71 if (has_colors())
72 (void) attrset((attr_t) COLOR_PAIR(pair));
74 dx = x2 - from_x;
75 dy = y2 - from_y;
77 ax = abs(dx * 2);
78 ay = abs(dy * 2);
80 sx = sign(dx);
81 sy = sign(dy);
83 x = from_x;
84 y = from_y;
86 if (ax > ay) {
87 d = ay - (ax / 2);
89 while (1) {
90 plot(x, y, ch);
91 if (x == x2)
92 return;
94 if (d >= 0) {
95 y += sy;
96 d -= ax;
98 x += sx;
99 d += ay;
101 } else {
102 d = ax - (ay / 2);
104 while (1) {
105 plot(x, y, ch);
106 if (y == y2)
107 return;
109 if (d >= 0) {
110 x += sx;
111 d -= ay;
113 y += sy;
114 d += ax;
120 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
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 double fraction = 0.0;
139 #endif
141 setlocale(LC_ALL, "");
143 initscr();
144 noecho();
145 cbreak();
146 nodelay(stdscr, TRUE);
147 curs_set(0);
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);
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 */
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);
180 MvAddStr(cy - sdy, cx + sdx, szChar);
183 MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
185 sradius = (4 * sradius) / 5;
186 for (;;) {
187 napms(100);
189 tim = time(0);
190 t = localtime(&tim);
192 hours = (t->tm_hour + (t->tm_min / 60.0));
193 if (hours > 12.0)
194 hours -= 12.0;
196 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
197 mdx = A2X(mangle, mradius);
198 mdy = A2Y(mangle, mradius);
200 hangle = ((hours) * (2.0 * PI) / 12.0);
201 hdx = A2X(hangle, hradius);
202 hdy = A2Y(hangle, hradius);
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);
212 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
214 (void) attrset(A_REVERSE);
215 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
216 attroff(A_REVERSE);
218 if (has_colors())
219 (void) attrset(COLOR_PAIR(1));
221 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
223 if (has_colors())
224 (void) attrset(COLOR_PAIR(0));
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)));
236 beep();
239 if ((ch = getch()) != ERR) {
240 #ifdef KEY_RESIZE
241 if (ch == KEY_RESIZE) {
242 flash();
243 erase();
244 wrefresh(curscr);
245 goto restart;
247 #endif
248 break;
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, ' ');
257 curs_set(1);
258 endwin();
259 ExitProgram(EXIT_SUCCESS);
261 #else
263 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
265 printf("This program requires the development header math.h\n");
266 ExitProgram(EXIT_FAILURE);
268 #endif