bevy_live_wallpaper 0.5.0

A Bevy plugin to create live wallpapers.
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
pub mod backend;
pub mod render;
pub mod surface;

use std::{
    collections::HashSet,
    ffi::{c_int, c_void},
    ptr::NonNull,
};

use as_raw_xcb_connection::AsRawXcbConnection;
use bevy::prelude::*;
use x11rb::COPY_DEPTH_FROM_PARENT;
use x11rb::protocol::randr::{self, ConnectionExt as RandrConnectionExt, MonitorInfo};
use x11rb::{
    connection::Connection,
    protocol::{
        Event,
        xproto::{ChangeWindowAttributesAux, ConnectionExt, EventMask},
    },
    xcb_ffi::XCBConnection,
};

use self::surface::X11SurfaceHandles;

use crate::{PointerButton, PointerSample, WallpaperTargetMonitor};

pub(crate) struct X11AppState {
    connection: XCBConnection,
    root_window: u32,
    wallpaper_window: u32,
    screen: c_int,
    closed: bool,
    target: WallpaperTargetMonitor,
    monitors: Vec<MonitorRect>,
    monitors_dirty: bool,
    pending_surface_config: Option<X11SurfaceConfig>,
}

impl X11AppState {
    pub(crate) fn connect(
        target: WallpaperTargetMonitor,
    ) -> Result<(Self, X11SurfaceConfig), String> {
        let (connection, screen_index) = XCBConnection::connect(None)
            .map_err(|err| format!("Failed to connect to X11: {err}"))?;

        let screen = connection
            .setup()
            .roots
            .get(screen_index)
            .ok_or_else(|| format!("Invalid X11 screen index {screen_index}"))?;
        let root_window = screen.root;
        let screen_width = u32::from(screen.width_in_pixels);
        let screen_height = u32::from(screen.height_in_pixels);
        let root_visual = screen.root_visual;
        let screen_id = screen_index as c_int;

        connection
            .change_window_attributes(
                root_window,
                &ChangeWindowAttributesAux::new().event_mask(EventMask::STRUCTURE_NOTIFY),
            )
            .map_err(|err| format!("Failed to select root window events: {err:?}"))?
            .check()
            .map_err(|err| format!("Failed to select root window events: {err:?}"))?;

        // Subscribe to RandR notifications (monitor hotplug/resize).
        connection
            .randr_select_input(
                root_window,
                randr::NotifyMask::CRTC_CHANGE
                    | randr::NotifyMask::OUTPUT_CHANGE
                    | randr::NotifyMask::SCREEN_CHANGE,
            )
            .map_err(|err| format!("Failed to select RandR input: {err:?}"))?;

        connection
            .flush()
            .map_err(|err| format!("Failed to flush X11 connection: {err:?}"))?;

        let mut state = Self {
            connection,
            root_window,
            wallpaper_window: 0,
            screen: screen_id,
            closed: false,
            target,
            monitors: Vec::new(),
            monitors_dirty: true,
            pending_surface_config: None,
        };

        state.refresh_monitors()?;
        state.create_or_update_wallpaper_window(root_visual)?;
        state.monitors_dirty = false;

        // Initial surface config uses current wallpaper window size.
        let config = Self::create_surface_config(
            &state.connection,
            state.wallpaper_window,
            screen_id,
            state.current_width().unwrap_or(screen_width),
            state.current_height().unwrap_or(screen_height),
        );

        state.pending_surface_config = Some(config);

        Ok((state, config))
    }

    fn create_surface_config(
        connection: &XCBConnection,
        window: u32,
        screen: c_int,
        width: u32,
        height: u32,
    ) -> X11SurfaceConfig {
        let ptr = NonNull::new(connection.as_raw_xcb_connection().cast::<c_void>())
            .expect("xcb connection pointer should be valid");
        let handles = X11SurfaceHandles::new(ptr, screen, window);

        X11SurfaceConfig {
            handles,
            width,
            height,
        }
    }

    fn current_width(&self) -> Option<u32> {
        self.monitor_for(self.target).map(|rect| rect.width as u32)
    }

    fn current_height(&self) -> Option<u32> {
        self.monitor_for(self.target).map(|rect| rect.height as u32)
    }

    pub(crate) fn is_running(&self) -> bool {
        !self.closed
    }

    pub(crate) fn queue_surface_config(&mut self, config: X11SurfaceConfig) {
        self.pending_surface_config = Some(config);
    }

    pub(crate) fn take_surface_config(&mut self) -> Option<X11SurfaceConfig> {
        self.pending_surface_config.take()
    }

