/
deskcal.c
116 lines (98 loc) · 2 KB
/
deskcal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
enum
{
Etimer = Ekeyboard << 1,
};
char *strwdays[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
char strmday[2+1];
char strclock[2+1+2+1];
Font *mdayfont;
Font *wdayfont;
Font *clockfont;
Tm *now;
int mday;
Font*
eopenfont(Display *d, char *name)
{
Font *f;
f = openfont(d, name);
if (f == nil) {
fprint(2, "fatal: cannot open font %s: %r\n", name);
exits("font error");
}
return f;
}
void
update(void)
{
now = localtime(time(0));
if (!mday || mday != now->mday) {
mday = now->mday;
sprint(strmday, "%d", mday);
}
sprint(strclock, "%02d:%02d", now->hour, now->min);
}
void
eresized(int new)
{
Point p, q;
if (new && getwindow(display, Refnone) <0) {
sysfatal("can't reattach to window: %r");
}
draw(screen, screen->r, display->white, nil, ZP);
p = stringsize(wdayfont, strwdays[now->wday]);
q.y = 0;
q.x = (screen->r.max.x - p.x) / 2;
string(screen, q, display->black, ZP, wdayfont, strwdays[now->wday]);
q.y = p.y;
p = stringsize(mdayfont, strmday);
q.x = (screen->r.max.x - p.x) / 2;
string(screen, q, display->black, ZP, mdayfont, strmday);
q.y += p.y;
p = stringsize(clockfont, strclock);
q.x = (screen->r.max.x - p.x) / 2;
string(screen, q, display->black, ZP, clockfont, strclock);
flushimage(display, 1);
}
void
main()
{
Event e;
if (initdraw(0, nil, "deskcal") <0) {
sysfatal("initdraw: %r");
}
wdayfont = eopenfont(display, "/lib/font/bit/lucsans/latin1.8.font");
mdayfont = eopenfont(display, "/lib/font/bit/lucsans/boldunicode.13.font");
clockfont = eopenfont(display, "/lib/font/bit/pelm/latin1.8.font");
if (etimer(Etimer, 900) == 0) {
fprint(2, "could not initialize timer: %r\n");
exits("system error");
}
update();
eresized(0);
einit(Emouse|Ekeyboard|Etimer);
for (;;) {
switch (event(&e)) {
case Etimer:
update();
eresized(0);
break;
case Ekeyboard:
if (e.kbdc == 0x7F) {
exits(nil);
}
break;
}
}
}