-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathxclock_utc.rs
315 lines (281 loc) · 9.28 KB
/
xclock_utc.rs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::convert::TryFrom;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use x11rb::atom_manager;
use x11rb::connection::Connection;
use x11rb::errors::{ConnectionError, ReplyOrIdError};
use x11rb::protocol::xproto::*;
use x11rb::protocol::Event;
use x11rb::rust_connection::RustConnection;
use x11rb::wrapper::ConnectionExt as _;
atom_manager! {
pub Atoms: AtomsCookie {
UTF8_STRING,
WM_DELETE_WINDOW,
WM_PROTOCOLS,
_NET_WM_NAME,
}
}
fn create_window(
conn: &impl Connection,
screen: &Screen,
atoms: &Atoms,
(width, height): (u16, u16),
) -> Result<Window, ReplyOrIdError> {
let win_id = conn.generate_id()?;
let win_aux =
CreateWindowAux::new().event_mask(EventMask::Exposure | EventMask::StructureNotify);
conn.create_window(
screen.root_depth,
win_id,
screen.root,
0,
0,
width,
height,
0,
WindowClass::InputOutput,
0,
&win_aux,
)?;
let title = "xclock";
conn.change_property8(
PropMode::Replace,
win_id,
AtomEnum::WM_NAME,
AtomEnum::STRING,
title.as_bytes(),
)?;
conn.change_property8(
PropMode::Replace,
win_id,
atoms._NET_WM_NAME,
atoms.UTF8_STRING,
title.as_bytes(),
)?;
conn.change_property32(
PropMode::Replace,
win_id,
atoms.WM_PROTOCOLS,
AtomEnum::ATOM,
&[atoms.WM_DELETE_WINDOW],
)?;
conn.map_window(win_id)?;
Ok(win_id)
}
fn redraw(
conn: &impl Connection,
screen: &Screen,
win_id: Window,
gc_id: Gcontext,
(width, height): (u16, u16),
) -> Result<(), ConnectionError> {
let (hour, minute, second) = get_time();
let center = ((width as f32) / 2.0, (height as f32) / 2.0);
let size = (width.min(height) as f32) / 2.0;
// Transform a value between 0 and 60 to a position on the clock (relative to the center)
let minute_to_outer_position = |minute: f32| {
let angle = (30.0 - minute) * 2.0 * std::f32::consts::PI / 60.0;
let (sin, cos) = angle.sin_cos();
(size * sin, size * cos)
};
// Create a line segment
fn create_line(center: (f32, f32), from: (f32, f32), to: (f32, f32)) -> Segment {
Segment {
x1: (center.0 + from.0).round() as _,
y1: (center.1 + from.1).round() as _,
x2: (center.0 + to.0).round() as _,
y2: (center.1 + to.1).round() as _,
}
}
// Draw the background
conn.change_gc(gc_id, &ChangeGCAux::new().foreground(screen.white_pixel))?;
conn.poly_fill_rectangle(
win_id,
gc_id,
&[Rectangle {
x: 0,
y: 0,
width,
height,
}],
)?;
conn.change_gc(gc_id, &ChangeGCAux::new().foreground(screen.black_pixel))?;
// Get a list of lines for the clock's face
let mut lines = (0..60)
.map(|minute| {
let outer = minute_to_outer_position(minute as _);
let length_factor = if minute % 5 == 0 { 0.8 } else { 0.9 };
create_line(
center,
outer,
(outer.0 * length_factor, outer.1 * length_factor),
)
})
.collect::<Vec<_>>();
// ... and also the hand for seconds
lines.push(create_line(
center,
(0.0, 0.0),
minute_to_outer_position(second as _),
));
// Draw everything
conn.poly_segment(win_id, gc_id, &lines)?;
// Now draw the hands
let point = |pos: (f32, f32), factor: f32| Point {
x: (center.0 + pos.0 * factor).round() as _,
y: (center.1 + pos.1 * factor).round() as _,
};
let hour_to_60 = (hour % 12) as f32 * 60.0 / 12.0;
for &(position, hand_length, hand_width) in
&[(hour_to_60, 0.6, 0.08), (minute as f32, 0.8, 0.05)]
{
let outer = minute_to_outer_position(position);
let ortho1 = (outer.1, -outer.0);
let ortho2 = (-outer.1, outer.0);
let opposite = (-outer.0, -outer.1);
let polygon = [
point(ortho1, hand_width),
point(opposite, hand_width),
point(ortho2, hand_width),
point(outer, hand_length),
];
conn.fill_poly(
win_id,
gc_id,
PolyShape::Complex,
CoordMode::Origin,
&polygon,
)?;
}
Ok(())
}
#[cfg(unix)]
fn poll_with_timeout(
conn: &RustConnection,
timeout: Duration,
) -> Result<(), Box<dyn std::error::Error>> {
use std::os::raw::c_int;
use std::os::unix::io::AsRawFd;
use nix::poll::{poll, PollFd, PollFlags};
let start_instant = Instant::now();
let fd = conn.stream().as_raw_fd();
let mut poll_fds = [PollFd::new(fd, PollFlags::POLLIN)];
loop {
let timeout_millis = timeout
.checked_sub(start_instant.elapsed())
.map(|remaining| c_int::try_from(remaining.as_millis()).unwrap_or(c_int::max_value()))
.unwrap_or(0);
match poll(&mut poll_fds, timeout_millis) {
Ok(_) => {
if poll_fds[0]
.revents()
.unwrap_or_else(PollFlags::empty)
.contains(PollFlags::POLLIN)
{
break;
}
}
// try again
Err(nix::Error::Sys(nix::errno::Errno::EINTR)) => {}
Err(e) => return Err(e.into()),
}
if start_instant.elapsed() >= timeout {
break;
}
}
// We do not really care about the result of poll. Either there was a timeout, in which case we
// try to handle events (there are none) and then redraw. Or there was an event, in which case
// we handle it and then still redraw.
Ok(())
}
#[cfg(windows)]
fn poll_with_timeout(
conn: &RustConnection,
timeout: Duration,
) -> Result<(), Box<dyn std::error::Error>> {
use std::os::windows::io::AsRawSocket;
use winapi::shared::minwindef::INT;
use winapi::um::winsock2::{POLLRDNORM, SOCKET, WSAPOLLFD};
use winapi_wsapoll::wsa_poll;
let start_instant = Instant::now();
let raw_socket = conn.stream().as_raw_socket();
let mut poll_fds = [WSAPOLLFD {
fd: raw_socket as SOCKET,
events: POLLRDNORM,
revents: 0,
}];
loop {
let timeout_millis = timeout
.checked_sub(start_instant.elapsed())
.map(|remaining| INT::try_from(remaining.as_millis()).unwrap_or(INT::max_value()))
.unwrap_or(0);
match wsa_poll(&mut poll_fds, timeout_millis) {
Ok(_) => {
if (poll_fds[0].revents & POLLRDNORM) != 0 {
break;
}
}
Err(e) => return Err(e.into()),
}
if start_instant.elapsed() >= timeout {
break;
}
}
// We do not really care about the result of poll. Either there was a timeout, in which case we
// try to handle events (there are none) and then redraw. Or there was an event, in which case
// we handle it and then still redraw.
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let (conn, screen_num) = RustConnection::connect(None)?;
// The following is only needed for start_timeout_thread(), which is used for 'tests'
let conn1 = std::sync::Arc::new(conn);
let conn = &*conn1;
let screen = &conn.setup().roots[screen_num];
let atoms = Atoms::new(conn)?.reply()?;
let (mut width, mut height) = (100, 100);
let win_id = create_window(conn, &screen, &atoms, (width, height))?;
let gc_id = conn.generate_id().unwrap();
conn.create_gc(gc_id, win_id, &CreateGCAux::default())?;
util::start_timeout_thread(conn1.clone(), win_id);
conn.flush()?;
loop {
poll_with_timeout(conn, Duration::from_millis(1_000))?;
while let Some(event) = conn.poll_for_event()? {
println!("{:?})", event);
match event {
Event::ConfigureNotify(event) => {
width = event.width;
height = event.height;
}
Event::ClientMessage(event) => {
let data = event.data.as_data32();
if event.format == 32
&& event.window == win_id
&& data[0] == atoms.WM_DELETE_WINDOW
{
println!("Window was asked to close");
return Ok(());
}
}
Event::Error(_) => println!("Got an unexpected error"),
_ => println!("Got an unknown event"),
}
}
redraw(conn, &screen, win_id, gc_id, (width, height))?;
conn.flush()?;
}
}
/// Get the current time as (hour, minute, second)
fn get_time() -> (u8, u8, u8) {
let total_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let (second, total_minutes) = (total_secs % 60, total_secs / 60);
let (minute, total_hours) = (total_minutes % 60, total_minutes / 60);
let hour = total_hours % 24;
// This is in UTC. Getting local time is complicated and not important for us.
(hour as _, minute as _, second as _)
}
include!("integration_test_util/util.rs");