1 /* $Id: tclock.c,v 1.22 2002/06/29 23:34:13 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 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;
131 int my_bg = COLOR_BLACK;
132 #if HAVE_GETTIMEOFDAY
133 struct timeval current;
134 double fraction = 0.0;
137 setlocale(LC_ALL, "");
142 nodelay(stdscr, TRUE);
147 #if HAVE_USE_DEFAULT_COLORS
148 if (use_default_colors() == OK)
151 init_pair(1, COLOR_RED, my_bg);
152 init_pair(2, COLOR_MAGENTA, my_bg);
153 init_pair(3, COLOR_GREEN, my_bg);
156 keypad(stdscr, TRUE);
159 cx = (COLS - 1) / 2; /* 39 */
160 cy = LINES / 2; /* 12 */
161 if (cx / ASPECT < cy)
165 sradius = (5 * cr) / 6; /* 10 */
166 mradius = (3 * cr) / 4; /* 9 */
167 hradius = cr / 2; /* 6 */
169 for (i = 0; i < 12; i++) {
170 sangle = (i + 1) * (2.0 * PI) / 12.0;
171 sdx = A2X(sangle, sradius);
172 sdy = A2Y(sangle, sradius);
173 sprintf(szChar, "%d", i + 1);
175 mvaddstr(cy - sdy, cx + sdx, szChar);
178 mvaddstr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
180 sradius = (4 * sradius) / 5;
187 hours = (t->tm_hour + (t->tm_min / 60.0));
191 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
192 mdx = A2X(mangle, mradius);
193 mdy = A2Y(mangle, mradius);
195 hangle = ((hours) * (2.0 * PI) / 12.0);
196 hdx = A2X(hangle, hradius);
197 hdy = A2Y(hangle, hradius);
199 #if HAVE_GETTIMEOFDAY
200 gettimeofday(¤t, 0);
201 fraction = (current.tv_usec / 1.0e6);
203 sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
204 sdx = A2X(sangle, sradius);
205 sdy = A2Y(sangle, sradius);
207 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
210 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
214 attrset(COLOR_PAIR(1));
216 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
219 attrset(COLOR_PAIR(0));
221 mvaddstr(LINES - 2, 0, ctime(&tim));
223 if ((t->tm_sec % 5) == 0
224 && t->tm_sec != lastbeep) {
225 lastbeep = t->tm_sec;
229 if ((ch = getch()) != ERR) {
231 if (ch == KEY_RESIZE) {
241 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
242 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
243 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
249 ExitProgram(EXIT_SUCCESS);