updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / plan9us-hg / clock.c
blob48613340672fe022cef8b226e042646da6fb7de4
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
6 Image *hrhand, *minhand;
7 Image *dots, *back;
9 Point
10 circlept(Point c, int r, int degrees)
12 double rad;
13 rad = (double) degrees * PI/180.0;
14 c.x += cos(rad)*r;
15 c.y -= sin(rad)*r;
16 return c;
19 void
20 redraw(Image *screen)
22 static int tm, ntm;
23 static Rectangle r;
24 static Point c;
25 static int rad;
26 int i;
27 int anghr, angmin;
28 static Tm tms;
29 static Tm ntms;
31 ntm = time(0);
32 if(ntm == tm && eqrect(screen->r, r))
33 return;
35 ntms = *localtime(ntm);
36 anghr = 90-(ntms.hour*5 + ntms.min/12)*6;
37 angmin = 90-ntms.min*6;
38 tm = ntm;
39 tms = ntms;
40 r = screen->r;
41 c = divpt(addpt(r.min, r.max), 2);
42 rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r);
43 rad /= 2;
44 rad -= 8;
46 draw(screen, screen->r, back, nil, ZP);
47 for(i=0; i<12; i++)
48 fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP);
50 line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP);
51 line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP);
53 flushimage(display, 1);
56 void
57 eresized(int new)
59 if(new && getwindow(display, Refnone) < 0)
60 fprint(2,"can't reattach to window");
61 redraw(screen);
64 #ifdef PLAN9PORT
65 void
66 usage(void)
68 fprint(2, "usage: clock [-W winsize]\n");
69 exits("usage");
71 #endif
73 void
74 main(int argc, char *argv[])
76 Event e;
77 Mouse m;
78 Menu menu;
79 char *mstr[] = {"exit", 0};
80 int key, timer;
82 #ifdef PLAN9PORT
83 ARGBEGIN{
84 case 'W':
85 winsize = EARGF(usage());
86 break;
87 default:
88 usage();
89 }ARGEND
90 #endif
92 initdraw(0,0,"clock");
93 back = allocimagemix(display, DPalebluegreen, DWhite);
95 hrhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkblue);
96 minhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPaleblue);
97 dots = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
98 redraw(screen);
100 einit(Emouse);
101 timer = etimer(0, 30*1000);
103 menu.item = mstr;
104 menu.lasthit = 0;
105 for(;;) {
106 key = event(&e);
107 if(key == Emouse) {
108 m = e.mouse;
109 if(m.buttons & 4) {
110 if(emenuhit(3, &m, &menu) == 0)
111 exits(0);
113 } else if(key == timer) {
114 redraw(screen);