pub mod multi;
pub mod raw;
pub mod slot;
use std::io;
use wayland_client::{
globals::{BindError, GlobalList},
protocol::wl_shm,
Connection, Dispatch, QueueHandle, WEnum,
};
use crate::{
error::GlobalError,
globals::{GlobalData, ProvidesBoundGlobal},
};
pub trait ShmHandler {
fn shm_state(&mut self) -> &mut Shm;
}
#[derive(Debug)]
pub struct Shm {
wl_shm: wl_shm::WlShm,
formats: Vec<wl_shm::Format>,
}
impl From<wl_shm::WlShm> for Shm {
fn from(wl_shm: wl_shm::WlShm) -> Self {
Self { wl_shm, formats: Vec::new() }
}
}
impl Shm {
pub fn bind<State>(globals: &GlobalList, qh: &QueueHandle<State>) -> Result<Shm, BindError>
where
State: Dispatch<wl_shm::WlShm, GlobalData, State> + ShmHandler + 'static,
{
let wl_shm = globals.bind(qh, 1..=1, GlobalData)?;
Ok(Shm { wl_shm, formats: Vec::with_capacity(2) })
}
pub fn wl_shm(&self) -> &wl_shm::WlShm {
&self.wl_shm
}
pub fn formats(&self) -> &[wl_shm::Format] {
&self.formats[..]
}
}
impl ProvidesBoundGlobal<wl_shm::WlShm, 1> for Shm {
fn bound_global(&self) -> Result<wl_shm::WlShm, GlobalError> {
Ok(self.wl_shm.clone())
}
}
#[derive(Debug, thiserror::Error)]
pub enum CreatePoolError {
#[error(transparent)]
Global(#[from] GlobalError),
#[error(transparent)]
Create(#[from] io::Error),
}
#[macro_export]
macro_rules! delegate_shm {
($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
$crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
[
$crate::reexports::client::protocol::wl_shm::WlShm: $crate::globals::GlobalData
] => $crate::shm::Shm
);
};
}
impl<D> Dispatch<wl_shm::WlShm, GlobalData, D> for Shm
where
D: Dispatch<wl_shm::WlShm, GlobalData> + ShmHandler,
{
fn event(
state: &mut D,
_proxy: &wl_shm::WlShm,
event: wl_shm::Event,
_: &GlobalData,
_: &Connection,
_: &QueueHandle<D>,
) {
match event {
wl_shm::Event::Format { format } => {
match format {
WEnum::Value(format) => {
state.shm_state().formats.push(format);
log::debug!(target: "sctk", "supported wl_shm format {:?}", format);
}
WEnum::Unknown(raw) => {
log::debug!(target: "sctk", "Unknown supported wl_shm format {:x}", raw);
}
};
}
_ => unreachable!(),
}
}
}