1 /* $Id: tclock.c,v 1.43 2022/07/27 19:23:01 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]"
117 #if HAVE_USE_DEFAULT_COLORS
118 ," -d invoke use_default_colors"
123 for (n = 0; n < SIZEOF(msg); n++)
124 fprintf(stderr, "%s\n", msg[n]);
126 ExitProgram(EXIT_FAILURE);
130 main(int argc, char *argv[])
133 double cr, mradius, hradius, mangle, hangle;
134 double sangle, sradius, hours;
145 short my_bg = COLOR_BLACK;
146 #if HAVE_GETTIMEOFDAY
147 struct timeval current;
149 double fraction = 0.0;
150 #if HAVE_USE_DEFAULT_COLORS
151 bool d_option = FALSE;
154 while ((ch = getopt(argc, argv, "d")) != -1) {
156 #if HAVE_USE_DEFAULT_COLORS
169 setlocale(LC_ALL, "");
174 nodelay(stdscr, TRUE);
179 #if HAVE_USE_DEFAULT_COLORS
180 if (d_option && (use_default_colors() == OK))
183 init_pair(1, COLOR_RED, my_bg);
184 init_pair(2, COLOR_MAGENTA, my_bg);
185 init_pair(3, COLOR_GREEN, my_bg);
186 init_pair(4, COLOR_WHITE, COLOR_BLUE);
189 keypad(stdscr, TRUE);
192 cx = (COLS - 1) / 2; /* 39 */
193 cy = LINES / 2; /* 12 */
194 if (cx / ASPECT < cy)
198 sradius = (5 * cr) / 6; /* 10 */
199 mradius = (3 * cr) / 4; /* 9 */
200 hradius = cr / 2; /* 6 */
202 for (i = 0; i < 12; i++) {
203 sangle = (i + 1) * (2.0 * PI) / 12.0;
204 sdx = A2X(sangle, sradius);
205 sdy = A2Y(sangle, sradius);
206 _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
208 MvAddStr(cy - sdy, cx + sdx, szChar);
211 MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
213 sradius = (4 * sradius) / 5;
220 hours = (t->tm_hour + (t->tm_min / 60.0));
224 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
225 mdx = A2X(mangle, mradius);
226 mdy = A2Y(mangle, mradius);
228 hangle = ((hours) * (2.0 * PI) / 12.0);
229 hdx = A2X(hangle, hradius);
230 hdy = A2Y(hangle, hradius);
232 #if HAVE_GETTIMEOFDAY
233 gettimeofday(¤t, 0);
234 fraction = ((double) current.tv_usec / 1.0e6);
236 sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
237 sdx = A2X(sangle, sradius);
238 sdy = A2Y(sangle, sradius);
240 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
242 (void) attrset(A_REVERSE);
243 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
247 (void) attrset(AttrArg(COLOR_PAIR(1), 0));
249 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
252 (void) attrset(AttrArg(COLOR_PAIR(0), 0));
255 MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
257 if ((t->tm_sec % 5) == 0
258 && t->tm_sec != lastbeep) {
259 lastbeep = t->tm_sec;
262 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
267 if ((ch = getch()) != ERR) {
269 if (ch == KEY_RESIZE) {
279 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
280 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
281 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
286 ExitProgram(EXIT_SUCCESS);
290 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
292 printf("This program requires the header math.h and trignometric functions\n");
293 ExitProgram(EXIT_FAILURE);