Merge pull request #22 from Drakulix/refactor/egl_surface

Decouple EGLSurface from EGLContext
This commit is contained in:
Victor Berger 2017-06-20 11:47:29 +02:00 committed by GitHub
commit 3363846d84
12 changed files with 785 additions and 532 deletions

View File

@ -17,6 +17,7 @@ winit = { git = "https://github.com/tomaka/winit.git", optional = true }
glium = { version = "~0.16.0", optional = true } glium = { version = "~0.16.0", optional = true }
input = { version = "~0.2.0", optional = true } input = { version = "~0.2.0", optional = true }
clippy = { version = "*", optional = true } clippy = { version = "*", optional = true }
rental = "0.4.11"
[build-dependencies] [build-dependencies]
gl_generator = "0.5" gl_generator = "0.5"

View File

@ -12,11 +12,13 @@ fn main() {
println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=build.rs");
let mut file = File::create(&dest.join("egl_bindings.rs")).unwrap(); let mut file = File::create(&dest.join("egl_bindings.rs")).unwrap();
Registry::new(Api::Egl, Registry::new(
Api::Egl,
(1, 5), (1, 5),
Profile::Core, Profile::Core,
Fallbacks::All, Fallbacks::All,
["EGL_KHR_create_context", [
"EGL_KHR_create_context",
"EGL_EXT_create_context_robustness", "EGL_EXT_create_context_robustness",
"EGL_KHR_create_context_no_error", "EGL_KHR_create_context_no_error",
"EGL_KHR_platform_x11", "EGL_KHR_platform_x11",
@ -27,7 +29,8 @@ fn main() {
"EGL_EXT_platform_x11", "EGL_EXT_platform_x11",
"EGL_MESA_platform_gbm", "EGL_MESA_platform_gbm",
"EGL_EXT_platform_wayland", "EGL_EXT_platform_wayland",
"EGL_EXT_platform_device"]) "EGL_EXT_platform_device",
.write_bindings(gl_generator::StructGenerator, &mut file) ],
).write_bindings(gl_generator::StructGenerator, &mut file)
.unwrap(); .unwrap();
} }

View File

