agent: wayland — evdev hotkey backend, download resume, SHA256 integrity

Add kon-hotkey crate with evdev-based global hotkey capture that works on
Wayland (and X11). Patterns from whisper-overlay: per-device async listeners,
inotify hotplug with udev permission retry, watch channel for live config
updates. Frontend detects Wayland at startup and selects evdev or
tauri-plugin-global-shortcut automatically.

Model downloads now support HTTP Range resume for interrupted downloads and
optional SHA256 integrity verification (incremental, no second pass).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 17:50:48 +01:00
parent 1933604176
commit 8e70cf9ff9
12 changed files with 804 additions and 18 deletions

View File

@@ -0,0 +1,107 @@
use std::sync::Arc;
use tauri::Emitter;
use tokio::sync::{mpsc, Mutex};
use kon_hotkey::{EvdevHotkeyListener, HotkeyCombo, HotkeyEvent};
/// Managed state for the evdev hotkey listener.
pub struct HotkeyState {
listener: Arc<Mutex<Option<EvdevHotkeyListener>>>,
}
impl HotkeyState {
pub fn new() -> Self {
Self {
listener: Arc::new(Mutex::new(None)),
}
}
}
/// Check whether the current session is running on Wayland.
#[tauri::command]
pub fn is_wayland_session() -> bool {
std::env::var("WAYLAND_DISPLAY").is_ok()
|| std::env::var("XDG_SESSION_TYPE")
.map(|v| v == "wayland")
.unwrap_or(false)
}
/// Check whether evdev hotkey capture is available (user in `input` group, etc.).
#[tauri::command]
pub fn check_hotkey_access() -> Result<(), String> {
kon_hotkey::check_evdev_access()
}
/// Start the evdev global hotkey listener. Emits "kon:hotkey-pressed" and
/// "kon:hotkey-released" events to the frontend.
///
/// If a listener is already running, it is stopped first.
#[tauri::command]
pub async fn start_evdev_hotkey(
app: tauri::AppHandle,
state: tauri::State<'_, HotkeyState>,
hotkey: String,
) -> Result<(), String> {
let combo = HotkeyCombo::from_tauri_str(&hotkey)
.ok_or_else(|| format!("Cannot parse hotkey: {hotkey}"))?;
// Stop existing listener if any
let mut guard = state.listener.lock().await;
if let Some(existing) = guard.take() {
existing.stop().await;
}
let (event_tx, mut event_rx) = mpsc::channel::<HotkeyEvent>(64);
let listener = EvdevHotkeyListener::start(combo, event_tx);
*guard = Some(listener);
drop(guard);
// Forward evdev events to Tauri event bus
let app_clone = app.clone();
tokio::spawn(async move {
while let Some(event) = event_rx.recv().await {
match event {
HotkeyEvent::Pressed => {
let _ = app_clone.emit("kon:hotkey-pressed", ());
}
HotkeyEvent::Released => {
let _ = app_clone.emit("kon:hotkey-released", ());
}
}
}
});
Ok(())
}
/// Update the hotkey combo on a running listener.
#[tauri::command]
pub async fn update_evdev_hotkey(
state: tauri::State<'_, HotkeyState>,
hotkey: String,
) -> Result<(), String> {
let combo = HotkeyCombo::from_tauri_str(&hotkey)
.ok_or_else(|| format!("Cannot parse hotkey: {hotkey}"))?;
let guard = state.listener.lock().await;
if let Some(ref listener) = *guard {
listener.set_hotkey(combo);
Ok(())
} else {
Err("Hotkey listener not running".to_string())
}
}
/// Stop the evdev hotkey listener.
#[tauri::command]
pub async fn stop_evdev_hotkey(
state: tauri::State<'_, HotkeyState>,
) -> Result<(), String> {
let mut guard = state.listener.lock().await;
if let Some(listener) = guard.take() {
listener.stop().await;
}
Ok(())
}