smithay/examples/helpers/shell.rs

105 lines
3.3 KiB
Rust
Raw Normal View History

2017-06-13 14:52:43 +00:00
use smithay::compositor::{CompositorToken, Handler as CompositorHandler};
2017-09-03 17:53:29 +00:00
use smithay::compositor::roles::{Role, RoleType};
2017-06-13 14:52:43 +00:00
use wayland_server::{Client, EventLoopHandle, GlobalHandler, Init, Resource};
use wayland_server::protocol::{wl_shell, wl_shell_surface, wl_surface};
/// A very basic handler for wl_shell
///
/// All it does is track which wl_shell_surface exist and which do not,
/// as well as the roles associated to them.
///
/// That's it.
2017-09-03 17:53:29 +00:00
pub struct WlShellStubHandler<U, R, H> {
2017-06-13 14:52:43 +00:00
my_id: Option<usize>,
2017-09-03 17:53:29 +00:00
token: CompositorToken<U, R, H>,
2017-06-13 14:52:43 +00:00
surfaces: Vec<(wl_shell_surface::WlShellSurface, wl_surface::WlSurface)>,
}
2017-09-03 17:53:29 +00:00
#[derive(Default)]
pub struct ShellSurfaceRole;
impl<U, R, H> WlShellStubHandler<U, R, H> {
pub fn new(compositor_token: CompositorToken<U, R, H>) -> WlShellStubHandler<U, R, H> {
2017-06-13 14:52:43 +00:00
WlShellStubHandler {
my_id: None,
token: compositor_token,
surfaces: Vec::new(),
}
}
pub fn surfaces(&self) -> &[(wl_shell_surface::WlShellSurface, wl_surface::WlSurface)] {
&self.surfaces
}
}
2017-09-03 17:53:29 +00:00
impl<U, R, H> Init for WlShellStubHandler<U, R, H> {
2017-06-13 14:52:43 +00:00
fn init(&mut self, evqh: &mut EventLoopHandle, index: usize) {
self.my_id = Some(index)
}
}
2017-09-03 17:53:29 +00:00
impl<U, R, H> GlobalHandler<wl_shell::WlShell> for WlShellStubHandler<U, R, H>
where
U: Send + 'static,
R: RoleType
+ Role<ShellSurfaceRole>
+ Send
+ 'static,
H: CompositorHandler<U, R>
+ Send
+ 'static,
2017-06-13 14:52:43 +00:00
{
fn bind(&mut self, evqh: &mut EventLoopHandle, client: &Client, global: wl_shell::WlShell) {
2017-09-03 17:53:29 +00:00
evqh.register::<_, Self>(
&global,
self.my_id.expect(
"WlShellStubHandler was not properly initialized.",
),
);
2017-06-13 14:52:43 +00:00
}
}
2017-09-03 17:53:29 +00:00
impl<U, R, H> wl_shell::Handler for WlShellStubHandler<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<ShellSurfaceRole>
+ Send
+ 'static,
H: CompositorHandler<U, R> + Send + 'static,
2017-06-13 14:52:43 +00:00
{
fn get_shell_surface(&mut self, evqh: &mut EventLoopHandle, client: &Client,
resource: &wl_shell::WlShell, id: wl_shell_surface::WlShellSurface,
surface: &wl_surface::WlSurface) {
2017-06-23 13:40:28 +00:00
let surface = surface.clone().expect(
"WlShellStubHandler can only manage surfaces managed by Smithay's CompositorHandler.",
);
2017-09-03 17:53:29 +00:00
if self.token.give_role::<ShellSurfaceRole>(&surface).is_err() {
2017-06-13 14:52:43 +00:00
// This surface already has a role, and thus cannot be given one!
2017-06-23 13:40:28 +00:00
resource.post_error(
wl_shell::Error::Role as u32,
"Surface already has a role.".into(),
);
2017-06-13 14:52:43 +00:00
return;
}
evqh.register::<_, Self>(&id, self.my_id.unwrap());
self.surfaces.push((id, surface))
}
}
2017-09-03 17:53:29 +00:00
server_declare_handler!(WlShellStubHandler<U: [Send], R: [RoleType, Role<ShellSurfaceRole>, Send], H: [CompositorHandler<U, R>, Send]>, wl_shell::Handler, wl_shell::WlShell);
2017-06-13 14:52:43 +00:00
2017-09-03 17:53:29 +00:00
impl<U, R, H> wl_shell_surface::Handler for WlShellStubHandler<U, R, H>
2017-06-23 13:40:28 +00:00
where
U: Send + 'static,
2017-09-03 17:53:29 +00:00
H: CompositorHandler<U, R>
+ Send
+ 'static,
2017-06-13 14:52:43 +00:00
{
}
2017-09-03 17:53:29 +00:00
server_declare_handler!(WlShellStubHandler<U: [Send], R: [Send], H: [CompositorHandler<U, R>, Send]>, wl_shell_surface::Handler, wl_shell_surface::WlShellSurface);