Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WLR layer surfaces #203

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/layer_shell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Abstraction in later_shell_wlr_module

use smithay_client_toolkit::{
default_environment,
environment::SimpleGlobal,
Expand Down
150 changes: 150 additions & 0 deletions examples/layer_shell_wlr_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use smithay_client_toolkit::{
default_environment,
environment::SimpleGlobal,
new_default_environment,
output::{with_output_info, OutputInfo},
reexports::{
calloop,
client::protocol::{wl_output, wl_shm},
protocols::wlr::unstable::layer_shell::v1::client::zwlr_layer_shell_v1,
},
shell::layer,
shm::AutoMemPool,
WaylandSource,
};

use std::cell::RefCell;
use std::rc::Rc;

default_environment!(Env,
fields = [
layer_shell: SimpleGlobal<zwlr_layer_shell_v1::ZwlrLayerShellV1>,
],
singles = [
zwlr_layer_shell_v1::ZwlrLayerShellV1 => layer_shell
],
);

struct Surface {
surface: layer::LayerSurface,
pool: AutoMemPool,
}

impl Surface {
fn new(surface: layer::LayerSurface, pool: AutoMemPool) -> Self {
surface.surface.commit();

Self { pool, surface }
}

/// Handles any events that have occurred since the last call, redrawing if needed.
/// Returns true if the surface should be dropped.
fn handle_events(&mut self) -> bool {
match self.surface.render_event.take() {
Some(layer::RenderEvent::Closed) => true,
Some(layer::RenderEvent::Configure { width, height }) => {
self.surface.dimensions = (width, height);
self.draw();
false
}
None => false,
}
}

fn draw(&mut self) {
let stride = 4 * self.surface.dimensions.0 as i32;
let width = self.surface.dimensions.0 as i32;
let height = self.surface.dimensions.1 as i32;

// Note: unwrap() is only used here in the interest of simplicity of the example.
// A "real" application should handle the case where both pools are still in use by the
// compositor.
let (canvas, buffer) =
self.pool.buffer(width, height, stride, wl_shm::Format::Argb8888).unwrap();

for dst_pixel in canvas.chunks_exact_mut(4) {
let pixel = 0x24021bu32.to_ne_bytes();
dst_pixel[0] = pixel[0];
dst_pixel[1] = pixel[1];
dst_pixel[2] = pixel[2];
dst_pixel[3] = pixel[3];
}

// Attach the buffer to the surface and mark the entire surface as damaged
self.surface.surface.attach(Some(&buffer), 0, 0);
self.surface.surface.damage_buffer(0, 0, width as i32, height as i32);

// Finally, commit the surface
self.surface.surface.commit();
}
}

fn main() {
let (env, display, queue) =
new_default_environment!(Env, fields = [layer_shell: SimpleGlobal::new(),])
.expect("Initial roundtrip failed!");

let surfaces = Rc::new(RefCell::new(Vec::new()));

let layer_shell = env.require_global::<zwlr_layer_shell_v1::ZwlrLayerShellV1>();

let env_handle = env.clone();
let surfaces_handle = Rc::clone(&surfaces);
let output_handler = move |output: wl_output::WlOutput, info: &OutputInfo| {
if info.obsolete {
// an output has been removed, release it
surfaces_handle.borrow_mut().retain(|(i, _)| *i != info.id);
output.release();
} else {
// an output has been created, construct a surface for it
let surface = env_handle.create_surface().detach();
let pool = env_handle.create_auto_pool().expect("Failed to create a memory pool!");

let wlr_shell = layer::LayerSurface::new(
&output,
surface,
&layer_shell.clone(),
layer::Layer::Background,
layer::Anchor::Top,
(info.modes[0].dimensions.0 as u32, info.modes[0].dimensions.1 as u32),
);

(*surfaces_handle.borrow_mut()).push((info.id, Surface::new(wlr_shell, pool)));
}
};

// Process currently existing outputs
for output in env.get_all_outputs() {
if let Some(info) = with_output_info(&output, Clone::clone) {
output_handler(output, &info);
}
}

// Setup a listener for changes
// The listener will live for as long as we keep this handle alive
let _listner_handle =
env.listen_for_outputs(move |output, info, _| output_handler(output, info));

let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();

WaylandSource::new(queue).quick_insert(event_loop.handle()).unwrap();

loop {
// This is ugly, let's hope that some version of drain_filter() gets stabilized soon
// https://github.com/rust-lang/rust/issues/43244
{
let mut surfaces = surfaces.borrow_mut();
let mut i = 0;
while i != surfaces.len() {
if surfaces[i].1.handle_events() {
surfaces.remove(i);
} else {
i += 1;
}
}
}

display.flush().unwrap();
event_loop.dispatch(None, &mut ()).unwrap();
}
}
110 changes: 110 additions & 0 deletions src/shell/layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/// WLR layer abstraction
Eskpil marked this conversation as resolved.
Show resolved Hide resolved
use std::cell::Cell;
use std::rc::Rc;

use wayland_client::{
protocol::{wl_output, wl_surface},
Attached, Main,
};

use wayland_protocols::wlr::unstable::layer_shell::v1::client::{
zwlr_layer_shell_v1, zwlr_layer_surface_v1,
};

pub use wayland_protocols::wlr::unstable::layer_shell::v1::client::{
zwlr_layer_shell_v1::Layer, zwlr_layer_surface_v1::Anchor,
};

/// Render event

#[derive(PartialEq, Copy, Clone)]
pub enum RenderEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement Debug?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably go for LayerEvent since Close isn't exactly an event related to rendering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, changed

/// Surface wants a reconfiguration/configuration
Configure {
/// The new width of the surface
width: u32,

/// The new height of the surface
height: u32,
},

/// Surface has closed and wants to be closed here also.
Closed,
}

