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 authored and elinorbgr committed Sep 19, 2023
1 parent cf825be commit f7b0d58
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()
}

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

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

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

/*
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()
}
}

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

0 comments on commit f7b0d58

Please sign in to comment.