mitsuami-linux 1.3.4

GpuSurface on Linux for mitsuami's GTK and Kirigami backends: a Wayland subsurface or an X11 child window, pointer locks and a shortcuts inhibitor
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! X11: a child window of the toolkit's window, on an xcb connection of
//! our own, and the pointer lock, a pointer grab on it, with XInput 2's
//! raw motion for the mouse's moves before the server's acceleration.

use std::ffi::CString;
use std::num::NonZeroU32;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use mitsuami_core::raw_window_handle::{
    HandleError, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle,
};
use mitsuami_core::{Modifiers, MouseButton, NativeSurface, Point, ScrollDelta, SurfaceHandle, SurfaceInput};
use x11rb::connection::{Connection as _, RequestConnection as _};
use x11rb::protocol::Event;
use x11rb::protocol::shape::{self, ConnectionExt as _};
use x11rb::protocol::xinput::{self, ConnectionExt as _};
use x11rb::protocol::xproto::{
    AtomEnum, ButtonPressEvent, ChangeWindowAttributesAux, ClientMessageEvent, ConfigureWindowAux, ConnectionExt as _,
    CreateWindowAux, EventMask, GrabMode, GrabStatus, Gravity, KeyButMask, NotifyMode, StackMode, WindowClass,
};
use x11rb::xcb_ffi::XCBConnection;

use crate::{LockEvent, LockSink};

/// A window of our own, a child of the toolkit's window, over the widget.
/// It takes no input (an empty input shape), so the pointer goes to the
/// toolkit's window under it. Clones share it; xcb connections are
/// thread-safe, so it may be used, and dropped, from any thread.
#[derive(Clone)]
pub struct ChildWindow(Arc<Inner>);

struct Inner {
    conn: XCBConnection,
    screen: usize,
    root: u32,
    window: u32,
    /// The toolkit's window it's in, while shown.
    parent: Mutex<Option<u32>>,
    /// Where it was put in its parent, in pixels; `None` while hidden.
    placed: Mutex<Option<(i32, i32, u32, u32)>>,
    /// The pointer lock in effect, by its number; 0 for none.
    lock: AtomicU32,
    locks: AtomicU32,
    /// Lock threads reading the connection's events. A new lock's thread
    /// waits for an earlier one to see its wake and end: xcb hands each
    /// event to one of the threads waiting, so two would take each
    /// other's.
    readers: AtomicU32,
}

impl ChildWindow {
    /// A window in `parent` (the toolkit's window, by its XID) on the
    /// display the toolkit uses, not shown yet.
    pub fn new(display_name: Option<&str>, parent: u32) -> Result<ChildWindow, String> {
        let name = display_name.map(CString::new).transpose().map_err(|e| e.to_string())?;
        let (conn, screen) = XCBConnection::connect(name.as_deref()).map_err(|e| e.to_string())?;
        let root = conn.setup().roots[screen].root;
        let window = conn.generate_id().map_err(|e| e.to_string())?;
        // No background: until the app presents, what was there shows.
        let aux = CreateWindowAux::new().background_pixmap(x11rb::NONE).bit_gravity(Gravity::NORTH_WEST);
        conn.create_window(0, window, parent, 0, 0, 1, 1, 0, WindowClass::INPUT_OUTPUT, 0, &aux)
            .map_err(|e| e.to_string())?;
        if conn.extension_information(shape::X11_EXTENSION_NAME).ok().flatten().is_some() {
            let _ = conn.shape_rectangles(
                shape::SO::SET,
                shape::SK::INPUT,
                x11rb::protocol::xproto::ClipOrdering::UNSORTED,
                window,
                0,
                0,
                &[],
            );
        }
        conn.flush().map_err(|e| e.to_string())?;
        Ok(ChildWindow(Arc::new(Inner {
            conn,
            screen,
            root,
            window,
            parent: Mutex::new(Some(parent)),
            placed: Mutex::new(None),
            lock: AtomicU32::new(0),
            locks: AtomicU32::new(0),
            readers: AtomicU32::new(0),
        })))
    }

