Skip to content

Commit

Permalink
On X11, fix IME crashing without available styles
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Oct 9, 2022
1 parent bb0f965 commit 72310b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Wayland, `wayland-csd-adwaita` now uses `ab_glyph` instead of `crossfont` to render the title for decorations.
- On Wayland, a new `wayland-csd-adwaita-crossfont` feature was added to use `crossfont` instead of `ab_glyph` for decorations.
- On Wayland, if not otherwise specified use upstream automatic CSD theme selection.
- On X11, fix XIM not picking IME when no styles advertised.

# 0.27.3

Expand Down
35 changes: 18 additions & 17 deletions src/platform_impl/linux/x11/ime/input_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,36 @@ unsafe fn open_im(xconn: &Arc<XConnection>, locale_modifiers: &CStr) -> Option<f
#[derive(Debug)]
pub struct InputMethod {
pub im: ffi::XIM,
pub preedit_style: Style,
pub none_style: Style,
_name: String,
}

#[derive(Debug, Default)]
pub struct InputMethodStyles {
pub preedit_style: Option<Style>,
pub none_style: Option<Style>,
}

impl InputMethod {
fn new(xconn: &Arc<XConnection>, im: ffi::XIM, name: String) -> Option<Self> {
fn new(im: ffi::XIM, name: String) -> Self {
Self { im, _name: name }
}

// XXX checking this during creation is not desired, since we don't want to filter
// ime based on styles since IME could be reloading, etc, so we ask for that when needed.
pub(crate) fn query_styles(&self, xconn: &Arc<XConnection>) -> InputMethodStyles {
let mut styles: *mut XIMStyles = std::ptr::null_mut();

// Query the styles supported by the XIM.
unsafe {
if !(xconn.xlib.XGetIMValues)(
im,
self.im,
ffi::XNQueryInputStyle_0.as_ptr() as *const _,
(&mut styles) as *mut _,
std::ptr::null_mut::<()>(),
)
.is_null()
{
return None;
return InputMethodStyles::default();
}
}

Expand All @@ -83,19 +93,10 @@ impl InputMethod {
(xconn.xlib.XFree)(styles.cast());
};

if preedit_style.is_none() && none_style.is_none() {
return None;
}

let preedit_style = preedit_style.unwrap_or_else(|| none_style.unwrap());
let none_style = none_style.unwrap_or(preedit_style);

Some(InputMethod {
im,
_name: name,
InputMethodStyles {
preedit_style,
none_style,
})
}
}
}

Expand Down Expand Up @@ -256,7 +257,7 @@ impl PotentialInputMethod {
pub fn open_im(&mut self, xconn: &Arc<XConnection>) -> Option<InputMethod> {
let im = unsafe { open_im(xconn, &self.name.c_string) };
self.successful = Some(im.is_some());
im.and_then(|im| InputMethod::new(xconn, im, self.name.string.clone()))
im.map(|im| InputMethod::new(im, self.name.string.clone()))
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/platform_impl/linux/x11/ime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ impl Ime {
None
} else {
let im = self.inner.im.as_ref().unwrap();
let styles = im.query_styles(&self.inner.xconn);
let style = if with_preedit {
im.preedit_style
styles.preedit_style.or(styles.none_style)
} else {
im.none_style
};
styles.none_style.or(styles.preedit_style)
}
.unwrap_or_default();

let context = unsafe {
ImeContext::new(
Expand Down

0 comments on commit 72310b5

Please sign in to comment.