1
0
Fork 0
forked from wry/wry

autocommit 2022-01-02 15:13:33 CET

This commit is contained in:
Julian Orth 2022-01-02 15:13:33 +01:00
commit d6172b273f
50 changed files with 5807 additions and 0 deletions

136
src/ifs/wl_shm/mod.rs Normal file
View file

@ -0,0 +1,136 @@
mod types;
use crate::globals::{Global, GlobalName};
use crate::ifs::wl_shm_pool::WlShmPool;
use crate::objects::{Interface, Object, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::WlClientData;
use std::rc::Rc;
pub use types::*;
const CREATE_POOL: u32 = 0;
const FORMAT: u32 = 0;
pub struct WlShmGlobal {
name: GlobalName,
}
pub struct WlShmObj {
global: Rc<WlShmGlobal>,
id: ObjectId,
client: Rc<WlClientData>,
}
impl WlShmGlobal {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
async fn bind_(
self: Rc<Self>,
id: ObjectId,
client: &Rc<WlClientData>,
_version: u32,
) -> Result<(), WlShmError> {
let obj = Rc::new(WlShmObj {
global: self,
id,
client: client.clone(),
});
client.attach_client_object(obj.clone())?;
for &format in Format::formats() {
client
.event(Box::new(FormatE {
obj: obj.clone(),
format,
}))
.await?;
}
Ok(())
}
}
impl WlShmObj {
async fn create_pool(&self, parser: WlParser<'_, '_>) -> Result<(), CreatePoolError> {
let create: CreatePool = self.client.parse(self, parser)?;
if create.size < 0 {
return Err(CreatePoolError::NegativeSize);
}
let pool = Rc::new(WlShmPool::new(
create.id,
&self.client,
create.fd,
create.size as usize,
)?);
self.client.attach_client_object(pool)?;
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlShmError> {
match request {
CREATE_POOL => self.create_pool(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Format {
Argb8888,
Xrgb8888,
}
impl Format {
fn uint(self) -> u32 {
match self {
Format::Argb8888 => 0,
Format::Xrgb8888 => 1,
}
}
fn formats() -> &'static [Format] {
&[Format::Argb8888, Format::Xrgb8888]
}
}
bind!(WlShmGlobal);
impl Global for WlShmGlobal {
fn name(&self) -> GlobalName {
self.name
}
fn interface(&self) -> Interface {
Interface::WlShm
}
fn version(&self) -> u32 {
1
}
fn pre_remove(&self) {
unreachable!()
}
}
handle_request!(WlShmObj);
impl Object for WlShmObj {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlShm
}
fn num_requests(&self) -> u32 {
CREATE_POOL + 1
}
}

80
src/ifs/wl_shm/types.rs Normal file
View file

@ -0,0 +1,80 @@
use crate::ifs::wl_shm::{Format, WlShmObj, FORMAT};
use crate::ifs::wl_shm_pool::WlShmPoolError;
use crate::objects::{Object, ObjectError, ObjectId};
use crate::utils::buffd::{WlFormatter, WlParser, WlParserError};
use crate::wl_client::{EventFormatter, RequestParser, WlClientError};
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
use thiserror::Error;
use uapi::OwnedFd;
#[derive(Debug, Error)]
pub enum WlShmError {
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
#[error("Could not process a `create_pool` request")]
CreatePoolError(#[from] CreatePoolError),
}
efrom!(WlShmError, ObjectError, ObjectError);
efrom!(WlShmError, ClientError, WlClientError);
#[derive(Debug, Error)]
pub enum CreatePoolError {
#[error("Parsing failed")]
ParseError(#[source] Box<WlParserError>),
#[error("The passed size is negative")]
NegativeSize,
#[error(transparent)]
WlShmPoolError(Box<WlShmPoolError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(CreatePoolError, ParseError, WlParserError);
efrom!(CreatePoolError, WlShmPoolError, WlShmPoolError);
efrom!(CreatePoolError, ClientError, WlClientError);
pub(super) struct CreatePool {
pub id: ObjectId,
pub fd: OwnedFd,
pub size: i32,
}
impl RequestParser<'_> for CreatePool {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
id: parser.object()?,
fd: parser.fd()?,
size: parser.int()?,
})
}
}
impl Debug for CreatePool {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"create_pool(id: {}, fd: {}, size: {})",
self.id,
self.fd.raw(),
self.size
)
}
}
pub(super) struct FormatE {
pub obj: Rc<WlShmObj>,
pub format: Format,
}
impl EventFormatter for FormatE {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(self.obj.id, FORMAT).uint(self.format.uint());
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for FormatE {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "format(format: {:?})", self.format)
}
}