smithay/examples/simple.rs

250 lines
9.4 KiB
Rust
Raw Normal View History

2017-06-13 14:52:43 +00:00
#[macro_use]
extern crate glium;
2017-09-05 17:51:05 +00:00
#[macro_use(define_roles)]
extern crate smithay;
2017-09-05 19:09:50 +00:00
extern crate wayland_protocols;
2017-09-05 17:51:05 +00:00
#[macro_use(server_declare_handler)]
extern crate wayland_server;
2017-03-07 10:53:57 +00:00
2017-06-11 21:03:25 +00:00
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_term;
2017-06-13 14:52:43 +00:00
mod helpers;
2017-06-11 21:03:25 +00:00
use glium::Surface;
2017-06-13 14:52:43 +00:00
2017-09-05 19:09:50 +00:00
use helpers::GliumDrawer;
2017-09-03 17:53:29 +00:00
use slog::{Drain, Logger};
2017-06-11 21:03:25 +00:00
use smithay::backend::graphics::glium::IntoGlium;
2017-03-19 20:56:10 +00:00
use smithay::backend::input::InputBackend;
2017-05-21 20:51:38 +00:00
use smithay::backend::winit;
2017-09-03 17:53:29 +00:00
use smithay::compositor::{self, CompositorHandler, CompositorToken, SubsurfaceRole, TraversalAction};
use smithay::compositor::roles::Role;
2017-09-05 19:09:50 +00:00
use smithay::shell::{self, PopupConfigure, PopupSurface, ShellClient, ShellHandler, ShellSurfaceRole,
ToplevelConfigure, ToplevelSurface};
2017-06-13 15:39:25 +00:00
use smithay::shm::{BufferData, ShmGlobal, ShmToken};
2017-06-11 21:03:25 +00:00
2017-09-05 19:09:50 +00:00
use wayland_protocols::unstable::xdg_shell::server::{zxdg_shell_v6, zxdg_toplevel_v6};
use wayland_server::{Client, EventLoopHandle, Liveness, Resource};
use wayland_server::protocol::{wl_compositor, wl_output, wl_seat, wl_shell, wl_shm, wl_subcompositor,
wl_surface};
2017-06-11 21:03:25 +00:00
2017-09-03 17:53:29 +00:00
define_roles!(Roles => [ ShellSurface, ShellSurfaceRole ] );
2017-06-13 14:52:43 +00:00
struct SurfaceHandler {
2017-06-13 15:39:25 +00:00
shm_token: ShmToken,
2017-06-13 14:52:43 +00:00
}
#[derive(Default)]
struct SurfaceData {
2017-06-13 15:39:25 +00:00
buffer: Option<(Vec<u8>, (u32, u32))>,
2017-09-05 19:09:50 +00:00
location: Option<(u32, u32)>,
2017-06-13 14:52:43 +00:00
}
2017-06-11 21:03:25 +00:00
2017-09-03 17:53:29 +00:00
impl compositor::Handler<SurfaceData, Roles> for SurfaceHandler {
2017-06-13 15:39:25 +00:00
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
2017-09-03 17:53:29 +00:00
token: CompositorToken<SurfaceData, Roles, SurfaceHandler>) {
2017-06-13 14:52:43 +00:00
// we retrieve the contents of the associated buffer and copy it
token.with_surface_data(surface, |attributes| {
match attributes.buffer.take() {
2017-06-13 15:39:25 +00:00
Some(Some((buffer, (x, y)))) => {
2017-06-13 14:52:43 +00:00
self.shm_token.with_buffer_contents(&buffer, |slice, data| {
let offset = data.offset as usize;
let stride = data.stride as usize;
let width = data.width as usize;
let height = data.height as usize;
2017-06-13 15:39:25 +00:00
let mut new_vec = Vec::with_capacity(width * height * 4);
2017-06-13 14:52:43 +00:00
for i in 0..height {
2017-06-23 13:40:28 +00:00
new_vec.extend(
&slice[(offset + i * stride)..(offset + i * stride + width * 4)],
);
2017-06-13 14:52:43 +00:00
}
2017-06-23 13:40:28 +00:00
attributes.user_data.buffer =
Some((new_vec, (data.width as u32, data.height as u32)));
2017-06-13 14:52:43 +00:00
});
}
Some(None) => {
// erase the contents
attributes.user_data.buffer = None;
}
None => {}
}
});
}
}
2017-03-07 10:53:57 +00:00
2017-09-05 19:09:50 +00:00
struct ShellSurfaceHandler;
impl shell::Handler<SurfaceData, Roles, SurfaceHandler, ()> for ShellSurfaceHandler {
fn new_client(&mut self, evlh: &mut EventLoopHandle, client: ShellClient<()>) {}
fn client_pong(&mut self, evlh: &mut EventLoopHandle, client: ShellClient<()>) {}
fn new_toplevel(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>)
-> ToplevelConfigure {
ToplevelConfigure {
size: None,
states: vec![],
serial: 42,
}
}
fn new_popup(&mut self, evlh: &mut EventLoopHandle,
surface: PopupSurface<SurfaceData, Roles, SurfaceHandler, ()>)
-> PopupConfigure {
PopupConfigure {
size: (10, 10),
position: (10, 10),
serial: 42,
}
}
fn move_(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>, seat: &wl_seat::WlSeat,
serial: u32) {
}
fn resize(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>, seat: &wl_seat::WlSeat,
serial: u32, edges: zxdg_toplevel_v6::ResizeEdge) {
}
fn grab(&mut self, evlh: &mut EventLoopHandle,
surface: PopupSurface<SurfaceData, Roles, SurfaceHandler, ()>, seat: &wl_seat::WlSeat,
serial: u32) {
}
fn change_display_state(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>,
maximized: Option<bool>, minimized: Option<bool>, fullscreen: Option<bool>,
output: Option<&wl_output::WlOutput>)
-> ToplevelConfigure {
ToplevelConfigure {
size: None,
states: vec![],
serial: 42,
}
}
fn show_window_menu(&mut self, evlh: &mut EventLoopHandle,
surface: ToplevelSurface<SurfaceData, Roles, SurfaceHandler, ()>,
seat: &wl_seat::WlSeat, serial: u32, x: i32, y: i32) {
}
}
2017-09-03 17:53:29 +00:00
type MyCompositorHandler = CompositorHandler<SurfaceData, Roles, SurfaceHandler>;
2017-09-05 19:09:50 +00:00
type MyShellHandler = ShellHandler<SurfaceData, Roles, SurfaceHandler, ShellSurfaceHandler, ()>;
2017-09-03 17:53:29 +00:00
2017-03-07 10:53:57 +00:00
fn main() {
2017-06-11 21:03:25 +00:00
// A logger facility, here we use the terminal for this example
2017-06-23 13:40:28 +00:00
let log = Logger::root(
slog_async::Async::default(slog_term::term_full().fuse()).fuse(),
o!(),
);
2017-06-11 21:03:25 +00:00
2017-05-21 20:40:15 +00:00
// Initialize a simple backend for testing
2017-06-11 21:03:25 +00:00
let (renderer, mut input) = winit::init(log.clone()).unwrap();
2017-05-21 20:40:15 +00:00
2017-06-13 14:52:43 +00:00
let (mut display, mut event_loop) = wayland_server::create_display();
2017-03-07 10:53:57 +00:00
2017-06-11 21:03:25 +00:00
/*
* Initialize wl_shm global
*/
2017-03-07 10:53:57 +00:00
// Insert the ShmGlobal as a handler to your event loop
// Here, we specify tha the standard Argb8888 and Xrgb8888 is the only supported.
2017-06-11 21:08:59 +00:00
let shm_handler_id = event_loop.add_handler_with_init(ShmGlobal::new(vec![], log.clone()));
2017-03-07 10:53:57 +00:00
// Register this handler to advertise a wl_shm global of version 1
2017-06-11 21:03:25 +00:00
event_loop.register_global::<wl_shm::WlShm, ShmGlobal>(shm_handler_id, 1);
2017-06-13 14:52:43 +00:00
// retreive the token
let shm_token = {
let state = event_loop.state();
2017-06-13 15:39:25 +00:00
state.get_handler::<ShmGlobal>(shm_handler_id).get_token()
2017-06-13 14:52:43 +00:00
};
2017-06-11 21:03:25 +00:00
/*
* Initialize the compositor global
*/
2017-09-03 17:53:29 +00:00
let compositor_handler_id = event_loop.add_handler_with_init(MyCompositorHandler::new(
2017-09-05 17:51:05 +00:00
SurfaceHandler {
shm_token: shm_token.clone(),
},
2017-06-23 13:40:28 +00:00
log.clone(),
));
2017-06-11 21:03:25 +00:00
// register it to handle wl_compositor and wl_subcompositor
2017-09-03 17:53:29 +00:00
event_loop.register_global::<wl_compositor::WlCompositor, MyCompositorHandler>(compositor_handler_id, 4);
2017-06-23 13:40:28 +00:00
event_loop
2017-09-03 17:53:29 +00:00
.register_global::<wl_subcompositor::WlSubcompositor, MyCompositorHandler>(compositor_handler_id, 1);
2017-06-13 14:52:43 +00:00
// retrieve the tokens
let compositor_token = {
2017-03-07 10:53:57 +00:00
let state = event_loop.state();
2017-06-13 15:39:25 +00:00
state
2017-09-03 17:53:29 +00:00
.get_handler::<MyCompositorHandler>(compositor_handler_id)
2017-06-13 15:39:25 +00:00
.get_token()
2017-03-07 10:53:57 +00:00
};
2017-06-13 14:52:43 +00:00
/*
2017-09-05 19:09:50 +00:00
* Initialize the shell global
2017-06-13 14:52:43 +00:00
*/
2017-09-05 19:09:50 +00:00
let shell_handler_id = event_loop.add_handler_with_init(MyShellHandler::new(
ShellSurfaceHandler,
compositor_token.clone(),
log.clone(),
));
event_loop.register_global::<wl_shell::WlShell, MyShellHandler>(shell_handler_id, 1);
event_loop.register_global::<zxdg_shell_v6::ZxdgShellV6, MyShellHandler>(shell_handler_id, 1);
2017-06-13 14:52:43 +00:00
2017-06-11 21:03:25 +00:00
/*
* Initialize glium
*/
let context = renderer.into_glium();
2017-03-07 10:53:57 +00:00
2017-06-13 14:52:43 +00:00
let drawer = GliumDrawer::new(&context);
/*
* Add a listening socket:
*/
2017-06-13 15:39:25 +00:00
let name = display.add_socket_auto().unwrap().into_string().unwrap();
2017-06-13 14:52:43 +00:00
println!("Listening on socket: {}", name);
2017-03-07 10:53:57 +00:00
loop {
input.dispatch_new_events().unwrap();
let mut frame = context.draw();
2017-06-13 14:52:43 +00:00
frame.clear(None, Some((0.8, 0.8, 0.9, 1.0)), false, None, None);
// redraw the frame, in a simple but inneficient way
{
let screen_dimensions = context.get_framebuffer_dimensions();
let state = event_loop.state();
2017-09-05 19:09:50 +00:00
for toplevel_surface in state
.get_handler::<MyShellHandler>(shell_handler_id)
.toplevel_surfaces()
2017-06-23 13:40:28 +00:00
{
2017-09-05 19:09:50 +00:00
if let Some(wl_surface) = toplevel_surface.get_surface() {
// this surface is a root of a subsurface tree that needs to be drawn
compositor_token.with_surface_tree(
wl_surface,
(100, 100),
|surface, attributes, role, &(mut x, mut y)| {
if let Some((ref contents, (w, h))) = attributes.user_data.buffer {
// there is actually something to draw !
if let Ok(subdata) = Role::<SubsurfaceRole>::data(role) {
x += subdata.x;
y += subdata.y;
}
drawer.draw(&mut frame, contents, (w, h), (x, y), screen_dimensions);
TraversalAction::DoChildren((x, y))
} else {
// we are not display, so our children are neither
TraversalAction::SkipChildren
2017-09-05 17:51:05 +00:00
}
2017-09-05 19:09:50 +00:00
},
);
}
2017-06-13 14:52:43 +00:00
}
}
frame.finish().unwrap();
event_loop.dispatch(Some(16)).unwrap();
2017-06-13 14:52:43 +00:00
display.flush_clients();
}
2017-03-07 10:53:57 +00:00
}