    /// What the app gets: it keeps the window alive.
    pub fn handle(&self) -> SurfaceHandle {
        SurfaceHandle::new(self.clone())
    }

    /// Puts it in `parent`, the toolkit's window, if it's elsewhere;
    /// hidden until it's placed.
    pub fn attach(&self, parent: u32) {
        let mut current = self.0.parent.lock().unwrap();
        if *current != Some(parent) {
            *self.0.placed.lock().unwrap() = None;
            let _ = self.0.conn.unmap_window(self.0.window);
            let _ = self.0.conn.reparent_window(self.0.window, parent, 0, 0);
            *current = Some(parent);
            let _ = self.0.conn.flush();
        }
        self.0.drain();
    }

    /// Hides it, and takes it out of the toolkit's window, which would
    /// destroy it with itself: the app may still present to it.
    pub fn detach(&self) {
        *self.0.placed.lock().unwrap() = None;
        let mut parent = self.0.parent.lock().unwrap();
        let _ = self.0.conn.unmap_window(self.0.window);
        if parent.take().is_some() {
            let _ = self.0.conn.reparent_window(self.0.window, self.0.root, 0, 0);
        }
        let _ = self.0.conn.flush();
        self.0.drain();
    }

    /// Where it sits in its parent, and its size, in pixels: hidden while
    /// it has no size. Returns whether that changed.
    pub fn place(&self, x: i32, y: i32, width: i32, height: i32) -> bool {
        if self.0.parent.lock().unwrap().is_none() {
            return false;
        }
        let at = (width > 0 && height > 0).then_some((x, y, width as u32, height as u32));
        let mut placed = self.0.placed.lock().unwrap();
        if *placed == at {
            return false;
        }
        let shown = placed.is_some();
        *placed = at;
        let conn = &self.0.conn;
        match at {
            Some((x, y, width, height)) => {
                let aux = ConfigureWindowAux::new().x(x).y(y).width(width).height(height).stack_mode(StackMode::ABOVE);
                let _ = conn.configure_window(self.0.window, &aux);
                if !shown {
                    let _ = conn.map_window(self.0.window);
                }
            }
            None => {
                let _ = conn.unmap_window(self.0.window);
            }
        }
        let _ = conn.flush();
        drop(placed);
        self.0.drain();
        true
    }

    /// Grabs the pointer on the window: the cursor hidden, held in the
    /// window's middle, and its moves reported (in points, at `scale`
    /// pixels to a point), with its buttons and wheel, which the grab
    /// takes from the toolkit. A click that asked for it holds the
    /// toolkit's grab until its button is let go, so it's tried until
    /// then. Released when dropped.
    pub fn lock_pointer(&self, scale: f32, sink: LockSink) -> PointerLock {
        let number = self.0.locks.fetch_add(1, Ordering::Relaxed) + 1;
        self.0.lock.store(number, Ordering::Release);
        let inner = self.0.clone();
        let spawned = std::thread::Builder::new()
            .name("mitsuami-x11-lock".into())
            .spawn(move || inner.hold_pointer(number, scale.max(1.0), sink));
        if let Err(problem) = spawned {
            eprintln!("mitsuami: no thread for a pointer lock: {problem}");
        }
        PointerLock { surface: self.clone(), number }
    }
}

impl Inner {
    fn locked(&self, number: u32) -> bool {
        self.lock.load(Ordering::Acquire) == number
    }

    /// Reads what came on the connection while no lock thread reads it:
    /// the errors of requests sent without waiting for a reply (a parent
    /// gone before its child was placed), and a lock's events and wake
    /// after it ended. xcb would keep them all. Not while a lock is in
    /// effect, or a thread may still read: they'd take its events.
    fn drain(&self) {
        if self.lock.load(Ordering::Acquire) == 0 && self.readers.load(Ordering::Acquire) == 0 {
            while let Ok(Some(_)) = self.conn.poll_for_event() {}
        }
    }

