summaryrefslogtreecommitdiff
path: root/appl/cmd/lego/clock.b
blob: 3b3c3e5057876e8b40a32b4775452db918d5ef8a (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
213
214
implement Clock;

include "sys.m";
	sys: Sys;

include "draw.m";
	draw: Draw;
	Point, Rect: import draw;

include "math.m";
	math: Math;
	sqrt, atan2, hypot, Degree: import math;

include "tk.m";
	tk: Tk;
	top: ref Tk->Toplevel;

include "tkclient.m";
	tkclient: Tkclient;

Clock: module {
	init:	fn(ctxt: ref Draw->Context, argl: list of string);
};

cmds := array[] of {
	"bind . <Configure> {send win resize}",
	"canvas .face -height 200 -width 200 -bg yellow",
	"bind .face <ButtonPress> {send ptr %x %y}",
	"bind .face <ButtonRelease> {send ptr release}",
	"pack .face -expand yes -fill both",
	"button .reset -text Reset -command {send win reset}",
	"pack .reset -after .Wm_t.title -side right -fill y",
	"pack propagate . no",
};

init(ctxt: ref Draw->Context, nil: list of string)
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	math = load Math Math->PATH;
	tk = load Tk Tk->PATH;
	tkclient = load Tkclient Tkclient->PATH;
	tkclient->init();

	sys->pctl(Sys->NEWPGRP, nil);

	clockface := sys->open("/chan/clockface", Sys->ORDWR);
	if (clockface == nil) {
		sys->print("open /chan/clockface failed: %r\n");
		raise "fail:clockface";
	}
	tock := chan of string;
	spawn readme(clockface, tock);

	titlech: chan of string;
	(top, titlech) = tkclient->toplevel(ctxt, "hh:mm", "", Tkclient->Appl);
	win := chan of string;
	ptr := chan of string;
	tk->namechan(top, win, "win");
	tk->namechan(top, ptr, "ptr");
	for(i:=0; i<len cmds; i++)
		tk->cmd(top, cmds[i]);
	tkclient->onscreen(top, nil);
	tkclient->startinput(top, "ptr"::nil);
	drawface();
	spawn hands(ptr, clockface);

	for (;;) alt {
	s := <-top.ctxt.kbd =>
		tk->keyboard(top, s);
	s := <-top.ctxt.ptr =>
		tk->pointer(top, *s);
	s := <-top.ctxt.ctl or
	s = <-top.wreq or
	s = <-titlech =>
		tkclient->wmctl(top, s);
	msg := <-win =>
		case msg {
		"resize" =>	drawface();
		"reset" =>		sys->fprint(clockface, "reset");
		}
	nowis := <-tock =>
		(n, toks) := sys->tokenize(nowis, ":");
		if (n == 2) {
			(hour, minute) = (int hd toks, int hd tl toks);
			setclock();
		}
	}
}

readme(fd: ref Sys->FD, ch: chan of string)
{
	buf := array[64] of byte;
	while ((n := sys->read(fd, buf, len buf)) > 0) {
		if (buf[n-1] == byte '\n')
			n--;
		ch <-= string buf[:n];
	}
	ch <-= "99:99";
}

hour, minute: int;
center, focus: Point;
major: int;

Frim:	con .98;
Fminute:	con .90;
Fhour:	con .45;
Fnub:	con .05;

hands(ptr: chan of string, fd: ref Sys->FD)
{
	for (;;) {
		pos := <-ptr;
		p := s2p(pos);
		hand := "";
		if (elinside(p, Fnub))
			hand = nil;
		else if (elinside(p, Fhour))
			hand = "hour";
		else if (elinside(p, Fminute))
			hand = "minute";

		do {
			p = s2p(pos).sub(center);
			angle := int (atan2(real -p.y, real p.x) / Degree);
			if (hand != nil)
				tkc(".face itemconfigure "+hand+" -start "+string angle+"; update");
			case hand {
			"hour" =>		hour = ((360+90-angle) / 30) % 12;
			"minute" =>	minute = ((360+90-angle) / 6) % 60;
			}
		} while ((pos = <-ptr) != "release");
		if (hand != nil)
			sys->fprint(fd, "%d:%d\n", hour, minute);
	}
}

drawface()
{
	elparms();
	tkc(sys->sprint(".face configure -scrollregion {0 0 %d %d}", 2*center.x, 2*center.y));
	tkc(".face delete all");
	tkc(".face create oval "+elrect(Frim)+" -fill fuchsia -outline aqua -width 2");
	for (a := 0; a < 360; a += 30)
		tkc(".face create arc "+elrect(Frim)+" -fill aqua -outline aqua -width 2 -extent 1 -start "+string a);
	tkc(".face create oval "+elrect(Fminute)+" -fill fuchsia -outline fuchsia");
	tkc(".face create oval "+elrect(Fnub)+" -fill aqua -outline aqua");
	tkc(".face create arc "+elrect(Fhour)+" -fill aqua -outline aqua -width 6 -extent 1 -tags hour");
	tkc(".face create arc "+elrect(Fminute)+" -fill aqua -outline aqua -width 2 -extent 1 -tags minute");
	setclock();
}

setclock()
{
	tkc(".face itemconfigure hour -start "+string (90 - 30*(hour%12) - minute/2));
	tkc(".face itemconfigure minute -start "+string (90 - 6*minute));
	tkc(sys->sprint(".Wm_t.title configure -text {%d:%.2d}", (hour+11)%12+1, minute));
	tkc("update");
}

elparms()
{
	center = (int tkc(".face cget actwidth") / 2, int tkc(".face cget actheight") / 2);
	dist := center.x*center.x - center.y*center.y;
	if (dist > 0) {
		major = 2 * center.x;
		focus = (int sqrt(real dist), 0);
	} else {
		major = 2 * center.y;
		focus = (0, int sqrt(real -dist));
	}
}

elinside(p: Point, frac: real): int
{
	foc := mulf(focus, frac);
	d := dist(p, center.add(foc)) + dist(p, center.sub(foc));
	return (d < frac * real major);
}

elrect(frac: real): string
{
	inset := mulf(center, 1.-frac);
	r := Rect(inset, center.mul(2).sub(inset));
	return sys->sprint("%d %d %d %d", r.min.x, r.min.y, r.max.x, r.max.y);
}

mulf(p: Point, f: real): Point
{
	return (int (f * real p.x), int (f * real p.y));
}

dist(p, q: Point): real
{
	p = p.sub(q);
	return hypot(real p.x, real p.y);
}

s2p(s: string): Point
{
	(nil, xy) := sys->tokenize(s, " ");
	if (len xy != 2)
		return (0, 0);
	return (int hd xy, int hd tl xy);
}

tkc(msg: string): string
{
	ret := tk->cmd(top, msg);
	if (ret != nil && ret[0] == '!')
		sys->print("tk error? %s → %s\n", msg, ret);
	return ret;
}