2017-09-03 17:53:29 +00:00
|
|
|
use super::{CompositorHandler, Damage, Handler as UserHandler, Rectangle, RectangleKind, Role, RoleType,
|
|
|
|
SubsurfaceRole};
|
2017-06-04 15:47:37 +00:00
|
|
|
use super::region::RegionData;
|
2017-06-04 20:41:42 +00:00
|
|
|
use super::tree::{Location, SurfaceData};
|
2017-06-13 15:39:25 +00:00
|
|
|
use wayland_server::{Client, Destroy, EventLoopHandle, Liveness, Resource};
|
2017-06-04 15:47:37 +00:00
|
|
|
use wayland_server::protocol::{wl_buffer, wl_callback, wl_compositor, wl_output, wl_region,
|
|
|
|
wl_subcompositor, wl_subsurface, wl_surface};
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
struct CompositorDestructor<U, R> {
|
2017-06-04 15:47:37 +00:00
|
|
|
_t: ::std::marker::PhantomData<U>,
|
2017-09-03 17:53:29 +00:00
|
|
|
_r: ::std::marker::PhantomData<R>,
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wl_compositor
|
|
|
|
*/
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R, H> wl_compositor::Handler for CompositorHandler<U, R, H>
|
2017-06-23 13:40:28 +00:00
|
|
|
where
|
|
|
|
U: Default + Send + 'static,
|
2017-09-03 17:53:29 +00:00
|
|
|
R: Default + Send + 'static,
|
|
|
|
H: UserHandler<U, R> + Send + 'static,
|
2017-06-04 20:12:22 +00:00
|
|
|
{
|
|
|
|
fn create_surface(&mut self, evqh: &mut EventLoopHandle, _: &Client, _: &wl_compositor::WlCompositor,
|
|
|
|
id: wl_surface::WlSurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "New surface created.");
|
2017-09-03 17:53:29 +00:00
|
|
|
unsafe { SurfaceData::<U, R>::init(&id) };
|
|
|
|
evqh.register_with_destructor::<_, CompositorHandler<U, R, H>, CompositorDestructor<U, R>>(
|
|
|
|
&id,
|
|
|
|
self.my_id,
|
|
|
|
);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2017-06-04 20:12:22 +00:00
|
|
|
fn create_region(&mut self, evqh: &mut EventLoopHandle, _: &Client, _: &wl_compositor::WlCompositor,
|
|
|
|
id: wl_region::WlRegion) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "New region created.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe { RegionData::init(&id) };
|
2017-09-03 17:53:29 +00:00
|
|
|
evqh.register_with_destructor::<_, CompositorHandler<U, R, H>, CompositorDestructor<U, R>>(
|
|
|
|
&id,
|
|
|
|
self.my_id,
|
|
|
|
);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
server_declare_handler!(CompositorHandler<U: [Default, Send], R: [Default, Send], H: [UserHandler<U, R>, Send]>, wl_compositor::Handler, wl_compositor::WlCompositor);
|
2017-06-04 15:47:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* wl_surface
|
|
|
|
*/
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R, H: UserHandler<U, R>> wl_surface::Handler for CompositorHandler<U, R, H> {
|
2017-06-04 15:47:37 +00:00
|
|
|
fn attach(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
|
|
|
|
buffer: Option<&wl_buffer::WlBuffer>, x: i32, y: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Attaching buffer to surface.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| {
|
2017-06-13 15:39:25 +00:00
|
|
|
d.buffer = Some(buffer.map(|b| (b.clone_unchecked(), (x, y))))
|
|
|
|
});
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn damage(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface, x: i32,
|
|
|
|
y: i32, width: i32, height: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Registering damage to surface.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| {
|
2017-06-04 20:12:22 +00:00
|
|
|
d.damage = Damage::Surface(Rectangle {
|
2017-06-23 13:40:28 +00:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
2017-06-04 20:12:22 +00:00
|
|
|
});
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 20:12:22 +00:00
|
|
|
fn frame(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
|
2017-06-04 15:47:37 +00:00
|
|
|
callback: wl_callback::WlCallback) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Frame surface callback.");
|
2017-06-13 14:46:31 +00:00
|
|
|
let token = self.get_token();
|
|
|
|
UserHandler::frame(&mut self.handler, evlh, client, surface, callback, token);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
fn set_opaque_region(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
|
|
|
|
region: Option<&wl_region::WlRegion>) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting surface opaque region.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
|
|
|
let attributes = region.map(|r| RegionData::get_attributes(r));
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| d.opaque_region = attributes);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn set_input_region(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
|
|
|
|
region: Option<&wl_region::WlRegion>) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting surface input region.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
|
|
|
let attributes = region.map(|r| RegionData::get_attributes(r));
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| d.input_region = attributes);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 20:12:22 +00:00
|
|
|
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Commit surface callback.");
|
2017-06-13 14:46:31 +00:00
|
|
|
let token = self.get_token();
|
|
|
|
UserHandler::commit(&mut self.handler, evlh, client, surface, token);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
fn set_buffer_transform(&mut self, _: &mut EventLoopHandle, _: &Client,
|
|
|
|
surface: &wl_surface::WlSurface, transform: wl_output::Transform) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting surface's buffer transform.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| d.buffer_transform = transform);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn set_buffer_scale(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
|
|
|
|
scale: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting surface's buffer scale.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| d.buffer_scale = scale);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn damage_buffer(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
|
|
|
|
x: i32, y: i32, width: i32, height: i32) {
|
2017-06-23 13:40:28 +00:00
|
|
|
trace!(
|
|
|
|
self.log,
|
|
|
|
"Registering damage to surface (buffer coordinates)."
|
|
|
|
);
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_data(surface, |d| {
|
2017-06-04 20:12:22 +00:00
|
|
|
d.damage = Damage::Buffer(Rectangle {
|
2017-06-23 13:40:28 +00:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
2017-06-04 20:12:22 +00:00
|
|
|
});
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
server_declare_handler!(CompositorHandler<U:[], R: [], H: [UserHandler<U, R>]>, wl_surface::Handler, wl_surface::WlSurface);
|
2017-06-04 15:47:37 +00:00
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R> Destroy<wl_surface::WlSurface> for CompositorDestructor<U, R> {
|
2017-06-04 15:47:37 +00:00
|
|
|
fn destroy(surface: &wl_surface::WlSurface) {
|
2017-09-03 17:53:29 +00:00
|
|
|
unsafe { SurfaceData::<U, R>::cleanup(surface) }
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wl_region
|
|
|
|
*/
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R, H> wl_region::Handler for CompositorHandler<U, R, H> {
|
2017-06-04 15:47:37 +00:00
|
|
|
fn add(&mut self, _: &mut EventLoopHandle, _: &Client, region: &wl_region::WlRegion, x: i32, y: i32,
|
|
|
|
width: i32, height: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Adding rectangle to a region.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-06-23 13:40:28 +00:00
|
|
|
RegionData::add_rectangle(
|
|
|
|
region,
|
|
|
|
RectangleKind::Add,
|
|
|
|
Rectangle {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
},
|
|
|
|
)
|
2017-06-04 15:47:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
fn subtract(&mut self, _: &mut EventLoopHandle, _: &Client, region: &wl_region::WlRegion, x: i32,
|
|
|
|
y: i32, width: i32, height: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Subtracting rectangle to a region.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-06-23 13:40:28 +00:00
|
|
|
RegionData::add_rectangle(
|
|
|
|
region,
|
|
|
|
RectangleKind::Subtract,
|
|
|
|
Rectangle {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
},
|
|
|
|
)
|
2017-06-04 15:47:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
server_declare_handler!(CompositorHandler<U: [], R: [], H: []>, wl_region::Handler, wl_region::WlRegion);
|
2017-06-04 15:47:37 +00:00
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R> Destroy<wl_region::WlRegion> for CompositorDestructor<U, R> {
|
2017-06-04 15:47:37 +00:00
|
|
|
fn destroy(region: &wl_region::WlRegion) {
|
|
|
|
unsafe { RegionData::cleanup(region) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wl_subcompositor
|
|
|
|
*/
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R, H> wl_subcompositor::Handler for CompositorHandler<U, R, H>
|
2017-06-23 13:40:28 +00:00
|
|
|
where
|
|
|
|
U: Send + 'static,
|
2017-09-03 17:53:29 +00:00
|
|
|
R: RoleType
|
|
|
|
+ Role<SubsurfaceRole>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2017-06-23 13:40:28 +00:00
|
|
|
H: Send + 'static,
|
2017-06-04 20:12:22 +00:00
|
|
|
{
|
2017-06-04 15:47:37 +00:00
|
|
|
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) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Creating new subsurface.");
|
2017-09-03 17:53:29 +00:00
|
|
|
if let Err(()) = unsafe { SurfaceData::<U, R>::set_parent(surface, parent) } {
|
|
|
|
resource.post_error(
|
|
|
|
wl_subcompositor::Error::BadSurface as u32,
|
|
|
|
"Surface already has a role.".into(),
|
|
|
|
);
|
|
|
|
return;
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2017-06-23 13:40:28 +00:00
|
|
|
id.set_user_data(Box::into_raw(
|
|
|
|
Box::new(unsafe { surface.clone_unchecked() }),
|
|
|
|
) as *mut _);
|
2017-09-03 17:53:29 +00:00
|
|
|
evqh.register_with_destructor::<_, CompositorHandler<U, R, H>, CompositorDestructor<U, R>>(
|
|
|
|
&id,
|
|
|
|
self.my_id,
|
|
|
|
);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
server_declare_handler!(CompositorHandler<U: [Send], R: [RoleType, Role<SubsurfaceRole>, Send], H: [Send]>, wl_subcompositor::Handler, wl_subcompositor::WlSubcompositor);
|
2017-06-04 15:47:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* wl_subsurface
|
|
|
|
*/
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
unsafe fn with_subsurface_attributes<U, R, F>(subsurface: &wl_subsurface::WlSubsurface, f: F)
|
2017-06-23 13:40:28 +00:00
|
|
|
where
|
2017-09-03 17:53:29 +00:00
|
|
|
F: FnOnce(&mut SubsurfaceRole),
|
|
|
|
R: RoleType + Role<SubsurfaceRole>,
|
2017-06-04 15:47:37 +00:00
|
|
|
{
|
|
|
|
let ptr = subsurface.get_user_data();
|
|
|
|
let surface = &*(ptr as *mut wl_surface::WlSurface);
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::with_role_data::<SubsurfaceRole, _, _>(surface, |d| f(d)).expect(
|
|
|
|
"The surface does not have a subsurface role while it has a wl_subsurface?!",
|
|
|
|
);
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R, H> wl_subsurface::Handler for CompositorHandler<U, R, H>
|
|
|
|
where
|
|
|
|
R: RoleType + Role<SubsurfaceRole>,
|
|
|
|
{
|
2017-06-04 20:41:42 +00:00
|
|
|
fn set_position(&mut self, _: &mut EventLoopHandle, _: &Client,
|
|
|
|
subsurface: &wl_subsurface::WlSubsurface, x: i32, y: i32) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting subsurface position.");
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
with_subsurface_attributes::<U, R, _>(subsurface, |attrs| {
|
2017-06-04 15:47:37 +00:00
|
|
|
attrs.x = x;
|
|
|
|
attrs.y = y;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 20:41:42 +00:00
|
|
|
fn place_above(&mut self, _: &mut EventLoopHandle, _: &Client,
|
|
|
|
subsurface: &wl_subsurface::WlSubsurface, sibling: &wl_surface::WlSurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting subsurface above an other.");
|
2017-06-04 20:41:42 +00:00
|
|
|
unsafe {
|
|
|
|
let ptr = subsurface.get_user_data();
|
|
|
|
let surface = &*(ptr as *mut wl_surface::WlSurface);
|
2017-09-03 17:53:29 +00:00
|
|
|
if let Err(()) = SurfaceData::<U, R>::reorder(surface, Location::After, sibling) {
|
2017-06-04 20:41:42 +00:00
|
|
|
subsurface.post_error(wl_subsurface::Error::BadSurface as u32, "Provided surface is not a sibling or parent.".into());
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2017-06-04 20:41:42 +00:00
|
|
|
fn place_below(&mut self, _: &mut EventLoopHandle, _: &Client,
|
|
|
|
subsurface: &wl_subsurface::WlSubsurface, sibling: &wl_surface::WlSurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting subsurface below an other.");
|
2017-06-04 20:41:42 +00:00
|
|
|
unsafe {
|
|
|
|
let ptr = subsurface.get_user_data();
|
|
|
|
let surface = &*(ptr as *mut wl_surface::WlSurface);
|
2017-09-03 17:53:29 +00:00
|
|
|
if let Err(()) = SurfaceData::<U, R>::reorder(surface, Location::Before, sibling) {
|
2017-06-04 20:41:42 +00:00
|
|
|
subsurface.post_error(wl_subsurface::Error::BadSurface as u32, "Provided surface is not a sibling or parent.".into());
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
2017-06-04 20:41:42 +00:00
|
|
|
fn set_sync(&mut self, _: &mut EventLoopHandle, _: &Client, subsurface: &wl_subsurface::WlSubsurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting subsurface sync."; "sync_status" => true);
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
with_subsurface_attributes::<U, R, _>(subsurface, |attrs| { attrs.sync = true; });
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 20:41:42 +00:00
|
|
|
fn set_desync(&mut self, _: &mut EventLoopHandle, _: &Client, subsurface: &wl_subsurface::WlSubsurface) {
|
2017-06-11 16:50:47 +00:00
|
|
|
trace!(self.log, "Setting subsurface sync."; "sync_status" => false);
|
2017-06-04 15:47:37 +00:00
|
|
|
unsafe {
|
2017-09-03 17:53:29 +00:00
|
|
|
with_subsurface_attributes::<U, R, _>(subsurface, |attrs| { attrs.sync = false; });
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
server_declare_handler!(CompositorHandler<U: [], R: [RoleType, Role<SubsurfaceRole>], H: []>, wl_subsurface::Handler, wl_subsurface::WlSubsurface);
|
2017-06-04 15:47:37 +00:00
|
|
|
|
2017-09-03 17:53:29 +00:00
|
|
|
impl<U, R> Destroy<wl_subsurface::WlSubsurface> for CompositorDestructor<U, R>
|
|
|
|
where R: RoleType + Role<SubsurfaceRole>
|
|
|
|
{
|
2017-06-04 15:47:37 +00:00
|
|
|
fn destroy(subsurface: &wl_subsurface::WlSubsurface) {
|
|
|
|
let ptr = subsurface.get_user_data();
|
|
|
|
subsurface.set_user_data(::std::ptr::null_mut());
|
|
|
|
unsafe {
|
|
|
|
let surface = Box::from_raw(ptr as *mut wl_surface::WlSurface);
|
2017-06-13 14:46:31 +00:00
|
|
|
if surface.status() == Liveness::Alive {
|
2017-09-03 17:53:29 +00:00
|
|
|
SurfaceData::<U, R>::unset_parent(&surface);
|
2017-06-13 14:46:31 +00:00
|
|
|
}
|
2017-06-04 15:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|