    /// Selects what tells a lock its grab is gone: the server lets a grab
    /// go when its window can't be seen (it, or the toolkit's window, is
    /// unmapped), and a grab's end is a leave of mode `Ungrab` from its
    /// window to the one the pointer is in. Without the shape extension
    /// the window takes input, the pointer is in it and there's no leave,
    /// so its unmapping is watched too.
    /// Turned off before an ungrab of our own, whose leave isn't a loss.
    fn watch_grab(&self, on: bool) {
        let mask = if on { EventMask::LEAVE_WINDOW | EventMask::STRUCTURE_NOTIFY } else { EventMask::NO_EVENT };
        let _ = self.conn.change_window_attributes(self.window, &ChangeWindowAttributesAux::new().event_mask(mask));
    }

    /// The middle of the window, in its own pixels.
    fn middle(&self) -> Option<(i16, i16)> {
        let (_, _, width, height) = (*self.placed.lock().unwrap())?;
        Some(((width / 2) as i16, (height / 2) as i16))
    }

    /// A lock's thread: grabs, then reports until the lock is dropped.
    fn hold_pointer(&self, number: u32, scale: f32, sink: LockSink) {
        // An earlier lock's thread ends at its wake, which it's the only
        // one reading. Not for long: a lock dropped wakes its thread.
        let waited = Instant::now();
        while self.readers.load(Ordering::Acquire) > 0 && waited.elapsed() < Duration::from_secs(1) {
            std::thread::sleep(Duration::from_millis(5));
        }
        let cursor = match self.blank_cursor() {
            Ok(cursor) => cursor,
            Err(_) => return sink(LockEvent::Ended),
        };
        let mask = EventMask::POINTER_MOTION | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE;
        let started = Instant::now();
        loop {
            if !self.locked(number) {
                let _ = self.conn.free_cursor(cursor);
                let _ = self.conn.flush();
                return;
            }
            let window = self.window;
            let grabbed = self
                .conn
                .grab_pointer(
                    false,
                    window,
                    mask,
                    GrabMode::ASYNC,
                    GrabMode::ASYNC,
                    window,
                    cursor,
                    x11rb::CURRENT_TIME,
                )
                .ok()
                .and_then(|cookie| cookie.reply().ok())
                .map(|reply| reply.status);
            match grabbed {
                Some(GrabStatus::SUCCESS) => break,
                Some(GrabStatus::ALREADY_GRABBED | GrabStatus::FROZEN)
                    if started.elapsed() < Duration::from_secs(2) =>
                {
                    std::thread::sleep(Duration::from_millis(20));
                }
                _ => {
                    let _ = self.conn.free_cursor(cursor);
                    let _ = self.conn.flush();
                    return sink(LockEvent::Ended);
                }
            }
        }
        // Counted before the lock is looked at again, so a newer lock's
        // thread either waits for this one or this one sees it's dropped.
        self.readers.fetch_add(1, Ordering::AcqRel);
        self.watch_grab(true);
        let _ = self.conn.free_cursor(cursor);
        let raw = self.select_raw_motion(true);
        // Dropped while the grab was on its way: the drop's ungrab may have
        // come first, and the grab would stay with no one reading it.
        if !self.locked(number) {
            self.readers.fetch_sub(1, Ordering::AcqRel);
            return self.let_go();
        }
        if !raw {
            eprintln!("mitsuami: no XInput 2 on this X server: no raw motion while the pointer is locked");
        }
        self.warp_to_middle();
        self.report(number, scale, &sink);
        self.readers.fetch_sub(1, Ordering::AcqRel);
        if !self.locked(number) {
            self.let_go();
        }
    }

    /// Lets the grab and raw motion go once no lock wants them: a newer
    /// lock keeps them, as its own.
    fn let_go(&self) {
        if self.lock.load(Ordering::Acquire) == 0 {
            self.watch_grab(false);
            let _ = self.conn.ungrab_pointer(x11rb::CURRENT_TIME);
            self.select_raw_motion(false);
            let _ = self.conn.flush();
        }
    }

