Split the client into modules, and clear out what iteration left behind

main.rs had grown to 1290 lines holding everything: the CLI, the client
state, the capture engine, the drawing, the input handling and twelve
Dispatch impls. It is now orchestration only, and the concerns live where
you would look for them — cli, app, capture, overlay — with the module
docs saying what each owns. No behaviour changed; the code moved.

Iterating in response to review left residue, now gone:

- Tile::handle was only ever written. Dropping a wayland-rs proxy does
  not destroy the object, so nothing needed it held.
- Tile::failed likewise: an earlier captures_settled() read it, and
  `settled` is what everything waits on now.
- A blanket #[allow(dead_code)] on Tile hid both of those. Fields are
  pub(crate) rather than pub so the lint keeps working.
- Tile's doc comment had drifted onto the Format enum during a patch.
- Three consecutive `if args.verbose` blocks became describe() and
  report(), and the loose ticks/releases/starved/pool_bytes counters
  became one Stats. `releases` went: frames already imply it.
- quit + quit_why + activate became one Ending enum and picked, so
  "closed by the compositor" is a state rather than a string.
- App::new took seven positional arguments, two of them bare integers in
  a row; the four that always travel together are now Settings.
- sway::scale and the display listing each called get_outputs; one call
  does both.
- scaled(rect, n) became Rect::scaled(n), and Format lives with Target
  where render() dispatches on it.

