smithay/examples/simple.rs

81 lines
2.5 KiB
Rust
Raw Normal View History

2017-03-07 10:53:57 +00:00
extern crate wayland_server;
extern crate smithay;
extern crate glium;
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;
use glium::Surface;
2017-06-11 21:08:59 +00:00
use slog::*;
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-06-11 21:08:59 +00:00
use smithay::compositor::{self, CompositorHandler};
2017-03-20 13:33:27 +00:00
use smithay::shm::ShmGlobal;
2017-06-11 21:03:25 +00:00
use wayland_server::protocol::{wl_compositor, wl_shm, wl_subcompositor};
struct SurfaceHandler;
2017-06-11 21:08:59 +00:00
impl compositor::Handler for SurfaceHandler {}
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-11 21:08:59 +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
let (_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);
/*
* Initialize the compositor global
*/
let compositor_handler_id =
2017-06-11 21:08:59 +00:00
event_loop.add_handler_with_init(CompositorHandler::<(), _>::new(SurfaceHandler, log.clone()));
2017-06-11 21:03:25 +00:00
// register it to handle wl_compositor and wl_subcompositor
event_loop.register_global::<wl_compositor::WlCompositor, CompositorHandler<(),SurfaceHandler>>(compositor_handler_id, 4);
event_loop.register_global::<wl_subcompositor::WlSubcompositor, CompositorHandler<(),SurfaceHandler>>(compositor_handler_id, 1);
2017-03-07 10:53:57 +00:00
2017-06-11 21:03:25 +00:00
/*
* retrieve the tokens
*/
let (shm_token, compositor_token) = {
2017-03-07 10:53:57 +00:00
let state = event_loop.state();
2017-06-11 21:08:59 +00:00
(state.get_handler::<ShmGlobal>(shm_handler_id).get_token(),
state
.get_handler::<CompositorHandler<(), SurfaceHandler>>(compositor_handler_id)
.get_token())
2017-03-07 10:53:57 +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
loop {
input.dispatch_new_events().unwrap();
let mut frame = context.draw();
frame.clear(None, Some((0.0, 0.0, 0.0, 1.0)), false, None, None);
frame.finish().unwrap();
event_loop.dispatch(Some(16)).unwrap();
}
2017-03-07 10:53:57 +00:00
}