From a3eb8a8bd482bdf8bef3da0f1a9b2aa2bda2c2d8 Mon Sep 17 00:00:00 2001 From: Timmy Xiao <34635512+tzx@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:08:34 -0800 Subject: [PATCH] Start IME support --- window/src/os/wayland/inputhandler.rs | 214 ++++---------------------- window/src/os/wayland/mod.rs | 2 +- window/src/os/wayland/state.rs | 7 + 3 files changed, 42 insertions(+), 181 deletions(-) diff --git a/window/src/os/wayland/inputhandler.rs b/window/src/os/wayland/inputhandler.rs index 5e280a6eb..2f8223228 100644 --- a/window/src/os/wayland/inputhandler.rs +++ b/window/src/os/wayland/inputhandler.rs @@ -1,193 +1,47 @@ //! Implements zwp_text_input_v3 for handling IME -use crate::connection::ConnectionOps; -use crate::os::wayland::{wl_id, WaylandConnection}; -use crate::{DeadKeyStatus, KeyCode, KeyEvent, Modifiers, WindowEvent}; -use smithay_client_toolkit::environment::GlobalHandler; -use std::collections::HashMap; -use std::sync::{Arc, Mutex}; -use wayland_client::protocol::wl_keyboard::WlKeyboard; -use wayland_client::protocol::wl_registry::WlRegistry; -use wayland_client::protocol::wl_seat::WlSeat; -use wayland_client::protocol::wl_surface::WlSurface; -use wayland_client::{Attached, DispatchData, Main}; -use wayland_protocols::unstable::text_input::v3::client::zwp_text_input_manager_v3::ZwpTextInputManagerV3; -use wayland_protocols::unstable::text_input::v3::client::zwp_text_input_v3::{ - Event, ZwpTextInputV3, -}; -use wezterm_input_types::KeyboardLedStatus; +use std::sync::Mutex; -#[derive(Default, Debug)] -struct PendingState { - pre_edit: Option, - commit: Option, +use smithay_client_toolkit::globals::GlobalData; +use wayland_client::{Dispatch, Proxy, QueueHandle}; +use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_manager_v3::ZwpTextInputManagerV3; +use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::ZwpTextInputV3; + +use super::state::WaylandState; + +pub(super) struct TextInputState { + text_input_manager: ZwpTextInputManagerV3, } -#[derive(Debug, Default)] -struct Inner { - input_by_seat: HashMap>, - keyboard_to_seat: HashMap, - surface_to_keyboard: HashMap, - pending_state: HashMap, +#[derive(Default)] +pub(super) struct TextInputData { + inner: Mutex, } -impl Inner { - fn handle_event( - &mut self, - input: Main, - event: Event, - _ddata: DispatchData, - _inner: &Arc>, +#[derive(Default)] +pub(super) struct TextInputDataInner {} + +impl Dispatch for TextInputState { + fn event( + _state: &mut WaylandState, + _proxy: &ZwpTextInputManagerV3, + _event: ::Event, + _data: &GlobalData, + _conn: &wayland_client::Connection, + _qhandle: &QueueHandle, ) { - log::trace!("{event:?}"); - let conn = WaylandConnection::get().unwrap().wayland(); - let pending_state = self.pending_state.entry(wl_id(&**input)).or_default(); - match event { - Event::PreeditString { - text, - cursor_begin: _, - cursor_end: _, - } => { - pending_state.pre_edit = text; - } - Event::CommitString { text } => { - pending_state.commit = text; - conn.dispatch_to_focused_window(WindowEvent::AdviseDeadKeyStatus( - DeadKeyStatus::None, - )); - } - Event::Done { serial } => { - *conn.last_serial.borrow_mut() = serial; - if let Some(text) = pending_state.commit.take() { - conn.dispatch_to_focused_window(WindowEvent::KeyEvent(KeyEvent { - key: KeyCode::composed(&text), - modifiers: Modifiers::NONE, - leds: KeyboardLedStatus::empty(), - repeat_count: 1, - key_is_down: true, - raw: None, - })); - } - let status = if let Some(text) = pending_state.pre_edit.take() { - DeadKeyStatus::Composing(text) - } else { - DeadKeyStatus::None - }; - conn.dispatch_to_focused_window(WindowEvent::AdviseDeadKeyStatus(status)); - } - _ => {} - } - } - - fn disable_all(&mut self) { - for input in self.input_by_seat.values() { - input.disable(); - input.commit(); - } + todo!() } } -pub struct InputHandler { - mgr: Option>, - inner: Arc>, -} - -impl InputHandler { - pub fn new() -> Self { - Self { - mgr: None, - inner: Arc::new(Mutex::new(Inner::default())), - } - } - - pub fn get_text_input_for_keyboard( - &self, - keyboard: &WlKeyboard, - ) -> Option> { - let inner = self.inner.lock().unwrap(); - let keyboard_id = wl_id(keyboard); - let seat_id = inner.keyboard_to_seat.get(&keyboard_id)?; - inner.input_by_seat.get(&seat_id).cloned() - } - - pub fn get_text_input_for_surface( - &self, - surface: &WlSurface, - ) -> Option> { - let inner = self.inner.lock().unwrap(); - let surface_id = wl_id(surface); - let keyboard_id = inner.surface_to_keyboard.get(&surface_id)?; - let seat_id = inner.keyboard_to_seat.get(&keyboard_id)?; - inner.input_by_seat.get(&seat_id).cloned() - } - - pub fn get_text_input_for_seat(&self, seat: &WlSeat) -> Option> { - let mgr = self.mgr.as_ref()?; - let mut inner = self.inner.lock().unwrap(); - let seat_id = wl_id(seat); - let input = inner.input_by_seat.entry(seat_id).or_insert_with(|| { - let input = mgr.get_text_input(seat); - let inner = Arc::clone(&self.inner); - - input.quick_assign(move |input, event, ddat| { - inner - .lock() - .unwrap() - .handle_event(input, event, ddat, &inner); - }); - - input.into() - }); - Some(input.clone()) - } - - pub fn advise_surface(&self, surface: &WlSurface, keyboard: &WlKeyboard) { - let surface_id = wl_id(surface); - let keyboard_id = wl_id(keyboard); - self.inner - .lock() - .unwrap() - .surface_to_keyboard - .insert(surface_id, keyboard_id); - } - - pub fn advise_seat(&self, seat: &WlSeat, keyboard: &WlKeyboard) { - self.get_text_input_for_seat(seat); - let keyboard_id = wl_id(keyboard); - let seat_id = wl_id(seat); - self.inner - .lock() - .unwrap() - .keyboard_to_seat - .insert(keyboard_id, seat_id); - } - - /// Workaround for - /// If we make sure to disable things before we close the app, - /// mutter is less likely to get in a bad state - pub fn shutdown(&self) { - self.inner.lock().unwrap().disable_all(); - } - - pub fn seat_defunct(&self, seat: &WlSeat) { - let seat_id = wl_id(seat); - self.inner.lock().unwrap().input_by_seat.remove(&seat_id); - } -} - -impl GlobalHandler for InputHandler { - fn created( - &mut self, - registry: Attached, - id: u32, - version: u32, - _ddata: DispatchData, +impl Dispatch for TextInputState { + fn event( + _state: &mut WaylandState, + _proxy: &ZwpTextInputV3, + _event: ::Event, + _data: &TextInputData, + _conn: &wayland_client::Connection, + _qhandle: &QueueHandle, ) { - log::debug!("created ZwpTextInputV3 {id} {version}"); - let mgr = registry.bind::(1, id); - self.mgr.replace(mgr.into()); - } - - fn get(&self) -> std::option::Option> { - self.mgr.clone() + todo!() } } diff --git a/window/src/os/wayland/mod.rs b/window/src/os/wayland/mod.rs index bf7cb43ba..7bfe7c39b 100644 --- a/window/src/os/wayland/mod.rs +++ b/window/src/os/wayland/mod.rs @@ -1,7 +1,7 @@ #![cfg(all(unix, not(target_os = "macos")))] pub mod connection; -// pub mod inputhandler; +pub mod inputhandler; // pub mod output; pub mod window; pub use self::window::*; diff --git a/window/src/os/wayland/state.rs b/window/src/os/wayland/state.rs index 419b50644..67b49865d 100644 --- a/window/src/os/wayland/state.rs +++ b/window/src/os/wayland/state.rs @@ -7,6 +7,7 @@ use smithay_client_toolkit::compositor::CompositorState; use smithay_client_toolkit::data_device_manager::data_device::DataDevice; use smithay_client_toolkit::data_device_manager::data_source::CopyPasteSource; use smithay_client_toolkit::data_device_manager::DataDeviceManagerState; +use smithay_client_toolkit::globals::GlobalData; use smithay_client_toolkit::output::{OutputHandler, OutputState}; use smithay_client_toolkit::registry::{ProvidesRegistryState, RegistryState}; use smithay_client_toolkit::seat::pointer::ThemedPointer; @@ -26,9 +27,12 @@ use wayland_client::protocol::wl_output::WlOutput; use wayland_client::protocol::wl_pointer::WlPointer; use wayland_client::protocol::wl_surface::WlSurface; use wayland_client::{delegate_dispatch, Connection, QueueHandle}; +use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_manager_v3::ZwpTextInputManagerV3; +use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::ZwpTextInputV3; use crate::x11::KeyboardWithFallback; +use super::inputhandler::{TextInputData, TextInputState}; use super::pointer::{PendingMouse, PointerUserData}; use super::{SurfaceUserData, WaylandWindowInner}; @@ -147,3 +151,6 @@ delegate_dispatch!(WaylandState: [WlPointer: PointerUserData] => SeatState); delegate_xdg_shell!(WaylandState); delegate_xdg_window!(WaylandState); + +delegate_dispatch!(WaylandState: [ZwpTextInputManagerV3: GlobalData] => TextInputState); +delegate_dispatch!(WaylandState: [ZwpTextInputV3: TextInputData] => TextInputState);