cargo fmt

This commit is contained in:
Victor Berger 2017-06-13 17:39:25 +02:00
parent a487c5963a
commit d475435fcb
4 changed files with 59 additions and 49 deletions

View File

@ -18,8 +18,7 @@ pub struct GliumDrawer<'a, F: 'a> {
}
impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
pub fn new(display: &'a F) -> 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,
@ -38,8 +37,7 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
Vertex {
position: [1.0, 0.0],
tex_coords: [1.0, 0.0],
}])
.unwrap();
}]).unwrap();
// building the index buffer
let index_buffer =
@ -73,8 +71,7 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
}
",
},
)
.unwrap();
).unwrap();
GliumDrawer {
display,
@ -84,23 +81,25 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
}
}
pub fn draw(&self, target: &mut glium::Frame, contents: &[u8], surface_dimensions: (u32, u32), surface_location: (i32,i32), screen_size: (u32,u32)) {
pub fn draw(&self, target: &mut glium::Frame, contents: &[u8], surface_dimensions: (u32, u32),
surface_location: (i32, i32), screen_size: (u32, u32)) {
let image = glium::texture::RawImage2d {
data: contents.into(),
width: surface_dimensions.0,
height: surface_dimensions.1,
format: glium::texture::ClientFormat::U8U8U8U8
format: glium::texture::ClientFormat::U8U8U8U8,
};
let opengl_texture = glium::texture::CompressedSrgbTexture2d::new(self.display, image).unwrap();
let xscale = 2.0*(surface_dimensions.0 as f32) / (screen_size.0 as f32);
let yscale = -2.0*(surface_dimensions.1 as f32) / (screen_size.1 as f32);
let xscale = 2.0 * (surface_dimensions.0 as f32) / (screen_size.0 as f32);
let yscale = -2.0 * (surface_dimensions.1 as f32) / (screen_size.1 as f32);
let x = 2.0*(surface_location.0 as f32) / (screen_size.0 as f32) - 1.0;
let y = 1.0 - 2.0*(surface_location.1 as f32) / (screen_size.1 as f32);
let x = 2.0 * (surface_location.0 as f32) / (screen_size.0 as f32) - 1.0;
let y = 1.0 - 2.0 * (surface_location.1 as f32) / (screen_size.1 as f32);
let uniforms = uniform! {
let uniforms =
uniform! {
matrix: [
[xscale, 0.0 , 0.0, 0.0],
[ 0.0 , yscale , 0.0, 0.0],
@ -110,7 +109,13 @@ impl<'a, F: glium::backend::Facade + 'a> GliumDrawer<'a, F> {
tex: &opengl_texture
};
target.draw(&self.vertex_buffer, &self.index_buffer, &self.program, &uniforms, &Default::default()).unwrap();
target
.draw(&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&Default::default())
.unwrap();
}
}

View File

@ -12,43 +12,45 @@ mod helpers;
use glium::Surface;
use helpers::{WlShellStubHandler, GliumDrawer};
use helpers::{GliumDrawer, WlShellStubHandler};
use slog::*;
use smithay::backend::graphics::glium::IntoGlium;
use smithay::backend::input::InputBackend;
use smithay::backend::winit;
use smithay::compositor::{self, CompositorHandler, CompositorToken};
use smithay::shm::{ShmGlobal, ShmToken, BufferData};
use smithay::compositor::{self, CompositorHandler, CompositorToken, TraversalAction};
use smithay::shm::{BufferData, ShmGlobal, ShmToken};
use wayland_server::{Client, EventLoopHandle, Liveness, Resource};
use wayland_server::protocol::{wl_compositor, wl_shell, wl_shm, wl_subcompositor, wl_surface};
use wayland_server::{EventLoopHandle,Client,Liveness, Resource};
struct SurfaceHandler {
shm_token: ShmToken
shm_token: ShmToken,
}
#[derive(Default)]
struct SurfaceData {
buffer: Option<(Vec<u8>, (u32, u32))>
buffer: Option<(Vec<u8>, (u32, u32))>,
}
impl compositor::Handler<SurfaceData> for SurfaceHandler {
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface, token: CompositorToken<SurfaceData, SurfaceHandler>) {
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
token: CompositorToken<SurfaceData, SurfaceHandler>) {
// 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)))) => {
Some(Some((buffer, (x, y)))) => {
self.shm_token.with_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);
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)));
});
}
@ -83,9 +85,7 @@ fn main() {
// retreive the token
let shm_token = {
let state = event_loop.state();
state
.get_handler::<ShmGlobal>(shm_handler_id)
.get_token()
state.get_handler::<ShmGlobal>(shm_handler_id).get_token()
};
@ -93,7 +93,11 @@ 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()));
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_subcompositor::WlSubcompositor, CompositorHandler<SurfaceData,SurfaceHandler>>(compositor_handler_id, 1);
@ -108,8 +112,8 @@ fn main() {
/*
* Initialize the shell stub global
*/
let shell_handler_id =
event_loop.add_handler_with_init(WlShellStubHandler::new(compositor_token.clone()));
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);
@ -123,11 +127,7 @@ fn main() {
/*
* Add a listening socket:
*/
let name = display
.add_socket_auto()
.unwrap()
.into_string()
.unwrap();
let name = display.add_socket_auto().unwrap().into_string().unwrap();
println!("Listening on socket: {}", name);
loop {

View File

@ -2,7 +2,7 @@ use super::{CompositorHandler, Damage, Handler as UserHandler, Rectangle, Rectan
SubsurfaceAttributes};
use super::region::RegionData;
use super::tree::{Location, SurfaceData};
use wayland_server::{Client, Destroy, EventLoopHandle, Resource, Liveness};
use wayland_server::{Client, Destroy, EventLoopHandle, Liveness, Resource};
use wayland_server::protocol::{wl_buffer, wl_callback, wl_compositor, wl_output, wl_region,
wl_subcompositor, wl_subsurface, wl_surface};
@ -53,8 +53,9 @@ impl<U, H: UserHandler<U>> wl_surface::Handler for CompositorHandler<U, H> {
buffer: Option<&wl_buffer::WlBuffer>, x: i32, y: i32) {
trace!(self.log, "Attaching buffer to surface.");
unsafe {
SurfaceData::<U>::with_data(surface,
|d| d.buffer = Some(buffer.map(|b| (b.clone_unchecked(), (x, y)))));
SurfaceData::<U>::with_data(surface, |d| {
d.buffer = Some(buffer.map(|b| (b.clone_unchecked(), (x, y))))
});
}
}
fn damage(&mut self, _: &mut EventLoopHandle, _: &Client, surface: &wl_surface::WlSurface, x: i32,
@ -129,7 +130,8 @@ impl<U, H: UserHandler<U>> wl_surface::Handler for CompositorHandler<U, H> {
}
}
unsafe impl<U, H: UserHandler<U>> ::wayland_server::Handler<wl_surface::WlSurface> for CompositorHandler<U, H> {
unsafe impl<U, H: UserHandler<U>> ::wayland_server::Handler<wl_surface::WlSurface>
for CompositorHandler<U, H> {
unsafe fn message(&mut self, evq: &mut EventLoopHandle, client: &Client,
resource: &wl_surface::WlSurface, opcode: u32,
args: *const ::wayland_server::sys::wl_argument)
@ -212,8 +214,9 @@ impl<U, H> wl_subcompositor::Handler for CompositorHandler<U, H>
}
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()));
SurfaceData::<U>::with_data(surface, |d| {
d.subsurface_attributes = Some(Default::default())
});
}
evqh.register_with_destructor::<_, CompositorHandler<U, H>, CompositorDestructor<U>>(&id, self.my_id);
}

