feat: add support to trigger refresh through external events.

- Allow users to trigger a dispatch by using
`Res<ExternalEventDispatcher>`.
This commit is contained in:
Naman Agrawal
2025-08-28 17:30:43 +05:30
parent a91508672e
commit 541e0e646f
2 changed files with 178 additions and 8 deletions
+42 -8
View File
@@ -1,9 +1,15 @@
use std::{
num::NonZero,
sync::mpsc::SendError,
time::{Duration, Instant},
};
use bevy::{app::PluginsState, prelude::*};
use smithay_client_toolkit::{
delegate_registry,
output::OutputState,
reexports::{
calloop::EventLoop,
calloop::{self, channel::Sender, EventLoop},
calloop_wayland_source::WaylandSource,
client::{globals::registry_queue_init, Connection},
},
@@ -27,23 +33,46 @@ pub mod prelude {
pub use smithay_client_toolkit::shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer};
}
pub struct Tick;
#[derive(Resource, Clone)]
pub struct ExternalEventDispatcher(Sender<Tick>);
impl ExternalEventDispatcher {
fn new(tx: Sender<Tick>) -> Self {
Self(tx)
}
pub fn dispatch(&self) -> Result<(), SendError<Tick>> {
self.0.send(Tick)
}
}
#[derive(Default)]
pub struct WaylandPlugin;
impl Plugin for WaylandPlugin {
fn build(&self, app: &mut App) {
let connection =
Connection::connect_to_env().expect("failed to connect to wayland socket!");
Connection::connect_to_env().expect("Failed to connect to wayland socket!");
let event_loop =
EventLoop::<WaylandState>::try_new().expect("failed to create event_loop!");
EventLoop::<WaylandState>::try_new().expect("Failed to create event_loop!");
let (globals, event_queue) = registry_queue_init::<WaylandState>(&connection)
.expect("failed to init registry queue");
.expect("Failed to init registry queue");
let qh = event_queue.handle();
let loop_handle = event_loop.handle();
WaylandSource::new(connection.clone(), event_queue)
.insert(loop_handle.clone())
.expect("failed to insert wayland source to event loop");
.expect("Failed to insert wayland source to event loop");
let (tx, rx) = calloop::channel::channel::<Tick>();
loop_handle
.insert_source(rx, |_, _, state| {
info!("External event was received!");
if state.plugins_state() == PluginsState::Cleaned {
state.update();
}
})
.expect("Failed to insert external tick channel!");
app.insert_resource(ExternalEventDispatcher::new(tx));
app.insert_non_send_resource(RegistryState::new(&globals));
app.insert_non_send_resource(connection.clone());
app.insert_non_send_resource(globals);
@@ -66,14 +95,19 @@ pub fn runner(mut app: App, mut event_loop: EventLoop<'_, WaylandState>) -> AppE
app.finish();
app.cleanup();
}
let mut state = WaylandState(app);
loop {
// TODO: Error handling
let frame_start = Instant::now();
let _ = event_loop.dispatch(Duration::from_millis(5000), &mut state);
if state.plugins_state() == PluginsState::Cleaned {
state.update();
}
let _ = event_loop.dispatch(None, &mut state);
let _ = event_loop.dispatch(Duration::from_millis(0), &mut state);
// TODO: Poll until delta time is greater than target frame time.
if Instant::now() - frame_start < Duration::from_millis(16) {
std::thread::sleep(Duration::from_millis(16) - (frame_start - Instant::now()));
}
let _ = event_loop.dispatch(Duration::from_millis(0), &mut state);
}
}