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

134
src/acceptor.rs Normal file
View file

@ -0,0 +1,134 @@
use crate::event_loop::{EventLoopDispatcher, EventLoopError, EventLoopId};
use crate::state::State;
use crate::wl_client::WlClientError;
use std::rc::Rc;
use thiserror::Error;
use uapi::{c, Errno, OwnedFd};
#[derive(Debug, Error)]
pub enum AcceptorError {
#[error("XDG_RUNTIME_DIR is not set")]
XrdNotSet,
#[error("XDG_RUNTIME_DIR is too long to form a unix socket address")]
XrdTooLong,
#[error("Could not create a wayland socket")]
SocketFailed(#[source] std::io::Error),
#[error("Could not start listening for incoming connections")]
ListenFailed(#[source] std::io::Error),
#[error("The wayland socket is in an error state")]
ErrorEvent,
#[error("Could not accept new connections")]
AcceptFailed(#[source] std::io::Error),
#[error("Could not spawn an event handler for a new connection")]
SpawnFailed(#[source] WlClientError),
#[error("Could not bind the socket to an address")]
BindFailed(#[source] std::io::Error),
#[error("All wayland addresses in the range 0..1000 are already in use")]
AddressesInUse,
#[error("The event loop caused an error")]
EventLoopError(#[from] EventLoopError),
}
pub struct Acceptor {
_unlinker: Unlinker,
id: EventLoopId,
fd: OwnedFd,
global: Rc<State>,
}
struct Unlinker(String);
impl Drop for Unlinker {
fn drop(&mut self) {
let _ = uapi::unlink(self.0.as_str());
}
}
fn socket_path(xrd: &str, id: u32) -> String {
format!("{}/wayland-{}", xrd, id)
}
fn bind_socket(fd: i32, xdr: &str) -> Result<u32, AcceptorError> {
let mut addr: c::sockaddr_un = uapi::pod_zeroed();
addr.sun_family = c::AF_UNIX as _;
for i in 0..1000 {
let path = socket_path(xdr, i);
if path.len() + 1 > addr.sun_path.len() {
return Err(AcceptorError::XrdTooLong);
}
let sun_path = uapi::as_bytes_mut(&mut addr.sun_path[..]);
sun_path[..path.len()].copy_from_slice(path.as_bytes());
sun_path[path.len()] = 0;
match uapi::bind(fd, &addr) {
Ok(()) => return Ok(i),
Err(Errno(c::EADDRINUSE)) => {
log::warn!("Socket {} is already in use", path);
}
Err(e) => return Err(AcceptorError::BindFailed(e.into())),
}
}
Err(AcceptorError::AddressesInUse)
}
impl Acceptor {
pub fn install(global: &Rc<State>) -> Result<(), AcceptorError> {
let xrd = match std::env::var("XDG_RUNTIME_DIR") {
Ok(d) => d,
Err(_) => return Err(AcceptorError::XrdNotSet),
};
let fd = match uapi::socket(
c::AF_UNIX,
c::SOCK_STREAM | c::SOCK_NONBLOCK | c::SOCK_CLOEXEC,
0,
) {
Ok(f) => f,
Err(e) => return Err(AcceptorError::SocketFailed(e.into())),
};
let socket_id = bind_socket(fd.raw(), &xrd)?;
let socket_path = socket_path(&xrd, socket_id);
log::info!("bound to socket {}", socket_path);
let unlinker = Unlinker(socket_path);
if let Err(e) = uapi::listen(fd.raw(), 4096) {
return Err(AcceptorError::ListenFailed(e.into()));
}
let id = global.el.id()?;
let acc = Rc::new(Acceptor {
_unlinker: unlinker,
id,
fd,
global: global.clone(),
});
global.el.insert(id, Some(acc.fd.raw()), c::EPOLLIN, acc)?;
Ok(())
}
}
impl EventLoopDispatcher for Acceptor {
fn dispatch(&self, events: i32) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if events & (c::EPOLLERR | c::EPOLLHUP) != 0 {
return Err(Box::new(AcceptorError::ErrorEvent));
}
loop {
let fd = match uapi::accept4(
self.fd.raw(),
uapi::sockaddr_none_mut(),
c::SOCK_NONBLOCK | c::SOCK_CLOEXEC,
) {
Ok((fd, _)) => fd,
Err(Errno(c::EAGAIN)) => break,
Err(e) => return Err(Box::new(AcceptorError::AcceptFailed(e.into()))),
};
let id = self.global.clients.id();
if let Err(e) = self.global.clients.spawn(id, &self.global, fd) {
return Err(Box::new(AcceptorError::SpawnFailed(e)));
}
}
Ok(())
}
}
impl Drop for Acceptor {
fn drop(&mut self) {
let _ = self.global.el.remove(self.id);
}
}

667
src/async_engine.rs Normal file
View file

@ -0,0 +1,667 @@
pub use crate::async_engine::yield_::Yield;
use crate::event_loop::{EventLoopError, EventLoopRef};
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::numcell::NumCell;
use crate::wheel::{WheelError, WheelRef};
pub use fd::AsyncFd;
use fd::AsyncFdData;
use queue::{DispatchQueue, Dispatcher};
use std::cell::{Cell, RefCell};
use std::future::Future;
use std::rc::Rc;
pub use task::SpawnedFuture;
use thiserror::Error;
pub use timeout::Timeout;
use timeout::TimeoutData;
use uapi::OwnedFd;
#[derive(Debug, Error)]
pub enum AsyncError {
#[error("The timer wheel returned an error: {0}")]
WheelError(#[from] WheelError),
#[error("The event loop caused an error: {0}")]
EventLoopError(#[from] EventLoopError),
}
pub struct AsyncEngine {
wheel: WheelRef,
el: EventLoopRef,
queue: Rc<DispatchQueue>,
fds: CopyHashMap<i32, Rc<AsyncFdData>>,
}
impl AsyncEngine {
pub fn new(el: &EventLoopRef, wheel: &WheelRef) -> Result<Self, AsyncError> {
let queue = Dispatcher::install(el)?;
Ok(Self {
wheel: wheel.clone(),
el: el.clone(),
queue,
fds: CopyHashMap::new(),
})
}
pub fn timeout(&self, ms: u64) -> Result<Timeout, AsyncError> {
let data = Rc::new(TimeoutData {
expired: Cell::new(false),
waker: RefCell::new(None),
});
let id = self.wheel.id()?;
self.wheel.timeout(id, ms, data.clone())?;
Ok(Timeout {
id,
wheel: self.wheel.clone(),
data,
})
}
pub fn spawn<T, F: Future<Output = T> + 'static>(&self, f: F) -> SpawnedFuture<T> {
self.queue.spawn(f)
}
pub fn fd(self: &Rc<Self>, fd: &Rc<OwnedFd>) -> Result<AsyncFd, AsyncError> {
let data = if let Some(afd) = self.fds.get(&fd.raw()) {
afd.ref_count.fetch_add(1);
afd
} else {
let id = self.el.id()?;
let afd = Rc::new(AsyncFdData {
ref_count: NumCell::new(1),
fd: fd.clone(),
id,
el: self.el.clone(),
write_registered: Cell::new(false),
read_registered: Cell::new(false),
readers: RefCell::new(vec![]),
writers: RefCell::new(vec![]),
erroneous: Cell::new(false),
});
self.el.insert(id, Some(fd.raw()), 0, afd.clone())?;
afd
};
Ok(AsyncFd {
engine: self.clone(),
data,
})
}
pub fn yield_now(&self) -> Yield {
Yield {
iteration: self.queue.iteration(),
queue: self.queue.clone(),
}
}
}
mod yield_ {
use crate::async_engine::queue::DispatchQueue;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
pub struct Yield {
pub(super) iteration: u64,
pub(super) queue: Rc<DispatchQueue>,
}
impl Future for Yield {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.queue.iteration() > self.iteration {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
}
mod timeout {
use crate::wheel::{WheelDispatcher, WheelId, WheelRef};
use std::cell::{Cell, RefCell};
use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
pub(super) struct TimeoutData {
pub expired: Cell<bool>,
pub waker: RefCell<Option<Waker>>,
}
impl WheelDispatcher for TimeoutData {
fn dispatch(self: Rc<Self>) -> Result<(), Box<dyn Error + Send + Sync>> {
self.expired.set(true);
if let Some(w) = self.waker.borrow_mut().take() {
w.wake();
}
Ok(())
}
}
pub struct Timeout {
pub(super) id: WheelId,
pub(super) wheel: WheelRef,
pub(super) data: Rc<TimeoutData>,
}
impl Future for Timeout {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.data.expired.get() {
Poll::Ready(())
} else {
*self.data.waker.borrow_mut() = Some(cx.waker().clone());
Poll::Pending
}
}
}
impl Drop for Timeout {
fn drop(&mut self) {
self.wheel.remove(self.id);
}
}
}
mod task {
use crate::async_engine::queue::DispatchQueue;
use std::future::Future;
use std::mem::ManuallyDrop;
use std::pin::Pin;
use std::ptr;
use std::rc::Rc;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
pub struct SpawnedFuture<T: 'static> {
vtable: &'static SpawnedFutureVtable<T>,
data: *mut u8,
}
impl<T> Future for SpawnedFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe { (self.vtable.poll)(self.data, cx) }
}
}
impl<T> Drop for SpawnedFuture<T> {
fn drop(&mut self) {
unsafe {
(self.vtable.drop)(self.data);
}
}
}
struct SpawnedFutureVTableProxy<T, F>(T, F);
impl<T: 'static, F: Future<Output = T>> SpawnedFutureVTableProxy<T, F> {
const VTABLE: &'static SpawnedFutureVtable<T> = &SpawnedFutureVtable {
poll: Self::poll,
drop: Self::drop,
};
unsafe fn poll(data: *mut u8, ctx: &mut Context<'_>) -> Poll<T> {
let task = &mut *(data as *mut Task<T, F>);
if task.state & COMPLETED == 0 {
task.waker = Some(ctx.waker().clone());
Poll::Pending
} else if task.state & EMPTIED == 0 {
task.state |= EMPTIED;
Poll::Ready(ptr::read(&mut *task.data.result))
} else {
panic!("Future polled after it has already been emptied");
}
}
unsafe fn drop(data: *mut u8) {
let task = &mut *(data as *mut Task<T, F>);
task.state |= CANCELLED;
if task.state & RUNNING == 0 {
task.drop_data();
}
task.dec_ref_count();
}
}
struct SpawnedFutureVtable<T> {
poll: unsafe fn(data: *mut u8, ctx: &mut Context<'_>) -> Poll<T>,
drop: unsafe fn(data: *mut u8),
}
union TaskData<T, F: Future<Output = T>> {
result: ManuallyDrop<T>,
future: ManuallyDrop<F>,
}
const RUNNING: usize = 1;
const RUN_AGAIN: usize = 2;
const COMPLETED: usize = 4;
const EMPTIED: usize = 8;
const CANCELLED: usize = 16;
struct Task<T, F: Future<Output = T>> {
ref_count: u64,
state: usize,
data: TaskData<T, F>,
waker: Option<Waker>,
queue: Rc<DispatchQueue>,
}
pub(super) struct Runnable {
data: *mut u8,
run: unsafe fn(data: *mut u8, run: bool),
}
impl Runnable {
pub(super) fn run(self) {
let slf = ManuallyDrop::new(self);
unsafe {
(slf.run)(slf.data, true);
}
}
}
impl Drop for Runnable {
fn drop(&mut self) {
unsafe {
(self.run)(self.data, false);
}
}
}
impl DispatchQueue {
pub(super) fn spawn<T, F: Future<Output = T>>(self: &Rc<Self>, f: F) -> SpawnedFuture<T> {
let mut f = Box::new(Task {
ref_count: 1,
state: 0,
data: TaskData {
future: ManuallyDrop::new(f),
},
waker: None,
queue: self.clone(),
});
unsafe {
f.schedule_run();
}
let f = Box::into_raw(f);
SpawnedFuture {
vtable: &SpawnedFutureVTableProxy::<T, F>::VTABLE,
data: f as _,
}
}
}
impl<T, F: Future<Output = T>> Task<T, F> {
const VTABLE: &'static RawWakerVTable = &RawWakerVTable::new(
Self::waker_clone,
Self::waker_wake,
Self::waker_wake_by_ref,
Self::waker_drop,
);
unsafe fn run_proxy(data: *mut u8, run: bool) {
let task = &mut *(data as *mut Self);
if run {
task.run();
}
task.dec_ref_count();
}
unsafe fn dec_ref_count(&mut self) {
self.ref_count -= 1;
if self.ref_count == 0 {
Box::from_raw(self);
}
}
unsafe fn inc_ref_count(&mut self) {
self.ref_count += 1;
}
unsafe fn waker_clone(data: *const ()) -> RawWaker {
let task = &mut *(data as *mut Self);
task.inc_ref_count();
RawWaker::new(data, Self::VTABLE)
}
unsafe fn waker_wake(data: *const ()) {
Self::waker_wake_by_ref(data);
Self::waker_drop(data);
}
unsafe fn waker_wake_by_ref(data: *const ()) {
let task = &mut *(data as *mut Self);
task.schedule_run();
}
unsafe fn waker_drop(data: *const ()) {
let task = &mut *(data as *mut Self);
task.dec_ref_count();
}
unsafe fn schedule_run(&mut self) {
if self.state & (COMPLETED | CANCELLED) == 0 {
if self.state & RUNNING == 0 {
self.state |= RUNNING;
self.inc_ref_count();
let data = self as *mut _ as _;
self.queue.push(Runnable {
data,
run: Self::run_proxy,
});
} else {
self.state |= RUN_AGAIN;
}
}
}
unsafe fn run(&mut self) {
if self.state & CANCELLED == 0 {
self.inc_ref_count();
let raw_waker = RawWaker::new(self as *const _ as _, &Self::VTABLE);
let waker = Waker::from_raw(raw_waker);
let mut ctx = Context::from_waker(&waker);
if let Poll::Ready(d) = Pin::new_unchecked(&mut *self.data.future).poll(&mut ctx) {
ManuallyDrop::drop(&mut self.data.future);
ptr::write(&mut self.data.result, ManuallyDrop::new(d));
self.state |= COMPLETED;
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
}
self.state &= !RUNNING;
if self.state & CANCELLED != 0 {
self.drop_data();
} else if self.state & RUN_AGAIN != 0 {
self.state &= !RUN_AGAIN;
self.schedule_run()
}
}
unsafe fn drop_data(&mut self) {
if self.state & COMPLETED == 0 {
ManuallyDrop::drop(&mut self.data.future);
} else if self.state & EMPTIED == 0 {
ManuallyDrop::drop(&mut self.data.result);
}
}
}
}
mod queue {
use crate::async_engine::task::Runnable;
use crate::async_engine::AsyncError;
use crate::event_loop::{EventLoopDispatcher, EventLoopId, EventLoopRef};
use crate::utils::numcell::NumCell;
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::error::Error;
use std::mem;
use std::rc::Rc;
pub(super) struct Dispatcher {
queue: Rc<DispatchQueue>,
stash: RefCell<VecDeque<Runnable>>,
}
impl Dispatcher {
pub fn install(el: &EventLoopRef) -> Result<Rc<DispatchQueue>, AsyncError> {
let id = el.id()?;
let queue = Rc::new(DispatchQueue {
id,
el: el.clone(),
dispatch_scheduled: Cell::new(false),
queue: RefCell::new(Default::default()),
iteration: Default::default(),
});
let slf = Rc::new(Dispatcher {
queue: queue.clone(),
stash: RefCell::new(Default::default()),
});
el.insert(id, None, 0, slf)?;
Ok(queue)
}
}
impl EventLoopDispatcher for Dispatcher {
fn dispatch(&self, _events: i32) -> Result<(), Box<dyn Error + Send + Sync>> {
loop {
self.queue.iteration.fetch_add(1);
let mut stash = self.stash.borrow_mut();
mem::swap(&mut *stash, &mut *self.queue.queue.borrow_mut());
if stash.is_empty() {
break;
}
for runnable in stash.drain(..) {
runnable.run();
}
}
self.queue.dispatch_scheduled.set(false);
Ok(())
}
}
impl Drop for Dispatcher {
fn drop(&mut self) {
let _ = self.queue.el.remove(self.queue.id);
mem::take(&mut *self.queue.queue.borrow_mut());
}
}
pub(super) struct DispatchQueue {
dispatch_scheduled: Cell<bool>,
id: EventLoopId,
el: EventLoopRef,
queue: RefCell<VecDeque<Runnable>>,
iteration: NumCell<u64>,
}
impl DispatchQueue {
pub fn push(&self, runnable: Runnable) {
self.queue.borrow_mut().push_back(runnable);
if !self.dispatch_scheduled.get() {
let _ = self.el.schedule(self.id);
self.dispatch_scheduled.set(true);
}
}
pub fn iteration(&self) -> u64 {
self.iteration.load()
}
}
}
mod fd {
use crate::async_engine::{AsyncEngine, AsyncError};
use crate::event_loop::{EventLoopDispatcher, EventLoopError, EventLoopId, EventLoopRef};
use crate::utils::numcell::NumCell;
use std::cell::{Cell, RefCell};
use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
use uapi::{c, OwnedFd};
type Queue = RefCell<Vec<(Waker, Rc<Cell<bool>>)>>;
pub(super) struct AsyncFdData {
pub(super) ref_count: NumCell<u64>,
pub(super) fd: Rc<OwnedFd>,
pub(super) id: EventLoopId,
pub(super) el: EventLoopRef,
pub(super) write_registered: Cell<bool>,
pub(super) read_registered: Cell<bool>,
pub(super) readers: Queue,
pub(super) writers: Queue,
pub(super) erroneous: Cell<bool>,
}
impl AsyncFdData {
fn update_interests(&self) -> Result<(), EventLoopError> {
let mut events = 0;
if self.write_registered.get() {
events |= c::EPOLLOUT;
}
if self.read_registered.get() {
events |= c::EPOLLIN;
}
let res = self.el.modify(self.id, events);
if res.is_err() {
self.erroneous.set(true);
let _ = self.el.remove(self.id);
}
res
}
fn poll(
&self,
woken: &Rc<Cell<bool>>,
cx: &mut Context<'_>,
registered: impl Fn(&AsyncFdData) -> &Cell<bool>,
queue: impl Fn(&AsyncFdData) -> &Queue,
) -> Poll<Result<(), AsyncError>> {
if woken.get() || self.erroneous.get() {
return Poll::Ready(Ok(()));
}
if !registered(self).get() {
registered(self).set(true);
if let Err(e) = self.update_interests() {
return Poll::Ready(Err(AsyncError::EventLoopError(e)));
}
}
queue(self)
.borrow_mut()
.push((cx.waker().clone(), woken.clone()));
Poll::Pending
}
}
impl EventLoopDispatcher for AsyncFdData {
fn dispatch(&self, events: i32) -> Result<(), Box<dyn Error + Send + Sync>> {
if events & (c::EPOLLERR | c::EPOLLHUP) != 0 {
self.erroneous.set(true);
if let Err(e) = self.el.remove(self.id) {
return Err(Box::new(e));
}
}
let mut woke_any = false;
if events & c::EPOLLIN != 0 || self.erroneous.get() {
let mut readers = self.readers.borrow_mut();
woke_any |= !readers.is_empty();
for (waker, woken) in readers.drain(..) {
woken.set(true);
waker.wake();
}
}
if events & c::EPOLLOUT != 0 || self.erroneous.get() {
let mut writers = self.writers.borrow_mut();
woke_any |= !writers.is_empty();
for (waker, woken) in writers.drain(..) {
woken.set(true);
waker.wake();
}
}
if !woke_any && !self.erroneous.get() {
self.read_registered.set(false);
self.write_registered.set(false);
if let Err(e) = self.update_interests() {
return Err(Box::new(e));
}
}
Ok(())
}
}
impl Drop for AsyncFdData {
fn drop(&mut self) {
let _ = self.el.remove(self.id);
}
}
pub struct AsyncFd {
pub(super) engine: Rc<AsyncEngine>,
pub(super) data: Rc<AsyncFdData>,
}
impl Clone for AsyncFd {
fn clone(&self) -> Self {
self.data.ref_count.fetch_add(1);
Self {
engine: self.engine.clone(),
data: self.data.clone(),
}
}
}
impl Drop for AsyncFd {
fn drop(&mut self) {
if self.data.ref_count.fetch_sub(1) == 1 {
self.engine.fds.remove(&self.data.fd.raw());
}
}
}
impl AsyncFd {
pub fn raw(&self) -> i32 {
self.data.fd.raw()
}
pub fn eng(&self) -> &Rc<AsyncEngine> {
&self.engine
}
pub fn readable(&self) -> AsyncFdReadable {
AsyncFdReadable {
fd: self,
woken: Rc::new(Cell::new(false)),
}
}
pub fn writable(&self) -> AsyncFdWritable {
AsyncFdWritable {
fd: self,
woken: Rc::new(Cell::new(false)),
}
}
}
pub struct AsyncFdReadable<'a> {
fd: &'a AsyncFd,
woken: Rc<Cell<bool>>,
}
impl<'a> Future for AsyncFdReadable<'a> {
type Output = Result<(), AsyncError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let data = &self.fd.data;
data.poll(&self.woken, cx, |d| &d.read_registered, |d| &d.readers)
}
}
pub struct AsyncFdWritable<'a> {
fd: &'a AsyncFd,
woken: Rc<Cell<bool>>,
}
impl<'a> Future for AsyncFdWritable<'a> {
type Output = Result<(), AsyncError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let data = &self.fd.data;
data.poll(&self.woken, cx, |d| &d.write_registered, |d| &d.writers)
}
}
}

