mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-17 01:00:41 +01:00
Wayland/wlroots: type mobile IME text through the virtual keyboard's spare keycodes (#51)
The mobile clients' input method sends `text_input {content}` over CONTROL. Upstream only
implements it for X11, so a Wayland host (Hyprland here) dropped the characters with a
warning. Wire it up: characters the session keymap already produces are pressed as
ordinary keycodes, and characters it cannot produce are carried by spare keycodes (the
keymap has ~33 named but unmapped ones) declared for the duration of one batch, in text
order, then removed again.
The focused client translates keycodes with the keymap the compositor last handed it, and
a keymap uploaded to a keyboard the seat already has is never handed on again: measured on
Hyprland 0.56.2, pressing the remapped keycodes then reproduces the previous message's
text (a space came out as the previous message's first character, and every message
repeated the one before it). Re-attaching the virtual keyboard is what makes the
compositor send the keymap to the client, so each batch does that, waits for the
compositor to install the keymap, and releases held keys first because the new keyboard
starts with none. No timing sleep is needed once the keyboard is re-attached, so there is
none. Plain latin text and spaces never touch the keymap at all. The pristine keymap is
restored after every call, so uu_wl_key keeps its own symbol map and the compositor is
never left with spare keys bound.
`wayland-text-probe` reports how many characters one upload can carry and types a sample;
UU_PROBE_SEQUENCE types one message per |-separated field, the way the phone's input
method sends them, which is what reproduced this locally.
Co-authored-by: Hey Claw <[email protected]>
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
4 files changed
+245
-11
No files matched your search
+15
-1
@@ -360,13 +360,27 @@ impl InputInjector {
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
|
||||
}
|
||||
/// Mobile input-method text (`text_input`). X11 only for now; other backends log once.
|
||||
/// Mobile input-method text (`text_input`). X11 types it directly; Wayland re-maps a spare
|
||||
/// keycode in the virtual keyboard's keymap. Other backends log once.
|
||||
pub fn text_input(&self, text: &str) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return };
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::X11 {
|
||||
let typed=unsafe { x11_type_utf8(c_text.as_ptr()) };
|
||||
log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed);
|
||||
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland {
|
||||
// Escape hatch: keep the old behaviour without rebuilding.
|
||||
if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v|v=="0") { return; }
|
||||
let typed=crate::wayland::type_utf8(text);
|
||||
if typed>=0 {
|
||||
log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed);
|
||||
return;
|
||||
}
|
||||
// A keymap whose keycodes are all mapped cannot carry text; report it once.
|
||||
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||
if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) {
|
||||
log::warn!("Mobile text input unavailable on the Wayland backend (no spare keycode in this keymap)");
|
||||
}
|
||||
} else {
|
||||
static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false);
|
||||
if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) {
|
||||
|
||||
+5
-2
@@ -190,8 +190,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.filter_module("uuremote_server", log::LevelFilter::Info).init();
|
||||
} else { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); }
|
||||
// An unknown argument must never fall through to starting a second full host server.
|
||||
const SUBCOMMANDS: [&str; 13] = ["show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
const SUBCOMMANDS: [&str; 14] = ["show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "wayland-text-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
||||
@@ -353,6 +353,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
|
||||
return wayland::input_probe().map_err(|e|e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("wayland-text-probe") {
|
||||
return wayland::text_probe().map_err(|e|e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("wayland-capture") {
|
||||
return wayland::capture_stdout().map_err(|e| e.into());
|
||||
}
|
||||
|
||||
@@ -8,6 +8,26 @@ extern "C" {
|
||||
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
|
||||
fn uu_wl_wake() -> i32;
|
||||
fn uu_wl_release_all() -> i32;
|
||||
fn uu_wl_type_utf8(text: *const libc::c_char) -> i32;
|
||||
fn uu_wl_text_ready() -> i32;
|
||||
}
|
||||
/// How many characters one keymap upload can carry; 0 means this keymap has no spare keycode.
|
||||
pub fn text_ready()->i32 {
|
||||
#[cfg(all(target_os="linux",feature="wayland"))]
|
||||
{ return unsafe { uu_wl_text_ready() }; }
|
||||
#[cfg(not(all(target_os="linux",feature="wayland")))]
|
||||
-1
|
||||
}
|
||||
/// Mobile input-method text (#13/#46). Returns the number of characters typed, or a negative code
|
||||
/// when this keymap has no spare keycode to carry them. Never logs the text.
|
||||
pub fn type_utf8(text:&str)->i32 {
|
||||
#[cfg(all(target_os="linux",feature="wayland"))]
|
||||
{
|
||||
let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return -1 };
|
||||
return unsafe { uu_wl_type_utf8(c_text.as_ptr()) };
|
||||
}
|
||||
#[cfg(not(all(target_os="linux",feature="wayland")))]
|
||||
{ let _=text; -1 }
|
||||
}
|
||||
pub fn release_all()->i32 {
|
||||
#[cfg(all(target_os="linux",feature="wayland"))]
|
||||
@@ -144,3 +164,33 @@ pub fn input_probe()->Result<(),String> {
|
||||
println!("Wayland test events sent; verify the native GTK acceptance report.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Local diagnostic for the mobile-text path: report the keymap's capacity for it, then type a
|
||||
/// sample into the focused window. Only the built-in sample is echoed; UU_PROBE_TEXT stays private.
|
||||
pub fn text_probe()->Result<(),String> {
|
||||
init()?;
|
||||
let ready=text_ready();
|
||||
if ready<=0 { return Err("this keymap has no spare keycode: mobile text input cannot be carried".into()); }
|
||||
const SAMPLE:&str="uu remote abc 你好世界";
|
||||
let (sample,builtin)=match std::env::var("UU_PROBE_TEXT") {
|
||||
Ok(value)=>(value,false), Err(_)=>(SAMPLE.to_owned(),true),
|
||||
};
|
||||
println!("Wayland text probe: spare keycodes={ready} characters={} bytes={}",sample.chars().count(),sample.len());
|
||||
if builtin { println!("Sample: {sample}"); }
|
||||
// UU_PROBE_SEQUENCE types each |-separated field as its own call - one text_input message each,
|
||||
// the way the phone's input method sends them - with UU_PROBE_STEP_MS between the calls. One
|
||||
// process means one virtual keyboard, exactly as the host has.
|
||||
let step=std::env::var("UU_PROBE_STEP_MS").ok().and_then(|v|v.parse::<u64>().ok()).unwrap_or(0);
|
||||
let sequence=std::env::var("UU_PROBE_SEQUENCE").unwrap_or_else(|_|sample.clone());
|
||||
let mut typed=0;
|
||||
for (index,message) in sequence.split('|').enumerate() {
|
||||
if index>0 && step>0 { std::thread::sleep(std::time::Duration::from_millis(step)); }
|
||||
let one=type_utf8(message);
|
||||
if one<0 { return Err(format!("Wayland text typing failed ({one})")); }
|
||||
typed+=one;
|
||||
}
|
||||
if typed<0 { return Err(format!("Wayland text typing failed ({typed})")); }
|
||||
println!("Typed {typed} of {} characters in {} calls; check that they match.",
|
||||
sequence.chars().filter(|character|*character!='|').count(),sequence.split('|').count());
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user