README had drifted too: the usage line still advertised --print, which
no longer exists, and omitted --format and --no-outputs. Its flag list is
now checked against --help, the memory figures are re-measured, and there
is a source layout for anyone arriving cold.
This commit is contained in:
Milad Alizadeh
2026-08-23 19:17:04 +01:00
parent 5b98615c41
commit 8d8a27c4c1
10 changed files with 1446 additions and 1265 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
name = "wl-pick"
version = "0.1.0"
edition = "2024"
description = "Live window switcher for wlroots compositors: a thumbnail grid with the look of a rofi theme"
description = "A live grid of window and display previews, for picking one"
license = "MIT"
[dependencies]
+35 -19
View File
@@ -35,25 +35,26 @@ is the one thing the rofi version had that this doesn't — see the roadmap.
## Usage
```
wl-pick [--print] [--verbose] [--hide-labels] [--font FAMILY] [--font-size PX]
[--live all|current|none] [--fps N] [--timeout SECS]
wl-pick [--format tsv|json|portal] [--live all|current|none] [--fps N]
[--no-outputs] [--hide-labels] [--font FAMILY] [--font-size PX]
[--timeout SECS] [--verbose]
```
- `--verbose` prints phase timings and how many windows were captured
- `--format tsv|json|portal` how to report the pick (default `tsv`)
- `--live all|current|none` which tiles keep updating (default `all`; displays
are always a single snapshot)
- `--fps N` cap on live updates per tile per second (default 12)
- `--no-outputs` windows only; displays are included as tiles by default
- `--hide-labels` draws an icon-only grid
- `--font FAMILY` label font family (default `Berkeley Mono`)
- `--font-size PX` label size in logical px
- `--live all|current|none` which tiles keep updating (default `all`)
- `--fps N` cap on updates per tile per second (default 12)
- `--timeout SECS` exits after a deadline (an escape hatch: the overlay takes an
exclusive keyboard grab)
- `--timeout SECS` exits after a deadline, in case the keyboard grab ever traps
you
- `--verbose` phase timings, the tile list, and capture stats
wl-pick is a chooser: it reports what you picked and leaves acting on it to the
caller. The pick goes to stdout, nothing does if you cancel, and the exit status
is 0 for a pick and 1 for a cancel.
wl-pick never acts on the choice — it has no idea what you want to do with it.
Focusing on sway looks like this:
wl-pick is a chooser: the pick goes to stdout, nothing does if you cancel, and
the exit status is 0 for a pick and 1 for a cancel. It never acts on the choice —
it has no idea what you want to do with it. Focusing on sway looks like this:
```sh
#!/usr/bin/env bash
@@ -121,9 +122,9 @@ from being expensive:
back, so the slot on screen is usually free too — waiting for it to stop being
displayed instead deadlocks after two frames.
The cost is memory and bandwidth: two full-resolution buffers per window (110 MB
of shm for ten windows on this display, versus 55 MB with `--live none`) and a
readback per refreshed frame. `--live current` refreshes only the selected tile,
The cost is memory and bandwidth: two full-resolution buffers per window (138 MB
of shm for eight windows and a display here, against 83 MB with `--live none`)
and a readback per refreshed frame. `--live current` refreshes only the selected tile,
which is much cheaper and still reads as alive.
## Look
@@ -131,8 +132,8 @@ which is much cheaper and still reads as alive.
Colours, font metrics and grid geometry come from the rofi theme this replaces
(gruvbox dark, a yellow selection filling the element padding, `ceil(sqrt(n))`
columns capped at 4, 16:9 tiles, `title · app` centred underneath) and live in
`src/theme.rs`. They will move to a config file so they can't drift from the
`.rasi`.
`src/theme.rs`, which is the one place to change them. They are not
configurable at runtime beyond the font flags.
The font is looked up by family name. Your own font directories are scanned
first because they are small; the full system scan (~37ms) happens only if the
@@ -161,9 +162,24 @@ unaffected. It matters more once previews are live.
- dmabuf capture, so the pixels never leave the GPU at all — and live previews
stop costing a readback per frame
## Source layout
```
main.rs orchestration: list, capture, map, report the pick
cli.rs flags, defaults, and the help text that documents them
sway.rs the window list and display names, over sway's IPC socket
target.rs what a tile stands for, and the three output formats
app.rs the Wayland client state every event dispatches into
capture.rs capture sessions, their buffers, and the live clock
overlay.rs the layer surface, the drawing, and the keyboard
theme.rs colours, grid geometry, aspect fitting
text.rs label shaping on a worker thread
shm.rs memfd allocation and the ARGB painter
```
## Building
```
cargo build --release
cargo test # grid geometry, ellipsising, glyph output
cargo test # grid geometry, ellipsising, output formats, glyph output
```
+326
View File
@@ -0,0 +1,326 @@
//! The client: everything the compositor talks to us through.
//!
//! `App` is the one state the Wayland event queue dispatches into, so it holds
//! the bound globals, the tiles, and the pieces of the overlay. The work itself
//! lives next door: capture.rs drives the sessions, overlay.rs draws and reads
//! the keyboard.
use std::error::Error;
use wayland_client::globals::{GlobalList, GlobalListContents};
use wayland_client::protocol::{
wl_buffer::WlBuffer,
wl_compositor::WlCompositor,
wl_output::{self, WlOutput},
wl_registry::WlRegistry,
wl_seat::WlSeat,
wl_shm::WlShm,
wl_shm_pool::WlShmPool,
wl_subcompositor::WlSubcompositor,
wl_subsurface::WlSubsurface,
wl_surface::WlSurface,
};
use wayland_client::{
Connection, Dispatch, Proxy, QueueHandle, delegate_noop, event_created_child,
};
use wayland_protocols::ext::foreign_toplevel_list::v1::client::{
ext_foreign_toplevel_handle_v1::{self, ExtForeignToplevelHandleV1},
ext_foreign_toplevel_list_v1::{self, ExtForeignToplevelListV1},
};
use wayland_protocols::ext::image_capture_source::v1::client::{
ext_foreign_toplevel_image_capture_source_manager_v1::ExtForeignToplevelImageCaptureSourceManagerV1,
ext_image_capture_source_v1::ExtImageCaptureSourceV1,
ext_output_image_capture_source_manager_v1::ExtOutputImageCaptureSourceManagerV1,
};
use wayland_protocols::ext::image_copy_capture::v1::client::ext_image_copy_capture_manager_v1::ExtImageCopyCaptureManagerV1;
use wayland_protocols::wp::viewporter::client::{
wp_viewport::WpViewport, wp_viewporter::WpViewporter,
};
use wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_shell_v1::ZwlrLayerShellV1;
use crate::capture::{Live, Tile};
use crate::shm;
use crate::target::Target;
use crate::text;
use crate::theme::{Layout, Theme};
/// What the caller decided before any of this started: the look, and how much
/// live capture to do. Passed as one value because `theme`, `live`, `fps` and
/// `scale` all arrive together and two of them are bare integers.
pub struct Settings {
pub theme: Theme,
pub live: Live,
pub fps: u32,
/// Integer output scale the overlay renders at.
pub scale: i32,
}
pub struct App {
pub(crate) compositor: WlCompositor,
pub(crate) subcompositor: WlSubcompositor,
pub(crate) shm: WlShm,
pub(crate) viewporter: WpViewporter,
pub(crate) layer_shell: ZwlrLayerShellV1,
pub(crate) copy_mgr: ExtImageCopyCaptureManagerV1,
pub(crate) src_mgr: ExtForeignToplevelImageCaptureSourceManagerV1,
/// Toplevel handles as the compositor announces them, paired with the
/// identifier that joins them to sway's tree.
pub(crate) toplevels: Vec<(ExtForeignToplevelHandleV1, String)>,
/// Displays, paired with the name the compositor gives them (wl_output v4).
pub(crate) outputs: Vec<(WlOutput, String)>,
pub(crate) output_src_mgr: Option<ExtOutputImageCaptureSourceManagerV1>,
pub(crate) tiles: Vec<Tile>,
pub(crate) theme: Theme,
pub(crate) layout: Layout,
pub(crate) live: Live,
pub(crate) fps: u32,
pub(crate) scale: i32,
pub(crate) sel: usize,
pub(crate) shift: bool,
pub(crate) labels: Option<text::Labels>,
pub(crate) surface: Option<WlSurface>,
pub(crate) chrome: Option<shm::Chrome>,
pub(crate) chrome_buffers: Vec<WlBuffer>,
pub(crate) configured: bool,
pub(crate) ending: Ending,
pub(crate) picked: Option<Target>,
pub(crate) stats: Stats,
}
/// Counters worth reporting with --verbose. Live capture is easy to get subtly
/// wrong — a starved buffer pool or a clock that never ticks both look like
/// "nothing updates" — so the numbers that distinguish those stay available.
#[derive(Default)]
pub struct Stats {
/// Frame callbacks received, i.e. how often the live clock fired.
pub(crate) ticks: u32,
/// Re-captures skipped because every buffer was still held.
pub(crate) starved: u32,
/// Bytes of shm handed to the compositor for capture buffers.
pub(crate) pool_bytes: usize,
}
/// Why the overlay stopped, for --verbose. An exit status of 1 cannot tell a
/// cancel from a surface the compositor took away.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Ending {
Running,
Picked,
Cancelled,
Closed,
}
impl Ending {
pub fn as_str(self) -> &'static str {
match self {
Ending::Running => "still running",
Ending::Picked => "picked",
Ending::Cancelled => "cancelled",
Ending::Closed => "the compositor closed the overlay",
}
}
}
impl App {
pub fn new(
globals: &GlobalList,
qh: &QueueHandle<Self>,
targets: Vec<Target>,
settings: Settings,
) -> Result<Self, Box<dyn Error>> {
let Settings {
theme,
live,
fps,
scale,
} = settings;
let layout = Layout::new(&theme, targets.len() as i32);
// Bind everything up front so a compositor missing a protocol fails
// here, with a name, rather than halfway through a capture.
let mut app = Self {
compositor: globals.bind(qh, 1..=6, ())?,
subcompositor: globals.bind(qh, 1..=1, ())?,
shm: globals.bind(qh, 1..=1, ())?,
viewporter: globals.bind(qh, 1..=1, ())?,
layer_shell: globals.bind(qh, 1..=5, ())?,
copy_mgr: globals.bind(qh, 1..=1, ())?,
src_mgr: globals.bind(qh, 1..=1, ())?,
toplevels: Vec::new(),
outputs: Vec::new(),
// Optional: a compositor without it simply gets no display tiles.
output_src_mgr: globals.bind(qh, 1..=1, ()).ok(),
tiles: targets.into_iter().map(Tile::new).collect(),
theme,
layout,
live,
fps,
scale,
sel: 0,
shift: false,
labels: None,
surface: None,
chrome: None,
chrome_buffers: Vec::new(),
configured: false,
ending: Ending::Running,
picked: None,
stats: Stats::default(),
};
let _: ExtForeignToplevelListV1 = globals.bind(qh, 1..=1, ())?;
// One wl_output per display, bound at v4 so it tells us its name.
for global in globals.contents().clone_list() {
if global.interface == WlOutput::interface().name {
let version = global.version.min(4);
if version >= 4 {
let output: WlOutput = globals.registry().bind(global.name, version, qh, ());
app.outputs.push((output, String::new()));
}
}
}
let _: WlSeat = globals.bind(qh, 1..=7, ())?;
Ok(app)
}
pub fn finished(&self) -> bool {
self.ending != Ending::Running
}
/// The pick, if there was one.
pub fn picked(&self) -> Option<&Target> {
self.picked.as_ref()
}
/// What the grid is about to show: one line per tile, then the geometry.
pub fn describe(&self) {
for (i, t) in self.tiles.iter().enumerate() {
let mark = if t.ready { "" } else { " (no thumbnail)" };
eprintln!(" [{i}] {}{mark}", t.target.tsv());
}
eprintln!(
"wl-pick: {} tile(s), {} captured; grid {}x{}, surface {}x{} logical \
at scale {}, {} MB of capture buffers",
self.tiles.len(),
self.tiles.iter().filter(|t| t.ready).count(),
self.layout.cols,
self.layout.rows,
self.layout.width,
self.layout.height,
self.scale,
self.stats.pool_bytes >> 20,
);
}
/// What live capture actually did, once the overlay is closing.
pub fn report(&self, open_for: std::time::Duration) {
let frames: u32 = self.tiles.iter().map(|t| t.frames).sum();
let secs = open_for.as_secs_f64();
eprintln!(
"wl-pick: {}, {frames} frame(s) over {secs:.1}s = {:.1}/s, {} tick(s), \
{} starved; per tile: {}",
self.ending.as_str(),
frames as f64 / secs,
self.stats.ticks,
self.stats.starved,
self.tiles
.iter()
.map(|t| t.frames.to_string())
.collect::<Vec<_>>()
.join(",")
);
}
pub fn captures_settled(&self) -> bool {
self.tiles.iter().all(|t| t.settled)
}
}
// --- enumeration ----------------------------------------------------------
impl Dispatch<WlRegistry, GlobalListContents> for App {
fn event(
_: &mut Self,
_: &WlRegistry,
_: <WlRegistry as Proxy>::Event,
_: &GlobalListContents,
_: &Connection,
_: &QueueHandle<Self>,
) {
}
}
impl Dispatch<ExtForeignToplevelListV1, ()> for App {
fn event(
app: &mut Self,
_: &ExtForeignToplevelListV1,
event: ext_foreign_toplevel_list_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let ext_foreign_toplevel_list_v1::Event::Toplevel { toplevel } = event {
app.toplevels.push((toplevel, String::new()));
}
}
event_created_child!(App, ExtForeignToplevelListV1, [
ext_foreign_toplevel_list_v1::EVT_TOPLEVEL_OPCODE => (ExtForeignToplevelHandleV1, ()),
]);
}
impl Dispatch<ExtForeignToplevelHandleV1, ()> for App {
fn event(
app: &mut Self,
handle: &ExtForeignToplevelHandleV1,
event: ext_foreign_toplevel_handle_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let ext_foreign_toplevel_handle_v1::Event::Identifier { identifier } = event
&& let Some(entry) = app.toplevels.iter_mut().find(|(h, _)| h == handle)
{
entry.1 = identifier;
}
}
}
/// wl_output tells us its name (v4), which is how a display tile is labelled
/// and how `focus output NAME` finds it again.
impl Dispatch<WlOutput, ()> for App {
fn event(
app: &mut Self,
output: &WlOutput,
event: wl_output::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let wl_output::Event::Name { name } = event
&& let Some(entry) = app.outputs.iter_mut().find(|(o, _)| o == output)
{
entry.1 = name;
}
}
}
// Interfaces we drive but never listen to.
delegate_noop!(App: WlCompositor);
delegate_noop!(App: WlSubcompositor);
delegate_noop!(App: WlSubsurface);
delegate_noop!(App: ignore WlShm);
delegate_noop!(App: WlShmPool);
delegate_noop!(App: WpViewporter);
delegate_noop!(App: WpViewport);
delegate_noop!(App: ZwlrLayerShellV1);
delegate_noop!(App: ExtImageCopyCaptureManagerV1);
delegate_noop!(App: ExtForeignToplevelImageCaptureSourceManagerV1);
delegate_noop!(App: ExtImageCaptureSourceV1);
delegate_noop!(App: ExtOutputImageCaptureSourceManagerV1);
delegate_noop!(App: ignore WlSurface);
// The chrome's own buffers: two slots alternating on keypresses, so their
// release timing does not matter.
delegate_noop!(App: ignore WlBuffer);
+447
View File
@@ -0,0 +1,447 @@
//! Capturing windows and displays.
//!
//! One session per tile, all opened before a single roundtrip so their buffer
//! constraints arrive together, and every first frame put in flight at once: the
//! compositor is bandwidth-bound reading pixels back, so serialising the
//! captures only adds latency.
//!
//! The pixels are never mapped into this process. A capture buffer goes straight
//! to a subsurface for display, so the compositor writes those pages and samples
//! them again itself.
use std::error::Error;
use std::os::fd::AsFd;
use std::time::{Duration, Instant};
use wayland_client::protocol::{
wl_buffer::{self, WlBuffer},
wl_callback, wl_output, wl_shm,
wl_subsurface::WlSubsurface,
wl_surface::WlSurface,
};
use wayland_client::{Connection, Dispatch, QueueHandle, WEnum};
use wayland_protocols::ext::image_capture_source::v1::client::ext_image_capture_source_v1::ExtImageCaptureSourceV1;
use wayland_protocols::ext::image_copy_capture::v1::client::{
ext_image_copy_capture_frame_v1::{self, ExtImageCopyCaptureFrameV1},
ext_image_copy_capture_manager_v1,
ext_image_copy_capture_session_v1::{self, ExtImageCopyCaptureSessionV1},
};
use wayland_protocols::wp::viewporter::client::wp_viewport::WpViewport;
use crate::app::App;
use crate::shm;
use crate::target::{Kind, Target};
/// Which tiles keep updating after the first frame.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Live {
/// Every tile.
All,
/// Only the selected tile: much cheaper, and still reads as alive.
Current,
/// Nothing: one snapshot each, a picker rather than an expose.
None,
}
/// One capture buffer. `busy` means the compositor still holds it — either it is
/// on screen or a capture is writing into it — so we must not scribble over it.
pub struct Slot {
pub(crate) buffer: WlBuffer,
pub(crate) busy: bool,
}
pub struct Tile {
pub(crate) target: Target,
pub(crate) session: Option<ExtImageCopyCaptureSessionV1>,
/// A capture in flight, and which slot it is filling.
pub(crate) frame: Option<ExtImageCopyCaptureFrameV1>,
pub(crate) filling: Option<usize>,
pub(crate) slots: Vec<Slot>,
/// The slot currently attached to the subsurface.
pub(crate) showing: Option<usize>,
pub(crate) formats: Vec<wl_shm::Format>,
pub(crate) format: Option<wl_shm::Format>,
/// Buffer size the session requires: the window's full resolution.
pub(crate) size: (u32, u32),
pub(crate) transform: wl_output::Transform,
pub(crate) session_done: bool,
pub(crate) ready: bool,
pub(crate) settled: bool,
/// When the last capture was asked for, for rate limiting, and how many
/// frames this tile has produced.
pub(crate) asked: Option<Instant>,
pub(crate) frames: u32,
pub(crate) surface: Option<WlSurface>,
pub(crate) subsurface: Option<WlSubsurface>,
pub(crate) viewport: Option<WpViewport>,
}
impl Tile {
pub fn new(target: Target) -> Self {
Self {
target,
session: None,
frame: None,
filling: None,
slots: Vec::new(),
showing: None,
formats: Vec::new(),
format: None,
size: (0, 0),
transform: wl_output::Transform::Normal,
session_done: false,
ready: false,
settled: false,
asked: None,
frames: 0,
surface: None,
subsurface: None,
viewport: None,
}
}
pub fn bytes(&self) -> usize {
self.size.0 as usize * 4 * self.size.1 as usize
}
/// Whether the buffer's contents are turned on their side relative to the
/// window, which flips the aspect ratio we have to fit.
pub fn rotated(&self) -> bool {
use wl_output::Transform;
matches!(
self.transform,
Transform::_90 | Transform::_270 | Transform::Flipped90 | Transform::Flipped270
)
}
}
impl App {
/// Open one capture session per window whose toplevel we recognise. They are
/// all opened before a single roundtrip, so every session's buffer
/// constraints arrive together instead of costing a round trip each.
pub fn open_sessions(&mut self, qh: &QueueHandle<Self>) {
for (i, tile) in self.tiles.iter_mut().enumerate() {
// A window's source comes from its toplevel handle, a display's from
// its wl_output; everything after that is identical.
let source: Option<ExtImageCaptureSourceV1> = match tile.target.kind {
Kind::Window => self
.toplevels
.iter()
.find(|(_, id)| !id.is_empty() && *id == tile.target.ft_id)
.map(|(handle, _)| self.src_mgr.create_source(handle, qh, ())),
Kind::Output => self
.outputs
.iter()
.find(|(_, n)| *n == tile.target.id)
.and_then(|(output, _)| {
self.output_src_mgr
.as_ref()
.map(|mgr| mgr.create_source(output, qh, ()))
}),
};
let Some(source) = source else {
// Nothing to capture from: the tile stays label-only, and must
// not be waited on.
tile.settled = true;
continue;
};
tile.session = Some(self.copy_mgr.create_session(
&source,
ext_image_copy_capture_manager_v1::Options::empty(),
qh,
i,
));
source.destroy();
}
}
/// Allocate the capture buffers in one pool and put every first frame in
/// flight at once: the compositor is bandwidth-bound reading pixels back, so
/// serialising the captures only adds latency.
///
/// Live mode gets two buffers per window. A capture may not write into the
/// buffer the compositor is currently displaying, so the two alternate:
/// fill B while A is on screen, swap, and wait for A's release before
/// touching it again.
pub fn start_captures(&mut self, qh: &QueueHandle<Self>) -> Result<(), Box<dyn Error>> {
const PAGE: usize = 4096;
let mut total = 0usize;
let mut offsets: Vec<Vec<usize>> = Vec::with_capacity(self.tiles.len());
for tile in &mut self.tiles {
offsets.push(Vec::new());
if tile.session.is_none() {
continue;
}
if !tile.session_done || tile.size.0 == 0 || tile.size.1 == 0 {
tile.settled = true;
continue;
}
// Any 32-bit format will do: we never read these pixels, we hand the
// buffer straight back for display, so byte order stays the
// compositor's business on both ends.
tile.format = tile
.formats
.iter()
.copied()
.find(|f| matches!(f, wl_shm::Format::Xrgb8888 | wl_shm::Format::Argb8888))
.or_else(|| tile.formats.first().copied());
if tile.format.is_none() {
tile.settled = true;
continue;
}
// Only a tile that will be re-captured needs a second buffer, and a
// display's is the size of the whole screen.
let slots = if self.live == Live::None || tile.target.kind == Kind::Output {
1
} else {
2
};
let last = offsets.last_mut().expect("just pushed");
for _ in 0..slots {
last.push(total);
total += tile.bytes().div_ceil(PAGE) * PAGE;
}
}
if total == 0 {
return Ok(());
}
self.stats.pool_bytes = total;
// Note: no mmap. The compositor writes these pages and samples them
// again for display; mapping them here would only cost us the faults.
let file = shm::memfd("wl-pick-capture", total)?;
let pool = self.shm.create_pool(file.as_fd(), total as i32, qh, ());
for (i, slot_offsets) in offsets.iter().enumerate() {
let (w, h, format) = {
let t = &self.tiles[i];
if t.settled || t.session.is_none() || t.format.is_none() {
continue;
}
(t.size.0 as i32, t.size.1 as i32, t.format.unwrap())
};
for &offset in slot_offsets {
let slot = self.tiles[i].slots.len();
let buffer = pool.create_buffer(offset as i32, w, h, w * 4, format, qh, (i, slot));
self.tiles[i].slots.push(Slot {
buffer,
busy: false,
});
}
self.request_capture(i, qh);
}
pool.destroy(); // the buffers keep the mapping alive
Ok(())
}
/// Ask the compositor for one frame of window `i`, into a free slot.
///
/// After a session's first frame the compositor only answers once the window
/// content changes, so a request left outstanding on an idle window costs
/// nothing: this is damage-driven, and the rate limit only bites on windows
/// that really are animating.
fn request_capture(&mut self, i: usize, qh: &QueueHandle<Self>) -> bool {
let t = &mut self.tiles[i];
if t.frame.is_some() || t.session.is_none() {
return false; // already waiting on one
}
let Some(slot) = t.slots.iter().position(|s| !s.busy) else {
self.stats.starved += 1;
return false; // both buffers still held by the compositor
};
let (w, h) = (t.size.0 as i32, t.size.1 as i32);
let frame = t
.session
.as_ref()
.expect("checked above")
.create_frame(qh, i);
frame.attach_buffer(&t.slots[slot].buffer);
frame.damage_buffer(0, 0, w, h);
frame.capture();
t.frame = Some(frame);
t.filling = Some(slot);
t.asked = Some(Instant::now());
true
}
/// A capture landed: show it, and let go of the slot it replaced.
fn frame_ready(&mut self, i: usize) {
let t = &mut self.tiles[i];
let Some(slot) = t.filling.take() else { return };
t.frames += 1;
t.ready = true;
t.settled = true;
t.slots[slot].busy = true; // the compositor reads it until it releases it
let previous = t.showing.replace(slot);
// Before the overlay is mapped there is nothing to attach to yet;
// place_tiles picks up `showing` instead.
if let Some(surface) = t.surface.clone() {
let (w, h) = (t.size.0 as i32, t.size.1 as i32);
surface.attach(Some(&t.slots[slot].buffer), 0, 0);
surface.damage_buffer(0, 0, w, h);
surface.commit();
} else if let Some(prev) = previous {
// Not on screen yet, so the old slot was never actually read.
t.slots[prev].busy = false;
}
}
/// Ask for the next frame callback. A commit is needed for the compositor to
/// schedule one, and an empty commit is enough.
pub fn arm_frame_callback(&mut self, qh: &QueueHandle<Self>) {
if self.live == Live::None {
return;
}
if let Some(surface) = self.surface.clone() {
surface.frame(qh, ());
surface.commit();
}
}
/// Re-capture whatever is due. Driven by frame callbacks, so it stops when
/// the overlay is not being presented.
pub fn tick(&mut self, qh: &QueueHandle<Self>) {
self.stats.ticks += 1;
if self.live == Live::None {
return;
}
let interval = Duration::from_secs_f64(1.0 / self.fps.max(1) as f64);
let now = Instant::now();
for i in 0..self.tiles.len() {
if self.live == Live::Current && i != self.sel {
continue;
}
// A display tile shows this overlay, which shows the display tile:
// refreshing it never settles and costs a whole screen per frame.
if self.tiles[i].target.kind == Kind::Output {
continue;
}
let t = &self.tiles[i];
if t.slots.is_empty() || t.frame.is_some() {
continue;
}
if t.asked.is_some_and(|a| now.duration_since(a) < interval) {
continue;
}
self.request_capture(i, qh);
}
}
}
// --- event plumbing -------------------------------------------------------
impl Dispatch<ExtImageCopyCaptureSessionV1, usize> for App {
fn event(
app: &mut Self,
_: &ExtImageCopyCaptureSessionV1,
event: ext_image_copy_capture_session_v1::Event,
&i: &usize,
_: &Connection,
_: &QueueHandle<Self>,
) {
let Some(tile) = app.tiles.get_mut(i) else {
return;
};
match event {
ext_image_copy_capture_session_v1::Event::BufferSize { width, height } => {
tile.size = (width, height)
}
ext_image_copy_capture_session_v1::Event::ShmFormat {
format: WEnum::Value(f),
} => tile.formats.push(f),
ext_image_copy_capture_session_v1::Event::Done => tile.session_done = true,
ext_image_copy_capture_session_v1::Event::Stopped => tile.settled = true,
_ => {}
}
}
}
impl Dispatch<ExtImageCopyCaptureFrameV1, usize> for App {
fn event(
app: &mut Self,
_: &ExtImageCopyCaptureFrameV1,
event: ext_image_copy_capture_frame_v1::Event,
&i: &usize,
_: &Connection,
_: &QueueHandle<Self>,
) {
let Some(tile) = app.tiles.get_mut(i) else {
return;
};
match event {
ext_image_copy_capture_frame_v1::Event::Transform {
transform: WEnum::Value(t),
} => tile.transform = t,
ext_image_copy_capture_frame_v1::Event::Ready => {
// The protocol wants the frame destroyed once ready; the buffer
// stays ours to display.
if let Some(frame) = tile.frame.take() {
frame.destroy();
}
app.frame_ready(i);
}
ext_image_copy_capture_frame_v1::Event::Failed { reason } => {
// Live mode just retries on the next tick; only a failure with no
// frame yet leaves the tile without a thumbnail.
// Live mode retries on the next tick; only a failure with no
// frame yet leaves the tile without a thumbnail.
if tile.frames == 0 {
eprintln!(
"wl-pick: capture failed for {:?} ({reason:?})",
tile.target.title
);
}
tile.settled = true;
if let Some(slot) = tile.filling.take() {
tile.slots[slot].busy = false;
}
if let Some(frame) = tile.frame.take() {
frame.destroy();
}
}
_ => {}
}
}
}
/// A released capture buffer is a slot we may capture into again.
///
/// Release is the whole contract: with wl_shm the compositor copies the pixels
/// out at commit and hands the buffer straight back, so the slot currently on
/// screen is usually free too. (Waiting for it to stop being the displayed slot
/// instead would deadlock — that release never comes twice.)
impl Dispatch<WlBuffer, (usize, usize)> for App {
fn event(
app: &mut Self,
_: &WlBuffer,
event: wl_buffer::Event,
&(tile, slot): &(usize, usize),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let wl_buffer::Event::Release = event
&& let Some(t) = app.tiles.get_mut(tile)
{
t.slots[slot].busy = false;
}
}
}
/// Frame callbacks are the clock for live updates: they arrive as the compositor
/// presents the overlay, so re-captures stop when it is not being shown.
impl Dispatch<wl_callback::WlCallback, ()> for App {
fn event(
app: &mut Self,
_: &wl_callback::WlCallback,
event: wl_callback::Event,
_: &(),
_: &Connection,
qh: &QueueHandle<Self>,
) {
if let wl_callback::Event::Done { .. } = event {
app.tick(qh);
app.arm_frame_callback(qh);
}
}
}
+175
View File
@@ -0,0 +1,175 @@
//! Command line: flags, defaults, and the help text that documents them.
use std::time::Duration;
use crate::app::Settings;
use crate::capture::Live;
use crate::target::Format;
use crate::theme::Theme;
const HELP: &str = "\
wl-pick — a live grid of window and display previews, for picking one
usage: wl-pick [options]
--format tsv|json|portal how to report the pick [tsv]
--live all|current|none which tiles keep updating live [all]
(displays are always a single snapshot)
--fps N cap on live updates per tile per second [12]
--no-outputs windows only; displays are included by default
--hide-labels draw an icon-only grid
--font FAMILY label font family [Berkeley Mono]
--font-size PX label size in logical px [13.3]
--timeout SECS exit anyway after SECS, in case the keyboard
grab ever traps you [off]
-v, --verbose phase timings, tile list and capture stats
-h, --help this
keys: arrows, hjkl or Tab/Shift+Tab move; Home/End jump; Enter picks;
Escape or q cancels
The pick goes to stdout and nothing does if you cancel, so exit status is
0 for a pick and 1 for a cancel. Acting on it is the caller's job.
formats:
tsv TYPE<TAB>ID<TAB>TOPLEVEL_ID<TAB>APP<TAB>TITLE, e.g.
window 1234 f0e1d2c3b4a59687 firefox Wikipedia
output HDMI-A-1 display HDMI-A-1
ID is the thing to act on: a sway con_id, or the display name.
TOPLEVEL_ID is the ext-foreign-toplevel-list-v1 identifier that
capture tools address a window by (grim -T, the desktop portal),
empty for a display.
json the same fields as one object, every key always present, for jq
portal \"Window: TOPLEVEL_ID\" or \"Monitor: NAME\", what
xdg-desktop-portal-wlr's simple chooser reads:
[screencast]
chooser_type=simple
chooser_cmd=wl-pick --format portal
focusing on sway:
IFS=$'\\t' read -r type id toplevel app title < <(wl-pick) &&
case $type in
window) swaymsg \"[con_id=$id] focus\" ;;
output) swaymsg \"focus output $id\" ;;
esac
";
pub struct Args {
pub(crate) format: Format,
pub(crate) outputs: bool,
pub(crate) verbose: bool,
pub(crate) hide_labels: bool,
pub(crate) font: Option<String>,
pub(crate) font_size: Option<f32>,
pub(crate) live: Live,
pub(crate) fps: u32,
pub(crate) timeout: Option<Duration>,
}
impl Args {
/// An exclusive keyboard grab makes a hung overlay unusable, so keep an
/// escape hatch that cannot itself deadlock: a thread that only exits.
pub fn arm_timeout(&self) {
if let Some(d) = self.timeout {
std::thread::spawn(move || {
std::thread::sleep(d);
eprintln!("wl-pick: timeout");
std::process::exit(2);
});
}
}
/// Everything the overlay needs to know up front. `scale` comes from the
/// compositor, not the command line, so it is passed in.
pub fn settings(&self, scale: i32) -> Settings {
Settings {
theme: self.theme(),
live: self.live,
fps: self.fps,
scale,
}
}
/// The look, with any overrides applied. Line height follows the font size
/// unless the size came from the theme, where it is already tuned.
fn theme(&self) -> Theme {
let base = Theme::default();
let font_px = self.font_size.unwrap_or(base.font_px);
Theme {
labels: !self.hide_labels,
font: self.font.clone().unwrap_or_else(|| base.font.clone()),
line_h: match self.font_size {
Some(_) => (font_px * 1.3).ceil() as i32,
None => base.line_h,
},
font_px,
..base
}
}
}
pub fn parse_args() -> Result<Args, String> {
let mut args = Args {
format: Format::Tsv,
outputs: true,
verbose: false,
hide_labels: false,
font: None,
font_size: None,
live: Live::All,
fps: 12,
timeout: None,
};
let mut it = std::env::args().skip(1);
while let Some(arg) = it.next() {
match arg.as_str() {
"--format" => {
args.format = match it.next().ok_or("--format needs tsv|json|portal")?.as_str() {
"tsv" => Format::Tsv,
"json" => Format::Json,
"portal" => Format::Portal,
other => return Err(format!("bad --format: {other}")),
}
}
"--outputs" => args.outputs = true,
"--no-outputs" => args.outputs = false,
"-v" | "--verbose" => args.verbose = true,
"--hide-labels" => args.hide_labels = true,
"--live" => {
args.live = match it.next().ok_or("--live needs all|current|none")?.as_str() {
"all" => Live::All,
"current" => Live::Current,
"none" => Live::None,
other => return Err(format!("bad --live: {other}")),
}
}
"--fps" => {
let v = it.next().ok_or("--fps needs a number")?;
args.fps = v.parse().map_err(|_| format!("bad --fps: {v}"))?;
}
"--font" => args.font = Some(it.next().ok_or("--font needs a family name")?),
"--font-size" => {
let v = it.next().ok_or("--font-size needs px")?;
args.font_size = Some(v.parse().map_err(|_| format!("bad --font-size: {v}"))?);
}
"--timeout" => {
let v = it.next().ok_or("--timeout needs seconds")?;
let secs: f64 = v.parse().map_err(|_| format!("bad --timeout: {v}"))?;
args.timeout = Some(Duration::from_secs_f64(secs));
}
"-h" | "--help" => {
print!("{HELP}");
std::process::exit(0);
}
other => return Err(format!("unknown argument: {other}")),
}
}
Ok(args)
}
+133 -1237
View File
File diff suppressed because it is too large Load Diff
+276
View File
@@ -0,0 +1,276 @@
//! The overlay itself: a layer surface for the chrome, one subsurface per tile,
//! and the keyboard.
//!
//! Scaling is the compositor's job. A tile attaches its capture buffer directly
//! and wp_viewporter names the rectangle to fit it into, so nothing here touches
//! a pixel of window content — only the background, selection and labels.
use std::error::Error;
use std::os::fd::AsFd;
use wayland_client::protocol::{
wl_keyboard::{self, WlKeyboard},
wl_seat::{self, WlSeat},
wl_shm,
};
use wayland_client::{Connection, Dispatch, QueueHandle, WEnum};
use wayland_protocols_wlr::layer_shell::v1::client::{
zwlr_layer_shell_v1::Layer,
zwlr_layer_surface_v1::{self, KeyboardInteractivity, ZwlrLayerSurfaceV1},
};
use crate::app::{App, Ending};
use crate::shm;
use crate::theme::{Rect, fit_centred};
// evdev keycodes: physical positions, so navigation works on any keyboard
// layout without an xkb keymap. Reading typed characters would need one.
const KEY_ESC: u32 = 1;
const KEY_TAB: u32 = 15;
const KEY_Q: u32 = 16;
// hjkl, by physical position: the same keys as vim on a qwerty layout.
const KEY_H: u32 = 35;
const KEY_J: u32 = 36;
const KEY_K: u32 = 37;
const KEY_L: u32 = 38;
const KEY_ENTER: u32 = 28;
const KEY_LEFTSHIFT: u32 = 42;
const KEY_RIGHTSHIFT: u32 = 54;
const KEY_KPENTER: u32 = 96;
const KEY_HOME: u32 = 102;
const KEY_UP: u32 = 103;
const KEY_LEFT: u32 = 105;
const KEY_RIGHT: u32 = 106;
const KEY_END: u32 = 107;
const KEY_DOWN: u32 = 108;
impl App {
/// Map the overlay: a layer surface sized to hug the grid, plus the shm the
/// chrome is painted into.
pub fn show(&mut self, qh: &QueueHandle<Self>) -> Result<(), Box<dyn Error>> {
let (lw, lh) = (self.layout.width, self.layout.height);
let surface = self.compositor.create_surface(qh, ());
let layer = self.layer_shell.get_layer_surface(
&surface,
None, // let the compositor place it on the active output
Layer::Overlay,
"wl-pick".to_string(),
qh,
(),
);
layer.set_size(lw as u32, lh as u32);
layer.set_keyboard_interactivity(KeyboardInteractivity::Exclusive);
surface.set_buffer_scale(self.scale);
surface.commit();
let (pw, ph) = (lw * self.scale, lh * self.scale);
let len = shm::Chrome::slot_len(pw, ph) * shm::Chrome::SLOTS;
let file = shm::memfd("wl-pick-chrome", len)?;
let pool = self.shm.create_pool(file.as_fd(), len as i32, qh, ());
for slot in 0..shm::Chrome::SLOTS {
self.chrome_buffers.push(pool.create_buffer(
(slot * shm::Chrome::slot_len(pw, ph)) as i32,
pw,
ph,
shm::Chrome::stride(pw),
wl_shm::Format::Argb8888,
qh,
(),
));
}
pool.destroy();
self.chrome = Some(shm::Chrome::new(&file, pw, ph)?);
self.surface = Some(surface);
Ok(())
}
/// Attach each captured buffer to its own subsurface and let the compositor
/// scale it into the tile rectangle.
pub fn place_tiles(&mut self, qh: &QueueHandle<Self>) {
let parent = self.surface.clone().expect("show() runs first");
for i in 0..self.tiles.len() {
if !self.tiles[i].ready {
continue;
}
let (bw, bh) = self.tiles[i].size;
let (fit_w, fit_h) = if self.tiles[i].rotated() {
(bh as i32, bw as i32)
} else {
(bw as i32, bh as i32)
};
let dst = fit_centred(fit_w, fit_h, self.layout.tile(i as i32));
let surface = self.compositor.create_surface(qh, ());
let subsurface = self.subcompositor.get_subsurface(&surface, &parent, qh, ());
let viewport = self.viewporter.get_viewport(&surface, qh, ());
subsurface.set_position(dst.x, dst.y);
// Tiles change independently of the chrome — a live frame arrives
// whenever its window does — so they must not wait on a parent
// commit.
subsurface.set_desync();
// The capture protocol reports the transform the compositor already
// applied to the buffer, which is exactly what this request means,
// so it passes straight through and the compositor un-rotates it.
surface.set_buffer_transform(self.tiles[i].transform);
viewport.set_destination(dst.w, dst.h);
let slot = self.tiles[i].showing.expect("a ready tile has a slot");
surface.attach(Some(&self.tiles[i].slots[slot].buffer), 0, 0);
surface.damage_buffer(0, 0, bw as i32, bh as i32);
surface.commit();
let t = &mut self.tiles[i];
t.surface = Some(surface);
t.subsurface = Some(subsurface);
t.viewport = Some(viewport);
}
// Subsurface placement is *parent* state: it only takes effect when the
// parent commits, desynced children included.
parent.commit();
}
/// Repaint background, selection highlight, labels and border.
pub fn paint(&mut self) {
let (scale, sel) = (self.scale, self.sel);
let elem = self.layout.elem(sel as i32).scaled(scale);
// Gather geometry before borrowing the chrome and the labels together.
let label_boxes: Vec<(usize, Rect)> = (0..self.tiles.len())
.filter_map(|i| self.layout.label(i as i32).map(|r| (i, r.scaled(scale))))
.collect();
let t = &self.theme;
let (bg, sel_bg, fg, sel_fg, border, border_px) = (
t.bg,
t.sel_bg,
t.fg,
t.sel_fg,
t.border,
t.border_px * scale,
);
let labels = self.labels.as_mut();
let Some(chrome) = self.chrome.as_mut() else {
return;
};
let slot = chrome.next_slot();
let (cw, ch) = (chrome.w, chrome.h);
let mut p = chrome.painter();
p.fill(bg);
// The selection fills the whole element box, padding included — the same
// thing rofi's element background does.
p.rect(elem, sel_bg);
if let Some(labels) = labels {
for (i, at) in label_boxes {
labels.draw(&mut p, i, at, if i == sel { sel_fg } else { fg });
}
}
p.frame(border_px, border);
let surface = self.surface.clone().expect("show() runs first");
surface.attach(self.chrome_buffers.get(slot), 0, 0);
surface.damage_buffer(0, 0, cw, ch);
surface.commit();
}
fn move_sel(&mut self, delta: i32) {
let n = self.tiles.len() as i32;
if n == 0 {
return;
}
self.sel = (self.sel as i32 + delta).rem_euclid(n) as usize;
self.paint();
}
fn move_row(&mut self, rows: i32) {
let n = self.tiles.len() as i32;
let target = self.sel as i32 + rows * self.layout.cols;
if target >= 0 && target < n {
self.sel = target as usize;
self.paint();
}
}
fn key(&mut self, code: u32) {
match code {
KEY_LEFTSHIFT | KEY_RIGHTSHIFT => self.shift = true,
KEY_ESC | KEY_Q => self.ending = Ending::Cancelled,
KEY_ENTER | KEY_KPENTER => {
self.picked = self.tiles.get(self.sel).map(|t| t.target.clone());
self.ending = Ending::Picked;
}
KEY_TAB if self.shift => self.move_sel(-1),
KEY_TAB | KEY_RIGHT | KEY_L => self.move_sel(1),
KEY_LEFT | KEY_H => self.move_sel(-1),
KEY_DOWN | KEY_J => self.move_row(1),
KEY_UP | KEY_K => self.move_row(-1),
KEY_HOME => {
self.sel = 0;
self.paint();
}
KEY_END => {
self.sel = self.tiles.len().saturating_sub(1);
self.paint();
}
_ => {}
}
}
}
// --- event plumbing -------------------------------------------------------
impl Dispatch<ZwlrLayerSurfaceV1, ()> for App {
fn event(
app: &mut Self,
layer: &ZwlrLayerSurfaceV1,
event: zwlr_layer_surface_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zwlr_layer_surface_v1::Event::Configure { serial, .. } => {
layer.ack_configure(serial);
app.configured = true;
}
zwlr_layer_surface_v1::Event::Closed => app.ending = Ending::Closed,
_ => {}
}
}
}
impl Dispatch<WlSeat, ()> for App {
fn event(
_: &mut Self,
seat: &WlSeat,
event: wl_seat::Event,
_: &(),
_: &Connection,
qh: &QueueHandle<Self>,
) {
if let wl_seat::Event::Capabilities {
capabilities: WEnum::Value(caps),
} = event
&& caps.contains(wl_seat::Capability::Keyboard)
{
seat.get_keyboard(qh, ());
}
}
}
impl Dispatch<WlKeyboard, ()> for App {
fn event(
app: &mut Self,
_: &WlKeyboard,
event: wl_keyboard::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
if let wl_keyboard::Event::Key { key, state, .. } = event {
match state {
WEnum::Value(wl_keyboard::KeyState::Pressed) => app.key(key),
WEnum::Value(wl_keyboard::KeyState::Released)
if key == KEY_LEFTSHIFT || key == KEY_RIGHTSHIFT =>
{
app.shift = false
}
_ => {}
}
}
}
}
+20 -8
View File
@@ -38,14 +38,26 @@ fn collect(node: &Node, out: &mut Vec<Target>) {
}
}
/// The largest integer scale in use, which is what the overlay renders at.
pub fn scale(conn: &mut Connection) -> Result<i32, swayipc::Error> {
Ok(conn
/// The active displays, and the scale the overlay should render at: the largest
/// in use, rounded up, since a buffer can be downscaled but not invented.
pub struct Displays {
pub(crate) names: Vec<String>,
pub(crate) scale: i32,
}
pub fn displays(conn: &mut Connection) -> Result<Displays, swayipc::Error> {
let active: Vec<_> = conn
.get_outputs()?
.iter()
.into_iter()
.filter(|o| o.active)
.map(|o| o.scale.unwrap_or(1.0).ceil() as i32)
.max()
.unwrap_or(1)
.max(1))
.collect();
Ok(Displays {
scale: active
.iter()
.map(|o| o.scale.unwrap_or(1.0).ceil() as i32)
.max()
.unwrap_or(1)
.max(1),
names: active.into_iter().map(|o| o.name).collect(),
})
}
+21
View File
@@ -6,6 +6,17 @@
use std::fmt;
/// How a pick is written to stdout.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Format {
/// Tab-separated columns, for `IFS=$'\t' read` or cut(1).
Tsv,
/// The same record as one JSON object.
Json,
/// What xdg-desktop-portal-wlr's `simple` chooser accepts.
Portal,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Window,
@@ -113,6 +124,16 @@ impl Target {
)
}
/// Render for `format`. `None` means this target cannot be named in that
/// format, which only the portal one can fail at.
pub fn render(&self, format: Format) -> Option<String> {
match format {
Format::Tsv => Some(self.tsv()),
Format::Json => Some(self.json()),
Format::Portal => self.portal(),
}
}
/// What xdg-desktop-portal-wlr's `simple` chooser accepts: `Monitor: NAME`
/// or `Window: <foreign-toplevel identifier>`. A window the compositor never
/// gave an identifier for cannot be named this way, hence the Option — and
+12
View File
@@ -155,6 +155,18 @@ pub struct Rect {
pub h: i32,
}
impl Rect {
/// Logical to physical, for painting into a scaled buffer.
pub fn scaled(self, scale: i32) -> Self {
Self {
x: self.x * scale,
y: self.y * scale,
w: self.w * scale,
h: self.h * scale,
}
}
}
/// Scale (w, h) to fit inside (bw, bh), keeping the aspect ratio, and centre it.
/// Windows are usually portrait-ish next to a 16:9 cell, so this letterboxes the
/// same way rofi's `element-icon { size: W H }` does.