Add subhandler for wl_surface.{commit,frame}

This commit is contained in:
Victor Berger 2017-06-04 22:12:22 +02:00
parent 8809f182b5
commit 1e960af5f2
4 changed files with 129 additions and 87 deletions

View File

@ -1,20 +1,22 @@
use super::CompositorHandler;
use super::{CompositorHandler, Handler as UserHandler};
use wayland_server::{Client, EventLoopHandle, GlobalHandler, Init};
use wayland_server::{Client, EventLoopHandle, GlobalHandler};
use wayland_server::protocol::{wl_compositor, wl_subcompositor};
impl<U: Default> GlobalHandler<wl_compositor::WlCompositor> for CompositorHandler<U>
where U: Send + Sync + 'static
impl<U: Default, H: UserHandler> GlobalHandler<wl_compositor::WlCompositor> for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
{
fn bind(&mut self, evlh: &mut EventLoopHandle, _: &Client, global: wl_compositor::WlCompositor) {
evlh.register::<_, CompositorHandler<U>>(&global, self.my_id);
evlh.register::<_, CompositorHandler<U, H>>(&global, self.my_id);
}
}
impl<U> GlobalHandler<wl_subcompositor::WlSubcompositor> for CompositorHandler<U>
where U: Send + Sync + 'static
impl<U, H> GlobalHandler<wl_subcompositor::WlSubcompositor> for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
{
fn bind(&mut self, evlh: &mut EventLoopHandle, _: &Client, global: wl_subcompositor::WlSubcompositor) {
evlh.register::<_, CompositorHandler<U>>(&global, self.my_id);
evlh.register::<_, CompositorHandler<U, H>>(&global, self.my_id);
}
}

View File

