updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / plan9us-hg / bclock.c
blob0eb432d41b1552a019ce6076d3b797d8b76c42fd
1 /* Author: Rob DeHart IRC Asaph */
2 /* Home: http://rob.cqtopia.com/plan9/bclock.c */
4 #include <u.h>
5 #include <libc.h>
6 #include <draw.h>
7 #include <event.h>
9 #define MARGIN 4
11 Image *bground, *gray;
13 void draw_block(int x, int y, int on)
15 Image *color;
16 Rectangle rect, bounds;
17 rect = screen->r;
18 int boxX, boxY;
19 boxX = (rect.max.x - rect.min.x - MARGIN * 7) / 6;
20 boxY = (rect.max.y - rect.min.y - MARGIN * 5) / 4;
21 bounds = Rect(MARGIN + rect.min.x + x * (boxX + MARGIN),
22 MARGIN + rect.min.y + y * (boxY + MARGIN),
23 MARGIN + rect.min.x + x * (boxX + MARGIN) + boxX,
24 MARGIN + rect.min.y + y * (boxY + MARGIN) + boxY);
25 if (on)
26 color = display->black;
27 else
28 color = gray;
29 draw(screen, bounds, color, nil, rect.min);
32 void light_cols(int num, int grp)
34 int tens, ones, i;
35 tens = num / 10;
36 ones = num % 10;
37 for (i = 3; i >= 0; --i)
39 if ((grp == 0 && i <= 1) || (grp != 0 && i <= 2))
40 draw_block(grp * 2, 3 - i, tens >> i & 1);
41 draw_block(grp * 2 + 1, 3 - i, ones >> i & 1);
46 void
47 redraw(Image *screen)
49 static int tm, ntm;
50 static Rectangle r;
51 static Tm tms;
52 static Tm ntms;
54 ntm = time(0);
55 if(ntm == tm && eqrect(screen->r, r))
56 return;
58 ntms = *localtime(ntm);
59 tm = ntm;
60 tms = ntms;
61 r = screen->r;
63 draw(screen, screen->r, bground, nil, ZP);
64 light_cols(ntms.hour, 0);
65 light_cols(ntms.min, 1);
66 light_cols(ntms.sec, 2);
68 flushimage(display, 1);
71 void
72 eresized(int new)
74 if(new && getwindow(display, Refnone) < 0)
75 fprint(2,"can't reattach to window");
76 redraw(screen);
79 #ifdef PLAN9PORT
80 void
81 usage(void)
83 fprint(2, "usage: bclock [-W winsize]\n");
84 exits("usage");
86 #endif
88 void
89 main(int argc, char *argv[])
91 Event e;
92 Mouse m;
93 Menu menu;
94 char *mstr[] = {"exit", 0};
95 int key, timer;
97 #ifdef PLAN9PORT
98 ARGBEGIN{
99 case 'W':
100 winsize = EARGF(usage());
101 break;
102 default:
103 usage();
104 }ARGEND
105 #endif
107 initdraw(0,0,"bclock");
108 bground = allocimagemix(display, DPalebluegreen, DWhite);
109 gray = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, 0xDDDDDDFF);
111 redraw(screen);
113 einit(Emouse);
114 timer = etimer(0, 1000);
116 menu.item = mstr;
117 menu.lasthit = 0;
118 for(;;) {
119 key = event(&e);
120 if(key == Emouse) {
121 m = e.mouse;
122 if(m.buttons & 4) {
123 if(emenuhit(3, &m, &menu) == 0)
124 exits(0);
126 } else if(key == timer) {
127 redraw(screen);