smithay/examples/winit.rs

111 lines
3.8 KiB
Rust
Raw Normal View History

2017-06-13 14:52:43 +00:00
#[macro_use]
extern crate glium;
extern crate rand;
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_term;
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
extern crate wayland_server;
2017-03-07 10:53:57 +00:00
2017-06-13 14:52:43 +00:00
mod helpers;
2017-06-11 21:03:25 +00:00
use glium::Surface;
2017-09-20 13:03:58 +00:00
use helpers::{shell_implementation, surface_implementation, GliumDrawer};
2017-09-03 17:53:29 +00:00
use slog::{Drain, Logger};
use smithay::backend::graphics::egl::EGLGraphicsBackend;
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-20 13:03:58 +00:00
use smithay::compositor::{compositor_init, SubsurfaceRole, TraversalAction};
2017-09-03 17:53:29 +00:00
use smithay::compositor::roles::Role;
2017-09-20 13:03:58 +00:00
use smithay::shell::shell_init;
use smithay::shm::init_shm_global;
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
/*
2017-09-20 13:03:58 +00:00
* Initialize the globals
2017-06-11 21:03:25 +00:00
*/
2017-06-13 14:52:43 +00:00
2017-09-20 13:03:58 +00:00
init_shm_global(&mut event_loop, vec![], log.clone());
2017-06-11 21:03:25 +00:00
2017-09-20 13:03:58 +00:00
let (compositor_token, _, _) =
compositor_init(&mut event_loop, surface_implementation(), (), log.clone());
2017-03-07 10:53:57 +00:00
2017-09-20 13:03:58 +00:00
let (shell_state_token, _, _) = shell_init(
&mut event_loop,
compositor_token,
shell_implementation(),
compositor_token,
2017-09-05 19:09:50 +00:00
log.clone(),
2017-09-20 13:03:58 +00:00
);
2017-06-13 14:52:43 +00:00
2017-06-11 21:03:25 +00:00
/*
* Initialize glium
*/
let drawer = GliumDrawer::new(renderer.into_glium());
2017-06-13 14:52:43 +00:00
/*
* 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 = drawer.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 = drawer.get_framebuffer_dimensions();
2017-06-13 14:52:43 +00:00
let state = event_loop.state();
2017-09-20 13:03:58 +00:00
for toplevel_surface in state.get(&shell_state_token).toplevel_surfaces() {
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
let initial_place = compositor_token
.with_surface_data(wl_surface, |data| data.user_data.location.unwrap_or((0, 0)));
2017-09-06 14:33:35 +00:00
compositor_token
.with_surface_tree(
wl_surface,
initial_place,
|_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.render(&mut frame, contents, (w, h), (x, y), screen_dimensions);
2017-09-06 14:33:35 +00:00
TraversalAction::DoChildren((x, y))
} else {
// we are not display, so our children are neither
TraversalAction::SkipChildren
2017-09-05 19:09:50 +00:00
}
2017-09-06 14:33:35 +00:00
},
)
.unwrap();
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
}