Skip to content

Commit

Permalink
client: Implement AsFd for Connection and EventQueue
Browse files Browse the repository at this point in the history
This helps for using an `Generic<EventQueue>` with calloop 0.11.
  • Loading branch information
ids1024 committed Sep 18, 2023
1 parent cf825be commit b2c9e76
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions wayland-backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

#### Additions

- client: Add `Backend::poll_fd` to return fd for polling

## 0.3.0 -- 2023-09-02

#### Breaking change
Expand Down
6 changes: 6 additions & 0 deletions wayland-backend/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl Backend {
self.backend.flush()
}

/// Access the Wayland socket FD for polling
#[inline]
pub fn poll_fd(&self) -> BorrowedFd {
self.backend.poll_fd()
}

Check warning on line 188 in wayland-backend/src/client_api.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/client_api.rs#L186-L188

Added lines #L186 - L188 were not covered by tests

/// Get the object ID for the `wl_display`
#[inline]
pub fn display_id(&self) -> ObjectId {
Expand Down
7 changes: 7 additions & 0 deletions wayland-backend/src/rs/client_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ impl InnerBackend {
}
Ok(())
}

pub fn poll_fd(&self) -> BorrowedFd {
let raw_fd = self.state.lock_protocol().socket.as_raw_fd();
// This allows the lifetime of the BorrowedFd to be tied to &self rather than the lock guard,
// which is the real safety concern
unsafe { BorrowedFd::borrow_raw(raw_fd) }
}

Check warning on line 200 in wayland-backend/src/rs/client_impl/mod.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/rs/client_impl/mod.rs#L195-L200

Added lines #L195 - L200 were not covered by tests
}

#[derive(Debug)]
Expand Down
11 changes: 11 additions & 0 deletions wayland-backend/src/sys/client_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ impl InnerBackend {
}
}

pub fn poll_fd(&self) -> BorrowedFd {
let guard = self.lock_state();
unsafe {
BorrowedFd::borrow_raw(ffi_dispatch!(
wayland_client_handle(),
wl_display_get_fd,
guard.display
))
}
}

Check warning on line 305 in wayland-backend/src/sys/client_impl/mod.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/sys/client_impl/mod.rs#L296-L305

Added lines #L296 - L305 were not covered by tests

pub fn dispatch_inner_queue(&self) -> Result<usize, WaylandError> {
self.inner.dispatch_lock.lock().unwrap().dispatch_pending(self.inner.clone())
}
Expand Down
5 changes: 5 additions & 0 deletions wayland-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

#### Additions

- Implement `AsFd` for `Connection` and `EventQueue` so they can easily be used in a
`calloop` source.

## 0.31.0 -- 2023-09-02

#### Breaking changes
Expand Down
9 changes: 8 additions & 1 deletion wayland-client/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
io::ErrorKind,
os::unix::io::OwnedFd,
os::unix::net::UnixStream,
os::unix::prelude::{AsRawFd, FromRawFd},
os::unix::prelude::{AsFd, AsRawFd, BorrowedFd, FromRawFd},
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -273,6 +273,13 @@ impl fmt::Display for ConnectError {
}
}

impl AsFd for Connection {
/// Provides fd from [`Backend::poll_fd`] for polling.
fn as_fd(&self) -> BorrowedFd<'_> {
self.backend.poll_fd()
}

Check warning on line 280 in wayland-client/src/conn.rs

View check run for this annotation

Codecov / codecov/patch

wayland-client/src/conn.rs#L278-L280

Added lines #L278 - L280 were not covered by tests
}

/*
wl_callback object data for wl_display.sync
*/
Expand Down
9 changes: 8 additions & 1 deletion wayland-client/src/event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::collections::VecDeque;
use std::convert::Infallible;
use std::marker::PhantomData;
use std::os::unix::io::OwnedFd;
use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
use std::sync::{atomic::Ordering, Arc, Condvar, Mutex};
use std::task;

Expand Down Expand Up @@ -349,6 +349,13 @@ impl<State> std::fmt::Debug for EventQueue<State> {
}
}

impl<State> AsFd for EventQueue<State> {
/// Provides fd from [`Backend::poll_fd`] for polling.
fn as_fd(&self) -> BorrowedFd<'_> {
self.conn.as_fd()
}

Check warning on line 356 in wayland-client/src/event_queue.rs

View check run for this annotation

Codecov / codecov/patch

wayland-client/src/event_queue.rs#L354-L356

Added lines #L354 - L356 were not covered by tests
}

impl<State> EventQueue<State> {
pub(crate) fn new(conn: Connection) -> Self {
let inner = Arc::new(Mutex::new(EventQueueInner {
Expand Down

0 comments on commit b2c9e76

Please sign in to comment.