View File

@ -213,7 +213,7 @@ impl Default for SubsurfaceAttributes {
}
/// Kind of a rectangle part of a region
#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub enum RectangleKind {
/// This rectangle should be added to the region
Add,
@ -223,7 +223,7 @@ pub enum RectangleKind {
}
/// A rectangle defined by its top-left corner and dimensions
#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub struct Rectangle {
/// horizontal position of the top-leftcorner of the rectangle, in surface coordinates
pub x: i32,
@ -445,7 +445,7 @@ impl<U, H> CompositorHandler<U, H> {
/// are forwarded directly to a handler implementing this trait that you must provide
/// at creation of the `CompositorHandler`.
#[allow(unused_variables)]
pub trait Handler<U> : Sized{
pub trait Handler<U>: Sized {
/// The double-buffered state has been validated by the client
///
/// At this point, the pending state that has been accumulated in the `SurfaceAttributes` associated
@ -453,7 +453,9 @@ pub trait Handler<U> : Sized{
///
/// See [`wayland_server::protocol::wl_surface::Handler::commit`](https://docs.rs/wayland-server/*/wayland_server/protocol/wl_surface/trait.Handler.html#method.commit)
/// for more details
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface, token: CompositorToken<U, Self>) {}
fn commit(&mut self, evlh: &mut EventLoopHandle, client: &Client, surface: &wl_surface::WlSurface,
token: CompositorToken<U, Self>) {
}
/// The client asks to be notified when would be a good time to update the contents of this surface
///
/// You must keep the provided `WlCallback` and trigger it at the appropriate time by calling