HAMMER Utilities: Sync with 60F
[dragonfly.git] / contrib / ncurses-5.4 / test / tclock.c
blobc3cce6d34f927d3331cef1c1850b9048b11e775e
1 /* $Id: tclock.c,v 1.24 2002/12/29 01:40:30 tom Exp $ */
3 #include <test.priv.h>
5 #include <math.h>
7 #if TIME_WITH_SYS_TIME
8 # include <sys/time.h>
9 # include <time.h>
10 #else
11 # if HAVE_SYS_TIME_H
12 # include <sys/time.h>
13 # else
14 # include <time.h>
15 # endif
16 #endif
19 tclock - analog/digital clock for curses.
20 If it gives you joy, then
21 (a) I'm glad
22 (b) you need to get out more :-)
24 This program is copyright Howard Jones, September 1994
25 (ha.jones@ic.ac.uk). It may be freely distributed as
26 long as this copyright message remains intact, and any
27 modifications are clearly marked as such. [In fact, if
28 you modify it, I wouldn't mind the modifications back,
29 especially if they add any nice features. A good one
30 would be a precalc table for the 60 hand positions, so
31 that the floating point stuff can be ditched. As I said,
32 it was a 20 hackup minute job.]
34 COMING SOON: tfishtank. Be the envy of your mac-owning
35 colleagues.
38 /* To compile: cc -o tclock tclock.c -lcurses -lm */
40 #ifndef PI
41 #define PI 3.141592654
42 #endif
44 #define sign(_x) (_x<0?-1:1)
46 #define ASPECT 2.2
47 #define ROUND(value) ((int)((value) + 0.5))
49 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
50 #define A2Y(angle,radius) ROUND(radius * cos(angle))
52 /* Plot a point */
53 static void
54 plot(int x, int y, char col)
56 mvaddch(y, x, (chtype) col);
59 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
60 static void
61 dline(int pair, int from_x, int from_y, int x2, int y2, char ch)
63 int dx, dy;
64 int ax, ay;
65 int sx, sy;
66 int x, y;
67 int d;
69 if (has_colors())
70 attrset(COLOR_PAIR(pair));
72 dx = x2 - from_x;
73 dy = y2 - from_y;
75 ax = abs(dx * 2);
76 ay = abs(dy * 2);
78 sx = sign(dx);
79 sy = sign(dy);
81 x = from_x;
82 y = from_y;
84 if (ax > ay) {
85 d = ay - (ax / 2);
87 while (1) {
88 plot(x, y, ch);
89 if (x == x2)
90 return;
92 if (d >= 0) {
93 y += sy;
94 d -= ax;
96 x += sx;
97 d += ay;
99 } else {
100 d = ax - (ay / 2);
102 while (1) {
103 plot(x, y, ch);
104 if (y == y2)
105 return;
107 if (d >= 0) {
108 x += sx;
109 d -= ay;
111 y += sy;
112 d += ax;
118 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
120 int i, cx, cy;
121 double cr, mradius, hradius, mangle, hangle;
122 double sangle, sradius, hours;
123 int hdx, hdy;
124 int mdx, mdy;
125 int sdx, sdy;
126 int ch;
127 int lastbeep = -1;
128 bool odd = FALSE;
129 time_t tim;
130 struct tm *t;
131 char szChar[10];
132 char *text;
133 int my_bg = COLOR_BLACK;
134 #if HAVE_GETTIMEOFDAY
135 struct timeval current;
136 double fraction = 0.0;
137 #endif
139 setlocale(LC_ALL, "");
141 initscr();
142 noecho();
143 cbreak();
144 nodelay(stdscr, TRUE);
145 curs_set(0);
147 if (has_colors()) {
148 start_color();
149 #if HAVE_USE_DEFAULT_COLORS
150 if (use_default_colors() == OK)
151 my_bg = -1;
152 #endif
153 init_pair(1, COLOR_RED, my_bg);
154 init_pair(2, COLOR_MAGENTA, my_bg);
155 init_pair(3, COLOR_GREEN, my_bg);
156 init_pair(4, COLOR_WHITE, COLOR_BLUE);
158 #ifdef KEY_RESIZE
159 keypad(stdscr, TRUE);
160 restart:
161 #endif
162 cx = (COLS - 1) / 2; /* 39 */
163 cy = LINES / 2; /* 12 */
164 if (cx / ASPECT < cy)
165 cr = cx / ASPECT;
166 else
167 cr = cy;
168 sradius = (5 * cr) / 6; /* 10 */
169 mradius = (3 * cr) / 4; /* 9 */
170 hradius = cr / 2; /* 6 */
172 for (i = 0; i < 12; i++) {
173 sangle = (i + 1) * (2.0 * PI) / 12.0;
174 sdx = A2X(sangle, sradius);
175 sdy = A2Y(sangle, sradius);
176 sprintf(szChar, "%d", i + 1);
178 mvaddstr(cy - sdy, cx + sdx, szChar);
181 mvaddstr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
183 sradius = (4 * sradius) / 5;
184 for (;;) {
185 napms(100);
187 tim = time(0);
188 t = localtime(&tim);
190 hours = (t->tm_hour + (t->tm_min / 60.0));
191 if (hours > 12.0)
192 hours -= 12.0;
194 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
195 mdx = A2X(mangle, mradius);
196 mdy = A2Y(mangle, mradius);
198 hangle = ((hours) * (2.0 * PI) / 12.0);
199 hdx = A2X(hangle, hradius);
200 hdy = A2Y(hangle, hradius);
202 #if HAVE_GETTIMEOFDAY
203 gettimeofday(&current, 0);
204 fraction = (current.tv_usec / 1.0e6);
205 #endif
206 sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
207 sdx = A2X(sangle, sradius);
208 sdy = A2Y(sangle, sradius);
210 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
212 attrset(A_REVERSE);
213 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
214 attroff(A_REVERSE);
216 if (has_colors())
217 attrset(COLOR_PAIR(1));
219 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
221 if (has_colors())
222 attrset(COLOR_PAIR(0));
224 text = ctime(&tim);
225 mvprintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
226 refresh();
227 if ((t->tm_sec % 5) == 0
228 && t->tm_sec != lastbeep) {
229 lastbeep = t->tm_sec;
230 if (has_colors()) {
231 odd = !odd;
232 bkgd(odd ? COLOR_PAIR(4) : COLOR_PAIR(0));
234 beep();
237 if ((ch = getch()) != ERR) {
238 #ifdef KEY_RESIZE
239 if (ch == KEY_RESIZE) {
240 flash();
241 erase();
242 wrefresh(curscr);
243 goto restart;
245 #endif
246 break;
249 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
250 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
251 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
255 curs_set(1);
256 endwin();
257 ExitProgram(EXIT_SUCCESS);