1 /* $Id: tclock.c,v 1.47 2022/12/10 23:23:27 tom Exp $ */
6 #if HAVE_MATH_H && HAVE_MATH_FUNCS
11 tclock - analog/digital clock for curses.
12 If it gives you joy, then
14 (b) you need to get out more :-)
16 This program is copyright Howard Jones, September 1994
17 (ha.jones@ic.ac.uk). It may be freely distributed as
18 long as this copyright message remains intact, and any
19 modifications are clearly marked as such. [In fact, if
20 you modify it, I wouldn't mind the modifications back,
21 especially if they add any nice features. A good one
22 would be a precalc table for the 60 hand positions, so
23 that the floating point stuff can be ditched. As I said,
24 it was a 20 hackup minute job.]
26 COMING SOON: tfishtank. Be the envy of your mac-owning
30 /* To compile: cc -o tclock tclock.c -lcurses -lm */
33 #define PI 3.141592654
36 #define sign(_x) (_x<0?-1:1)
39 #define ROUND(value) ((int)((value) + 0.5))
41 #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
42 #define A2Y(angle,radius) ROUND(radius * cos(angle))
46 plot(int x, int y, int col)
48 MvAddCh(y, x, (chtype) col);
51 /* Draw a diagonal(arbitrary) line using Bresenham's algorithm. */
53 dline(int pair, int from_x, int from_y, int x2, int y2, int ch)
62 (void) attrset(AttrArg(COLOR_PAIR(pair), 0));
112 static const char *msg[] =
114 "Usage: tclock [options]"
118 #if HAVE_USE_DEFAULT_COLORS
119 ," -d invoke use_default_colors"
124 for (n = 0; n < SIZEOF(msg); n++)
125 fprintf(stderr, "%s\n", msg[n]);
127 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
134 main(int argc, char *argv[])
137 double cr, mradius, hradius, mangle, hangle;
138 double sangle, sradius, hours;
149 short my_bg = COLOR_BLACK;
150 #if HAVE_GETTIMEOFDAY
151 struct timeval current;
153 double fraction = 0.0;
154 #if HAVE_USE_DEFAULT_COLORS
155 bool d_option = FALSE;
158 while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
160 #if HAVE_USE_DEFAULT_COLORS
167 ExitProgram(EXIT_SUCCESS);
169 usage(ch == OPTS_USAGE);
176 setlocale(LC_ALL, "");
181 nodelay(stdscr, TRUE);
186 #if HAVE_USE_DEFAULT_COLORS
187 if (d_option && (use_default_colors() == OK))
190 init_pair(1, COLOR_RED, my_bg);
191 init_pair(2, COLOR_MAGENTA, my_bg);
192 init_pair(3, COLOR_GREEN, my_bg);
193 init_pair(4, COLOR_WHITE, COLOR_BLUE);
196 keypad(stdscr, TRUE);
199 cx = (COLS - 1) / 2; /* 39 */
200 cy = LINES / 2; /* 12 */
201 if (cx / ASPECT < cy)
205 sradius = (5 * cr) / 6; /* 10 */
206 mradius = (3 * cr) / 4; /* 9 */
207 hradius = cr / 2; /* 6 */
209 for (i = 0; i < 12; i++) {
210 sangle = (i + 1) * (2.0 * PI) / 12.0;
211 sdx = A2X(sangle, sradius);
212 sdy = A2Y(sangle, sradius);
213 _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
215 MvAddStr(cy - sdy, cx + sdx, szChar);
218 MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
220 sradius = (4 * sradius) / 5;
227 hours = (t->tm_hour + (t->tm_min / 60.0));
231 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
232 mdx = A2X(mangle, mradius);
233 mdy = A2Y(mangle, mradius);
235 hangle = ((hours) * (2.0 * PI) / 12.0);
236 hdx = A2X(hangle, hradius);
237 hdy = A2Y(hangle, hradius);
239 #if HAVE_GETTIMEOFDAY
240 gettimeofday(¤t, 0);
241 fraction = ((double) current.tv_usec / 1.0e6);
243 sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
244 sdx = A2X(sangle, sradius);
245 sdy = A2Y(sangle, sradius);
247 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
249 (void) attrset(A_REVERSE);
250 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
254 (void) attrset(AttrArg(COLOR_PAIR(1), 0));
256 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
259 (void) attrset(AttrArg(COLOR_PAIR(0), 0));
262 MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
264 if ((t->tm_sec % 5) == 0
265 && t->tm_sec != lastbeep) {
266 lastbeep = t->tm_sec;
269 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
274 if ((ch = getch()) != ERR) {
276 if (ch == KEY_RESIZE) {
286 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
287 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
288 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
293 ExitProgram(EXIT_SUCCESS);
299 printf("This program requires the header math.h and trignometric functions\n");
300 ExitProgram(EXIT_FAILURE);