mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-17 01:00:41 +01:00
uinput input backend and terminal-aware paste
- uinput.rs: kernel virtual keyboard + absolute pointer (Sunshine-style), physical evdev keycodes so the compositor's keysym lookup cannot drop shifted symbols (#3). Auto on Wayland/portal when /dev/uinput is accessible (udev uaccess rule), XTest kept on X11 (it suppresses autorepeat for held keys over latency). UU_INPUT_BACKEND=auto|uinput|native. - Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as VK_LCONTROL). In a focused X11 terminal Ctrl+V becomes Ctrl+Shift+V so it pastes instead of quoting the next key; Ctrl+C always interrupts (copy is Cmd+Shift+C). UU_TERMINAL_SHORTCUTS=0 disables. Verified on the 3090 with the official Mac viewer: paste, interrupt, uinput typing incl. underscores. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ddbc75bbf6
commit
49edf1d389
4 files changed
+395
-2
No files matched your search
+105
-2
@@ -8,7 +8,10 @@ extern "C" {
|
||||
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
|
||||
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
|
||||
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
||||
fn x11_terminal_shortcut_state() -> libc::c_int;
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
||||
@@ -33,11 +36,22 @@ struct PendingPointer {
|
||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||
#[derive(Default)]
|
||||
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
||||
pub struct InputInjector { portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||
#[derive(Default)]
|
||||
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||
|
||||
pub struct InputInjector { shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||
|
||||
impl InputInjector {
|
||||
pub fn new() -> Self {
|
||||
Self { portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||
let backend = crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11);
|
||||
let uinput = if crate::uinput::wanted(backend) {
|
||||
match crate::uinput::Devices::open() {
|
||||
Ok(devices) => { log::info!("Input backend: uinput (kernel virtual keyboard/pointer)"); Some(devices) }
|
||||
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
||||
}
|
||||
} else { None };
|
||||
Self { shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||
}
|
||||
|
||||
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
||||
@@ -49,6 +63,7 @@ impl InputInjector {
|
||||
}
|
||||
|
||||
pub fn mouse_absolute_normalized(&self, x: f64, y: f64, size: (u32,u32)) {
|
||||
*self.screen.lock().unwrap() = size;
|
||||
if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
|
||||
let mut pending = self.pending.lock().unwrap();
|
||||
pending.absolute = Some(position);
|
||||
@@ -117,7 +132,47 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as VK_LCONTROL), which in
|
||||
/// a Linux terminal quotes the next key instead of pasting. In a focused terminal Ctrl+V becomes
|
||||
/// Ctrl+Shift+V. Ctrl+C is never translated: it must always interrupt, and copy is Cmd+Shift+C
|
||||
/// (= Ctrl+Shift+C). X11 only; `UU_TERMINAL_SHORTCUTS=0` disables.
|
||||
fn terminal_shift(&self, key: i32, is_press: bool) -> bool {
|
||||
let bit = match key { 0x11 | 0xa2 | 0xa3 => 1, 0x10 | 0xa0 | 0xa1 => 2, 0x12 | 0xa4 | 0xa5 => 4, 0x5b | 0x5c => 8, _ => 0 };
|
||||
let mut shortcuts = self.shortcuts.lock().unwrap();
|
||||
if bit != 0 {
|
||||
if is_press { shortcuts.modifiers |= bit } else { shortcuts.modifiers &= !bit }
|
||||
return false;
|
||||
}
|
||||
if !is_press { return shortcuts.shifted.remove(&key); }
|
||||
if key != 0x56 || shortcuts.modifiers != 1 { return false; }
|
||||
if std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0") || std::env::var_os("DISPLAY").is_none() { return false; }
|
||||
let shift = unsafe { x11_terminal_shortcut_state() } & 1 != 0;
|
||||
if shift {
|
||||
shortcuts.shifted.insert(key);
|
||||
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
|
||||
log::info!("Terminal shortcut: Ctrl+V sent as Ctrl+Shift+V");
|
||||
}
|
||||
}
|
||||
shift
|
||||
}
|
||||
|
||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||
if self.terminal_shift(key, is_press) {
|
||||
const SHIFT_L: u64 = 0xffe1;
|
||||
if is_press {
|
||||
self.inject_key(SHIFT_L, true);
|
||||
self.key_symbol_event(key, true);
|
||||
} else {
|
||||
self.key_symbol_event(key, false);
|
||||
self.inject_key(SHIFT_L, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.key_symbol_event(key, is_press);
|
||||
}
|
||||
|
||||
fn key_symbol_event(&self, key: i32, is_press: bool) {
|
||||
if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
|
||||
let result = self.inject_key(symbol,is_press);
|
||||
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
@@ -158,19 +213,44 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn uinput_result(result: std::io::Result<()>) -> i32 {
|
||||
match result {
|
||||
Ok(()) => 0,
|
||||
Err(e) => {
|
||||
static FAILURES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if FAILURES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 8 { log::warn!("uinput injection failed: {}", e.kind()); }
|
||||
-1
|
||||
}
|
||||
}
|
||||
}
|
||||
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||
let (w,h)=*self.screen.lock().unwrap();
|
||||
if w > 1 && h > 1 {
|
||||
let scale = |v: i32, extent: u32| (i64::from(v) * i64::from(crate::uinput::ABS_MAX) / i64::from(extent - 1)) as i32;
|
||||
return Self::uinput_result(uinput.move_absolute(scale(x,w),scale(y,h)));
|
||||
}
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
|
||||
else if relative { unsafe { x11_mouse_relative(x,y) } } else { unsafe { x11_mouse_move(x,y) } }
|
||||
}
|
||||
fn inject_button(&self,button:i32,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
|
||||
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||
}
|
||||
fn inject_key(&self,symbol:u64,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||
@@ -213,6 +293,29 @@ mod motion_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shortcut_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn modifiers_are_tracked_and_non_terminal_keys_pass_through() {
|
||||
let input = InputInjector::new();
|
||||
assert!(!input.terminal_shift(0x11, true));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 1);
|
||||
// No X display in tests: Ctrl+V is not translated.
|
||||
assert!(!input.terminal_shift(0x56, true));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x11, false));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 0);
|
||||
// A translated key keeps its synthetic Shift until its own release.
|
||||
input.shortcuts.lock().unwrap().shifted.insert(0x56);
|
||||
assert!(input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
// Ctrl+C is never translated.
|
||||
input.terminal_shift(0x11, true);
|
||||
assert!(!input.terminal_shift(0x43, true));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod scroll_tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user