149
src/clientmem.rs Normal file
View file

@ -0,0 +1,149 @@
use std::cell::{Cell, UnsafeCell};
use std::mem::MaybeUninit;
use std::sync::atomic::{compiler_fence, Ordering};
use std::{mem, ptr};
use thiserror::Error;
use uapi::c;
use uapi::c::raise;
#[derive(Debug, Error)]
pub enum ClientMemError {
#[error("Could not install the sigbus handler")]
SigactionFailed(#[source] std::io::Error),
#[error("A SIGBUS occurred while accessing mapped memory")]
Sigbus,
#[error("mmap failed")]
MmapFailed(#[source] std::io::Error),
}
pub struct ClientMem {
failed: Cell<bool>,
sigbus_impossible: bool,
data: *mut [Cell<u8>],
}
impl ClientMem {
pub fn new(fd: i32, len: usize) -> Result<Self, ClientMemError> {
let mut sigbus_impossible = false;
if let Ok(seals) = uapi::fcntl_get_seals(fd) {
if seals & c::F_SEAL_SHRINK != 0 {
if let Ok(stat) = uapi::fstat(fd) {
sigbus_impossible = stat.st_size as u64 >= len as u64;
}
}
}
let data = unsafe {
let data = c::mmap64(
ptr::null_mut(),
len,
c::PROT_READ | c::PROT_WRITE,
c::MAP_SHARED,
fd,
0,
);
if data == c::MAP_FAILED {
return Err(ClientMemError::MmapFailed(uapi::Errno::default().into()));
}
std::slice::from_raw_parts_mut(data as *mut Cell<u8>, len)
};
Ok(Self {
failed: Cell::new(false),
sigbus_impossible,
data,
})
}
pub fn access<T, F: FnOnce(&[Cell<u8>]) -> T>(&self, f: F) -> Result<T, ClientMemError> {
unsafe {
if self.sigbus_impossible {
return Ok(f(&mut *self.data));
}
MEM.with(|m| {
let mref = MemRef {
mem: self,
outer: *m.get(),
};
*m.get() = &mref;
compiler_fence(Ordering::SeqCst);
let res = f(&mut *self.data);
*m.get() = mref.outer;
compiler_fence(Ordering::SeqCst);
match self.failed.get() {
true => Err(ClientMemError::Sigbus),
_ => Ok(res),
}
})
}
}
pub fn len(&self) -> usize {
unsafe { (*self.data).len() }
}
}
impl Drop for ClientMem {
fn drop(&mut self) {
unsafe {
c::munmap(self.data as _, self.len());
}
}
}
struct MemRef {
mem: *const ClientMem,
outer: *const MemRef,
}
thread_local! {
static MEM: UnsafeCell<*const MemRef> = UnsafeCell::new(ptr::null());
}
unsafe fn kill() -> ! {
c::signal(c::SIGBUS, c::SIG_DFL);
raise(c::SIGBUS);
unreachable!();
}
unsafe extern "C" fn sigbus(sig: i32, info: &c::siginfo_t, _ucontext: *mut c::c_void) {
assert_eq!(sig, c::SIGBUS);
let mut memr_ptr = MEM.with(|m| ptr::read(m.get()));
while !memr_ptr.is_null() {
let memr = &*memr_ptr;
let mem = &*memr.mem;
let lo = mem.data as *mut u8 as usize;
let hi = lo + mem.len();
let fault_addr = info.si_addr() as usize;
if fault_addr < lo || fault_addr >= hi {
memr_ptr = memr.outer;
continue;
}
let res = c::mmap64(
lo as _,
hi - lo,
c::PROT_WRITE | c::PROT_READ,
c::MAP_ANONYMOUS | c::MAP_PRIVATE,
-1,
0,
);
if res == c::MAP_FAILED {
kill();
}
mem.failed.set(true);
return;
}
kill();
}
pub fn init() -> Result<(), ClientMemError> {
unsafe {
let mut action: c::sigaction = MaybeUninit::zeroed().assume_init();
action.sa_sigaction =
mem::transmute(sigbus as unsafe extern "C" fn(i32, &c::siginfo_t, *mut c::c_void));
action.sa_flags = c::SA_NODEFER | c::SA_SIGINFO;
let res = c::sigaction(c::SIGBUS, &action, ptr::null_mut());
match uapi::map_err!(res) {
Ok(_) => Ok(()),
Err(e) => Err(ClientMemError::SigactionFailed(e.into())),
}
}
}

246
src/event_loop.rs Normal file
View file

@ -0,0 +1,246 @@
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::numcell::NumCell;
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::rc::{Rc, Weak};
use thiserror::Error;
use uapi::{c, Errno, OwnedFd};
#[derive(Debug, Error)]
pub enum EventLoopError {
#[error("Could not create an epoll fd: {0}")]
CreateFailed(std::io::Error),
#[error("epoll_wait failed: {0}")]
WaitFailed(std::io::Error),
#[error("A dispatcher returned a fatal error: {0}")]
DispatcherError(Box<dyn std::error::Error + Send + Sync>),
#[error("Could not insert an fd to wait on: {0}")]
InsertFailed(std::io::Error),
#[error("Could not modify an fd to wait on: {0}")]
ModifyFailed(std::io::Error),
#[error("Could not remove an fd to wait on: {0}")]
RemoveFailed(std::io::Error),
#[error("Entry is not registered")]
NoEntry,
#[error("Event loop is already destroyed")]
Destroyed,
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct EventLoopId(u64);
pub trait EventLoopDispatcher {
fn dispatch(&self, events: i32) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
}
#[derive(Clone)]
struct Entry {
fd: Option<i32>,
dispatcher: Rc<dyn EventLoopDispatcher>,
}
struct EventLoopData {
epoll: OwnedFd,
run: Cell<bool>,
next_id: NumCell<u64>,
entries: CopyHashMap<u64, Entry>,
scheduled: RefCell<VecDeque<u64>>,
}
pub struct EventLoop {
data: Rc<EventLoopData>,
}
#[derive(Clone)]
pub struct EventLoopRef {
data: Weak<EventLoopData>,
}
impl EventLoopData {
fn new() -> Result<Self, EventLoopError> {
let epoll = match uapi::epoll_create1(c::EPOLL_CLOEXEC) {
Ok(e) => e,
Err(e) => return Err(EventLoopError::CreateFailed(e.into())),
};
Ok(Self {
epoll,
run: Cell::new(true),
next_id: NumCell::new(1),
entries: CopyHashMap::new(),
scheduled: RefCell::new(Default::default()),
})
}
fn id(&self) -> EventLoopId {
EventLoopId(self.next_id.fetch_add(1))
}
fn stop(&self) {
self.run.set(false);
}
fn insert(
&self,
id: EventLoopId,
fd: Option<i32>,
events: i32,
dispatcher: Rc<dyn EventLoopDispatcher>,
) -> Result<(), EventLoopError> {
let id = id.0;
if let Some(fd) = fd {
let event = c::epoll_event {
events: events as _,
u64: id,
};
if let Err(e) = uapi::epoll_ctl(self.epoll.raw(), c::EPOLL_CTL_ADD, fd, Some(&event)) {
return Err(EventLoopError::InsertFailed(e.into()));
}
}
self.entries.set(id, Entry { fd, dispatcher });
Ok(())
}
fn modify(&self, id: EventLoopId, events: i32) -> Result<(), EventLoopError> {
let id = id.0;
let entry = match self.entries.get(&id) {
Some(e) => e,
None => return Err(EventLoopError::NoEntry),
};
if let Some(fd) = entry.fd {
let event = c::epoll_event {
events: events as _,
u64: id,
};
if let Err(e) = uapi::epoll_ctl(self.epoll.raw(), c::EPOLL_CTL_MOD, fd, Some(&event)) {
return Err(EventLoopError::ModifyFailed(e.into()));
}
}
Ok(())
}
fn remove(&self, id: EventLoopId) -> Result<(), EventLoopError> {
let id = id.0;
let entry = match self.entries.remove(&id) {
Some(e) => e,
None => return Err(EventLoopError::NoEntry),
};
if let Some(fd) = entry.fd {
if let Err(e) = uapi::epoll_ctl(self.epoll.raw(), c::EPOLL_CTL_DEL, fd, None) {
return Err(EventLoopError::RemoveFailed(e.into()));
}
}
Ok(())
}
fn schedule(&self, id: EventLoopId) {
self.scheduled.borrow_mut().push_back(id.0);
}
fn run(&self) -> Result<(), EventLoopError> {
let mut buf = [c::epoll_event { events: 0, u64: 0 }; 16];
while self.run.get() {
while let Some(id) = self.scheduled.borrow_mut().pop_front() {
if !self.run.get() {
break;
}
if let Some(entry) = self.entries.get(&id) {
if let Err(e) = entry.dispatcher.dispatch(0) {
return Err(EventLoopError::DispatcherError(e));
}
}
}
let num = match uapi::epoll_wait(self.epoll.raw(), &mut buf, -1) {
Ok(n) => n,
Err(Errno(c::EINTR)) => continue,
Err(e) => return Err(EventLoopError::WaitFailed(e.into())),
};
for event in &buf[..num] {
if !self.run.get() {
break;
}
let id = event.u64;
let entry = match self.entries.get(&id) {
Some(d) => d,
None => {
log::warn!(
"Client {} created an event but has already been removed",
id,
);
continue;
}
};
if let Err(e) = entry.dispatcher.dispatch(event.events as i32) {
return Err(EventLoopError::DispatcherError(e));
}
}
}
Ok(())
}
}
impl EventLoop {
pub fn new() -> Result<Self, EventLoopError> {
Ok(Self {
data: Rc::new(EventLoopData::new()?),
})
}
pub fn to_ref(&self) -> EventLoopRef {
EventLoopRef {
data: Rc::downgrade(&self.data),
}
}
pub fn run(&self) -> Result<(), EventLoopError> {
self.data.run()
}
}
impl EventLoopRef {
pub fn id(&self) -> Result<EventLoopId, EventLoopError> {
match self.data.upgrade() {
Some(d) => Ok(d.id()),
None => Err(EventLoopError::Destroyed),
}
}
pub fn stop(&self) {
if let Some(d) = self.data.upgrade() {
d.stop();
}
}
pub fn insert(
&self,
id: EventLoopId,
fd: Option<i32>,
events: i32,
dispatcher: Rc<dyn EventLoopDispatcher>,
) -> Result<(), EventLoopError> {
match self.data.upgrade() {
Some(d) => d.insert(id, fd, events, dispatcher),
None => Err(EventLoopError::Destroyed),
}
}
pub fn modify(&self, id: EventLoopId, events: i32) -> Result<(), EventLoopError> {
match self.data.upgrade() {
Some(d) => d.modify(id, events),
None => Err(EventLoopError::Destroyed),
}
}
pub fn remove(&self, id: EventLoopId) -> Result<(), EventLoopError> {
match self.data.upgrade() {
Some(d) => d.remove(id),
None => Err(EventLoopError::Destroyed),
}
}
pub fn schedule(&self, id: EventLoopId) -> Result<(), EventLoopError> {
match self.data.upgrade() {
Some(d) => Ok(d.schedule(id)),
None => Err(EventLoopError::Destroyed),
}
}
}

164
src/globals.rs Normal file
View file

@ -0,0 +1,164 @@
use crate::ifs::wl_compositor::WlCompositorError;
use crate::ifs::wl_registry::WlRegistry;
use crate::ifs::wl_shm::WlShmError;
use crate::ifs::wl_subcompositor::WlSubcompositorError;
use crate::ifs::xdg_wm_base::XdgWmBaseError;
use crate::objects::{Interface, ObjectId};
use crate::utils::copyhashmap::CopyHashMap;
use crate::wl_client::{DynEventFormatter, WlClientData, WlClientError};
use crate::{NumCell, State};
use ahash::AHashSet;
use std::fmt::{Display, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum GlobalError {
#[error("The requested global {0} does not exist")]
GlobalDoesNotExist(GlobalName),
#[error("An error occurred while trying to send all globals via a new registry")]
SendAllError(#[source] Box<WlClientError>),
#[error("An error occurred in a wl_compositor")]
WlCompositorError(#[source] Box<WlCompositorError>),
#[error("An error occurred in a wl_shm")]
WlShmError(#[source] Box<WlShmError>),
#[error("An error occurred in a wl_subcompositor")]
WlSubcompositorError(#[source] Box<WlSubcompositorError>),
#[error("An error occurred in a xdg_wm_base")]
XdgWmBaseError(#[source] Box<XdgWmBaseError>),
}
efrom!(GlobalError, WlCompositorError, WlCompositorError);
efrom!(GlobalError, WlShmError, WlShmError);
efrom!(GlobalError, WlSubcompositorError, WlSubcompositorError);
efrom!(GlobalError, XdgWmBaseError, XdgWmBaseError);
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalName(u32);
impl GlobalName {
pub fn from_raw(id: u32) -> Self {
Self(id)
}
pub fn raw(self) -> u32 {
self.0
}
}
impl Display for GlobalName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
pub trait GlobalBind {
fn bind<'a>(
self: Rc<Self>,
client: &'a Rc<WlClientData>,
id: ObjectId,
version: u32,
) -> Pin<Box<dyn Future<Output = Result<(), GlobalError>> + 'a>>;
}
pub trait Global: GlobalBind {
fn name(&self) -> GlobalName;
fn interface(&self) -> Interface;
fn version(&self) -> u32;
fn pre_remove(&self);
}
pub struct Globals {
next_name: NumCell<u32>,
registry: CopyHashMap<GlobalName, Rc<dyn Global>>,
}
impl Globals {
pub fn new() -> Self {
Self {
next_name: NumCell::new(1),
registry: CopyHashMap::new(),
}
}
pub fn name(&self) -> GlobalName {
let id = self.next_name.fetch_add(1);
if id == 0 {
panic!("Global names overflowed");
}
GlobalName(id)
}
pub fn insert_no_broadcast<'a>(&'a self, global: Rc<dyn Global>) {
self.insert_no_broadcast_(&global);
}
fn insert_no_broadcast_<'a>(&'a self, global: &Rc<dyn Global>) {
self.registry.set(global.name(), global.clone());
}
pub async fn insert<'a>(&'a self, state: &'a State, global: Rc<dyn Global>) {
self.insert_no_broadcast_(&global);
self.broadcast(state, |r| r.global(&global)).await;
}
pub fn get(&self, name: GlobalName) -> Result<Rc<dyn Global>, GlobalError> {
self.take(name, false)
}
pub async fn remove(
&self,
state: &State,
name: GlobalName,
) -> Result<Rc<dyn Global>, GlobalError> {
let global = self.take(name, true)?;
global.pre_remove();
self.broadcast(state, |r| r.global_remove(name)).await;
Ok(global)
}
pub async fn notify_all(
&self,
client: &WlClientData,
registry: &Rc<WlRegistry>,
) -> Result<(), GlobalError> {
let globals = self.registry.lock();
for global in globals.values() {
if let Err(e) = client.event(registry.global(global)).await {
return Err(GlobalError::SendAllError(Box::new(e)));
}
}
Ok(())
}
async fn broadcast<F: Fn(&Rc<WlRegistry>) -> DynEventFormatter>(&self, state: &State, f: F) {
let mut clients_to_check = AHashSet::new();
state.clients.broadcast(|c| {
let registries = c.objects.registries();
for registry in registries.values() {
if c.event_locked(f(registry)) {
clients_to_check.insert(c.id);
}
}
});
for client in clients_to_check.drain() {
if let Ok(c) = state.clients.get(client) {
let _ = c.check_queue_size().await;
}
}
}
fn take(&self, name: GlobalName, remove: bool) -> Result<Rc<dyn Global>, GlobalError> {
let res = if remove {
self.registry.remove(&name)
} else {
self.registry.get(&name)
};
match res {
Some(g) => Ok(g),
None => Err(GlobalError::GlobalDoesNotExist(name)),
}
}
}

10
src/ifs/mod.rs Normal file
View file

@ -0,0 +1,10 @@
pub mod wl_callback;
pub mod wl_compositor;
pub mod wl_display;
pub mod wl_region;
pub mod wl_registry;
pub mod wl_shm;
pub mod wl_shm_pool;
pub mod wl_subcompositor;
pub mod wl_surface;
pub mod xdg_wm_base;

View file

@ -0,0 +1,47 @@
mod types;
use crate::objects::{Interface, Object, ObjectError, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::DynEventFormatter;
use std::rc::Rc;
use types::*;
const DONE: u32 = 0;
pub struct WlCallback {
id: ObjectId,
}
impl WlCallback {
pub fn new(id: ObjectId) -> Self {
Self { id }
}
pub fn done(self: &Rc<Self>) -> DynEventFormatter {
Box::new(Done { obj: self.clone() })
}
async fn handle_request_(
&self,
_request: u32,
_parser: WlParser<'_, '_>,
) -> Result<(), ObjectError> {
unreachable!();
}
}
handle_request!(WlCallback);
impl Object for WlCallback {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlCallback
}
fn num_requests(&self) -> u32 {
0
}
}

View file

@ -0,0 +1,23 @@
use crate::ifs::wl_callback::{WlCallback, DONE};
use crate::objects::Object;
use crate::utils::buffd::WlFormatter;
use crate::wl_client::EventFormatter;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
pub(super) struct Done {
pub obj: Rc<WlCallback>,
}
impl EventFormatter for Done {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(self.obj.id, DONE).uint(0);
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for Done {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "done(callback_data: 0)")
}
}

View file

@ -0,0 +1,111 @@
mod types;
use crate::globals::{Global, GlobalName};
use crate::ifs::wl_surface::WlSurface;
use crate::objects::{Interface, Object, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::WlClientData;
use std::rc::Rc;
pub use types::*;
use crate::ifs::wl_region::WlRegion;
const CREATE_SURFACE: u32 = 0;
const CREATE_REGION: u32 = 1;
pub struct WlCompositorGlobal {
name: GlobalName,
}
pub struct WlCompositorObj {
global: Rc<WlCompositorGlobal>,
id: ObjectId,
client: Rc<WlClientData>,
version: u32,
}
impl WlCompositorGlobal {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
async fn bind_(
self: Rc<Self>,
id: ObjectId,
client: &Rc<WlClientData>,
version: u32,
) -> Result<(), WlCompositorError> {
let obj = Rc::new(WlCompositorObj {
global: self,
id,
client: client.clone(),
version,
});
client.attach_client_object(obj)?;
Ok(())
}
}
impl WlCompositorObj {
async fn create_surface(&self, parser: WlParser<'_, '_>) -> Result<(), CreateSurfaceError> {
let surface: CreateSurface = self.client.parse(self, parser)?;
let surface = Rc::new(WlSurface::new(surface.id, &self.client));
self.client.attach_client_object(surface)?;
Ok(())
}
async fn create_region(&self, parser: WlParser<'_, '_>) -> Result<(), CreateRegionError> {
let region: CreateRegion = self.client.parse(self, parser)?;
let region = Rc::new(WlRegion::new(region.id, &self.client));
self.client.attach_client_object(region)?;
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlCompositorError> {
match request {
CREATE_SURFACE => self.create_surface(parser).await?,
CREATE_REGION => self.create_region(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
bind!(WlCompositorGlobal);
impl Global for WlCompositorGlobal {
fn name(&self) -> GlobalName {
self.name
}
fn interface(&self) -> Interface {
Interface::WlCompositor
}
fn version(&self) -> u32 {
4
}
fn pre_remove(&self) {
unreachable!()
}
}
handle_request!(WlCompositorObj);
impl Object for WlCompositorObj {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlCompositor
}
fn num_requests(&self) -> u32 {
CREATE_REGION + 1
}
}

View file

@ -0,0 +1,76 @@
use crate::objects::{ObjectError, ObjectId};
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::{RequestParser, WlClientError};
use std::fmt::{Debug, Formatter};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlCompositorError {
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
#[error("Could not process `create_surface` request")]
CreateSurfaceError(#[source] Box<CreateSurfaceError>),
#[error("Could not process `create_region` request")]
CreateRegionError(#[source] Box<CreateRegionError>),
}
efrom!(WlCompositorError, ObjectError, ObjectError);
efrom!(WlCompositorError, ClientError, WlClientError);
efrom!(WlCompositorError, CreateSurfaceError, CreateSurfaceError);
efrom!(WlCompositorError, CreateRegionError, CreateRegionError);
#[derive(Debug, Error)]
pub enum CreateSurfaceError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(CreateSurfaceError, ParseFailed, WlParserError);
efrom!(CreateSurfaceError, ClientError, WlClientError);
#[derive(Debug, Error)]
pub enum CreateRegionError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(CreateRegionError, ParseFailed, WlParserError);
efrom!(CreateRegionError, ClientError, WlClientError);
pub(super) struct CreateSurface {
pub id: ObjectId,
}
impl RequestParser<'_> for CreateSurface {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
id: parser.object()?,
})
}
}
impl Debug for CreateSurface {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "create_surface(id: {})", self.id)
}
}
pub(super) struct CreateRegion {
pub id: ObjectId,
}
impl RequestParser<'_> for CreateRegion {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
id: parser.object()?,
})
}
}
impl Debug for CreateRegion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "create_region(id: {})", self.id)
}
}

