1 /* $Id: tclock.c,v 1.26 2009/08/29 18:47:26 tom Exp $ */
12 # include <sys/time.h>
19 tclock - analog/digital clock for curses.
20 If it gives you joy, then
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
38 /* To compile: cc -o tclock tclock.c -lcurses -lm */
41 #define PI 3.141592654
44 #define sign(_x) (_x<0?-1:1)
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))
54 plot(int x, int y, char col)
56 mvaddch(y, x, (chtype) col);
59 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
61 dline(int pair, int from_x, int from_y, int x2, int y2, char ch)
70 (void) attrset(COLOR_PAIR(pair));
118 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
121 double cr, mradius, hradius, mangle, hangle;
122 double sangle, sradius, hours;
133 int my_bg = COLOR_BLACK;
134 #if HAVE_GETTIMEOFDAY
135 struct timeval current;
136 double fraction = 0.0;
139 setlocale(LC_ALL, "");
144 nodelay(stdscr, TRUE);
149 #if HAVE_USE_DEFAULT_COLORS
150 if (use_default_colors() == OK)
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);
159 keypad(stdscr, TRUE);
162 cx = (COLS - 1) / 2; /* 39 */
163 cy = LINES / 2; /* 12 */
164 if (cx / ASPECT < 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;
190 hours = (t->tm_hour + (t->tm_min / 60.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(¤t, 0);
204 fraction = (current.tv_usec / 1.0e6);
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 (void) attrset(A_REVERSE);
213 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
217 (void) attrset(COLOR_PAIR(1));
219 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
222 (void) attrset(COLOR_PAIR(0));
225 mvprintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
227 if ((t->tm_sec % 5) == 0
228 && t->tm_sec != lastbeep) {
229 lastbeep = t->tm_sec;
232 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
237 if ((ch = getch()) != ERR) {
239 if (ch == KEY_RESIZE) {
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, ' ');
257 ExitProgram(EXIT_SUCCESS);