    /// Reports the grab's events until the lock is dropped, or the server
    /// lets the grab go. An earlier lock's wake, left unread, is just
    /// another event, as are errors.
    fn report(&self, number: u32, scale: f32, sink: &LockSink) {
        let point = |x: i16, y: i16| Point::new(x as f32 / scale, y as f32 / scale);
        loop {
            let Ok(event) = self.conn.wait_for_event() else { return };
            if !self.locked(number) {
                return;
            }
            match event {
                Event::MotionNotify(motion) => {
                    let Some((mx, my)) = self.middle() else { continue };
                    let (dx, dy) = (motion.event_x - mx, motion.event_y - my);
                    // The warp back to the middle comes as a move too.
                    if dx != 0 || dy != 0 {
                        let (dx, dy) = (dx as f32 / scale, dy as f32 / scale);
                        sink(LockEvent::Input(SurfaceInput::Motion { dx, dy }));
                        self.warp_to_middle();
                    }
                }
                Event::XinputRawMotion(raw) => {
                    if let Some((dx, dy)) = raw_motion(&raw) {
                        sink(LockEvent::Input(SurfaceInput::RawMotion { dx, dy }));
                    }
                }
                Event::ButtonPress(press) => {
                    if let Some(input) = button_input(&press, true, point) {
                        sink(LockEvent::Input(input));
                    }
                }
                Event::ButtonRelease(release) => {
                    if let Some(input) = button_input(&release, false, point) {
                        sink(LockEvent::Input(input));
                    }
                }
                Event::LeaveNotify(leave) if leave.mode == NotifyMode::UNGRAB && leave.event == self.window => {
                    return self.lost(sink);
                }
                Event::UnmapNotify(unmap) if unmap.window == self.window => return self.lost(sink),
                _ => {}
            }
        }
    }

    /// The server let the grab go: what went with it is let go too, while
    /// this thread is still counted, so a newer lock's grab comes after.
    /// The lock stays in effect until the app lets go of it.
    fn lost(&self, sink: &LockSink) {
        self.watch_grab(false);
        let _ = self.conn.ungrab_pointer(x11rb::CURRENT_TIME);
        self.select_raw_motion(false);
        let _ = self.conn.flush();
        sink(LockEvent::Ended);
    }

    /// Raw motion comes only to the root window, and to a client that
    /// said it speaks XInput 2. Returns whether it's selected (or, turning
    /// it off, was asked to be unselected).
    fn select_raw_motion(&self, on: bool) -> bool {
        if self.conn.extension_information(xinput::X11_EXTENSION_NAME).ok().flatten().is_none() {
            return false;
        }
        if on {
            let version = self.conn.xinput_xi_query_version(2, 0).ok().and_then(|cookie| cookie.reply().ok());
            if version.is_none_or(|v| v.major_version < 2) {
                return false;
            }
        }
        let mask = if on { xinput::XIEventMask::RAW_MOTION } else { xinput::XIEventMask::from(0u32) };
        let masks = [xinput::EventMask { deviceid: xinput::Device::ALL_MASTER.into(), mask: vec![mask] }];
        let selected = self.conn.xinput_xi_select_events(self.root, &masks).is_ok();
        let _ = self.conn.flush();
        selected
    }

    fn warp_to_middle(&self) {
        if let Some((x, y)) = self.middle() {
            let _ = self.conn.warp_pointer(x11rb::NONE, self.window, 0, 0, 0, 0, x, y);
            let _ = self.conn.flush();
        }
    }

    /// An invisible cursor: a one-pixel pixmap with nothing set, as its
    /// image and mask.
    fn blank_cursor(&self) -> Result<u32, x11rb::errors::ReplyOrIdError> {
        let pixmap = self.conn.generate_id()?;
        self.conn.create_pixmap(1, pixmap, self.window, 1, 1)?;
        let cursor = self.conn.generate_id()?;
        self.conn.create_cursor(cursor, pixmap, pixmap, 0, 0, 0, 0, 0, 0, 0, 0)?;
        self.conn.free_pixmap(pixmap)?;
        Ok(cursor)
    }
}

