Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for X input method #691

Merged
merged 3 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,18 @@ impl Display {
pub fn get_window_id(&self) -> Option<usize> {
self.window.get_window_id()
}

/// Adjust the XIM editor position according to the new location of the cursor
pub fn update_ime_position(&mut self, terminal: &Term) {
use index::{Point, Line, Column};
use term::SizeInfo;
let Point{line: Line(row), col: Column(col)} = terminal.cursor().point;
let SizeInfo{cell_width: cw,
cell_height: ch,
padding_x: px,
padding_y: py, ..} = *terminal.size_info();
let nspot_y = (py + (row + 1) as f32 * ch) as i16;
let nspot_x = (px + col as f32 * cw) as i16;
self.window().send_xim_spot(nspot_x, nspot_y);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ fn run(mut config: Config, options: cli::Options) -> Result<(), Box<Error>> {

// Maybe draw the terminal
if terminal.needs_draw() {
// Try to update the position of the input method editor
display.update_ime_position(&terminal);
chrisduerr marked this conversation as resolved.
Show resolved Hide resolved
// Handle pending resize events
//
// The second argument is a list of types that want to be notified
Expand Down
7 changes: 6 additions & 1 deletion src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl IndexMut<CharsetIndex> for Charsets {
#[derive(Default, Copy, Clone)]
pub struct Cursor {
/// The location of this cursor
point: Point,
pub point: Point,

/// Template cell when using this cursor
template: Cell,
Expand Down Expand Up @@ -1054,6 +1054,11 @@ impl Term {
&self.mode
}

#[inline]
pub fn cursor(&self) -> &Cursor {
&self.cursor
}

pub fn swap_alt(&mut self) {
if self.alt {
let template = self.empty_cell;
Expand Down
32 changes: 32 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ impl Window {
title: &str
) -> Result<Window> {
let event_loop = EventsLoop::new();

Window::platform_window_init();
let window = WindowBuilder::new()
.with_title(title);
let context = ContextBuilder::new()
Expand Down Expand Up @@ -282,6 +284,36 @@ impl Window {
}
}

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
pub fn platform_window_init() {
/// Set up env to make XIM work correctly
use x11_dl::xlib;
use libc::{setlocale, LC_CTYPE};
let xlib = xlib::Xlib::open().expect("get xlib");
unsafe {
/// Use empty c string to fallback to LC_CTYPE in environment variables
setlocale(LC_CTYPE, b"\0".as_ptr() as *const _);
/// Use empty c string for implementation dependent behavior,
/// which might be the XMODIFIERS set in env
(xlib.XSetLocaleModifiers)(b"\0".as_ptr() as *const _);
}
}

/// TODO: change this directive when adding functions for other platforms
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd")))]
pub fn platform_window_init() {
}

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
pub fn send_xim_spot(&self, x: i16, y: i16) {
use glutin::os::unix::WindowExt;
self.window.send_xim_spot(x, y);
}

#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd")))]
pub fn send_xim_spot(&self, x: i16, y: i16) {
chrisduerr marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(not(target_os = "macos"))]
pub fn get_window_id(&self) -> Option<usize> {
use glutin::os::unix::WindowExt;
Expand Down