Conflicts:
	.travis.yml
	Cargo.toml
	examples/fullscreen.rs
	src/api/android/mod.rs
	src/api/cocoa/headless.rs
	src/api/cocoa/helpers.rs
	src/api/cocoa/mod.rs
	src/api/glx/mod.rs
	src/api/osmesa/mod.rs
	src/api/win32/callback.rs
	src/headless.rs
	src/lib.rs
	src/platform/linux/mod.rs
	src/window.rs
This commit is contained in:
Andrey Lesnikov 2016-09-19 19:53:28 +03:00
commit 7f6ae8910e
22 changed files with 332 additions and 194 deletions

View file

@ -14,7 +14,7 @@ pub fn keycode_to_element(scancode: libc::c_uint) -> Option<VirtualKeyCode> {
//ffi::XK_Sys_Req => events::VirtualKeyCode::Sys_req,
ffi::XK_Escape => events::VirtualKeyCode::Escape,
ffi::XK_Delete => events::VirtualKeyCode::Delete,
//ffi::XK_Multi_key => events::VirtualKeyCode::Multi_key,
ffi::XK_Multi_key => events::VirtualKeyCode::Compose,
//ffi::XK_Kanji => events::VirtualKeyCode::Kanji,
//ffi::XK_Muhenkan => events::VirtualKeyCode::Muhenkan,
//ffi::XK_Henkan_Mode => events::VirtualKeyCode::Henkan_mode,

View file

@ -241,7 +241,7 @@ impl XInputEventHandler {
let new_cursor_pos = (event_data.event_x, event_data.event_y);
if new_cursor_pos != self.current_state.cursor_pos {
self.current_state.cursor_pos = new_cursor_pos;
Some(MouseMoved((new_cursor_pos.0 as i32, new_cursor_pos.1 as i32)))
Some(MouseMoved(new_cursor_pos.0 as i32, new_cursor_pos.1 as i32))
} else {
None
}

View file

@ -3,7 +3,7 @@ use CreationError;
use CreationError::OsError;
use libc;
use std::borrow::Borrow;
use std::{mem, ptr};
use std::{mem, ptr, cmp};
use std::cell::Cell;
use std::sync::atomic::AtomicBool;
use std::collections::VecDeque;
@ -285,11 +285,23 @@ impl Window {
pub fn new(display: &Arc<XConnection>, window_attrs: &WindowAttributes)
-> Result<Window, CreationError>
{
let dimensions = window_attrs.dimensions.unwrap_or((800, 600));
let dimensions = {
// not implemented
assert!(window_attrs.min_dimensions.is_none());
assert!(window_attrs.max_dimensions.is_none());
// x11 only applies constraints when the window is actively resized
// by the user, so we have to manually apply the initial constraints
let mut dimensions = window_attrs.dimensions.unwrap_or((800, 600));
if let Some(max) = window_attrs.max_dimensions {
dimensions.0 = cmp::min(dimensions.0, max.0);
dimensions.1 = cmp::min(dimensions.1, max.1);
}
if let Some(min) = window_attrs.min_dimensions {
dimensions.0 = cmp::max(dimensions.0, min.0);
dimensions.1 = cmp::max(dimensions.1, min.1);
}
dimensions
};
let screen_id = match window_attrs.monitor {
Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32,
@ -502,6 +514,32 @@ impl Window {
(display.xf86vmode.XF86VidModeSetViewPort)(display.display, screen_id, 0, 0);
display.check_errors().expect("Failed to call XF86VidModeSetViewPort");
}
} else {
// set size hints
let mut size_hints: ffi::XSizeHints = unsafe { mem::zeroed() };
size_hints.flags = ffi::PSize;
size_hints.width = dimensions.0 as i32;
size_hints.height = dimensions.1 as i32;
if let Some(dimensions) = window_attrs.min_dimensions {
size_hints.flags |= ffi::PMinSize;
size_hints.min_width = dimensions.0 as i32;
size_hints.min_height = dimensions.1 as i32;
}
if let Some(dimensions) = window_attrs.max_dimensions {
size_hints.flags |= ffi::PMaxSize;
size_hints.max_width = dimensions.0 as i32;
size_hints.max_height = dimensions.1 as i32;
}
unsafe {
(display.xlib.XSetNormalHints)(display.display, window, &mut size_hints);
display.check_errors().expect("Failed to call XSetNormalHints");
}
}
// creating the window object
@ -706,51 +744,115 @@ impl Window {
pub fn set_cursor(&self, cursor: MouseCursor) {
unsafe {
use std::ffi::CString;
let cursor_name = match cursor {
MouseCursor::Alias => "link",
MouseCursor::Arrow => "arrow",
MouseCursor::Cell => "plus",
MouseCursor::Copy => "copy",
MouseCursor::Crosshair => "crosshair",
MouseCursor::Default => "left_ptr",
MouseCursor::Grabbing => "grabbing",
MouseCursor::Hand | MouseCursor::Grab => "hand",
MouseCursor::Help => "question_arrow",
MouseCursor::Move => "move",
MouseCursor::NoDrop => "circle",
MouseCursor::NotAllowed => "crossed_circle",
MouseCursor::Progress => "left_ptr_watch",
let load = |name: &str| {
self.load_cursor(name)
};
let loadn = |names: &[&str]| {
self.load_first_existing_cursor(names)
};
// Try multiple names in some cases where the name
// differs on the desktop environments or themes.
//
// Try the better looking (or more suiting) names first.
let mut xcursor = match cursor {
MouseCursor::Alias => load("link"),
MouseCursor::Arrow => load("arrow"),
MouseCursor::Cell => load("plus"),
MouseCursor::Copy => load("copy"),
MouseCursor::Crosshair => load("crosshair"),
MouseCursor::Default => load("left_ptr"),
MouseCursor::Hand => load("hand1"),
MouseCursor::Help => load("question_arrow"),
MouseCursor::Move => load("move"),
MouseCursor::Grab => loadn(&["openhand", "grab"]),
MouseCursor::Grabbing => loadn(&["closedhand", "grabbing"]),
MouseCursor::Progress => load("left_ptr_watch"),
MouseCursor::AllScroll => load("all-scroll"),
MouseCursor::ContextMenu => load("context-menu"),
MouseCursor::NoDrop => loadn(&["no-drop", "circle"]),
MouseCursor::NotAllowed => load("crossed_circle"),
/// Resize cursors
MouseCursor::EResize => "right_side",
MouseCursor::NResize => "top_side",
MouseCursor::NeResize => "top_right_corner",
MouseCursor::NwResize => "top_left_corner",
MouseCursor::SResize => "bottom_side",
MouseCursor::SeResize => "bottom_right_corner",
MouseCursor::SwResize => "bottom_left_corner",
MouseCursor::WResize => "left_side",
MouseCursor::EwResize | MouseCursor::ColResize => "h_double_arrow",
MouseCursor::NsResize | MouseCursor::RowResize => "v_double_arrow",
MouseCursor::NwseResize => "bd_double_arrow",
MouseCursor::NeswResize => "fd_double_arrow",
MouseCursor::EResize => load("right_side"),
MouseCursor::NResize => load("top_side"),
MouseCursor::NeResize => load("top_right_corner"),
MouseCursor::NwResize => load("top_left_corner"),
MouseCursor::SResize => load("bottom_side"),
MouseCursor::SeResize => load("bottom_right_corner"),
MouseCursor::SwResize => load("bottom_left_corner"),
MouseCursor::WResize => load("left_side"),
MouseCursor::EwResize => load("h_double_arrow"),
MouseCursor::NsResize => load("v_double_arrow"),
MouseCursor::NwseResize => loadn(&["bd_double_arrow", "size_bdiag"]),
MouseCursor::NeswResize => loadn(&["fd_double_arrow", "size_fdiag"]),
MouseCursor::ColResize => loadn(&["split_h", "h_double_arrow"]),
MouseCursor::RowResize => loadn(&["split_v", "v_double_arrow"]),
MouseCursor::Text | MouseCursor::VerticalText => "xterm",
MouseCursor::Wait => "watch",
MouseCursor::Text => loadn(&["text", "xterm"]),
MouseCursor::VerticalText => load("vertical-text"),
/// TODO: Find matching X11 cursors
MouseCursor::ContextMenu | MouseCursor::NoneCursor |
MouseCursor::AllScroll | MouseCursor::ZoomIn |
MouseCursor::ZoomOut => "left_ptr",
MouseCursor::Wait => load("watch"),
MouseCursor::ZoomIn => load("zoom-in"),
MouseCursor::ZoomOut => load("zoom-out"),
MouseCursor::NoneCursor => self.create_empty_cursor(),
};
let c_string = CString::new(cursor_name.as_bytes().to_vec()).unwrap();
let xcursor = (self.x.display.xcursor.XcursorLibraryLoadCursor)(self.x.display.display, c_string.as_ptr());
self.x.display.check_errors().expect("Failed to call XcursorLibraryLoadCursor");
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, xcursor);
(self.x.display.xlib.XFlush)(self.x.display.display);
(self.x.display.xlib.XFreeCursor)(self.x.display.display, xcursor);
self.x.display.check_errors().expect("Failed to call XDefineCursor");
if xcursor != 0 {
(self.x.display.xlib.XFreeCursor)(self.x.display.display, xcursor);
}
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())
}
}
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...
fn create_empty_cursor(&self) -> ffi::Cursor {
use std::mem;
let data = 0;
unsafe {
let pixmap = (self.x.display.xlib.XCreateBitmapFromData)(self.x.display.display, self.x.window, &data, 1, 1);
if pixmap == 0 {
// Failed to allocate
return 0;
}
// We don't care about this color, since it only fills bytes
// in the pixmap which are not 0 in the mask.
let dummy_color: ffi::XColor = mem::uninitialized();
let cursor = (self.x.display.xlib.XCreatePixmapCursor)(self.x.display.display,
pixmap,
pixmap,
&dummy_color as *const _ as *mut _,
&dummy_color as *const _ as *mut _, 0, 0);
(self.x.display.xlib.XFreePixmap)(self.x.display.display, pixmap);
cursor
}
}
@ -772,13 +874,10 @@ impl Window {
},
Normal => {},
Hide => {
// NB: Calling XDefineCursor with None (aka 0)
// as a value resets the cursor to the default.
unsafe {
let xcursor = (self.x.display.xlib.XCreateFontCursor)(self.x.display.display, 68/*XC_left_ptr*/);
self.x.display.check_errors().expect("Failed to call XCreateFontCursor");
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, xcursor);
self.x.display.check_errors().expect("Failed to call XDefineCursor");
(self.x.display.xlib.XFlush)(self.x.display.display);
(self.x.display.xlib.XFreeCursor)(self.x.display.display, xcursor);
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, 0);
}
},
}
@ -787,25 +886,20 @@ impl Window {
match state {
Normal => Ok(()),
Hide => {
let data = &[0, 0, 0, 0, 0, 0, 0, 0];
unsafe {
let mut black = ffi::XColor {
red: 0, green: 0, blue: 0,
pad: 0, pixel: 0, flags: 0,
};
let bitmap = (self.x.display.xlib.XCreateBitmapFromData)(self.x.display.display, self.x.window, data.as_ptr(), 8, 8);
let cursor = (self.x.display.xlib.XCreatePixmapCursor)(self.x.display.display, bitmap, bitmap, &mut black, &mut black, 0, 0);
let cursor = self.create_empty_cursor();
(self.x.display.xlib.XDefineCursor)(self.x.display.display, self.x.window, cursor);
self.x.display.check_errors().expect("Failed to call XDefineCursor");
(self.x.display.xlib.XFreeCursor)(self.x.display.display, cursor);
(self.x.display.xlib.XFreePixmap)(self.x.display.display, bitmap);
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(())
},
Grab => {
unsafe {
match (self.x.display.xlib.XGrabPointer)(
self.x.display.display, self.x.window, ffi::False,
self.x.display.display, self.x.window, ffi::True,
(ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::EnterWindowMask |
ffi::LeaveWindowMask | ffi::PointerMotionMask | ffi::PointerMotionHintMask |
ffi::Button1MotionMask | ffi::Button2MotionMask | ffi::Button3MotionMask |