/// The x and y valuators (0 and 1) of a raw motion, unaccelerated: its
/// values come in the order of the valuators its mask has, and an axis it
/// doesn't have didn't move.
fn raw_motion(event: &xinput::RawMotionEvent) -> Option<(f32, f32)> {
    let mask = event.valuator_mask.first().copied().unwrap_or(0);
    let value = |valuator: u32| {
        if mask & (1 << valuator) == 0 {
            return 0.0;
        }
        let index = (mask & ((1 << valuator) - 1)).count_ones() as usize;
        event.axisvalues_raw.get(index).map_or(0.0, |v| v.integral as f64 + v.frac as f64 / 4_294_967_296.0)
    };
    let (dx, dy) = (value(0), value(1));
    (dx != 0.0 || dy != 0.0).then_some((dx as f32, dy as f32))
}

/// A button or a wheel's notch while the pointer is grabbed. X11 has the
/// wheel as buttons 4 to 7, pressed and released at once.
fn button_input(event: &ButtonPressEvent, pressed: bool, point: impl Fn(i16, i16) -> Point) -> Option<SurfaceInput> {
    let modifiers = modifiers(event.state);
    let scroll = |x: f32, y: f32| SurfaceInput::Scroll { delta: ScrollDelta::Lines { x, y }, modifiers };
    let button = match event.detail {
        1 => MouseButton::Primary,
        2 => MouseButton::Middle,
        3 => MouseButton::Secondary,
        4..=7 if !pressed => return None,
        4 => return Some(scroll(0.0, -1.0)),
        5 => return Some(scroll(0.0, 1.0)),
        6 => return Some(scroll(-1.0, 0.0)),
        7 => return Some(scroll(1.0, 0.0)),
        8 => MouseButton::Back,
        9 => MouseButton::Forward,
        n => MouseButton::Other(n as u16),
    };
    Some(SurfaceInput::Button { button, pressed, position: point(event.event_x, event.event_y), modifiers })
}

/// Modifier keys from an X11 event's state: Mod1 is Alt, Mod4 Super, as
/// desktops map them.
pub fn modifiers(state: KeyButMask) -> Modifiers {
    Modifiers {
        shift: state.contains(KeyButMask::SHIFT),
        control: state.contains(KeyButMask::CONTROL),
        alt: state.contains(KeyButMask::MOD1),
        meta: state.contains(KeyButMask::MOD4),
    }
}

/// A pointer grab on a [`ChildWindow`]; released when dropped.
pub struct PointerLock {
    surface: ChildWindow,
    number: u32,
}

impl Drop for PointerLock {
    fn drop(&mut self) {
        let inner = &self.surface.0;
        if inner.lock.compare_exchange(self.number, 0, Ordering::AcqRel, Ordering::Acquire).is_err() {
            return;
        }
        inner.watch_grab(false);
        let _ = inner.conn.ungrab_pointer(x11rb::CURRENT_TIME);
        inner.select_raw_motion(false);
        // Wakes the lock's thread, which sees it's no longer locked. An
        // event sent with no mask goes to the window's own client.
        let wake = ClientMessageEvent::new(32, inner.window, AtomEnum::INTEGER, [self.number, 0, 0, 0, 0]);
        let _ = inner.conn.send_event(false, inner.window, EventMask::NO_EVENT, wake);
        let _ = inner.conn.flush();
    }
}

impl Drop for Inner {
    fn drop(&mut self) {
        let _ = self.conn.destroy_window(self.window);
        let _ = self.conn.flush();
    }
}

// SAFETY: it owns the window and its connection, destroyed only when it's
// dropped.
unsafe impl NativeSurface for ChildWindow {
    fn window_handle(&self) -> Result<RawWindowHandle, HandleError> {
        let window = NonZeroU32::new(self.0.window).ok_or(HandleError::Unavailable)?;
        Ok(RawWindowHandle::Xcb(XcbWindowHandle::new(window)))
    }

    fn display_handle(&self) -> Result<RawDisplayHandle, HandleError> {
        let conn = NonNull::new(self.0.conn.get_raw_xcb_connection());
        Ok(RawDisplayHandle::Xcb(XcbDisplayHandle::new(conn, self.0.screen as i32)))
    }
}