Merge pull request #126 from tomaka/impl-20

Switch to a futures-compatible API
This commit is contained in:
tomaka 2017-02-11 08:47:23 +01:00 committed by GitHub
commit 3ad1274a6d
23 changed files with 410 additions and 278 deletions

View file

@ -1,17 +1,19 @@
extern crate winit;
use winit::{Event, ElementState, MouseCursor};
use winit::{Event, ElementState, MouseCursor, WindowEvent};
fn main() {
let window = winit::WindowBuilder::new().build().unwrap();
let events_loop = winit::EventsLoop::new();
let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
window.set_title("A fantastic window!");
let cursors = [MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand, MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text, MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress, MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::NoneCursor, MouseCursor::Cell, MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy, MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing, MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut, MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize, MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize, MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize, MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize, MouseCursor::ColResize, MouseCursor::RowResize];
let mut cursor_idx = 0;
for event in window.wait_events() {
events_loop.run_forever(|event| {
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
Event::WindowEvent { event: WindowEvent::KeyboardInput(ElementState::Pressed, _, _), .. } => {
println!("Setting cursor to \"{:?}\"", cursors[cursor_idx]);
window.set_cursor(cursors[cursor_idx]);
if cursor_idx < cursors.len() - 1 {
@ -20,8 +22,10 @@ fn main() {
cursor_idx = 0;
}
},
Event::Closed => break,
_ => (),
Event::WindowEvent { event: WindowEvent::Closed, .. } => {
events_loop.interrupt()
},
_ => ()
}
}
});
}

View file

@ -22,19 +22,25 @@ fn main() {
monitor
};
let window = winit::WindowBuilder::new()
let events_loop = winit::EventsLoop::new();
let _window = winit::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor)
.build()
.build(&events_loop)
.unwrap();
for event in window.wait_events() {
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
winit::Event::Closed => break,
winit::Event::KeyboardInput(_, _, Some(winit::VirtualKeyCode::Escape)) => break,
_ => ()
winit::Event::WindowEvent { event, .. } => {
match event {
winit::WindowEvent::Closed => events_loop.interrupt(),
winit::WindowEvent::KeyboardInput(_, _, Some(winit::VirtualKeyCode::Escape)) => events_loop.interrupt(),
_ => ()
}
},
}
}
});
}

View file

@ -1,34 +1,42 @@
extern crate winit;
use winit::{Event, ElementState};
use winit::{WindowEvent, ElementState};
fn main() {
let window = winit::WindowBuilder::new().build().unwrap();
let events_loop = winit::EventsLoop::new();
let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
window.set_title("winit - Cursor grabbing test");
let mut grabbed = false;
for event in window.wait_events() {
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
if grabbed {
grabbed = false;
window.set_cursor_state(winit::CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
window.set_cursor_state(winit::CursorState::Grab)
.ok().expect("could not grab mouse cursor");
winit::Event::WindowEvent { event, .. } => {
match event {
WindowEvent::KeyboardInput(ElementState::Pressed, _, _) => {
if grabbed {
grabbed = false;
window.set_cursor_state(winit::CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
window.set_cursor_state(winit::CursorState::Grab)
.ok().expect("could not grab mouse cursor");
}
},
WindowEvent::Closed => events_loop.interrupt(),
a @ WindowEvent::MouseMoved(_, _) => {
println!("{:?}", a);
},
_ => (),
}
},
Event::Closed => break,
a @ Event::MouseMoved(_, _) => {
println!("{:?}", a);
},
_ => (),
}
}
});
}

View file

@ -1,16 +1,20 @@
extern crate winit;
fn main() {
let window = winit::WindowBuilder::new()
let events_loop = winit::EventsLoop::new();
let _window = winit::WindowBuilder::new()
.with_min_dimensions(400, 200)
.with_max_dimensions(800, 400)
.build()
.build(&events_loop)
.unwrap();
for event in window.wait_events() {
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
winit::Event::Closed => break,
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => events_loop.interrupt(),
_ => ()
}
}
});
}

View file

