mirror of
https://github.com/deunix-educ/sourizz.git
synced 2026-11-25 04:39:43 +01:00
Fix the pointer freezes on Wayland: stop reopening /dev/input nodes every 3 s
Devices::rescan reopened every event node it did not keep (power button, audio jacks, video bus...) on each call. Opening a node takes 25-70 ms on the field-test laptop, so each rescan blocked the daemon for ~650 ms and the pointer froze for that long every 3 s while Sourizz was drawing. Rejected nodes are now remembered and only new ones are opened. Found with examples/layer_probe.rs, a bare layer-shell overlay with knobs that was extended one Sourizz component at a time until the stall reappeared. The README limitation about trails under KWin was wrong and is removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -47,7 +47,7 @@ Adding a config option touches: `config.rs` (struct, `Default`, `sanitize`), `i1
|
|||||||
- **egui ids must be unique on screen.** Two `ComboBox`es sharing an id (e.g. both labelled "Style") act as one control and their popups close each other, so the lists could not be opened. `combo()` in `settings.rs` takes an explicit id — never derive it from the label. `examples/click_at.rs` drives the real settings window through XTEST for such checks (it refuses to click unless the pointer is over our own window).
|
- **egui ids must be unique on screen.** Two `ComboBox`es sharing an id (e.g. both labelled "Style") act as one control and their popups close each other, so the lists could not be opened. `combo()` in `settings.rs` takes an explicit id — never derive it from the label. `examples/click_at.rs` drives the real settings window through XTEST for such checks (it refuses to click unless the pointer is over our own window).
|
||||||
- **Raising an already-open settings window does not work under KWin.** Focus-stealing prevention turns another process's activation request (`ViewportCommand::Focus` after a `SIGUSR1`) into a blinking taskbar entry (`_NET_WM_STATE_DEMANDS_ATTENTION`), so a window buried under others stays buried. `Core::open_settings` therefore kills the old child and spawns a new one, which always maps on top. It is safe because the settings save as they change.
|
- **Raising an already-open settings window does not work under KWin.** Focus-stealing prevention turns another process's activation request (`ViewportCommand::Focus` after a `SIGUSR1`) into a blinking taskbar entry (`_NET_WM_STATE_DEMANDS_ATTENTION`), so a window buried under others stays buried. `Core::open_settings` therefore kills the old child and spawns a new one, which always maps on top. It is safe because the settings save as they change.
|
||||||
- Process name `sourizz-settings` is truncated to 15 chars by the kernel: use `pgrep -x sourizz-setting`, and never `pgrep -f` from a shell whose own command line contains the pattern.
|
- Process name `sourizz-settings` is truncated to 15 chars by the kernel: use `pgrep -x sourizz-setting`, and never `pgrep -f` from a shell whose own command line contains the pattern.
|
||||||
- **KWin Wayland stalls whenever we draw (real hardware, Intel + NVIDIA laptop, KWin 6.3.6).** The pointer freezes ~0.5 s every 2-3 s as soon as anything is drawn, even the classic ring alone with `ring.pulse_speed = 0`. It does not happen with `enabled = off` (script and D-Bus feed still running), without Sourizz, or when dragging a window. An earlier "ring alone is fine" result was wrong: the config had changed between tests, so record the config md5 with each run. Measured and ruled out: our loop blocking (no iteration over 40 ms), buffer allocation (reusing buffers changed nothing), damage size (40-380 kpx per commit, unrelated to the stalls), the D-Bus pointer feed, waiting for frame callbacks (committing after 20 ms changed nothing), trail style (`dots`, `rainbow`), pointer speed, frame rate (`general.fps = 30`), continuous commits as such, NVIDIA runtime PM (stays `suspended`). KWin's main thread is not saturated (11-32 % of a core, never in `D` state) and its script kept delivering 100+ positions/s during the stalls, so the wait is probably on the GPU / page flip, with frame callbacks arriving 500-600 ms late. Not tried: a probe client with a variable surface size and rate (would show whether the 1920x1080 shm buffer matters), `KWIN_DRM_DEVICES`, position extrapolation for the ring lag. Documented as a known limitation in both READMEs.
|
- **Never reopen `/dev/input/event*` nodes on a timer.** On the field-test laptop (Intel + NVIDIA, KDE Plasma 6.3.6 Wayland) opening and closing one node costs 25-70 ms, so the old `Devices::rescan` (every 3 s, reopening every node it did not keep: power button, audio jacks, video bus…) blocked the daemon for ~650 ms and froze the pointer for that long, every 3 s, as soon as anything was drawn. `Devices` now remembers rejected nodes (`skipped`) and only opens new ones. How it was found, worth reusing: `examples/layer_probe.rs` is a bare layer-shell overlay with knobs (`--size`, `--fps`, `--damage`, `--follow` for the KWin pointer script, `--seat`, `--evdev`, `--no-rescan`); adding Sourizz's pieces to it one at a time showed which one brought the stall back. Two traps: the "callback delay" I first measured was the time until *our* loop handled the callback, so it showed our own stall as if it were the compositor's; and a loop timer that omits a section (here the rescan, after `poll`) cannot see a stall in it. Record the config md5 with each run, since a changed config once made a test look clean.
|
||||||
|
|
||||||
## Testing Wayland without a Wayland session
|
## Testing Wayland without a Wayland session
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -185,15 +185,13 @@ La session elle-même se choisit sur l'écran de connexion (par exemple « Plasm
|
|||||||
|
|
||||||
Limite connue : sur les sorties HiDPI avec un facteur d'échelle supérieur à 1, la surcouche est dessinée en 1× puis agrandie par le compositeur, ce qui la rend légèrement floue (les positions restent correctes).
|
Limite connue : sur les sorties HiDPI avec un facteur d'échelle supérieur à 1, la surcouche est dessinée en 1× puis agrandie par le compositeur, ce qui la rend légèrement floue (les positions restent correctes).
|
||||||
|
|
||||||
Limite connue : sous KDE Plasma Wayland (KWin 6.3.6, portable à GPU hybride Intel UHD 630 + NVIDIA MX250), le pointeur se fige environ une demi-seconde toutes les deux à trois secondes dès que Sourizz dessine quoi que ce soit, l'anneau seul compris. Cela n'arrive pas avec tous les effets coupés, ni sans Sourizz, ni en déplaçant une fenêtre. Sourizz lui-même n'est pas bloqué pendant ces gels, la zone qu'il redessine est petite et le thread principal de KWin n'est pas saturé : la cause n'est pas encore comprise, et couper la traînée n'y change rien. Sur une telle configuration, utilisez la session X11. L'anneau peut aussi traîner légèrement derrière un pointeur rapide sous Wayland, car sa position suit un chemin plus long (script KWin puis D-Bus) que les événements bruts XInput2 utilisés sous X11.
|
|
||||||
|
|
||||||
## Ce qui a été testé
|
## Ce qui a été testé
|
||||||
|
|
||||||
En toute honnêteté sur la vérification :
|
En toute honnêteté sur la vérification :
|
||||||
|
|
||||||
- **Automatisé :** 57 tests unitaires (configuration, moteur, suivi des zones à redessiner, invariants de rendu, gestion des touches avec de vraies dispositions AZERTY/QWERTY, traduction evdev, analyse des commandes). En particulier, un test de propriété vérifie que le redessin incrémental est identique au pixel près à un redessin complet pour chaque forme de projecteur, style de traînée et effet de clic.
|
- **Automatisé :** 57 tests unitaires (configuration, moteur, suivi des zones à redessiner, invariants de rendu, gestion des touches avec de vraies dispositions AZERTY/QWERTY, traduction evdev, analyse des commandes). En particulier, un test de propriété vérifie que le redessin incrémental est identique au pixel près à un redessin complet pour chaque forme de projecteur, style de traînée et effet de clic.
|
||||||
- **X11 :** éprouvé en conditions réelles sous Debian 13, KDE Plasma 6.3, deux écrans — fenêtres de surcouche, traversée des clics, dessin de l'anneau / des clics / des traînées, aucun pixel fantôme après un mouvement rapide, raccourci global, ligne de commande, rechargement de la configuration en direct, menu de l'icône via D-Bus, fenêtre de réglages.
|
- **X11 :** éprouvé en conditions réelles sous Debian 13, KDE Plasma 6.3, deux écrans — fenêtres de surcouche, traversée des clics, dessin de l'anneau / des clics / des traînées, aucun pixel fantôme après un mouvement rapide, raccourci global, ligne de commande, rechargement de la configuration en direct, menu de l'icône via D-Bus, fenêtre de réglages.
|
||||||
- **Wayland :** éprouvé contre un compositeur **KWin 6.3** imbriqué — surcouche layer-shell, rendu de l'anneau / du projecteur / de la barre d'annotation, capture du mode annotation, script KWin de position du pointeur. Lancé aussi dans une vraie session KDE Plasma 6.3.6 Wayland (voir la limite sur la traînée ci-dessus) : l'anneau suit le pointeur grâce au script KWin et les vrais périphériques souris, pavé tactile et clavier sont ouverts. **Non vérifié** en session réelle : effets et sons de clic, affichage des touches et mode annotation. **Non testé** en vrai HiDPI, sous GNOME, Hyprland, sway, niri ou d'autres compositeurs.
|
- **Wayland :** éprouvé contre un compositeur **KWin 6.3** imbriqué — surcouche layer-shell, rendu de l'anneau / du projecteur / de la barre d'annotation, capture du mode annotation, script KWin de position du pointeur. Lancé aussi dans une vraie session KDE Plasma 6.3.6 Wayland : l'anneau suit le pointeur grâce au script KWin et les vrais périphériques souris, pavé tactile et clavier sont ouverts. **Non vérifié** en session réelle : effets et sons de clic, affichage des touches et mode annotation. **Non testé** en vrai HiDPI, sous GNOME, Hyprland, sway, niri ou d'autres compositeurs.
|
||||||
|
|
||||||
Considérez la prise en charge de Wayland comme une première version solide qui demande davantage de tests sur le terrain ; les rapports de bogues accompagnés de la sortie de `sourizz -vv` sont bienvenus.
|
Considérez la prise en charge de Wayland comme une première version solide qui demande davantage de tests sur le terrain ; les rapports de bogues accompagnés de la sortie de `sourizz -vv` sont bienvenus.
|
||||||
|
|
||||||
|
|||||||
@@ -185,15 +185,13 @@ The session itself is chosen at the login screen (for example "Plasma (X11)" or
|
|||||||
|
|
||||||
Known limitation: on HiDPI outputs with a scale above 1 the overlay is drawn at 1× and upscaled by the compositor, so it looks slightly soft (positions are correct).
|
Known limitation: on HiDPI outputs with a scale above 1 the overlay is drawn at 1× and upscaled by the compositor, so it looks slightly soft (positions are correct).
|
||||||
|
|
||||||
Known limitation: on KDE Plasma Wayland (KWin 6.3.6, laptop with an Intel UHD 630 + NVIDIA MX250 hybrid GPU) the pointer freezes for about half a second every two to three seconds as soon as Sourizz draws anything, the ring alone included. It does not happen with every effect switched off, nor without Sourizz, nor when dragging a window around. Sourizz itself is not blocked during these stalls, the area it redraws is small, and KWin's main thread is not saturated, so the cause is not understood yet; turning the trail off does not help. On such a setup, use the X11 session. The ring can also lag slightly behind a fast-moving pointer on Wayland, because its position takes a longer path (KWin script, then D-Bus) than the raw XInput2 events used on X11.
|
|
||||||
|
|
||||||
## What was tested
|
## What was tested
|
||||||
|
|
||||||
Being honest about verification:
|
Being honest about verification:
|
||||||
|
|
||||||
- **Automated:** 57 unit tests (config, engine, damage tracking, rendering invariants, key handling with real AZERTY/QWERTY layouts, evdev translation, IPC parsing). Notably, a property test checks that incremental repainting is pixel-identical to a full repaint for every spotlight shape, trail style and click effect.
|
- **Automated:** 57 unit tests (config, engine, damage tracking, rendering invariants, key handling with real AZERTY/QWERTY layouts, evdev translation, IPC parsing). Notably, a property test checks that incremental repainting is pixel-identical to a full repaint for every spotlight shape, trail style and click effect.
|
||||||
- **X11:** exercised live on Debian 13, KDE Plasma 6.3, two monitors — overlay windows, click-through, ring/click/trail drawing, no ghost pixels after fast motion, global hotkey, CLI, live config reload, tray menu over D-Bus, settings window.
|
- **X11:** exercised live on Debian 13, KDE Plasma 6.3, two monitors — overlay windows, click-through, ring/click/trail drawing, no ghost pixels after fast motion, global hotkey, CLI, live config reload, tray menu over D-Bus, settings window.
|
||||||
- **Wayland:** exercised against a nested **KWin 6.3** compositor — layer-shell overlay, ring/spotlight/annotation toolbar rendering, annotation mode capture, the KWin pointer script. Also run in a real KDE Plasma 6.3.6 Wayland session (see the trail limitation above): the ring follows the pointer through the KWin script and the real mouse, touchpad and keyboard devices are opened. **Not verified** on a real session: click effects and sounds, keystroke display and annotation mode. **Not tested** on real HiDPI, GNOME, or Hyprland, sway, niri or other compositors.
|
- **Wayland:** exercised against a nested **KWin 6.3** compositor — layer-shell overlay, ring/spotlight/annotation toolbar rendering, annotation mode capture, the KWin pointer script. Also run in a real KDE Plasma 6.3.6 Wayland session: the ring follows the pointer through the KWin script and the real mouse, touchpad and keyboard devices are opened. **Not verified** on a real session: click effects and sounds, keystroke display and annotation mode. **Not tested** on real HiDPI, GNOME, or Hyprland, sway, niri or other compositors.
|
||||||
|
|
||||||
Treat Wayland support as a solid first version that needs more field testing; bug reports with `sourizz -vv` output are welcome.
|
Treat Wayland support as a solid first version that needs more field testing; bug reports with `sourizz -vv` output are welcome.
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,423 @@
|
|||||||
|
//! Probe for compositor stalls: a bare layer-shell overlay that redraws a moving ring.
|
||||||
|
//!
|
||||||
|
//! It creates the same kind of surface as the Wayland backend (overlay layer, empty input region,
|
||||||
|
//! `wl_shm` ARGB8888 buffers, a fresh buffer per commit) but with the knobs that matter exposed, so
|
||||||
|
//! one can find out which of them makes a compositor stall (see "KWin Wayland stalls" in CLAUDE.md).
|
||||||
|
//! The ring follows its own path, so nothing has to move; move the real pointer to see whether the
|
||||||
|
//! arrow freezes. Prints, once a second, the commits and the longest commit-to-frame-callback delay.
|
||||||
|
//!
|
||||||
|
//! ```sh
|
||||||
|
//! cargo run --release --example layer_probe -- --size full --fps 60 --damage ring
|
||||||
|
//! cargo run --release --example layer_probe -- --size 400x400 --fps 60
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use anyhow::{Context, Result, anyhow};
|
||||||
|
use clap::{Parser, ValueEnum};
|
||||||
|
use rustix::event::{PollFd, PollFlags, Timespec, poll};
|
||||||
|
use smithay_client_toolkit::{
|
||||||
|
compositor::{CompositorHandler, CompositorState, FrameCallbackData, Region},
|
||||||
|
delegate_registry,
|
||||||
|
dispatch2::Dispatch2,
|
||||||
|
output::{OutputHandler, OutputState},
|
||||||
|
registry::{ProvidesRegistryState, RegistryState},
|
||||||
|
registry_handlers,
|
||||||
|
seat::{
|
||||||
|
Capability, SeatHandler, SeatState,
|
||||||
|
pointer::{PointerEvent, PointerHandler},
|
||||||
|
},
|
||||||
|
shell::{
|
||||||
|
WaylandSurface,
|
||||||
|
wlr_layer::{Anchor, KeyboardInteractivity, Layer, LayerShell, LayerShellHandler, LayerSurface, LayerSurfaceConfigure},
|
||||||
|
},
|
||||||
|
shm::{Shm, ShmHandler, slot::SlotPool},
|
||||||
|
};
|
||||||
|
use sourizz::{
|
||||||
|
control::{self, Hub},
|
||||||
|
evdev_in::Devices,
|
||||||
|
kwin::KwinFeed,
|
||||||
|
};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use wayland_client::{
|
||||||
|
Connection, QueueHandle,
|
||||||
|
globals::registry_queue_init,
|
||||||
|
protocol::{wl_keyboard, wl_output, wl_pointer, wl_seat, wl_shm, wl_surface},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, ValueEnum, PartialEq)]
|
||||||
|
enum Damage {
|
||||||
|
/// Report the whole surface as damaged on every commit.
|
||||||
|
Full,
|
||||||
|
/// Report only the box around the ring (what Sourizz does).
|
||||||
|
Ring,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(about = "Layer-shell stall probe")]
|
||||||
|
struct Args {
|
||||||
|
/// `full` (the whole output, like Sourizz) or `WIDTHxHEIGHT` anchored top-left.
|
||||||
|
#[arg(long, default_value = "full")]
|
||||||
|
size: String,
|
||||||
|
/// Commits per second.
|
||||||
|
#[arg(long, default_value_t = 60)]
|
||||||
|
fps: u32,
|
||||||
|
/// What to report as damaged.
|
||||||
|
#[arg(long, value_enum, default_value = "ring")]
|
||||||
|
damage: Damage,
|
||||||
|
/// Commit on a timer without waiting for the compositor's frame callback.
|
||||||
|
#[arg(long)]
|
||||||
|
free_running: bool,
|
||||||
|
/// Follow the real pointer (KDE only, through the same KWin script as Sourizz) and commit only
|
||||||
|
/// when it moved, instead of running on a path of its own.
|
||||||
|
#[arg(long)]
|
||||||
|
follow: bool,
|
||||||
|
/// Bind the seat's pointer and keyboard objects, as the Wayland backend does.
|
||||||
|
#[arg(long)]
|
||||||
|
seat: bool,
|
||||||
|
/// Read the input devices in /dev/input (events discarded), as the Wayland backend does.
|
||||||
|
#[arg(long)]
|
||||||
|
evdev: bool,
|
||||||
|
/// With --evdev: scan /dev/input once and never again (the backend rescans every 3 s).
|
||||||
|
#[arg(long)]
|
||||||
|
no_rescan: bool,
|
||||||
|
/// Stop after this many seconds (0 = until interrupted).
|
||||||
|
#[arg(long, default_value_t = 0)]
|
||||||
|
seconds: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
const RING_R: f32 = 36.0;
|
||||||
|
const RING_BOX: i32 = 90;
|
||||||
|
|
||||||
|
struct Probe {
|
||||||
|
registry_state: RegistryState,
|
||||||
|
output_state: OutputState,
|
||||||
|
seat_state: Option<SeatState>,
|
||||||
|
wl_pointer_obj: Option<wl_pointer::WlPointer>,
|
||||||
|
wl_keyboard_obj: Option<wl_keyboard::WlKeyboard>,
|
||||||
|
shm: Shm,
|
||||||
|
pool: SlotPool,
|
||||||
|
layer: LayerSurface,
|
||||||
|
args: Args,
|
||||||
|
fixed: Option<(i32, i32)>,
|
||||||
|
size: Option<(i32, i32)>,
|
||||||
|
canvas: Vec<u8>,
|
||||||
|
/// Box around the ring in the previous frame, to be cleared.
|
||||||
|
prev_box: Option<(i32, i32)>,
|
||||||
|
frame_pending: bool,
|
||||||
|
committed_at: Instant,
|
||||||
|
next_due: Instant,
|
||||||
|
t0: Instant,
|
||||||
|
stat_since: Instant,
|
||||||
|
commits: u32,
|
||||||
|
cb_max: Duration,
|
||||||
|
exit: bool,
|
||||||
|
feed: Option<KwinFeed>,
|
||||||
|
pointer: Option<(f32, f32)>,
|
||||||
|
dirty: bool,
|
||||||
|
moves: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Probe {
|
||||||
|
fn ring_center(&self, w: i32, h: i32) -> (f32, f32) {
|
||||||
|
if self.args.follow {
|
||||||
|
return self.pointer.unwrap_or((w as f32 / 2.0, h as f32 / 2.0));
|
||||||
|
}
|
||||||
|
let t = self.t0.elapsed().as_secs_f32();
|
||||||
|
let (rx, ry) = ((w as f32 / 2.0 - RING_BOX as f32).max(1.0), (h as f32 / 2.0 - RING_BOX as f32).max(1.0));
|
||||||
|
(w as f32 / 2.0 + rx * (t * 2.3).sin(), h as f32 / 2.0 + ry * (t * 3.1 + 1.0).sin())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Paint the ring into `self.canvas` and return the damaged box (x, y, w, h), clipped.
|
||||||
|
fn draw(&mut self, w: i32, h: i32) -> (i32, i32, i32, i32) {
|
||||||
|
let (cx, cy) = self.ring_center(w, h);
|
||||||
|
let (bx, by) =
|
||||||
|
((cx as i32 - RING_BOX / 2).clamp(0, (w - RING_BOX).max(0)), (cy as i32 - RING_BOX / 2).clamp(0, (h - RING_BOX).max(0)));
|
||||||
|
let clip = |x: i32, y: i32| (x.max(0), y.max(0), (x + RING_BOX).min(w) - x.max(0), (y + RING_BOX).min(h) - y.max(0));
|
||||||
|
if let Some((px, py)) = self.prev_box {
|
||||||
|
let (x, y, bw, bh) = clip(px, py);
|
||||||
|
for row in y..y + bh {
|
||||||
|
let o = (row * w + x) as usize * 4;
|
||||||
|
self.canvas[o..o + bw as usize * 4].fill(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let (x, y, bw, bh) = clip(bx, by);
|
||||||
|
for row in y..y + bh {
|
||||||
|
for col in x..x + bw {
|
||||||
|
let d = ((col as f32 + 0.5 - cx).powi(2) + (row as f32 + 0.5 - cy).powi(2)).sqrt();
|
||||||
|
let a = (2.5 + 0.5 - (d - RING_R).abs()).clamp(0.0, 1.0) * 0.95;
|
||||||
|
let o = (row * w + col) as usize * 4;
|
||||||
|
// premultiplied BGRA of amber (255, 193, 7)
|
||||||
|
self.canvas[o..o + 4].copy_from_slice(&[(7.0 * a) as u8, (193.0 * a) as u8, (255.0 * a) as u8, (255.0 * a) as u8]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The damaged box is the union of the cleared and the painted one.
|
||||||
|
let (ux, uy, uw, uh) = match self.prev_box {
|
||||||
|
Some((px, py)) => {
|
||||||
|
let (x0, y0) = (px.min(bx), py.min(by));
|
||||||
|
let (x1, y1) = ((px + RING_BOX).max(bx + RING_BOX), (py + RING_BOX).max(by + RING_BOX));
|
||||||
|
(x0.max(0), y0.max(0), x1.min(w) - x0.max(0), y1.min(h) - y0.max(0))
|
||||||
|
}
|
||||||
|
None => (x, y, bw, bh),
|
||||||
|
};
|
||||||
|
self.prev_box = Some((bx, by));
|
||||||
|
(ux, uy, uw, uh)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn commit(&mut self, qh: &QueueHandle<Self>) {
|
||||||
|
let Some((w, h)) = self.size else { return };
|
||||||
|
let dmg = self.draw(w, h);
|
||||||
|
let Ok((buffer, slot)) = self.pool.create_buffer(w, h, w * 4, wl_shm::Format::Argb8888) else {
|
||||||
|
eprintln!("cannot allocate a {w}x{h} buffer");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
slot.copy_from_slice(&self.canvas);
|
||||||
|
let surface = self.layer.wl_surface().clone();
|
||||||
|
match self.args.damage {
|
||||||
|
Damage::Full => surface.damage_buffer(0, 0, w, h),
|
||||||
|
Damage::Ring => surface.damage_buffer(dmg.0, dmg.1, dmg.2, dmg.3),
|
||||||
|
}
|
||||||
|
surface.frame(qh, FrameCallbackData(surface.clone()));
|
||||||
|
if buffer.attach_to(&surface).is_ok() {
|
||||||
|
self.layer.commit();
|
||||||
|
self.frame_pending = true;
|
||||||
|
self.committed_at = Instant::now();
|
||||||
|
self.commits += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tick(&mut self, qh: &QueueHandle<Self>) {
|
||||||
|
let now = Instant::now();
|
||||||
|
if let Some(p) = self.feed.as_ref().and_then(|f| f.take_latest()) {
|
||||||
|
self.pointer = Some(p);
|
||||||
|
self.dirty = true;
|
||||||
|
self.moves += 1;
|
||||||
|
}
|
||||||
|
let wanted = !self.args.follow || self.dirty;
|
||||||
|
if self.size.is_some() && wanted && now >= self.next_due && (!self.frame_pending || self.args.free_running) {
|
||||||
|
self.dirty = false;
|
||||||
|
self.commit(qh);
|
||||||
|
let step = Duration::from_secs_f64(1.0 / self.args.fps.max(1) as f64);
|
||||||
|
self.next_due = (self.next_due + step).max(now);
|
||||||
|
}
|
||||||
|
if now.duration_since(self.stat_since) >= Duration::from_secs(1) {
|
||||||
|
println!("positions={:3} commits={:3} longest callback delay {:4} ms", self.moves, self.commits, self.cb_max.as_millis());
|
||||||
|
self.moves = 0;
|
||||||
|
self.commits = 0;
|
||||||
|
self.cb_max = Duration::ZERO;
|
||||||
|
self.stat_since = now;
|
||||||
|
}
|
||||||
|
if self.args.seconds > 0 && self.t0.elapsed() >= Duration::from_secs(self.args.seconds) {
|
||||||
|
self.exit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timeout(&self) -> Duration {
|
||||||
|
let wait = self.next_due.saturating_duration_since(Instant::now());
|
||||||
|
if self.frame_pending && !self.args.free_running { Duration::from_millis(100) } else { wait.min(Duration::from_millis(100)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompositorHandler for Probe {
|
||||||
|
fn scale_factor_changed(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_surface::WlSurface, _: i32) {}
|
||||||
|
fn transform_changed(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_surface::WlSurface, _: wl_output::Transform) {}
|
||||||
|
fn surface_enter(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_surface::WlSurface, _: &wl_output::WlOutput) {}
|
||||||
|
fn surface_leave(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_surface::WlSurface, _: &wl_output::WlOutput) {}
|
||||||
|
fn frame(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_surface::WlSurface, _: u32) {
|
||||||
|
self.cb_max = self.cb_max.max(self.committed_at.elapsed());
|
||||||
|
self.frame_pending = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputHandler for Probe {
|
||||||
|
fn output_state(&mut self) -> &mut OutputState {
|
||||||
|
&mut self.output_state
|
||||||
|
}
|
||||||
|
fn new_output(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_output::WlOutput) {}
|
||||||
|
fn update_output(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_output::WlOutput) {}
|
||||||
|
fn output_destroyed(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_output::WlOutput) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayerShellHandler for Probe {
|
||||||
|
fn closed(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &LayerSurface) {
|
||||||
|
self.exit = true;
|
||||||
|
}
|
||||||
|
fn configure(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &LayerSurface, configure: LayerSurfaceConfigure, _serial: u32) {
|
||||||
|
let (w, h) = match self.fixed {
|
||||||
|
Some(s) => s,
|
||||||
|
None => (configure.new_size.0 as i32, configure.new_size.1 as i32),
|
||||||
|
};
|
||||||
|
if w == 0 || h == 0 || self.size == Some((w, h)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
eprintln!("surface {w}x{h}");
|
||||||
|
self.size = Some((w, h));
|
||||||
|
self.canvas = vec![0; w as usize * h as usize * 4];
|
||||||
|
self.prev_box = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Routes `wl_keyboard` events (ignored) to [`Dispatch2`].
|
||||||
|
struct KbData;
|
||||||
|
|
||||||
|
impl Dispatch2<wl_keyboard::WlKeyboard, Probe> for KbData {
|
||||||
|
fn event(&self, _: &mut Probe, _: &wl_keyboard::WlKeyboard, _: wl_keyboard::Event, _: &Connection, _: &QueueHandle<Probe>) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SeatHandler for Probe {
|
||||||
|
fn seat_state(&mut self) -> &mut SeatState {
|
||||||
|
self.seat_state.as_mut().expect("seat state is created with --seat")
|
||||||
|
}
|
||||||
|
fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {}
|
||||||
|
fn new_capability(&mut self, _: &Connection, qh: &QueueHandle<Self>, seat: wl_seat::WlSeat, cap: Capability) {
|
||||||
|
if cap == Capability::Pointer && self.wl_pointer_obj.is_none() {
|
||||||
|
self.wl_pointer_obj = self.seat_state.as_mut().and_then(|s| s.get_pointer(qh, &seat).ok());
|
||||||
|
}
|
||||||
|
if cap == Capability::Keyboard && self.wl_keyboard_obj.is_none() {
|
||||||
|
self.wl_keyboard_obj = Some(seat.get_keyboard(qh, KbData));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn remove_capability(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat, _: Capability) {}
|
||||||
|
fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PointerHandler for Probe {
|
||||||
|
fn pointer_frame(&mut self, _: &Connection, _: &QueueHandle<Self>, _: &wl_pointer::WlPointer, _: &[PointerEvent]) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShmHandler for Probe {
|
||||||
|
fn shm_state(&mut self) -> &mut Shm {
|
||||||
|
&mut self.shm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delegate_registry!(Probe);
|
||||||
|
smithay_client_toolkit::delegate_dispatch2!(Probe);
|
||||||
|
|
||||||
|
impl ProvidesRegistryState for Probe {
|
||||||
|
fn registry(&mut self) -> &mut RegistryState {
|
||||||
|
&mut self.registry_state
|
||||||
|
}
|
||||||
|
registry_handlers![OutputState, SeatState];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
let app_args_follow = args.follow;
|
||||||
|
let app_args_seat = args.seat;
|
||||||
|
let no_rescan = args.no_rescan;
|
||||||
|
let mut devices = if args.evdev { Some(Devices::scan()) } else { None };
|
||||||
|
let mut last_scan = Instant::now();
|
||||||
|
let fixed = match args.size.as_str() {
|
||||||
|
"full" => None,
|
||||||
|
s => {
|
||||||
|
let (w, h) = s.split_once('x').ok_or_else(|| anyhow!("--size is `full` or WIDTHxHEIGHT"))?;
|
||||||
|
Some((w.parse::<i32>()?, h.parse::<i32>()?))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let conn = Connection::connect_to_env().context("cannot connect to the Wayland compositor")?;
|
||||||
|
let (globals, mut queue) = registry_queue_init(&conn)?;
|
||||||
|
let qh = queue.handle();
|
||||||
|
let compositor = CompositorState::bind(&globals, &qh)?;
|
||||||
|
let layer_shell = LayerShell::bind(&globals, &qh).map_err(|_| anyhow!("the compositor lacks wlr-layer-shell"))?;
|
||||||
|
let shm = Shm::bind(&globals, &qh)?;
|
||||||
|
let pool = SlotPool::new(1 << 20, &shm)?;
|
||||||
|
|
||||||
|
let surface = compositor.create_surface(&qh);
|
||||||
|
let layer = layer_shell.create_layer_surface(&qh, surface, Layer::Overlay, Some("layer-probe"), None);
|
||||||
|
match fixed {
|
||||||
|
None => {
|
||||||
|
layer.set_anchor(Anchor::TOP | Anchor::BOTTOM | Anchor::LEFT | Anchor::RIGHT);
|
||||||
|
layer.set_size(0, 0);
|
||||||
|
}
|
||||||
|
Some((w, h)) => {
|
||||||
|
layer.set_anchor(Anchor::TOP | Anchor::LEFT);
|
||||||
|
layer.set_size(w as u32, h as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
layer.set_exclusive_zone(-1);
|
||||||
|
layer.set_keyboard_interactivity(KeyboardInteractivity::None);
|
||||||
|
layer.set_input_region(Some(Region::new(&compositor)?.wl_region()));
|
||||||
|
layer.commit();
|
||||||
|
|
||||||
|
let (hub, _commands, wake) = Hub::new()?;
|
||||||
|
let feed = if app_args_follow { KwinFeed::start(hub) } else { None };
|
||||||
|
if app_args_follow && feed.is_none() {
|
||||||
|
return Err(anyhow!("--follow needs KDE Plasma (the KWin pointer script could not be loaded)"));
|
||||||
|
}
|
||||||
|
let now = Instant::now();
|
||||||
|
let mut app = Probe {
|
||||||
|
registry_state: RegistryState::new(&globals),
|
||||||
|
output_state: OutputState::new(&globals, &qh),
|
||||||
|
seat_state: if app_args_seat { Some(SeatState::new(&globals, &qh)) } else { None },
|
||||||
|
wl_pointer_obj: None,
|
||||||
|
wl_keyboard_obj: None,
|
||||||
|
shm,
|
||||||
|
pool,
|
||||||
|
layer,
|
||||||
|
args,
|
||||||
|
fixed,
|
||||||
|
size: None,
|
||||||
|
canvas: Vec::new(),
|
||||||
|
prev_box: None,
|
||||||
|
frame_pending: false,
|
||||||
|
committed_at: now,
|
||||||
|
next_due: now,
|
||||||
|
t0: now,
|
||||||
|
stat_since: now,
|
||||||
|
commits: 0,
|
||||||
|
cb_max: Duration::ZERO,
|
||||||
|
exit: false,
|
||||||
|
feed,
|
||||||
|
pointer: None,
|
||||||
|
dirty: false,
|
||||||
|
moves: 0,
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"size={} fps={} damage={} {}",
|
||||||
|
app.args.size,
|
||||||
|
app.args.fps,
|
||||||
|
if app.args.damage == Damage::Full { "full" } else { "ring" },
|
||||||
|
if app.args.free_running { "free-running" } else { "paced by frame callbacks" }
|
||||||
|
);
|
||||||
|
|
||||||
|
while !app.exit {
|
||||||
|
queue.dispatch_pending(&mut app)?;
|
||||||
|
app.tick(&qh);
|
||||||
|
conn.flush()?;
|
||||||
|
let Some(guard) = queue.prepare_read() else { continue };
|
||||||
|
let t = app.timeout();
|
||||||
|
let ts = Timespec { tv_sec: t.as_secs() as i64, tv_nsec: t.subsec_nanos() as _ };
|
||||||
|
let fd = guard.connection_fd();
|
||||||
|
let dev_fds = devices.as_ref().map(|d| d.fds()).unwrap_or_default();
|
||||||
|
let mut fds = vec![PollFd::new(&fd, PollFlags::IN), PollFd::new(&wake, PollFlags::IN)];
|
||||||
|
fds.extend(dev_fds.iter().map(|f| PollFd::new(f, PollFlags::IN)));
|
||||||
|
let _ = poll(&mut fds, Some(&ts));
|
||||||
|
let ready: Vec<bool> = fds.iter().map(|f| f.revents().intersects(PollFlags::IN | PollFlags::HUP | PollFlags::ERR)).collect();
|
||||||
|
drop(fds);
|
||||||
|
drop(dev_fds);
|
||||||
|
if ready[1] {
|
||||||
|
control::drain(&wake);
|
||||||
|
}
|
||||||
|
if let Some(d) = devices.as_mut() {
|
||||||
|
for (n, r) in ready.iter().enumerate().skip(2).rev() {
|
||||||
|
if *r {
|
||||||
|
let _ = d.read(n - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !no_rescan && last_scan.elapsed() > Duration::from_secs(3) {
|
||||||
|
last_scan = Instant::now();
|
||||||
|
d.rescan();
|
||||||
|
eprintln!("rescan of /dev/input took {} ms", last_scan.elapsed().as_millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ready[0] {
|
||||||
|
match guard.read() {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(wayland_client::backend::WaylandError::Io(e)) if e.kind() == std::io::ErrorKind::WouldBlock => {}
|
||||||
|
Err(e) => return Err(e).context("lost the connection to the compositor"),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
drop(guard);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+11
-3
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
use crate::input::Button;
|
use crate::input::Button;
|
||||||
use rustix::fd::{AsFd, BorrowedFd};
|
use rustix::fd::{AsFd, BorrowedFd};
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub const EV_KEY: u16 = 1;
|
pub const EV_KEY: u16 = 1;
|
||||||
@@ -86,13 +87,16 @@ pub fn translate(events: impl IntoIterator<Item = (u16, u16, i32)>) -> Vec<Raw>
|
|||||||
|
|
||||||
pub struct Devices {
|
pub struct Devices {
|
||||||
devs: Vec<(PathBuf, evdev::Device)>,
|
devs: Vec<(PathBuf, evdev::Device)>,
|
||||||
|
/// Nodes already opened and found uninteresting (power button, audio jacks…). Opening a node
|
||||||
|
/// costs tens of milliseconds on some machines, so they must not be reopened on every rescan.
|
||||||
|
skipped: HashSet<PathBuf>,
|
||||||
/// Devices that exist but could not be opened (permission denied).
|
/// Devices that exist but could not be opened (permission denied).
|
||||||
pub denied: usize,
|
pub denied: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Devices {
|
impl Devices {
|
||||||
pub fn scan() -> Devices {
|
pub fn scan() -> Devices {
|
||||||
let mut d = Devices { devs: Vec::new(), denied: 0 };
|
let mut d = Devices { devs: Vec::new(), skipped: HashSet::new(), denied: 0 };
|
||||||
d.rescan();
|
d.rescan();
|
||||||
d
|
d
|
||||||
}
|
}
|
||||||
@@ -110,11 +114,13 @@ impl Devices {
|
|||||||
/// Open any readable input device we do not have yet (cheap enough to call every few seconds).
|
/// Open any readable input device we do not have yet (cheap enough to call every few seconds).
|
||||||
pub fn rescan(&mut self) {
|
pub fn rescan(&mut self) {
|
||||||
let Ok(dir) = std::fs::read_dir("/dev/input") else { return };
|
let Ok(dir) = std::fs::read_dir("/dev/input") else { return };
|
||||||
|
// A node that went away may come back under the same number as another device.
|
||||||
|
self.skipped.retain(|p| p.exists());
|
||||||
let mut denied = 0;
|
let mut denied = 0;
|
||||||
for entry in dir.flatten() {
|
for entry in dir.flatten() {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
let is_event = path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("event"));
|
let is_event = path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("event"));
|
||||||
if !is_event || self.devs.iter().any(|(p, _)| *p == path) {
|
if !is_event || self.skipped.contains(&path) || self.devs.iter().any(|(p, _)| *p == path) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match evdev::Device::open(&path) {
|
match evdev::Device::open(&path) {
|
||||||
@@ -124,7 +130,9 @@ impl Devices {
|
|||||||
self.devs.push((path, dev));
|
self.devs.push((path, dev));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(_) => {}
|
Ok(_) => {
|
||||||
|
self.skipped.insert(path);
|
||||||
|
}
|
||||||
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => denied += 1,
|
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => denied += 1,
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user