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