1 /* $Id: tclock.c,v 1.48 2023/02/25 16:42:22 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[])
136 static TimeType initial;
139 double cr, mradius, hradius, mangle, hangle;
140 double sangle, sradius, hours;
151 short my_bg = COLOR_BLACK;
153 #if HAVE_USE_DEFAULT_COLORS
154 bool d_option = FALSE;
157 while ((ch = getopt(argc, argv, OPTS_COMMON "d")) != -1) {
159 #if HAVE_USE_DEFAULT_COLORS
166 ExitProgram(EXIT_SUCCESS);
168 usage(ch == OPTS_USAGE);
175 setlocale(LC_ALL, "");
180 nodelay(stdscr, TRUE);
185 #if HAVE_USE_DEFAULT_COLORS
186 if (d_option && (use_default_colors() == OK))
189 init_pair(1, COLOR_RED, my_bg);
190 init_pair(2, COLOR_MAGENTA, my_bg);
191 init_pair(3, COLOR_GREEN, my_bg);
192 init_pair(4, COLOR_WHITE, COLOR_BLUE);
195 keypad(stdscr, TRUE);
198 cx = (COLS - 1) / 2; /* 39 */
199 cy = LINES / 2; /* 12 */
200 if (cx / ASPECT < cy)
204 sradius = (5 * cr) / 6; /* 10 */
205 mradius = (3 * cr) / 4; /* 9 */
206 hradius = cr / 2; /* 6 */
208 for (i = 0; i < 12; i++) {
209 sangle = (i + 1) * (2.0 * PI) / 12.0;
210 sdx = A2X(sangle, sradius);
211 sdy = A2Y(sangle, sradius);
212 _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
214 MvAddStr(cy - sdy, cx + sdx, szChar);
217 MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
219 sradius = (4 * sradius) / 5;
226 hours = (t->tm_hour + (t->tm_min / 60.0));
230 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
231 mdx = A2X(mangle, mradius);
232 mdy = A2Y(mangle, mradius);
234 hangle = ((hours) * (2.0 * PI) / 12.0);
235 hdx = A2X(hangle, hradius);
236 hdy = A2Y(hangle, hradius);
238 GetClockTime(¤t);
240 sangle = (ElapsedSeconds(&initial, ¤t) * (2.0 * PI) / 60.0);
241 sdx = A2X(sangle, sradius);
242 sdy = A2Y(sangle, sradius);
244 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
246 (void) attrset(A_REVERSE);
247 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
251 (void) attrset(AttrArg(COLOR_PAIR(1), 0));
253 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
256 (void) attrset(AttrArg(COLOR_PAIR(0), 0));
259 MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
261 if ((t->tm_sec % 5) == 0
262 && t->tm_sec != lastbeep) {
263 lastbeep = t->tm_sec;
266 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
271 if ((ch = getch()) != ERR) {
273 if (ch == KEY_RESIZE) {
283 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
284 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
285 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
290 ExitProgram(EXIT_SUCCESS);
296 printf("This program requires the header math.h and trignometric functions\n");
297 ExitProgram(EXIT_FAILURE);