diff --git a/window/src/os/wayland/inputhandler.rs b/window/src/os/wayland/inputhandler.rs index 2f8223228..315c8c9fa 100644 --- a/window/src/os/wayland/inputhandler.rs +++ b/window/src/os/wayland/inputhandler.rs @@ -1,20 +1,117 @@ //! Implements zwp_text_input_v3 for handling IME +use std::borrow::Borrow; +use std::collections::HashMap; use std::sync::Mutex; use smithay_client_toolkit::globals::GlobalData; +use wayland_client::backend::ObjectId; +use wayland_client::globals::{BindError, GlobalList}; +use wayland_client::protocol::wl_keyboard::WlKeyboard; +use wayland_client::protocol::wl_seat::WlSeat; +use wayland_client::protocol::wl_surface::WlSurface; 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 wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::{ + Event as TextInputEvent, ZwpTextInputV3, +}; +use wezterm_input_types::{KeyCode, KeyEvent, KeyboardLedStatus, Modifiers}; + +use crate::{DeadKeyStatus, WindowEvent}; use super::state::WaylandState; +#[derive(Clone, Default, Debug)] +struct PendingState { + pre_edit: Option, + commit: Option, +} + pub(super) struct TextInputState { text_input_manager: ZwpTextInputManagerV3, + inner: Mutex, +} + +#[derive(Debug, Default)] +struct Inner { + input_by_seat: HashMap, + keyboard_to_seat: HashMap, + surface_to_keyboard: HashMap, + pending_state: HashMap, +} + +impl TextInputState { + pub(super) fn bind( + globals: &GlobalList, + queue_handle: &QueueHandle, + ) -> Result { + let text_input_manager = globals.bind(queue_handle, 1..=1, GlobalData)?; + Ok(Self { + text_input_manager, + inner: Mutex::new(Inner::default()), + }) + } + + pub fn get_text_input_for_keyboard(&self, keyboard: &WlKeyboard) -> Option { + let inner = self.inner.lock().unwrap(); + let keyboard_id = keyboard.id(); + let seat_id = inner.keyboard_to_seat.get(&keyboard_id)?; + inner.input_by_seat.get(&seat_id).cloned() + } + + pub(super) fn get_text_input_for_surface(&self, surface: &WlSurface) -> Option { + let inner = self.inner.lock().unwrap(); + let surface_id = surface.id(); + 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() + } + + fn get_text_input_for_seat( + &self, + seat: &WlSeat, + qh: &QueueHandle, + ) -> Option { + let mgr = &self.text_input_manager; + let mut inner = self.inner.lock().unwrap(); + let seat_id = seat.id(); + let input = inner.input_by_seat.entry(seat_id).or_insert_with(|| { + let input = mgr.get_text_input(seat, &qh, TextInputData::default()); + input.into() + }); + Some(input.clone()) + } + + pub(super) fn advise_surface(&self, surface: &WlSurface, keyboard: &WlKeyboard) { + let surface_id = surface.id(); + let keyboard_id = keyboard.id(); + self.inner + .lock() + .unwrap() + .surface_to_keyboard + .insert(surface_id, keyboard_id); + } + + pub(super) fn advise_seat( + &self, + seat: &WlSeat, + keyboard: &WlKeyboard, + qh: &QueueHandle, + ) { + self.get_text_input_for_seat(seat, qh); + let keyboard_id = keyboard.id(); + let seat_id = seat.id(); + self.inner + .lock() + .unwrap() + .keyboard_to_seat + .insert(keyboard_id, seat_id); + } } #[derive(Default)] pub(super) struct TextInputData { - inner: Mutex, + // XXX: inner could probably be moved here + _inner: Mutex, } #[derive(Default)] @@ -29,19 +126,80 @@ impl Dispatch for TextInputStat _conn: &wayland_client::Connection, _qhandle: &QueueHandle, ) { - todo!() + // No events from ZwpTextInputMangerV3 + unreachable!(); } } impl Dispatch for TextInputState { fn event( - _state: &mut WaylandState, - _proxy: &ZwpTextInputV3, - _event: ::Event, + state: &mut WaylandState, + input: &ZwpTextInputV3, + event: ::Event, _data: &TextInputData, _conn: &wayland_client::Connection, _qhandle: &QueueHandle, ) { - todo!() + log::trace!("ZwpTextInputEvent: {event:?}"); + let mut pending_state = { + let text_input = &mut state.text_input; + let mut inner = text_input.inner.lock().unwrap(); + inner.pending_state.entry(input.id()).or_default().clone() + }; + + match event { + TextInputEvent::PreeditString { + text, + cursor_begin: _, + cursor_end: _, + } => { + pending_state.pre_edit = text; + } + TextInputEvent::CommitString { text } => { + pending_state.commit = text; + state.dispatch_to_focused_window(WindowEvent::AdviseDeadKeyStatus( + DeadKeyStatus::None, + )); + } + TextInputEvent::Done { serial } => { + *state.last_serial.borrow_mut() = serial; + if let Some(text) = pending_state.commit.take() { + state.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 + }; + state.dispatch_to_focused_window(WindowEvent::AdviseDeadKeyStatus(status)); + } + _ => {} + } + + state + .text_input + .inner + .lock() + .unwrap() + .pending_state + .insert(input.id(), pending_state); + } +} + +impl WaylandState { + fn dispatch_to_focused_window(&self, event: WindowEvent) { + if let Some(&window_id) = self.keyboard_window_id.borrow().as_ref() { + if let Some(win) = self.window_by_id(window_id) { + let mut inner = win.borrow_mut(); + inner.events.dispatch(event); + } + } } } diff --git a/window/src/os/wayland/keyboard.rs b/window/src/os/wayland/keyboard.rs index e4324c2b0..a831d29c3 100644 --- a/window/src/os/wayland/keyboard.rs +++ b/window/src/os/wayland/keyboard.rs @@ -15,7 +15,7 @@ use super::SurfaceUserData; impl Dispatch for WaylandState { fn event( state: &mut WaylandState, - _proxy: &WlKeyboard, + keyboard: &WlKeyboard, event: ::Event, _data: &KeyboardData, _conn: &wayland_client::Connection, @@ -31,13 +31,21 @@ impl Dispatch for WaylandState { if let Some(sud) = SurfaceUserData::try_from_wl(&surface) { let window_id = sud.window_id; state.keyboard_window_id.borrow_mut().replace(window_id); + if let Some(input) = state.text_input.get_text_input_for_keyboard(keyboard) { + input.enable(); + input.commit(); + } + state.text_input.advise_surface(surface, keyboard); } else { log::warn!("{:?}, no known surface", event); } } WlKeyboardEvent::Leave { serial, .. } => { - // TODO: I know nothing about the input handler currently *state.last_serial.borrow_mut() = *serial; + if let Some(input) = state.text_input.get_text_input_for_keyboard(keyboard) { + input.disable(); + input.commit(); + } } WlKeyboardEvent::Key { serial, .. } | WlKeyboardEvent::Modifiers { serial, .. } => { *state.last_serial.borrow_mut() = *serial; diff --git a/window/src/os/wayland/seat.rs b/window/src/os/wayland/seat.rs index 0e3bac623..198a61600 100644 --- a/window/src/os/wayland/seat.rs +++ b/window/src/os/wayland/seat.rs @@ -28,6 +28,8 @@ impl SeatHandler for WaylandState { log::trace!("Setting keyboard capability"); let keyboard = seat.get_keyboard(qh, KeyboardData {}); self.keyboard = Some(keyboard.clone()); + + self.text_input.advise_seat(&seat, &keyboard, qh); } if capability == Capability::Pointer && self.pointer.is_none() { diff --git a/window/src/os/wayland/state.rs b/window/src/os/wayland/state.rs index 67b49865d..c97fde2e5 100644 --- a/window/src/os/wayland/state.rs +++ b/window/src/os/wayland/state.rs @@ -42,6 +42,7 @@ pub(super) struct WaylandState { registry: RegistryState, pub(super) output: OutputState, pub(super) compositor: CompositorState, + pub(super) text_input: TextInputState, pub(super) seat: SeatState, pub(super) xdg: XdgShell, pub(super) windows: RefCell>>>, @@ -72,6 +73,7 @@ impl WaylandState { registry: RegistryState::new(globals), output: OutputState::new(globals, qh), compositor: CompositorState::bind(globals, qh)?, + text_input: TextInputState::bind(globals, qh)?, windows: RefCell::new(HashMap::new()), seat: SeatState::new(globals, qh), xdg: XdgShell::bind(globals, qh)?, diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index ac3a11029..65cb72c60 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -354,6 +354,13 @@ impl WindowOps for WaylandWindow { }); } + fn set_text_cursor_position(&self, cursor: Rect) { + WaylandConnection::with_window_inner(self.0, move |inner| { + inner.set_text_cursor_position(cursor); + Ok(()) + }); + } + fn set_title(&self, title: &str) { let title = title.to_owned(); WaylandConnection::with_window_inner(self.0, |inner| { @@ -851,6 +858,34 @@ impl WaylandWindowInner { self.do_paint().unwrap(); } + fn set_text_cursor_position(&mut self, rect: Rect) { + let conn = WaylandConnection::get().unwrap().wayland(); + let state = &conn.wayland_state.borrow(); + let surface = self.surface().clone(); + + let surface_id = surface.id(); + let active_surface_id = state.active_surface_id.borrow().as_ref().unwrap().clone(); + + if surface_id == active_surface_id { + if self.text_cursor.map(|prior| prior != rect).unwrap_or(true) { + self.text_cursor.replace(rect); + + let surface_udata = SurfaceUserData::from_wl(&surface); + let factor = surface_udata.surface_data().scale_factor(); + + if let Some(input) = state.text_input.get_text_input_for_surface(&surface) { + input.set_cursor_rectangle( + rect.min_x() as i32 / factor, + rect.min_y() as i32 / factor, + rect.width() as i32 / factor, + rect.height() as i32 / factor, + ); + input.commit(); + } + } + } + } + fn set_title(&mut self, title: String) { if let Some(last_title) = self.title.as_ref() { if last_title == &title {