winit/platform_impl/linux/x11/ime/
inner.rs

1use std::collections::HashMap;
2use std::mem;
3use std::sync::Arc;
4
5use super::context::ImeContext;
6use super::input_method::{InputMethod, PotentialInputMethods};
7use super::{ffi, XConnection, XError};
8use crate::platform_impl::platform::x11::ime::ImeEventSender;
9
10pub(crate) unsafe fn close_im(xconn: &Arc<XConnection>, im: ffi::XIM) -> Result<(), XError> {
11    unsafe { (xconn.xlib.XCloseIM)(im) };
12    xconn.check_errors()
13}
14
15pub(crate) unsafe fn destroy_ic(xconn: &Arc<XConnection>, ic: ffi::XIC) -> Result<(), XError> {
16    unsafe { (xconn.xlib.XDestroyIC)(ic) };
17    xconn.check_errors()
18}
19
20pub(crate) struct ImeInner {
21    pub xconn: Arc<XConnection>,
22    pub im: Option<InputMethod>,
23    pub potential_input_methods: PotentialInputMethods,
24    pub contexts: HashMap<ffi::Window, Option<ImeContext>>,
25    // WARNING: this is initially zeroed!
26    pub destroy_callback: ffi::XIMCallback,
27    pub event_sender: ImeEventSender,
28    // Indicates whether or not the input method was destroyed on the server end
29    // (i.e. if ibus/fcitx/etc. was terminated/restarted)
30    pub is_destroyed: bool,
31    pub is_fallback: bool,
32}
33
34impl ImeInner {
35    pub(crate) fn new(
36        xconn: Arc<XConnection>,
37        potential_input_methods: PotentialInputMethods,
38        event_sender: ImeEventSender,
39    ) -> Self {
40        ImeInner {
41            xconn,
42            im: None,
43            potential_input_methods,
44            contexts: HashMap::new(),
45            destroy_callback: unsafe { mem::zeroed() },
46            event_sender,
47            is_destroyed: false,
48            is_fallback: false,
49        }
50    }
51
52    pub unsafe fn close_im_if_necessary(&self) -> Result<bool, XError> {
53        if !self.is_destroyed && self.im.is_some() {
54            unsafe { close_im(&self.xconn, self.im.as_ref().unwrap().im) }.map(|_| true)
55        } else {
56            Ok(false)
57        }
58    }
59
60    pub unsafe fn destroy_ic_if_necessary(&self, ic: ffi::XIC) -> Result<bool, XError> {
61        if !self.is_destroyed {
62            unsafe { destroy_ic(&self.xconn, ic) }.map(|_| true)
63        } else {
64            Ok(false)
65        }
66    }
67
68    pub unsafe fn destroy_all_contexts_if_necessary(&self) -> Result<bool, XError> {
69        for context in self.contexts.values().flatten() {
70            unsafe { self.destroy_ic_if_necessary(context.ic)? };
71        }
72        Ok(!self.is_destroyed)
73    }
74}