From 212e4943f9a8b1be0ee540d020fec4cca19bc706 Mon Sep 17 00:00:00 2001 From: Onekki <31927350+Onekki@users.noreply.github.com> Date: Thu, 5 Nov 2026 19:30:25 +0800 Subject: [PATCH] =?UTF-8?q?Hyprland=20=E6=89=8B=E6=9C=BA=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=B3=95=E6=96=87=E5=AD=97=EF=BC=9A=E7=BB=9F=E4=B8=80=E8=B5=B0?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E6=96=87=E5=AD=97=E9=94=AE=E7=9B=98=E5=B9=B6?= =?UTF-8?q?=E6=AF=8F=E6=89=B9=E9=87=8D=E6=96=B0=20attach=20(#176)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 所有 wlroots 合成器使用独立文字键盘、每批重新 attach 下发 keymap;ASCII 也以 Unicode keysym 发送,Wayland 下 text_input 先走 keysym;切换前同步原设备修饰键。 --- c_engine/wayland_engine.c | 94 ++++++++++++++------------------------- src/input.rs | 15 +++++-- 2 files changed, 45 insertions(+), 64 deletions(-) diff --git a/c_engine/wayland_engine.c b/c_engine/wayland_engine.c index b02a69d..b607b18 100644 --- a/c_engine/wayland_engine.c +++ b/c_engine/wayland_engine.c @@ -159,37 +159,10 @@ static size_t symbols_close(const char *text) { } return 0; } -/* Named keycodes the keymap gives no symbol to. Reading the compositor's own list is the only - portable way: pressing an unmapped keycode types nothing, and uu_wl_key never matches one - because it searches by keysym. Highest first, since uu_wl_key takes the lowest match. */ +/* Reusable codes for the separate text-only keyboard. Remapping real printable keys on that device + keeps the physical-key map untouched and is the form Chromium accepts; the evdev "reserved" codes + that have no symbol are discarded by some clients. */ static unsigned find_spare_codes(xkb_keycode_t *out,unsigned limit) { - unsigned count=0; - xkb_keycode_t min=xkb_keymap_min_keycode(keymap); - for(xkb_keycode_t code=xkb_keymap_max_keycode(keymap);code>min && count=sizeof(held_keys) || !xkb_keymap_key_get_name(keymap,code)) continue; - int mapped=0; - for(xkb_level_index_t level=0;level0) mapped=1; - } - if(!mapped) out[count++]=code; - } - return count; -} -/* A keycode the session keymap already produces this symbol with, unshifted. Typing through it - needs no upload, so no client can mistranslate it - and it is how plain latin text and spaces - arrive without touching the keymap at all. Spares never match: the pristine keymap leaves them - unmapped. */ -static xkb_keycode_t existing_keycode(xkb_keysym_t symbol) { - for(xkb_keycode_t code=xkb_keymap_min_keycode(keymap);code<=xkb_keymap_max_keycode(keymap);code++) { - if(code<8 || code>=sizeof(held_keys)) continue; - const xkb_keysym_t *syms; - if(xkb_keymap_key_get_syms_by_level(keymap,code,0,0,&syms)==1 && syms[0]==symbol) return code; - } - return 0; -} -static unsigned find_niri_text_codes(xkb_keycode_t *out,unsigned limit) { - /* Printable physical keys work in Chromium; reserved evdev codes can be discarded. */ static const xkb_keycode_t text_codes[]={24,25,26,27,28,29,30,31,32,33,38,39,40,41,42,43,44,45,46,52,53,54,55,56,57,58}; unsigned count=0; for(unsigned i=0;i=0x80 && cp<0xa0)) continue; /* controls carry no text */ - else if(cp<0x100) sym=cp; /* Latin-1 keysyms equal the code point */ - else sym=(xkb_keysym_t)(0x1000000|cp); /* Unicode keysym */ - xkb_keycode_t existing=getenv("NIRI_SOCKET")?0:existing_keycode(sym); - if(existing) { - if(count && flush_batch(buffer,capacity,names,codes,count,&typed)!=0) { failed=1; break; } - count=0; - press_keycode(existing); typed++; - continue; - } - if(count==(getenv("NIRI_SOCKET")?spare_count:UU_TEXT_SPARE)) { + /* Every character goes out as a Unicode keysym, including ASCII: a plain Latin-1 keysym + is what a running input method composes as pinyin, so "abc" would turn into candidates + instead of reaching the field. */ + else sym=(xkb_keysym_t)(0x1000000|cp); + if(count==spare_count) { if(flush_batch(buffer,capacity,names,codes,count,&typed)!=0) { failed=1; break; } count=0; } @@ -278,23 +250,25 @@ static int type_utf8_locked(const char *text) { } if(!failed && count) failed=flush_batch(buffer,capacity,names,codes,count,&typed)!=0; free(buffer); - /* Restore the pristine keymap whatever happened above, so uu_wl_key keeps its own symbol map. */ - if(getenv("NIRI_SOCKET")) {if(sync_display()!=0) return -1;usleep(8000);} - else if(upload_keymap(keymap_text,keymap_text_len)!=0) return -1; return failed && !typed?-1:typed; } int uu_wl_type_utf8(const char *text) { pthread_mutex_lock(&lock); int typed=-1; if(initialized && text && keymap_text && keymap_insert && spare_count) { - if(getenv("NIRI_SOCKET")) { - /* Only niri uses printable codes on a separate text device; preserve the Hyprland path. */ - release_held_keys_locked(); - struct zwp_virtual_keyboard_v1 *saved=keyboard; - keyboard=zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(keyboard_manager,seat); - if(keyboard) {typed=type_utf8_locked(text);sync_display();usleep(8000);if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);} - keyboard=saved; - } else typed=type_utf8_locked(text); + if(!keyboard_manager) { pthread_mutex_unlock(&lock); return -1; } + /* Text has its own device: remapping printable codes must not change physical-key input. */ + release_held_keys_locked(); + /* The original device stays alive, so key-up alone must not leave its modifiers held. */ + zwp_virtual_keyboard_v1_modifiers(keyboard, + xkb_state_serialize_mods(key_state,XKB_STATE_MODS_DEPRESSED), + xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LATCHED), + xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LOCKED), + xkb_state_serialize_layout(key_state,XKB_STATE_LAYOUT_EFFECTIVE)); + struct zwp_virtual_keyboard_v1 *saved=keyboard; + keyboard=zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(keyboard_manager,seat); + if(keyboard) {typed=type_utf8_locked(text);sync_display();usleep(8000);if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);} + keyboard=saved; } if(typed>=0 && wl_display_flush(display)<0) typed=-1; pthread_mutex_unlock(&lock); return typed; @@ -597,7 +571,7 @@ int uu_wl_init(int32_t *w,int32_t *h) { /* Text typing needs unmapped keycodes and a place to declare them. A keymap without either only costs that path - uu_wl_type_utf8 refuses and the caller keeps its own fallback. */ keymap_insert=symbols_close(keymap_text); - spare_count=getenv("NIRI_SOCKET")?find_niri_text_codes(spare_codes,UU_TEXT_SPARE):find_spare_codes(spare_codes,UU_TEXT_SPARE); + spare_count=find_spare_codes(spare_codes,UU_TEXT_SPARE); } initialized=1; *w=width; *h=height; result=0; end: diff --git a/src/input.rs b/src/input.rs index d791b21..fc5eff3 100644 --- a/src/input.rs +++ b/src/input.rs @@ -742,14 +742,21 @@ impl InputInjector { self.emit_uinput_text(&crate::uinput::text_plan(text)) } - /// Mobile input-method text (`text_input`). uinput types the layout-mappable ASCII prefix - /// without clipboard access. X11 types directly; native Wayland re-maps a spare - /// keycode in the virtual keyboard's keymap. Portal and other Wayland environments inject - /// via clipboard paste (Shift+Insert). Set UU_WAYLAND_TEXT=0 to disable Wayland text injection. + /// Mobile input-method text (`text_input`). X11 types directly. Native Wayland uses the text + /// keyboard's Unicode keysyms first: uinput sends physical keycodes, which a running input + /// method composes as pinyin, so "hello" would become candidates instead of text. uinput stays + /// as the fallback when the keymap path is unavailable. Portal and other Wayland environments + /// inject via clipboard paste (Shift+Insert). + /// Set UU_WAYLAND_TEXT=0 to disable Wayland text injection. pub fn text_input(&self, text: &str) { let _serial=self.serial.lock().unwrap(); if self.backend != crate::desktop_backend::DesktopBackend::X11 { if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v| v == "0") { return; } + if self.backend == crate::desktop_backend::DesktopBackend::Wayland + && crate::wayland::type_utf8(text) >= 0 { + log::info!("Desktop text input: chars={} result=keysyms", text.chars().count()); + return; + } let typed = self.type_uinput_text(text); if typed > 0 { log::info!("Desktop text input (uinput): chars={typed}"); } if typed == text.len() { return; }