aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@swtch.com>2020-01-13 16:53:30 -0500
committerRuss Cox <rsc@swtch.com>2020-01-13 16:53:30 -0500
commitac6456a0cc67ca33c7b9c64cf7a4598961de8911 (patch)
tree0a4a6c2f5ad81cf00c97cf0106532e9ba04559af
parentbe263a722e8957d99f2a51bb11106103bf878b44 (diff)
downloadplan9port-ac6456a0cc67ca33c7b9c64cf7a4598961de8911.tar.gz
plan9port-ac6456a0cc67ca33c7b9c64cf7a4598961de8911.tar.bz2
plan9port-ac6456a0cc67ca33c7b9c64cf7a4598961de8911.zip
clock: import from plan 9
Fixes #250.
-rw-r--r--man/man1/date.113
-rw-r--r--src/cmd/draw/clock.c104
2 files changed, 112 insertions, 5 deletions
diff --git a/man/man1/date.1 b/man/man1/date.1
index 1ce96f38..10db8dfe 100644
--- a/man/man1/date.1
+++ b/man/man1/date.1
@@ -1,6 +1,6 @@
.TH DATE 1
.SH NAME
-date \- date and time
+date, clock \- date and time
.SH SYNOPSIS
.B date
[
@@ -8,8 +8,8 @@ date \- date and time
] [
.I seconds
]
-.\" .br
-.\" .B clock
+.br
+.B clock
.SH DESCRIPTION
Print the date, in the format
.PP
@@ -34,6 +34,9 @@ If the optional argument
.I seconds
is present, it is used as the time to convert rather than
the real time.
+.PP
+.I Clock
+draws a simple analog clock in its window.
.\" .SH FILES
.\" .TF /adm/timezone/local
.\" .TP
@@ -54,5 +57,5 @@ the real time.
.\" draws a simple analog clock in its window.
.SH SOURCE
.B \*9/src/cmd/date.c
-.\" .br
-.\" .B \*9/src/cmd/draw/clock.c
+.br
+.B \*9/src/cmd/draw/clock.c
diff --git a/src/cmd/draw/clock.c b/src/cmd/draw/clock.c
new file mode 100644
index 00000000..b804e1f4
--- /dev/null
+++ b/src/cmd/draw/clock.c
@@ -0,0 +1,104 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+
+Image *hrhand, *minhand;
+Image *dots, *back;
+
+Point
+circlept(Point c, int r, int degrees)
+{
+ double rad;
+ rad = (double) degrees * PI/180.0;
+ c.x += cos(rad)*r;
+ c.y -= sin(rad)*r;
+ return c;
+}
+
+void
+redraw(Image *screen)
+{
+ static int tm, ntm;
+ static Rectangle r;
+ static Point c;
+ static int rad;
+ int i;
+ int anghr, angmin;
+ static Tm tms;
+ static Tm ntms;
+
+ ntm = time(0);
+ if(ntm == tm && eqrect(screen->r, r))
+ return;
+
+ ntms = *localtime(ntm);
+ anghr = 90-(ntms.hour*5 + ntms.min/12)*6;
+ angmin = 90-ntms.min*6;
+ tm = ntm;
+ tms = ntms;
+ r = screen->r;
+ c = divpt(addpt(r.min, r.max), 2);
+ rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r);
+ rad /= 2;
+ rad -= 8;
+
+ draw(screen, screen->r, back, nil, ZP);
+ for(i=0; i<12; i++)
+ fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP);
+
+ line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP);
+ line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP);
+
+ flushimage(display, 1);
+}
+
+void
+eresized(int new)
+{
+ if(new && getwindow(display, Refnone) < 0)
+ fprint(2,"can't reattach to window");
+ redraw(screen);
+}
+
+void
+main(int argc, char **argv)
+{
+ Event e;
+ Mouse m;
+ Menu menu;
+ char *mstr[] = {"exit", 0};
+ int key, timer;
+ int t;
+
+ USED(argc);
+ USED(argv);
+
+ if (initdraw(0, 0, "clock") < 0)
+ sysfatal("initdraw failed");
+ back = allocimagemix(display, DPalebluegreen, DWhite);
+
+ hrhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkblue);
+ minhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPaleblue);
+ dots = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
+ redraw(screen);
+
+ einit(Emouse);
+ t = (30*1000);
+ timer = etimer(0, t);
+
+ menu.item = mstr;
+ menu.lasthit = 0;
+ for(;;) {
+ key = event(&e);
+ if(key == Emouse) {
+ m = e.mouse;
+ if(m.buttons & 4) {
+ if(emenuhit(3, &m, &menu) == 0)
+ exits(0);
+ }
+ } else if(key == timer) {
+ redraw(screen);
+ }
+ }
+}