1 /* $Id: tclock.c,v 1.33 2013/09/28 21:52:34 tom Exp $ */
10 # include <sys/time.h>
14 # include <sys/time.h>
21 tclock - analog/digital clock for curses.
22 If it gives you joy, then
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
40 /* To compile: cc -o tclock tclock.c -lcurses -lm */
43 #define PI 3.141592654
46 #define sign(_x) (_x<0?-1:1)
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))
56 plot(int x, int y, int col)
58 MvAddCh(y, x, (chtype) col);
61 /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
63 dline(int pair, int from_x, int from_y, int x2, int y2, int ch)
72 (void) attrset((attr_t) COLOR_PAIR(pair));
120 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
123 double cr, mradius, hradius, mangle, hangle;
124 double sangle, sradius, hours;
135 short my_bg = COLOR_BLACK;
136 #if HAVE_GETTIMEOFDAY
137 struct timeval current;
139 double fraction = 0.0;
141 setlocale(LC_ALL, "");
146 nodelay(stdscr, TRUE);
151 #if HAVE_USE_DEFAULT_COLORS
152 if (use_default_colors() == OK)
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);
161 keypad(stdscr, TRUE);
164 cx = (COLS - 1) / 2; /* 39 */
165 cy = LINES / 2; /* 12 */
166 if (cx / ASPECT < 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;
192 hours = (t->tm_hour + (t->tm_min / 60.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(¤t, 0);
206 fraction = ((double) current.tv_usec / 1.0e6);
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, '.');
219 (void) attrset((attr_t) COLOR_PAIR(1));
221 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
224 (void) attrset((attr_t) COLOR_PAIR(0));
227 MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
229 if ((t->tm_sec % 5) == 0
230 && t->tm_sec != lastbeep) {
231 lastbeep = t->tm_sec;
234 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
239 if ((ch = getch()) != ERR) {
241 if (ch == KEY_RESIZE) {
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, ' ');
259 ExitProgram(EXIT_SUCCESS);
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);