    pub(crate) fn poll_events(&mut self) {
        loop {
            match self.connection.poll_for_event() {
                Ok(Some(Event::ConfigureNotify(event))) => {
                    if event.window == self.wallpaper_window {
                        let width = u32::from(event.width.max(1));
                        let height = u32::from(event.height.max(1));
                        let config = Self::create_surface_config(
                            &self.connection,
                            self.wallpaper_window,
                            self.screen,
                            width,
                            height,
                        );
                        self.queue_surface_config(config);
                    }
                }
                Ok(Some(Event::RandrNotify(_))) | Ok(Some(Event::RandrScreenChangeNotify(_))) => {
                    self.monitors_dirty = true;
                }
                Ok(Some(_)) => {}
                Ok(None) => break,
                Err(err) => {
                    warn!("X11 poll_for_event failed: {err:?}");
                    self.closed = true;
                    break;
                }
            }
        }

        if self.monitors_dirty && !self.closed {
            if let Err(err) = self.refresh_monitors() {
                warn!("Failed to refresh RandR monitors: {err}");
            } else if let Err(err) = self.apply_target(self.target) {
                warn!("Failed to apply target monitor after RandR change: {err}");
            }
            self.monitors_dirty = false;
        }
    }

    /// Returns a snapshot of the current pointer (root) position and buttons.
    pub(crate) fn poll_pointer(&self, prev: Option<&PointerSample>) -> Option<PointerSample> {
        let reply = self
            .connection
            .query_pointer(self.root_window)
            .ok()?
            .reply()
            .ok()?;

        let position = Vec2::new(f32::from(reply.root_x), f32::from(reply.root_y));
        let prev_position = prev.map(|p| p.position).unwrap_or(position);
        let delta = position - prev_position;

        let pressed = pressed_buttons(reply.mask.bits());
        let last_button = detect_last_button(prev.map(|p| &p.pressed), &pressed);

        let output = self.output_for_position(position);

        Some(PointerSample {
            output,
            position,
            delta,
            pressed,
            last_button,
        })
    }

    fn output_for_position(&self, position: Vec2) -> Option<u32> {
        self.monitors
            .iter()
            .enumerate()
            .find(|(_, rect)| {
                let px = position.x as i32;
                let py = position.y as i32;
                px >= rect.x as i32
                    && px < rect.x as i32 + rect.width as i32
                    && py >= rect.y as i32
                    && py < rect.y as i32 + rect.height as i32
            })
            .map(|(idx, _)| idx as u32)
    }

    pub(crate) fn apply_target(&mut self, target: WallpaperTargetMonitor) -> Result<(), String> {
        let Some(rect) = self.monitor_for(target) else {
            return Err("No monitors available for selected target".into());
        };

        self.target = target;

        // Move/resize wallpaper window to selected monitor bounds.
        let aux = x11rb::protocol::xproto::ConfigureWindowAux::new()
            .x(i32::from(rect.x))
            .y(i32::from(rect.y))
            .width(rect.width as u32)
            .height(rect.height as u32)
            .stack_mode(x11rb::protocol::xproto::StackMode::BELOW);
        self.connection
            .configure_window(self.wallpaper_window, &aux)
            .map_err(|err| format!("Failed to configure wallpaper window: {err:?}"))?
            .check()
            .map_err(|err| format!("Failed to configure wallpaper window: {err:?}"))?;
        self.connection
            .flush()
            .map_err(|err| format!("Failed to flush wallpaper configure: {err:?}"))?;

        let config = Self::create_surface_config(
            &self.connection,
            self.wallpaper_window,
            self.screen,
            rect.width as u32,
            rect.height as u32,
        );
        self.queue_surface_config(config);
        Ok(())
    }

    fn refresh_monitors(&mut self) -> Result<(), String> {
        let reply = self
            .connection
            .randr_get_monitors(self.root_window, true)
            .map_err(|err| format!("Failed to request RandR monitors: {err:?}"))?
            .reply()
            .map_err(|err| format!("Failed to read RandR monitors reply: {err:?}"))?;

        self.monitors = reply.monitors.into_iter().map(MonitorRect::from).collect();
        Ok(())
    }

    fn monitor_for(&self, target: WallpaperTargetMonitor) -> Option<MonitorRect> {
        match target {
            WallpaperTargetMonitor::All => MonitorRect::bounding(&self.monitors),
            WallpaperTargetMonitor::Primary => self
                .monitors
                .iter()
                .find(|m| m.primary)
                .or_else(|| self.monitors.first())
                .copied(),
            WallpaperTargetMonitor::Index(n) => self.monitors.get(n).copied(),
        }
    }

    pub(crate) fn current_bounds(&self) -> Option<(i32, i32, u32, u32)> {
        self.monitor_for(self.target).map(|rect| {
            (
                rect.x as i32,
                rect.y as i32,
                rect.width as u32,
                rect.height as u32,
            )
        })
    }

