smithay/examples/helpers/implementations.rs

202 lines
7.8 KiB
Rust
Raw Normal View History

use super::{SurfaceKind, WindowMap};
2017-12-28 14:28:15 +00:00
use glium::texture::Texture2d;
2017-09-20 13:03:58 +00:00
use rand;
2018-01-07 21:30:38 +00:00
use smithay::backend::graphics::egl::wayland::{BufferAccessError, Format};
use smithay::backend::graphics::egl::wayland::{EGLDisplay, EGLImages};
2018-04-22 09:58:39 +00:00
use smithay::wayland::compositor::{compositor_init, CompositorToken, SurfaceAttributes, SurfaceEvent};
use smithay::wayland::shell::xdg::{xdg_shell_init, PopupConfigure, ShellState as XdgShellState,
ToplevelConfigure, XdgRequest, XdgSurfaceRole};
use smithay::wayland::shell::legacy::{wl_shell_init, ShellRequest, ShellState as WlShellState,
ShellSurfaceKind, ShellSurfaceRole};
2017-12-10 21:09:17 +00:00
use smithay::wayland::shm::with_buffer_contents as shm_buffer_contents;
2017-09-22 12:56:59 +00:00
use std::cell::RefCell;
use std::rc::Rc;
2018-04-22 09:58:39 +00:00
use std::sync::{Arc, Mutex};
use wayland_server::{Display, LoopToken, Resource};
use wayland_server::protocol::{wl_buffer, wl_callback, wl_shell_surface, wl_surface};
2017-09-20 13:03:58 +00:00
define_roles!(Roles => [ XdgSurface, XdgSurfaceRole ] [ ShellSurface, ShellSurfaceRole<()>] );
2017-09-20 13:03:58 +00:00
#[derive(Default)]
pub struct SurfaceData {
2017-12-21 15:01:16 +00:00
pub buffer: Option<Buffer>,
2017-12-28 14:28:15 +00:00
pub texture: Option<Texture2d>,
2017-09-20 13:03:58 +00:00
}
2017-12-10 21:09:17 +00:00
pub enum Buffer {
2017-12-28 14:28:15 +00:00
Egl { images: EGLImages },
2017-12-10 21:09:17 +00:00
Shm { data: Vec<u8>, size: (u32, u32) },
}
2018-04-22 09:58:39 +00:00
fn surface_commit(
surface: &Resource<wl_surface::WlSurface>,
token: CompositorToken<SurfaceData, Roles>,
display: &RefCell<Option<EGLDisplay>>,
) {
// 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)))) => {
// we ignore hotspot coordinates in this simple example
match if let Some(display) = display.borrow().as_ref() {
display.egl_buffer_contents(buffer)
} else {
Err(BufferAccessError::NotManaged(buffer))
} {
Ok(images) => {
match images.format {
Format::RGB => {}
Format::RGBA => {}
_ => {
// we don't handle the more complex formats here.
attributes.user_data.buffer = None;
attributes.user_data.texture = None;
2018-04-22 09:58:39 +00:00
return;
2018-01-07 21:30:38 +00:00
}
2018-04-22 09:58:39 +00:00
};
2017-12-28 14:28:15 +00:00
attributes.user_data.texture = None;
2018-04-22 09:58:39 +00:00
attributes.user_data.buffer = Some(Buffer::Egl { images });
2017-09-20 13:03:58 +00:00
}
2018-04-22 09:58:39 +00:00
Err(BufferAccessError::NotManaged(buffer)) => {
shm_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;
let mut new_vec = Vec::with_capacity(width * height * 4);
for i in 0..height {
new_vec
.extend(&slice[(offset + i * stride)..(offset + i * stride + width * 4)]);
}
attributes.user_data.texture = None;
attributes.user_data.buffer = Some(Buffer::Shm { data: new_vec, size: (data.width as u32, data.height as u32) });
}).expect("Got EGL buffer with no set EGLDisplay. You need to unbind your EGLContexts before dropping them!");
buffer.send(wl_buffer::Event::Release);
}
Err(err) => panic!("EGL error: {}", err),
2017-09-20 13:03:58 +00:00
}
}
2018-04-22 09:58:39 +00:00
Some(None) => {
// erase the contents
attributes.user_data.buffer = None;
attributes.user_data.texture = None;
}
None => {}
}
});
2017-09-20 13:03:58 +00:00
}
2017-09-22 12:56:59 +00:00
fn get_size(attrs: &SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)> {
attrs
.user_data
.buffer
.as_ref()
2017-12-28 14:28:15 +00:00
.map(|ref buffer| match **buffer {
Buffer::Shm { ref size, .. } => *size,
Buffer::Egl { ref images } => (images.width, images.height),
})
.map(|(x, y)| (x as i32, y as i32))
2017-09-22 12:56:59 +00:00
}
2018-04-22 09:58:39 +00:00
pub type MyWindowMap =
WindowMap<SurfaceData, Roles, (), (), fn(&SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)>>;
2017-09-22 12:56:59 +00:00
pub fn init_shell(
2018-04-22 09:58:39 +00:00
display: &mut Display,
looptoken: LoopToken,
log: ::slog::Logger,
egl_display: Rc<RefCell<Option<EGLDisplay>>>,
2018-01-07 21:30:38 +00:00
) -> (
2018-04-22 09:58:39 +00:00
CompositorToken<SurfaceData, Roles>,
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 c_egl_display = egl_display.clone();
let (compositor_token, _, _) = compositor_init(
display,
looptoken.clone(),
move |request, (surface, ctoken)| match request {
SurfaceEvent::Commit => surface_commit(&surface, ctoken, &*c_egl_display),
SurfaceEvent::Frame { callback } => callback
.implement(|e, _| match e {}, None::<fn(_, _)>)
.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
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
let xdg_window_map = window_map.clone();
let (xdg_shell_state, _, _) = xdg_shell_init(
2018-04-22 09:58:39 +00:00
display,
looptoken.clone(),
2018-04-22 09:58:39 +00:00
compositor_token.clone(),
move |shell_event, ()| match shell_event {
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,
});
xdg_window_map
.borrow_mut()
.insert(SurfaceKind::Xdg(surface), (x, y));
2018-04-22 09:58:39 +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(),
);
// init the wl_shell
let shell_window_map = window_map.clone();
let (wl_shell_state, _) = wl_shell_init(
display,
looptoken,
compositor_token.clone(),
move |req: ShellRequest<_, _, ()>, ()| match req {
ShellRequest::SetKind {
surface,
kind: ShellSurfaceKind::Toplevel,
} => {
// 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));
}
_ => (),
},
log.clone(),
);
(
compositor_token,
xdg_shell_state,
wl_shell_state,
window_map,
)
2017-09-22 12:56:59 +00:00
}