-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement single-pixel buffer protocol
- Loading branch information
1 parent
839b393
commit 595eb4d
Showing
6 changed files
with
270 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod idle_notify; | ||
pub mod screencopy; | ||
pub mod single_pixel_buffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use smithay::reexports::wayland_server::protocol::wl_buffer::{self, WlBuffer}; | ||
use smithay::reexports::wayland_server::{ | ||
Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, | ||
}; | ||
use smithay::wayland::buffer::BufferHandler; | ||
use smithay::reexports::wayland_protocols::wp::single_pixel_buffer::v1::server::wp_single_pixel_buffer_manager_v1::{ | ||
self, WpSinglePixelBufferManagerV1, | ||
}; | ||
|
||
use crate::protocols::single_pixel_buffer::{SinglePixelBufferState, SinglePixelBufferUserData}; | ||
|
||
impl<D> GlobalDispatch<WpSinglePixelBufferManagerV1, (), D> for SinglePixelBufferState | ||
where | ||
D: GlobalDispatch<WpSinglePixelBufferManagerV1, ()>, | ||
D: Dispatch<WpSinglePixelBufferManagerV1, ()>, | ||
D: 'static, | ||
{ | ||
fn bind( | ||
_state: &mut D, | ||
_dh: &DisplayHandle, | ||
_client: &Client, | ||
resource: New<WpSinglePixelBufferManagerV1>, | ||
_global_data: &(), | ||
data_init: &mut DataInit<'_, D>, | ||
) { | ||
data_init.init(resource, ()); | ||
} | ||
} | ||
|
||
impl<D> Dispatch<WpSinglePixelBufferManagerV1, (), D> for SinglePixelBufferState | ||
where | ||
D: Dispatch<WpSinglePixelBufferManagerV1, ()>, | ||
D: Dispatch<WlBuffer, SinglePixelBufferUserData>, | ||
D: 'static, | ||
{ | ||
fn request( | ||
_state: &mut D, | ||
_client: &Client, | ||
_manager: &WpSinglePixelBufferManagerV1, | ||
request: wp_single_pixel_buffer_manager_v1::Request, | ||
_data: &(), | ||
_dh: &DisplayHandle, | ||
data_init: &mut DataInit<'_, D>, | ||
) { | ||
match request { | ||
wp_single_pixel_buffer_manager_v1::Request::CreateU32RgbaBuffer { | ||
id: buffer, | ||
r, | ||
g, | ||
b, | ||
a, | ||
} => { | ||
data_init.init(buffer, SinglePixelBufferUserData { r, g, b, a }); | ||
}, | ||
wp_single_pixel_buffer_manager_v1::Request::Destroy => {}, | ||
_ => unimplemented!(), | ||
} | ||
} | ||
} | ||
|
||
impl<D> Dispatch<WlBuffer, SinglePixelBufferUserData, D> for SinglePixelBufferState | ||
where | ||
D: Dispatch<WlBuffer, SinglePixelBufferUserData>, | ||
D: BufferHandler, | ||
{ | ||
fn request( | ||
data: &mut D, | ||
_client: &Client, | ||
buffer: &wl_buffer::WlBuffer, | ||
request: wl_buffer::Request, | ||
_udata: &SinglePixelBufferUserData, | ||
_dh: &DisplayHandle, | ||
_data_init: &mut DataInit<'_, D>, | ||
) { | ||
match request { | ||
wl_buffer::Request::Destroy => { | ||
data.buffer_destroyed(buffer); | ||
}, | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//! Utilities for handling the `wp_single_pixel_buffer` protocol | ||
use _single_pixel_buffer::v1::server::wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1; | ||
use smithay::reexports::wayland_protocols::wp::single_pixel_buffer as _single_pixel_buffer; | ||
use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; | ||
use smithay::reexports::wayland_server::{Dispatch, DisplayHandle, GlobalDispatch, Resource}; | ||
|
||
mod handlers; | ||
|
||
/// Delegate state of WpSinglePixelBuffer protocol | ||
#[derive(Debug)] | ||
pub struct SinglePixelBufferState; | ||
|
||
impl SinglePixelBufferState { | ||
/// Create a new [`WpSinglePixelBufferManagerV1`] global | ||
// | ||
/// The id provided by [`SinglePixelBufferState::global`] may be used to | ||
/// remove or disable this global in the future. | ||
pub fn new<D>(display: &DisplayHandle) -> Self | ||
where | ||
D: GlobalDispatch<WpSinglePixelBufferManagerV1, ()>, | ||
D: Dispatch<WpSinglePixelBufferManagerV1, ()>, | ||
D: 'static, | ||
{ | ||
display.create_global::<D, WpSinglePixelBufferManagerV1, _>(1, ()); | ||
|
||
Self | ||
} | ||
} | ||
|
||
/// User data of `WlBuffer` backed by single pixel | ||
#[derive(Debug)] | ||
pub struct SinglePixelBufferUserData { | ||
/// Value of the buffer's red channel | ||
pub r: u32, | ||
/// Value of the buffer's green channel | ||
pub g: u32, | ||
/// Value of the buffer's blue channel | ||
pub b: u32, | ||
/// Value of the buffer's alpha channel | ||
pub a: u32, | ||
} | ||
|
||
impl SinglePixelBufferUserData { | ||
/// Check if pixel has alpha | ||
pub fn has_alpha(&self) -> bool { | ||
self.a != u32::MAX | ||
} | ||
|
||
/// RGAB8888 color buffer | ||
pub fn rgba8888(&self) -> [u8; 4] { | ||
let divisor = u32::MAX / 255; | ||
|
||
[ | ||
(self.r / divisor) as u8, | ||
(self.g / divisor) as u8, | ||
(self.b / divisor) as u8, | ||
(self.a / divisor) as u8, | ||
] | ||
} | ||
} | ||
|
||
/// Error that can occur when accessing an SinglePixelBuffer | ||
#[derive(Debug)] | ||
pub enum BufferAccessError { | ||
/// This buffer is not managed by the SinglePixelBuffer handler | ||
NotManaged, | ||
} | ||
|
||
/// Gets the data of a `SinglePixelBuffer` backed [`WlBuffer`]. | ||
pub fn get_single_pixel_buffer( | ||
buffer: &WlBuffer, | ||
) -> Result<&SinglePixelBufferUserData, BufferAccessError> { | ||
buffer.data::<SinglePixelBufferUserData>().ok_or(BufferAccessError::NotManaged) | ||
} | ||
|
||
/// Macro used to delegate `WpSinglePixelBuffer` events | ||
#[macro_export] | ||
macro_rules! delegate_single_pixel_buffer { | ||
($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { | ||
smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
smithay::reexports::wayland_protocols::wp::single_pixel_buffer::v1::server::wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1: () | ||
] => $crate::protocols::single_pixel_buffer::SinglePixelBufferState); | ||
|
||
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
smithay::reexports::wayland_protocols::wp::single_pixel_buffer::v1::server::wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1: () | ||
] => $crate::protocols::single_pixel_buffer::SinglePixelBufferState); | ||
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer: $crate::protocols::single_pixel_buffer::SinglePixelBufferUserData | ||
] => $crate::protocols::single_pixel_buffer::SinglePixelBufferState); | ||
}; | ||
} |
Oops, something went wrong.