Skip to content
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build = "build.rs"
lazy_static = "0.2.0"
libc = "0.2"
shared_library = "0.1.0"
winit = "0.6"
winit = "0.6.2"

[build-dependencies]
gl_generator = "0.5"
Expand Down Expand Up @@ -42,5 +42,5 @@ dwmapi-sys = "0.1"

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os="dragonfly", target_os="openbsd"))'.dependencies]
osmesa-sys = "0.1.0"
wayland-client = { version = "0.7.4", features = ["egl", "dlopen"] }
wayland-client = { version = "0.8.6", features = ["egl", "dlopen"] }
x11-dl = "2.4"
2 changes: 1 addition & 1 deletion examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
events_loop.run_forever(|event| {
match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::KeyboardInput(glutin::ElementState::Pressed, _, _) => {
glutin::WindowEvent::KeyboardInput(glutin::ElementState::Pressed, _, _, _) => {
println!("Setting cursor to \"{:?}\"", cursors[cursor_idx]);
window.set_cursor(cursors[cursor_idx]);
if cursor_idx < cursors.len() - 1 {
Expand Down
2 changes: 1 addition & 1 deletion examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::Closed => events_loop.interrupt(),
glutin::WindowEvent::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) =>
glutin::WindowEvent::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape), _) =>
events_loop.interrupt(),
_ => (),
},
Expand Down
2 changes: 1 addition & 1 deletion examples/grabbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
match event {
glutin::Event::WindowEvent { event, .. } => match event {

glutin::WindowEvent::KeyboardInput(glutin::ElementState::Pressed, _, _) => {
glutin::WindowEvent::KeyboardInput(glutin::ElementState::Pressed, _, _, _) => {
if grabbed {
grabbed = false;
window.set_cursor_state(glutin::CursorState::Normal)
Expand Down
108 changes: 53 additions & 55 deletions src/platform/linux/api_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,80 @@ pub struct Window {
winit_window: winit::Window,
}

enum DisplayServer {
X(x11::Window),
Wayland(wayland::Window)
}

pub enum WaitEventsIterator<'a> {
#[doc(hidden)]
X(winit::WaitEventsIterator<'a>),
#[doc(hidden)]
Wayland(wayland::WaitEventsIterator<'a>)
pub enum EventsLoop {
X(winit::EventsLoop),
Wayland(wayland::EventsLoop)
}

impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
impl EventsLoop {
/// Builds a new events loop.
pub fn new() -> EventsLoop {
let winit_events_loop = winit::EventsLoop::new();
// Ideally, winit would expose an API telling us wether we are
// in Wayland mode or X11 mode
if wayland_client::default_connect().is_ok() {
EventsLoop::Wayland(wayland::EventsLoop::new(winit_events_loop))
} else {
EventsLoop::X(winit_events_loop)
}
}

/// Fetches all the events that are pending, calls the callback function for each of them,
/// and returns.
#[inline]
fn next(&mut self) -> Option<Event> {
match self {
&mut WaitEventsIterator::X(ref mut it) => it.next(),
&mut WaitEventsIterator::Wayland(ref mut it) => it.next()
pub fn poll_events<F>(&self, callback: F)
where F: FnMut(Event)
{
match *self {
EventsLoop::X(ref evlp) => evlp.poll_events(callback),
EventsLoop::Wayland(ref evlp) => evlp.poll_events(callback)
}
}
}

pub enum PollEventsIterator<'a> {
#[doc(hidden)]
X(winit::PollEventsIterator<'a>),
#[doc(hidden)]
Wayland(wayland::PollEventsIterator<'a>)
}

impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
/// 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)
{
match *self {
EventsLoop::X(ref evlp) => evlp.run_forever(callback),
EventsLoop::Wayland(ref evlp) => evlp.run_forever(callback)
}
}

/// If we called `run_forever()`, stops the process of waiting for events.
#[inline]
fn next(&mut self) -> Option<Event> {
match self {
&mut PollEventsIterator::X(ref mut it) => it.next(),
&mut PollEventsIterator::Wayland(ref mut it) => it.next()
pub fn interrupt(&self) {
match *self {
EventsLoop::X(ref evlp) => evlp.interrupt(),
EventsLoop::Wayland(ref evlp) => evlp.interrupt()
}
}
}

enum DisplayServer {
X(x11::Window),
Wayland(wayland::Window)
}

impl Window {
#[inline]
pub fn new(
events_loop: &EventsLoop,
_: &WindowAttributes,
pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>,
_: &PlatformSpecificWindowBuilderAttributes,
winit_builder: winit::WindowBuilder,
) -> Result<Window, CreationError> {
let window = match wayland_client::default_connect() {
Ok(_) => {
let window = match *events_loop {
EventsLoop::Wayland(ref evlp) => {
let opengl = opengl.clone().map_sharing(|w| match w.display_server {
DisplayServer::Wayland(ref w) => w,
_ => panic!() // TODO: return an error
});
let (display_server, winit_window) = try!(wayland::Window::new(
evlp,
pf_reqs,
&opengl,
winit_builder,
Expand All @@ -92,12 +107,13 @@ impl Window {
winit_window: winit_window,
}
},
Err(_) => {
EventsLoop::X(ref evlp) => {
let opengl = opengl.clone().map_sharing(|w| match w.display_server {
DisplayServer::X(ref w) => w,
_ => panic!() // TODO: return an error
});
let (display_server, winit_window) = try!(x11::Window::new(
evlp,
pf_reqs,
&opengl,
winit_builder,
Expand All @@ -111,6 +127,10 @@ impl Window {
Ok(window)
}

pub fn id(&self) -> winit::WindowId {
self.winit_window.id()
}

pub fn set_title(&self, title: &str) {
self.winit_window.set_title(title)
}
Expand Down Expand Up @@ -157,20 +177,6 @@ impl Window {
}
}

pub fn poll_events(&self) -> PollEventsIterator {
match self.display_server {
DisplayServer::X(_) => PollEventsIterator::X(self.winit_window.poll_events()),
DisplayServer::Wayland(ref w) => PollEventsIterator::Wayland(w.poll_events(&self.winit_window)),
}
}

pub fn wait_events(&self) -> WaitEventsIterator {
match self.display_server {
DisplayServer::X(_) => WaitEventsIterator::X(self.winit_window.wait_events()),
DisplayServer::Wayland(ref w) => WaitEventsIterator::Wayland(w.wait_events(&self.winit_window)),
}
}

pub unsafe fn platform_display(&self) -> *mut libc::c_void {
self.winit_window.platform_display()
}
Expand All @@ -189,14 +195,6 @@ impl Window {
&mut self.winit_window
}

pub fn create_window_proxy(&self) -> winit::WindowProxy {
self.winit_window.create_window_proxy()
}

pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
self.winit_window.set_window_resize_callback(callback);
}

pub fn set_cursor(&self, cursor: winit::MouseCursor) {
self.winit_window.set_cursor(cursor);
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use PixelFormatRequirements;

use api::osmesa::{self, OsMesaContext};

pub use self::api_dispatch::{Window};
pub use self::api_dispatch::{Window, EventsLoop};
pub use self::api_dispatch::PlatformSpecificWindowBuilderAttributes;
pub use self::api_dispatch::{WaitEventsIterator, PollEventsIterator};

mod api_dispatch;
mod wayland;
mod x11;
Expand Down
109 changes: 64 additions & 45 deletions src/platform/linux/wayland.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::{Mutex, Weak, Arc};
use std::collections::HashMap;
use std::ffi::CString;
use winit;
use winit::os::unix::WindowExt;
Expand All @@ -6,56 +8,87 @@ use api::dlopen;
use api::egl;
use api::egl::Context as EglContext;
use wayland_client::egl as wegl;
use Event;
use {Event, WindowEvent};

pub struct Window {
egl_surface: wegl::WlEglSurface,
egl_surface: Arc<wegl::WlEglSurface>,
context: EglContext,
}

pub struct WaitEventsIterator<'a> {
window: &'a Window,
winit_iterator: winit::WaitEventsIterator<'a>,

pub struct EventsLoop {
winit_events_loop: winit::EventsLoop,
egl_surfaces: Mutex<HashMap<winit::WindowId, Weak<wegl::WlEglSurface>>>,
}

impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
impl EventsLoop {
/// Builds a new events loop.
pub fn new(events_loop: winit::EventsLoop) -> EventsLoop {
EventsLoop {
winit_events_loop: events_loop,
egl_surfaces: Mutex::new(HashMap::new()),
}
}

fn next(&mut self) -> Option<Event> {
let event = self.winit_iterator.next();
match event {
Some(Event::Resized(x, y)) => self.window.egl_surface.resize(x as i32, y as i32, 0, 0),
_ => {},
fn resize_surface(&self, window_id: winit::WindowId, x: u32, y: u32) {
if let Ok(egl_surfaces) = self.egl_surfaces.lock() {
if let Some(surface) = egl_surfaces[&window_id].upgrade() {
surface.resize(x as i32, y as i32, 0, 0)
}
}
event
}
}

pub struct PollEventsIterator<'a> {
window: &'a Window,
winit_iterator: winit::PollEventsIterator<'a>,
}
fn insert_window(&self,
window_id: winit::WindowId,
egl_surface: &Arc<wegl::WlEglSurface>)
{
if let Ok(mut my_surfaces) = self.egl_surfaces.lock() {
my_surfaces.insert(window_id, Arc::downgrade(egl_surface));
}
}

impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
/// Fetches all the events that are pending, calls the callback function for each of them,
/// and returns.
#[inline]
pub fn poll_events<F>(&self, mut callback: F)
where F: FnMut(Event)
{
self.winit_events_loop.poll_events(|event| {
if let Event::WindowEvent { window_id, event: WindowEvent::Resized(x, y) } = event {
self.resize_surface(window_id, x, y)
}
callback(event);
})
}

fn next(&mut self) -> Option<Event> {
let event = self.winit_iterator.next();
match event {
Some(Event::Resized(x, y)) => self.window.egl_surface.resize(x as i32, y as i32, 0, 0),
_ => {},
}
event
/// Runs forever until `interrupt()` is called. Whenever an event happens, calls the callback.
#[inline]
pub fn run_forever<F>(&self, mut callback: F)
where F: FnMut(Event)
{
self.winit_events_loop.run_forever(|event| {
if let Event::WindowEvent { window_id, event: WindowEvent::Resized(x, y) } = event {
self.resize_surface(window_id, x, y)
}
callback(event);
})
}

/// If we called `run_forever()`, stops the process of waiting for events.
#[inline]
pub fn interrupt(&self) {
self.winit_events_loop.interrupt()
}
}

impl Window {
pub fn new(
events_loop: &EventsLoop,
pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>,
winit_builder: winit::WindowBuilder,
) -> Result<(Window, winit::Window), CreationError> {
let winit_window = winit_builder.build().unwrap();
let winit_window = winit_builder.build(&events_loop.winit_events_loop).unwrap();
let wayland_window = {
let (w, h) = winit_window.get_inner_size().unwrap();
let surface = winit_window.get_wayland_client_surface().unwrap();
Expand All @@ -77,33 +110,19 @@ impl Window {
)
};
Window {
egl_surface: egl_surface,
egl_surface: Arc::new(egl_surface),
context: context,
}
};
// Store a copy of the `context`'s `IdRef` so that we can `update` it on `Resized` events.
events_loop.insert_window(winit_window.id(), &wayland_window.egl_surface);
Ok((wayland_window, winit_window))
}

pub fn set_inner_size(&self, x: u32, y: u32, winit_window: &winit::Window) {
winit_window.set_inner_size(x, y);
self.egl_surface.resize(x as i32, y as i32, 0, 0);
}

#[inline]
pub fn wait_events<'a>(&'a self, winit_window: &'a winit::Window) -> WaitEventsIterator {
WaitEventsIterator {
window: self,
winit_iterator: winit_window.wait_events()
}
}

#[inline]
pub fn poll_events<'a>(&'a self, winit_window: &'a winit::Window) -> PollEventsIterator {
PollEventsIterator {
window: self,
winit_iterator: winit_window.poll_events()
}
}
}

impl GlContext for Window {
Expand Down
Loading