-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.c
More file actions
172 lines (153 loc) · 5.33 KB
/
Copy pathclock.c
File metadata and controls
172 lines (153 loc) · 5.33 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* clock - analog clock face driven by the system time.
*
* Press Esc or q (or close the window) to quit.
*
* Exercises: XDrawArc / XFillArc, XDrawLine, XCopyArea-based double
* buffering off a pixmap, gettimeofday-paced redraw via select() on
* ConnectionNumber, XSetForeground for swapping hand colors, WM_DELETE_WINDOW.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#define WIN_SIZE 256
#define TICK_MS 100
static void draw_hand(Display *dpy,
Drawable d,
GC gc,
int cx,
int cy,
double angle,
double length)
{
int x = cx + (int) (sin(angle) * length);
int y = cy - (int) (cos(angle) * length);
XDrawLine(dpy, d, gc, cx, cy, x, y);
}
static void render(Display *dpy,
Pixmap buf,
GC bg_gc,
GC face_gc,
GC hour_gc,
GC second_gc,
const struct tm *now)
{
const int cx = WIN_SIZE / 2;
const int cy = WIN_SIZE / 2;
const int r = WIN_SIZE / 2 - 8;
XFillRectangle(dpy, buf, bg_gc, 0, 0, WIN_SIZE, WIN_SIZE);
/* Face outline. XDrawArc spans 360 * 64 sixteenths of a degree. */
XDrawArc(dpy, buf, face_gc, cx - r, cy - r, r * 2, r * 2, 0, 360 * 64);
/* Hour ticks: 12 short radial lines. */
for (int i = 0; i < 12; i++) {
double a = i * (2.0 * M_PI / 12.0);
int x0 = cx + (int) (sin(a) * (r - 8));
int y0 = cy - (int) (cos(a) * (r - 8));
int x1 = cx + (int) (sin(a) * r);
int y1 = cy - (int) (cos(a) * r);
XDrawLine(dpy, buf, face_gc, x0, y0, x1, y1);
}
/* Smooth hand positions: minute hand creeps with seconds, hour hand with
* minutes. */
double s = now->tm_sec;
double m = now->tm_min + s / 60.0;
double h = (now->tm_hour % 12) + m / 60.0;
draw_hand(dpy, buf, hour_gc, cx, cy, h * (2.0 * M_PI / 12.0), r * 0.50);
draw_hand(dpy, buf, hour_gc, cx, cy, m * (2.0 * M_PI / 60.0), r * 0.75);
draw_hand(dpy, buf, second_gc, cx, cy, s * (2.0 * M_PI / 60.0), r * 0.90);
/* Cap dot at the pivot. */
XFillArc(dpy, buf, face_gc, cx - 3, cy - 3, 6, 6, 0, 360 * 64);
}
static void present(Display *dpy, Window win, Pixmap buf, GC blit_gc)
{
XCopyArea(dpy, buf, win, blit_gc, 0, 0, WIN_SIZE, WIN_SIZE, 0, 0);
XFlush(dpy);
}
int main(void)
{
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "XOpenDisplay failed\n");
return 1;
}
int screen = DefaultScreen(dpy);
int depth = DefaultDepth(dpy, screen);
Window root = RootWindow(dpy, screen);
unsigned long bg = WhitePixel(dpy, screen);
unsigned long fg = BlackPixel(dpy, screen);
Window win =
XCreateSimpleWindow(dpy, root, 0, 0, WIN_SIZE, WIN_SIZE, 1, fg, bg);
XStoreName(dpy, win, "clock");
Atom wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &wm_delete, 1);
XSelectInput(dpy, win, ExposureMask | KeyPressMask);
XMapWindow(dpy, win);
Pixmap buf = XCreatePixmap(dpy, win, WIN_SIZE, WIN_SIZE, depth);
/* Allocate a red pixel for the second hand. */
Colormap cmap = DefaultColormap(dpy, screen);
XColor red = {.red = 0xE000, .green = 0x2000, .blue = 0x2000,
.flags = DoRed | DoGreen | DoBlue};
XAllocColor(dpy, cmap, &red);
XGCValues gcv;
gcv.foreground = bg;
GC bg_gc = XCreateGC(dpy, buf, GCForeground, &gcv);
gcv.foreground = fg;
GC face_gc = XCreateGC(dpy, buf, GCForeground, &gcv);
gcv.foreground = fg;
GC hour_gc = XCreateGC(dpy, buf, GCForeground, &gcv);
XSetLineAttributes(dpy, hour_gc, 3, LineSolid, CapRound, JoinRound);
gcv.foreground = red.pixel;
GC second_gc = XCreateGC(dpy, buf, GCForeground, &gcv);
GC blit_gc = XCreateGC(dpy, win, 0, NULL);
int x_fd = ConnectionNumber(dpy);
int last_sec = -1;
for (;;) {
while (XPending(dpy)) {
XEvent e;
XNextEvent(dpy, &e);
switch (e.type) {
case Expose:
if (e.xexpose.count == 0)
present(dpy, win, buf, blit_gc);
break;
case KeyPress: {
KeySym ks = XLookupKeysym(&e.xkey, 0);
if (ks == XK_Escape || ks == XK_q || ks == XK_Q)
goto done;
} break;
case ClientMessage:
if ((Atom) e.xclient.data.l[0] == wm_delete)
goto done;
break;
}
}
time_t now_t = time(NULL);
struct tm now;
localtime_r(&now_t, &now);
if (now.tm_sec != last_sec) {
render(dpy, buf, bg_gc, face_gc, hour_gc, second_gc, &now);
present(dpy, win, buf, blit_gc);
last_sec = now.tm_sec;
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(x_fd, &rfds);
struct timeval tv = {0, TICK_MS * 1000};
select(x_fd + 1, &rfds, NULL, NULL, &tv);
}
done:
XFreeGC(dpy, blit_gc);
XFreeGC(dpy, second_gc);
XFreeGC(dpy, hour_gc);
XFreeGC(dpy, face_gc);
XFreeGC(dpy, bg_gc);
XFreePixmap(dpy, buf);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}