/// A struct representing the layer surface (wlr)
Copy link
Member

@i509VCB i509VCB Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to go into detail about what a layer is in the context of Wayland and possible use cases for a layer


pub struct LayerSurface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug here also

/// The raw wl_surface
pub surface: wl_surface::WlSurface,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably make the surface, layer, anchor and dimensions accessible via functions similar to how the Window abstraction is. The surface() function specifically returning an &wl_surface::WlSurface


pub(crate) layer_surface: Main<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now, we can always introduce a LayerInner when ext-layer-shell is initiated into wayland-protocols


/// On what layer it should be positioned
pub layer: Layer,

/// Where on the screen it is positioned
pub anchor: Anchor,

/// The dimensions of the wlr surface
pub dimensions: (u32, u32),

/// The next render event
pub render_event: Rc<Cell<Option<RenderEvent>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mimic what Window does here with callbacks.

Using a callback here makes sense and also provides the DispatchData the client has provided.

This would mean your LayerSurface::new would also now need to take a FnMut(Event, DispatchData) + 'static parameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the methods to create a window you will see one of the parameters is a Impl which has type bounds defined in the where statement.

This FnMut the user passes in is essentially a lambda that is invoked when some event occurs on the window.

You'll see where you call quick_assign on the layer you create that you are given an event, that is where you will invoke the callback.

The DispatchData is a dynamic data container for some arbitrary data a user of the library passes in that is available when events come in.

}

impl LayerSurface {
/// Create a new wlr shell

pub fn new(
output: &wl_output::WlOutput,
surface: wl_surface::WlSurface,
layer_shell: &Attached<zwlr_layer_shell_v1::ZwlrLayerShellV1>,
layer: Layer,
anchor: Anchor,
dimensions: (u32, u32),
) -> Self {
let layer_surface =
layer_shell.get_layer_surface(&surface, Some(output), layer, "example".to_owned());

layer_surface.set_size(dimensions.0, dimensions.1);
// Anchor to the top left corner of the output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment probably doesn't apply

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nop, i forgot to remove it after my CTRL C + CTRL V from the example.

layer_surface.set_anchor(anchor);

let next_render_event = Rc::new(Cell::new(None::<RenderEvent>));
let next_render_event_handle = Rc::clone(&next_render_event);
layer_surface.quick_assign(move |layer_surface, event, _| {
match (event, next_render_event_handle.get()) {
(zwlr_layer_surface_v1::Event::Closed, _) => {
next_render_event_handle.set(Some(RenderEvent::Closed));
}
(zwlr_layer_surface_v1::Event::Configure { serial, width, height }, next)
if next != Some(RenderEvent::Closed) =>
{
layer_surface.ack_configure(serial);
next_render_event_handle.set(Some(RenderEvent::Configure { width, height }));
}
(_, _) => {}
}
});

// Commit so that the server will send a configure event
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial commit for configure I guess?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it as this point i suggest we hand it off to the user and let them do what they want.

surface.commit();

Self {
surface,
layer_surface,
layer,
anchor,
render_event: next_render_event,
dimensions: (dimensions.0 as u32, dimensions.1 as u32),
}
}
}

impl Drop for LayerSurface {
fn drop(&mut self) {
self.layer_surface.destroy();
self.surface.destroy();
}
}
1 change: 1 addition & 0 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use wayland_protocols::{

use crate::environment::{Environment, GlobalHandler};

pub mod layer;
mod wl;
mod xdg;
mod zxdg;
Expand Down