130
src/ifs/wl_display/mod.rs Normal file
View file

@ -0,0 +1,130 @@
mod types;
use crate::ifs::wl_callback::WlCallback;
use crate::ifs::wl_registry::WlRegistry;
use crate::objects::{Interface, Object, ObjectError, ObjectId, WL_DISPLAY_ID};
use crate::utils::buffd::WlParser;
use crate::wl_client::{DynEventFormatter, WlClientData};
use std::rc::Rc;
pub use types::*;
const SYNC: u32 = 0;
const GET_REGISTRY: u32 = 1;
const ERROR: u32 = 0;
const DELETE_ID: u32 = 1;
const INVALID_OBJECT: u32 = 0;
const INVALID_METHOD: u32 = 1;
const NO_MEMORY: u32 = 2;
const IMPLEMENTATION: u32 = 3;
pub struct WlDisplay {
client: Rc<WlClientData>,
}
impl WlDisplay {
pub fn new(client: &Rc<WlClientData>) -> Self {
Self {
client: client.clone(),
}
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlDisplayError> {
match request {
SYNC => self.sync(parser).await?,
GET_REGISTRY => self.get_registry(parser).await?,
_ => unreachable!(),
}
Ok(())
}
async fn sync(&self, parser: WlParser<'_, '_>) -> Result<(), SyncError> {
let sync: Sync = self.client.parse(self, parser)?;
let cb = Rc::new(WlCallback::new(sync.callback));
self.client.attach_client_object(cb.clone())?;
self.client.event(cb.done()).await?;
self.client
.objects
.remove_obj(&self.client, cb.id())
.await?;
Ok(())
}
async fn get_registry(&self, parser: WlParser<'_, '_>) -> Result<(), GetRegistryError> {
let gr: GetRegistry = self.client.parse(self, parser)?;
let registry = Rc::new(WlRegistry::new(gr.registry, &self.client));
self.client.attach_client_object(registry.clone())?;
self.client
.state
.globals
.notify_all(&self.client, &registry)
.await?;
Ok(())
}
fn error(
self: &Rc<Self>,
object_id: ObjectId,
code: u32,
message: String,
) -> DynEventFormatter {
Box::new(Error {
obj: self.clone(),
object_id,
code,
message,
})
}
pub fn invalid_request(self: &Rc<Self>, obj: &dyn Object, request: u32) -> DynEventFormatter {
let id = obj.id();
let msg = format!(
"Object {} of type {} has no method {}",
id,
obj.interface().name(),
request
);
self.error(id, INVALID_METHOD, msg)
}
pub fn invalid_object(self: &Rc<Self>, id: ObjectId) -> DynEventFormatter {
let msg = format!("Object {} does not exist", id,);
self.error(id, INVALID_OBJECT, msg)
}
pub fn implementation_error(self: &Rc<Self>, msg: String) -> DynEventFormatter {
self.error(WL_DISPLAY_ID, IMPLEMENTATION, msg)
}
pub fn delete_id(self: &Rc<Self>, id: ObjectId) -> DynEventFormatter {
Box::new(DeleteId {
obj: self.clone(),
id,
})
}
}
handle_request!(WlDisplay);
impl Object for WlDisplay {
fn id(&self) -> ObjectId {
WL_DISPLAY_ID
}
fn interface(&self) -> Interface {
Interface::WlDisplay
}
fn num_requests(&self) -> u32 {
GET_REGISTRY + 1
}
fn into_display(self: Rc<Self>) -> Result<Rc<WlDisplay>, ObjectError> {
Ok(self)
}
}

127
src/ifs/wl_display/types.rs Normal file
View file

@ -0,0 +1,127 @@
use crate::globals::GlobalError;
use crate::ifs::wl_display::{WlDisplay, DELETE_ID, ERROR};
use crate::objects::{Object, ObjectError, ObjectId, WL_DISPLAY_ID};
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;
#[derive(Debug, Error)]
pub enum WlDisplayError {
#[error("Could not process a get_registry request")]
GetRegistry(#[source] Box<GetRegistryError>),
#[error("A client error occurred")]
SyncError(#[source] Box<SyncError>),
}
efrom!(WlDisplayError, GetRegistry, GetRegistryError);
efrom!(WlDisplayError, SyncError, SyncError);
#[derive(Debug, Error)]
pub enum GetRegistryError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error("An object error occurred")]
ObjectError(#[source] Box<ObjectError>),
#[error("An object error occurred")]
ClientError(#[source] Box<WlClientError>),
#[error("An error occurred while processing globals")]
GlobalError(#[source] Box<GlobalError>),
}
efrom!(GetRegistryError, ParseFailed, WlParserError);
efrom!(GetRegistryError, ObjectError, ObjectError);
efrom!(GetRegistryError, GlobalError, GlobalError);
efrom!(GetRegistryError, ClientError, WlClientError);
#[derive(Debug, Error)]
pub enum SyncError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error("An object error occurred")]
ObjectError(#[source] Box<ObjectError>),
#[error("A client error occurred")]
ClientError(#[source] Box<WlClientError>),
}
efrom!(SyncError, ParseFailed, WlParserError);
efrom!(SyncError, ObjectError, ObjectError);
efrom!(SyncError, ClientError, WlClientError);
pub(super) struct GetRegistry {
pub registry: ObjectId,
}
impl RequestParser<'_> for GetRegistry {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
registry: parser.object()?,
})
}
}
impl Debug for GetRegistry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "get_registry(registry: {})", self.registry)
}
}
pub(super) struct Sync {
pub callback: ObjectId,
}
impl RequestParser<'_> for Sync {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
callback: parser.object()?,
})
}
}
impl Debug for Sync {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "sync(callback: {})", self.callback)
}
}
pub(super) struct DeleteId {
pub obj: Rc<WlDisplay>,
pub id: ObjectId,
}
impl EventFormatter for DeleteId {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(WL_DISPLAY_ID, DELETE_ID).object(self.id);
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for DeleteId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "delete_id(id: {})", self.id)
}
}
pub(super) struct Error {
pub obj: Rc<WlDisplay>,
pub object_id: ObjectId,
pub code: u32,
pub message: String,
}
impl EventFormatter for Error {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(WL_DISPLAY_ID, ERROR)
.object(self.object_id)
.uint(self.code)
.string(&self.message);
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"error(object_id: {}, code: {}, message: {:?})",
self.object_id, self.code, self.message
)
}
}

104
src/ifs/wl_region/mod.rs Normal file
View file

