2018-04-10 22:18:30 -04:00
|
|
|
// Important: all XIM calls need to happen from the same thread!
|
|
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
mod callbacks;
|
|
|
|
|
mod context;
|
2018-04-10 22:18:30 -04:00
|
|
|
mod inner;
|
|
|
|
|
mod input_method;
|
|
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
|
use std::sync::Arc;
|
2018-04-10 22:18:30 -04:00
|
|
|
|
2023-12-25 09:25:09 +01:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-02-25 19:20:39 -08:00
|
|
|
use tracing::debug;
|
2023-12-25 09:25:09 +01:00
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
use self::callbacks::*;
|
|
|
|
|
use self::context::ImeContext;
|
|
|
|
|
pub use self::context::ImeContextCreationError;
|
2019-06-21 11:33:15 -04:00
|
|
|
use self::inner::{close_im, ImeInner};
|
2022-09-11 19:36:56 +03:00
|
|
|
use self::input_method::{PotentialInputMethods, Style};
|
2018-04-10 22:18:30 -04:00
|
|
|
use super::{ffi, util, XConnection, XError};
|
2022-09-11 19:36:56 +03:00
|
|
|
|
2022-05-07 05:29:25 +03:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
|
pub enum ImeEvent {
|
|
|
|
|
Enabled,
|
|
|
|
|
Start,
|
|
|
|
|
Update(String, usize),
|
|
|
|
|
End,
|
|
|
|
|
Disabled,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type ImeReceiver = Receiver<ImeRequest>;
|
|
|
|
|
pub type ImeSender = Sender<ImeRequest>;
|
|
|
|
|
pub type ImeEventReceiver = Receiver<(ffi::Window, ImeEvent)>;
|
|
|
|
|
pub type ImeEventSender = Sender<(ffi::Window, ImeEvent)>;
|
2018-04-10 22:18:30 -04:00
|
|
|
|
2022-05-07 05:29:25 +03:00
|
|
|
/// Request to control XIM handler from the window.
|
|
|
|
|
pub enum ImeRequest {
|
|
|
|
|
/// Set IME spot position for given `window_id`.
|
|
|
|
|
Position(ffi::Window, i16, i16),
|
|
|
|
|
|
|
|
|
|
/// Allow IME input for the given `window_id`.
|
|
|
|
|
Allow(ffi::Window, bool),
|
|
|
|
|
}
|
2018-04-10 22:18:30 -04:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-01-10 08:46:48 +00:00
|
|
|
pub(crate) enum ImeCreationError {
|
2022-11-23 13:07:58 +01:00
|
|
|
// Boxed to prevent large error type
|
|
|
|
|
OpenFailure(Box<PotentialInputMethods>),
|
2024-01-06 16:54:29 +01:00
|
|
|
SetDestroyCallbackFailed(#[allow(dead_code)] XError),
|
2018-04-10 22:18:30 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 08:46:48 +00:00
|
|
|
pub(crate) struct Ime {
|
2018-04-10 22:18:30 -04:00
|
|
|
xconn: Arc<XConnection>,
|
|
|
|
|
// The actual meat of this struct is boxed away, since it needs to have a fixed location in
|
|
|
|
|
// memory so we can pass a pointer to it around.
|
|
|
|
|
inner: Box<ImeInner>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Ime {
|
2022-05-07 05:29:25 +03:00
|
|
|
pub fn new(
|
|
|
|
|
xconn: Arc<XConnection>,
|
|
|
|
|
event_sender: ImeEventSender,
|
|
|
|
|
) -> Result<Self, ImeCreationError> {
|
2018-04-10 22:18:30 -04:00
|
|
|
let potential_input_methods = PotentialInputMethods::new(&xconn);
|
|
|
|
|
|
|
|
|
|
let (mut inner, client_data) = {
|
2022-05-07 05:29:25 +03:00
|
|
|
let mut inner = Box::new(ImeInner::new(xconn, potential_input_methods, event_sender));
|
2018-04-10 22:18:30 -04:00
|
|
|
let inner_ptr = Box::into_raw(inner);
|
|
|
|
|
let client_data = inner_ptr as _;
|
|
|
|
|
let destroy_callback =
|
|
|
|
|
ffi::XIMCallback { client_data, callback: Some(xim_destroy_callback) };
|
|
|
|
|
inner = unsafe { Box::from_raw(inner_ptr) };
|
|
|
|
|
inner.destroy_callback = destroy_callback;
|
|
|
|
|
(inner, client_data)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let xconn = Arc::clone(&inner.xconn);
|
|
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
let input_method = inner.potential_input_methods.open_im(
|
|
|
|
|
&xconn,
|
|
|
|
|
Some(&|| {
|
|
|
|
|
let _ = unsafe { set_instantiate_callback(&xconn, client_data) };
|
|
|
|
|
}),
|
|
|
|
|
);
|
2018-04-10 22:18:30 -04:00
|
|
|
|
|
|
|
|
let is_fallback = input_method.is_fallback();
|
|
|
|
|
if let Some(input_method) = input_method.ok() {
|
|
|
|
|
inner.is_fallback = is_fallback;
|
|
|
|
|
unsafe {
|
2022-11-23 13:07:58 +01:00
|
|
|
let result = set_destroy_callback(&xconn, input_method.im, &inner)
|
2018-04-10 22:18:30 -04:00
|
|
|
.map_err(ImeCreationError::SetDestroyCallbackFailed);
|
|
|
|
|
if result.is_err() {
|
|
|
|
|
let _ = close_im(&xconn, input_method.im);
|
|
|
|
|
}
|
|
|
|
|
result?;
|
|
|
|
|
}
|
2022-09-11 19:36:56 +03:00
|
|
|
inner.im = Some(input_method);
|
2018-04-10 22:18:30 -04:00
|
|
|
Ok(Ime { xconn, inner })
|
|
|
|
|
} else {
|
2022-11-23 13:07:58 +01:00
|
|
|
Err(ImeCreationError::OpenFailure(Box::new(inner.potential_input_methods)))
|
2018-04-10 22:18:30 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_destroyed(&self) -> bool {
|
|
|
|
|
self.inner.is_destroyed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This pattern is used for various methods here:
|
|
|
|
|
// Ok(_) indicates that nothing went wrong internally
|
|
|
|
|
// Ok(true) indicates that the action was actually performed
|
|
|
|
|
// Ok(false) indicates that the action is not presently applicable
|
2022-05-07 05:29:25 +03:00
|
|
|
pub fn create_context(
|
|
|
|
|
&mut self,
|
|
|
|
|
window: ffi::Window,
|
|
|
|
|
with_preedit: bool,
|
|
|
|
|
) -> Result<bool, ImeContextCreationError> {
|
2018-04-10 22:18:30 -04:00
|
|
|
let context = if self.is_destroyed() {
|
|
|
|
|
// Create empty entry in map, so that when IME is rebuilt, this window has a context.
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2022-09-11 19:36:56 +03:00
|
|
|
let im = self.inner.im.as_ref().unwrap();
|
|
|
|
|
let style = if with_preedit { im.preedit_style } else { im.none_style };
|
|
|
|
|
|
2022-07-30 17:15:57 +03:00
|
|
|
let context = unsafe {
|
|
|
|
|
ImeContext::new(
|
|
|
|
|
&self.inner.xconn,
|
2022-09-11 19:36:56 +03:00
|
|
|
im.im,
|
|
|
|
|
style,
|
2022-07-30 17:15:57 +03:00
|
|
|
window,
|
|
|
|
|
None,
|
|
|
|
|
self.inner.event_sender.clone(),
|
2022-09-11 19:36:56 +03:00
|
|
|
)?
|
|
|
|
|
};
|
2022-07-30 17:15:57 +03:00
|
|
|
|
|
|
|
|
// Check the state on the context, since it could fail to enable or disable preedit.
|
2022-09-11 19:36:56 +03:00
|
|
|
let event = if matches!(style, Style::None(_)) {
|
|
|
|
|
if with_preedit {
|
|
|
|
|
debug!("failed to create IME context with preedit support.")
|
|
|
|
|
}
|
2022-05-07 05:29:25 +03:00
|
|
|
ImeEvent::Disabled
|
2022-09-11 19:36:56 +03:00
|
|
|
} else {
|
|
|
|
|
if !with_preedit {
|
|
|
|
|
debug!("failed to create IME context without preedit support.")
|
|
|
|
|
}
|
|
|
|
|
ImeEvent::Enabled
|
2022-05-07 05:29:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.inner.event_sender.send((window, event)).expect("Failed to send enabled event");
|
|
|
|
|
|
2022-07-30 17:15:57 +03:00
|
|
|
Some(context)
|
2018-04-10 22:18:30 -04:00
|
|
|
};
|
2022-09-11 19:36:56 +03:00
|
|
|
|
2018-04-10 22:18:30 -04:00
|
|
|
self.inner.contexts.insert(window, context);
|
|
|
|
|
Ok(!self.is_destroyed())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_context(&self, window: ffi::Window) -> Option<ffi::XIC> {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2023-01-27 07:18:58 +03:00
|
|
|
if let Some(Some(context)) = self.inner.contexts.get(&window) {
|
2018-04-10 22:18:30 -04:00
|
|
|
Some(context.ic)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn remove_context(&mut self, window: ffi::Window) -> Result<bool, XError> {
|
|
|
|
|
if let Some(Some(context)) = self.inner.contexts.remove(&window) {
|
|
|
|
|
unsafe {
|
|
|
|
|
self.inner.destroy_ic_if_necessary(context.ic)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn focus(&mut self, window: ffi::Window) -> Result<bool, XError> {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
if let Some(&mut Some(ref mut context)) = self.inner.contexts.get_mut(&window) {
|
|
|
|
|
context.focus(&self.xconn).map(|_| true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unfocus(&mut self, window: ffi::Window) -> Result<bool, XError> {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
if let Some(&mut Some(ref mut context)) = self.inner.contexts.get_mut(&window) {
|
|
|
|
|
context.unfocus(&self.xconn).map(|_| true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn send_xim_spot(&mut self, window: ffi::Window, x: i16, y: i16) {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if let Some(&mut Some(ref mut context)) = self.inner.contexts.get_mut(&window) {
|
|
|
|
|
context.set_spot(&self.xconn, x as _, y as _);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-07 05:29:25 +03:00
|
|
|
|
|
|
|
|
pub fn set_ime_allowed(&mut self, window: ffi::Window, allowed: bool) {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(&mut Some(ref mut context)) = self.inner.contexts.get_mut(&window) {
|
2022-09-11 19:36:56 +03:00
|
|
|
if allowed == context.is_allowed() {
|
2022-05-07 05:29:25 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove context for that window.
|
|
|
|
|
let _ = self.remove_context(window);
|
|
|
|
|
|
|
|
|
|
// Create new context supporting IME input.
|
|
|
|
|
let _ = self.create_context(window, allowed);
|
|
|
|
|
}
|
2024-10-13 21:57:02 +03:00
|
|
|
|
|
|
|
|
pub fn is_ime_allowed(&self, window: ffi::Window) -> bool {
|
|
|
|
|
if self.is_destroyed() {
|
|
|
|
|
false
|
|
|
|
|
} else if let Some(Some(context)) = self.inner.contexts.get(&window) {
|
|
|
|
|
context.is_allowed()
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-10 22:18:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Ime {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
let _ = self.inner.destroy_all_contexts_if_necessary();
|
|
|
|
|
let _ = self.inner.close_im_if_necessary();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|