hex/src/linux_rune_hud.rs
Dax 6d86e58f82
HUD: draw the macOS shaders on Linux and Windows through Rune, with Dot scope (#318)
Port dictation_indicator.metal to WGSL (rune_hud.wgsl) and draw it on the GPU
from a Rune overlay compositor shared by Linux and Windows (rune_hud.rs). This
replaces the Windows CPU shader port and the Linux text pill, adds Dot scope
with its auto level and processing looks when the dot-scope experiment is on,
and feeds the Linux HUD the listener's meter and job outcomes.
2026-10-28 19:12:47 -04:00

135 lines
4.9 KiB
Rust

//! The Linux recording HUD: the shared Rune HUD (`rune_hud`), the macOS
//! capsule or Dot scope drawn on the GPU, as an overlay in the Hex process.
//! Rune makes it a layer-shell surface on Wayland (overlay layer, no
//! keyboard, empty input region) and an override-redirect window with an
//! empty input shape on X11, so it never takes focus or a click.
//!
//! It only observes: the listener reports its capture, meter, and pending
//! output through `LinuxIndicator`, which never waits on the HUD and turns
//! those reports into the HUD's events. The listener can start before the
//! window's app exists; reports before `install` are dropped.
use std::collections::VecDeque;
use std::sync::{Mutex, OnceLock};
use rune::{Availability, OverlayHandle};
use crate::app_settings::IndicatorPosition;
use crate::rune_hud::{Hud, HudEvent, HudSender};
/// The newest listener's generation: an older listener's reports stop counting.
static GENERATION: Mutex<u64> = Mutex::new(0);
/// Where the newest listener wants the HUD, for an `install` after it starts.
static POSITION: Mutex<IndicatorPosition> = Mutex::new(IndicatorPosition::Top);
static HUD: OnceLock<(HudSender, OverlayHandle<Hud>)> = OnceLock::new();
/// A listener's handle on the HUD; dropping it hides what it reported.
pub(crate) struct LinuxIndicator {
generation: Option<u64>,
recording: bool,
/// Submitted dictations still being transcribed or pasted, oldest first.
jobs: VecDeque<u64>,
next_job: u64,
}
impl LinuxIndicator {
pub(crate) fn new(position: IndicatorPosition) -> Self {
*POSITION.lock().unwrap_or_else(|error| error.into_inner()) = position;
if let Some((sender, _)) = HUD.get() {
sender.set_position(position);
}
let generation = (position != IndicatorPosition::Hidden).then(|| {
let mut current = GENERATION.lock().unwrap_or_else(|error| error.into_inner());
*current += 1;
*current
});
let indicator = Self {
generation,
recording: false,
jobs: VecDeque::new(),
next_job: 0,
};
indicator.send(HudEvent::Reset);
indicator
}
fn sender(&self) -> Option<&'static HudSender> {
let generation = self.generation?;
let current = *GENERATION.lock().unwrap_or_else(|error| error.into_inner());
(generation == current)
.then(|| HUD.get().map(|(sender, _)| sender))
.flatten()
}
fn send(&self, event: HudEvent) {
if let Some(sender) = self.sender() {
sender.send(event);
}
}
/// The listener's state after each turn: whether it records, and how
/// many dictations it submitted that have not finished.
pub(crate) fn update(&mut self, recording: bool, pending: usize) {
if recording && !self.recording {
self.send(HudEvent::Started);
}
let submitted = pending > self.jobs.len();
while pending > self.jobs.len() {
self.next_job += 1;
self.jobs.push_back(self.next_job);
self.send(HudEvent::Submitted {
job_id: self.next_job,
});
}
if !recording && self.recording && !submitted {
self.send(HudEvent::Cancelled);
}
// Dictations that ended without `finished` (the listener stopping).
while pending < self.jobs.len() {
if let Some(job_id) = self.jobs.pop_front() {
self.send(HudEvent::JobCancelled { job_id });
}
}
self.recording = recording;
}
/// The oldest submitted dictation finished: pasted, or failed.
pub(crate) fn finished(&mut self, pasted: bool) {
if let Some(job_id) = self.jobs.pop_front() {
self.send(if pasted {
HudEvent::JobCompleted { job_id }
} else {
HudEvent::JobFailed { job_id }
});
}
}
/// Captured audio while recording, for the level the HUD shows.
pub(crate) fn meter(&self, samples: &[f32]) {
if let Some(sender) = self.sender() {
sender.meter(samples);
}
}
}
impl Drop for LinuxIndicator {
fn drop(&mut self) {
self.send(HudEvent::Reset);
}
}
/// Adds the HUD overlay to the window's app and starts its thread.
pub(crate) fn install<S: 'static>(app: &mut rune::App<S>) {
let handle = crate::rune_hud::install(app);
let overlay = handle.overlay();
let position = *POSITION.lock().unwrap_or_else(|error| error.into_inner());
let _ = HUD.set((handle.start(position), overlay));
}
/// Why the HUD can't show, once Rune has tried.
pub(crate) fn limitation() -> Option<String> {
let (_, overlay) = HUD.get()?;
(overlay.availability() == Availability::Unavailable).then(|| {
"Recording HUD unavailable: this compositor does not support Wayland layer-shell. Dictation still works.".into()
})
}