Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: Implement AsFd for Connection and EventQueue #656

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

ids1024 marked this conversation as resolved.
Show resolved Hide resolved
## 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 @@
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 @@
}
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 @@
}
}

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.
ids1024 marked this conversation as resolved.
Show resolved Hide resolved

## 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 @@
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 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::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 @@ -31,14 +31,14 @@
///
/// In the rare case of an interface with *events* creating new objects (in the core protocol, the only
/// instance of this is the `wl_data_device.data_offer` event), you'll need to implement the
/// [`Dispatch::event_created_child()`] method. See the [`event_created_child!`](event_created_child!) macro

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

View workflow job for this annotation

GitHub Actions / Documentation on Github Pages

unresolved link to `event_created_child`
/// for a simple way to do this.
///
/// ## Modularity
///
/// To provide generic handlers for downstream usage, it is possible to make an implementation of the trait
/// that is generic over the last type argument, as illustrated below. Users will then be able to
/// automatically delegate their implementation to yours using the [`delegate_dispatch!`] macro.

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

View workflow job for this annotation

GitHub Actions / Documentation on Github Pages

unresolved link to `delegate_dispatch`
///
/// As a result, when your implementation is instanciated, the last type parameter `State` will be the state
/// struct of the app using your generic implementation. You can put additional trait constraints on it to
Expand Down Expand Up @@ -118,7 +118,7 @@
/// Method used to initialize the user-data of objects created by events
///
/// If the interface does not have any such event, you can ignore it. If not, the
/// [`event_created_child!`](event_created_child!) macro is provided for overriding it.

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

View workflow job for this annotation

GitHub Actions / Documentation on Github Pages

unresolved link to `event_created_child`
#[cfg_attr(coverage, coverage(off))]
fn event_created_child(opcode: u16, _qhandle: &QueueHandle<State>) -> Arc<dyn ObjectData> {
panic!(
Expand Down Expand Up @@ -349,6 +349,13 @@
}
}

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
Loading