@ -1,34 +1,33 @@
extern crate winit;
use std::thread;
fn main() {
let window1 = winit::WindowBuilder::new().build().unwrap();
let window2 = winit::WindowBuilder::new().build().unwrap();
let window3 = winit::WindowBuilder::new().build().unwrap();
let events_loop = winit::EventsLoop::new();
let t1 = thread::spawn(move || {
run(window1);
});
let window1 = winit::Window::new(&events_loop).unwrap();
let window2 = winit::Window::new(&events_loop).unwrap();
let window3 = winit::Window::new(&events_loop).unwrap();
let t2 = thread::spawn(move || {
run(window2);
});
let mut num_windows = 3;
let t3 = thread::spawn(move || {
run(window3);
});
let _ = t1.join();
let _ = t2.join();
let _ = t3.join();
}
fn run(window: winit::Window) {
for event in window.wait_events() {
events_loop.run_forever(|event| {
match event {
winit::Event::Closed => break,
_ => ()
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, window_id } => {
if window_id == window1.id() {
println!("Window 1 has been closed")
} else if window_id == window2.id() {
println!("Window 2 has been closed")
} else if window_id == window3.id() {
println!("Window 3 has been closed");
} else {
unreachable!()
}
num_windows -= 1;
if num_windows == 0 {
events_loop.interrupt();
}
},
_ => (),
}
}
})
}

View file

@ -1,22 +1,20 @@
extern crate winit;
fn resize_callback(width: u32, height: u32) {
println!("Window resized to {}x{}", width, height);
}
fn main() {
let mut window = winit::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let events_loop = winit::EventsLoop::new();
for event in window.wait_events() {
let window = winit::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build(&events_loop).unwrap();
window.set_title("A fantastic window!");
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
winit::Event::Closed => break,
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => events_loop.interrupt(),
_ => ()
}
}
});
}

View file

@ -1,22 +1,19 @@
extern crate winit;
fn resize_callback(width: u32, height: u32) {
println!("Window resized to {}x{}", width, height);
}
fn main() {
let events_loop = winit::EventsLoop::new();
let window = winit::WindowBuilder::new()
.with_title("A fantastic window!")
.with_window_resize_callback(resize_callback)
.build()
.build(&events_loop)
.unwrap();
for event in window.wait_events() {
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
winit::Event::Closed => break,
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => events_loop.interrupt(),
_ => ()
}
}
});
}

View file

@ -1,4 +1,4 @@
use {Event, ElementState, MouseButton, MouseScrollDelta, TouchPhase};
use {WindowEvent as Event, ElementState, MouseButton, MouseScrollDelta, TouchPhase};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

View file

@ -1,7 +1,7 @@
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use {VirtualKeyCode, ElementState, Event};
use {VirtualKeyCode, ElementState, WindowEvent as Event};
use super::wayland_kbd;
use wayland_client::EventQueueHandle;

View file

@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use wayland_client::{EventQueue, EventQueueHandle, Init};
use wayland_client::protocol::{wl_display,wl_surface,wl_shell_surface};
use {CreationError, MouseCursor, CursorState, Event, WindowAttributes};
use {CreationError, MouseCursor, CursorState, WindowEvent as Event, WindowAttributes};
use platform::MonitorId as PlatformMonitorId;
use super::WaylandContext;

View file