@ -0,0 +1,104 @@
mod types;
use crate::objects::{Interface, Object, ObjectError, ObjectId};
use crate::pixman::Region;
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::{RequestParser, WlClientData};
use std::cell::RefCell;
use std::rc::Rc;
pub use types::*;
use crate::ifs::wl_display::WlDisplay;
const DESTROY: u32 = 0;
const ADD: u32 = 1;
const SUBTRACT: u32 = 2;
pub struct WlRegion {
id: ObjectId,
client: Rc<WlClientData>,
rect: RefCell<Region>,
}
impl WlRegion {
pub fn new(id: ObjectId, client: &Rc<WlClientData>) -> Self {
Self {
id,
client: client.clone(),
rect: RefCell::new(Region::new()),
}
}
pub fn region(&self) -> Region {
self.rect.borrow().clone()
}
async fn destroy(&self, parser: WlParser<'_, '_>) -> Result<(), DestroyError> {
let _destroy: Destroy = self.client.parse(self, parser)?;
self.client.objects.remove_obj(&self.client, self.id).await?;
Ok(())
}
async fn add(&self, parser: WlParser<'_, '_>) -> Result<(), AddError> {
let add: Add = self.client.parse(self, parser)?;
if add.width < 0 || add.height < 0 {
return Err(AddError::NegativeExtents);
}
let mut rect = self.rect.borrow_mut();
*rect = rect.add(&Region::rect(add.x, add.y, add.width as _, add.height as _));
Ok(())
}
async fn subtract(&self, parser: WlParser<'_, '_>) -> Result<(), SubtractError> {
let subtract: Subtract = self.client.parse(self, parser)?;
if subtract.width < 0 || subtract.height < 0 {
return Err(SubtractError::NegativeExtents);
}
let mut rect = self.rect.borrow_mut();
*rect = rect.subtract(&Region::rect(
subtract.x,
subtract.y,
subtract.width as _,
subtract.height as _,
));
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlRegionError> {
match request {
DESTROY => self.destroy(parser).await?,
ADD => self.add(parser).await?,
SUBTRACT => self.subtract(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
handle_request!(WlRegion);
impl Object for WlRegion {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlRegion
}
fn num_requests(&self) -> u32 {
SUBTRACT + 1
}
fn post_attach(self: Rc<Self>) {
self.client.objects.regions.set(self.id, self.clone());
}
fn pre_release(&self) -> Result<(), ObjectError> {
self.client.objects.regions.remove(&self.id);
Ok(())
}
}

107
src/ifs/wl_region/types.rs Normal file
View file

@ -0,0 +1,107 @@
use crate::objects::{ObjectError, ObjectId};
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::RequestParser;
use std::fmt::{Debug, Formatter};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlRegionError {
#[error("Could not process `destroy` request")]
DestroyError(#[from] DestroyError),
#[error("Could not process `add` request")]
AddError(#[from] AddError),
#[error("Could not process `subtract` request")]
SubtractError(#[from] SubtractError),
}
#[derive(Debug, Error)]
pub enum DestroyError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error(transparent)]
ObjectError(Box<ObjectError>),
}
efrom!(DestroyError, ParseFailed, WlParserError);
efrom!(DestroyError, ObjectError, ObjectError);
#[derive(Debug, Error)]
pub enum AddError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error("width and/or height are negative")]
NegativeExtents,
}
efrom!(AddError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum SubtractError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error("width and/or height are negative")]
NegativeExtents,
}
efrom!(SubtractError, ParseFailed, WlParserError);
pub(super) struct Destroy;
impl RequestParser<'_> for Destroy {
fn parse(_parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self)
}
}
impl Debug for Destroy {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "destroy()")
}
}
pub(super) struct Add {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl RequestParser<'_> for Add {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
x: parser.int()?,
y: parser.int()?,
width: parser.int()?,
height: parser.int()?,
})
}
}
impl Debug for Add {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"add(x: {}, y: {}, width: {}, height: {})",
self.x, self.y, self.width, self.height,
)
}
}
pub(super) struct Subtract {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl RequestParser<'_> for Subtract {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
x: parser.int()?,
y: parser.int()?,
width: parser.int()?,
height: parser.int()?,
})
}
}
impl Debug for Subtract {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"subtract(x: {}, y: {}, width: {}, height: {})",
self.x, self.y, self.width, self.height,
)
}
}

View file

@ -0,0 +1,91 @@
mod types;
use crate::globals::{Global, GlobalName};
use crate::objects::{Interface, Object, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::{DynEventFormatter, WlClientData};
use std::rc::Rc;
pub use types::*;
const BIND: u32 = 0;
const GLOBAL: u32 = 0;
const GLOBAL_REMOVE: u32 = 1;
pub struct WlRegistry {
id: ObjectId,
client: Rc<WlClientData>,
}
impl WlRegistry {
pub fn new(id: ObjectId, client: &Rc<WlClientData>) -> Self {
Self {
id,
client: client.clone(),
}
}
pub fn global(self: &Rc<Self>, global: &Rc<dyn Global>) -> DynEventFormatter {
Box::new(GlobalE {
obj: self.clone(),
global: global.clone(),
})
}
pub fn global_remove(self: &Rc<Self>, name: GlobalName) -> DynEventFormatter {
Box::new(GlobalRemove {
obj: self.clone(),
name,
})
}
async fn bind(&self, parser: WlParser<'_, '_>) -> Result<(), BindError> {
let bind: Bind = self.client.parse(self, parser)?;
let global = self.client.state.globals.get(bind.name)?;
if global.interface().name() != bind.interface {
return Err(BindError::InvalidInterface(InterfaceError {
name: global.name(),
interface: global.interface(),
actual: bind.interface.to_string(),
}));
}
if bind.version > global.version() {
return Err(BindError::InvalidVersion(VersionError {
name: global.name(),
interface: global.interface(),
version: global.version(),
actual: bind.version,
}));
}
global.bind(&self.client, bind.id, bind.version).await?;
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlRegistryError> {
match request {
BIND => self.bind(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
handle_request!(WlRegistry);
impl Object for WlRegistry {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlRegistry
}
fn num_requests(&self) -> u32 {
BIND + 1
}
}

View file

@ -0,0 +1,117 @@
use crate::globals::{Global, GlobalError, GlobalName};
use crate::ifs::wl_registry::{WlRegistry, GLOBAL, GLOBAL_REMOVE};
use crate::objects::{Interface, Object, ObjectId};
use crate::utils::buffd::{WlFormatter, WlParser, WlParserError};
use crate::wl_client::{EventFormatter, RequestParser};
use std::fmt::{Debug, Formatter};
use std::rc::Rc;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlRegistryError {
#[error("Could not process bind request")]
BindError(#[source] Box<BindError>),
}
efrom!(WlRegistryError, BindError, BindError);
#[derive(Debug, Error)]
pub enum BindError {
#[error("Parsing failed")]
ParseError(#[source] Box<WlParserError>),
#[error(transparent)]
GlobalError(Box<GlobalError>),
#[error("Tried to bind to global {} of type {} using interface {}", .0.name, .0.interface.name(), .0.actual)]
InvalidInterface(InterfaceError),
#[error("Tried to bind to global {} of type {} and version {} using version {}", .0.name, .0.interface.name(), .0.version, .0.actual)]
InvalidVersion(VersionError),
}
#[derive(Debug)]
pub struct InterfaceError {
pub name: GlobalName,
pub interface: Interface,
pub actual: String,
}
#[derive(Debug)]
pub struct VersionError {
pub name: GlobalName,
pub interface: Interface,
pub version: u32,
pub actual: u32,
}
efrom!(BindError, ParseError, WlParserError);
efrom!(BindError, GlobalError, GlobalError);
pub(super) struct GlobalE {
pub obj: Rc<WlRegistry>,
pub global: Rc<dyn Global>,
}
impl EventFormatter for GlobalE {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(self.obj.id, GLOBAL)
.uint(self.global.name().raw())
.string(self.global.interface().name())
.uint(self.global.version());
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for GlobalE {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"global(name: {}, interface: {:?}, version: {})",
self.global.name(),
self.global.interface().name(),
self.global.version()
)
}
}
pub(super) struct GlobalRemove {
pub obj: Rc<WlRegistry>,
pub name: GlobalName,
}
impl EventFormatter for GlobalRemove {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>) {
fmt.header(self.obj.id, GLOBAL_REMOVE).uint(self.name.raw());
}
fn obj(&self) -> &dyn Object {
&*self.obj
}
}
impl Debug for GlobalRemove {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "global_remove(name: {})", self.name)
}
}
pub(super) struct Bind<'a> {
pub name: GlobalName,
pub id: ObjectId,
pub interface: &'a str,
pub version: u32,
}
impl<'a> RequestParser<'a> for Bind<'a> {
fn parse(parser: &mut WlParser<'_, 'a>) -> Result<Self, WlParserError> {
Ok(Self {
name: parser.global()?,
interface: parser.string()?,
version: parser.uint()?,
id: parser.object()?,
})
}
}
impl Debug for Bind<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"bind(name: {}, interface: {:?}, version: {}, id: {})",
self.name, self.interface, self.version, self.id
)
}
}

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)
}
}

View file

@ -0,0 +1,94 @@
mod types;
use crate::clientmem::ClientMem;
use crate::objects::{Interface, Object, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::WlClientData;
use std::cell::RefCell;
use std::rc::Rc;
pub use types::*;
use uapi::OwnedFd;
const CREATE_BUFFER: u32 = 0;
const DESTROY: u32 = 1;
const RESIZE: u32 = 2;
pub struct WlShmPool {
id: ObjectId,
client: Rc<WlClientData>,
fd: OwnedFd,
mem: RefCell<Rc<ClientMem>>,
}
impl WlShmPool {
pub fn new(
id: ObjectId,
client: &Rc<WlClientData>,
fd: OwnedFd,
len: usize,
) -> Result<Self, WlShmPoolError> {
Ok(Self {
id,
client: client.clone(),
mem: RefCell::new(Rc::new(ClientMem::new(fd.raw(), len)?)),
fd,
})
}
async fn create_buffer(&self, parser: WlParser<'_, '_>) -> Result<(), CreateBufferError> {
let create: CreateBuffer = self.client.parse(self, parser)?;
Ok(())
}
async fn destroy(&self, parser: WlParser<'_, '_>) -> Result<(), DestroyError> {
let _destroy: Destroy = self.client.parse(self, parser)?;
self.client
.objects
.remove_obj(&self.client, self.id)
.await?;
Ok(())
}
async fn resize(&self, parser: WlParser<'_, '_>) -> Result<(), ResizeError> {
let resize: Resize = self.client.parse(self, parser)?;
let mut mem = self.mem.borrow_mut();
if resize.size < 0 {
return Err(ResizeError::NegativeSize);
}
if (resize.size as usize) < mem.len() {
return Err(ResizeError::CannotShrink);
}
*mem = Rc::new(ClientMem::new(self.fd.raw(), resize.size as usize)?);
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlShmPoolError> {
match request {
CREATE_BUFFER => self.create_buffer(parser).await?,
DESTROY => self.destroy(parser).await?,
RESIZE => self.resize(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
handle_request!(WlShmPool);
impl Object for WlShmPool {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlShmPool
}
fn num_requests(&self) -> u32 {
RESIZE + 1
}
}

View file

@ -0,0 +1,117 @@
use crate::clientmem::ClientMemError;
use crate::objects::{ObjectError, ObjectId};
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::{RequestParser, WlClientError};
use std::fmt::{Debug, Formatter};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlShmPoolError {
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
#[error("Could not process a `create_buffer` request")]
CreateBufferError(#[from] CreateBufferError),
#[error("Could not process a `destroy` request")]
DestroyError(#[from] DestroyError),
#[error("Could not process a `resize` request")]
ResizeError(#[from] ResizeError),
#[error(transparent)]
ClientMemError(Box<ClientMemError>),
}
efrom!(WlShmPoolError, ObjectError, ObjectError);
efrom!(WlShmPoolError, ClientError, WlClientError);
efrom!(WlShmPoolError, ClientMemError, ClientMemError);
#[derive(Debug, Error)]
pub enum CreateBufferError {
#[error("Parsing failed")]
ParseError(#[source] Box<WlParserError>),
#[error(transparent)]
ObjectError(Box<ObjectError>),
}
efrom!(CreateBufferError, ParseError, WlParserError);
efrom!(CreateBufferError, ObjectError, ObjectError);
#[derive(Debug, Error)]
pub enum DestroyError {
#[error("Parsing failed")]
ParseError(#[source] Box<WlParserError>),
#[error(transparent)]
ObjectError(Box<ObjectError>),
}
efrom!(DestroyError, ParseError, WlParserError);
efrom!(DestroyError, ObjectError, ObjectError);
#[derive(Debug, Error)]
pub enum ResizeError {
#[error("Parsing failed")]
ParseError(#[source] Box<WlParserError>),
#[error("Tried to shrink the pool")]
CannotShrink,
#[error("Requested size is negative")]
NegativeSize,
#[error(transparent)]
ClientMemError(Box<ClientMemError>),
}
efrom!(ResizeError, ParseError, WlParserError);
efrom!(ResizeError, ClientMemError, ClientMemError);
pub(super) struct CreateBuffer {
pub id: ObjectId,
pub offset: i32,
pub width: i32,
pub height: i32,
pub stride: i32,
pub format: u32,
}
impl RequestParser<'_> for CreateBuffer {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
id: parser.object()?,
offset: parser.int()?,
width: parser.int()?,
height: parser.int()?,
stride: parser.int()?,
format: parser.uint()?,
})
}
}
impl Debug for CreateBuffer {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"create_buffer(id: {}, offset: {}, width: {}, height: {}, stride: {}, format: {})",
self.id, self.offset, self.width, self.height, self.stride, self.format,
)
}
}
pub(super) struct Destroy;
impl RequestParser<'_> for Destroy {
fn parse(_parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self)
}
}
impl Debug for Destroy {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "destroy()",)
}
}
pub(super) struct Resize {
pub size: i32,
}
impl RequestParser<'_> for Resize {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
size: parser.int()?,
})
}
}
impl Debug for Resize {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "resize(size: {})", self.size,)
}
}

View file

@ -0,0 +1,80 @@
mod types;
use crate::globals::{Global, GlobalName};
use crate::objects::{Interface, Object, ObjectError, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::WlClientData;
use std::rc::Rc;
pub use types::*;
pub struct WlSubcompositorGlobal {
name: GlobalName,
}
pub struct WlSubcompositorObj {
global: Rc<WlSubcompositorGlobal>,
id: ObjectId,
}
impl WlSubcompositorGlobal {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
async fn bind_(
self: Rc<Self>,
id: ObjectId,
client: &WlClientData,
_version: u32,
) -> Result<(), WlSubcompositorError> {
let obj = Rc::new(WlSubcompositorObj { global: self, id });
client.attach_client_object(obj)?;
Ok(())
}
}
impl WlSubcompositorObj {
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), ObjectError> {
unreachable!();
}
}
bind!(WlSubcompositorGlobal);
impl Global for WlSubcompositorGlobal {
fn name(&self) -> GlobalName {
self.name
}
fn interface(&self) -> Interface {
Interface::WlSubcompositor
}
fn version(&self) -> u32 {
1
}
fn pre_remove(&self) {
unreachable!()
}
}
handle_request!(WlSubcompositorObj);
impl Object for WlSubcompositorObj {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlSubcompositor
}
fn num_requests(&self) -> u32 {
0
}
}

View file

@ -0,0 +1,14 @@
use crate::objects::ObjectError;
use crate::wl_client::WlClientError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlSubcompositorError {
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(WlSubcompositorError, ObjectError, ObjectError);
efrom!(WlSubcompositorError, ClientError, WlClientError);

162
src/ifs/wl_surface/mod.rs Normal file
View file

@ -0,0 +1,162 @@
mod types;
use std::cell::Cell;
use crate::objects::{Interface, Object, ObjectError, ObjectId};
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::{RequestParser, WlClientData};
use std::rc::Rc;
pub use types::*;
use crate::pixman::Region;
const DESTROY: u32 = 0;
const ATTACH: u32 = 1;
const DAMAGE: u32 = 2;
const FRAME: u32 = 3;
const SET_OPAQUE_REGION: u32 = 4;
const SET_INPUT_REGION: u32 = 5;
const COMMIT: u32 = 6;
const SET_BUFFER_TRANSFORM: u32 = 7;
const SET_BUFFER_SCALE: u32 = 8;
const DAMAGE_BUFFER: u32 = 9;
const ENTER: u32 = 0;
const LEAVE: u32 = 1;
const INVALID_SCALE: u32 = 0;
const INVALID_TRANSFORM: u32 = 1;
const INVALID_SIZE: u32 = 2;
pub struct WlSurface {
id: ObjectId,
client: Rc<WlClientData>,
pending: PendingState,
}
#[derive(Default)]
struct PendingState {
opaque_region: Cell<Option<Region>>,
input_region: Cell<Option<Region>>,
}
impl WlSurface {
pub fn new(id: ObjectId, client: &Rc<WlClientData>) -> Self {
Self {
id,
client: client.clone(),
pending: Default::default(),
}
}
fn parse<'a, T: RequestParser<'a>>(
&self,
parser: WlParser<'_, 'a>,
) -> Result<T, WlParserError> {
self.client.parse(self, parser)
}
async fn destroy(&self, parser: WlParser<'_, '_>) -> Result<(), DestroyError> {
let destroy: Destroy = self.parse(parser)?;
Ok(())
}
async fn attach(&self, parser: WlParser<'_, '_>) -> Result<(), AttachError> {
let attach: Attach = self.parse(parser)?;
Ok(())
}
async fn damage(&self, parser: WlParser<'_, '_>) -> Result<(), DamageError> {
let damage: Damage = self.parse(parser)?;
Ok(())
}
async fn frame(&self, parser: WlParser<'_, '_>) -> Result<(), FrameError> {
let frame: Frame = self.parse(parser)?;
Ok(())
}
async fn set_opaque_region(
&self,
parser: WlParser<'_, '_>,
) -> Result<(), SetOpaqueRegionError> {
let region: SetOpaqueRegion = self.parse(parser)?;
let region = self.client.get_region(region.region)?;
self.pending.opaque_region.set(Some(region.region()));
Ok(())
}
async fn set_input_region(&self, parser: WlParser<'_, '_>) -> Result<(), SetInputRegionError> {
let region: SetInputRegion = self.parse(parser)?;
let region = self.client.get_region(region.region)?;
self.pending.input_region.set(Some(region.region()));
Ok(())
}
async fn commit(&self, parser: WlParser<'_, '_>) -> Result<(), CommitError> {
let commit: Commit = self.parse(parser)?;
Ok(())
}
async fn set_buffer_transform(
&self,
parser: WlParser<'_, '_>,
) -> Result<(), SetBufferTransformError> {
let transform: SetBufferTransform = self.parse(parser)?;
Ok(())
}
async fn set_buffer_scale(&self, parser: WlParser<'_, '_>) -> Result<(), SetBufferScaleError> {
let scale: SetBufferScale = self.parse(parser)?;
Ok(())
}
async fn damage_buffer(&self, parser: WlParser<'_, '_>) -> Result<(), DamageBufferError> {
let damage: DamageBuffer = self.parse(parser)?;
Ok(())
}
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), WlSurfaceError> {
match request {
DESTROY => self.destroy(parser).await?,
ATTACH => self.attach(parser).await?,
DAMAGE => self.damage(parser).await?,
FRAME => self.frame(parser).await?,
SET_OPAQUE_REGION => self.set_opaque_region(parser).await?,
SET_INPUT_REGION => self.set_input_region(parser).await?,
COMMIT => self.commit(parser).await?,
SET_BUFFER_TRANSFORM => self.set_buffer_transform(parser).await?,
SET_BUFFER_SCALE => self.set_buffer_scale(parser).await?,
DAMAGE_BUFFER => self.damage_buffer(parser).await?,
_ => unreachable!(),
}
Ok(())
}
}
handle_request!(WlSurface);
impl Object for WlSurface {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::WlSurface
}
fn num_requests(&self) -> u32 {
DAMAGE_BUFFER + 1
}
fn pre_release(&self) -> Result<(), ObjectError> {
self.client.objects.surfaces.remove(&self.id);
Ok(())
}
fn post_attach(self: Rc<Self>) {
self.client.objects.surfaces.set(self.id, self.clone());
}
}

