1 /* $Id: tclock.c,v 1.38 2017/09/09 00:37:06 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(AttrArg(COLOR_PAIR(pair), 0));
122 static const char *msg[] =
124 "Usage: tclock [options]"
127 #if HAVE_USE_DEFAULT_COLORS
128 ," -d invoke use_default_colors"
133 for (n = 0; n < SIZEOF(msg); n++)
134 fprintf(stderr, "%s\n", msg[n]);
136 ExitProgram(EXIT_FAILURE);
140 main(int argc, char *argv[])
143 double cr, mradius, hradius, mangle, hangle;
144 double sangle, sradius, hours;
155 short my_bg = COLOR_BLACK;
156 #if HAVE_GETTIMEOFDAY
157 struct timeval current;
159 double fraction = 0.0;
160 #if HAVE_USE_DEFAULT_COLORS
161 bool d_option = FALSE;
164 while ((ch = getopt(argc, argv, "d")) != -1) {
166 #if HAVE_USE_DEFAULT_COLORS
179 setlocale(LC_ALL, "");
184 nodelay(stdscr, TRUE);
189 #if HAVE_USE_DEFAULT_COLORS
190 if (d_option && (use_default_colors() == OK))
193 init_pair(1, COLOR_RED, my_bg);
194 init_pair(2, COLOR_MAGENTA, my_bg);
195 init_pair(3, COLOR_GREEN, my_bg);
196 init_pair(4, COLOR_WHITE, COLOR_BLUE);
199 keypad(stdscr, TRUE);
202 cx = (COLS - 1) / 2; /* 39 */
203 cy = LINES / 2; /* 12 */
204 if (cx / ASPECT < cy)
208 sradius = (5 * cr) / 6; /* 10 */
209 mradius = (3 * cr) / 4; /* 9 */
210 hradius = cr / 2; /* 6 */
212 for (i = 0; i < 12; i++) {
213 sangle = (i + 1) * (2.0 * PI) / 12.0;
214 sdx = A2X(sangle, sradius);
215 sdy = A2Y(sangle, sradius);
216 _nc_SPRINTF(szChar, _nc_SLIMIT(sizeof(szChar)) "%d", i + 1);
218 MvAddStr(cy - sdy, cx + sdx, szChar);
221 MvAddStr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
223 sradius = (4 * sradius) / 5;
230 hours = (t->tm_hour + (t->tm_min / 60.0));
234 mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
235 mdx = A2X(mangle, mradius);
236 mdy = A2Y(mangle, mradius);
238 hangle = ((hours) * (2.0 * PI) / 12.0);
239 hdx = A2X(hangle, hradius);
240 hdy = A2Y(hangle, hradius);
242 #if HAVE_GETTIMEOFDAY
243 gettimeofday(¤t, 0);
244 fraction = ((double) current.tv_usec / 1.0e6);
246 sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
247 sdx = A2X(sangle, sradius);
248 sdy = A2Y(sangle, sradius);
250 dline(3, cx, cy, cx + mdx, cy - mdy, '#');
252 (void) attrset(A_REVERSE);
253 dline(2, cx, cy, cx + hdx, cy - hdy, '.');
257 (void) attrset(AttrArg(COLOR_PAIR(1), 0));
259 dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
262 (void) attrset(AttrArg(COLOR_PAIR(0), 0));
265 MvPrintw(2, 0, "%.*s", (int) (strlen(text) - 1), text);
267 if ((t->tm_sec % 5) == 0
268 && t->tm_sec != lastbeep) {
269 lastbeep = t->tm_sec;
272 bkgd((chtype) (odd ? COLOR_PAIR(4) : COLOR_PAIR(0)));
277 if ((ch = getch()) != ERR) {
279 if (ch == KEY_RESIZE) {
289 dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
290 dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
291 dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
296 ExitProgram(EXIT_SUCCESS);
300 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
302 printf("This program requires the development header math.h\n");
303 ExitProgram(EXIT_FAILURE);