2018-10-02 21:37:24 +00:00
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
rc::Rc,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
2018-05-07 17:56:38 +00:00
|
|
|
|
2017-09-20 13:03:58 +00:00
|
|
|
use rand;
|
2018-05-07 17:56:38 +00:00
|
|
|
|
2018-10-02 21:37:24 +00:00
|
|
|
use smithay::{
|
|
|
|
wayland::{
|
|
|
|
compositor::{compositor_init, CompositorToken, SurfaceAttributes, SurfaceEvent},
|
|
|
|
shell::{
|
|
|
|
legacy::{
|
|
|
|
wl_shell_init, ShellRequest, ShellState as WlShellState, ShellSurfaceKind, ShellSurfaceRole,
|
|
|
|
},
|
|
|
|
xdg::{
|
|
|
|
xdg_shell_init, PopupConfigure, ShellState as XdgShellState, ToplevelConfigure, XdgRequest,
|
|
|
|
XdgSurfaceRole,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wayland_server::{
|
|
|
|
protocol::{wl_buffer, wl_callback, wl_shell_surface, wl_surface},
|
|
|
|
Display, Resource,
|
|
|
|
},
|
2018-09-24 22:32:09 +00:00
|
|
|
};
|
2017-12-10 21:09:17 +00:00
|
|
|
|
2018-05-07 17:56:38 +00:00
|
|
|
use window_map::{Kind as SurfaceKind, WindowMap};
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2018-05-07 17:56:38 +00:00
|
|
|
define_roles!(Roles => [ XdgSurface, XdgSurfaceRole ] [ ShellSurface, ShellSurfaceRole<()>] );
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
pub type MyWindowMap =
|
2018-04-23 09:40:41 +00:00
|
|
|
WindowMap<SurfaceData, Roles, (), (), fn(&SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)>>;
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2018-05-08 18:08:17 +00:00
|
|
|
pub type MyCompositorToken = CompositorToken<SurfaceData, Roles>;
|
|
|
|
|
2018-01-05 19:04:46 +00:00
|
|
|
pub fn init_shell(
|
2018-04-22 09:58:39 +00:00
|
|
|
display: &mut Display,
|
|
|
|
log: ::slog::Logger,
|
2018-01-07 21:30:38 +00:00
|
|
|
) -> (
|
2018-04-22 09:58:39 +00:00
|
|
|
CompositorToken<SurfaceData, Roles>,
|
2018-04-23 09:40:41 +00:00
|
|
|
Arc<Mutex<XdgShellState<SurfaceData, Roles, ()>>>,
|
|
|
|
Arc<Mutex<WlShellState<SurfaceData, Roles, ()>>>,
|
2018-01-07 21:30:38 +00:00
|
|
|
Rc<RefCell<MyWindowMap>>,
|
|
|
|
) {
|
2018-04-22 09:58:39 +00:00
|
|
|
// Create the compositor
|
|
|
|
let (compositor_token, _, _) = compositor_init(
|
|
|
|
display,
|
2018-09-24 22:30:39 +00:00
|
|
|
move |request, surface, ctoken| match request {
|
2018-05-19 17:51:26 +00:00
|
|
|
SurfaceEvent::Commit => surface_commit(&surface, ctoken),
|
2018-04-23 16:26:17 +00:00
|
|
|
SurfaceEvent::Frame { callback } => callback
|
2018-09-24 22:30:39 +00:00
|
|
|
.implement(|e, _| match e {}, None::<fn(_)>, ())
|
2018-04-23 16:26:17 +00:00
|
|
|
.send(wl_callback::Event::Done { callback_data: 0 }),
|
2018-04-22 09:58:39 +00:00
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
2017-09-22 12:56:59 +00:00
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
// Init a window map, to track the location of our windows
|
2018-04-23 09:40:41 +00:00
|
|
|
let window_map = Rc::new(RefCell::new(WindowMap::<_, _, (), (), _>::new(
|
2017-09-22 12:56:59 +00:00
|
|
|
compositor_token,
|
|
|
|
get_size as _,
|
|
|
|
)));
|
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
// init the xdg_shell
|
2018-04-23 09:40:41 +00:00
|
|
|
let xdg_window_map = window_map.clone();
|
|
|
|
let (xdg_shell_state, _, _) = xdg_shell_init(
|
2018-04-22 09:58:39 +00:00
|
|
|
display,
|
2018-06-27 12:04:29 +00:00
|
|
|
compositor_token,
|
2018-09-24 22:30:39 +00:00
|
|
|
move |shell_event| match shell_event {
|
2018-04-22 17:58:10 +00:00
|
|
|
XdgRequest::NewToplevel { surface } => {
|
2018-04-22 09:58:39 +00:00
|
|
|
// place the window at a random location in the [0;300]x[0;300] square
|
|
|
|
use rand::distributions::{IndependentSample, Range};
|
|
|
|
let range = Range::new(0, 300);
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let x = range.ind_sample(&mut rng);
|
|
|
|
let y = range.ind_sample(&mut rng);
|
|
|
|
surface.send_configure(ToplevelConfigure {
|
|
|
|
size: None,
|
|
|
|
states: vec![],
|
|
|
|
serial: 42,
|
|
|
|
});
|
2018-04-23 09:40:41 +00:00
|
|
|
xdg_window_map
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(SurfaceKind::Xdg(surface), (x, y));
|
2018-04-22 09:58:39 +00:00
|
|
|
}
|
2018-04-22 17:58:10 +00:00
|
|
|
XdgRequest::NewPopup { surface } => surface.send_configure(PopupConfigure {
|
2018-04-22 09:58:39 +00:00
|
|
|
size: (10, 10),
|
|
|
|
position: (10, 10),
|
|
|
|
serial: 42,
|
|
|
|
}),
|
|
|
|
_ => (),
|
2017-09-22 12:56:59 +00:00
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2018-04-23 09:40:41 +00:00
|
|
|
// init the wl_shell
|
|
|
|
let shell_window_map = window_map.clone();
|
|
|
|
let (wl_shell_state, _) = wl_shell_init(
|
|
|
|
display,
|
2018-06-27 12:04:29 +00:00
|
|
|
compositor_token,
|
2018-09-24 22:32:09 +00:00
|
|
|
move |req: ShellRequest<_, _, ()>| {
|
2018-06-27 12:04:29 +00:00
|
|
|
if let ShellRequest::SetKind {
|
2018-04-23 09:40:41 +00:00
|
|
|
surface,
|
|
|
|
kind: ShellSurfaceKind::Toplevel,
|
2018-09-24 22:32:09 +00:00
|
|
|
} = req
|
|
|
|
{
|
2018-04-23 09:40:41 +00:00
|
|
|
// place the window at a random location in the [0;300]x[0;300] square
|
|
|
|
use rand::distributions::{IndependentSample, Range};
|
|
|
|
let range = Range::new(0, 300);
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let x = range.ind_sample(&mut rng);
|
|
|
|
let y = range.ind_sample(&mut rng);
|
|
|
|
surface.send_configure((0, 0), wl_shell_surface::Resize::None);
|
|
|
|
shell_window_map
|
|
|
|
.borrow_mut()
|
|
|
|
.insert(SurfaceKind::Wl(surface), (x, y));
|
2018-09-24 22:32:09 +00:00
|
|
|
}
|
|
|
|
},
|
2018-04-23 09:40:41 +00:00
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2018-05-19 17:51:26 +00:00
|
|
|
(compositor_token, xdg_shell_state, wl_shell_state, window_map)
|
2017-09-22 12:56:59 +00:00
|
|
|
}
|
2018-05-07 17:56:38 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SurfaceData {
|
2018-05-19 17:51:26 +00:00
|
|
|
pub buffer: Option<Resource<wl_buffer::WlBuffer>>,
|
|
|
|
pub texture: Option<::glium_drawer::TextureMetadata>,
|
2018-05-07 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 17:51:26 +00:00
|
|
|
fn surface_commit(surface: &Resource<wl_surface::WlSurface>, token: CompositorToken<SurfaceData, Roles>) {
|
2018-05-07 17:56:38 +00:00
|
|
|
// we retrieve the contents of the associated buffer and copy it
|
|
|
|
token.with_surface_data(surface, |attributes| {
|
|
|
|
match attributes.buffer.take() {
|
|
|
|
Some(Some((buffer, (_x, _y)))) => {
|
2018-05-19 17:51:26 +00:00
|
|
|
// new contents
|
|
|
|
// TODO: handle hotspot coordinates
|
|
|
|
attributes.user_data.buffer = Some(buffer);
|
|
|
|
attributes.user_data.texture = None;
|
2018-05-07 17:56:38 +00:00
|
|
|
}
|
|
|
|
Some(None) => {
|
|
|
|
// erase the contents
|
|
|
|
attributes.user_data.buffer = None;
|
|
|
|
attributes.user_data.texture = None;
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_size(attrs: &SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)> {
|
|
|
|
attrs
|
|
|
|
.user_data
|
2018-05-19 17:51:26 +00:00
|
|
|
.texture
|
2018-05-07 17:56:38 +00:00
|
|
|
.as_ref()
|
2018-05-19 17:51:26 +00:00
|
|
|
.map(|ref meta| meta.dimensions)
|
2018-05-07 17:56:38 +00:00
|
|
|
.map(|(x, y)| (x as i32, y as i32))
|
|
|
|
}
|