Struct wayland_client::EventQueue
source · [−]pub struct EventQueue<D> { /* private fields */ }
Expand description
An event queue
This is an abstraction for handling event dispatching, that allows you to ensure
access to some common state &mut D
to your event handlers.
Event queues are created through Connection::new_event_queue()
.
Upon creation, a wayland object is assigned to an event queue by passing the associated QueueHandle
as argument to the method creating it. All event received by that object will be processed by that event
queue, when dispatch_pending()
or
blocking_dispatch()
is invoked.
Implementations
sourceimpl<D> EventQueue<D>
impl<D> EventQueue<D>
sourcepub fn handle(&self) -> QueueHandle<D>
pub fn handle(&self) -> QueueHandle<D>
Get a QueueHandle
for this event queue
sourcepub fn dispatch_pending(&mut self, data: &mut D) -> Result<usize, DispatchError>
pub fn dispatch_pending(&mut self, data: &mut D) -> Result<usize, DispatchError>
Dispatch pending events
Events are accumulated in the event queue internal buffer when the Wayland socket is read using
the read APIs on Connection
, or when reading is done from an other thread.
This method will dispatch all such pending events by sequentially invoking their associated handlers:
the Dispatch
implementations on the provided &mut D
.
sourcepub fn blocking_dispatch(
&mut self,
data: &mut D
) -> Result<usize, DispatchError>
pub fn blocking_dispatch(
&mut self,
data: &mut D
) -> Result<usize, DispatchError>
Block waiting for events and dispatch them
This method is similar to dispatch_pending
, but if there are no
pending events it will also block waiting for the Wayland server to send an event.
A simple app event loop can consist of invoking this method in a loop.
sourcepub fn sync_roundtrip(&mut self, data: &mut D) -> Result<usize, DispatchError>
pub fn sync_roundtrip(&mut self, data: &mut D) -> Result<usize, DispatchError>
Synchronous roundtrip
This function will cause a synchronous round trip with the wayland server. This function will block until all requests in the queue are sent and processed by the server.
This function may be useful during initial setup with the compositor. This function may also be useful where you need to guarantee all requests prior to calling this function are completed.
sourcepub fn prepare_read(&self) -> Result<ReadEventsGuard, WaylandError>
pub fn prepare_read(&self) -> Result<ReadEventsGuard, WaylandError>
Start a synchronized read from the socket
This is needed if you plan to wait on readiness of the Wayland socket using an event
loop. See ReadEventsGuard
for details. Once the events are received, you’ll then
need to dispatch them from the event queue using
EventQueue::dispatch_pending()
.
If you don’t need to manage multiple event sources, see
blocking_dispatch()
for a simpler mechanism.
sourcepub fn flush(&self) -> Result<(), WaylandError>
pub fn flush(&self) -> Result<(), WaylandError>
Flush pending outgoing events to the server
This needs to be done regularly to ensure the server receives all your requests.
sourcepub fn poll_dispatch_pending(
&mut self,
cx: &mut Context<'_>,
data: &mut D
) -> Poll<Result<Infallible, DispatchError>>
pub fn poll_dispatch_pending(
&mut self,
cx: &mut Context<'_>,
data: &mut D
) -> Poll<Result<Infallible, DispatchError>>
Attempt to dispatch events from this queue, registering the current task for wakeup if no events are pending.
This method is similar to dispatch_pending
; it will not
perform reads on the Wayland socket. Reads on the socket by other tasks or threads will
cause the current task to wake up if events are pending on this queue.
use futures_channel::mpsc::Receiver;
use futures_util::future::{poll_fn,select};
use futures_util::stream::StreamExt;
use wayland_client::EventQueue;
struct Data;
enum AppEvent {
SomethingHappened(u32),
}
impl Data {
fn handle(&mut self, event: AppEvent) {
// actual event handling goes here
}
}
// An async task that is spawned on an executor in order to handle events that need access
// to a specific data object.
async fn run(data: &mut Data, mut wl_queue: EventQueue<Data>, mut app_queue: Receiver<AppEvent>)
-> Result<(), Box<dyn std::error::Error>>
{
use futures_util::future::Either;
loop {
match select(
poll_fn(|cx| wl_queue.poll_dispatch_pending(cx, data)),
app_queue.next(),
).await {
Either::Left((res, _)) => match res? {},
Either::Right((Some(event), _)) => {
data.handle(event);
}
Either::Right((None, _)) => return Ok(()),
}
}
}
Trait Implementations
Auto Trait Implementations
impl<D> !RefUnwindSafe for EventQueue<D>
impl<D> Send for EventQueue<D>
impl<D> Sync for EventQueue<D>
impl<D> Unpin for EventQueue<D>
impl<D> !UnwindSafe for EventQueue<D>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more