plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

clock.c (1910B)


      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 argc, char **argv)
     64 {
     65 	Event e;
     66 	Mouse m;
     67 	Menu menu;
     68 	char *mstr[] = {"exit", 0};
     69 	int key, timer;
     70 	int t;
     71 
     72 	USED(argc);
     73 	USED(argv);
     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 }