summaryrefslogtreecommitdiff
path: root/aclock.c
blob: e89c4e60a6f9e70e8e6dd26f8d63f1359fcafc8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* $Id$ */
/*
 * aclock - ascii clock for UNIX Console
 *
 * Copyright (c) 2002 Antoni Sawicki <tenox@tenox.tc>
 * Version 1.8 (unix-curses); Dublin, June 2002
 *
 * Copmpilation: cc aclock.c -o aclock -lcurses -lm
 *
 */

#include <sys/ioctl.h>
#include <sys/time.h>
#include <curses.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <time.h>

int	die = 0;
int	redraw = 0;
enum	{RED = 1, CYAN, BLUE, GREEN};

char *strtime(char *, size_t, struct tm *);
void sevenseg(char [3][80], size_t, char *);

void
catch(int sig)
{
	switch (sig) {
	case SIGWINCH:
		redraw = 1;
		break;
	case SIGINT:
		die = 1;
		break;
	default:
		break;
	}
}

void
settimer(int sec, int usec)
{
	struct	itimerval itv;

	itv.it_value.tv_sec = sec;
	itv.it_value.tv_usec = usec;
	itv.it_interval = itv.it_value;

	setitimer(ITIMER_REAL, &itv, NULL);
}

void
draw_circle(int hand_max, int sYcen, int sXcen, int FontHW)
{
	int     x, y, r;

	for (r = 0; r < 60; r++) {
		x = cos(r * M_PI / 180 * 6) * hand_max * FontHW + sXcen;
		y = sin(r * M_PI / 180 * 6) * hand_max + sYcen;
		if (r % 5) {
			attron(COLOR_PAIR(CYAN));
			mvaddch(y, x, '.');
			attroff(COLOR_PAIR(CYAN));
		} else {
			attron(COLOR_PAIR(RED));
			mvaddch(y, x, 'o');
			attroff(COLOR_PAIR(RED));
		}
	}
}

void
draw_hand(int minute, int hlenght, char c, int sXcen, int sYcen, int FontHW)
{
	int     x, y, n;
	float   r = (minute - 15) * (M_PI / 180) * 6;

	for (n = 1; n < hlenght; n++) {
		x = cos(r) * n * FontHW + sXcen;
		y = sin(r) * n + sYcen;
		mvaddch(y, x, c);
	}
}

const char INFO[] = "Copyright (c) 2002 by Antek Sawicki <tenox@tenox.tc>\n"
	"Version 1.8; Dublin, June 2002\n";

int
main(int argc, char **argv)
{
	char    digital_time[32], stime[80], dtime[3][80];
	int	i, ch;
	int	dflag = 0, sflag = 0;
	int     FontHW = 2;
	int     sXmax, sYmax, smax, hand_max, sXcen, sYcen;
	time_t  t;
	struct	tm *ltime;
	struct	winsize ws;
	struct	timeval tv;
	int	hascolors;

	while ((ch = getopt(argc, argv, "ds")) != -1) {
		switch (ch) {
		case 'd':
			dflag = 1;
			break;
		case 's':
			sflag = 1;
			break;
		default:
			break;
		}
	}

	argc -= optind;
	argv += optind;

	signal(SIGWINCH, catch);
	signal(SIGALRM, catch);
	signal(SIGINT, catch);

	initscr();
	curs_set(0);

	if ((hascolors = has_colors()))
		start_color();

	init_pair(RED, COLOR_RED, COLOR_BLACK);
	init_pair(CYAN, COLOR_CYAN, COLOR_BLACK);
	init_pair(BLUE, COLOR_BLUE, COLOR_BLACK);
	init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);

	while (!die) {
		gettimeofday(&tv, NULL);
		settimer(0, 1000000 - tv.tv_usec);

		if (redraw && ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) {
			resizeterm(ws.ws_row, ws.ws_col);
			clear();
			redraw = 0;
		}

		time(&t);
		ltime = localtime(&t);
		sXmax = COLS;
		sYmax = LINES;

		if (sXmax / 2 <= sYmax)
			smax = sXmax / 2;
		else
			smax = sYmax;

		hand_max = (smax / 2) - 1;

		sXcen = sXmax / 2;
		sYcen = sYmax / 2 - 1;

		erase();
		draw_circle(hand_max, sYcen, sXcen, FontHW);

		if (dflag) {
			attron(COLOR_PAIR(GREEN));
			strftime(digital_time, sizeof(digital_time), "%d-%m-%y", ltime);
			sevenseg(dtime, sizeof(dtime[0]), digital_time);
			for (i = 0; i < 3; i++)
				mvprintw(i + sYcen - sYmax / 4 - 1,
				    sXcen - strlen(dtime[i]) / 2, dtime[i]);
			strftime(digital_time, sizeof(digital_time), "%H:%M:%S", ltime);
			sevenseg(dtime, sizeof(dtime[0]), digital_time);
			for (i = 0; i < 3; i++)
				mvprintw(i + sYcen + sYmax / 4 - 1,
				    sXcen - strlen(dtime[i]) / 2, dtime[i]);
			attroff(COLOR_PAIR(GREEN));
		}

		if (hascolors)
			attron(A_BOLD);

		attron(COLOR_PAIR(RED));
		draw_hand((ltime->tm_hour * 5) + (ltime->tm_min / 10),
		    2 * hand_max / 3, '#', sXcen, sYcen, FontHW);
		attroff(COLOR_PAIR(RED));

		attron(COLOR_PAIR(CYAN));
		draw_hand(ltime->tm_min, hand_max - 2, '*', sXcen, sYcen, FontHW);
		attroff(COLOR_PAIR(CYAN));

		attron(COLOR_PAIR(BLUE));
		draw_hand(ltime->tm_sec, hand_max - 1, '.', sXcen, sYcen, FontHW);
		attroff(COLOR_PAIR(BLUE));

		if (sflag) {
			attron(COLOR_PAIR(BLUE));
			strtime(stime, sizeof(stime), ltime);
			mvprintw(sYmax - 1, sXcen - strlen(stime) / 2, stime);
			attroff(COLOR_PAIR(BLUE));
		}

		if (hascolors)
			attroff(A_BOLD);

		refresh();
		pause();
	}
	curs_set(1);
	endwin();

	return 0;
}