299
src/ifs/wl_surface/types.rs Normal file
View file

@ -0,0 +1,299 @@
use crate::objects::ObjectId;
use crate::utils::buffd::{WlParser, WlParserError};
use crate::wl_client::{RequestParser, WlClientError};
use std::fmt::{Debug, Formatter};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WlSurfaceError {
#[error("Could not process `destroy` request")]
DestroyError(#[source] Box<DestroyError>),
#[error("Could not process `attach` request")]
AttachError(#[source] Box<AttachError>),
#[error("Could not process `damage` request")]
DamageError(#[source] Box<DamageError>),
#[error("Could not process `frame` request")]
FrameError(#[source] Box<FrameError>),
#[error("Could not process `set_opaque_region` request")]
SetOpaqueRegionError(#[source] Box<SetOpaqueRegionError>),
#[error("Could not process `set_input_region` request")]
SetInputRegionError(#[source] Box<SetInputRegionError>),
#[error("Could not process `commit` request")]
CommitError(#[source] Box<CommitError>),
#[error("Could not process `set_buffer_transform` request")]
SetBufferTransformError(#[source] Box<SetBufferTransformError>),
#[error("Could not process `set_buffer_scale_error` request")]
SetBufferScaleError(#[source] Box<SetBufferScaleError>),
#[error("Could not process `damage_buffer` request")]
DamageBufferError(#[source] Box<DamageBufferError>),
}
efrom!(WlSurfaceError, DestroyError, DestroyError);
efrom!(WlSurfaceError, AttachError, AttachError);
efrom!(WlSurfaceError, DamageError, DamageError);
efrom!(WlSurfaceError, FrameError, FrameError);
efrom!(WlSurfaceError, SetOpaqueRegionError, SetOpaqueRegionError);
efrom!(WlSurfaceError, SetInputRegionError, SetInputRegionError);
efrom!(WlSurfaceError, CommitError, CommitError);
efrom!(
WlSurfaceError,
SetBufferTransformError,
SetBufferTransformError
);
efrom!(WlSurfaceError, SetBufferScaleError, SetBufferScaleError);
efrom!(WlSurfaceError, DamageBufferError, DamageBufferError);
#[derive(Debug, Error)]
pub enum DestroyError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(DestroyError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum AttachError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(AttachError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum DamageError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(DamageError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum FrameError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(FrameError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum SetOpaqueRegionError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(SetOpaqueRegionError, ParseFailed, WlParserError);
efrom!(SetOpaqueRegionError, ClientError, WlClientError);
#[derive(Debug, Error)]
pub enum SetInputRegionError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(SetInputRegionError, ParseFailed, WlParserError);
efrom!(SetInputRegionError, ClientError, WlClientError);
#[derive(Debug, Error)]
pub enum CommitError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(CommitError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum SetBufferTransformError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(SetBufferTransformError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum SetBufferScaleError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(SetBufferScaleError, ParseFailed, WlParserError);
#[derive(Debug, Error)]
pub enum DamageBufferError {
#[error("Parsing failed")]
ParseFailed(#[source] Box<WlParserError>),
}
efrom!(DamageBufferError, ParseFailed, WlParserError);
pub(super) struct Destroy;
impl RequestParser<'_> for Destroy {
fn parse(_parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self)
}
}
impl Debug for Destroy {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "destroy()")
}
}
pub(super) struct Attach {
pub buffer: ObjectId,
pub x: i32,
pub y: i32,
}
impl RequestParser<'_> for Attach {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
buffer: parser.object()?,
x: parser.int()?,
y: parser.int()?,
})
}
}
impl Debug for Attach {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"attach(buffer: {}, x: {}, y: {})",
self.buffer, self.x, self.y
)
}
}
pub(super) struct Damage {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl RequestParser<'_> for Damage {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
x: parser.int()?,
y: parser.int()?,
width: parser.int()?,
height: parser.int()?,
})
}
}
impl Debug for Damage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"damage(x: {}, y: {}, width: {}, height: {})",
self.x, self.y, self.width, self.height
)
}
}
pub(super) struct Frame {
pub callback: ObjectId,
}
impl RequestParser<'_> for Frame {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
callback: parser.object()?,
})
}
}
impl Debug for Frame {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "frame(callback: {})", self.callback)
}
}
pub(super) struct SetOpaqueRegion {
pub region: ObjectId,
}
impl RequestParser<'_> for SetOpaqueRegion {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
region: parser.object()?,
})
}
}
impl Debug for SetOpaqueRegion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "set_opaque_region(region: {})", self.region)
}
}
pub(super) struct SetInputRegion {
pub region: ObjectId,
}
impl RequestParser<'_> for SetInputRegion {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
region: parser.object()?,
})
}
}
impl Debug for SetInputRegion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "set_input_region(region: {})", self.region)
}
}
pub(super) struct Commit;
impl RequestParser<'_> for Commit {
fn parse(_parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self)
}
}
impl Debug for Commit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "commit()")
}
}
pub(super) struct SetBufferTransform {
pub transform: i32,
}
impl RequestParser<'_> for SetBufferTransform {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
transform: parser.int()?,
})
}
}
impl Debug for SetBufferTransform {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "set_buffer_transform(transform: {})", self.transform)
}
}
pub(super) struct SetBufferScale {
pub scale: i32,
}
impl RequestParser<'_> for SetBufferScale {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
scale: parser.int()?,
})
}
}
impl Debug for SetBufferScale {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "set_buffer_scale(scale: {})", self.scale)
}
}
pub(super) struct DamageBuffer {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl RequestParser<'_> for DamageBuffer {
fn parse(parser: &mut WlParser<'_, '_>) -> Result<Self, WlParserError> {
Ok(Self {
x: parser.int()?,
y: parser.int()?,
width: parser.int()?,
height: parser.int()?,
})
}
}
impl Debug for DamageBuffer {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"damage_buffer(x: {}, y: {}, width: {}, height: {})",
self.x, self.y, self.width, self.height
)
}
}

View file

@ -0,0 +1,85 @@
mod types;
use crate::globals::{Global, GlobalName};
use crate::objects::{Interface, Object, ObjectError, ObjectId};
use crate::utils::buffd::WlParser;
use crate::wl_client::WlClientData;
use std::rc::Rc;
pub use types::*;
pub struct XdgWmBaseGlobal {
name: GlobalName,
}
pub struct XdgWmBaseObj {
global: Rc<XdgWmBaseGlobal>,
id: ObjectId,
version: u32,
}
impl XdgWmBaseGlobal {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
async fn bind_(
self: Rc<Self>,
id: ObjectId,
client: &WlClientData,
version: u32,
) -> Result<(), XdgWmBaseError> {
let obj = Rc::new(XdgWmBaseObj {
global: self,
id,
version,
});
client.attach_client_object(obj)?;
Ok(())
}
}
impl XdgWmBaseObj {
async fn handle_request_(
&self,
request: u32,
parser: WlParser<'_, '_>,
) -> Result<(), ObjectError> {
unreachable!();
}
}
bind!(XdgWmBaseGlobal);
impl Global for XdgWmBaseGlobal {
fn name(&self) -> GlobalName {
self.name
}
fn interface(&self) -> Interface {
Interface::XdgWmBase
}
fn version(&self) -> u32 {
3
}
fn pre_remove(&self) {
unreachable!()
}
}
handle_request!(XdgWmBaseObj);
impl Object for XdgWmBaseObj {
fn id(&self) -> ObjectId {
self.id
}
fn interface(&self) -> Interface {
Interface::XdgWmBase
}
fn num_requests(&self) -> u32 {
0
}
}

View file

@ -0,0 +1,14 @@
use crate::objects::ObjectError;
use crate::wl_client::WlClientError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum XdgWmBaseError {
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error(transparent)]
ClientError(Box<WlClientError>),
}
efrom!(XdgWmBaseError, ObjectError, ObjectError);
efrom!(XdgWmBaseError, ClientError, WlClientError);

48
src/macros.rs Normal file
View file

@ -0,0 +1,48 @@
macro_rules! efrom {
($ename:ty, $vname:ident, $sname:ty) => {
impl From<$sname> for $ename {
fn from(e: $sname) -> Self {
Self::$vname(Box::new(e))
}
}
};
}
macro_rules! handle_request {
($oname:ty) => {
impl crate::objects::ObjectHandleRequest for $oname {
fn handle_request<'a>(
&'a self,
request: u32,
parser: crate::utils::buffd::WlParser<'a, 'a>,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = Result<(), crate::objects::ObjectError>> + 'a>,
> {
Box::pin(async move {
self.handle_request_(request, parser).await?;
Ok(())
})
}
}
};
}
macro_rules! bind {
($oname:ty) => {
impl crate::globals::GlobalBind for $oname {
fn bind<'a>(
self: std::rc::Rc<Self>,
client: &'a std::rc::Rc<crate::wl_client::WlClientData>,
id: crate::objects::ObjectId,
version: u32,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = Result<(), crate::globals::GlobalError>> + 'a>,
> {
Box::pin(async move {
self.bind_(id, client, version).await?;
Ok(())
})
}
}
};
}

78
src/main.rs Normal file
View file