@ -7,7 +7,7 @@ use std::slice::from_raw_parts;
use WindowAttributes;
use events::Event;
use events::WindowEvent as Event;
use super::{events, ffi};
use super::XConnection;
@ -123,7 +123,7 @@ impl XInputEventHandler {
}
pub fn translate_key_event(&self, event: &mut ffi::XKeyEvent) -> Vec<Event> {
use events::Event::{KeyboardInput, ReceivedCharacter};
use events::WindowEvent::{KeyboardInput, ReceivedCharacter};
use events::ElementState::{Pressed, Released};
let mut translated_events = Vec::new();
@ -170,7 +170,7 @@ impl XInputEventHandler {
}
pub fn translate_event(&mut self, cookie: &ffi::XGenericEventCookie) -> Option<Event> {
use events::Event::{Focused, MouseEntered, MouseInput, MouseLeft, MouseMoved, MouseWheel};
use events::WindowEvent::{Focused, MouseEntered, MouseInput, MouseLeft, MouseMoved, MouseWheel};
use events::ElementState::{Pressed, Released};
use events::MouseButton::{Left, Right, Middle};
use events::MouseScrollDelta::LineDelta;

View file

@ -1,4 +1,4 @@
use {Event, MouseCursor};
use {WindowEvent as Event, MouseCursor};
use CreationError;
use CreationError::OsError;
use libc;
@ -178,7 +178,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
},
ffi::ClientMessage => {
use events::Event::{Closed, Awakened};
use events::WindowEvent::{Closed, Awakened};
use std::sync::atomic::Ordering::Relaxed;
let client_msg: &ffi::XClientMessageEvent = unsafe { mem::transmute(&xev) };
@ -192,7 +192,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
},
ffi::ConfigureNotify => {
use events::Event::Resized;
use events::WindowEvent::Resized;
let cfg_event: &ffi::XConfigureEvent = unsafe { mem::transmute(&xev) };
let (current_width, current_height) = self.window.current_size.get();
if current_width != cfg_event.width || current_height != cfg_event.height {
@ -202,7 +202,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
},
ffi::Expose => {
use events::Event::Refresh;
use events::WindowEvent::Refresh;
return Some(Refresh);
},

101
src/api_transition.rs Normal file
View file

@ -0,0 +1,101 @@
//! This temporary module generates types that wrap around the old API (winit v5 and below) and
//! expose the new API (winit v6 and above).
//!
//! This is temporary so that existing backends can smoothly transition. After all implementations
//! have finished transitionning, this module should disappear.
macro_rules! gen_api_transition {
() => {
pub struct EventsLoop {
windows: ::std::sync::Mutex<Vec<::std::sync::Arc<Window>>>,
interrupted: ::std::sync::atomic::AtomicBool,
}
impl EventsLoop {
pub fn new() -> EventsLoop {
EventsLoop {
windows: ::std::sync::Mutex::new(vec![]),
interrupted: ::std::sync::atomic::AtomicBool::new(false),
}
}
pub fn interrupt(&self) {
self.interrupted.store(true, ::std::sync::atomic::Ordering::Relaxed);
}
pub fn poll_events<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
let mut windows = self.windows.lock().unwrap();
for window in windows.iter() {
for event in window.poll_events() {
callback(::Event::WindowEvent {
window_id: ::WindowId(WindowId(&**window as *const Window as usize)),
event: event,
})
}
}
}
pub fn run_forever<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
self.interrupted.store(false, ::std::sync::atomic::Ordering::Relaxed);
// Yeah that's a very bad implementation.
loop {
self.poll_events(|e| callback(e));
::std::thread::sleep_ms(5);
if self.interrupted.load(::std::sync::atomic::Ordering::Relaxed) {
break;
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(usize);
pub struct Window2 {
pub window: ::std::sync::Arc<Window>,
events_loop: ::std::sync::Weak<EventsLoop>,
}
impl ::std::ops::Deref for Window2 {
type Target = Window;
#[inline]
fn deref(&self) -> &Window {
&*self.window
}
}
impl Window2 {
pub fn new(events_loop: ::std::sync::Arc<EventsLoop>, window: &::WindowAttributes,
pl_attribs: &PlatformSpecificWindowBuilderAttributes)
-> Result<Window2, CreationError>
{
let win = ::std::sync::Arc::new(try!(Window::new(window, pl_attribs)));
events_loop.windows.lock().unwrap().push(win.clone());
Ok(Window2 {
window: win,
events_loop: ::std::sync::Arc::downgrade(&events_loop),
})
}
#[inline]
pub fn id(&self) -> WindowId {
WindowId(&*self.window as *const Window as usize)
}
}
impl Drop for Window2 {
fn drop(&mut self) {
if let Some(ev) = self.events_loop.upgrade() {
let mut windows = ev.windows.lock().unwrap();
windows.retain(|w| &**w as *const Window != &*self.window as *const _);
}
}
}
};
}

View file

@ -1,7 +1,19 @@
use std::path::PathBuf;
use WindowId;
#[derive(Clone, Debug)]
pub enum Event {
WindowEvent {
window_id: WindowId,
event: WindowEvent,
}
}
#[derive(Clone, Debug)]
pub enum WindowEvent {
// TODO: remove ; can break the lib internally so be careful
Awakened,
/// The size of the window has changed.
Resized(u32, u32),
@ -49,9 +61,6 @@ pub enum Event {
/// is being pressed) and stage (integer representing the click level).
TouchpadPressure(f32, i64),
/// The event loop was woken up by another thread.
Awakened,
/// The window needs to be redrawn.
Refresh,
@ -60,7 +69,6 @@ pub enum Event {
/// The parameter is true if app was suspended, and false if it has been resumed.
Suspended(bool),
/// Touch event has been received
Touch(Touch)
}

View file

@ -2,10 +2,18 @@
//!
//! # Building a window
//!
//! There are two ways to create a window:
//! Before you can build a window, you first need to build an `EventsLoop`. This is done with the
//! `EventsLoop::new()` function. Example:
//!
//! - Calling `Window::new()`.
//! - Calling `let builder = WindowBuilder::new()` then `builder.build()`.
//! ```no_run
//! use winit::EventsLoop;
//! let events_loop = EventsLoop::new();
//! ```
//!
//! Once this is done there are two ways to create a window:
//!
//! - Calling `Window::new(&events_loop)`.
//! - Calling `let builder = WindowBuilder::new()` then `builder.build(&events_loop)`.
//!
//! The first way is the simpliest way and will give you default values for everything.
//!
@ -14,11 +22,58 @@
//!
//! # Events handling
//!
//! Once a window has been created, you can handle the events that it generates. There are two ways
//! to do so: with `poll_events` or with `wait_events`. The former returns an iterator that ends
//! when no event is available, and the latter returns an iterator that blocks and waits for events
//! if none is available. Depending on which kind of program you're writing, you usually choose
//! one or the other.
//! Once a window has been created, it will *generate events*. For example whenever the user moves
//! the window, resizes the window, moves the mouse, etc. an event is generated.
//!
//! The events generated by a window can be retreived from the `EventsLoop` the window was created
//! with.
//!
//! There are two ways to do so. The first is to call `events_loop.poll_events(...)`, which will
//! retreive all the events pending on the windows and immediately return after no new event is
//! available. You usually want to use this method in application that render continuously on the
//! screen, such as video games.
//!
//! ```no_run
//! use winit::Event;
//! use winit::WindowEvent;
//! # use winit::EventsLoop;
//! # let events_loop = EventsLoop::new();
//!
//! loop {
//! events_loop.poll_events(|event| {
//! match event {
//! Event::WindowEvent { event: WindowEvent::Resized(w, h), .. } => {
//! println!("The window was resized to {}x{}", w, h);
//! },
//! _ => ()
//! }
//! });
//! }
//! ```
//!
//! The second way is to call `events_loop.run_forever(...)`. As its name tells, it will run
//! forever unless it is stopped by calling `events_loop.interrupt()`.
//!
//! ```no_run
//! use winit::Event;
//! use winit::WindowEvent;
//! # use winit::EventsLoop;
//! # let events_loop = EventsLoop::new();
//!
//! events_loop.run_forever(|event| {
//! match event {
//! Event::WindowEvent { event: WindowEvent::Closed, .. } => {
//! println!("The window was closed ; stopping");
//! events_loop.interrupt();
//! },
//! _ => ()
//! }
//! });
//! ```
//!
//! If you use multiple windows, the `WindowEvent` event has a member named `window_id`. You can
//! compare it with the value returned by the `id()` method of `Window` in order to know which
//! window has received the event.
//!
//! # Drawing on the window
//!
@ -64,11 +119,15 @@ extern crate x11_dl;
#[macro_use(wayland_env,declare_handler)]
extern crate wayland_client;
use std::sync::Arc;
pub use events::*;
pub use window::{WindowProxy, PollEventsIterator, WaitEventsIterator};
pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_primary_monitor};
pub use native_monitor::NativeMonitorId;
#[macro_use]
mod api_transition;
mod api;
mod platform;
mod events;
@ -81,20 +140,73 @@ pub mod os;
/// # Example
///
/// ```no_run
/// use winit::Event;
/// use winit::EventsLoop;
/// use winit::Window;
/// let window = Window::new().unwrap();
/// use winit::WindowEvent;
///
/// loop {
/// for event in window.wait_events() {
/// match event {
/// // process events here
/// _ => ()
/// }
/// let events_loop = EventsLoop::new();
/// let window = Window::new(&events_loop).unwrap();
///
/// events_loop.run_forever(|event| {
/// match event {
/// Event::WindowEvent { event: WindowEvent::Closed, .. } => {
/// events_loop.interrupt();
/// },
/// _ => ()
/// }
/// }
/// });
/// ```
pub struct Window {
window: platform::Window,
window: platform::Window2,
}
/// Identifier of a window. Unique for each window.
///
/// Can be obtained with `window.id()`.
///
/// Whenever you receive an event specific to a window, this event contains a `WindowId` which you
/// can then compare to the ids of your windows.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(platform::WindowId);
/// Provides a way to retreive events from the windows that were registered to it.
// TODO: document usage in multiple threads
pub struct EventsLoop {
events_loop: Arc<platform::EventsLoop>,
}
impl EventsLoop {
/// Builds a new events loop.
pub fn new() -> EventsLoop {
EventsLoop {
events_loop: Arc::new(platform::EventsLoop::new()),
}
}
/// Fetches all the events that are pending, calls the callback function for each of them,
/// and returns.
#[inline]
pub fn poll_events<F>(&self, callback: F)
where F: FnMut(Event)
{
self.events_loop.poll_events(callback)
}
/// Runs forever until `interrupt()` is called. Whenever an event happens, calls the callback.
#[inline]
pub fn run_forever<F>(&self, callback: F)
where F: FnMut(Event)
{
self.events_loop.run_forever(callback)
}
/// If we called `run_forever()`, stops the process of waiting for events.
// TODO: what if we're waiting from multiple threads?
#[inline]
pub fn interrupt(&self) {
self.events_loop.interrupt()
}
}
/// Object that allows you to build windows.
@ -257,10 +369,6 @@ pub struct WindowAttributes {
/// [iOS only] Enable multitouch, see [UIView#multipleTouchEnabled]
/// (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/multipleTouchEnabled)
pub multitouch: bool,
/// A function called upon resizing, necessary to receive resize events on Mac and possibly
/// other systems.
pub resize_callback: Option<fn(u32, u32)>,
}
impl Default for WindowAttributes {
@ -276,7 +384,6 @@ impl Default for WindowAttributes {
transparent: false,
decorations: true,
multitouch: false,
resize_callback: None,
}
}
}

View file

@ -86,7 +86,7 @@ pub trait WindowExt {
impl WindowExt for Window {
#[inline]
fn get_xlib_window(&self) -> Option<*mut libc::c_void> {
match self.window {
match *self.window.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_window()),
_ => None
}
@ -94,28 +94,28 @@ impl WindowExt for Window {
#[inline]
fn get_xlib_display(&self) -> Option<*mut libc::c_void> {
match self.window {
match *self.window.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_display()),
_ => None
}
}
fn get_xlib_screen_id(&self) -> Option<*mut libc::c_void> {
match self.window {
match *self.window.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_screen_id()),
_ => None
}
}
fn get_xlib_xconnection(&self) -> Option<Arc<XConnection>> {
match self.window {
match *self.window.window {
LinuxWindow::X(ref w) => Some(w.get_xlib_xconnection()),
_ => None
}
}
fn get_xcb_connection(&self) -> Option<*mut libc::c_void> {
match self.window {
match *self.window.window {
LinuxWindow::X(ref w) => Some(w.get_xcb_connection()),
_ => None
}
@ -136,7 +136,7 @@ impl WindowExt for Window {
#[inline]
fn get_wayland_client_surface(&self) -> Option<&WlSurface> {
match self.window {
match *self.window.window {
LinuxWindow::Wayland(ref w) => Some(w.get_surface()),
_ => None
}
@ -144,7 +144,7 @@ impl WindowExt for Window {
#[inline]
fn get_wayland_client_display(&self) -> Option<&WlDisplay> {
match self.window {
match *self.window.window {
LinuxWindow::Wayland(ref w) => Some(w.get_display()),
_ => None
}

View file

@ -6,7 +6,7 @@ use libc;
use std::ffi::{CString};
use std::sync::mpsc::{Receiver, channel};
use std::os::raw::c_void;
use {CreationError, Event, MouseCursor};
use {CreationError, WindowEvent as Event, MouseCursor};
use CreationError::OsError;
use events::ElementState::{Pressed, Released};
use events::{Touch, TouchPhase};
@ -17,6 +17,8 @@ use CursorState;
use WindowAttributes;
use native_monitor::NativeMonitorId;
gen_api_transition!();
pub struct Window {
native_window: *const c_void,
event_rx: Receiver<android_glue::Event>,

View file

@ -71,7 +71,8 @@ use objc::runtime::{Class, Object, Sel, BOOL, YES };
use objc::declare::{ ClassDecl };
use native_monitor::NativeMonitorId;
use { CreationError, CursorState, MouseCursor, Event, WindowAttributes };
use { CreationError, CursorState, MouseCursor, WindowAttributes };
use WindowEvent as Event;
use events::{ Touch, TouchPhase };
mod ffi;
@ -163,6 +164,8 @@ impl MonitorId {
}
}
gen_api_transition!();
#[derive(Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes;

View file

@ -5,7 +5,7 @@ use std::sync::Arc;
use CreationError;
use CursorState;
use Event;
use WindowEvent as Event;
use MouseCursor;
use WindowAttributes;
use libc;
@ -17,6 +17,8 @@ use api::x11::XError;
use api::x11::XNotSupported;
use api::x11::ffi::XVisualInfo;
gen_api_transition!();
#[derive(Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub visual_infos: Option<XVisualInfo>,

View file

@ -1,6 +1,6 @@
#![cfg(target_os = "macos")]
use {CreationError, Event, MouseCursor, CursorState};
use {CreationError, WindowEvent as Event, MouseCursor, CursorState};
use CreationError::OsError;
use libc;
@ -29,6 +29,8 @@ use os::macos::WindowExt;
use events::ElementState;
use events::{self, MouseButton, TouchPhase};
gen_api_transition!();
pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
mod monitor;

View file

@ -7,7 +7,7 @@ use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use CursorState;
use Event;
use WindowEvent as Event;
use super::event;
use super::WindowState;
@ -68,7 +68,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
{
match msg {
winapi::WM_DESTROY => {
use events::Event::Closed;
use events::WindowEvent::Closed;
CONTEXT_STASH.with(|context_stash| {
let context_stash = context_stash.borrow();
@ -93,7 +93,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_SIZE => {
use events::Event::Resized;
use events::WindowEvent::Resized;
let w = winapi::LOWORD(lparam as winapi::DWORD) as u32;
let h = winapi::HIWORD(lparam as winapi::DWORD) as u32;
send_event(window, Resized(w, h));
@ -101,7 +101,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_MOVE => {
use events::Event::Moved;
use events::WindowEvent::Moved;
let x = winapi::LOWORD(lparam as winapi::DWORD) as i32;
let y = winapi::HIWORD(lparam as winapi::DWORD) as i32;
send_event(window, Moved(x, y));
@ -110,7 +110,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
winapi::WM_CHAR => {
use std::mem;
use events::Event::ReceivedCharacter;
use events::WindowEvent::ReceivedCharacter;
let chr: char = mem::transmute(wparam as u32);
send_event(window, ReceivedCharacter(chr));
0
@ -125,7 +125,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
}
winapi::WM_MOUSEMOVE => {
use events::Event::{MouseEntered, MouseMoved};
use events::WindowEvent::{MouseEntered, MouseMoved};
let mouse_outside_window = CONTEXT_STASH.with(|context_stash| {
let mut context_stash = context_stash.borrow_mut();
if let Some(context_stash) = context_stash.as_mut() {
@ -159,7 +159,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_MOUSELEAVE => {
use events::Event::MouseLeft;
use events::WindowEvent::MouseLeft;
let mouse_in_window = CONTEXT_STASH.with(|context_stash| {
let mut context_stash = context_stash.borrow_mut();
if let Some(context_stash) = context_stash.as_mut() {
@ -180,7 +180,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_MOUSEWHEEL => {
use events::Event::MouseWheel;
use events::WindowEvent::MouseWheel;
use events::MouseScrollDelta::LineDelta;
use events::TouchPhase;
@ -194,7 +194,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_KEYDOWN | winapi::WM_SYSKEYDOWN => {
use events::Event::KeyboardInput;
use events::WindowEvent::KeyboardInput;
use events::ElementState::Pressed;
if msg == winapi::WM_SYSKEYDOWN && wparam as i32 == winapi::VK_F4 {
user32::DefWindowProcW(window, msg, wparam, lparam)
@ -206,7 +206,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_KEYUP | winapi::WM_SYSKEYUP => {
use events::Event::KeyboardInput;
use events::WindowEvent::KeyboardInput;
use events::ElementState::Released;
let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam);
send_event(window, KeyboardInput(Released, scancode, vkey));
@ -214,7 +214,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_LBUTTONDOWN => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Left;
use events::ElementState::Pressed;
send_event(window, MouseInput(Pressed, Left));
@ -222,7 +222,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_LBUTTONUP => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Left;
use events::ElementState::Released;
send_event(window, MouseInput(Released, Left));
@ -230,7 +230,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_RBUTTONDOWN => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Right;
use events::ElementState::Pressed;
send_event(window, MouseInput(Pressed, Right));
@ -238,7 +238,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_RBUTTONUP => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Right;
use events::ElementState::Released;
send_event(window, MouseInput(Released, Right));
@ -246,7 +246,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_MBUTTONDOWN => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Middle;
use events::ElementState::Pressed;
send_event(window, MouseInput(Pressed, Middle));
@ -254,7 +254,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_MBUTTONUP => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Middle;
use events::ElementState::Released;
send_event(window, MouseInput(Released, Middle));
@ -262,7 +262,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_XBUTTONDOWN => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Other;
use events::ElementState::Pressed;
let xbutton = winapi::HIWORD(wparam as winapi::DWORD) as winapi::c_int; // waiting on PR for winapi to add GET_XBUTTON_WPARAM
@ -271,7 +271,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_XBUTTONUP => {
use events::Event::MouseInput;
use events::WindowEvent::MouseInput;
use events::MouseButton::Other;
use events::ElementState::Released;
let xbutton = winapi::HIWORD(wparam as winapi::DWORD) as winapi::c_int;
@ -300,13 +300,13 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_SETFOCUS => {
use events::Event::Focused;
use events::WindowEvent::Focused;
send_event(window, Focused(true));
0
},
winapi::WM_KILLFOCUS => {
use events::Event::Focused;
use events::WindowEvent::Focused;
send_event(window, Focused(false));
0
},
@ -345,7 +345,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
winapi::WM_DROPFILES => {
use events::Event::DroppedFile;
use events::WindowEvent::DroppedFile;
let hdrop = wparam as winapi::HDROP;
let mut pathbuf: [u16; winapi::MAX_PATH] = mem::uninitialized();
@ -394,7 +394,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
},
x if x == *super::WAKEUP_MSG_ID => {
use events::Event::Awakened;
use events::WindowEvent::Awakened;
send_event(window, Awakened);
0
},

View file

@ -10,11 +10,13 @@ use std::sync::{
Mutex
};
use std::sync::mpsc::Receiver;
use {CreationError, Event, MouseCursor};
use {CreationError, WindowEvent as Event, MouseCursor};
use CursorState;
use WindowAttributes;
gen_api_transition!();
#[derive(Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub parent: Option<winapi::HWND>,

View file

@ -2,10 +2,12 @@ use std::collections::vec_deque::IntoIter as VecDequeIter;
use CreationError;
use CursorState;
use Event;
use WindowEvent as Event;
use EventsLoop;
use MouseCursor;
use Window;
use WindowBuilder;
use WindowId;
use native_monitor::NativeMonitorId;
use libc;
@ -93,20 +95,11 @@ impl WindowBuilder {
self
}
/// Provides a resize callback that is called by Mac (and potentially other
/// operating systems) during resize operations. This can be used to repaint
/// during window resizing.
#[inline]
pub fn with_window_resize_callback(mut self, cb: fn(u32, u32)) -> WindowBuilder {
self.window.resize_callback = Some(cb);
self
}
/// Builds the window.
///
/// Error should be very rare and only occur in case of permission denied, incompatible system,
/// out of memory, etc.
pub fn build(mut self) -> Result<Window, CreationError> {
pub fn build(mut self, events_loop: &EventsLoop) -> Result<Window, CreationError> {
// resizing the window to the dimensions of the monitor when fullscreen
if self.window.dimensions.is_none() && self.window.monitor.is_some() {
self.window.dimensions = Some(self.window.monitor.as_ref().unwrap().get_dimensions())
@ -118,45 +111,23 @@ impl WindowBuilder {
}
// building
let mut w = try!(platform::Window::new(&self.window, &self.platform_specific));
// a window resize callback was given
if let Some(callback) = self.window.resize_callback {
w.set_window_resize_callback(Some(callback));
}
let w = try!(platform::Window2::new(events_loop.events_loop.clone(), &self.window, &self.platform_specific));
Ok(Window { window: w })
}
/// Builds the window.
///
/// The context is build in a *strict* way. That means that if the backend couldn't give
/// you what you requested, an `Err` will be returned.
#[inline]
pub fn build_strict(self) -> Result<Window, CreationError> {
self.build()
}
}
impl Default for Window {
#[inline]
fn default() -> Window {
Window::new().unwrap()
}
}
impl Window {
/// Creates a new OpenGL context, and a Window for platforms where this is appropriate.
///
/// This function is equivalent to `WindowBuilder::new().build()`.
/// This function is equivalent to `WindowBuilder::new().build(events_loop)`.
///
/// Error should be very rare and only occur in case of permission denied, incompatible system,
/// out of memory, etc.
#[inline]
pub fn new() -> Result<Window, CreationError> {
pub fn new(events_loop: &EventsLoop) -> Result<Window, CreationError> {
let builder = WindowBuilder::new();
builder.build()
builder.build(events_loop)
}
/// Modifies the title of the window.
@ -276,24 +247,6 @@ impl Window {
self.window.set_inner_size(x, y)
}
/// Returns an iterator that poll for the next event in the window's events queue.
/// Returns `None` if there is no event in the queue.
///
/// Contrary to `wait_events`, this function never blocks.
#[inline]
pub fn poll_events(&self) -> PollEventsIterator {
PollEventsIterator(self.window.poll_events())
}
/// Returns an iterator that returns events one by one, blocking if necessary until one is
/// available.
///
/// The iterator never returns `None`.
#[inline]
pub fn wait_events(&self) -> WaitEventsIterator {
WaitEventsIterator(self.window.wait_events())
}
/// DEPRECATED. Gets the native platform specific display for this window.
/// This is typically only required when integrating with
/// other libraries that need this information.
@ -312,23 +265,6 @@ impl Window {
self.window.platform_window()
}
/// Create a window proxy for this window, that can be freely
/// passed to different threads.
#[inline]
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy {
proxy: self.window.create_window_proxy()
}
}
/// Sets a resize callback that is called by Mac (and potentially other
/// operating systems) during resize operations. This can be used to repaint
/// during window resizing.
#[inline]
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
self.window.set_window_resize_callback(callback);
}
/// Modifies the mouse cursor of the window.
/// Has no effect on Android.
pub fn set_cursor(&self, cursor: MouseCursor) {
@ -356,57 +292,10 @@ impl Window {
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
self.window.set_cursor_state(state)
}
}
/// Represents a thread safe subset of operations that can be called
/// on a window. This structure can be safely cloned and sent between
/// threads.
#[derive(Clone)]
pub struct WindowProxy {
proxy: platform::WindowProxy,
}
impl WindowProxy {
/// Triggers a blocked event loop to wake up. This is
/// typically called when another thread wants to wake
/// up the blocked rendering thread to cause a refresh.
#[inline]
pub fn wakeup_event_loop(&self) {
self.proxy.wakeup_event_loop();
}
}
/// An iterator for the `poll_events` function.
pub struct PollEventsIterator<'a>(platform::PollEventsIterator<'a>);
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
/// An iterator for the `wait_events` function.
pub struct WaitEventsIterator<'a>(platform::WaitEventsIterator<'a>);
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
#[inline]
fn next(&mut self) -> Option<Event> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
pub fn id(&self) -> WindowId {
WindowId(self.window.id())
}
}