aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/api/wayland/context.rs94
-rw-r--r--src/api/wayland/events.rs10
-rw-r--r--src/api/wayland/mod.rs1
-rw-r--r--src/api/wayland/window.rs153
4 files changed, 234 insertions, 24 deletions
diff --git a/src/api/wayland/context.rs b/src/api/wayland/context.rs
index 4fc743b..8e1196d 100644
--- a/src/api/wayland/context.rs
+++ b/src/api/wayland/context.rs
@@ -1,11 +1,21 @@
-use wayland_client::EventIterator;
+use Event as GlutinEvent;
+
+use std::collections::{HashMap, VecDeque};
+use std::sync::{Arc, Mutex};
+
+use libc::c_void;
+
+use wayland_client::{EventIterator, Proxy, ProxyId};
use wayland_client::wayland::get_display;
-use wayland_client::wayland::compositor::WlCompositor;
+use wayland_client::wayland::compositor::{WlCompositor, WlSurface};
+use wayland_client::wayland::output::WlOutput;
use wayland_client::wayland::seat::WlSeat;
use wayland_client::wayland::shell::WlShell;
use wayland_client::wayland::shm::WlShm;
use wayland_client::wayland::subcompositor::WlSubcompositor;
+use super::wayland_window::DecoratedSurface;
+
lazy_static! {
pub static ref WAYLAND_CONTEXT: Option<WaylandContext> = {
WaylandContext::init()
@@ -22,7 +32,9 @@ wayland_env!(InnerEnv,
pub struct WaylandContext {
inner: InnerEnv,
- iterator: EventIterator
+ iterator: Mutex<EventIterator>,
+ monitors: Vec<WlOutput>,
+ queues: Mutex<HashMap<ProxyId, Arc<Mutex<VecDeque<GlutinEvent>>>>>
}
impl WaylandContext {
@@ -32,11 +44,81 @@ impl WaylandContext {
None => return None
};
- let (inner_env, iterator) = InnerEnv::init(display);
+ let (mut inner_env, iterator) = InnerEnv::init(display);
+
+ let monitors = inner_env.globals.iter()
+ .flat_map(|&(id, _, _)| inner_env.rebind_id::<WlOutput>(id))
+ .map(|(monitor, _)| monitor)
+ .collect();
+
+ inner_env.display.sync_roundtrip().unwrap();
Some(WaylandContext {
inner: inner_env,
- iterator: iterator
+ iterator: Mutex::new(iterator),
+ monitors: monitors,
+ queues: Mutex::new(HashMap::new())
})
}
-} \ No newline at end of file
+
+ pub fn new_surface(&self) -> Option<(WlSurface, Arc<Mutex<VecDeque<GlutinEvent>>>)> {
+ self.inner.compositor.as_ref().map(|c| {
+ let s = c.0.create_surface();
+ let id = s.id();
+ let queue = {
+ let mut q = VecDeque::new();
+ q.push_back(GlutinEvent::Refresh);
+ Arc::new(Mutex::new(q))
+ };
+ self.queues.lock().unwrap().insert(id, queue.clone());
+ (s, queue)
+ })
+ }
+
+ pub fn dropped_surface(&self, id: ProxyId) {
+ self.queues.lock().unwrap().remove(&id);
+ }
+
+ pub fn decorated_from(&self, surface: &WlSurface, width: i32, height: i32) -> Option<DecoratedSurface> {
+ let inner = &self.inner;
+ match (&inner.compositor, &inner.subcompositor, &inner.shm, &inner.shell) {
+ (&Some(ref compositor), &Some(ref subcompositor), &Some(ref shm), &Some(ref shell)) => {
+ DecoratedSurface::new(
+ surface, width, height,
+ &compositor.0, &subcompositor.0, &shm.0, &shell.0,
+ self.inner.rebind::<WlSeat>().map(|(seat, _)| seat)
+ ).ok()
+ }
+ _ => None
+ }
+ }
+
+ pub fn display_ptr(&self) -> *const c_void {
+ self.inner.display.ptr() as *const _
+ }
+
+ pub fn dispatch_events(&self) {
+ self.inner.display.dispatch_pending().unwrap();
+ let mut iterator = self.iterator.lock().unwrap();
+ let queues = self.queues.lock().unwrap();
+ for evt in &mut *iterator {
+ if let Some((evt, id)) = super::events::translate_event(evt) {
+ if let Some(q) = queues.get(&id) {
+ q.lock().unwrap().push_back(evt);
+ }
+ }
+ }
+ }
+
+ pub fn flush_events(&self) -> ::std::io::Result<i32> {
+ self.inner.display.flush()
+ }
+
+ pub fn read_events(&self) -> ::std::io::Result<Option<i32>> {
+ let guard = match self.inner.display.prepare_read() {
+ Some(g) => g,
+ None => return Ok(None)
+ };
+ return guard.read_events().map(|i| Some(i));
+ }
+}
diff --git a/src/api/wayland/events.rs b/src/api/wayland/events.rs
new file mode 100644
index 0000000..86f0575
--- /dev/null
+++ b/src/api/wayland/events.rs
@@ -0,0 +1,10 @@
+use Event as GlutinEvent;
+
+use wayland_client::Event as WaylandEvent;
+use wayland_client::ProxyId;
+
+pub fn translate_event(evt: WaylandEvent) -> Option<(GlutinEvent, ProxyId)> {
+ match evt {
+ _ => None
+ }
+} \ No newline at end of file
diff --git a/src/api/wayland/mod.rs b/src/api/wayland/mod.rs
index f30389c..f2e3b2f 100644
--- a/src/api/wayland/mod.rs
+++ b/src/api/wayland/mod.rs
@@ -7,6 +7,7 @@ extern crate wayland_kbd;
extern crate wayland_window;
mod context;
+mod events;
mod monitor;
mod window;
diff --git a/src/api/wayland/window.rs b/src/api/wayland/window.rs
index df50731..7532443 100644
--- a/src/api/wayland/window.rs
+++ b/src/api/wayland/window.rs
@@ -1,9 +1,18 @@
+use std::collections::VecDeque;
+use std::ffi::CString;
+use std::sync::{Arc, Mutex};
+
+use libc;
+
use {ContextError, CreationError, CursorState, Event, GlAttributes, GlContext,
MouseCursor, PixelFormat, PixelFormatRequirements, WindowAttributes};
-
+use api::dlopen;
+use api::egl;
use api::egl::Context as EglContext;
-use libc;
+use wayland_client::egl as wegl;
+use super::wayland_window::{DecoratedSurface, add_borders, substract_borders};
+use super::context::{WaylandContext, WAYLAND_CONTEXT};
#[derive(Clone)]
pub struct WindowProxy;
@@ -16,9 +25,36 @@ impl WindowProxy {
}
pub struct Window {
+ wayland_context: &'static WaylandContext,
+ egl_surface: wegl::WlEglSurface,
+ decorated_surface: Mutex<DecoratedSurface>,
+ evt_queue: Arc<Mutex<VecDeque<Event>>>,
+ inner_size: Mutex<(i32, i32)>,
+ resize_callback: Option<fn(u32, u32)>,
pub context: EglContext,
}
+impl Window {
+ fn next_event(&self) -> Option<Event> {
+ let mut newsize = None;
+ for (_, w, h) in &mut *self.decorated_surface.lock().unwrap() {
+ newsize = Some((w, h));
+ }
+ if let Some((w, h)) = newsize {
+ let (w, h) = substract_borders(w, h);
+ *self.inner_size.lock().unwrap() = (w, h);
+ self.decorated_surface.lock().unwrap().resize(w, h);
+ self.egl_surface.resize(w, h, 0, 0);
+ if let Some(f) = self.resize_callback {
+ f(w as u32, h as u32);
+ }
+ Some(Event::Resized(w as u32, h as u32))
+ } else {
+ self.evt_queue.lock().unwrap().pop_front()
+ }
+ }
+}
+
pub struct PollEventsIterator<'a> {
window: &'a Window,
}
@@ -27,7 +63,13 @@ impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
- unimplemented!()
+ match self.window.next_event() {
+ Some(evt) => return Some(evt),
+ None => {}
+ }
+ // the queue was empty, try a dispatch and see the result
+ self.window.wayland_context.dispatch_events();
+ return self.window.next_event();
}
}
@@ -39,7 +81,21 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
fn next(&mut self) -> Option<Event> {
- unimplemented!()
+ loop {
+ match self.window.next_event() {
+ Some(evt) => return Some(evt),
+ None => {}
+ }
+ // the queue was empty, try a dispatch & read and see the result
+ self.window.wayland_context.flush_events().expect("Connexion with the wayland compositor lost.");
+ match self.window.wayland_context.read_events() {
+ Ok(_) => {
+ // events were read or dispatch is needed, in both cases, we dispatch
+ self.window.wayland_context.dispatch_events()
+ }
+ Err(_) => panic!("Connexion with the wayland compositor lost.")
+ }
+ }
}
}
@@ -51,45 +107,97 @@ impl Window {
assert!(window.min_dimensions.is_none());
assert!(window.max_dimensions.is_none());
- unimplemented!()
+ let wayland_context = match *WAYLAND_CONTEXT {
+ Some(ref c) => c,
+ None => return Err(CreationError::NotSupported),
+ };
+
+ if !wegl::is_available() {
+ return Err(CreationError::NotSupported)
+ }
+
+ let (w, h) = window.dimensions.unwrap_or((800, 600));
+
+ let (surface, evt_queue) = match wayland_context.new_surface() {
+ Some(t) => t,
+ None => return Err(CreationError::NotSupported)
+ };
+
+ let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32);
+
+ let context = {
+ let libegl = unsafe { dlopen::dlopen(b"libEGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
+ if libegl.is_null() {
+ return Err(CreationError::NotSupported);
+ }
+ let egl = ::api::egl::ffi::egl::Egl::load_with(|sym| {
+ let sym = CString::new(sym).unwrap();
+ unsafe { dlopen::dlsym(libegl, sym.as_ptr()) }
+ });
+ try!(EglContext::new(
+ egl,
+ pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
+ egl::NativeDisplay::Wayland(Some(wayland_context.display_ptr() as *const _)))
+ .and_then(|p| p.finish(unsafe { egl_surface.egl_surfaceptr() } as *const _))
+ )
+ };
+
+ let decorated_surface = match wayland_context.decorated_from(&egl_surface, w as i32, h as i32) {
+ Some(s) => s,
+ None => return Err(CreationError::NotSupported)
+ };
+
+ Ok(Window {
+ wayland_context: wayland_context,
+ egl_surface: egl_surface,
+ decorated_surface: Mutex::new(decorated_surface),
+ evt_queue: evt_queue,
+ inner_size: Mutex::new((w as i32, h as i32)),
+ resize_callback: None,
+ context: context
+ })
}
pub fn set_title(&self, title: &str) {
- unimplemented!()
+ self.decorated_surface.lock().unwrap().set_title(title.into())
}
#[inline]
pub fn show(&self) {
- unimplemented!()
+ // TODO
}
#[inline]
pub fn hide(&self) {
- unimplemented!()
+ // TODO
}
#[inline]
pub fn get_position(&self) -> Option<(i32, i32)> {
- unimplemented!()
+ // Not possible with wayland
+ None
}
#[inline]
pub fn set_position(&self, _x: i32, _y: i32) {
- unimplemented!()
+ // Not possible with wayland
}
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
- unimplemented!()
+ let (w, h) = *self.inner_size.lock().unwrap();
+ Some((w as u32, h as u32))
}
#[inline]
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
- unimplemented!()
+ let (w, h) = *self.inner_size.lock().unwrap();
+ let (w, h) = add_borders(w, h);
+ Some((w as u32, h as u32))
}
#[inline]
pub fn set_inner_size(&self, x: u32, y: u32) {
- unimplemented!()
+ self.decorated_surface.lock().unwrap().resize(x as i32, y as i32)
}
#[inline]
@@ -113,17 +221,18 @@ impl Window {
#[inline]
pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) {
- unimplemented!()
+ self.resize_callback = callback;
}
#[inline]
pub fn set_cursor(&self, cursor: MouseCursor) {
- unimplemented!()
+ // TODO
}
#[inline]
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
- unimplemented!()
+ // TODO
+ Ok(())
}
#[inline]
@@ -132,8 +241,9 @@ impl Window {
}
#[inline]
- pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
- unimplemented!()
+ pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> {
+ // TODO: not yet possible on wayland
+ Ok(())
}
#[inline]
@@ -178,3 +288,10 @@ impl GlContext for Window {
self.context.get_pixel_format().clone()
}
}
+
+impl Drop for Window {
+ fn drop(&mut self) {
+ use wayland_client::Proxy;
+ self.wayland_context.dropped_surface((*self.egl_surface).id())
+ }
+} \ No newline at end of file