|
| 1 | +use std::ptr; |
| 2 | +use std::sync::Arc; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::os::raw::c_char; |
| 5 | + |
| 6 | +use super::{ffi, XConnection, XError}; |
| 7 | + |
| 8 | +use super::inner::{close_im, ImeInner}; |
| 9 | +use super::input_method::PotentialInputMethods; |
| 10 | +use super::context::{ImeContextCreationError, ImeContext}; |
| 11 | + |
| 12 | +pub unsafe fn xim_set_callback( |
| 13 | + xconn: &Arc<XConnection>, |
| 14 | + xim: ffi::XIM, |
| 15 | + field: *const c_char, |
| 16 | + callback: *mut ffi::XIMCallback, |
| 17 | +) -> Result<(), XError> { |
| 18 | + // It's advisable to wrap variadic FFI functions in our own functions, as we want to minimize |
| 19 | + // access that isn't type-checked. |
| 20 | + (xconn.xlib.XSetIMValues)( |
| 21 | + xim, |
| 22 | + field, |
| 23 | + callback, |
| 24 | + ptr::null_mut::<()>(), |
| 25 | + ); |
| 26 | + xconn.check_errors() |
| 27 | +} |
| 28 | + |
| 29 | +// Set a callback for when an input method matching the current locale modifiers becomes |
| 30 | +// available. Note that this has nothing to do with what input methods are open or able to be |
| 31 | +// opened, and simply uses the modifiers that are set when the callback is set. |
| 32 | +// * This is called per locale modifier, not per input method opened with that locale modifier. |
| 33 | +// * Trying to set this for multiple locale modifiers causes problems, i.e. one of the rebuilt |
| 34 | +// input contexts would always silently fail to use the input method. |
| 35 | +pub unsafe fn set_instantiate_callback( |
| 36 | + xconn: &Arc<XConnection>, |
| 37 | + client_data: ffi::XPointer, |
| 38 | +) -> Result<(), XError> { |
| 39 | + (xconn.xlib.XRegisterIMInstantiateCallback)( |
| 40 | + xconn.display, |
| 41 | + ptr::null_mut(), |
| 42 | + ptr::null_mut(), |
| 43 | + ptr::null_mut(), |
| 44 | + Some(xim_instantiate_callback), |
| 45 | + client_data, |
| 46 | + ); |
| 47 | + xconn.check_errors() |
| 48 | +} |
| 49 | + |
| 50 | +pub unsafe fn unset_instantiate_callback( |
| 51 | + xconn: &Arc<XConnection>, |
| 52 | + client_data: ffi::XPointer, |
| 53 | +) -> Result<(), XError> { |
| 54 | + (xconn.xlib.XUnregisterIMInstantiateCallback)( |
| 55 | + xconn.display, |
| 56 | + ptr::null_mut(), |
| 57 | + ptr::null_mut(), |
| 58 | + ptr::null_mut(), |
| 59 | + Some(xim_instantiate_callback), |
| 60 | + client_data, |
| 61 | + ); |
| 62 | + xconn.check_errors() |
| 63 | +} |
| 64 | + |
| 65 | +pub unsafe fn set_destroy_callback( |
| 66 | + xconn: &Arc<XConnection>, |
| 67 | + im: ffi::XIM, |
| 68 | + inner: &ImeInner, |
| 69 | +) -> Result<(), XError> { |
| 70 | + xim_set_callback( |
| 71 | + &xconn, |
| 72 | + im, |
| 73 | + ffi::XNDestroyCallback_0.as_ptr() as *const _, |
| 74 | + &inner.destroy_callback as *const _ as *mut _, |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Debug)] |
| 79 | +enum ReplaceImError { |
| 80 | + MethodOpenFailed(PotentialInputMethods), |
| 81 | + ContextCreationFailed(ImeContextCreationError), |
| 82 | + SetDestroyCallbackFailed(XError), |
| 83 | +} |
| 84 | + |
| 85 | +// Attempt to replace current IM (which may or may not be presently valid) with a new one. This |
| 86 | +// includes replacing all existing input contexts and free'ing resources as necessary. This only |
| 87 | +// modifies existing state if all operations succeed. |
| 88 | +unsafe fn replace_im(inner: *mut ImeInner) -> Result<(), ReplaceImError> { |
| 89 | + let xconn = &(*inner).xconn; |
| 90 | + |
| 91 | + let (new_im, is_fallback) = { |
| 92 | + let new_im = (*inner).potential_input_methods.open_im(xconn, None); |
| 93 | + let is_fallback = new_im.is_fallback(); |
| 94 | + ( |
| 95 | + new_im.ok().ok_or_else(|| { |
| 96 | + ReplaceImError::MethodOpenFailed((*inner).potential_input_methods.clone()) |
| 97 | + })?, |
| 98 | + is_fallback, |
| 99 | + ) |
| 100 | + }; |
| 101 | + |
| 102 | + // It's important to always set a destroy callback, since there's otherwise potential for us |
| 103 | + // to try to use or free a resource that's already been destroyed on the server. |
| 104 | + { |
| 105 | + let result = set_destroy_callback(xconn, new_im.im, &*inner); |
| 106 | + if result.is_err() { |
| 107 | + let _ = close_im(xconn, new_im.im); |
| 108 | + } |
| 109 | + result |
| 110 | + }.map_err(ReplaceImError::SetDestroyCallbackFailed)?; |
| 111 | + |
| 112 | + let mut new_contexts = HashMap::new(); |
| 113 | + for (window, old_context) in (*inner).contexts.iter() { |
| 114 | + let spot = old_context.as_ref().map(|old_context| old_context.ic_spot); |
| 115 | + let new_context = { |
| 116 | + let result = ImeContext::new( |
| 117 | + xconn, |
| 118 | + new_im.im, |
| 119 | + *window, |
| 120 | + spot, |
| 121 | + ); |
| 122 | + if result.is_err() { |
| 123 | + let _ = close_im(xconn, new_im.im); |
| 124 | + } |
| 125 | + result.map_err(ReplaceImError::ContextCreationFailed)? |
| 126 | + }; |
| 127 | + new_contexts.insert(*window, Some(new_context)); |
| 128 | + } |
| 129 | + |
| 130 | + // If we've made it this far, everything succeeded. |
| 131 | + let _ = (*inner).destroy_all_contexts_if_necessary(); |
| 132 | + let _ = (*inner).close_im_if_necessary(); |
| 133 | + (*inner).im = new_im.im; |
| 134 | + (*inner).contexts = new_contexts; |
| 135 | + (*inner).is_destroyed = false; |
| 136 | + (*inner).is_fallback = is_fallback; |
| 137 | + Ok(()) |
| 138 | +} |
| 139 | + |
| 140 | +pub unsafe extern fn xim_instantiate_callback( |
| 141 | + _display: *mut ffi::Display, |
| 142 | + client_data: ffi::XPointer, |
| 143 | + // This field is unsupplied. |
| 144 | + _call_data: ffi::XPointer, |
| 145 | +) { |
| 146 | + let inner: *mut ImeInner = client_data as _; |
| 147 | + if !inner.is_null() { |
| 148 | + let xconn = &(*inner).xconn; |
| 149 | + let result = replace_im(inner); |
| 150 | + if result.is_ok() { |
| 151 | + let _ = unset_instantiate_callback(xconn, client_data); |
| 152 | + (*inner).is_fallback = false; |
| 153 | + } else if result.is_err() && (*inner).is_destroyed { |
| 154 | + // We have no usable input methods! |
| 155 | + result.expect("Failed to reopen input method"); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// This callback is triggered when the input method is closed on the server end. When this |
| 161 | +// happens, XCloseIM/XDestroyIC doesn't need to be called, as the resources have already been |
| 162 | +// free'd (attempting to do so causes our connection to freeze). |
| 163 | +pub unsafe extern fn xim_destroy_callback( |
| 164 | + _xim: ffi::XIM, |
| 165 | + client_data: ffi::XPointer, |
| 166 | + // This field is unsupplied. |
| 167 | + _call_data: ffi::XPointer, |
| 168 | +) { |
| 169 | + let inner: *mut ImeInner = client_data as _; |
| 170 | + if !inner.is_null() { |
| 171 | + (*inner).is_destroyed = true; |
| 172 | + let xconn = &(*inner).xconn; |
| 173 | + if !(*inner).is_fallback { |
| 174 | + let _ = set_instantiate_callback(xconn, client_data); |
| 175 | + // Attempt to open fallback input method. |
| 176 | + let result = replace_im(inner); |
| 177 | + if result.is_ok() { |
| 178 | + (*inner).is_fallback = true; |
| 179 | + } else { |
| 180 | + // We have no usable input methods! |
| 181 | + result.expect("Failed to open fallback input method"); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments