Files
VoelinClient/crates/voelin-platform/src/hotkey/tracker.rs
T
Claude e050148a32 Rename the project to Voelin; packages hold only the app, tsgw is a Linux server package
The product is Voelin (app id io.github.faumaray.Voelin), everywhere:
- crates tsc-* -> voelin-* (and voelin-fuzz), the desktop binary
  tsc-desktop -> voelin, the development CLI tsctl -> voelinctl;
- the Android app: applicationId, Kotlin package io.github.faumaray.voelin,
  VoelinApp, libvoelin_android.so (JNI class names follow);
- TSC_* environment variables -> VOELIN_*, Gradle properties tsc.* ->
  voelin.*, signing secrets VOELIN_SIGNING_*;
- the data/config/cache/state folder tsc -> voelin, the keyring service,
  thread names, the dev compose project;
- desktop file, metainfo, icon, Flatpak manifest, NSIS installer, docs.
The gateway keeps its name: bin tsgw (crate voelin-gateway), TSGW_* env
vars, the tsgw.v1+json protocol, the tsgw image. The window title and the
About page show "Voelin <version>" from voelin_platform::APP_NAME.

Packages (scripts/package.sh): the app only (voelin, voelin.exe, APKs,
Flatpak). voelinctl is a development tool and in no package. tsgw is for
servers and now its own Linux package, tsgw_<v>_amd64.deb and
tsgw-<v>-linux-x86_64.tar.gz, with a sandboxed systemd unit
(packaging/linux/tsgw.service; /etc/tsgw config, /var/lib/tsgw state) and
dependencies of its own (no GUI libraries). docs/gateway-admin.md has an
Install section.

Fixes:
- package-windows: makensis on Windows found no file through "D:/a/..."
  paths; pass Windows paths (cygpath -w).
- StartupWMClass is the app id, which the app sets as its X11 WM_CLASS
  (it was the binary name, so desktops could not match the window).
- Gradle wrapper download timeout 10 s -> 60 s (slow proxies).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-26 21:11:17 +00:00

148 lines
3.8 KiB
Rust

//! Turns raw key presses and releases into hotkey events. Used by the
//! backends that see every key (X11 raw events, the Windows keyboard hook);
//! the portal does its own matching.
use std::collections::HashSet;
use tokio::sync::mpsc;
use super::{Hotkey, HotkeyEvent, HotkeyEvents, Key, Modifiers};
struct Registration {
id: String,
hotkey: Hotkey,
events: mpsc::UnboundedSender<HotkeyEvent>,
active: bool,
}
#[derive(Default)]
pub(crate) struct Tracker {
registrations: Vec<Registration>,
held: HashSet<Key>,
}
impl Tracker {
/// Register (or replace) `id`.
pub fn add(&mut self, id: &str, hotkey: Hotkey) -> HotkeyEvents {
self.remove(id);
let (tx, rx) = mpsc::unbounded_channel();
self.registrations.push(Registration { id: id.into(), hotkey, events: tx, active: false });
rx
}
pub fn remove(&mut self, id: &str) -> bool {
let before = self.registrations.len();
self.registrations.retain(|r| r.id != id);
self.registrations.len() != before
}
#[cfg(test)]
pub fn is_empty(&self) -> bool {
self.registrations.is_empty()
}
fn held_modifiers(&self) -> Modifiers {
let mut m = Modifiers::NONE;
for kind in self.held.iter().filter_map(|k| k.modifier()) {
m.add(kind);
}
m
}
/// A key went down (`pressed`) or up. Auto-repeat presses are ignored.
pub fn key(&mut self, key: Key, pressed: bool) {
if pressed {
self.held.insert(key);
let held = self.held_modifiers();
for r in &mut self.registrations {
if !r.active && r.hotkey.key == key && r.hotkey.modifiers.is_subset_of(held) {
r.active = true;
let _ = r.events.send(HotkeyEvent::Pressed);
}
}
} else {
self.held.remove(&key);
for r in &mut self.registrations {
if r.active && r.hotkey.key == key {
r.active = false;
let _ = r.events.send(HotkeyEvent::Released);
}
}
}
// Receivers that were dropped unregister themselves.
self.registrations.retain(|r| !r.events.is_closed());
}
/// Release everything (e.g. the keyboard state was lost).
pub fn release_all(&mut self) {
self.held.clear();
for r in &mut self.registrations {
if r.active {
r.active = false;
let _ = r.events.send(HotkeyEvent::Released);
}
}
}
}
#[cfg(test)]
mod tests {
use super::super::NamedKey;
use super::*;
fn drain(rx: &mut HotkeyEvents) -> Vec<HotkeyEvent> {
std::iter::from_fn(|| rx.try_recv().ok()).collect()
}
#[test]
fn press_release_with_modifiers() {
use HotkeyEvent::*;
let mut t = Tracker::default();
let mut ptt = t.add("ptt", "Ctrl+F13".parse().unwrap());
let mut plain = t.add("mute", "M".parse().unwrap());
let ctrl = Key::Named(NamedKey::RightCtrl);
// Without Ctrl nothing happens for Ctrl+F13.
t.key(Key::F(13), true);
t.key(Key::F(13), false);
assert!(drain(&mut ptt).is_empty());
t.key(ctrl, true);
t.key(Key::F(13), true);
t.key(Key::F(13), true); // auto-repeat
// Letting go of Ctrl first keeps it pressed until F13 is released.
t.key(ctrl, false);
assert_eq!(drain(&mut ptt), vec![Pressed]);
t.key(Key::F(13), false);
assert_eq!(drain(&mut ptt), vec![Released]);
// Extra modifiers do not block a plain hotkey.
t.key(Key::Named(NamedKey::LeftShift), true);
t.key(Key::Letter('M'), true);
t.key(Key::Letter('M'), false);
assert_eq!(drain(&mut plain), vec![Pressed, Released]);
}
#[test]
fn modifier_alone_and_unregister() {
use HotkeyEvent::*;
let mut t = Tracker::default();
let mut rx = t.add("ptt", "RightCtrl".parse().unwrap());
let key = Key::Named(NamedKey::RightCtrl);
t.key(key, true);
t.release_all();
assert_eq!(drain(&mut rx), vec![Pressed, Released]);
assert!(t.remove("ptt"));
assert!(!t.remove("ptt"));
t.key(key, true);
assert!(rx.try_recv().is_err());
// A dropped receiver unregisters on the next key.
let rx = t.add("x", "A".parse().unwrap());
drop(rx);
t.key(Key::Letter('B'), true);
assert!(t.is_empty());
}
}