cargo fmt

This commit is contained in:
Victor Berger 2017-06-23 15:40:28 +02:00
parent 9d2f03943e
commit 4f6a852652
8 changed files with 186 additions and 123 deletions

View File

@ -21,23 +21,27 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
pub fn new(display: &'a F) -> GliumDrawer<'a, F> {
// building the vertex buffer, which contains all the vertices that we will draw
let vertex_buffer = glium::VertexBuffer::new(display,
&[Vertex {
position: [0.0, 0.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [0.0, 1.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
Vertex {
position: [1.0, 0.0],
tex_coords: [1.0, 0.0],
}]).unwrap();
let vertex_buffer = glium::VertexBuffer::new(
display,
&[
Vertex {
position: [0.0, 0.0],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [0.0, 1.0],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
Vertex {
position: [1.0, 0.0],
tex_coords: [1.0, 0.0],
},
],
).unwrap();
// building the index buffer
let index_buffer =
@ -110,11 +114,13 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
};
target
.draw(&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&Default::default())
.draw(
&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&Default::default(),
)
.unwrap();
}

View File

@ -49,17 +49,22 @@ impl<U, H> GlobalHandler<wl_shell::WlShell> for WlShellStubHandler<U, H>
}
impl<U, H> wl_shell::Handler for WlShellStubHandler<U, H>
where U: Send + 'static,
H: CompositorHandler<U> + Send + 'static
where
U: Send + 'static,
H: CompositorHandler<U> + Send + 'static,
{
fn get_shell_surface(&mut self, evqh: &mut EventLoopHandle, client: &Client,
resource: &wl_shell::WlShell, id: wl_shell_surface::WlShellSurface,
surface: &wl_surface::WlSurface) {
let surface = surface.clone().expect("WlShellStubHandler can only manage surfaces managed by Smithay's CompositorHandler.");
let surface = surface.clone().expect(
"WlShellStubHandler can only manage surfaces managed by Smithay's CompositorHandler.",
);
if self.token.give_role(&surface).is_err() {
// This surface already has a role, and thus cannot be given one!
resource.post_error(wl_shell::Error::Role as u32,
"Surface already has a role.".into());
resource.post_error(
wl_shell::Error::Role as u32,
"Surface already has a role.".into(),
);
return;
}
evqh.register::<_, Self>(&id, self.my_id.unwrap());
@ -70,8 +75,9 @@ impl<U, H> wl_shell::Handler for WlShellStubHandler<U, H>
server_declare_handler!(WlShellStubHandler<U: [Send], H: [CompositorHandler<U>, Send]>, wl_shell::Handler, wl_shell::WlShell);
impl<U, H> wl_shell_surface::Handler for WlShellStubHandler<U, H>
where U: Send + 'static,
H: CompositorHandler<U> + Send + 'static
where
U: Send + 'static,
H: CompositorHandler<U> + Send + 'static,
{
}

View File

@ -48,10 +48,12 @@ impl compositor::Handler<SurfaceData> for SurfaceHandler {
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)]);
new_vec.extend(
&slice[(offset + i * stride)..(offset + i * stride + width * 4)],
);
}
attributes.user_data.buffer = Some((new_vec,
(data.width as u32, data.height as u32)));
attributes.user_data.buffer =
Some((new_vec, (data.width as u32, data.height as u32)));
});
}
@ -67,8 +69,10 @@ impl compositor::Handler<SurfaceData> for SurfaceHandler {
fn main() {
// A logger facility, here we use the terminal for this example
let log = Logger::root(slog_async::Async::default(slog_term::term_full().fuse()).fuse(),
o!());
let log = Logger::root(
slog_async::Async::default(slog_term::term_full().fuse()).fuse(),
o!(),
);
// Initialize a simple backend for testing
let (renderer, mut input) = winit::init(log.clone()).unwrap();
@ -93,14 +97,16 @@ fn main() {
/*
* Initialize the compositor global
*/
let compositor_handler_id =
event_loop.add_handler_with_init(CompositorHandler::<SurfaceData, _>::new(SurfaceHandler {
shm_token: shm_token
.clone(),
},
log.clone()));
let compositor_handler_id = event_loop.add_handler_with_init(CompositorHandler::<SurfaceData, _>::new(
SurfaceHandler { shm_token: shm_token.clone() },
log.clone(),
));
// register it to handle wl_compositor and wl_subcompositor
event_loop.register_global::<wl_compositor::WlCompositor, CompositorHandler<SurfaceData,SurfaceHandler>>(compositor_handler_id, 4);
event_loop
.register_global::<wl_compositor::WlCompositor, CompositorHandler<SurfaceData, SurfaceHandler>>(
compositor_handler_id,
4,
);
event_loop.register_global::<wl_subcompositor::WlSubcompositor, CompositorHandler<SurfaceData,SurfaceHandler>>(compositor_handler_id, 1);
// retrieve the tokens
let compositor_token = {
@ -113,10 +119,12 @@ fn main() {
/*
* Initialize the shell stub global
*/
let shell_handler_id = event_loop
.add_handler_with_init(WlShellStubHandler::new(compositor_token.clone()));
event_loop.register_global::<wl_shell::WlShell, WlShellStubHandler<SurfaceData, SurfaceHandler>>(shell_handler_id,
1);
let shell_handler_id =
event_loop.add_handler_with_init(WlShellStubHandler::new(compositor_token.clone()));
event_loop.register_global::<wl_shell::WlShell, WlShellStubHandler<SurfaceData, SurfaceHandler>>(
shell_handler_id,
1,
);
/*
* Initialize glium
@ -143,27 +151,30 @@ fn main() {
for &(_, ref surface) in
state
.get_handler::<WlShellStubHandler<SurfaceData, SurfaceHandler>>(shell_handler_id)
.surfaces() {
.surfaces()
{
if surface.status() != Liveness::Alive {
continue;
}
// this surface is a root of a subsurface tree that needs to be drawn
compositor_token.with_surface_tree(surface, (100, 100), |surface,
attributes,
&(mut x, mut y)| {
if let Some((ref contents, (w, h))) = attributes.user_data.buffer {
// there is actually something to draw !
if let Some(ref subdata) = attributes.subsurface_attributes {
x += subdata.x;
y += subdata.y;
compositor_token.with_surface_tree(
surface,
(100, 100),
|surface, attributes, &(mut x, mut y)| {
if let Some((ref contents, (w, h))) = attributes.user_data.buffer {
// there is actually something to draw !
if let Some(ref subdata) = attributes.subsurface_attributes {
x += subdata.x;
y += subdata.y;
}
drawer.draw(&mut frame, contents, (w, h), (x, y), screen_dimensions);
TraversalAction::DoChildren((x, y))
} else {
// we are not display, so our children are neither
TraversalAction::SkipChildren
}
drawer.draw(&mut frame, contents, (w, h), (x, y), screen_dimensions);
TraversalAction::DoChildren((x, y))
} else {
// we are not display, so our children are neither
TraversalAction::SkipChildren
}
});
},
);
}
}
frame.finish().unwrap();

View File

@ -14,8 +14,9 @@ impl<U: Default, H: UserHandler<U>> GlobalHandler<wl_compositor::WlCompositor> f
}
impl<U, H> GlobalHandler<wl_subcompositor::WlSubcompositor> for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
where
U: Send + 'static,
H: Send + 'static,
{
fn bind(&mut self, evlh: &mut EventLoopHandle, _: &Client, global: wl_subcompositor::WlSubcompositor) {
debug!(self.log, "New subcompositor global binded.");

View File

@ -15,8 +15,9 @@ struct CompositorDestructor<U> {
*/
impl<U, H> wl_compositor::Handler for CompositorHandler<U, H>
where U: Default + Send + 'static,
H: UserHandler<U> + Send + 'static
where
U: Default + Send + 'static,
H: UserHandler<U> + Send + 'static,
{
fn create_surface(&mut self, evqh: &mut EventLoopHandle, _: &Client, _: &wl_compositor::WlCompositor,
id: wl_surface::WlSurface) {
@ -54,11 +55,11 @@ impl<U, H: UserHandler<U>> wl_surface::Handler for CompositorHandler<U, H> {
unsafe {
SurfaceData::<U>::with_data(surface, |d| {
d.damage = Damage::Surface(Rectangle {
x,
y,
width,
height,
})
x,
y,
width,
height,
})
});
}
}
@ -105,16 +106,18 @@ impl<U, H: UserHandler<U>> wl_surface::Handler for CompositorHandler<U, H> {
}
fn damage_buffer(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface,
x: i32, y: i32, width: i32, height: i32) {
trace!(self.log,
"Registering damage to surface (buffer coordinates).");
trace!(
self.log,
"Registering damage to surface (buffer coordinates)."
);
unsafe {
SurfaceData::<U>::with_data(surface, |d| {
d.damage = Damage::Buffer(Rectangle {
x,
y,
width,
height,
})
x,
y,
width,
height,
})
});
}
}
@ -137,28 +140,32 @@ impl<U, H> wl_region::Handler for CompositorHandler<U, H> {
width: i32, height: i32) {
trace!(self.log, "Adding rectangle to a region.");
unsafe {
RegionData::add_rectangle(region,
RectangleKind::Add,
Rectangle {
x,
y,
width,
height,
})
RegionData::add_rectangle(
region,
RectangleKind::Add,
Rectangle {
x,
y,
width,
height,
},
)
};
}
fn subtract(&mut self, _: &mut EventLoopHandle, _: &Client, region: &wl_region::WlRegion, x: i32,
y: i32, width: i32, height: i32) {
trace!(self.log, "Subtracting rectangle to a region.");
unsafe {
RegionData::add_rectangle(region,
RectangleKind::Subtract,
Rectangle {
x,
y,
width,
height,
})
RegionData::add_rectangle(
region,
RectangleKind::Subtract,
Rectangle {
x,
y,
width,
height,
},
)
};
}
}
@ -176,8 +183,9 @@ impl<U> Destroy<wl_region::WlRegion> for CompositorDestructor<U> {
*/
impl<U, H> wl_subcompositor::Handler for CompositorHandler<U, H>
where U: Send + 'static,
H: Send + 'static
where
U: Send + 'static,
H: Send + 'static,
{
fn get_subsurface(&mut self, evqh: &mut EventLoopHandle, _: &Client,
resource: &wl_subcompositor::WlSubcompositor, id: wl_subsurface::WlSubsurface,
@ -187,7 +195,9 @@ impl<U, H> wl_subcompositor::Handler for CompositorHandler<U, H>
resource.post_error(wl_subcompositor::Error::BadSurface as u32, "Surface already has a role.".into());
return
}
id.set_user_data(Box::into_raw(Box::new(unsafe { surface.clone_unchecked() })) as *mut _);
id.set_user_data(Box::into_raw(
Box::new(unsafe { surface.clone_unchecked() }),
) as *mut _);
unsafe {
SurfaceData::<U>::with_data(surface, |d| {
d.subsurface_attributes = Some(Default::default())
@ -204,7 +214,8 @@ server_declare_handler!(CompositorHandler<U: [Send], H: [Send]>, wl_subcomposito
*/
unsafe fn with_subsurface_attributes<U, F>(subsurface: &wl_subsurface::WlSubsurface, f: F)
where F: FnOnce(&mut SubsurfaceAttributes)
where
F: FnOnce(&mut SubsurfaceAttributes),
{
let ptr = subsurface.get_user_data();
let surface = &*(ptr as *mut wl_surface::WlSurface);

View File

@ -285,10 +285,13 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn with_surface_data<F>(&self, surface: &wl_surface::WlSurface, f: F)
where F: FnOnce(&mut SurfaceAttributes<U>)
where
F: FnOnce(&mut SurfaceAttributes<U>),
{
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe {
SurfaceData::<U>::with_data(surface, f);
}
@ -303,10 +306,13 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn with_surface_tree<F, T>(&self, surface: &wl_surface::WlSurface, initial: T, f: F) -> Result<(), ()>
where F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>
where
F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>,
{
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe {
SurfaceData::<U>::map_tree(surface, initial, f);
}
@ -320,8 +326,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn get_parent(&self, surface: &wl_surface::WlSurface) -> Option<wl_surface::WlSurface> {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe { SurfaceData::<U>::get_parent(surface) }
}
@ -330,8 +338,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn get_children(&self, surface: &wl_surface::WlSurface) -> Vec<wl_surface::WlSurface> {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe { SurfaceData::<U>::get_children(surface) }
}
@ -340,8 +350,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn role_status(&self, surface: &wl_surface::WlSurface) -> RoleStatus {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe { SurfaceData::<U>::role_status(surface) }
}
@ -355,8 +367,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn give_role(&self, surface: &wl_surface::WlSurface) -> Result<(), ()> {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe { SurfaceData::<U>::give_role(surface) }
}
@ -369,8 +383,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the surface is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn remove_role(&self, surface: &wl_surface::WlSurface) -> Result<(), ()> {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(surface, self.hid),
"Accessing the data of foreign surfaces is not supported."
);
unsafe { SurfaceData::<U>::remove_role(surface) }
}
@ -379,8 +395,10 @@ impl<U: Send + 'static, H: Handler<U> + Send + 'static> CompositorToken<U, H> {
/// If the region is not managed by the CompositorGlobal that provided this token, this
/// will panic (having more than one compositor is not supported).
pub fn get_region_attributes(&self, region: &wl_region::WlRegion) -> RegionAttributes {
assert!(resource_is_registered::<_, CompositorHandler<U, H>>(region, self.hid),
"Accessing the data of foreign regions is not supported.");
assert!(
resource_is_registered::<_, CompositorHandler<U, H>>(region, self.hid),
"Accessing the data of foreign regions is not supported."
);
unsafe { RegionData::get_attributes(region) }
}
}
@ -410,7 +428,8 @@ impl<U, H> Init for CompositorHandler<U, H> {
impl<U, H> CompositorHandler<U, H> {
/// Create a new CompositorHandler
pub fn new<L>(handler: H, logger: L) -> CompositorHandler<U, H>
where L: Into<Option<::slog::Logger>>
where
L: Into<Option<::slog::Logger>>,
{
let log = ::slog_or_stdlog(logger);
CompositorHandler {
@ -423,8 +442,10 @@ impl<U, H> CompositorHandler<U, H> {
/// Create a token to access the data associated to the objects managed by this handler.
pub fn get_token(&self) -> CompositorToken<U, H> {
assert!(self.my_id != ::std::usize::MAX,
"CompositorHandler is not initialized yet.");
assert!(
self.my_id != ::std::usize::MAX,
"CompositorHandler is not initialized yet."
);
trace!(self.log, "Creating a compositor token.");
CompositorToken {
hid: self.my_id,

View File

@ -13,7 +13,9 @@ pub struct RegionData {
impl RegionData {
/// Initialize the user_data of a region, must be called right when the surface is created
pub unsafe fn init(region: &wl_region::WlRegion) {
region.set_user_data(Box::into_raw(Box::new(Mutex::new(RegionData::default()))) as *mut _)
region.set_user_data(Box::into_raw(
Box::new(Mutex::new(RegionData::default())),
) as *mut _)
}
/// Cleans the user_data of that surface, must be called when it is destroyed

View File

@ -70,7 +70,9 @@ impl<U: Default> SurfaceData<U> {
/// Initialize the user_data of a surface, must be called right when the surface is created
pub unsafe fn init(surface: &wl_surface::WlSurface) {
surface.set_user_data(Box::into_raw(Box::new(Mutex::new(SurfaceData::<U>::new()))) as *mut _)
surface.set_user_data(Box::into_raw(
Box::new(Mutex::new(SurfaceData::<U>::new())),
) as *mut _)
}
}
@ -270,7 +272,8 @@ impl<U> SurfaceData<U> {
/// Note that an internal lock is taken during access of this data,
/// so the tree cannot be manipulated at the same time
pub unsafe fn with_data<F>(surface: &wl_surface::WlSurface, f: F)
where F: FnOnce(&mut SurfaceAttributes<U>)
where
F: FnOnce(&mut SurfaceAttributes<U>),
{
let data_mutex = Self::get_data(surface);
let mut data_guard = data_mutex.lock().unwrap();
@ -286,13 +289,15 @@ impl<U> SurfaceData<U> {
/// The callback returns wether the traversal should continue or not. Returning
/// false will cause an early-stopping.
pub unsafe fn map_tree<F, T>(root: &wl_surface::WlSurface, initial: T, mut f: F)
where F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>
where
F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>,
{
// helper function for recursion
unsafe fn map<U, F, T>(surface: &wl_surface::WlSurface, root: &wl_surface::WlSurface, initial: &T,
f: &mut F)
-> bool
where F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>
where
F: FnMut(&wl_surface::WlSurface, &mut SurfaceAttributes<U>, &T) -> TraversalAction<T>,
{
// stop if we met the root, so to not deadlock/inifinte loop
if surface.equals(root) {