x11: Remember cursor after changing cursor state (#374)

This commit is contained in:
Tuomas Siipola 2017-12-27 15:30:59 +02:00 committed by Pierre Krieger
parent 011720848a
commit f89dc9e903

View file

@ -61,6 +61,7 @@ unsafe impl Sync for Window2 {}
pub struct Window2 { pub struct Window2 {
pub x: Arc<XWindow>, pub x: Arc<XWindow>,
cursor: Mutex<MouseCursor>,
cursor_state: Mutex<CursorState>, cursor_state: Mutex<CursorState>,
} }
@ -149,6 +150,7 @@ impl Window2 {
root, root,
screen_id, screen_id,
}), }),
cursor: Mutex::new(MouseCursor::Default),
cursor_state: Mutex::new(CursorState::Normal), cursor_state: Mutex::new(CursorState::Normal),
}; };
@ -568,8 +570,25 @@ impl Window2 {
} }
} }
pub fn set_cursor(&self, cursor: MouseCursor) { fn load_cursor(&self, name: &str) -> ffi::Cursor {
use std::ffi::CString;
unsafe { unsafe {
let c_string = CString::new(name.as_bytes()).unwrap();
(self.x.display.xcursor.XcursorLibraryLoadCursor)(self.x.display.display, c_string.as_ptr())
}
}
fn load_first_existing_cursor(&self, names :&[&str]) -> ffi::Cursor {
for name in names.iter() {
let xcursor = self.load_cursor(name);
if xcursor != 0 {
return xcursor;
}
}
0
}
fn get_cursor(&self, cursor: MouseCursor) -> ffi::Cursor {
let load = |name: &str| { let load = |name: &str| {
self.load_cursor(name) self.load_cursor(name)
}; };
@ -582,7 +601,7 @@ impl Window2 {
// differs on the desktop environments or themes. // differs on the desktop environments or themes.
// //
// Try the better looking (or more suiting) names first. // Try the better looking (or more suiting) names first.
let xcursor = match cursor { match cursor {
MouseCursor::Alias => load("link"), MouseCursor::Alias => load("link"),
MouseCursor::Arrow => load("arrow"), MouseCursor::Arrow => load("arrow"),
MouseCursor::Cell => load("plus"), MouseCursor::Cell => load("plus"),
@ -627,34 +646,27 @@ impl Window2 {
MouseCursor::ZoomOut => load("zoom-out"), MouseCursor::ZoomOut => load("zoom-out"),
MouseCursor::NoneCursor => self.create_empty_cursor(), MouseCursor::NoneCursor => self.create_empty_cursor(),
}; }
}
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, xcursor); fn update_cursor(&self, cursor: ffi::Cursor) {
if xcursor != 0 { unsafe {
(self.x.display.xlib.XFreeCursor)(self.x.display.display, xcursor); (self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, cursor);
if cursor != 0 {
(self.x.display.xlib.XFreeCursor)(self.x.display.display, cursor);
} }
self.x.display.check_errors().expect("Failed to set or free the cursor"); self.x.display.check_errors().expect("Failed to set or free the cursor");
} }
} }
fn load_cursor(&self, name: &str) -> ffi::Cursor { pub fn set_cursor(&self, cursor: MouseCursor) {
use std::ffi::CString; let mut current_cursor = self.cursor.lock().unwrap();
unsafe { *current_cursor = cursor;
let c_string = CString::new(name.as_bytes()).unwrap(); if *self.cursor_state.lock().unwrap() != CursorState::Hide {
(self.x.display.xcursor.XcursorLibraryLoadCursor)(self.x.display.display, c_string.as_ptr()) self.update_cursor(self.get_cursor(*current_cursor));
} }
} }
fn load_first_existing_cursor(&self, names :&[&str]) -> ffi::Cursor {
for name in names.iter() {
let xcursor = self.load_cursor(name);
if xcursor != 0 {
return xcursor;
}
}
0
}
// TODO: This could maybe be cached. I don't think it's worth // TODO: This could maybe be cached. I don't think it's worth
// the complexity, since cursor changes are not so common, // the complexity, since cursor changes are not so common,
// and this is just allocating a 1x1 pixmap... // and this is just allocating a 1x1 pixmap...
@ -699,27 +711,14 @@ impl Window2 {
} }
}, },
Normal => {}, Normal => {},
Hide => { Hide => self.update_cursor(self.get_cursor(*self.cursor.lock().unwrap())),
// NB: Calling XDefineCursor with None (aka 0)
// as a value resets the cursor to the default.
unsafe {
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, 0);
}
},
} }
*cursor_state = state; *cursor_state = state;
match state { match state {
Normal => Ok(()), Normal => Ok(()),
Hide => { Hide => {
unsafe { self.update_cursor(self.create_empty_cursor());
let cursor = self.create_empty_cursor();
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, cursor);
if cursor != 0 {
(self.x.display.xlib.XFreeCursor)(self.x.display.display, cursor);
}
self.x.display.check_errors().expect("Failed to call XDefineCursor or free the empty cursor");
}
Ok(()) Ok(())
}, },
Grab => { Grab => {