Skip to content

Commit

Permalink
[vent-window] Wayland: refactor event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jul 23, 2024
1 parent 3e34b5d commit f1bda5b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 97 deletions.
2 changes: 1 addition & 1 deletion crates/vent-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl VentApplication {
// TODO
let mut renderer = DefaultRuntimeRenderer::new(&project, &app_window);

let mut controller = CameraController3D::new(30.0, 1.0);
let mut controller = CameraController3D::new(100.0, 1.0);
let mut delta_time = 0.0;

// TODO, Handle scale factor change
Expand Down
4 changes: 3 additions & 1 deletion crates/vent-window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod keyboard;
pub mod mouse;
pub mod platform;

type EventHandler = dyn FnMut(WindowEvent);

Check failure on line 9 in crates/vent-window/src/lib.rs

View workflow job for this annotation

GitHub Actions / Windows Build

type alias `EventHandler` is never used

#[derive(PartialEq, Clone)]
pub enum WindowEvent {
Close,
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Window {

pub fn poll<F>(mut self, event_handler: F)
where
F: FnMut(WindowEvent),
F: FnMut(WindowEvent) + 'static,
{
self.window.poll(event_handler);
}
Expand Down
148 changes: 53 additions & 95 deletions crates/vent-window/src/platform/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
use std::{
num::NonZeroU32,
ptr::NonNull,
sync::{
mpsc::{channel, Receiver, Sender},
Arc,
},
time::Duration,
};
use std::{num::NonZeroU32, ptr::NonNull, sync::Arc, time::Duration};

use raw_window_handle::{
RawDisplayHandle, RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle,
Expand Down Expand Up @@ -53,7 +45,7 @@ use xkbcommon::xkb;

use crate::{
keyboard::{Key, KeyState},
mouse, WindowAttribs, WindowEvent,
mouse, EventHandler, WindowAttribs, WindowEvent,
};

pub struct PlatformWindow {
Expand Down Expand Up @@ -88,8 +80,7 @@ struct WaylandWindow {
keyboard_focus: bool,
themed_pointer: Option<ThemedPointer>,

event_sender: Sender<WindowEvent>,
event_receiver: Receiver<WindowEvent>,
event_handler: Option<Box<EventHandler>>,
running: bool,
}

Expand Down Expand Up @@ -174,7 +165,7 @@ impl WaylandWindow {
frame.draw();
}
}
self.event_sender.send(WindowEvent::Draw).unwrap();
(self.event_handler.as_mut().unwrap())(WindowEvent::Draw);
// wl_surface.damage_buffer is the preferred method of setting the damage region
// on compositor version 4 and above.
self.window
Expand All @@ -189,7 +180,8 @@ impl WaylandWindow {
pub fn close(&mut self) {
if self.running {
log::warn!("Closing wayland window...");
self.event_sender.send(WindowEvent::Close).unwrap();
(self.event_handler.as_mut().unwrap())(WindowEvent::Close);

self.running = false;
}
}
Expand Down Expand Up @@ -318,12 +310,10 @@ impl KeyboardHandler for WaylandWindow {
_serial: u32,
event: sctk::seat::keyboard::KeyEvent,
) {
self.event_sender
.send(WindowEvent::Key {
key: convert_key(event.keysym.raw()),
state: KeyState::Pressed,
})
.expect("Failed to send key event");
(self.event_handler.as_mut().unwrap())(WindowEvent::Key {
key: convert_key(event.keysym.raw()),
state: KeyState::Pressed,
})
}

fn release_key(
Expand All @@ -334,12 +324,10 @@ impl KeyboardHandler for WaylandWindow {
_serial: u32,
event: sctk::seat::keyboard::KeyEvent,
) {
self.event_sender
.send(WindowEvent::Key {
key: convert_key(event.keysym.raw()),
state: KeyState::Released,
})
.expect("Failed to send key event");
(self.event_handler.as_mut().unwrap())(WindowEvent::Key {
key: convert_key(event.keysym.raw()),
state: KeyState::Released,
});
}

fn update_modifiers(
Expand Down Expand Up @@ -399,9 +387,7 @@ impl PointerHandler for WaylandWindow {
self.set_cursor = true;
self.decorations_cursor = Some(new_cursor);
}
self.event_sender
.send(WindowEvent::MouseMotion { x, y })
.unwrap();
(self.event_handler.as_mut().unwrap())(WindowEvent::MouseMotion { x, y });
}
PointerEventKind::Press {
button,
Expand Down Expand Up @@ -440,57 +426,36 @@ impl PointerHandler for WaylandWindow {
}
}

fn press_mouse(button: u32, state: &WaylandWindow, mouse_state: mouse::ButtonState) {
fn press_mouse(button: u32, state: &mut WaylandWindow, mouse_state: mouse::ButtonState) {
match button {
BTN_LEFT => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::LEFT,
state: mouse_state,
})
.unwrap(),
BTN_RIGHT => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::RIGHT,
state: mouse_state,
})
.unwrap(),
BTN_MIDDLE => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::MIDDLE,
state: mouse_state,
})
.unwrap(),
BTN_SIDE => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::SIDE,
state: mouse_state,
})
.unwrap(),
BTN_EXTRA => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::EXTRA,
state: mouse_state,
})
.unwrap(),
BTN_FORWARD => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::FORWARD,
state: mouse_state,
})
.unwrap(),
BTN_BACK => state
.event_sender
.send(WindowEvent::MouseButton {
button: crate::mouse::Button::BACK,
state: mouse_state,
})
.unwrap(),
BTN_LEFT => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::LEFT,
state: mouse_state,
}),
BTN_RIGHT => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::RIGHT,
state: mouse_state,
}),
BTN_MIDDLE => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::MIDDLE,
state: mouse_state,
}),
BTN_SIDE => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::SIDE,
state: mouse_state,
}),
BTN_EXTRA => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::EXTRA,
state: mouse_state,
}),
BTN_FORWARD => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::FORWARD,
state: mouse_state,
}),
BTN_BACK => (state.event_handler.as_mut().unwrap())(WindowEvent::MouseButton {
button: crate::mouse::Button::BACK,
state: mouse_state,
}),
_ => (),
}
}
Expand Down Expand Up @@ -646,12 +611,10 @@ impl WindowHandler for WaylandWindow {
// Update new width and height;
self.attribs.width = width;
self.attribs.height = height;
self.event_sender
.send(WindowEvent::Resize {
new_width: width.into(),
new_height: height.into(),
})
.unwrap();
(self.event_handler.as_mut().unwrap())(WindowEvent::Resize {
new_width: width.into(),
new_height: height.into(),
});

// Initiate the first draw.
if self.configured {
Expand All @@ -666,8 +629,6 @@ impl PlatformWindow {
let conn = wayland_client::Connection::connect_to_env().expect("Failed to get connection");
log::debug!("Connected to Wayland Server");

let (event_sender, event_receiver) = channel::<WindowEvent>();

let (globals, event_queue) = registry_queue_init(&conn).unwrap();
let qhandle = event_queue.handle();
let event_loop: EventLoop<WaylandWindow> =
Expand Down Expand Up @@ -701,8 +662,6 @@ impl PlatformWindow {
configured: true,
output_state,
seat_state,
event_receiver,
event_sender,
_xdg_shell_state: xdg_shell_state,
registry_state,
shm_state,
Expand All @@ -715,6 +674,7 @@ impl PlatformWindow {
decorations_cursor: None,
set_cursor: false,
themed_pointer: None,
event_handler: None,
running: true,
};

Expand All @@ -727,19 +687,17 @@ impl PlatformWindow {
}
}

pub fn poll<F>(&mut self, mut event_handler: F)
pub fn poll<F>(&mut self, event_handler: F)
where
F: FnMut(WindowEvent),
F: FnMut(WindowEvent) + 'static,
{
self.state.event_handler = Some(Box::new(event_handler));

while self.state.running {
self.connection.flush().unwrap();
self.event_loop
.dispatch(Duration::from_millis(16), &mut self.state)
.expect("Failed to dispatch pending");

while let Ok(event) = self.state.event_receiver.try_recv() {
event_handler(event);
}
}
}

Expand Down

0 comments on commit f1bda5b

Please sign in to comment.