x11: Overhaul XIM code (#451)

Fixes #195
Fixes #277
Fixes #455

* Read `XMODIFIERS` explicitly/directly instead of calling `XSetLocaleModifiers` with an
empty string. This is useful for debugging purposes, and more clear to read and handle.
* Fallback to local input method if the one specified in `XMODIFIERS` is later closed on the
server end (i.e. if ibus/fcitx is terminated). Previously, that would cause the event loop
to freeze and usually also segfault.
* If using the fallback input method, respond to the `XMODIFIERS` input method later
becoming available. This means that the input method restarting is handled, and that even if
the program was started while ibus/fcitx/etc. was unavailable, it will start using it as
soon as it becomes available.
* Only one input method is opened for the whole event loop, with each window having its own
input context.
* IME works completely out of the box now, no longer requiring application developers to
call `setlocale` or `XSetLocaleModifiers`.
* Detailed error messages are provided if no input method could be opened. However, no
information is provided to the user if their intended `XMODIFIERS` input method failed to
open but the fallbacks (which will ostensibly always succeed) succeeded; in my opinion, this
is something that is best filled by adding a logging feature to winit.
This commit is contained in:
Francesca Frangipane 2018-04-10 22:18:30 -04:00 committed by GitHub
parent f08bf44670
commit 09c809003b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 880 additions and 137 deletions

View file

@ -14,22 +14,24 @@ use events::ModifiersState;
use std::{mem, ptr, slice};
use std::sync::{Arc, Mutex, Weak};
use std::sync::atomic::{self, AtomicBool};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::mpsc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::CStr;
use std::os::raw::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong};
use libc;
use libc::{self, setlocale, LC_CTYPE};
mod events;
mod monitor;
mod window;
mod xdisplay;
mod dnd;
mod ime;
mod util;
use self::dnd::{Dnd, DndState};
use self::ime::{ImeReceiver, ImeSender, ImeCreationError, Ime};
// API TRANSITION
//
@ -42,9 +44,9 @@ pub struct EventsLoop {
display: Arc<XConnection>,
wm_delete_window: ffi::Atom,
dnd: Dnd,
ime_receiver: Receiver<(WindowId, i16, i16)>,
ime_sender: Sender<(WindowId, i16, i16)>,
ime: RefCell<HashMap<WindowId, util::Ime>>,
ime_receiver: ImeReceiver,
ime_sender: ImeSender,
ime: RefCell<Ime>,
windows: Arc<Mutex<HashMap<WindowId, WindowData>>>,
devices: Mutex<HashMap<DeviceId, Device>>,
xi2ext: XExtension,
@ -70,7 +72,17 @@ impl EventsLoop {
let dnd = Dnd::new(Arc::clone(&display))
.expect("Failed to call XInternAtoms when initializing drag and drop");
let (ime_sender, ime_receiver) = channel();
let (ime_sender, ime_receiver) = mpsc::channel();
// Input methods will open successfully without setting the locale, but it won't be
// possible to actually commit pre-edit sequences.
unsafe { setlocale(LC_CTYPE, b"\0".as_ptr() as *const _); }
let ime = RefCell::new({
let result = Ime::new(Arc::clone(&display));
if let Err(ImeCreationError::OpenFailure(ref state)) = result {
panic!(format!("Failed to open input method: {:#?}", state));
}
result.expect("Failed to set input method destruction callback")
});
let xi2ext = unsafe {
let mut result = XExtension {
@ -115,7 +127,7 @@ impl EventsLoop {
dnd,
ime_receiver,
ime_sender,
ime: RefCell::new(HashMap::new()),
ime,
windows: Arc::new(Mutex::new(HashMap::new())),
devices: Mutex::new(HashMap::new()),
xi2ext,
@ -419,7 +431,10 @@ impl EventsLoop {
let window = xev.window;
self.ime.borrow_mut().remove(&WindowId(window));
self.ime
.borrow_mut()
.remove_context(window)
.expect("Failed to destroy input context");
}
ffi::Expose => {
@ -485,8 +500,8 @@ impl EventsLoop {
}
if state == Pressed {
let written = if let Some(ime) = self.ime.borrow().get(&WindowId(window)) {
unsafe { util::lookup_utf8(&self.display, ime.ic, xkev) }
let written = if let Some(ic) = self.ime.borrow().get_context(window) {
unsafe { util::lookup_utf8(&self.display, ic, xkev) }
} else {
return;
};
@ -736,11 +751,13 @@ impl EventsLoop {
let window_id = mkwid(xev.event);
if let Some(ime) = self.ime.borrow().get(&WindowId(xev.event)) {
ime.focus().expect("Failed to focus input context");
} else {
if let None = self.windows.lock().unwrap().get(&WindowId(xev.event)) {
return;
}
self.ime
.borrow_mut()
.focus(xev.event)
.expect("Failed to focus input context");
callback(Event::WindowEvent { window_id, event: Focused(true) });
@ -764,11 +781,13 @@ impl EventsLoop {
ffi::XI_FocusOut => {
let xev: &ffi::XIFocusOutEvent = unsafe { &*(xev.data as *const _) };
if let Some(ime) = self.ime.borrow().get(&WindowId(xev.event)) {
ime.unfocus().expect("Failed to unfocus input context");
} else {
if let None = self.windows.lock().unwrap().get(&WindowId(xev.event)) {
return;
}
self.ime
.borrow_mut()
.unfocus(xev.event)
.expect("Failed to unfocus input context");
callback(Event::WindowEvent {
window_id: mkwid(xev.event),
@ -888,9 +907,7 @@ impl EventsLoop {
match self.ime_receiver.try_recv() {
Ok((window_id, x, y)) => {
if let Some(ime) = self.ime.borrow_mut().get_mut(&window_id) {
ime.send_xim_spot(x, y);
}
self.ime.borrow_mut().send_xim_spot(window_id, x, y);
}
Err(_) => ()
}
@ -987,7 +1004,7 @@ pub struct Window {
pub window: Arc<Window2>,
display: Weak<XConnection>,
windows: Weak<Mutex<HashMap<WindowId, WindowData>>>,
ime_sender: Sender<(WindowId, i16, i16)>,
ime_sender: ImeSender,
}
impl ::std::ops::Deref for Window {
@ -1006,9 +1023,10 @@ impl Window {
) -> Result<Self, CreationError> {
let win = Arc::new(try!(Window2::new(&x_events_loop, window, pl_attribs)));
let ime = util::Ime::new(Arc::clone(&x_events_loop.display), win.id().0)
.expect("Failed to initialize IME");
x_events_loop.ime.borrow_mut().insert(win.id(), ime);
x_events_loop.ime
.borrow_mut()
.create_context(win.id().0)
.expect("Failed to create input context");
x_events_loop.windows.lock().unwrap().insert(win.id(), WindowData {
config: None,
@ -1031,7 +1049,7 @@ impl Window {
#[inline]
pub fn send_xim_spot(&self, x: i16, y: i16) {
let _ = self.ime_sender.send((self.window.id(), x, y));
let _ = self.ime_sender.send((self.window.id().0, x, y));
}
}