smithay/examples/helpers/implementations.rs

181 lines
7.1 KiB
Rust
Raw Normal View History

use super::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};
2017-09-30 09:43:14 +00:00
use smithay::wayland::compositor::{compositor_init, CompositorToken, SurfaceAttributes,
SurfaceUserImplementation};
use smithay::wayland::shell::{shell_init, PopupConfigure, ShellState, ShellSurfaceRole,
2017-09-30 09:43:14 +00:00
ShellSurfaceUserImplementation, ToplevelConfigure};
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;
use wayland_server::{EventLoop, StateToken};
2017-09-20 13:03:58 +00:00
define_roles!(Roles => [ ShellSurface, ShellSurfaceRole ] );
#[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-01-07 21:30:38 +00:00
pub fn surface_implementation(
) -> SurfaceUserImplementation<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>> {
2017-09-20 13:03:58 +00:00
SurfaceUserImplementation {
commit: |_, display, surface, token| {
2017-09-20 13:03:58 +00:00
// we retrieve the contents of the associated buffer and copy it
token.with_surface_data(surface, |attributes| {
2017-12-28 14:28:15 +00:00
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() {
2018-01-07 21:30:38 +00:00
display.egl_buffer_contents(buffer)
} else {
Err(BufferAccessError::NotManaged(buffer))
} {
2017-12-28 14:28:15 +00:00
Ok(images) => {
match images.format {
2018-01-07 21:30:38 +00:00
Format::RGB => {}
Format::RGBA => {}
2017-12-28 14:28:15 +00:00
_ => {
// we don't handle the more complex formats here.
attributes.user_data.buffer = None;
attributes.user_data.texture = None;
return;
2018-01-07 21:30:38 +00:00
}
2017-12-28 14:28:15 +00:00
};
attributes.user_data.texture = None;
2017-12-28 14:28:15 +00:00
attributes.user_data.buffer = Some(Buffer::Egl { images });
2018-01-07 21:30:38 +00:00
}
2017-12-28 14:28:15 +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;
2017-12-28 14:28:15 +00:00
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!");
2017-12-28 14:28:15 +00:00
buffer.release();
2018-01-07 21:30:38 +00:00
}
2017-12-28 14:28:15 +00:00
Err(err) => panic!("EGL error: {}", err),
2017-12-10 21:09:17 +00:00
}
2017-09-20 13:03:58 +00:00
}
2017-12-28 14:28:15 +00:00
Some(None) => {
2017-09-20 13:03:58 +00:00
// erase the contents
attributes.user_data.buffer = None;
2017-12-28 14:28:15 +00:00
attributes.user_data.texture = None;
2017-09-20 13:03:58 +00:00
}
2017-12-28 14:28:15 +00:00
None => {}
2017-09-20 13:03:58 +00:00
}
});
},
frame: |_, _, _, callback, _| {
callback.done(0);
},
}
}
pub struct ShellIData<F> {
pub token: CompositorToken<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>>,
pub window_map: Rc<RefCell<super::WindowMap<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>, (), F>>>,
2017-09-22 12:56:59 +00:00
}
2018-01-07 21:30:38 +00:00
pub fn shell_implementation<F>(
) -> ShellSurfaceUserImplementation<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>, ShellIData<F>, ()>
2017-09-22 12:56:59 +00:00
where
F: Fn(&SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)>,
2017-09-20 13:03:58 +00:00
{
ShellSurfaceUserImplementation {
new_client: |_, _, _| {},
client_pong: |_, _, _| {},
2017-09-22 12:56:59 +00:00
new_toplevel: |_, idata, 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);
idata.window_map.borrow_mut().insert(toplevel, (x, y));
2017-09-20 13:03:58 +00:00
ToplevelConfigure {
size: None,
states: vec![],
serial: 42,
}
},
2018-01-07 21:30:38 +00:00
new_popup: |_, _, _| PopupConfigure {
size: (10, 10),
position: (10, 10),
serial: 42,
2017-09-20 13:03:58 +00:00
},
move_: |_, _, _, _, _| {},
resize: |_, _, _, _, _, _| {},
grab: |_, _, _, _, _| {},
2018-01-07 21:30:38 +00:00
change_display_state: |_, _, _, _, _, _, _| ToplevelConfigure {
size: None,
states: vec![],
serial: 42,
2017-09-20 13:03:58 +00:00
},
show_window_menu: |_, _, _, _, _, _, _| {},
}
}
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
}
pub type MyWindowMap = WindowMap<
2017-09-22 12:56:59 +00:00
SurfaceData,
Roles,
Rc<RefCell<Option<EGLDisplay>>>,
2017-09-22 12:56:59 +00:00
(),
fn(&SurfaceAttributes<SurfaceData>) -> Option<(i32, i32)>,
>;
pub fn init_shell(
2018-01-07 21:30:38 +00:00
evl: &mut EventLoop, log: ::slog::Logger, data: Rc<RefCell<Option<EGLDisplay>>>
) -> (
CompositorToken<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>>,
StateToken<ShellState<SurfaceData, Roles, Rc<RefCell<Option<EGLDisplay>>>, ()>>,
Rc<RefCell<MyWindowMap>>,
) {
2017-12-21 15:01:16 +00:00
let (compositor_token, _, _) = compositor_init(evl, surface_implementation(), data, log.clone());
2017-09-22 12:56:59 +00:00
let window_map = Rc::new(RefCell::new(WindowMap::<_, _, _, (), _>::new(
compositor_token,
get_size as _,
)));
let (shell_state_token, _, _) = shell_init(
evl,
compositor_token,
shell_implementation(),
ShellIData {
token: compositor_token,
window_map: window_map.clone(),
},
log.clone(),
);
(compositor_token, shell_state_token, window_map)
}