    fn create_or_update_wallpaper_window(&mut self, visual: u32) -> Result<(), String> {
        if self.monitors.is_empty() {
            return Err("No monitors reported by RandR; cannot create wallpaper window".into());
        }

        let rect = self
            .monitor_for(self.target)
            .unwrap_or_else(|| self.monitors[0]);

        if self.wallpaper_window == 0 {
            let window = self
                .connection
                .generate_id()
                .map_err(|err| format!("Failed to generate window id: {err:?}"))?;

            let aux = x11rb::protocol::xproto::CreateWindowAux::new()
                .event_mask(EventMask::STRUCTURE_NOTIFY)
                .override_redirect(1)
                .background_pixel(0)
                .border_pixel(0);

            self.connection
                .create_window(
                    COPY_DEPTH_FROM_PARENT,
                    window,
                    self.root_window,
                    rect.x,
                    rect.y,
                    rect.width,
                    rect.height,
                    0,
                    x11rb::protocol::xproto::WindowClass::INPUT_OUTPUT,
                    visual,
                    &aux,
                )
                .map_err(|err| format!("Failed to create wallpaper window: {err:?}"))?
                .check()
                .map_err(|err| format!("Failed to create wallpaper window: {err:?}"))?;

            // Place behind other windows.
            let config_aux = x11rb::protocol::xproto::ConfigureWindowAux::new()
                .stack_mode(x11rb::protocol::xproto::StackMode::BELOW);
            self.connection
                .configure_window(window, &config_aux)
                .map_err(|err| format!("Failed to lower wallpaper window: {err:?}"))?
                .check()
                .map_err(|err| format!("Failed to lower wallpaper window: {err:?}"))?;

            self.connection
                .map_window(window)
                .map_err(|err| format!("Failed to map wallpaper window: {err:?}"))?
                .check()
                .map_err(|err| format!("Failed to map wallpaper window: {err:?}"))?;

            self.wallpaper_window = window;
        } else {
            self.apply_target(self.target)?;
        }

        Ok(())
    }
}

#[derive(Clone, Copy)]
pub(crate) struct X11SurfaceConfig {
    pub handles: X11SurfaceHandles,
    pub width: u32,
    pub height: u32,
}

#[derive(Clone, Copy, Debug, Default)]
struct MonitorRect {
    x: i16,
    y: i16,
    width: u16,
    height: u16,
    primary: bool,
}

impl MonitorRect {
    fn bounding(monitors: &[Self]) -> Option<Self> {
        let mut iter = monitors.iter();
        let first = iter.next().copied()?;

        let mut min_x = first.x as i32;
        let mut min_y = first.y as i32;
        let mut max_x = first.x as i32 + first.width as i32;
        let mut max_y = first.y as i32 + first.height as i32;

        for m in iter {
            min_x = min_x.min(m.x as i32);
            min_y = min_y.min(m.y as i32);
            max_x = max_x.max(m.x as i32 + m.width as i32);
            max_y = max_y.max(m.y as i32 + m.height as i32);
        }

        Some(Self {
            x: min_x as i16,
            y: min_y as i16,
            width: (max_x - min_x) as u16,
            height: (max_y - min_y) as u16,
            primary: false,
        })
    }
}

impl From<MonitorInfo> for MonitorRect {
    fn from(m: MonitorInfo) -> Self {
        Self {
            x: m.x,
            y: m.y,
            width: m.width,
            height: m.height,
            primary: m.primary,
        }
    }
}

fn pressed_buttons(mask: u16) -> HashSet<MouseButton> {
    let mut set = HashSet::new();

    let has = |button: u8| -> bool { mask & (1u16 << (button + 7)) != 0 };

    if has(1) {
        set.insert(MouseButton::Left);
    }
    if has(2) {
        set.insert(MouseButton::Middle);
    }
    if has(3) {
        set.insert(MouseButton::Right);
    }

    // Ignore BUTTON_4/BUTTON_5 (scroll) to avoid treating wheel motion as held buttons.

    set
}

fn detect_last_button(
    prev: Option<&HashSet<MouseButton>>,
    current: &HashSet<MouseButton>,
) -> Option<PointerButton> {
    let empty = HashSet::new();
    let prev = prev.unwrap_or(&empty);

    let mut newly_pressed: Vec<MouseButton> = current.difference(prev).copied().collect();
    if let Some(btn) = prioritize_button(&mut newly_pressed) {
        return Some(PointerButton {
            button: Some(btn),
            pressed: true,
        });
    }

    let mut released: Vec<MouseButton> = prev.difference(current).copied().collect();
    if let Some(btn) = prioritize_button(&mut released) {
        return Some(PointerButton {
            button: Some(btn),
            pressed: false,
        });
    }

    None
}

fn prioritize_button(buttons: &mut Vec<MouseButton>) -> Option<MouseButton> {
    // Deterministic priority similar to common UX expectations.
    let priority = [MouseButton::Left, MouseButton::Right, MouseButton::Middle];

    for p in priority {
        if let Some(pos) = buttons.iter().position(|b| *b == p) {
            return Some(buttons.swap_remove(pos));
        }
    }

    buttons.pop()
}