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 x: Arc<XWindow>,
cursor: Mutex<MouseCursor>,
cursor_state: Mutex<CursorState>,
}
@ -149,6 +150,7 @@ impl Window2 {
root,
screen_id,
}),
cursor: Mutex::new(MouseCursor::Default),
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 {
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| {
self.load_cursor(name)
};
@ -582,7 +601,7 @@ impl Window2 {
// differs on the desktop environments or themes.
//
// Try the better looking (or more suiting) names first.
let xcursor = match cursor {
match cursor {
MouseCursor::Alias => load("link"),
MouseCursor::Arrow => load("arrow"),
MouseCursor::Cell => load("plus"),
@ -627,34 +646,27 @@ impl Window2 {
MouseCursor::ZoomOut => load("zoom-out"),
MouseCursor::NoneCursor => self.create_empty_cursor(),
};
}
}
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, xcursor);
if xcursor != 0 {
(self.x.display.xlib.XFreeCursor)(self.x.display.display, xcursor);
fn update_cursor(&self, cursor: ffi::Cursor) {
unsafe {
(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");
}
}
fn load_cursor(&self, name: &str) -> ffi::Cursor {
use std::ffi::CString;
unsafe {
let c_string = CString::new(name.as_bytes()).unwrap();
(self.x.display.xcursor.XcursorLibraryLoadCursor)(self.x.display.display, c_string.as_ptr())
pub fn set_cursor(&self, cursor: MouseCursor) {
let mut current_cursor = self.cursor.lock().unwrap();
*current_cursor = cursor;
if *self.cursor_state.lock().unwrap() != CursorState::Hide {
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
// the complexity, since cursor changes are not so common,
// and this is just allocating a 1x1 pixmap...
@ -699,27 +711,14 @@ impl Window2 {
}
},
Normal => {},
Hide => {
// 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);
}
},
Hide => self.update_cursor(self.get_cursor(*self.cursor.lock().unwrap())),
}
*cursor_state = state;
match state {
Normal => Ok(()),
Hide => {
unsafe {
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");
}
self.update_cursor(self.create_empty_cursor());
Ok(())
},
Grab => {