@ -1,8 +1,8 @@
use super::{Rectangle, RectangleKind, SubsurfaceAttributes, Damage, CompositorHandler};
use super::{CompositorHandler, Damage, Handler as UserHandler, Rectangle, RectangleKind,
SubsurfaceAttributes};
use super::region::RegionData;
use super::tree::SurfaceData;
use super::CompositorToken;
use wayland_server::{Client, Destroy, EventLoopHandle, Init, Resource};
use wayland_server::{Client, Destroy, EventLoopHandle, Resource};
use wayland_server::protocol::{wl_buffer, wl_callback, wl_compositor, wl_output, wl_region,
wl_subcompositor, wl_subsurface, wl_surface};
@ -14,26 +14,31 @@ struct CompositorDestructor<U> {
* wl_compositor
*/
impl<U: Default + Send + 'static> wl_compositor::Handler for CompositorHandler<U> {
fn create_surface(&mut self, evqh: &mut EventLoopHandle, _: &Client,
_: &wl_compositor::WlCompositor, id: wl_surface::WlSurface) {
impl<U, H> wl_compositor::Handler for CompositorHandler<U, H>
where U: Default + Send + 'static,
H: UserHandler + Send + 'static
{
fn create_surface(&mut self, evqh: &mut EventLoopHandle, _: &Client, _: &wl_compositor::WlCompositor,
id: wl_surface::WlSurface) {
unsafe { SurfaceData::<U>::init(&id) };
evqh.register_with_destructor::<_, CompositorHandler<U>, CompositorDestructor<U>>(&id, self.my_id);
evqh.register_with_destructor::<_, CompositorHandler<U, H>, CompositorDestructor<U>>(&id, self.my_id);
}
fn create_region(&mut self, evqh: &mut EventLoopHandle, _: &Client,
_: &wl_compositor::WlCompositor, id: wl_region::WlRegion) {
fn create_region(&mut self, evqh: &mut EventLoopHandle, _: &Client, _: &wl_compositor::WlCompositor,
id: wl_region::WlRegion) {
unsafe { RegionData::init(&id) };
evqh.register_with_destructor::<_, CompositorHandler<U>, CompositorDestructor<U>>(&id, self.my_id);
evqh.register_with_destructor::<_, CompositorHandler<U, H>, CompositorDestructor<U>>(&id, self.my_id);
}
}
unsafe impl<U: Default + Send + 'static> ::wayland_server::Handler<wl_compositor::WlCompositor>
for CompositorHandler<U> {
unsafe impl<U, H> ::wayland_server::Handler<wl_compositor::WlCompositor> for CompositorHandler<U, H>
where U: Default + Send + 'static,
H: UserHandler + Send + 'static
{
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_compositor::WlCompositor, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
-> Result<(), ()> {
<CompositorHandler<U> as ::wayland_server::protocol::wl_compositor::Handler>::__message(self, evq, client, resource, opcode, args)
<CompositorHandler<U, H> as ::wayland_server::protocol::wl_compositor::Handler>::__message(self, evq, client, resource, opcode, args)
}
}
@ -41,7 +46,7 @@ unsafe impl<U: Default + Send + 'static> ::wayland_server::Handler<wl_compositor
* wl_surface
*/
impl<U> wl_surface::Handler for CompositorHandler<U> {
impl<U, H: UserHandler> wl_surface::Handler for CompositorHandler<U, H> {
fn attach(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
buffer: Option<&wl_buffer::WlBuffer>, x: i32, y: i32) {
unsafe {
@ -52,13 +57,19 @@ impl<U> wl_surface::Handler for CompositorHandler<U> {
fn damage(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface, x: i32,
y: i32, width: i32, height: i32) {
unsafe {
SurfaceData::<U>::with_data(surface,
|d| d.damage = Damage::Surface(Rectangle { x, y, width, height }));
SurfaceData::<U>::with_data(surface, |d| {
d.damage = Damage::Surface(Rectangle {
x,
y,
width,
height,
})
});
}
}
fn frame(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
fn frame(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
callback: wl_callback::WlCallback) {
unimplemented!()
UserHandler::frame(&mut self.handler, evlh, client, surface, callback);
}
fn set_opaque_region(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
region: Option<&wl_region::WlRegion>) {
@ -74,8 +85,8 @@ impl<U> wl_surface::Handler for CompositorHandler<U> {
SurfaceData::<U>::with_data(surface, |d| d.input_region = attributes);
}
}
fn commit(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface) {
unimplemented!()
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface) {
UserHandler::commit(&mut self.handler, evlh, client, surface);
}
fn set_buffer_transform(&mut self, _: &mut EventLoopHandle, _: &Client,
surface: &wl_surface::WlSurface, transform: wl_output::Transform) {
@ -92,18 +103,24 @@ impl<U> wl_surface::Handler for CompositorHandler<U> {
fn damage_buffer(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
x: i32, y: i32, width: i32, height: i32) {
unsafe {
SurfaceData::<U>::with_data(surface,
|d| d.damage = Damage::Buffer(Rectangle { x, y, width, height }));
SurfaceData::<U>::with_data(surface, |d| {
d.damage = Damage::Buffer(Rectangle {
x,
y,
width,
height,
})
});
}
}
}
unsafe impl<U> ::wayland_server::Handler<wl_surface::WlSurface> for CompositorHandler<U> {
unsafe impl<U, H: UserHandler> ::wayland_server::Handler<wl_surface::WlSurface> for CompositorHandler<U, H> {
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_surface::WlSurface, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
-> Result<(), ()> {
<CompositorHandler<U> as ::wayland_server::protocol::wl_surface::Handler>::__message(self, evq, client, resource, opcode, args)
<CompositorHandler<U, H> as ::wayland_server::protocol::wl_surface::Handler>::__message(self, evq, client, resource, opcode, args)
}
}
@ -117,11 +134,12 @@ impl<U> Destroy<wl_surface::WlSurface> for CompositorDestructor<U> {
* wl_region
*/
impl<U> wl_region::Handler for CompositorHandler<U> {
impl<U, H> wl_region::Handler for CompositorHandler<U, H> {
fn add(&mut self, _: &mut EventLoopHandle, _: &Client, region: &wl_region::WlRegion, x: i32, y: i32,
width: i32, height: i32) {
unsafe {
RegionData::add_rectangle(region, RectangleKind::Add,
RegionData::add_rectangle(region,
RectangleKind::Add,
Rectangle {
x,
y,
@ -133,7 +151,8 @@ impl<U> wl_region::Handler for CompositorHandler<U> {
fn subtract(&mut self, _: &mut EventLoopHandle, _: &Client, region: &wl_region::WlRegion, x: i32,
y: i32, width: i32, height: i32) {
unsafe {
RegionData::add_rectangle(region, RectangleKind::Subtract,
RegionData::add_rectangle(region,
RectangleKind::Subtract,
Rectangle {
x,
y,
@ -144,12 +163,12 @@ impl<U> wl_region::Handler for CompositorHandler<U> {
}
}
unsafe impl<U> ::wayland_server::Handler<wl_region::WlRegion> for CompositorHandler<U> {
unsafe impl<U, H> ::wayland_server::Handler<wl_region::WlRegion> for CompositorHandler<U, H> {
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_region::WlRegion, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
-> Result<(), ()> {
<CompositorHandler<U> as ::wayland_server::protocol::wl_region::Handler>::__message(self, evq, client, resource, opcode, args)
<CompositorHandler<U, H> as ::wayland_server::protocol::wl_region::Handler>::__message(self, evq, client, resource, opcode, args)
}
}
@ -163,7 +182,10 @@ impl<U> Destroy<wl_region::WlRegion> for CompositorDestructor<U> {
* wl_subcompositor
*/
impl<U: Send + 'static> wl_subcompositor::Handler for CompositorHandler<U> {
impl<U, H> wl_subcompositor::Handler for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
{
fn get_subsurface(&mut self, evqh: &mut EventLoopHandle, _: &Client,
resource: &wl_subcompositor::WlSubcompositor, id: wl_subsurface::WlSubsurface,
surface: &wl_surface::WlSurface, parent: &wl_surface::WlSurface) {
@ -176,17 +198,19 @@ impl<U: Send + 'static> wl_subcompositor::Handler for CompositorHandler<U> {
SurfaceData::<U>::with_data(surface,
|d| d.subsurface_attributes = Some(Default::default()));
}
evqh.register_with_destructor::<_, CompositorHandler<U>, CompositorDestructor<U>>(&id, self.my_id);
evqh.register_with_destructor::<_, CompositorHandler<U, H>, CompositorDestructor<U>>(&id, self.my_id);
}
}
unsafe impl<U: Send + 'static> ::wayland_server::Handler<wl_subcompositor::WlSubcompositor>
for CompositorHandler<U> {
unsafe impl<U, H> ::wayland_server::Handler<wl_subcompositor::WlSubcompositor> for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
{
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_subcompositor::WlSubcompositor, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
-> Result<(), ()> {
<CompositorHandler<U> as ::wayland_server::protocol::wl_subcompositor::Handler>::__message(self, evq, client, resource, opcode, args)
<CompositorHandler<U, H> as ::wayland_server::protocol::wl_subcompositor::Handler>::__message(self, evq, client, resource, opcode, args)
}
}
@ -202,9 +226,9 @@ unsafe fn with_subsurface_attributes<U, F>(subsurface: &wl_subsurface::WlSubsurf
SurfaceData::<U>::with_data(surface, |d| f(d.subsurface_attributes.as_mut().unwrap()));
}
impl<U> wl_subsurface::Handler for CompositorHandler<U> {
fn set_position(&mut self, _: &mut EventLoopHandle, _: &Client,
resource: &wl_subsurface::WlSubsurface, x: i32, y: i32) {
impl<U, H> wl_subsurface::Handler for CompositorHandler<U, H> {
fn set_position(&mut self, _: &mut EventLoopHandle, _: &Client, resource: &wl_subsurface::WlSubsurface,
x: i32, y: i32) {
unsafe {
with_subsurface_attributes::<U, _>(resource, |attrs| {
attrs.x = x;
@ -212,34 +236,32 @@ impl<U> wl_subsurface::Handler for CompositorHandler<U> {
});
}
}
fn place_above(&mut self, _: &mut EventLoopHandle, _: &Client,
resource: &wl_subsurface::WlSubsurface, sibling: &wl_surface::WlSurface) {
fn place_above(&mut self, _: &mut EventLoopHandle, _: &Client, resource: &wl_subsurface::WlSubsurface,
sibling: &wl_surface::WlSurface) {
unimplemented!()
}
fn place_below(&mut self, _: &mut EventLoopHandle, _: &Client,
resource: &wl_subsurface::WlSubsurface, sibling: &wl_surface::WlSurface) {
fn place_below(&mut self, _: &mut EventLoopHandle, _: &Client, resource: &wl_subsurface::WlSubsurface,
sibling: &wl_surface::WlSurface) {
unimplemented!()
}
fn set_sync(&mut self, _: &mut EventLoopHandle, _: &Client,
resource: &wl_subsurface::WlSubsurface) {
fn set_sync(&mut self, _: &mut EventLoopHandle, _: &Client, resource: &wl_subsurface::WlSubsurface) {
unsafe {
with_subsurface_attributes::<U, _>(resource, |attrs| { attrs.sync = true; });
}
}
fn set_desync(&mut self, _: &mut EventLoopHandle, _: &Client,
resource: &wl_subsurface::WlSubsurface) {
fn set_desync(&mut self, _: &mut EventLoopHandle, _: &Client, resource: &wl_subsurface::WlSubsurface) {
unsafe {
with_subsurface_attributes::<U, _>(resource, |attrs| { attrs.sync = false; });
}
}
}
unsafe impl<U> ::wayland_server::Handler<wl_subsurface::WlSubsurface> for CompositorHandler<U> {
unsafe impl<U, H> ::wayland_server::Handler<wl_subsurface::WlSubsurface> for CompositorHandler<U, H> {
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_subsurface::WlSubsurface, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
-> Result<(), ()> {
<CompositorHandler<U> as ::wayland_server::protocol::wl_subsurface::Handler>::__message(self, evq, client, resource, opcode, args)
<CompositorHandler<U, H> as ::wayland_server::protocol::wl_subsurface::Handler>::__message(self, evq, client, resource, opcode, args)
}
}

View File

@ -5,9 +5,9 @@ mod region;
pub use self::tree::RoleStatus;
use self::tree::SurfaceData;
use wayland_server::{Client, EventLoopHandle, Init, resource_is_registered};
use wayland_server::protocol::{wl_buffer, wl_output, wl_surface};
use wayland_server::{EventLoopHandle, resource_is_registered, Init};
use wayland_server::protocol::{wl_buffer, wl_callback, wl_output, wl_surface};
/// Description of which part of a surface
/// should be considered damaged and needs to be redrawn
@ -19,7 +19,7 @@ pub enum Damage {
/// A rectangle containing the smaazed zone, in buffer coordinates
///
/// Note: Buffer scaling must be taken into consideration
Buffer(Rectangle)
Buffer(Rectangle),
}
/// Data associated with a surface, aggreged by the handlers
@ -166,12 +166,13 @@ impl Default for RegionAttributes {
/// access data associated with the wl_surface and wl_region managed
/// by the `CompositorGlobal` that provided it.
#[derive(Copy,Clone)]
pub struct CompositorToken<U> {
pub struct CompositorToken<U, H> {
hid: usize,
_data: ::std::marker::PhantomData<*mut U>,
_handler: ::std::marker::PhantomData<*mut H>,
}
impl<U: Send + 'static> CompositorToken<U> {
impl<U: Send + 'static, H: Handler + Send + 'static> CompositorToken<U, H> {
/// Access the data of a surface
///
/// The closure will be called with the contents of the data associated with this surface.
@ -181,7 +182,7 @@ impl<U: Send + 'static> CompositorToken<U> {
pub fn with_surface_data<F>(&self, surface: &wl_surface::WlSurface, f: F)
where F: FnOnce(&mut SurfaceAttributes<U>)
{
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::with_data(surface, f);
@ -198,7 +199,7 @@ impl<U: Send + 'static> CompositorToken<U> {
pub fn with_surface_tree<F>(&self, surface: &wl_surface::WlSurface, f: F) -> Result<(), ()>
where F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>) -> bool
{
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::map_tree(surface, f);
@ -213,11 +214,9 @@ impl<U: Send + 'static> CompositorToken<U> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn get_parent(&self, surface: &wl_surface::WlSurface) -> Option<wl_surface::WlSurface> {
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::get_parent(surface)
}
unsafe { SurfaceData::<U>::get_parent(surface) }
}
/// Retrieve the role status this surface
@ -225,11 +224,9 @@ impl<U: Send + 'static> CompositorToken<U> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn role_status(&self, surface: &wl_surface::WlSurface) -> RoleStatus {
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::role_status(surface)
}
unsafe { SurfaceData::<U>::role_status(surface) }
}
/// Register that this surface has a role
@ -242,11 +239,9 @@ impl<U: Send + 'static> CompositorToken<U> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn give_role(&self, surface: &wl_surface::WlSurface) -> Result<(), ()> {
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::give_role(surface)
}
unsafe { SurfaceData::<U>::give_role(surface) }
}
/// Register that this surface has no role
@ -258,43 +253,66 @@ impl<U: Send + 'static> CompositorToken<U> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn remove_role(&self, surface: &wl_surface::WlSurface) -> Result<(), ()> {
assert!(resource_is_registered::<_, CompositorHandler<U>>(surface, self.hid),
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
unsafe {
SurfaceData::<U>::remove_role(surface)
}
unsafe { SurfaceData::<U>::remove_role(surface) }
}
}
pub struct CompositorHandler<U> {
pub struct CompositorHandler<U, H> {
my_id: usize,
log: ::slog::Logger,
handler: H,
_data: ::std::marker::PhantomData<U>,
}
impl<U> Init for CompositorHandler<U> {
impl<U, H> Init for CompositorHandler<U, H> {
fn init(&mut self, _evqh: &mut EventLoopHandle, index: usize) {
self.my_id = index;
debug!(self.log, "Init finished")
}
}
impl<U> CompositorHandler<U> {
pub fn new<L>(logger: L) -> CompositorHandler<U>
impl<U, H> CompositorHandler<U, H> {
/// Create a new CompositorHandler
pub fn new<L>(handler: H, logger: L) -> CompositorHandler<U, H>
where L: Into<Option<::slog::Logger>>
{
let log = ::slog_or_stdlog(logger);
CompositorHandler {
my_id: ::std::usize::MAX,
log: log.new(o!("smithay_module" => "compositor_handler")),
handler: handler,
_data: ::std::marker::PhantomData,
}
}
pub fn get_token(&self) -> CompositorToken<U> {
/// Create a token to access the data associated to the objects managed by this handler.
pub fn get_token(&self) -> CompositorToken<U, H> {
assert!(self.my_id != ::std::usize::MAX,
"CompositorHandler is not initialized yet.");
CompositorToken {
hid: self.my_id,
_data: ::std::marker::PhantomData,
_handler: ::std::marker::PhantomData,
}
}
/// Access the underlying sub-handler
pub fn get_handler(&mut self) -> &mut H {
&mut self.handler
}
}
/// Sub-handler trait for surface event handling
///
/// The global provided by Smithay cannot process these events for you, so they
/// are forwarded directly to a handler implementing this trait that you must provide
/// at creation of the `CompositorHandler`.
pub trait Handler {
/// See `wayland_server::protocol::wl_surface::Handler::commit`
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface);
/// See `wayland_server::protocol::wl_surface::Handler::frame`
fn frame(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
callback: wl_callback::WlCallback);
}

View File

@ -1,4 +1,4 @@
use super::{Rectangle, RegionAttributes, RectangleKind};
use super::{Rectangle, RectangleKind, RegionAttributes};
use std::sync::Mutex;
use wayland_server::Resource;