-
Notifications
You must be signed in to change notification settings - Fork 41
/
xeyes.rs
315 lines (271 loc) · 10.5 KB
/
xeyes.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
// This is (quite loosely) based on the original xeyes
extern crate x11rb;
use x11rb::xcb_ffi::XCBConnection;
use x11rb::connection::Connection;
use x11rb::x11_utils::Event;
use x11rb::generated::xproto::*;
use x11rb::generated::shape::{self, ConnectionExt as _};
use x11rb::wrapper::ConnectionExt as _;
use x11rb::errors::ConnectionError;
const PUPIL_SIZE: i16 = 50;
const EYE_SIZE: i16 = 50;
// Draw the big background of the eyes
fn draw_eyes<C: Connection>(conn: &C, win_id: WINDOW, black: GCONTEXT, white: GCONTEXT,
window_size: (u16, u16))
-> Result<(), ConnectionError>
{
// Draw the black outlines
let mut arc1 = Arc {
x: 0,
y: 0,
width: window_size.0 / 2,
height: window_size.1,
angle1: 0*64,
angle2: 360*64
};
let mut arc2 = arc1.clone();
arc2.x = arc2.width as i16;
conn.poly_fill_arc(win_id, black, &[arc1, arc2])?;
// Draw the white inner part
for mut arc in [&mut arc1, &mut arc2].iter_mut() {
arc.x += EYE_SIZE;
arc.y += EYE_SIZE;
arc.width -= 2 * EYE_SIZE as u16;
arc.height -= 2 * EYE_SIZE as u16;
}
conn.poly_fill_arc(win_id, white, &[arc1, arc2])?;
Ok(())
}
// Draw the pupils inside the eye
fn draw_pupils<C: Connection>(conn: &C, win_id: WINDOW, gc: GCONTEXT,
((x1, y1), (x2, y2)): ((i16, i16), (i16, i16)))
-> Result<(), ConnectionError>
{
// Transform center to top left corner
let (x1, y1) = (x1 - PUPIL_SIZE / 2, y1 - PUPIL_SIZE / 2);
let (x2, y2) = (x2 - PUPIL_SIZE / 2, y2 - PUPIL_SIZE / 2);
let arc1 = Arc {
x: x1,
y: y1,
width: PUPIL_SIZE as _,
height: PUPIL_SIZE as _,
angle1: 90 * 64,
angle2: 360*64
};
let mut arc2 = arc1.clone();
arc2.x = x2;
arc2.y = y2;
// Do the drawing
conn.poly_fill_arc(win_id, gc, &[arc1, arc2])?;
Ok(())
}
// Given two points, return their squared distance
fn distance_squared(p1: (f64, f64), p2: (f64, f64)) -> f64 {
let dx = p1.0 - p2.0;
let dy = p1.1 - p2.1;
dx * dx + dy * dy
}
// Compute the position of a pupil inside of the given area.
fn compute_pupil(area: (i16, i16, i16, i16), mouse: (i16, i16)) -> (i16, i16)
{
// What is the center of the eye?
let center_x = area.0 + area.2 / 2;
let center_y = area.1 + area.3 / 2;
let (w, h) = (area.2 as f64 / 2.0, area.3 as f64 / 2.0);
// Is the mouse exactly on the center?
if (center_x, center_y) == mouse {
return mouse;
}
let center = (center_x as f64, center_y as f64);
let mouse = (mouse.0 as f64, mouse.1 as f64);
// Calculate the offset of the mouse position from the center
let diff = (mouse.0 as f64 - center.0, mouse.1 as f64 - center.1);
// An eclipse is described by this equation, where the angle 'a' is varied over all values, but
// does not actually describe the angle from the center due to the different scaling in x and y
// direction.
//
// x = w * cos(a)
// y = h * sin(a)
//
// With tan(a) = sin(a)/cos(a), we get
//
// tan(a) * x = w * sin(a) => sin(a) = tan(a) * x / w
// y = h * sin(a) => sin(a) = y / h
//
// and thus
//
// tan(a) * x / w = y / h
//
// which we can rearrange to
//
// tan(a) = (y * w) / (x * h)
//
// And thus, the angle we are looking for is:
//
// a = arctan((y * w) / (x * h))
//
// However, due to tan() being the way it is, we actually need:
let a = (diff.1 * w).atan2(diff.0 * h);
// Now compute the corresponding point on the ellipse (relative to the center)
let (cx, cy) = (w * a.cos(), h * a.sin());
// ...and also compute the actual point
let (x, y) = ((center.0 + cx) as _, (center.1 + cy) as _);
// Return the point that is closer to the center
if distance_squared(center, mouse) < distance_squared(center, (x, y)) {
(mouse.0 as _, mouse.1 as _)
} else {
(x as _, y as _)
}
}
// Compute the position of both pupils.
fn compute_pupils(window_size: (u16, u16), mouse_position: (i16, i16)) -> ((i16, i16), (i16, i16))
{
let border = PUPIL_SIZE + EYE_SIZE;
let half_width = window_size.0 as i16 / 2;
let width = half_width - 2 * border;
let height = window_size.1 as i16 - 2 * border;
(compute_pupil((border, border, width, height), mouse_position),
compute_pupil((border + half_width, border, width, height), mouse_position))
}
struct FreePixmap<'c, C: Connection>(&'c C, PIXMAP);
impl<C: Connection> Drop for FreePixmap<'_, C> {
fn drop(&mut self) {
self.0.free_pixmap(self.1).unwrap();
}
}
struct FreeGC<'c, C: Connection>(&'c C, GCONTEXT);
impl<C: Connection> Drop for FreeGC<'_, C> {
fn drop(&mut self) {
self.0.free_gc(self.1).unwrap();
}
}
fn create_pixmap_wrapper<'c, C: Connection>(conn: &'c C, depth: u8, drawable: DRAWABLE, size: (u16, u16))
-> Result<FreePixmap<'c, C>, ConnectionError>
{
let pixmap = conn.generate_id();
conn.create_pixmap(depth, pixmap, drawable, size.0, size.1)?;
Ok(FreePixmap(conn, pixmap))
}
fn shape_window<C: Connection>(conn: &C, win_id: WINDOW, window_size: (u16, u16))
-> Result<(), ConnectionError>
{
// Create a pixmap for the shape
let pixmap = create_pixmap_wrapper(conn, 1, win_id, window_size)?;
// Fill the pixmap with what will indicate "transparent"
let gc = create_gc_with_foreground(conn, pixmap.1, 0)?;
let _free_gc = FreeGC(conn, gc);
let rect = Rectangle {
x: 0,
y: 0,
width: window_size.0,
height: window_size.1
};
conn.poly_fill_rectangle(pixmap.1, gc, &[rect])?;
// Draw the eyes as "not transparent"
let values = ChangeGCAux::new().foreground(1);
conn.change_gc(gc, &values)?;
draw_eyes(conn, pixmap.1, gc, gc, window_size)?;
// Set the shape of the window
conn.mask(shape::SO::Set, shape::SK::Bounding, win_id, 0, 0, pixmap.1)?;
Ok(())
}
fn setup_window<C: Connection>(conn: &C, screen: &Screen, window_size: (u16, u16),
wm_protocols: ATOM, wm_delete_window: ATOM)
-> Result<WINDOW, ConnectionError>
{
let win_id = conn.generate_id();
let win_aux = CreateWindowAux::new()
.event_mask(EventMask::Exposure | EventMask::StructureNotify | EventMask::PointerMotion)
.background_pixel(screen.white_pixel);
conn.create_window(24, win_id, screen.root, 0, 0, window_size.0, window_size.1, 0,
WindowClass::InputOutput, 0, &win_aux)?;
let title = "xeyes";
conn.change_property8(PropMode::Replace, win_id, Atom::WM_NAME.into(), Atom::STRING.into(), title.as_bytes()).unwrap();
conn.change_property32(PropMode::Replace, win_id, wm_protocols, Atom::ATOM.into(), &[wm_delete_window]).unwrap();
conn.map_window(win_id).unwrap();
Ok(win_id)
}
fn create_gc_with_foreground<C: Connection>(conn: &C, win_id: WINDOW, foreground: u32)
-> Result<GCONTEXT, ConnectionError>
{
let gc = conn.generate_id();
let gc_aux = CreateGCAux::new()
.graphics_exposures(0)
.foreground(foreground);
conn.create_gc(gc, win_id, &gc_aux)?;
Ok(gc)
}
fn main() {
let (conn, screen_num) = XCBConnection::connect(None).expect("Failed to connect to the X11 server");
let screen = &conn.setup().roots[screen_num];
let (wm_protocols, wm_delete_window) = {
let protocols = conn.intern_atom(false, "WM_PROTOCOLS".as_bytes()).unwrap();
let delete = conn.intern_atom(false, "WM_DELETE_WINDOW".as_bytes()).unwrap();
(protocols.reply().unwrap().atom, delete.reply().unwrap().atom)
};
let mut window_size = (700, 500);
let has_shape = conn.extension_information(shape::X11_EXTENSION_NAME).is_some();
let win_id = setup_window(&conn, screen, window_size, wm_protocols, wm_delete_window).unwrap();
let mut pixmap = create_pixmap_wrapper(&conn, screen.root_depth, win_id, window_size).unwrap();
let black_gc = create_gc_with_foreground(&conn, win_id, screen.black_pixel).unwrap();
let white_gc = create_gc_with_foreground(&conn, win_id, screen.white_pixel).unwrap();
conn.flush();
let mut need_repaint = false;
let mut need_reshape = false;
let mut mouse_position = (0, 0);
loop {
let event = conn.wait_for_event().unwrap();
let mut event_option = Some(event);
while let Some(event) = event_option {
match event.response_type() {
EXPOSE_EVENT => {
let event = ExposeEvent::from(event);
if event.count == 0 {
need_repaint = true;
}
}
CONFIGURE_NOTIFY_EVENT => {
let event = ConfigureNotifyEvent::from(event);
window_size = (event.width, event.height);
pixmap = create_pixmap_wrapper(&conn, screen.root_depth, win_id,
window_size).unwrap();
need_reshape = true;
}
MOTION_NOTIFY_EVENT => {
let event = MotionNotifyEvent::from(event);
mouse_position = (event.event_x, event.event_y);
need_repaint = true;
}
MAP_NOTIFY_EVENT => {
need_reshape = true;
}
CLIENT_MESSAGE_EVENT => {
let event = ClientMessageEvent::from(event);
let data = event.data.as_data32();
if event.format == 32 && event.window == win_id && data[0] == wm_delete_window {
println!("Window was asked to close");
return;
}
}
0 => { println!("Unknown error {:?}", event); },
_ => { println!("Unknown event {:?}", event); }
}
event_option = conn.poll_for_event().unwrap();
}
if need_reshape && has_shape {
shape_window(&conn, win_id, window_size).unwrap();
need_reshape = false;
}
if need_repaint {
// Draw new pupils
let pos = compute_pupils(window_size, mouse_position);
draw_eyes(&conn, pixmap.1, black_gc, white_gc, window_size).unwrap();
draw_pupils(&conn, pixmap.1, black_gc, pos).unwrap();
// Copy drawing from pixmap to window
conn.copy_area(pixmap.1, win_id, white_gc, 0, 0, 0, 0,
window_size.0, window_size.1).unwrap();
conn.flush();
need_repaint = false;
}
}
}