@ -17,9 +17,10 @@ fn main() {
// Insert the ShmGlobal as a handler to your event loop // Insert the ShmGlobal as a handler to your event loop
// Here, we specify tha the standard Argb8888 and Xrgb8888 is the only supported. // Here, we specify tha the standard Argb8888 and Xrgb8888 is the only supported.
let handler_id = let handler_id = event_loop.add_handler_with_init(ShmGlobal::new(
event_loop.add_handler_with_init(ShmGlobal::new(vec![], vec![],
None /* we don't provide a logger here */)); None, /* we don't provide a logger here */
));
// Register this handler to advertise a wl_shm global of version 1 // Register this handler to advertise a wl_shm global of version 1
let shm_global = event_loop.register_global::<wl_shm::WlShm, ShmGlobal>(handler_id, 1); let shm_global = event_loop.register_global::<wl_shm::WlShm, ShmGlobal>(handler_id, 1);

View File

@ -10,12 +10,14 @@ use super::GraphicsBackend;
use libloading::Library; use libloading::Library;
use nix::{c_int, c_void}; use nix::{c_int, c_void};
use slog;
use std::error::{self, Error}; use std::error::{self, Error};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::mem; use std::mem;
use std::ops::Deref;
use std::ptr; use std::ptr;
#[allow(non_camel_case_types, dead_code)] #[allow(non_camel_case_types, dead_code)]
@ -44,16 +46,35 @@ mod ffi {
/// Native types to create an `EGLContext` from. /// Native types to create an `EGLContext` from.
/// Currently supported providers are X11, Wayland and GBM. /// Currently supported providers are X11, Wayland and GBM.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Native { pub enum NativeDisplay {
/// X11 Display and Window objects to create an `EGLContext` upon. /// X11 Display to create an `EGLContext` upon.
X11(ffi::NativeDisplayType, ffi::NativeWindowType), X11(ffi::NativeDisplayType),
/// Wayland Display and Surface objects to create an `EGLContext` upon. /// Wayland Display to create an `EGLContext` upon.
Wayland(ffi::NativeDisplayType, ffi::NativeWindowType), Wayland(ffi::NativeDisplayType),
/// GBM Display /// GBM Display
Gbm(ffi::NativeDisplayType, ffi::NativeWindowType), Gbm(ffi::NativeDisplayType),
} }
/// Error that can happen while creating an `EGLContext` /// Native types to create an `EGLSurface` from.
/// Currently supported providers are X11, Wayland and GBM.
#[derive(Clone, Copy)]
pub enum NativeSurface {
/// X11 Window to create an `EGLSurface` upon.
X11(ffi::NativeWindowType),
/// Wayland Surface to create an `EGLSurface` upon.
Wayland(ffi::NativeWindowType),
/// GBM Surface
Gbm(ffi::NativeWindowType),
}
#[derive(Clone, Copy, PartialEq)]
enum NativeType {
X11,
Wayland,
Gbm,
}
/// Error that can happen while creating an `EGLContext` or `EGLSurface`
#[derive(Debug)] #[derive(Debug)]
pub enum CreationError { pub enum CreationError {
/// I/O error from the underlying system /// I/O error from the underlying system
@ -64,6 +85,10 @@ pub enum CreationError {
OpenGlVersionNotSupported, OpenGlVersionNotSupported,
/// There is no pixel format available that fulfills all requirements /// There is no pixel format available that fulfills all requirements
NoAvailablePixelFormat, NoAvailablePixelFormat,
/// Surface creation from an unsupport combination
///
/// E.g creating a surface from an X11 window on a context created from a wayland display
NonMatchingSurfaceType,
/// Context creation is not supported on this system /// Context creation is not supported on this system
NotSupported, NotSupported,
} }
@ -97,6 +122,7 @@ impl error::Error for CreationError {
"Couldn't find any pixel format that matches \ "Couldn't find any pixel format that matches \
the criterias." the criterias."
} }
CreationError::NonMatchingSurfaceType => "Surface type does not match the context type.",
CreationError::NotSupported => "Context creation is not supported on the current window system", CreationError::NotSupported => "Context creation is not supported on the current window system",
} }
} }
@ -115,8 +141,24 @@ pub struct EGLContext {
context: ffi::egl::types::EGLContext, context: ffi::egl::types::EGLContext,
display: ffi::egl::types::EGLDisplay, display: ffi::egl::types::EGLDisplay,
egl: ffi::egl::Egl, egl: ffi::egl::Egl,
surface: ffi::egl::types::EGLSurface, config_id: ffi::egl::types::EGLConfig,
surface_attributes: Vec<c_int>,
pixel_format: PixelFormat, pixel_format: PixelFormat,
backend_type: NativeType,
logger: slog::Logger,
}
/// EGL surface of a given egl context for rendering
pub struct EGLSurface<'a> {
context: &'a EGLContext,
surface: ffi::egl::types::EGLSurface,
}
impl<'a> Deref for EGLSurface<'a> {
type Target = EGLContext;
fn deref(&self) -> &Self::Target {
self.context
}
} }
impl EGLContext { impl EGLContext {
@ -124,12 +166,13 @@ impl EGLContext {
/// ///
/// # Unsafety /// # Unsafety
/// ///
/// This method is marked unsafe, because the contents of `Native` cannot be verified and msy /// This method is marked unsafe, because the contents of `NativeDisplay` cannot be verified and may
/// contain dangeling pointers are similar unsafe content /// contain dangling pointers or similar unsafe content
pub unsafe fn new<L>(native: Native, mut attributes: GlAttributes, reqs: PixelFormatRequirements, pub unsafe fn new<L>(native: NativeDisplay, mut attributes: GlAttributes, reqs: PixelFormatRequirements,
logger: L) logger: L)
-> Result<EGLContext, CreationError> -> Result<EGLContext, CreationError>
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
let logger = logger.into(); let logger = logger.into();
let log = ::slog_or_stdlog(logger.clone()).new(o!("smithay_module" => "renderer_egl")); let log = ::slog_or_stdlog(logger.clone()).new(o!("smithay_module" => "renderer_egl"));
@ -154,14 +197,18 @@ impl EGLContext {
} }
} }
Some((1, _)) => { Some((1, _)) => {
error!(log, error!(
"OpenGLES 1.* is not supported by the EGL renderer backend"); log,
"OpenGLES 1.* is not supported by the EGL renderer backend"
);
return Err(CreationError::OpenGlVersionNotSupported); return Err(CreationError::OpenGlVersionNotSupported);
} }
Some(version) => { Some(version) => {
error!(log, error!(
log,
"OpenGLES {:?} is unknown and not supported by the EGL renderer backend", "OpenGLES {:?} is unknown and not supported by the EGL renderer backend",
version); version
);
return Err(CreationError::OpenGlVersionNotSupported); return Err(CreationError::OpenGlVersionNotSupported);
} }
}; };
@ -196,51 +243,59 @@ impl EGLContext {
let has_dp_extension = |e: &str| dp_extensions.iter().any(|s| s == e); let has_dp_extension = |e: &str| dp_extensions.iter().any(|s| s == e);
let display = match native { let display = match native {
Native::X11(display, _) if has_dp_extension("EGL_KHR_platform_x11") && NativeDisplay::X11(display)
egl.GetPlatformDisplay.is_loaded() => { if has_dp_extension("EGL_KHR_platform_x11") && egl.GetPlatformDisplay.is_loaded() => {
trace!(log, "EGL Display Initialization via EGL_KHR_platform_x11"); trace!(log, "EGL Display Initialization via EGL_KHR_platform_x11");
egl.GetPlatformDisplay(ffi::egl::PLATFORM_X11_KHR, display as *mut _, ptr::null()) egl.GetPlatformDisplay(ffi::egl::PLATFORM_X11_KHR, display as *mut _, ptr::null())
} }
Native::X11(display, _) if has_dp_extension("EGL_EXT_platform_x11") && NativeDisplay::X11(display)
egl.GetPlatformDisplayEXT.is_loaded() => { if has_dp_extension("EGL_EXT_platform_x11") && egl.GetPlatformDisplayEXT.is_loaded() => {
trace!(log, "EGL Display Initialization via EGL_EXT_platform_x11"); trace!(log, "EGL Display Initialization via EGL_EXT_platform_x11");
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_X11_EXT, display as *mut _, ptr::null()) egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_X11_EXT, display as *mut _, ptr::null())
} }
Native::Gbm(display, _) if has_dp_extension("EGL_KHR_platform_gbm") && NativeDisplay::Gbm(display)
egl.GetPlatformDisplay.is_loaded() => { if has_dp_extension("EGL_KHR_platform_gbm") && egl.GetPlatformDisplay.is_loaded() => {
trace!(log, "EGL Display Initialization via EGL_KHR_platform_gbm"); trace!(log, "EGL Display Initialization via EGL_KHR_platform_gbm");
egl.GetPlatformDisplay(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null()) egl.GetPlatformDisplay(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null())
} }
Native::Gbm(display, _) if has_dp_extension("EGL_MESA_platform_gbm") && NativeDisplay::Gbm(display)
egl.GetPlatformDisplayEXT.is_loaded() => { if has_dp_extension("EGL_MESA_platform_gbm") && egl.GetPlatformDisplayEXT.is_loaded() => {
trace!(log, "EGL Display Initialization via EGL_MESA_platform_gbm"); trace!(log, "EGL Display Initialization via EGL_MESA_platform_gbm");
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null()) egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_GBM_KHR, display as *mut _, ptr::null())
} }
Native::Wayland(display, _) if has_dp_extension("EGL_KHR_platform_wayland") && NativeDisplay::Wayland(display)
egl.GetPlatformDisplay.is_loaded() => { if has_dp_extension("EGL_KHR_platform_wayland") && egl.GetPlatformDisplay.is_loaded() => {
trace!(log, trace!(
"EGL Display Initialization via EGL_KHR_platform_wayland"); log,
egl.GetPlatformDisplay(ffi::egl::PLATFORM_WAYLAND_KHR, "EGL Display Initialization via EGL_KHR_platform_wayland"
);
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_WAYLAND_KHR,
display as *mut _, display as *mut _,
ptr::null()) ptr::null(),
)
} }
Native::Wayland(display, _) if has_dp_extension("EGL_EXT_platform_wayland") && NativeDisplay::Wayland(display)
egl.GetPlatformDisplayEXT.is_loaded() => { if has_dp_extension("EGL_EXT_platform_wayland") && egl.GetPlatformDisplayEXT.is_loaded() => {
trace!(log, trace!(
"EGL Display Initialization via EGL_EXT_platform_wayland"); log,
egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_WAYLAND_EXT, "EGL Display Initialization via EGL_EXT_platform_wayland"
);
egl.GetPlatformDisplayEXT(
ffi::egl::PLATFORM_WAYLAND_EXT,
display as *mut _, display as *mut _,
ptr::null()) ptr::null(),
)
} }
Native::X11(display, _) | NativeDisplay::X11(display) |
Native::Gbm(display, _) | NativeDisplay::Gbm(display) |
Native::Wayland(display, _) => { NativeDisplay::Wayland(display) => {
trace!(log, "Default EGL Display Initialization via GetDisplay"); trace!(log, "Default EGL Display Initialization via GetDisplay");
egl.GetDisplay(display as *mut _) egl.GetDisplay(display as *mut _)
} }
@ -266,7 +321,6 @@ impl EGLContext {
let p = CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32)); let p = CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32));
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| String::new()); let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| String::new());
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>() list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
} else { } else {
vec![] vec![]
}; };
@ -274,8 +328,10 @@ impl EGLContext {
info!(log, "EGL Extensions: {:?}", extensions); info!(log, "EGL Extensions: {:?}", extensions);
if egl_version >= (1, 2) && egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 { if egl_version >= (1, 2) && egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
error!(log, error!(
"OpenGLES not supported by the underlying EGL implementation"); log,
"OpenGLES not supported by the underlying EGL implementation"
);
return Err(CreationError::OpenGlVersionNotSupported); return Err(CreationError::OpenGlVersionNotSupported);
} }
@ -298,8 +354,10 @@ impl EGLContext {
match version { match version {
(3, _) => { (3, _) => {
if egl_version < (1, 3) { if egl_version < (1, 3) {
error!(log, error!(
"OpenglES 3.* is not supported on EGL Versions lower then 1.3"); log,
"OpenglES 3.* is not supported on EGL Versions lower then 1.3"
);
return Err(CreationError::NoAvailablePixelFormat); return Err(CreationError::NoAvailablePixelFormat);
} }
trace!(log, "Setting RENDERABLE_TYPE to OPENGL_ES3"); trace!(log, "Setting RENDERABLE_TYPE to OPENGL_ES3");
@ -311,8 +369,10 @@ impl EGLContext {
} }
(2, _) => { (2, _) => {
if egl_version < (1, 3) { if egl_version < (1, 3) {
error!(log, error!(
"OpenglES 2.* is not supported on EGL Versions lower then 1.3"); log,
"OpenglES 2.* is not supported on EGL Versions lower then 1.3"
);
return Err(CreationError::NoAvailablePixelFormat); return Err(CreationError::NoAvailablePixelFormat);
} }
trace!(log, "Setting RENDERABLE_TYPE to OPENGL_ES2"); trace!(log, "Setting RENDERABLE_TYPE to OPENGL_ES2");
@ -340,14 +400,18 @@ impl EGLContext {
trace!(log, "Setting RED_SIZE to {}", color / 3); trace!(log, "Setting RED_SIZE to {}", color / 3);
out.push(ffi::egl::RED_SIZE as c_int); out.push(ffi::egl::RED_SIZE as c_int);
out.push((color / 3) as c_int); out.push((color / 3) as c_int);
trace!(log, trace!(
log,
"Setting GREEN_SIZE to {}", "Setting GREEN_SIZE to {}",
color / 3 + if color % 3 != 0 { 1 } else { 0 }); color / 3 + if color % 3 != 0 { 1 } else { 0 }
);
out.push(ffi::egl::GREEN_SIZE as c_int); out.push(ffi::egl::GREEN_SIZE as c_int);
out.push((color / 3 + if color % 3 != 0 { 1 } else { 0 }) as c_int); out.push((color / 3 + if color % 3 != 0 { 1 } else { 0 }) as c_int);
trace!(log, trace!(
log,
"Setting BLUE_SIZE to {}", "Setting BLUE_SIZE to {}",
color / 3 + if color % 3 == 2 { 1 } else { 0 }); color / 3 + if color % 3 == 2 { 1 } else { 0 }
);
out.push(ffi::egl::BLUE_SIZE as c_int); out.push(ffi::egl::BLUE_SIZE as c_int);
out.push((color / 3 + if color % 3 == 2 { 1 } else { 0 }) as c_int); out.push((color / 3 + if color % 3 == 2 { 1 } else { 0 }) as c_int);
} }
@ -388,12 +452,17 @@ impl EGLContext {
// calling `eglChooseConfig` // calling `eglChooseConfig`
let mut config_id = mem::uninitialized(); let mut config_id = mem::uninitialized();
let mut num_configs = mem::uninitialized(); let mut num_configs = mem::uninitialized();
if egl.ChooseConfig(display, if egl.ChooseConfig(
display,
descriptor.as_ptr(), descriptor.as_ptr(),
&mut config_id, &mut config_id,
1, 1,
&mut num_configs) == 0 { &mut num_configs,
return Err(CreationError::OsError(String::from("eglChooseConfig failed"))); ) == 0
{
return Err(CreationError::OsError(
String::from("eglChooseConfig failed"),
));
} }
if num_configs == 0 { if num_configs == 0 {
error!(log, "No matching color format found"); error!(log, "No matching color format found");
@ -468,8 +537,10 @@ impl EGLContext {
if context.is_null() { if context.is_null() {
match egl.GetError() as u32 { match egl.GetError() as u32 {
ffi::egl::BAD_ATTRIBUTE => { ffi::egl::BAD_ATTRIBUTE => {
error!(log, error!(
"Context creation failed as one or more requirements could not be met. Try removing some gl attributes or pixel format requirements"); log,
"Context creation failed as one or more requirements could not be met. Try removing some gl attributes or pixel format requirements"
);
return Err(CreationError::OpenGlVersionNotSupported); return Err(CreationError::OpenGlVersionNotSupported);
} }
e => panic!("eglCreateContext failed: 0x{:x}", e), e => panic!("eglCreateContext failed: 0x{:x}", e),
@ -478,7 +549,7 @@ impl EGLContext {
debug!(log, "EGL context successfully created"); debug!(log, "EGL context successfully created");
let surface_attributes = { let surface_attributes = {
let mut out: Vec<c_int> = Vec::with_capacity(2); let mut out: Vec<c_int> = Vec::with_capacity(3);
match reqs.double_buffer { match reqs.double_buffer {
Some(true) => { Some(true) => {
@ -494,52 +565,69 @@ impl EGLContext {
None => {} None => {}
} }
out.push(ffi::egl::NONE as i32);
out out
}; };
trace!(log, "Creating EGL window surface...");
let surface = {
let surface = match native {
Native::X11(_, window) |
Native::Wayland(_, window) |
Native::Gbm(_, window) => {
egl.CreateWindowSurface(display, config_id, window, surface_attributes.as_ptr())
}
};
if surface.is_null() {
return Err(CreationError::OsError(String::from("eglCreateWindowSurface failed")));
}
surface
};
debug!(log, "EGL window surface successfully created");
info!(log, "EGL context created"); info!(log, "EGL context created");
Ok(EGLContext { Ok(EGLContext {
_lib: lib, _lib: lib,
context: context as *const _, context: context as *const _,
display: display as *const _, display: display as *const _,
egl: egl, egl: egl,
surface: surface as *const _, config_id: config_id,
surface_attributes: surface_attributes,
pixel_format: desc, pixel_format: desc,
backend_type: match native {
NativeDisplay::X11(_) => NativeType::X11,
NativeDisplay::Wayland(_) => NativeType::Wayland,
NativeDisplay::Gbm(_) => NativeType::Gbm,
},
logger: log,
}) })
} }
/// Swaps buffers at the end of a frame. /// Creates a surface bound to the given egl context for rendering
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> { ///
let ret = unsafe { /// # Unsafety
self.egl ///
.SwapBuffers(self.display as *const _, self.surface as *const _) /// This method is marked unsafe, because the contents of `NativeSurface` cannot be verified and may
/// contain dangling pointers or similar unsafe content
pub unsafe fn create_surface<'a>(&'a self, native: NativeSurface)
-> Result<EGLSurface<'a>, CreationError> {
trace!(self.logger, "Creating EGL window surface...");
let surface = {
let surface = match (native, self.backend_type) {
(NativeSurface::X11(window), NativeType::X11) |
(NativeSurface::Wayland(window), NativeType::Wayland) |
(NativeSurface::Gbm(window), NativeType::Gbm) => {
self.egl.CreateWindowSurface(
self.display,
self.config_id,
window,
self.surface_attributes.as_ptr(),
)
}
_ => return Err(CreationError::NonMatchingSurfaceType),
}; };
if ret == 0 { if surface.is_null() {
match unsafe { self.egl.GetError() } as u32 { return Err(CreationError::OsError(
ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost), String::from("eglCreateWindowSurface failed"),
err => panic!("eglSwapBuffers failed (eglGetError returned 0x{:x})", err), ));
}
} else {
Ok(())
} }
surface
};
debug!(self.logger, "EGL window surface successfully created");
Ok(EGLSurface {
context: &self,
surface: surface,
})
} }
/// Returns the address of an OpenGL function. /// Returns the address of an OpenGL function.
@ -556,6 +644,32 @@ impl EGLContext {
unsafe { self.egl.GetCurrentContext() == self.context as *const _ } unsafe { self.egl.GetCurrentContext() == self.context as *const _ }
} }
/// Returns the pixel format of the main framebuffer of the context.
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format
}
}
impl<'a> EGLSurface<'a> {
/// Swaps buffers at the end of a frame.
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
let ret = unsafe {
self.context.egl.SwapBuffers(
self.context.display as *const _,
self.surface as *const _,
)
};
if ret == 0 {
match unsafe { self.context.egl.GetError() } as u32 {
ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost),
err => panic!("eglSwapBuffers failed (eglGetError returned 0x{:x})", err),
}
} else {
Ok(())
}
}
/// Makes the OpenGL context the current context in the current thread. /// Makes the OpenGL context the current context in the current thread.
/// ///
/// # Unsafety /// # Unsafety
@ -563,14 +677,15 @@ impl EGLContext {
/// This function is marked unsafe, because the context cannot be made current /// This function is marked unsafe, because the context cannot be made current
/// on multiple threads. /// on multiple threads.
pub unsafe fn make_current(&self) -> Result<(), SwapBuffersError> { pub unsafe fn make_current(&self) -> Result<(), SwapBuffersError> {
let ret = self.egl let ret = self.context.egl.MakeCurrent(
.MakeCurrent(self.display as *const _, self.context.display as *const _,
self.surface as *const _, self.surface as *const _,
self.surface as *const _, self.surface as *const _,
self.context as *const _); self.context.context as *const _,
);
if ret == 0 { if ret == 0 {
match self.egl.GetError() as u32 { match self.context.egl.GetError() as u32 {
ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost), ffi::egl::CONTEXT_LOST => Err(SwapBuffersError::ContextLost),
err => panic!("eglMakeCurrent failed (eglGetError returned 0x{:x})", err), err => panic!("eglMakeCurrent failed (eglGetError returned 0x{:x})", err),
} }
@ -578,30 +693,38 @@ impl EGLContext {
Ok(()) Ok(())
} }
} }
/// Returns the pixel format of the main framebuffer of the context.
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format
}
} }
unsafe impl Send for EGLContext {} unsafe impl Send for EGLContext {}
unsafe impl Sync for EGLContext {} unsafe impl Sync for EGLContext {}
unsafe impl<'a> Send for EGLSurface<'a> {}
unsafe impl<'a> Sync for EGLSurface<'a> {}
impl Drop for EGLContext { impl Drop for EGLContext {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
// we don't call MakeCurrent(0, 0) because we are not sure that the context // we don't call MakeCurrent(0, 0) because we are not sure that the context
// is still the current one // is still the current one
self.egl self.egl.DestroyContext(
.DestroyContext(self.display as *const _, self.context as *const _); self.display as *const _,
self.egl self.context as *const _,
.DestroySurface(self.display as *const _, self.surface as *const _); );
self.egl.Terminate(self.display as *const _); self.egl.Terminate(self.display as *const _);
} }
} }
} }
impl<'a> Drop for EGLSurface<'a> {
fn drop(&mut self) {
unsafe {
self.context.egl.DestroySurface(
self.context.display as *const _,
self.surface as *const _,
);
}
}
}
/// Error that can happen when swapping buffers. /// Error that can happen when swapping buffers.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SwapBuffersError { pub enum SwapBuffersError {

View File

@ -48,8 +48,10 @@ impl<T: EGLGraphicsBackend + 'static> GliumGraphicsBackend<T> {
/// Note that destroying a `Frame` is immediate, even if vsync is enabled. /// Note that destroying a `Frame` is immediate, even if vsync is enabled.
#[inline] #[inline]
pub fn draw(&self) -> Frame { pub fn draw(&self) -> Frame {
Frame::new(self.context.clone(), Frame::new(
self.backend.get_framebuffer_dimensions()) self.context.clone(),
self.backend.get_framebuffer_dimensions(),
)
} }
} }

