Skip to content

Commit

Permalink
Make the Wayland API multi-thread-friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
elinorbgr committed May 6, 2015
1 parent b8881a0 commit e70fc3b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 38 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ user32-sys = "0.1"
kernel32-sys = "0.1"

[target.i686-unknown-linux-gnu.dependencies]
<<<<<<< HEAD
osmesa-sys = "0.0.5"
wayland-client = "*"
x11 = "*"
Expand Down
139 changes: 102 additions & 37 deletions src/api/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use self::wayland::egl::{EGLSurface, is_egl_available};
use self::wayland::core::{Display, Registry, Compositor, Shell, ShellSurface,
Seat, Pointer, default_display, WSurface};
Seat, Pointer, default_display, WSurface, SurfaceId,
Surface};

use libc;
use api::dlopen;
Expand All @@ -16,18 +17,21 @@ use CursorState;
use MouseCursor;
use GlContext;

use std::collections::VecDeque;
use std::collections::{VecDeque, HashMap};
use std::sync::{Arc, Mutex};
use std::ffi::CString;

extern crate wayland_client as wayland;

pub struct WaylandContext {
struct WaylandContext {
pub display: Display,
pub registry: Registry,
pub compositor: Compositor,
pub shell: Shell,
pub seat: Seat
pub seat: Seat,
pub pointer: Option<Pointer<WSurface>>,
windows_event_queues: Arc<Mutex<HashMap<SurfaceId, Arc<Mutex<VecDeque<Event>>>>>>,
current_pointer_surface: Arc<Mutex<Option<SurfaceId>>>
}

impl WaylandContext {
Expand All @@ -53,21 +57,94 @@ impl WaylandContext {
};
// let the other globals get their events
display.sync_roundtrip();

let current_pointer_surface = Arc::new(Mutex::new(None));

// rustc has trouble finding the correct type here, so we explicit it.
let windows_event_queues = Arc::new(Mutex::new(
HashMap::<SurfaceId, Arc<Mutex<VecDeque<Event>>>>::new()
));

// handle inputs
let mut pointer = seat.get_pointer();
if let Some(ref mut p) = pointer {
// set the enter/leave callbacks
let current_surface = current_pointer_surface.clone();
p.set_enter_action(move |_, sid, x, y| {
*current_surface.lock().unwrap() = Some(sid);
});
let current_surface = current_pointer_surface.clone();
p.set_leave_action(move |_, sid| {
*current_surface.lock().unwrap() = None;
});
// set the events callbacks
let current_surface = current_pointer_surface.clone();
let event_queues = windows_event_queues.clone();
p.set_motion_action(move |_, _, x, y| {
// dispatch to the appropriate queue
let sid = *current_surface.lock().unwrap();
if let Some(sid) = sid {
let map = event_queues.lock().unwrap();
if let Some(queue) = map.get(&sid) {
queue.lock().unwrap().push_back(Event::Moved(x as i32,y as i32))
}
}
});
let current_surface = current_pointer_surface.clone();
let event_queues = windows_event_queues.clone();
p.set_button_action(move |_, sid, b, s| {
use self::wayland::core::ButtonState;
use MouseButton;
use ElementState;
let button = match b {
0x110 => MouseButton::Left,
0x111 => MouseButton::Right,
0x112 => MouseButton::Middle,
_ => return
};
let state = match s {
ButtonState::WL_POINTER_BUTTON_STATE_RELEASED => ElementState::Released,
ButtonState::WL_POINTER_BUTTON_STATE_PRESSED => ElementState::Pressed
};
// dispatch to the appropriate queue
let sid = *current_surface.lock().unwrap();
if let Some(sid) = sid {
let map = event_queues.lock().unwrap();
if let Some(queue) = map.get(&sid) {
queue.lock().unwrap().push_back(Event::MouseInput(state, button))
}
}
});
}
Some(WaylandContext {
display: display,
registry: registry,
compositor: compositor,
shell: shell,
seat: seat,
pointer: pointer,
windows_event_queues: windows_event_queues,
current_pointer_surface: current_pointer_surface
})
}

fn register_surface(&self, sid: SurfaceId, queue: Arc<Mutex<VecDeque<Event>>>) {
self.windows_event_queues.lock().unwrap().insert(sid, queue);
}

fn deregister_surface(&self, sid: SurfaceId) {
self.windows_event_queues.lock().unwrap().remove(&sid);
}
}

lazy_static! {
static ref WAYLAND_CONTEXT: Option<WaylandContext> = {
WaylandContext::new()
};
}

pub struct Window {
wayland_context: WaylandContext,
shell_surface: ShellSurface<EGLSurface>,
pointer: Option<Pointer<WSurface>>,
pending_events: Arc<Mutex<VecDeque<Event>>>,
pub context: EglContext,
}
Expand Down Expand Up @@ -117,7 +194,9 @@ impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;

fn next(&mut self) -> Option<Event> {
self.window.wayland_context.display.dispatch_pending();
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
ctxt.display.dispatch_pending();
}
self.window.pending_events.lock().unwrap().pop_front()
}
}
Expand All @@ -130,7 +209,9 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;

fn next(&mut self) -> Option<Event> {
self.window.wayland_context.display.dispatch();
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
ctxt.display.dispatch();
}
self.window.pending_events.lock().unwrap().pop_front()
}
}
Expand All @@ -139,8 +220,8 @@ impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
use self::wayland::internals::FFI;

let wayland_context = match WaylandContext::new() {
Some(c) => c,
let wayland_context = match *WAYLAND_CONTEXT {
Some(ref c) => c,
None => return Err(CreationError::NotSupported),
};

Expand Down Expand Up @@ -175,36 +256,12 @@ impl Window {
shell_surface.set_toplevel();
let events = Arc::new(Mutex::new(VecDeque::new()));

let mut pointer = wayland_context.seat.get_pointer();
if let Some(ref mut p) = pointer {
// set the events callbacks
let motion_events = events.clone();
p.set_motion_action(move |_, _, x, y| {
motion_events.lock().unwrap().push_back(Event::Moved(x as i32,y as i32))
});
let action_events = events.clone();
p.set_button_action(move |_, _, b, s| {
use self::wayland::core::ButtonState;
use MouseButton;
use ElementState;
let button = match b {
0x110 => MouseButton::Left,
0x111 => MouseButton::Right,
0x112 => MouseButton::Middle,
_ => return
};
let state = match s {
ButtonState::WL_POINTER_BUTTON_STATE_RELEASED => ElementState::Released,
ButtonState::WL_POINTER_BUTTON_STATE_PRESSED => ElementState::Pressed
};
action_events.lock().unwrap().push_back(Event::MouseInput(state, button));
});
}
wayland_context.register_surface(shell_surface.get_wsurface().get_id(), events.clone());

wayland_context.display.flush();

Ok(Window {
wayland_context: wayland_context,
shell_surface: shell_surface,
pointer: pointer,
pending_events: events,
context: context
})
Expand Down Expand Up @@ -310,4 +367,12 @@ impl GlContext for Window {
fn get_pixel_format(&self) -> PixelFormat {
self.context.get_pixel_format().clone()
}
}

impl Drop for Window {
fn drop(&mut self) {
if let Some(ref ctxt) = *WAYLAND_CONTEXT {
ctxt.deregister_surface(self.shell_surface.get_wsurface().get_id())
}
}
}

0 comments on commit e70fc3b

Please sign in to comment.