summaryrefslogtreecommitdiff
path: root/aclock.c
blob: d7db4caffca7cb670d71d993dfd13f6f738ab4e7 (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
/*
 * 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 <curses.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

void
catch(int sig)
{
	struct	winsize ws;

	switch (sig) {
	case SIGWINCH:
		if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
			resizeterm(ws.ws_row, ws.ws_col);
		clear();
		break;
	default:
		break;
	}
}

void
settimer(int sec)
{
	struct	itimerval itv;

	itv.it_value.tv_sec = sec;
	itv.it_value.tv_usec = 0;
	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;
	char    c;

	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;
		c = ".o"[!(r%5)];
		mvaddch(y, x, c);
	}
}

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);
	}
}


int
main(void)
{
	char    INFO[] = "Copyright (c) 2002 by Antek Sawicki <tenox@tenox.tc>\n"
	"Version 1.8; Dublin, June 2002\n";
	char    digital_time[15];
	int     FontHW = 2;
	int     sXmax, sYmax, smax, hand_max, sXcen, sYcen;
	time_t  t;
	struct tm *ltime;

	signal(SIGWINCH, catch);
	signal(SIGALRM, catch);
	settimer(1);
	initscr();

	for (;;) {
		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;

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

		draw_hand((ltime->tm_hour * 5) + (ltime->tm_min / 10),
		    2 * hand_max / 3, '#', sXcen, sYcen, FontHW);
		draw_hand(ltime->tm_min, hand_max - 2, '*', sXcen, sYcen, FontHW);
		draw_hand(ltime->tm_sec, hand_max - 1, '.', sXcen, sYcen, FontHW);

		mvaddstr(sYmax / 4, sXcen - 5, ".:ACLOCK:.");
		mvprintw(4 * sYmax / 5, sXcen - 5, "[%02d:%02d:%02d]",
		    ltime->tm_hour, ltime->tm_min, ltime->tm_sec);

		refresh();
		pause();
	}
	endwin();

	return 0;
}