@ -0,0 +1,78 @@
use crate::acceptor::AcceptorError;
use crate::clientmem::ClientMemError;
use crate::event_loop::EventLoopError;
use crate::globals::Globals;
use crate::ifs::wl_compositor::WlCompositorGlobal;
use crate::ifs::wl_shm::WlShmGlobal;
use crate::ifs::wl_subcompositor::WlSubcompositorGlobal;
use crate::ifs::xdg_wm_base::XdgWmBaseGlobal;
use crate::sighand::SighandError;
use crate::state::State;
use crate::utils::numcell::NumCell;
use crate::wl_client::WlClients;
use anyhow::anyhow;
use log::LevelFilter;
use std::rc::Rc;
use thiserror::Error;
#[macro_use]
mod macros;
mod acceptor;
mod async_engine;
mod clientmem;
mod event_loop;
mod globals;
mod ifs;
mod objects;
mod pixman;
mod sighand;
mod state;
mod time;
mod utils;
mod wheel;
mod wl_client;
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
.init();
if let Err(e) = main_() {
log::error!("A fatal error occurred: {:#}", anyhow!(e));
std::process::exit(1);
}
}
#[derive(Debug, Error)]
enum MainError {
#[error("The client acceptor caused an error")]
AcceptorError(#[from] AcceptorError),
#[error("The event loop caused an error")]
EventLoopError(#[from] EventLoopError),
#[error("The signal handler caused an error")]
SighandError(#[from] SighandError),
#[error("The clientmem subsystem caused an error")]
ClientmemError(#[from] ClientMemError),
}
fn main_() -> Result<(), MainError> {
clientmem::init()?;
let el = event_loop::EventLoop::new().unwrap();
sighand::install(&el.to_ref())?;
let wheel = wheel::WheelRef::new(&el.to_ref()).unwrap();
let engine = Rc::new(async_engine::AsyncEngine::new(&el.to_ref(), &wheel).unwrap());
let globals = Globals::new();
globals.insert_no_broadcast(Rc::new(WlCompositorGlobal::new(globals.name())));
globals.insert_no_broadcast(Rc::new(WlShmGlobal::new(globals.name())));
globals.insert_no_broadcast(Rc::new(WlSubcompositorGlobal::new(globals.name())));
globals.insert_no_broadcast(Rc::new(XdgWmBaseGlobal::new(globals.name())));
let state = Rc::new(State {
eng: engine,
el: el.to_ref(),
clients: WlClients::new(),
next_name: NumCell::new(1),
globals,
});
acceptor::Acceptor::install(&state)?;
el.run()?;
Ok(())
}

249
src/objects.rs Normal file
View file

@ -0,0 +1,249 @@
use crate::ifs::wl_compositor::WlCompositorError;
use crate::ifs::wl_display::{WlDisplay, WlDisplayError};
use crate::ifs::wl_registry::{WlRegistry, WlRegistryError};
use crate::ifs::wl_shm::WlShmError;
use crate::ifs::wl_shm_pool::WlShmPoolError;
use crate::ifs::wl_surface::{WlSurface, WlSurfaceError};
use crate::utils::buffd::{WlParser, WlParserError};
use crate::utils::copyhashmap::CopyHashMap;
use crate::wl_client::{WlClientData, WlClientError};
use ahash::AHashMap;
use std::cell::{RefCell, RefMut};
use std::fmt::{Display, Formatter};
use std::future::Future;
use std::mem;
use std::pin::Pin;
use std::rc::Rc;
use thiserror::Error;
use crate::ifs::wl_region::{WlRegion, WlRegionError};
#[derive(Debug, Error)]
pub enum ObjectError {
#[error("A client error occurred")]
ClientError(#[source] Box<WlClientError>),
#[error("Cannot parse the message")]
ParserError(#[source] Box<WlParserError>),
#[error("Server tried to allocate more than 0x1_00_00_00 ids")]
TooManyIds,
#[error("The server object id is out of bounds")]
ServerIdOutOfBounds,
#[error("The object id is unknown")]
UnknownId,
#[error("The id is already in use")]
IdAlreadyInUse,
#[error("The client object id is out of bounds")]
ClientIdOutOfBounds,
#[error("An error occurred in a `wl_display`")]
WlDisplayError(#[source] Box<WlDisplayError>),
#[error("An error occurred in a `wl_registry`")]
WlRegistryError(#[source] Box<WlRegistryError>),
#[error("Could not add object {0} to the client")]
AddObjectError(ObjectId, #[source] Box<ObjectError>),
#[error("An error occurred in a `wl_surface`")]
WlSurfaceError(#[source] Box<WlSurfaceError>),
#[error("An error occurred in a `wl_compositor`")]
WlCompositorError(#[source] Box<WlCompositorError>),
#[error("An error occurred in a `wl_shm`")]
WlShmError(#[source] Box<WlShmError>),
#[error("An error occurred in a `wl_shm_pool`")]
WlShmPoolError(#[source] Box<WlShmPoolError>),
#[error("An error occurred in a `wl_region`")]
WlRegionError(#[source] Box<WlRegionError>),
#[error("Object {0} is not a display")]
NotADisplay(ObjectId),
}
efrom!(ObjectError, ClientError, WlClientError);
efrom!(ObjectError, ParserError, WlParserError);
efrom!(ObjectError, WlDisplayError, WlDisplayError);
efrom!(ObjectError, WlRegistryError, WlRegistryError);
efrom!(ObjectError, WlSurfaceError, WlSurfaceError);
efrom!(ObjectError, WlCompositorError, WlCompositorError);
efrom!(ObjectError, WlShmError, WlShmError);
efrom!(ObjectError, WlShmPoolError, WlShmPoolError);
efrom!(ObjectError, WlRegionError, WlRegionError);
pub const WL_DISPLAY_ID: ObjectId = ObjectId(1);
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct ObjectId(u32);
impl ObjectId {
pub fn from_raw(raw: u32) -> Self {
Self(raw)
}
pub fn raw(self) -> u32 {
self.0
}
}
impl Display for ObjectId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
pub trait ObjectHandleRequest {
fn handle_request<'a>(
&'a self,
request: u32,
parser: WlParser<'a, 'a>,
) -> Pin<Box<dyn Future<Output = Result<(), ObjectError>> + 'a>>;
}
pub trait Object: ObjectHandleRequest {
fn id(&self) -> ObjectId;
fn interface(&self) -> Interface;
fn num_requests(&self) -> u32;
fn pre_release(&self) -> Result<(), ObjectError> {
Ok(())
}
fn post_attach(self: Rc<Self>) {}
fn into_display(self: Rc<Self>) -> Result<Rc<WlDisplay>, ObjectError> {
Err(ObjectError::NotADisplay(self.id()))
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Interface {
WlDisplay,
WlCallback,
WlCompositor,
WlRegistry,
WlShm,
WlShmPool,
WlSubcompositor,
XdgWmBase,
WlSurface,
WlRegion,
}
impl Interface {
pub fn name(self) -> &'static str {
match self {
Interface::WlDisplay => "wl_display",
Interface::WlCallback => "wl_callback",
Interface::WlCompositor => "wl_compositor",
Interface::WlRegistry => "wl_registry",
Interface::WlShm => "wl_shm",
Interface::WlSubcompositor => "wl_subcompositor",
Interface::XdgWmBase => "xdg_wm_base",
Interface::WlSurface => "wl_surface",
Interface::WlShmPool => "wl_shm_pool",
Interface::WlRegion => "wl_region",
}
}
}
pub struct Objects {
registry: CopyHashMap<ObjectId, Rc<dyn Object>>,
registries: CopyHashMap<ObjectId, Rc<WlRegistry>>,
pub surfaces: CopyHashMap<ObjectId, Rc<WlSurface>>,
pub regions: CopyHashMap<ObjectId, Rc<WlRegion>>,
ids: RefCell<Vec<usize>>,
}
const MIN_SERVER_ID: u32 = 0xff000000;
const SEG_SIZE: usize = 8 * mem::size_of::<usize>();
impl Objects {
pub fn new() -> Self {
Self {
registry: Default::default(),
registries: Default::default(),
surfaces: Default::default(),
regions: Default::default(),
ids: RefCell::new(vec![]),
}
}
pub fn destroy(&self) {
self.registry.clear();
self.registries.clear();
self.surfaces.clear();
}
fn id(&self, client_data: &WlClientData) -> Result<ObjectId, ObjectError> {
const MAX_ID_OFFSET: u32 = u32::MAX - MIN_SERVER_ID;
let offset = self.id_offset();
if offset > MAX_ID_OFFSET {
log::error!(
"Client {} caused the server to allocate more than 0x{:x} ids",
client_data.id,
MAX_ID_OFFSET + 1
);
return Err(ObjectError::TooManyIds);
}
Ok(ObjectId(MIN_SERVER_ID + offset))
}
pub fn get_obj(&self, id: ObjectId) -> Result<Rc<dyn Object>, ObjectError> {
match self.registry.get(&id) {
Some(o) => Ok(o),
_ => Err(ObjectError::UnknownId),
}
}
pub fn add_client_object(&self, obj: Rc<dyn Object>) -> Result<(), ObjectError> {
let id = obj.id();
let res = (|| {
if id.0 == 0 || id.0 >= MIN_SERVER_ID {
return Err(ObjectError::ClientIdOutOfBounds);
}
if self.registry.contains(&id) {
return Err(ObjectError::IdAlreadyInUse);
}
self.registry.set(id, obj.clone());
obj.post_attach();
Ok(())
})();
if let Err(e) = res {
return Err(ObjectError::AddObjectError(id, Box::new(e)));
}
Ok(())
}
pub async fn remove_obj(
&self,
client_data: &WlClientData,
id: ObjectId,
) -> Result<(), ObjectError> {
let obj = match self.registry.remove(&id) {
Some(o) => o,
_ => return Err(ObjectError::UnknownId),
};
obj.pre_release()?;
if id.0 >= MIN_SERVER_ID {
let offset = (id.0 - MIN_SERVER_ID) as usize;
let pos = offset / SEG_SIZE;
let seg_offset = offset % SEG_SIZE;
let mut ids = self.ids.borrow_mut();
if ids.len() <= pos {
return Err(ObjectError::ServerIdOutOfBounds);
}
ids[pos] |= 1 << seg_offset;
}
client_data
.event(client_data.display()?.delete_id(id))
.await?;
Ok(())
}
pub fn registries(&self) -> RefMut<AHashMap<ObjectId, Rc<WlRegistry>>> {
self.registries.lock()
}
fn id_offset(&self) -> u32 {
let mut ids = self.ids.borrow_mut();
for (pos, seg) in ids.iter_mut().enumerate() {
if *seg != 0 {
let offset = seg.trailing_zeros();
*seg &= !(1 << offset);
return (pos * SEG_SIZE) as u32 + offset;
}
}
ids.push(!1);
((ids.len() - 1) * SEG_SIZE) as u32
}
}

96
src/pixman/mod.rs Normal file
View file

@ -0,0 +1,96 @@
use std::ops::{Add, Sub};
use std::ptr;
use uapi::c;
#[link(name = "pixman-1")]
extern "C" {
fn pixman_region32_init(region: *mut Region);
fn pixman_region32_init_rect(
region: *mut Region,
x: c::c_int,
y: c::c_int,
width: c::c_uint,
height: c::c_uint,
);
fn pixman_region32_fini(region: *mut Region);
fn pixman_region32_copy(dst: *mut Region, src: *const Region);
fn pixman_region32_union(dst: *mut Region, a: *const Region, b: *const Region);
fn pixman_region32_subtract(dst: *mut Region, a: *const Region, b: *const Region);
}
#[repr(C)]
#[derive(Copy, Clone, Default, Debug)]
pub struct Box32 {
x1: i32,
y1: i32,
x2: i32,
y2: i32,
}
#[repr(C)]
struct RegionData {
size: c::c_long,
num_rects: c::c_long,
// rects: [Box32; size],
}
#[repr(C)]
pub struct Region {
extents: Box32,
data: *mut RegionData,
}
impl Region {
pub fn new() -> Self {
let mut slf = Region {
extents: Default::default(),
data: ptr::null_mut(),
};
unsafe {
pixman_region32_init(&mut slf);
}
slf
}
pub fn rect(x: i32, y: i32, width: u32, height: u32) -> Self {
let mut new = Region::new();
unsafe {
pixman_region32_init_rect(&mut new, x as _, y as _, width as _, height as _);
}
new
}
pub fn add(&self, region: &Self) -> Self {
let mut new = Region::new();
unsafe {
pixman_region32_union(&mut new, self, region);
}
new
}
pub fn subtract(&self, region: &Self) -> Self {
let mut new = Region::new();
unsafe {
pixman_region32_subtract(&mut new, self, region);
}
new
}
}
impl Clone for Region {
fn clone(&self) -> Self {
let mut new = Region::new();
unsafe {
pixman_region32_copy(&mut new, self);
}
new
}
}
impl Drop for Region {
fn drop(&mut self) {
unsafe {
pixman_region32_fini(self);
}
}
}

75
src/sighand.rs Normal file
View file

@ -0,0 +1,75 @@
use crate::event_loop::{EventLoopDispatcher, EventLoopId, EventLoopRef};
use crate::EventLoopError;
use std::error::Error;
use std::rc::Rc;
use thiserror::Error;
use uapi::{c, Errno, OwnedFd};
#[derive(Debug, Error)]
pub enum SighandError {
#[error("The signal fd is in an error state")]
ErrorEvent,
#[error("Could not read from the signal fd")]
ReadFailed(#[source] std::io::Error),
#[error("Could not block the signalfd signals")]
BlockFailed(#[source] std::io::Error),
#[error("Could not create a signalfd")]
CreateFailed(#[source] std::io::Error),
#[error("The event loop caused an error")]
EventLoopError(#[from] EventLoopError),
}
pub fn install(el: &EventLoopRef) -> Result<(), SighandError> {
let mut set: c::sigset_t = uapi::pod_zeroed();
uapi::sigaddset(&mut set, c::SIGINT).unwrap();
if let Err(e) = uapi::pthread_sigmask(c::SIG_BLOCK, Some(&set), None) {
return Err(SighandError::BlockFailed(e.into()));
}
let fd = match uapi::signalfd_new(&set, c::SFD_CLOEXEC | c::SFD_NONBLOCK) {
Ok(fd) => fd,
Err(e) => return Err(SighandError::CreateFailed(e.into())),
};
let id = el.id()?;
let sh = Rc::new(Sighand {
fd,
id,
el: el.clone(),
});
el.insert(id, Some(sh.fd.raw()), c::EPOLLIN, sh)?;
Ok(())
}
struct Sighand {
fd: OwnedFd,
id: EventLoopId,
el: EventLoopRef,
}
impl EventLoopDispatcher for Sighand {
fn dispatch(&self, events: i32) -> Result<(), Box<dyn Error + Send + Sync>> {
if events & (c::EPOLLERR | c::EPOLLHUP) != 0 {
return Err(Box::new(SighandError::ErrorEvent));
}
let mut sigfd: c::signalfd_siginfo = uapi::pod_zeroed();
loop {
if let Err(e) = uapi::read(self.fd.raw(), &mut sigfd) {
match e {
Errno(c::EAGAIN) => break,
_ => return Err(Box::new(SighandError::ReadFailed(e.into()))),
}
}
log::info!("Received signal {}", sigfd.ssi_signo);
if sigfd.ssi_signo == c::SIGINT as _ {
log::info!("Exiting");
self.el.stop();
}
}
Ok(())
}
}
impl Drop for Sighand {
fn drop(&mut self) {
let _ = self.el.remove(self.id);
}
}

14
src/state.rs Normal file
View file

@ -0,0 +1,14 @@
use crate::async_engine::AsyncEngine;
use crate::event_loop::EventLoopRef;
use crate::globals::Globals;
use crate::utils::numcell::NumCell;
use crate::wl_client::WlClients;
use std::rc::Rc;
pub struct State {
pub eng: Rc<AsyncEngine>,
pub el: EventLoopRef,
pub clients: WlClients,
pub next_name: NumCell<u32>,
pub globals: Globals,
}

97
src/time.rs Normal file
View file

@ -0,0 +1,97 @@
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};
use std::ops::{Add, Sub};
use std::time::Duration;
use thiserror::Error;
use uapi::c;
#[derive(Debug, Error)]
pub enum TimeError {
#[error("clock_gettime failed: {0}")]
ClockGettime(std::io::Error),
}
#[derive(Copy, Clone)]
pub struct Time(pub c::timespec);
impl Debug for Time {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Time")
.field("tv_sec", &self.0.tv_sec)
.field("tv_nsec", &self.0.tv_nsec)
.finish()
}
}
impl Time {
pub fn now() -> Result<Time, TimeError> {
let mut time = uapi::pod_zeroed();
if let Err(e) = uapi::clock_gettime(c::CLOCK_MONOTONIC, &mut time) {
return Err(TimeError::ClockGettime(e.into()));
}
Ok(Self(time))
}
pub fn round_to_ms(self) -> Time {
if self.0.tv_nsec > 999_000_000 {
Time(c::timespec {
tv_sec: self.0.tv_sec + 1,
tv_nsec: 0,
})
} else {
Time(c::timespec {
tv_sec: self.0.tv_sec,
tv_nsec: (self.0.tv_nsec + 999_999) / 1_000_000 * 1_000_000,
})
}
}
}
impl Eq for Time {}
impl PartialEq for Time {
fn eq(&self, other: &Self) -> bool {
self.0.tv_sec == other.0.tv_sec && self.0.tv_nsec == other.0.tv_nsec
}
}
impl Ord for Time {
fn cmp(&self, other: &Self) -> Ordering {
self.0
.tv_sec
.cmp(&other.0.tv_sec)
.then_with(|| self.0.tv_nsec.cmp(&other.0.tv_nsec))
}
}
impl PartialOrd for Time {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Sub<Time> for Time {
type Output = Duration;
fn sub(self, rhs: Time) -> Self::Output {
let sec = self.0.tv_sec - rhs.0.tv_sec;
let nsec = self.0.tv_nsec - rhs.0.tv_nsec;
Duration::from_nanos(sec as u64 * 1_000_000_000 + nsec as u64)
}
}
impl Add<Duration> for Time {
type Output = Self;
fn add(mut self, rhs: Duration) -> Self::Output {
let secs = (rhs.as_nanos() / 1_000_000_000) as c::time_t;
let nsecs = (rhs.as_nanos() % 1_000_000_000) as c::c_long;
self.0.tv_sec += secs;
self.0.tv_nsec += nsecs;
if self.0.tv_nsec > 999_999_999 {
self.0.tv_sec += 1;
self.0.tv_nsec -= 1_000_000_000;
}
self
}
}

105
src/utils/buffd/buf_in.rs Normal file
View file

@ -0,0 +1,105 @@
use crate::async_engine::AsyncFd;
use crate::utils::buffd::{BufFdError, BUF_SIZE, CMSG_BUF_SIZE, MAX_IN_FD};
use std::collections::VecDeque;
use std::mem::MaybeUninit;
use uapi::{c, Errno, OwnedFd, Pod};
pub struct BufFdIn {
fd: AsyncFd,
in_fd: VecDeque<OwnedFd>,
in_buf: Box<[MaybeUninit<u8>; BUF_SIZE]>,
in_cmsg_buf: Box<[MaybeUninit<u8>; CMSG_BUF_SIZE]>,
in_left: usize,
in_right: usize,
}
impl BufFdIn {
pub fn new(fd: AsyncFd) -> Self {
Self {
fd,
in_fd: Default::default(),
in_buf: Box::new([MaybeUninit::uninit(); BUF_SIZE]),
in_cmsg_buf: Box::new([MaybeUninit::uninit(); CMSG_BUF_SIZE]),
in_left: 0,
in_right: 0,
}
}
pub async fn read_full<T: Pod + ?Sized>(&mut self, buf: &mut T) -> Result<(), BufFdError> {
let bytes = unsafe { uapi::as_maybe_uninit_bytes_mut2(buf) };
let mut offset = 0;
while offset < bytes.len() {
if self.read_full_(bytes, &mut offset)? {
self.fd.readable().await?;
}
}
Ok(())
}
fn read_full_(
&mut self,
bytes: &mut [MaybeUninit<u8>],
offset: &mut usize,
) -> Result<bool, BufFdError> {
let num_bytes = (bytes.len() - *offset).min(self.in_right - self.in_left);
if num_bytes > 0 {
let left = self.in_left % BUF_SIZE;
let right = (self.in_left + num_bytes) % BUF_SIZE;
if left < right {
bytes[*offset..*offset + num_bytes].copy_from_slice(&self.in_buf[left..right]);
} else {
bytes[*offset..*offset + (BUF_SIZE - left)].copy_from_slice(&self.in_buf[left..]);
bytes[*offset + (BUF_SIZE - left)..*offset + num_bytes]
.copy_from_slice(&self.in_buf[..right]);
}
self.in_left += num_bytes;
*offset += num_bytes;
}
if *offset == bytes.len() {
return Ok(false);
}
let left = self.in_left % BUF_SIZE;
let right = self.in_right % BUF_SIZE;
let mut iov = if right < left {
[&mut self.in_buf[right..left], &mut []]
} else {
let (l, r) = self.in_buf.split_at_mut(right);
[r, &mut l[..left]]
};
let mut hdr = uapi::MsghdrMut {
iov: &mut iov[..],
control: Some(&mut self.in_cmsg_buf[..]),
name: uapi::sockaddr_none_mut(),
flags: 0,
};
let (iov, _, mut cmsg) = match uapi::recvmsg(self.fd.raw(), &mut hdr, c::MSG_DONTWAIT) {
Ok((iov, _, _)) if iov.is_empty() => return Err(BufFdError::Closed),
Ok(v) => v,
Err(Errno(c::EAGAIN)) => return Ok(true),
Err(e) => return Err(BufFdError::Io(e.into())),
};
self.in_right += iov.len();
while cmsg.len() > 0 {
let (_, hdr, data) = match uapi::cmsg_read(&mut cmsg) {
Ok(m) => m,
Err(e) => return Err(BufFdError::Io(e.into())),
};
if (hdr.cmsg_level, hdr.cmsg_type) == (c::SOL_SOCKET, c::SCM_RIGHTS) {
self.in_fd.extend(uapi::pod_iter(data).unwrap());
}
}
if self.in_fd.len() > MAX_IN_FD {
return Err(BufFdError::TooManyFds);
}
Ok(false)
}
pub fn get_fd(&mut self) -> Result<OwnedFd, BufFdError> {
match self.in_fd.pop_front() {
Some(f) => Ok(f),
None => Err(BufFdError::NoFd),
}
}
}

121
src/utils/buffd/buf_out.rs Normal file
View file

@ -0,0 +1,121 @@
use crate::async_engine::AsyncFd;
use crate::utils::buffd::{BufFdError, BUF_SIZE, CMSG_BUF_SIZE};
use futures::{select, FutureExt};
use std::collections::VecDeque;
use std::mem::MaybeUninit;
use std::slice;
use uapi::{c, Errno, OwnedFd};
pub(super) const OUT_BUF_SIZE: usize = 2 * BUF_SIZE;
pub(super) struct MsgFds {
pub(super) pos: usize,
pub(super) fds: Vec<OwnedFd>,
}
pub struct BufFdOut {
fd: AsyncFd,
pub(super) out_pos: usize,
pub(super) out_buf: *mut [MaybeUninit<u8>; OUT_BUF_SIZE],
pub(super) fds: VecDeque<MsgFds>,
cmsg_buf: Box<[MaybeUninit<u8>; CMSG_BUF_SIZE]>,
}
impl BufFdOut {
pub fn new(fd: AsyncFd) -> Self {
Self {
fd,
out_pos: 0,
out_buf: Box::into_raw(Box::new([MaybeUninit::<u32>::uninit(); OUT_BUF_SIZE / 4])) as _,
fds: Default::default(),
cmsg_buf: Box::new([MaybeUninit::uninit(); CMSG_BUF_SIZE]),
}
}
pub fn write(&mut self, bytes: &[MaybeUninit<u8>]) {
if bytes.len() > OUT_BUF_SIZE - self.out_pos {
panic!("Out buffer overflow");
}
unsafe {
(*self.out_buf)[self.out_pos..self.out_pos + bytes.len()].copy_from_slice(bytes);
}
self.out_pos += bytes.len();
}
pub fn needs_flush(&self) -> bool {
self.out_pos > BUF_SIZE
}
pub async fn flush(&mut self) -> Result<(), BufFdError> {
let mut timeout = None;
let mut pos = 0;
while pos < self.out_pos {
if self.flush_sync(&mut pos)? {
if timeout.is_none() {
timeout = Some(self.fd.eng().timeout(5000)?.fuse());
}
select! {
_ = timeout.as_mut().unwrap() => return Err(BufFdError::Timeout),
res = self.fd.writable().fuse() => res?,
}
}
}
self.out_pos = 0;
Ok(())
}
fn flush_sync(&mut self, pos: &mut usize) -> Result<bool, BufFdError> {
while *pos < self.out_pos {
let mut buf = unsafe { &(*self.out_buf)[*pos..self.out_pos] };
let mut cmsg_len = 0;
let mut fds_opt = None;
{
let mut f = self.fds.front().map(|f| f.pos);
if f == Some(*pos) {
let fds = self.fds.pop_front().unwrap();
let hdr = c::cmsghdr {
cmsg_len: 0,
cmsg_level: c::SOL_SOCKET,
cmsg_type: c::SCM_RIGHTS,
};
let mut cmsg_buf = &mut self.cmsg_buf[..];
cmsg_len = uapi::cmsg_write(&mut cmsg_buf, hdr, &fds.fds[..]).unwrap();
fds_opt = Some(fds);
f = self.fds.front().map(|f| f.pos)
}
if let Some(next_pos) = f {
buf = &buf[..next_pos - *pos];
}
}
let hdr = uapi::Msghdr {
iov: slice::from_ref(&buf),
control: Some(&self.cmsg_buf[..cmsg_len]),
name: uapi::sockaddr_none_ref(),
};
let bytes_sent =
match uapi::sendmsg(self.fd.raw(), &hdr, c::MSG_DONTWAIT | c::MSG_NOSIGNAL) {
Ok(b) => b,
Err(Errno(c::EAGAIN)) => {
if let Some(fds) = fds_opt {
self.fds.push_front(fds);
}
return Ok(true);
}
Err(Errno(c::ECONNRESET)) => return Err(BufFdError::Closed),
Err(e) => return Err(BufFdError::Io(e.into())),
};
*pos += bytes_sent;
}
Ok(false)
}
}
impl Drop for BufFdOut {
fn drop(&mut self) {
unsafe {
Box::from_raw(self.out_buf as *mut [MaybeUninit<u32>; OUT_BUF_SIZE / 4]);
}
}
}

31
src/utils/buffd/mod.rs Normal file
View file

@ -0,0 +1,31 @@
use crate::async_engine::AsyncError;
pub use buf_in::BufFdIn;
pub use buf_out::BufFdOut;
use thiserror::Error;
pub use wl_formatter::WlFormatter;
pub use wl_parser::{WlParser, WlParserError};
mod buf_in;
mod buf_out;
mod wl_formatter;
mod wl_parser;
#[derive(Debug, Error)]
pub enum BufFdError {
#[error("An IO error occurred")]
Io(#[source] std::io::Error),
#[error("An async error occurred")]
Async(#[from] AsyncError),
#[error("The peer did not send a file descriptor")]
NoFd,
#[error("The peer sent too many file descriptors")]
TooManyFds,
#[error("The peer closed the connection")]
Closed,
#[error("The connection timed out")]
Timeout,
}
const BUF_SIZE: usize = 4096;
const CMSG_BUF_SIZE: usize = 4096;
const MAX_IN_FD: usize = 4;

View file

@ -0,0 +1,78 @@
use crate::objects::ObjectId;
use crate::utils::buffd::buf_out::{BufFdOut, MsgFds};
use std::mem;
use std::mem::MaybeUninit;
use uapi::OwnedFd;
pub struct WlFormatter<'a> {
buf: &'a mut BufFdOut,
pos: usize,
fds: Vec<OwnedFd>,
}
impl<'a> WlFormatter<'a> {
pub fn new(buf: &'a mut BufFdOut) -> Self {
Self {
pos: buf.out_pos,
buf,
fds: vec![],
}
}
pub fn int(&mut self, int: i32) -> &mut Self {
self.buf.write(uapi::as_maybe_uninit_bytes(&int));
self
}
pub fn uint(&mut self, int: u32) -> &mut Self {
self.buf.write(uapi::as_maybe_uninit_bytes(&int));
self
}
pub fn fixed(&mut self, fixed: f64) -> &mut Self {
let int = (fixed * 256.0) as i32;
self.buf.write(uapi::as_maybe_uninit_bytes(&int));
self
}
pub fn string(&mut self, s: &str) -> &mut Self {
let len = s.len() + 1;
let cap = (len + 3) & !3;
self.uint(len as u32);
self.buf.write(uapi::as_maybe_uninit_bytes(s.as_bytes()));
let none = [MaybeUninit::new(0); 4];
self.buf.write(&none[..cap - len + 1]);
self
}
pub fn fd(&mut self, fd: OwnedFd) -> &mut Self {
self.fds.push(fd);
self
}
pub fn object(&mut self, obj: ObjectId) -> &mut Self {
self.uint(obj.raw())
}
pub fn header(&mut self, obj: ObjectId, event: u32) -> &mut Self {
self.object(obj).uint(event)
}
}
impl<'a> Drop for WlFormatter<'a> {
fn drop(&mut self) {
assert!(self.buf.out_pos - self.pos >= 8);
assert_eq!(self.pos % 4, 0);
unsafe {
let second_ptr = (self.buf.out_buf as *mut u8).add(self.pos + 4) as *mut u32;
let len = ((self.buf.out_pos - self.pos) as u32) << 16;
*second_ptr |= len;
}
if self.fds.len() > 0 {
self.buf.fds.push_back(MsgFds {
pos: self.pos,
fds: mem::take(&mut self.fds),
})
}
}
}

View file

@ -0,0 +1,93 @@
use crate::globals::GlobalName;
use crate::objects::ObjectId;
use crate::utils::buffd::BufFdIn;
use thiserror::Error;
use uapi::OwnedFd;
#[derive(Debug, Error)]
pub enum WlParserError {
#[error("The message ended unexpectedly")]
UnexpectedEof,
#[error("The message contained a non-utf8 string")]
NonUtf8,
#[error("The message contained a string of size 0")]
EmptyString,
#[error("Message is missing a required file descriptor")]
MissingFd,
#[error("There is trailing data after the message")]
TrailingData,
}
pub struct WlParser<'a, 'b> {
buf: &'a mut BufFdIn,
pos: usize,
data: &'b [u8],
}
impl<'a, 'b> WlParser<'a, 'b> {
pub fn new(buf: &'a mut BufFdIn, data: &'b [u32]) -> Self {
Self {
buf,
pos: 0,
data: unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 4) },
}
}
pub fn int(&mut self) -> Result<i32, WlParserError> {
if self.data.len() - self.pos < 4 {
return Err(WlParserError::UnexpectedEof);
}
let res = unsafe { *(self.data.as_ptr().add(self.pos) as *const i32) };
self.pos += 4;
Ok(res)
}
pub fn uint(&mut self) -> Result<u32, WlParserError> {
self.int().map(|i| i as u32)
}
pub fn object(&mut self) -> Result<ObjectId, WlParserError> {
self.int().map(|i| ObjectId::from_raw(i as u32))
}
pub fn global(&mut self) -> Result<GlobalName, WlParserError> {
self.int().map(|i| GlobalName::from_raw(i as u32))
}
pub fn fixed(&mut self) -> Result<f64, WlParserError> {
self.int().map(|i| i as f64 / 256.0)
}
pub fn string(&mut self) -> Result<&'b str, WlParserError> {
let len = self.uint()? as usize;
if len == 0 {
return Err(WlParserError::EmptyString);
}
let cap = (len + 3) & !3;
if cap > self.data.len() - self.pos {
return Err(WlParserError::UnexpectedEof);
}
let s = &self.data[self.pos..self.pos + len - 1];
let s = match std::str::from_utf8(s) {
Ok(s) => s,
_ => return Err(WlParserError::NonUtf8),
};
self.pos += cap;
Ok(s)
}
pub fn fd(&mut self) -> Result<OwnedFd, WlParserError> {
match self.buf.get_fd() {
Ok(fd) => Ok(fd),
_ => Err(WlParserError::MissingFd),
}
}
pub fn eof(&self) -> Result<(), WlParserError> {
if self.pos == self.data.len() {
Ok(())
} else {
Err(WlParserError::TrailingData)
}
}
}

45
src/utils/copyhashmap.rs Normal file
View file

@ -0,0 +1,45 @@
use ahash::AHashMap;
use std::cell::{RefCell, RefMut};
use std::hash::Hash;
pub struct CopyHashMap<K, V> {
map: RefCell<AHashMap<K, V>>,
}
impl<K, V> Default for CopyHashMap<K, V> {
fn default() -> Self {
Self {
map: Default::default(),
}
}
}
impl<K: Eq + Hash, V: Clone> CopyHashMap<K, V> {
pub fn new() -> Self {
Self::default()
}
pub fn set(&self, k: K, v: V) {
self.map.borrow_mut().insert(k, v);
}
pub fn get(&self, k: &K) -> Option<V> {
self.map.borrow_mut().get(k).cloned()
}
pub fn remove(&self, k: &K) -> Option<V> {
self.map.borrow_mut().remove(k)
}
pub fn contains(&self, k: &K) -> bool {
self.map.borrow_mut().contains_key(k)
}
pub fn lock(&self) -> RefMut<AHashMap<K, V>> {
self.map.borrow_mut()
}
pub fn clear(&self) {
self.map.borrow_mut().clear();
}
}

63
src/utils/lock.rs Normal file
View file

@ -0,0 +1,63 @@
use std::cell::{RefCell, RefMut};
use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::task::{Context, Poll, Waker};
pub struct AsyncLock<T> {
data: RefCell<T>,
waiters: RefCell<Vec<Waker>>,
}
impl<T> AsyncLock<T> {
pub fn lock<'a>(&'a self) -> LockedFuture<'a, T> {
LockedFuture { lock: self }
}
}
pub struct LockedFuture<'a, T> {
lock: &'a AsyncLock<T>,
}
impl<'a, T> Future for LockedFuture<'a, T> {
type Output = Locked<'a, T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Ok(data) = self.lock.data.try_borrow_mut() {
Poll::Ready(Locked {
data,
lock: self.lock,
})
} else {
self.lock.waiters.borrow_mut().push(cx.waker().clone());
Poll::Pending
}
}
}
pub struct Locked<'a, T> {
data: RefMut<'a, T>,
lock: &'a AsyncLock<T>,
}
impl<'a, T> Deref for Locked<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.data.deref()
}
}
impl<'a, T> DerefMut for Locked<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.data.deref_mut()
}
}
impl<'a, T> Drop for Locked<'a, T> {
fn drop(&mut self) {
for waiter in self.lock.waiters.borrow_mut().drain(..) {
waiter.wake();
}
}
}

7
src/utils/mod.rs Normal file
View file

@ -0,0 +1,7 @@
pub mod buffd;
pub mod copyhashmap;
pub mod lock;
pub mod numcell;
pub mod oneshot;
pub mod queue;
pub mod vec_ext;

38
src/utils/numcell.rs Normal file
View file

@ -0,0 +1,38 @@
use std::cell::Cell;
use std::ops::{Add, Sub};
#[derive(Default)]
pub struct NumCell<T> {
t: Cell<T>,
}
impl<T> NumCell<T> {
pub fn new(t: T) -> Self {
Self { t: Cell::new(t) }
}
pub fn load(&self) -> T
where
T: Copy,
{
self.t.get()
}
pub fn fetch_add(&self, n: T) -> T
where
T: Copy + Add<T, Output = T>,
{
let res = self.t.get();
self.t.set(res + n);
res
}
pub fn fetch_sub(&self, n: T) -> T
where
T: Copy + Sub<T, Output = T>,
{
let res = self.t.get();
self.t.set(res - n);
res
}
}

48
src/utils/oneshot.rs Normal file
View file

@ -0,0 +1,48 @@
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
pub fn oneshot<T>() -> (OneshotTx<T>, OneshotRx<T>) {
let os = Rc::new(Oneshot {
data: Cell::new(None),
waiter: Cell::new(None),
});
(OneshotTx { data: os.clone() }, OneshotRx { data: os })
}
struct Oneshot<T> {
data: Cell<Option<T>>,
waiter: Cell<Option<Waker>>,
}
pub struct OneshotTx<T> {
data: Rc<Oneshot<T>>,
}
pub struct OneshotRx<T> {
data: Rc<Oneshot<T>>,
}
impl<T> OneshotTx<T> {
pub fn send(self, t: T) {
self.data.data.set(Some(t));
if let Some(waiter) = self.data.waiter.replace(None) {
waiter.wake();
}
}
}
impl<T> Future for OneshotRx<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(data) = self.data.data.replace(None) {
Poll::Ready(data)
} else {
self.data.waiter.set(Some(cx.waker().clone()));
Poll::Pending
}
}
}

55
src/utils/queue.rs Normal file
View file

@ -0,0 +1,55 @@
use std::cell::RefCell;
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, Waker};
pub struct AsyncQueue<T> {
data: RefCell<VecDeque<T>>,
waiters: RefCell<Vec<Waker>>,
}
impl<T> AsyncQueue<T> {
pub fn new() -> Self {
Self {
data: RefCell::new(Default::default()),
waiters: RefCell::new(vec![]),
}
}
pub fn push(&self, t: T) {
self.data.borrow_mut().push_back(t);
for waiter in self.waiters.borrow_mut().drain(..) {
waiter.wake();
}
}
pub fn try_pop(&self) -> Option<T> {
self.data.borrow_mut().pop_front()
}
pub fn pop<'a>(&'a self) -> AsyncQueuePop<'a, T> {
AsyncQueuePop { queue: self }
}
pub fn size(&self) -> usize {
self.data.borrow().len()
}
}
pub struct AsyncQueuePop<'a, T> {
queue: &'a AsyncQueue<T>,
}
impl<'a, T> Future for AsyncQueuePop<'a, T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(t) = self.queue.try_pop() {
Poll::Ready(t)
} else {
self.queue.waiters.borrow_mut().push(cx.waker().clone());
Poll::Pending
}
}
}

