]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/clock.c
merge
[plan9front.git] / sys / src / cmd / clock.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5
6 Image *hrhand, *minhand;
7 Image *dots, *back;
8
9 Point
10 circlept(Point c, int r, int degrees)
11 {
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;
17 }
18
19 void
20 redraw(Image *screen)
21 {
22         static int tm, ntm;
23         static Rectangle r;
24         static Point c;
25         static int rad;
26         static Image *im;
27         int i;
28         int anghr, angmin;
29         static Tm tms;
30         static Tm ntms;
31
32         ntm = time(0);
33         if(ntm == tm && eqrect(screen->r, r))
34                 return;
35
36         ntms = *localtime(ntm);
37         anghr = 90-(ntms.hour*5 + ntms.min/12)*6;
38         angmin = 90-ntms.min*6;
39         tm = ntm;
40         tms = ntms;
41         r = screen->r;
42         c = divpt(addpt(r.min, r.max), 2);
43         rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r);
44         rad /= 2;
45         rad -= 8;
46
47         draw(screen, screen->r, back, nil, ZP);
48         for(i=0; i<12; i++)
49                 fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP);
50
51         line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP);
52         line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP);
53
54         flushimage(display, 1);
55 }
56
57 void
58 eresized(int new)
59 {
60         if(new && getwindow(display, Refnone) < 0)
61                 fprint(2,"can't reattach to window");
62         redraw(screen);
63 }
64
65 void
66 main(int, char**)
67 {
68         Event e;
69         Mouse m;
70         Menu menu;
71         char *mstr[] = {"exit", 0};
72         int key, timer;
73         int t;
74
75         if (initdraw(0, 0, "clock") < 0)
76                 sysfatal("initdraw failed");
77         back = allocimagemix(display, DPalebluegreen, DWhite);
78
79         hrhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkblue);
80         minhand = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPaleblue);
81         dots = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
82         redraw(screen);
83
84         einit(Emouse);
85         t = (30*1000);
86         timer = etimer(0, t);
87
88         menu.item = mstr;
89         menu.lasthit = 0;
90         for(;;) {
91                 key = event(&e);
92                 if(key == Emouse) {
93                         m = e.mouse;
94                         if(m.buttons & 4) {
95                                 if(emenuhit(3, &m, &menu) == 0)
96                                         exits(0);
97                         }
98                 } else if(key == timer) {
99                         redraw(screen);
100                 }
101         }       
102 }