View File

@ -49,7 +49,8 @@ impl ::std::cmp::PartialEq for Seat {
impl ::std::hash::Hash for Seat { impl ::std::hash::Hash for Seat {
fn hash<H>(&self, state: &mut H) fn hash<H>(&self, state: &mut H)
where H: ::std::hash::Hasher where
H: ::std::hash::Hasher,
{ {
self.id.hash(state); self.id.hash(state);
} }
@ -275,7 +276,10 @@ pub trait PointerMotionAbsoluteEvent: Event {
/// Device position converted to the targets coordinate space. /// Device position converted to the targets coordinate space.
/// E.g. the focused output's resolution. /// E.g. the focused output's resolution.
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) { fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(self.x_transformed(coordinate_space.0), self.y_transformed(coordinate_space.1)) (
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
} }
/// Device x position converted to the targets coordinate space's width. /// Device x position converted to the targets coordinate space's width.
@ -336,7 +340,10 @@ pub trait TouchDownEvent: Event {
/// Touch position converted into the target coordinate space. /// Touch position converted into the target coordinate space.
/// E.g. the focused output's resolution. /// E.g. the focused output's resolution.
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) { fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(self.x_transformed(coordinate_space.0), self.y_transformed(coordinate_space.1)) (
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
} }
/// Touch event's x-coordinate in the device's native coordinate space /// Touch event's x-coordinate in the device's native coordinate space
@ -395,7 +402,10 @@ pub trait TouchMotionEvent: Event {
/// Touch position converted into the target coordinate space. /// Touch position converted into the target coordinate space.
/// E.g. the focused output's resolution. /// E.g. the focused output's resolution.
fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) { fn position_transformed(&self, coordinate_space: (u32, u32)) -> (u32, u32) {
(self.x_transformed(coordinate_space.0), self.y_transformed(coordinate_space.1)) (
self.x_transformed(coordinate_space.0),
self.y_transformed(coordinate_space.1),
)
} }
/// Touch event's x-coordinate in the device's native coordinate space /// Touch event's x-coordinate in the device's native coordinate space

View File

@ -26,7 +26,8 @@ impl LibinputInputBackend {
/// Initialize a new `LibinputInputBackend` from a given already initialized libinput /// Initialize a new `LibinputInputBackend` from a given already initialized libinput
/// context. /// context.
pub fn new<L>(context: libinput::Libinput, logger: L) -> Self pub fn new<L>(context: libinput::Libinput, logger: L) -> Self
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "backend_libinput")); let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "backend_libinput"));
info!(log, "Initializing a libinput backend"); info!(log, "Initializing a libinput backend");
@ -270,9 +271,9 @@ impl backend::InputBackend for LibinputInputBackend {
} }
fn get_handler(&mut self) -> Option<&mut backend::InputHandler<Self>> { fn get_handler(&mut self) -> Option<&mut backend::InputHandler<Self>> {
self.handler self.handler.as_mut().map(|handler| {
.as_mut() handler as &mut backend::InputHandler<Self>
.map(|handler| handler as &mut backend::InputHandler<Self>) })
} }
fn clear_handler(&mut self) { fn clear_handler(&mut self) {
@ -328,8 +329,8 @@ impl backend::InputBackend for LibinputInputBackend {
Entry::Vacant(seat_entry) => { Entry::Vacant(seat_entry) => {
let mut hasher = DefaultHasher::default(); let mut hasher = DefaultHasher::default();
seat_entry.key().hash(&mut hasher); seat_entry.key().hash(&mut hasher);
let seat = seat_entry let seat =
.insert(backend::Seat::new(hasher.finish(), new_caps)); seat_entry.insert(backend::Seat::new(hasher.finish(), new_caps));
if let Some(ref mut handler) = self.handler { if let Some(ref mut handler) = self.handler {
trace!(self.logger, "Calling on_seat_created with {:?}", seat); trace!(self.logger, "Calling on_seat_created with {:?}", seat);
handler.on_seat_created(seat); handler.on_seat_created(seat);
@ -394,18 +395,20 @@ impl backend::InputBackend for LibinputInputBackend {
use input::event::touch::*; use input::event::touch::*;
if let Some(ref mut handler) = self.handler { if let Some(ref mut handler) = self.handler {
let device_seat = touch_event.device().seat(); let device_seat = touch_event.device().seat();
let seat = &self.seats let seat = &self.seats.get(&device_seat).expect(
.get(&device_seat) "Recieved touch event of non existing Seat",
.expect("Recieved touch event of non existing Seat"); );
match touch_event { match touch_event {
TouchEvent::Down(down_event) => { TouchEvent::Down(down_event) => {
trace!(self.logger, "Calling on_touch_down with {:?}", down_event); trace!(self.logger, "Calling on_touch_down with {:?}", down_event);
handler.on_touch_down(seat, down_event) handler.on_touch_down(seat, down_event)
} }
TouchEvent::Motion(motion_event) => { TouchEvent::Motion(motion_event) => {
trace!(self.logger, trace!(
self.logger,
"Calling on_touch_motion with {:?}", "Calling on_touch_motion with {:?}",
motion_event); motion_event
);
handler.on_touch_motion(seat, motion_event) handler.on_touch_motion(seat, motion_event)
} }
TouchEvent::Up(up_event) => { TouchEvent::Up(up_event) => {
@ -413,9 +416,11 @@ impl backend::InputBackend for LibinputInputBackend {
handler.on_touch_up(seat, up_event) handler.on_touch_up(seat, up_event)
} }
TouchEvent::Cancel(cancel_event) => { TouchEvent::Cancel(cancel_event) => {
trace!(self.logger, trace!(
self.logger,
"Calling on_touch_cancel with {:?}", "Calling on_touch_cancel with {:?}",
cancel_event); cancel_event
);
handler.on_touch_cancel(seat, cancel_event) handler.on_touch_cancel(seat, cancel_event)
} }
TouchEvent::Frame(frame_event) => { TouchEvent::Frame(frame_event) => {
@ -431,9 +436,9 @@ impl backend::InputBackend for LibinputInputBackend {
KeyboardEvent::Key(key_event) => { KeyboardEvent::Key(key_event) => {
if let Some(ref mut handler) = self.handler { if let Some(ref mut handler) = self.handler {
let device_seat = key_event.device().seat(); let device_seat = key_event.device().seat();
let seat = &self.seats let seat = &self.seats.get(&device_seat).expect(
.get(&device_seat) "Recieved key event of non existing Seat",
.expect("Recieved key event of non existing Seat"); );
trace!(self.logger, "Calling on_keyboard_key with {:?}", key_event); trace!(self.logger, "Calling on_keyboard_key with {:?}", key_event);
handler.on_keyboard_key(seat, key_event); handler.on_keyboard_key(seat, key_event);
} }
@ -444,49 +449,63 @@ impl backend::InputBackend for LibinputInputBackend {
use input::event::pointer::*; use input::event::pointer::*;
if let Some(ref mut handler) = self.handler { if let Some(ref mut handler) = self.handler {
let device_seat = pointer_event.device().seat(); let device_seat = pointer_event.device().seat();
let seat = &self.seats let seat = &self.seats.get(&device_seat).expect(
.get(&device_seat) "Recieved pointer event of non existing Seat",
.expect("Recieved pointer event of non existing Seat"); );
match pointer_event { match pointer_event {
PointerEvent::Motion(motion_event) => { PointerEvent::Motion(motion_event) => {
trace!(self.logger, trace!(
self.logger,
"Calling on_pointer_move with {:?}", "Calling on_pointer_move with {:?}",
motion_event); motion_event
);
handler.on_pointer_move(seat, motion_event); handler.on_pointer_move(seat, motion_event);
} }
PointerEvent::MotionAbsolute(motion_abs_event) => { PointerEvent::MotionAbsolute(motion_abs_event) => {
trace!(self.logger, trace!(
self.logger,
"Calling on_pointer_move_absolute with {:?}", "Calling on_pointer_move_absolute with {:?}",
motion_abs_event); motion_abs_event
);
handler.on_pointer_move_absolute(seat, motion_abs_event); handler.on_pointer_move_absolute(seat, motion_abs_event);
} }
PointerEvent::Axis(axis_event) => { PointerEvent::Axis(axis_event) => {
let rc_axis_event = Rc::new(axis_event); let rc_axis_event = Rc::new(axis_event);
if rc_axis_event.has_axis(Axis::Vertical) { if rc_axis_event.has_axis(Axis::Vertical) {
trace!(self.logger, trace!(
self.logger,
"Calling on_pointer_axis for Axis::Vertical with {:?}", "Calling on_pointer_axis for Axis::Vertical with {:?}",
*rc_axis_event); *rc_axis_event
handler.on_pointer_axis(seat, );
handler.on_pointer_axis(
seat,
self::PointerAxisEvent { self::PointerAxisEvent {
axis: Axis::Vertical, axis: Axis::Vertical,
event: rc_axis_event.clone(), event: rc_axis_event.clone(),
}); },
);
} }
if rc_axis_event.has_axis(Axis::Horizontal) { if rc_axis_event.has_axis(Axis::Horizontal) {
trace!(self.logger, trace!(
self.logger,
"Calling on_pointer_axis for Axis::Horizontal with {:?}", "Calling on_pointer_axis for Axis::Horizontal with {:?}",
*rc_axis_event); *rc_axis_event
handler.on_pointer_axis(seat, );
handler.on_pointer_axis(
seat,
self::PointerAxisEvent { self::PointerAxisEvent {
axis: Axis::Horizontal, axis: Axis::Horizontal,
event: rc_axis_event.clone(), event: rc_axis_event.clone(),
}); },
);
} }
} }
PointerEvent::Button(button_event) => { PointerEvent::Button(button_event) => {
trace!(self.logger, trace!(
self.logger,
"Calling on_pointer_button with {:?}", "Calling on_pointer_button with {:?}",
button_event); button_event
);
handler.on_pointer_button(seat, button_event); handler.on_pointer_button(seat, button_event);
} }
} }

View File

@ -2,8 +2,8 @@
use backend::{SeatInternal, TouchSlotInternal}; use backend::{SeatInternal, TouchSlotInternal};
use backend::graphics::GraphicsBackend; use backend::graphics::GraphicsBackend;
use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, Native, use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, NativeDisplay,
PixelFormat, PixelFormatRequirements, SwapBuffersError}; NativeSurface, PixelFormat, PixelFormatRequirements, SwapBuffersError};
use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState, use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState,
KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent, KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent,
PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent, PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent,
@ -20,11 +20,25 @@ use winit::{CreationError as WinitCreationError, ElementState, Event, EventsLoop
WindowBuilder, WindowEvent}; WindowBuilder, WindowEvent};
use winit::os::unix::{WindowExt, get_x11_xconnection}; use winit::os::unix::{WindowExt, get_x11_xconnection};
rental! {
mod egl {
use std::boxed::Box;
use ::backend::graphics::egl::{EGLContext, EGLSurface};
#[rental(deref_suffix)]
pub struct RentEGL {
context: Box<EGLContext>,
surface: EGLSurface<'context>,
}
}
}
/// Window with an active EGL Context created by `winit`. Implements the /// Window with an active EGL Context created by `winit`. Implements the
/// `EGLGraphicsBackend` graphics backend trait /// `EGLGraphicsBackend` graphics backend trait
pub struct WinitGraphicsBackend { pub struct WinitGraphicsBackend {
window: Rc<Window>, window: Rc<Window>,
context: EGLContext, context: egl::RentEGL,
logger: ::slog::Logger, logger: ::slog::Logger,
} }
@ -47,13 +61,16 @@ pub struct WinitInputBackend {
/// graphics backend trait and a corresponding `WinitInputBackend`, which implements /// graphics backend trait and a corresponding `WinitInputBackend`, which implements
/// the `InputBackend` trait /// the `InputBackend` trait
pub fn init<L>(logger: L) -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> pub fn init<L>(logger: L) -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError>
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
init_from_builder(WindowBuilder::new() init_from_builder(
WindowBuilder::new()
.with_dimensions(1280, 800) .with_dimensions(1280, 800)
.with_title("Smithay") .with_title("Smithay")
.with_visibility(true), .with_visibility(true),
logger) logger,
)
} }
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend` /// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
@ -61,26 +78,30 @@ pub fn init<L>(logger: L) -> Result<(WinitGraphicsBackend, WinitInputBackend), C
/// `WinitInputBackend`, which implements the `InputBackend` trait /// `WinitInputBackend`, which implements the `InputBackend` trait
pub fn init_from_builder<L>(builder: WindowBuilder, logger: L) pub fn init_from_builder<L>(builder: WindowBuilder, logger: L)
-> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError>
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
init_from_builder_with_gl_attr(builder, init_from_builder_with_gl_attr(
builder,
GlAttributes { GlAttributes {
version: None, version: None,
profile: None, profile: None,
debug: cfg!(debug_assertions), debug: cfg!(debug_assertions),
vsync: true, vsync: true,
}, },
logger) logger,
)
} }
/// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend` /// Create a new `WinitGraphicsBackend`, which implements the `EGLGraphicsBackend`
/// graphics backend trait, from a given `WindowBuilder` struct, as well as given /// graphics backend trait, from a given `WindowBuilder` struct, as well as given
/// `GlAttributes` for further customization of the rendering pipeline and a /// `GlAttributes` for further customization of the rendering pipeline and a
/// corresponding `WinitInputBackend`, which implements the `InputBackend` trait. /// corresponding `WinitInputBackend`, which implements the `InputBackend` trait.
pub fn init_from_builder_with_gl_attr<L> pub fn init_from_builder_with_gl_attr<L>(
(builder: WindowBuilder, attributes: GlAttributes, logger: L) builder: WindowBuilder, attributes: GlAttributes, logger: L)
-> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError> -> Result<(WinitGraphicsBackend, WinitInputBackend), CreationError>
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "backend_winit")); let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "backend_winit"));
info!(log, "Initializing a winit backend"); info!(log, "Initializing a winit backend");
@ -89,23 +110,36 @@ pub fn init_from_builder_with_gl_attr<L>
let window = Rc::new(builder.build(&events_loop)?); let window = Rc::new(builder.build(&events_loop)?);
debug!(log, "Window created"); debug!(log, "Window created");
let (native, surface) = if let (Some(conn), Some(window)) = let (native_display, native_surface, surface) =
(get_x11_xconnection(), window.get_xlib_window()) { if let (Some(conn), Some(window)) = (get_x11_xconnection(), window.get_xlib_window()) {
debug!(log, "Window is backed by X11"); debug!(log, "Window is backed by X11");
(Native::X11(conn.display as *const _, window), None) (
NativeDisplay::X11(conn.display as *const _),
NativeSurface::X11(window),
None,
)
} else if let (Some(display), Some(surface)) = } else if let (Some(display), Some(surface)) =
(window.get_wayland_display(), window.get_wayland_client_surface()) { (
window.get_wayland_display(),
window.get_wayland_client_surface(),
)
{
debug!(log, "Window is backed by Wayland"); debug!(log, "Window is backed by Wayland");
let (w, h) = window.get_inner_size().unwrap(); let (w, h) = window.get_inner_size().unwrap();
let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32); let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32);
(Native::Wayland(display, egl_surface.ptr() as *const _), Some(egl_surface)) (
NativeDisplay::Wayland(display),
NativeSurface::Wayland(egl_surface.ptr() as *const _),
Some(egl_surface),
)
} else { } else {
error!(log, "Window is backed by an unsupported graphics framework"); error!(log, "Window is backed by an unsupported graphics framework");
return Err(CreationError::NotSupported); return Err(CreationError::NotSupported);
}; };
let context = unsafe { let context = unsafe {
match EGLContext::new(native, match EGLContext::new(
native_display,
attributes, attributes,
PixelFormatRequirements { PixelFormatRequirements {
hardware_accelerated: Some(true), hardware_accelerated: Some(true),
@ -113,7 +147,8 @@ pub fn init_from_builder_with_gl_attr<L>
alpha_bits: Some(8), alpha_bits: Some(8),
..Default::default() ..Default::default()
}, },
log.clone()) { log.clone(),
) {
Ok(context) => context, Ok(context) => context,
Err(err) => { Err(err) => {
error!(log, "EGLContext creation failed:\n {}", err); error!(log, "EGLContext creation failed:\n {}", err);
@ -122,9 +157,15 @@ pub fn init_from_builder_with_gl_attr<L>
} }
}; };
Ok((WinitGraphicsBackend { Ok((
WinitGraphicsBackend {
window: window.clone(), window: window.clone(),
context: context, context: match egl::RentEGL::try_new(Box::new(context), move |context| unsafe {
context.create_surface(native_surface)
}) {
Ok(x) => x,
Err(::rental::TryNewError(err, _)) => return Err(err),
},
logger: log.new(o!("smithay_winit_component" => "graphics")), logger: log.new(o!("smithay_winit_component" => "graphics")),
}, },
WinitInputBackend { WinitInputBackend {
@ -133,16 +174,19 @@ pub fn init_from_builder_with_gl_attr<L>
surface: surface, surface: surface,
time_counter: 0, time_counter: 0,
key_counter: 0, key_counter: 0,
seat: Seat::new(0, seat: Seat::new(
0,
SeatCapabilities { SeatCapabilities {
pointer: true, pointer: true,
keyboard: true, keyboard: true,
touch: true, touch: true,
}), },
),
input_config: (), input_config: (),
handler: None, handler: None,
logger: log.new(o!("smithay_winit_component" => "input")), logger: log.new(o!("smithay_winit_component" => "input")),
})) },
))
} }
impl GraphicsBackend for WinitGraphicsBackend { impl GraphicsBackend for WinitGraphicsBackend {
@ -163,7 +207,7 @@ impl GraphicsBackend for WinitGraphicsBackend {
impl EGLGraphicsBackend for WinitGraphicsBackend { impl EGLGraphicsBackend for WinitGraphicsBackend {
fn swap_buffers(&self) -> Result<(), SwapBuffersError> { fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
trace!(self.logger, "Swapping buffers"); trace!(self.logger, "Swapping buffers");
self.context.swap_buffers() self.context.rent(|surface| surface.swap_buffers())
} }
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void { unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
@ -172,9 +216,9 @@ impl EGLGraphicsBackend for WinitGraphicsBackend {
} }
fn get_framebuffer_dimensions(&self) -> (u32, u32) { fn get_framebuffer_dimensions(&self) -> (u32, u32) {
self.window self.window.get_inner_size_pixels().expect(
.get_inner_size_pixels() "Window does not exist anymore",
.expect("Window does not exist anymore") )
} }
fn is_current(&self) -> bool { fn is_current(&self) -> bool {
@ -183,7 +227,7 @@ impl EGLGraphicsBackend for WinitGraphicsBackend {
unsafe fn make_current(&self) -> Result<(), SwapBuffersError> { unsafe fn make_current(&self) -> Result<(), SwapBuffersError> {
debug!(self.logger, "Setting EGL context to be the current context"); debug!(self.logger, "Setting EGL context to be the current context");
self.context.make_current() self.context.rent(|surface| surface.make_current())
} }
fn get_pixel_format(&self) -> PixelFormat { fn get_pixel_format(&self) -> PixelFormat {
@ -267,16 +311,17 @@ impl PointerMotionAbsoluteEvent for WinitMouseMovedEvent {
} }
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
cmp::min((self.x * width as f64 / cmp::min(
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as f64) as u32, (self.x * width as f64 / self.window.get_inner_size_points().unwrap_or((width, 0)).0 as f64) as u32,
0) 0,
)
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
cmp::min((self.y * height as f64 / cmp::min(
self.window.get_inner_size_points().unwrap_or((0, height)).1 as f64) as (self.y * height as f64 / self.window.get_inner_size_points().unwrap_or((0, height)).1 as f64) as u32,
u32, 0,
0) )
} }
} }
@ -369,15 +414,19 @@ impl TouchDownEvent for WinitTouchStartedEvent {
} }
fn x_transformed(&self, width: u32) -> u32 { fn x_transformed(&self, width: u32) -> u32 {
cmp::min(self.location.0 as i32 * width as i32 / cmp::min(
self.location.0 as i32 * width as i32 /
self.window.get_inner_size_points().unwrap_or((width, 0)).0 as i32, self.window.get_inner_size_points().unwrap_or((width, 0)).0 as i32,
0) as u32 0,
) as u32
} }
fn y_transformed(&self, height: u32) -> u32 { fn y_transformed(&self, height: u32) -> u32 {
cmp::min(self.location.1 as i32 * height as i32 / cmp::min(
self.location.1 as i32 * height as i32 /
self.window.get_inner_size_points().unwrap_or((0, height)).1 as i32, self.window.get_inner_size_points().unwrap_or((0, height)).1 as i32,
0) as u32 0,
) as u32
} }
} }
@ -482,16 +531,18 @@ impl InputBackend for WinitInputBackend {
} }
fn get_handler(&mut self) -> Option<&mut InputHandler<Self>> { fn get_handler(&mut self) -> Option<&mut InputHandler<Self>> {
self.handler self.handler.as_mut().map(|handler| {
.as_mut() handler as &mut InputHandler<Self>
.map(|handler| handler as &mut InputHandler<Self>) })
} }
fn clear_handler(&mut self) { fn clear_handler(&mut self) {
if let Some(mut handler) = self.handler.take() { if let Some(mut handler) = self.handler.take() {
trace!(self.logger, trace!(
self.logger,
"Calling on_seat_destroyed with {:?}", "Calling on_seat_destroyed with {:?}",
self.seat); self.seat
);
handler.on_seat_destroyed(&self.seat); handler.on_seat_destroyed(&self.seat);
} }
info!(self.logger, "Removing input handler"); info!(self.logger, "Removing input handler");
@ -531,8 +582,7 @@ impl InputBackend for WinitInputBackend {
let mut handler = self.handler.as_mut(); let mut handler = self.handler.as_mut();
let logger = &self.logger; let logger = &self.logger;
self.events_loop self.events_loop.poll_events(move |event| match event {
.poll_events(move |event| match event {
Event::WindowEvent { event, .. } => { Event::WindowEvent { event, .. } => {
match (event, handler.as_mut()) { match (event, handler.as_mut()) {
(WindowEvent::Resized(x, y), _) => { (WindowEvent::Resized(x, y), _) => {
@ -552,29 +602,32 @@ impl InputBackend for WinitInputBackend {
*key_counter = key_counter.checked_sub(1).unwrap_or(0) *key_counter = key_counter.checked_sub(1).unwrap_or(0)
} }
}; };
trace!(logger, trace!(
logger,
"Calling on_keyboard_key with {:?}", "Calling on_keyboard_key with {:?}",
(scancode, state)); (scancode, state)
handler.on_keyboard_key(seat, );
handler.on_keyboard_key(
seat,
WinitKeyboardInputEvent { WinitKeyboardInputEvent {
time: *time_counter, time: *time_counter,
key: scancode, key: scancode,
count: *key_counter, count: *key_counter,
state: state, state: state,
}) },
)
} }
(WindowEvent::MouseMoved { position: (x, y), .. }, (WindowEvent::MouseMoved { position: (x, y), .. }, Some(handler)) => {
Some(handler)) => { trace!(logger, "Calling on_pointer_move_absolute with {:?}", (x, y));
trace!(logger, handler.on_pointer_move_absolute(
"Calling on_pointer_move_absolute with {:?}", seat,
(x, y));
handler.on_pointer_move_absolute(seat,
WinitMouseMovedEvent { WinitMouseMovedEvent {
window: window.clone(), window: window.clone(),
time: *time_counter, time: *time_counter,
x: x, x: x,
y: y, y: y,
}) },
)
} }
(WindowEvent::MouseWheel { delta, .. }, Some(handler)) => { (WindowEvent::MouseWheel { delta, .. }, Some(handler)) => {
match delta { match delta {
@ -586,9 +639,11 @@ impl InputBackend for WinitInputBackend {
time: *time_counter, time: *time_counter,
delta: delta, delta: delta,
}; };
trace!(logger, trace!(
logger,
"Calling on_pointer_axis for Axis::Horizontal with {:?}", "Calling on_pointer_axis for Axis::Horizontal with {:?}",
x); x
);
handler.on_pointer_axis(seat, event); handler.on_pointer_axis(seat, event);
} }
if y != 0.0 { if y != 0.0 {
@ -597,24 +652,30 @@ impl InputBackend for WinitInputBackend {
time: *time_counter, time: *time_counter,
delta: delta, delta: delta,
}; };
trace!(logger, trace!(
logger,
"Calling on_pointer_axis for Axis::Vertical with {:?}", "Calling on_pointer_axis for Axis::Vertical with {:?}",
y); y
);
handler.on_pointer_axis(seat, event); handler.on_pointer_axis(seat, event);
} }
} }
} }
} }
(WindowEvent::MouseInput { state, button, .. }, Some(handler)) => { (WindowEvent::MouseInput { state, button, .. }, Some(handler)) => {
trace!(logger, trace!(
logger,
"Calling on_pointer_button with {:?}", "Calling on_pointer_button with {:?}",
(button, state)); (button, state)
handler.on_pointer_button(seat, );
handler.on_pointer_button(
seat,
WinitMouseInputEvent { WinitMouseInputEvent {
time: *time_counter, time: *time_counter,
button: button, button: button,
state: state, state: state,
}) },
)
} }
(WindowEvent::Touch(Touch { (WindowEvent::Touch(Touch {
phase: TouchPhase::Started, phase: TouchPhase::Started,
@ -624,13 +685,15 @@ impl InputBackend for WinitInputBackend {
}), }),
Some(handler)) => { Some(handler)) => {
trace!(logger, "Calling on_touch_down at {:?}", (x, y)); trace!(logger, "Calling on_touch_down at {:?}", (x, y));
handler.on_touch_down(seat, handler.on_touch_down(
seat,
WinitTouchStartedEvent { WinitTouchStartedEvent {
window: window.clone(), window: window.clone(),
time: *time_counter, time: *time_counter,
location: (x, y), location: (x, y),
id: id, id: id,
}) },
)
} }
(WindowEvent::Touch(Touch { (WindowEvent::Touch(Touch {
phase: TouchPhase::Moved, phase: TouchPhase::Moved,
@ -640,13 +703,15 @@ impl InputBackend for WinitInputBackend {
}), }),
Some(handler)) => { Some(handler)) => {
trace!(logger, "Calling on_touch_motion at {:?}", (x, y)); trace!(logger, "Calling on_touch_motion at {:?}", (x, y));
handler.on_touch_motion(seat, handler.on_touch_motion(
seat,
WinitTouchMovedEvent { WinitTouchMovedEvent {
window: window.clone(), window: window.clone(),
time: *time_counter, time: *time_counter,
location: (x, y), location: (x, y),
id: id, id: id,
}) },
)
} }
(WindowEvent::Touch(Touch { (WindowEvent::Touch(Touch {
phase: TouchPhase::Ended, phase: TouchPhase::Ended,
@ -656,19 +721,23 @@ impl InputBackend for WinitInputBackend {
}), }),
Some(handler)) => { Some(handler)) => {
trace!(logger, "Calling on_touch_motion at {:?}", (x, y)); trace!(logger, "Calling on_touch_motion at {:?}", (x, y));
handler.on_touch_motion(seat, handler.on_touch_motion(
seat,
WinitTouchMovedEvent { WinitTouchMovedEvent {
window: window.clone(), window: window.clone(),
time: *time_counter, time: *time_counter,
location: (x, y), location: (x, y),
id: id, id: id,
}); },
);
trace!(logger, "Calling on_touch_up"); trace!(logger, "Calling on_touch_up");
handler.on_touch_up(seat, handler.on_touch_up(
seat,
WinitTouchEndedEvent { WinitTouchEndedEvent {
time: *time_counter, time: *time_counter,
id: id, id: id,
}); },
);
} }
(WindowEvent::Touch(Touch { (WindowEvent::Touch(Touch {
phase: TouchPhase::Cancelled, phase: TouchPhase::Cancelled,
@ -677,11 +746,13 @@ impl InputBackend for WinitInputBackend {
}), }),
Some(handler)) => { Some(handler)) => {
trace!(logger, "Calling on_touch_cancel"); trace!(logger, "Calling on_touch_cancel");
handler.on_touch_cancel(seat, handler.on_touch_cancel(
seat,
WinitTouchCancelledEvent { WinitTouchCancelledEvent {
time: *time_counter, time: *time_counter,
id: id, id: id,
}) },
)
} }
(WindowEvent::Closed, _) => { (WindowEvent::Closed, _) => {
warn!(logger, "Window closed"); warn!(logger, "Window closed");

View File

@ -95,14 +95,15 @@ impl KbdInternal {
// FIXME: This is an issue with the xkbcommon-rs crate that does not reflect this // FIXME: This is an issue with the xkbcommon-rs crate that does not reflect this
// non-threadsafety properly. // non-threadsafety properly.
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS); let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = xkb::Keymap::new_from_names(&context, let keymap = xkb::Keymap::new_from_names(
&context,
&rules, &rules,
&model, &model,
&layout, &layout,
&variant, &variant,
options, options,
xkb::KEYMAP_COMPILE_NO_FLAGS) xkb::KEYMAP_COMPILE_NO_FLAGS,
.ok_or(())?; ).ok_or(())?;
let state = xkb::State::new(&keymap); let state = xkb::State::new(&keymap);
Ok(KbdInternal { Ok(KbdInternal {
focus: None, focus: None,
@ -148,8 +149,10 @@ impl KbdInternal {
fn serialize_pressed_keys(&self) -> Vec<u8> { fn serialize_pressed_keys(&self) -> Vec<u8> {
let serialized = unsafe { let serialized = unsafe {
::std::slice::from_raw_parts(self.pressed_keys.as_ptr() as *const u8, ::std::slice::from_raw_parts(
self.pressed_keys.len() * 4) self.pressed_keys.as_ptr() as *const u8,
self.pressed_keys.len() * 4,
)
}; };
serialized.into() serialized.into()
} }
@ -167,7 +170,8 @@ pub enum Error {
pub fn create_keyboard_handler<L>(rules: &str, model: &str, layout: &str, variant: &str, pub fn create_keyboard_handler<L>(rules: &str, model: &str, layout: &str, variant: &str,
options: Option<String>, logger: L) options: Option<String>, logger: L)
-> Result<KbdHandle, Error> -> Result<KbdHandle, Error>
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "xkbcommon_handler")); let log = ::slog_or_stdlog(logger).new(o!("smithay_module" => "xkbcommon_handler"));
info!(log, "Initializing a xkbcommon handler with keymap"; info!(log, "Initializing a xkbcommon handler with keymap";
@ -184,9 +188,9 @@ pub fn create_keyboard_handler<L>(rules: &str, model: &str, layout: &str, varian
// prepare a tempfile with the keymap, to send it to clients // prepare a tempfile with the keymap, to send it to clients
let mut keymap_file = tempfile().map_err(Error::IoError)?; let mut keymap_file = tempfile().map_err(Error::IoError)?;
let keymap_data = internal.keymap.get_as_string(xkb::KEYMAP_FORMAT_TEXT_V1); let keymap_data = internal.keymap.get_as_string(xkb::KEYMAP_FORMAT_TEXT_V1);
keymap_file keymap_file.write_all(keymap_data.as_bytes()).map_err(
.write_all(keymap_data.as_bytes()) Error::IoError,
.map_err(Error::IoError)?; )?;
keymap_file.flush().map_err(Error::IoError)?; keymap_file.flush().map_err(Error::IoError)?;
trace!(log, "Keymap loaded and copied to tempfile."; trace!(log, "Keymap loaded and copied to tempfile.";
@ -235,7 +239,8 @@ impl KbdHandle {
/// The module `smithay::keyboard::keysyms` exposes definitions of all possible keysyms /// The module `smithay::keyboard::keysyms` exposes definitions of all possible keysyms
/// to be compared against. This includes non-characted keysyms, such as XF86 special keys. /// to be compared against. This includes non-characted keysyms, such as XF86 special keys.
pub fn input<F>(&self, keycode: u32, state: KeyState, serial: u32, filter: F) pub fn input<F>(&self, keycode: u32, state: KeyState, serial: u32, filter: F)
where F: FnOnce(&ModifiersState, Keysym) -> bool where
F: FnOnce(&ModifiersState, Keysym) -> bool,
{ {
trace!(self.arc.logger, "Handling keystroke"; "keycode" => keycode, "state" => format_args!("{:?}", state)); trace!(self.arc.logger, "Handling keystroke"; "keycode" => keycode, "state" => format_args!("{:?}", state));
let mut guard = self.arc.internal.lock().unwrap(); let mut guard = self.arc.internal.lock().unwrap();
@ -315,8 +320,10 @@ impl KbdHandle {
/// This should be done first, before anything else is done with this keyboard. /// This should be done first, before anything else is done with this keyboard.
pub fn send_keymap(&self, kbd: &wl_keyboard::WlKeyboard) { pub fn send_keymap(&self, kbd: &wl_keyboard::WlKeyboard) {
trace!(self.arc.logger, "Sending keymap to client"); trace!(self.arc.logger, "Sending keymap to client");
kbd.keymap(wl_keyboard::KeymapFormat::XkbV1, kbd.keymap(
wl_keyboard::KeymapFormat::XkbV1,
self.arc.keymap_file.as_raw_fd(), self.arc.keymap_file.as_raw_fd(),
self.arc.keymap_len); self.arc.keymap_len,
);
} }
} }

View File

@ -13,6 +13,8 @@ extern crate wayland_server;
extern crate nix; extern crate nix;
extern crate xkbcommon; extern crate xkbcommon;
extern crate tempfile; extern crate tempfile;
#[macro_use]
extern crate rental;
#[cfg(feature = "backend_winit")] #[cfg(feature = "backend_winit")]
extern crate winit; extern crate winit;
@ -35,10 +37,11 @@ pub mod backend;
pub mod keyboard; pub mod keyboard;
fn slog_or_stdlog<L>(logger: L) -> ::slog::Logger fn slog_or_stdlog<L>(logger: L) -> ::slog::Logger
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
use slog::Drain; use slog::Drain;
logger logger.into().unwrap_or_else(|| {
.into() ::slog::Logger::root(::slog_stdlog::StdLog.fuse(), o!())
.unwrap_or_else(|| ::slog::Logger::root(::slog_stdlog::StdLog.fuse(), o!())) })
} }

View File

@ -89,7 +89,8 @@ impl ShmGlobal {
/// as they are required by the protocol. Formats given as argument /// as they are required by the protocol. Formats given as argument
/// as additionnaly advertized. /// as additionnaly advertized.
pub fn new<L>(mut formats: Vec<wl_shm::Format>, logger: L) -> ShmGlobal pub fn new<L>(mut formats: Vec<wl_shm::Format>, logger: L) -> ShmGlobal
where L: Into<Option<::slog::Logger>> where
L: Into<Option<::slog::Logger>>,
{ {
let log = ::slog_or_stdlog(logger); let log = ::slog_or_stdlog(logger);
@ -147,7 +148,8 @@ impl ShmGlobalToken {
/// If the buffer is not managed by the associated ShmGlobal, the closure is not called /// If the buffer is not managed by the associated ShmGlobal, the closure is not called
/// and this method will return `Err(())` (this will be the case for an EGL buffer for example). /// and this method will return `Err(())` (this will be the case for an EGL buffer for example).
pub fn with_buffer_contents<F>(&self, buffer: &wl_buffer::WlBuffer, f: F) -> Result<(), BufferAccessError> pub fn with_buffer_contents<F>(&self, buffer: &wl_buffer::WlBuffer, f: F) -> Result<(), BufferAccessError>
where F: FnOnce(&[u8], BufferData) where
F: FnOnce(&[u8], BufferData),
{ {
if !resource_is_registered::<_, ShmHandler>(buffer, self.hid) { if !resource_is_registered::<_, ShmHandler>(buffer, self.hid) {
return Err(BufferAccessError::NotManaged); return Err(BufferAccessError::NotManaged);
@ -156,7 +158,8 @@ impl ShmGlobalToken {
if data.pool if data.pool
.with_data_slice(|slice| f(slice, data.data)) .with_data_slice(|slice| f(slice, data.data))
.is_err() { .is_err()
{
// SIGBUS error occured // SIGBUS error occured
buffer.post_error(wl_shm::Error::InvalidFd as u32, "Bad pool size.".into()); buffer.post_error(wl_shm::Error::InvalidFd as u32, "Bad pool size.".into());
return Err(BufferAccessError::BadMap); return Err(BufferAccessError::BadMap);
@ -205,8 +208,10 @@ impl wl_shm::Handler for ShmHandler {
fn create_pool(&mut self, evqh: &mut EventLoopHandle, _client: &Client, shm: &wl_shm::WlShm, fn create_pool(&mut self, evqh: &mut EventLoopHandle, _client: &Client, shm: &wl_shm::WlShm,
pool: wl_shm_pool::WlShmPool, fd: RawFd, size: i32) { pool: wl_shm_pool::WlShmPool, fd: RawFd, size: i32) {
if size <= 0 { if size <= 0 {
shm.post_error(wl_shm::Error::InvalidFd as u32, shm.post_error(
"Invalid size for a new wl_shm_pool.".into()); wl_shm::Error::InvalidFd as u32,
"Invalid size for a new wl_shm_pool.".into(),
);
return; return;
} }
let mmap_pool = match Pool::new(fd, size as usize, self.log.clone()) { let mmap_pool = match Pool::new(fd, size as usize, self.log.clone()) {
@ -280,8 +285,10 @@ impl wl_shm_pool::Handler for ShmHandler {
match arc_pool.resize(size) { match arc_pool.resize(size) {
Ok(()) => {} Ok(()) => {}
Err(ResizeError::InvalidSize) => { Err(ResizeError::InvalidSize) => {
pool.post_error(wl_shm::Error::InvalidFd as u32, pool.post_error(
"Invalid new size for a wl_shm_pool.".into()); wl_shm::Error::InvalidFd as u32,
"Invalid new size for a wl_shm_pool.".into(),
);
} }
Err(ResizeError::MremapFailed) => { Err(ResizeError::MremapFailed) => {
pool.post_error(wl_shm::Error::InvalidFd as u32, "mremap failed.".into()); pool.post_error(wl_shm::Error::InvalidFd as u32, "mremap failed.".into());

View File

@ -160,12 +160,14 @@ impl Drop for MemMap {
// mman::mmap should really be unsafe... why isn't it? // mman::mmap should really be unsafe... why isn't it?
unsafe fn map(fd: RawFd, size: usize) -> Result<*mut u8, ()> { unsafe fn map(fd: RawFd, size: usize) -> Result<*mut u8, ()> {
let ret = mman::mmap(ptr::null_mut(), let ret = mman::mmap(
ptr::null_mut(),
size, size,
mman::PROT_READ, mman::PROT_READ,
mman::MAP_SHARED, mman::MAP_SHARED,
fd, fd,
0); 0,
);
ret.map(|p| p as *mut u8).map_err(|_| ()) ret.map(|p| p as *mut u8).map_err(|_| ())
} }
@ -176,20 +178,24 @@ unsafe fn unmap(ptr: *mut u8, size: usize) -> Result<(), ()> {
} }
unsafe fn nullify_map(ptr: *mut u8, size: usize) -> Result<(), ()> { unsafe fn nullify_map(ptr: *mut u8, size: usize) -> Result<(), ()> {
let ret = mman::mmap(ptr as *mut _, let ret = mman::mmap(
ptr as *mut _,
size, size,
mman::PROT_READ, mman::PROT_READ,
mman::MAP_ANONYMOUS | mman::MAP_PRIVATE | mman::MAP_FIXED, mman::MAP_ANONYMOUS | mman::MAP_PRIVATE | mman::MAP_FIXED,
-1, -1,
0); 0,
);
ret.map(|_| ()).map_err(|_| ()) ret.map(|_| ()).map_err(|_| ())
} }
unsafe fn place_sigbus_handler() { unsafe fn place_sigbus_handler() {
// create our sigbus handler // create our sigbus handler
let action = SigAction::new(SigHandler::SigAction(sigbus_handler), let action = SigAction::new(
SigHandler::SigAction(sigbus_handler),
signal::SA_NODEFER, signal::SA_NODEFER,
signal::SigSet::empty()); signal::SigSet::empty(),
);
match signal::sigaction(Signal::SIGBUS, &action) { match signal::sigaction(Signal::SIGBUS, &action) {
Ok(old_signal) => { Ok(old_signal) => {
OLD_SIGBUS_HANDLER = Box::into_raw(Box::new(old_signal)); OLD_SIGBUS_HANDLER = Box::into_raw(Box::new(old_signal));