23
src/utils/vec_ext.rs Normal file
View file

@ -0,0 +1,23 @@
use std::mem::MaybeUninit;
use std::ops::Range;
use std::slice;
pub trait VecExt<T> {
fn split_at_spare_mut_ext(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]);
}
impl<T> VecExt<T> for Vec<T> {
fn split_at_spare_mut_ext(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
let Range {
start: ptr,
end: spare_ptr,
} = self.as_mut_ptr_range();
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
let spare_len = self.capacity() - self.len();
unsafe {
let initialized = slice::from_raw_parts_mut(ptr, self.len());
let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
(initialized, spare)
}
}
}

207
src/wheel.rs Normal file
View file

@ -0,0 +1,207 @@
use crate::event_loop::{EventLoopDispatcher, EventLoopError, EventLoopId, EventLoopRef};
use crate::time::{Time, TimeError};
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::numcell::NumCell;
use std::cell::{Cell, RefCell};
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::rc::{Rc, Weak};
use std::time::Duration;
use thiserror::Error;
use uapi::{c, OwnedFd};
#[derive(Debug, Error)]
pub enum WheelError {
#[error("Could not create the timerfd: {0}")]
CreateFailed(std::io::Error),
#[error("Could not set the timerfd: {0}")]
SetFailed(std::io::Error),
#[error("The timerfd is in an error state")]
ErrorEvent,
#[error("An event loop error occurred: {0}")]
EventLoopError(#[from] EventLoopError),
#[error("Cannot determine the time: {0}")]
TimeError(#[from] TimeError),
#[error("The timer wheel is already destroyed")]
Destroyed,
}
pub trait WheelDispatcher {
fn dispatch(self: Rc<Self>) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
}
#[derive(Clone)]
pub struct WheelRef {
data: Weak<WheelData>,
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
struct WheelEntry {
expiration: Time,
id: u64,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct WheelId(u64);
struct WheelData {
id: EventLoopId,
fd: OwnedFd,
el: EventLoopRef,
next_id: NumCell<u64>,
start: Time,
current_expiration: Cell<Option<Time>>,
dispatchers: CopyHashMap<u64, Rc<dyn WheelDispatcher>>,
expirations: RefCell<BinaryHeap<Reverse<WheelEntry>>>,
}
impl WheelData {
fn new(el: &EventLoopRef) -> Result<Rc<Self>, WheelError> {
let fd = match uapi::timerfd_create(c::CLOCK_MONOTONIC, c::TFD_CLOEXEC | c::TFD_NONBLOCK) {
Ok(fd) => fd,
Err(e) => return Err(WheelError::CreateFailed(e.into())),
};
let id = el.id()?;
let wheel = Rc::new(Self {
id,
fd,
el: el.clone(),
next_id: Default::default(),
start: Time::now()?,
current_expiration: Cell::new(None),
dispatchers: CopyHashMap::new(),
expirations: RefCell::new(Default::default()),
});
el.insert(id, Some(wheel.fd.raw()), c::EPOLLIN, wheel.clone())?;
Ok(wheel)
}
fn id(&self) -> WheelId {
WheelId(self.next_id.fetch_add(1))
}
fn timeout(
&self,
id: WheelId,
ms: u64,
dispatcher: Rc<dyn WheelDispatcher>,
) -> Result<(), WheelError> {
let expiration = (Time::now()? + Duration::from_millis(ms)).round_to_ms();
let current = self.current_expiration.get();
if current.is_none() || expiration - self.start < current.unwrap() - self.start {
let res = uapi::timerfd_settime(
self.fd.raw(),
c::TFD_TIMER_ABSTIME,
&c::itimerspec {
it_interval: uapi::pod_zeroed(),
it_value: expiration.0,
},
);
if let Err(e) = res {
return Err(WheelError::SetFailed(e.into()));
}
self.current_expiration.set(Some(expiration));
}
self.expirations.borrow_mut().push(Reverse(WheelEntry {
expiration,
id: id.0,
}));
self.dispatchers.set(id.0, dispatcher);
Ok(())
}
fn remove(&self, id: WheelId) {
log::trace!("removing {:?} from wheel", id);
self.dispatchers.remove(&id.0);
}
}
impl EventLoopDispatcher for WheelData {
fn dispatch(&self, events: i32) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if events & (c::EPOLLERR | c::EPOLLHUP) != 0 {
return Err(Box::new(WheelError::ErrorEvent));
}
let mut n = 0u64;
while uapi::read(self.fd.raw(), &mut n).is_ok() {}
let now = match Time::now() {
Ok(n) => n,
Err(e) => return Err(Box::new(e)),
};
let dist = now - self.start;
let mut to_dispatch = vec![];
{
let mut expirations = self.expirations.borrow_mut();
while let Some(Reverse(entry)) = expirations.peek() {
if entry.expiration - self.start > dist {
break;
}
if let Some(dispatcher) = self.dispatchers.remove(&entry.id) {
to_dispatch.push(dispatcher);
}
expirations.pop();
}
self.current_expiration.set(None);
while let Some(Reverse(entry)) = expirations.peek() {
if self.dispatchers.get(&entry.id).is_some() {
let res = uapi::timerfd_settime(
self.fd.raw(),
c::TFD_TIMER_ABSTIME,
&c::itimerspec {
it_interval: uapi::pod_zeroed(),
it_value: entry.expiration.0,
},
);
if let Err(e) = res {
return Err(Box::new(WheelError::SetFailed(e.into())));
}
self.current_expiration.set(Some(entry.expiration));
break;
}
expirations.pop();
}
}
for dispatcher in to_dispatch {
dispatcher.dispatch()?;
}
Ok(())
}
}
impl Drop for WheelData {
fn drop(&mut self) {
let _ = self.el.remove(self.id);
}
}
impl WheelRef {
pub fn new(el: &EventLoopRef) -> Result<Self, WheelError> {
Ok(Self {
data: Rc::downgrade(&WheelData::new(el)?),
})
}
pub fn id(&self) -> Result<WheelId, WheelError> {
match self.data.upgrade() {
Some(d) => Ok(d.id()),
_ => Err(WheelError::Destroyed),
}
}
pub fn timeout(
&self,
id: WheelId,
ms: u64,
dispatcher: Rc<dyn WheelDispatcher>,
) -> Result<(), WheelError> {
match self.data.upgrade() {
Some(d) => d.timeout(id, ms, dispatcher),
_ => Err(WheelError::Destroyed),
}
}
pub fn remove(&self, id: WheelId) {
if let Some(wheel) = self.data.upgrade() {
wheel.remove(id);
}
}
}

420
src/wl_client.rs Normal file
View file

@ -0,0 +1,420 @@
use crate::async_engine::{AsyncError, AsyncFd, SpawnedFuture};
use crate::ifs::wl_display::WlDisplay;
use crate::objects::{Object, ObjectError, ObjectId, Objects, WL_DISPLAY_ID};
use crate::state::State;
use crate::utils::buffd::{BufFdError, BufFdIn, BufFdOut, WlFormatter, WlParser, WlParserError};
use crate::utils::copyhashmap::CopyHashMap;
use crate::utils::numcell::NumCell;
use crate::utils::oneshot::{oneshot, OneshotRx, OneshotTx};
use crate::utils::queue::AsyncQueue;
use crate::utils::vec_ext::VecExt;
use anyhow::anyhow;
use futures::{select, FutureExt};
use std::cell::Cell;
use std::fmt::{Debug, Display, Formatter};
use std::mem;
use std::rc::Rc;
use thiserror::Error;
use uapi::OwnedFd;
use crate::ifs::wl_region::WlRegion;
#[derive(Debug, Error)]
pub enum WlClientError {
#[error("An error occurred in the async engine")]
Async(#[from] AsyncError),
#[error("An error occurred reading from/writing to the client")]
Io(#[from] BufFdError),
#[error("An error occurred while processing a request")]
RequestError(#[source] Box<ObjectError>),
#[error("Client tried to invoke a non-existent method")]
InvalidMethod,
#[error("Client tried to access non-existent object {0}")]
InvalidObject(ObjectId),
#[error("The message size is < 8")]
MessageSizeTooSmall,
#[error("The size of the message is not a multiple of 4")]
UnalignedMessage,
#[error("The outgoing buffer overflowed")]
OutBufferOverflow,
#[error("The requested client {0} does not exist")]
ClientDoesNotExist(ClientId),
#[error(transparent)]
ObjectError(Box<ObjectError>),
#[error("There is no region with id {0}")]
RegionDoesNotExist(ObjectId),
}
efrom!(WlClientError, ObjectError, ObjectError);
impl WlClientError {
fn peer_closed(&self) -> bool {
match self {
WlClientError::Io(BufFdError::Closed) => true,
_ => false,
}
}
}
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct ClientId(u64);
impl Display for ClientId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
pub struct WlClients {
next_client_id: NumCell<u64>,
clients: CopyHashMap<ClientId, Rc<WlClient>>,
shutdown_clients: CopyHashMap<ClientId, Rc<WlClient>>,
}
impl WlClients {
pub fn new() -> Self {
Self {
next_client_id: NumCell::new(1),
clients: CopyHashMap::new(),
shutdown_clients: CopyHashMap::new(),
}
}
pub fn id(&self) -> ClientId {
ClientId(self.next_client_id.fetch_add(1))
}
pub fn get(&self, id: ClientId) -> Result<Rc<WlClientData>, WlClientError> {
match self.clients.get(&id) {
Some(c) => Ok(c.data.clone()),
_ => Err(WlClientError::ClientDoesNotExist(id)),
}
}
pub fn spawn(
&self,
id: ClientId,
global: &Rc<State>,
socket: OwnedFd,
) -> Result<(), WlClientError> {
let (send, recv) = oneshot();
let data = Rc::new(WlClientData {
id,
state: global.clone(),
socket: global.eng.fd(&Rc::new(socket))?,
objects: Objects::new(),
events: AsyncQueue::new(),
shutdown: Cell::new(Some(send)),
shutdown_sent: Cell::new(false),
});
data.objects
.add_client_object(Rc::new(WlDisplay::new(&data)))
.expect("");
let client = Rc::new(WlClient {
_handler: global.eng.spawn(client(data.clone(), recv)),
data,
});
self.clients.set(client.data.id, client.clone());
log::info!("Client {} connected", id);
Ok(())
}
pub fn kill(&self, client: ClientId) {
log::info!("Removing client {}", client.0);
if self.clients.remove(&client).is_none() {
self.shutdown_clients.remove(&client);
}
}
pub fn shutdown(&self, client_id: ClientId) {
if let Some(client) = self.clients.remove(&client_id) {
log::info!("Shutting down client {}", client.data.id.0);
client.data.shutdown.replace(None).unwrap().send(());
client.data.events.push(WlEvent::Shutdown);
client.data.shutdown_sent.set(true);
self.shutdown_clients.set(client_id, client);
}
}
pub fn broadcast<B>(&self, mut f: B)
where
B: FnMut(&Rc<WlClientData>),
{
let clients = self.clients.lock();
for client in clients.values() {
f(&client.data);
}
}
}
struct WlClient {
data: Rc<WlClientData>,
_handler: SpawnedFuture<()>,
}
impl Drop for WlClient {
fn drop(&mut self) {
self.data.objects.destroy();
}
}
pub trait EventFormatter: Debug {
fn format(self: Box<Self>, fmt: &mut WlFormatter<'_>);
fn obj(&self) -> &dyn Object;
}
pub type DynEventFormatter = Box<dyn EventFormatter>;
pub trait RequestParser<'a>: Debug + Sized {
fn parse(parser: &mut WlParser<'_, 'a>) -> Result<Self, WlParserError>;
}
enum WlEvent {
Flush,
Shutdown,
Event(Box<dyn EventFormatter>),
}
pub struct WlClientData {
pub id: ClientId,
pub state: Rc<State>,
socket: AsyncFd,
pub objects: Objects,
events: AsyncQueue<WlEvent>,
shutdown: Cell<Option<OneshotTx<()>>>,
shutdown_sent: Cell<bool>,
}
const MAX_PENDING_EVENTS: usize = 100;
impl WlClientData {
pub fn invalid_request(&self, obj: &dyn Object, request: u32) {
log::error!(
"Client {} sent an invalid request {} on object {} of type {}",
self.id.0,
request,
obj.id(),
obj.interface().name(),
);
match self.display() {
Ok(d) => self.fatal_event(d.invalid_request(obj, request)),
Err(e) => {
log::error!(
"Could not retrieve display of client {}: {:#}",
self.id,
anyhow!(e)
);
self.state.clients.kill(self.id);
}
}
}
pub fn display(&self) -> Result<Rc<WlDisplay>, WlClientError> {
Ok(self.objects.get_obj(WL_DISPLAY_ID)?.into_display()?)
}
pub fn parse<'a, R: RequestParser<'a>>(
&self,
obj: &impl Object,
mut parser: WlParser<'_, 'a>,
) -> Result<R, WlParserError> {
let res = R::parse(&mut parser)?;
parser.eof()?;
log::trace!(
"Client {} -> {}@{}.{:?}",
self.id,
obj.interface().name(),
obj.id(),
res
);
Ok(res)
}
pub fn fatal_event(&self, event: Box<dyn EventFormatter>) {
self.events.push(WlEvent::Event(event));
self.state.clients.shutdown(self.id);
}
pub fn event_locked(&self, event: Box<dyn EventFormatter>) -> bool {
self.events.push(WlEvent::Event(event));
self.events.size() > MAX_PENDING_EVENTS
}
pub async fn event(&self, event: Box<dyn EventFormatter>) -> Result<(), WlClientError> {
self.event2(WlEvent::Event(event)).await
}
async fn event2(&self, event: WlEvent) -> Result<(), WlClientError> {
self.events.push(event);
self.check_queue_size().await
}
pub async fn check_queue_size(&self) -> Result<(), WlClientError> {
if self.events.size() > MAX_PENDING_EVENTS {
self.state.eng.yield_now().await;
if self.events.size() > MAX_PENDING_EVENTS {
log::error!("Client {} is too slow at fetching events", self.id.0);
self.state.clients.kill(self.id);
return Err(WlClientError::OutBufferOverflow);
}
}
Ok(())
}
pub fn attach_client_object(&self, obj: Rc<dyn Object>) -> Result<(), WlClientError> {
self.objects.add_client_object(obj.clone())?;
obj.post_attach();
Ok(())
}
pub fn get_region(&self, id: ObjectId) -> Result<Rc<WlRegion>, WlClientError> {
match self.objects.regions.get(&id) {
Some(r) => Ok(r),
_ => Err(WlClientError::RegionDoesNotExist(id)),
}
}
}
async fn client(data: Rc<WlClientData>, shutdown: OneshotRx<()>) {
let mut recv = data.state.eng.spawn(receive(data.clone())).fuse();
let _send = data.state.eng.spawn(send(data.clone()));
select! {
_ = recv => { },
_ = shutdown.fuse() => { },
}
drop(recv);
if !data.shutdown_sent.get() {
data.events.push(WlEvent::Shutdown);
}
match data.state.eng.timeout(5000) {
Ok(timeout) => {
timeout.await;
log::error!("Could not shut down client {} within 5 seconds", data.id.0);
}
Err(e) => {
log::error!("Could not create a timeout: {:#}", e);
}
}
data.state.clients.kill(data.id);
}
async fn receive(data: Rc<WlClientData>) {
let display = data.display().unwrap();
let recv = async {
let mut buf = BufFdIn::new(data.socket.clone());
let mut data_buf = Vec::<u32>::new();
loop {
let mut hdr = [0u32, 0];
buf.read_full(&mut hdr[..]).await?;
let obj_id = ObjectId::from_raw(hdr[0]);
let len = (hdr[1] >> 16) as usize;
let request = hdr[1] & 0xffff;
let obj = match data.objects.get_obj(obj_id) {
Ok(obj) => obj,
_ => {
data.fatal_event(display.invalid_object(obj_id));
return Err(WlClientError::InvalidObject(obj_id));
}
};
// log::trace!("obj: {}, request: {}, len: {}", obj_id, request, len);
if request >= obj.num_requests() {
data.invalid_request(&*obj, request);
return Err(WlClientError::InvalidMethod);
}
if len < 8 {
return Err(WlClientError::MessageSizeTooSmall);
}
if len % 4 != 0 {
return Err(WlClientError::UnalignedMessage);
}
let len = len / 4 - 2;
data_buf.clear();
data_buf.reserve(len);
let unused = data_buf.split_at_spare_mut_ext().1;
buf.read_full(&mut unused[..len]).await?;
unsafe {
data_buf.set_len(len);
}
// log::trace!("{:x?}", data_buf);
let parser = WlParser::new(&mut buf, &data_buf[..]);
if let Err(e) = obj.handle_request(request, parser).await {
return Err(WlClientError::RequestError(Box::new(e)));
}
data.event2(WlEvent::Flush).await?;
}
};
let res: Result<(), WlClientError> = recv.await;
if let Err(e) = res {
if e.peer_closed() {
log::info!("Client {} terminated the connection", data.id.0);
data.state.clients.kill(data.id);
} else {
let e = anyhow!(e);
log::error!(
"An error occurred while trying to handle a message from client {}: {:#}",
data.id.0,
e
);
if !data.shutdown_sent.get() {
data.fatal_event(display.implementation_error(format!("{:#}", e)));
}
}
}
}
async fn send(data: Rc<WlClientData>) {
let send = async {
let mut buf = BufFdOut::new(data.socket.clone());
let mut flush_requested = false;
loop {
let mut event = data.events.pop().await;
loop {
match event {
WlEvent::Flush => {
flush_requested = true;
}
WlEvent::Shutdown => {
buf.flush().await?;
return Ok(());
}
WlEvent::Event(e) => {
if log::log_enabled!(log::Level::Trace) {
let obj = e.obj();
log::trace!(
"Client {} <= {}@{}.{:?}",
data.id,
obj.interface().name(),
obj.id(),
e
);
}
e.format(&mut WlFormatter::new(&mut buf));
if buf.needs_flush() {
buf.flush().await?;
flush_requested = false;
}
}
}
event = match data.events.try_pop() {
Some(e) => e,
_ => break,
};
}
if mem::take(&mut flush_requested) {
buf.flush().await?;
}
}
};
let res: Result<(), WlClientError> = send.await;
if let Err(e) = res {
if e.peer_closed() {
log::info!("Client {} terminated the connection", data.id.0);
} else {
log::error!(
"An error occurred while sending data to client {}: {:#}",
data.id.0,
e
);
}
}
data.state.clients.kill(data.id);
}