Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

capture mouse in gecko app

Layle65a063561303
diff --git a/Cargo.lock b/Cargo.lock
index 3f04fb9..3a1d3a7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2389,8 +2389,12 @@ dependencies = [
"tracing",
"tracing-subscriber",
"walkdir",
+ "wayland-client",
+ "wayland-protocols",
"webbrowser",
"wgpu",
+ "windows-sys 0.61.2",
+ "x11-dl",
"zerocopy",
"zip",
]
diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml
index dfe573b..636bcba 100644
--- a/crates/app/Cargo.toml
+++ b/crates/app/Cargo.toml
@@ -38,3 +38,11 @@ anyhow = "1.0.102"
webbrowser = "1.0.5"
reqwest = { version = "0.13.4", features = ["json"] }
tempfile = "3.27.0"
+
+[target.'cfg(target_os = "linux")'.dependencies]
+wayland-client = { version = "0.31.14", features = ["system", "dlopen"] }
+wayland-protocols = { version = "0.32.12", features = ["client", "unstable"] }
+x11-dl = "2.21.0"
+
+[target.'cfg(windows)'.dependencies]
+windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_UI_WindowsAndMessaging", "Win32_Graphics_Gdi"] }
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 645a2a6..d67bd03 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::path::PathBuf;
-use std::sync::Arc;
+use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use backend_wgpu::capture;
@@ -42,6 +42,8 @@ pub enum Message {
LibraryWindowOpened(window::Id),
PlayerWindowOpened(window::Id, Box<Game>, Arc<PlayerState>),
PlayerTick,
+ PlayerCaptureMouse(window::Id, bool),
+ PlayerFocused(window::Id, bool),
WindowClosed(window::Id),
LibraryPicked(Platform, Option<PathBuf>),
ScanRequested,
@@ -130,6 +132,9 @@ pub struct PlayerWindow {
pub(crate) game: Game,
pub(crate) state: Arc<PlayerState>,
pub(crate) fullscreen: bool,
+ pub(crate) mouse_capture: Arc<Mutex<player::MouseCapture>>,
+ pub(crate) capture_intent: bool,
+ pub(crate) captured: bool,
pub(crate) overlay: bool,
pub(crate) toast: Option<Toast>,
}
@@ -378,15 +383,41 @@ impl App {
game: *game,
state,
fullscreen: false,
+ mouse_capture: Arc::new(Mutex::new(player::MouseCapture::default())),
+ capture_intent: false,
+ captured: false,
overlay: false,
toast: None,
},
);
Task::none()
}
+ Message::PlayerCaptureMouse(id, capture) => {
+ let Some(player) = self.players.get_mut(&id) else {
+ return Task::none();
+ };
+
+ player.capture_intent = capture;
+ self.apply_mouse_capture(id, capture)
+ }
+ Message::PlayerFocused(id, focused) => {
+ let Some(player) = self.players.get(&id) else {
+ return Task::none();
+ };
+
+ if !focused {
+ player.state.clear_input();
+ }
+
+ let capture = focused && player.capture_intent;
+ self.apply_mouse_capture(id, capture)
+ }
Message::PlayerTick => {
let now = Instant::now();
for player in self.players.values_mut() {
+ if player.captured {
+ player.mouse_capture.lock().unwrap().poll();
+ }
if player.toast.as_ref().is_some_and(|t| now >= t.expires) {
player.toast = None;
}
@@ -980,7 +1011,8 @@ impl App {
let text_color = palette.text;
let status = player.state.status();
- let shader: Element<'a, Message> = player::shader_widget(player.state.clone(), window_id).into();
+ let shader: Element<'a, Message> =
+ player::shader_widget(player.state.clone(), window_id, player.captured).into();
let overlay: Option<Element<'a, Message>> = match &status {
PlayerStatus::Ready => None,
@@ -1022,6 +1054,28 @@ impl App {
.into()
}
+ fn apply_mouse_capture(&mut self, id: window::Id, capture: bool) -> Task<Message> {
+ let Some(player) = self.players.get_mut(&id) else {
+ return Task::none();
+ };
+
+ let capture = capture && player.game.platform == Platform::Wii;
+ if player.captured == capture && !(capture && player::MouseCapture::RECLIP_ON_MOVE) {
+ return Task::none();
+ }
+
+ player.captured = capture;
+ let mouse_capture = player.mouse_capture.clone();
+
+ window::run(id, move |window| {
+ if let Err(error) = mouse_capture.lock().unwrap().set(window, capture) {
+ tracing::warn!(%error, "failed to change player mouse capture");
+ }
+ Message::Noop
+ })
+ .discard()
+ }
+
fn idle_setup(&mut self) -> Option<&mut Setup> {
self.setup.as_mut().filter(|s| !s.busy)
}
diff --git a/crates/app/src/keybinds.rs b/crates/app/src/keybinds.rs
index 8ef8cea..46f4b47 100644
--- a/crates/app/src/keybinds.rs
+++ b/crates/app/src/keybinds.rs
@@ -61,6 +61,7 @@ pub struct HotkeysConfig {
pub screenshot: Option<String>,
pub save_state: Option<String>,
pub load_state: Option<String>,
+ pub release_mouse: Option<String>,
}
#[derive(Debug, Clone, Copy)]
@@ -158,6 +159,7 @@ pub struct Hotkeys {
pub screenshot: Code,
pub save_state: Code,
pub load_state: Code,
+ pub release_mouse: Code,
}
impl Default for Hotkeys {
@@ -170,6 +172,7 @@ impl Default for Hotkeys {
screenshot: Code::F12,
save_state: Code::F5,
load_state: Code::F7,
+ release_mouse: Code::Escape,
}
}
}
@@ -190,6 +193,7 @@ pub enum Hotkey {
Screenshot,
SaveState,
LoadState,
+ ReleaseMouse,
}
impl Hotkeys {
@@ -208,6 +212,8 @@ impl Hotkeys {
Some(Hotkey::SaveState)
} else if key == self.load_state {
Some(Hotkey::LoadState)
+ } else if key == self.release_mouse {
+ Some(Hotkey::ReleaseMouse)
} else {
None
}
@@ -255,6 +261,7 @@ pub enum KeyTarget {
HotkeyScreenshot,
HotkeySaveState,
HotkeyLoadState,
+ HotkeyReleaseMouse,
}
pub const GC_KEY_TARGETS: &[(KeyTarget, &str)] = &[
@@ -303,6 +310,7 @@ pub const HOTKEY_TARGETS: &[(KeyTarget, &str)] = &[
(KeyTarget::HotkeyScreenshot, "Screenshot"),
(KeyTarget::HotkeySaveState, "Save State"),
(KeyTarget::HotkeyLoadState, "Load State"),
+ (KeyTarget::HotkeyReleaseMouse, "Release Mouse"),
];
pub fn field(config: &mut KeyboardConfig, target: KeyTarget) -> &mut Option<String> {
@@ -346,6 +354,7 @@ pub fn field(config: &mut KeyboardConfig, target: KeyTarget) -> &mut Option<Stri
KeyTarget::HotkeyScreenshot => &mut config.hotkeys.screenshot,
KeyTarget::HotkeySaveState => &mut config.hotkeys.save_state,
KeyTarget::HotkeyLoadState => &mut config.hotkeys.load_state,
+ KeyTarget::HotkeyReleaseMouse => &mut config.hotkeys.release_mouse,
}
}
@@ -391,6 +400,7 @@ impl Keymap {
KeyTarget::HotkeyScreenshot => self.hotkeys.screenshot,
KeyTarget::HotkeySaveState => self.hotkeys.save_state,
KeyTarget::HotkeyLoadState => self.hotkeys.load_state,
+ KeyTarget::HotkeyReleaseMouse => self.hotkeys.release_mouse,
}
}
}
@@ -440,6 +450,7 @@ impl KeyboardConfig {
self::set(&mut keymap.hotkeys.screenshot, &self.hotkeys.screenshot);
self::set(&mut keymap.hotkeys.save_state, &self.hotkeys.save_state);
self::set(&mut keymap.hotkeys.load_state, &self.hotkeys.load_state);
+ self::set(&mut keymap.hotkeys.release_mouse, &self.hotkeys.release_mouse);
keymap
}
diff --git a/crates/app/src/player.rs b/crates/app/src/player.rs
index c1fa697..e9ca4a1 100644
--- a/crates/app/src/player.rs
+++ b/crates/app/src/player.rs
@@ -1,8 +1,10 @@
mod audio;
mod emu_thread;
mod input;
+mod mouse_capture;
mod state;
mod widget;
+pub use mouse_capture::MouseCapture;
pub use state::{PlayerState, PlayerStatus, physical_to_code};
pub use widget::shader_widget;
diff --git a/crates/app/src/player/mouse_capture.rs b/crates/app/src/player/mouse_capture.rs
new file mode 100644
index 0000000..12b3a0b
--- /dev/null
+++ b/crates/app/src/player/mouse_capture.rs
@@ -0,0 +1,273 @@
+use iced::window::Window;
+
+#[derive(Default)]
+pub struct MouseCapture {
+ active: bool,
+ #[cfg(target_os = "linux")]
+ wayland: Option<wayland::Capture>,
+}
+
+impl MouseCapture {
+ pub const RECLIP_ON_MOVE: bool = cfg!(target_os = "windows");
+
+ pub fn set(&mut self, window: &dyn Window, capture: bool) -> anyhow::Result<()> {
+ if self.active == capture && !(capture && Self::RECLIP_ON_MOVE) {
+ return Ok(());
+ }
+
+ self::confine(self, window, capture)?;
+ self.active = capture;
+
+ Ok(())
+ }
+
+ pub fn poll(&mut self) {
+ #[cfg(target_os = "linux")]
+ if let Some(capture) = &mut self.wayland {
+ capture.poll();
+ }
+ }
+}
+
+#[cfg(target_os = "linux")]
+fn confine(capture_state: &mut MouseCapture, window: &dyn Window, capture: bool) -> anyhow::Result<()> {
+ use iced::window::raw_window_handle::RawWindowHandle;
+
+ match window.window_handle()?.as_raw() {
+ RawWindowHandle::Wayland(surface) => {
+ if capture_state.wayland.is_none() && capture {
+ capture_state.wayland = Some(wayland::Capture::new(window, surface)?);
+ }
+
+ if let Some(wayland) = &mut capture_state.wayland {
+ wayland.set(capture)?;
+ }
+
+ Ok(())
+ }
+ RawWindowHandle::Xlib(handle) => self::confine_x11(window, handle.window, capture),
+ _ => anyhow::bail!("mouse confinement is unsupported on this window system"),
+ }
+}
+
+#[cfg(target_os = "linux")]
+fn confine_x11(window: &dyn Window, target: u64, capture: bool) -> anyhow::Result<()> {
+ use iced::window::raw_window_handle::RawDisplayHandle;
+ use std::sync::OnceLock;
+ use x11_dl::xlib;
+
+ static XLIB: OnceLock<Result<xlib::Xlib, String>> = OnceLock::new();
+
+ let RawDisplayHandle::Xlib(display) = window.display_handle()?.as_raw() else {
+ anyhow::bail!("missing X11 display");
+ };
+
+ let display = display.display.ok_or_else(|| anyhow::anyhow!("missing X11 display"))?;
+ let xlib = XLIB
+ .get_or_init(|| xlib::Xlib::open().map_err(|error| error.to_string()))
+ .as_ref()
+ .map_err(|error| anyhow::anyhow!("{error}"))?;
+
+ // SAFETY: iced lends live Xlib handles on the window's event thread.
+ unsafe {
+ (xlib.XUngrabPointer)(display.as_ptr().cast(), xlib::CurrentTime);
+
+ if capture {
+ let result = (xlib.XGrabPointer)(
+ display.as_ptr().cast(),
+ target,
+ xlib::True,
+ (xlib::ButtonPressMask
+ | xlib::ButtonReleaseMask
+ | xlib::PointerMotionMask
+ | xlib::EnterWindowMask
+ | xlib::LeaveWindowMask) as u32,
+ xlib::GrabModeAsync,
+ xlib::GrabModeAsync,
+ target,
+ 0,
+ xlib::CurrentTime,
+ );
+ anyhow::ensure!(result == xlib::GrabSuccess, "X11 mouse capture failed: {result}");
+ }
+
+ (xlib.XFlush)(display.as_ptr().cast());
+ }
+
+ Ok(())
+}
+
+#[cfg(target_os = "windows")]
+fn confine(_capture_state: &mut MouseCapture, window: &dyn Window, capture: bool) -> anyhow::Result<()> {
+ use iced::window::raw_window_handle::RawWindowHandle;
+ use windows_sys::Win32::Foundation::{POINT, RECT};
+ use windows_sys::Win32::Graphics::Gdi::ClientToScreen;
+ use windows_sys::Win32::UI::WindowsAndMessaging::{ClipCursor, GetClientRect};
+
+ let RawWindowHandle::Win32(handle) = window.window_handle()?.as_raw() else {
+ anyhow::bail!("mouse confinement is unsupported on this window system");
+ };
+
+ // SAFETY: iced lends a live HWND; all output pointers are valid stack storage.
+ unsafe {
+ let mut rect = RECT::default();
+
+ if capture {
+ let hwnd = handle.hwnd.get() as _;
+ anyhow::ensure!(GetClientRect(hwnd, &mut rect) != 0, "GetClientRect failed");
+
+ let mut origin = POINT::default();
+ anyhow::ensure!(ClientToScreen(hwnd, &mut origin) != 0, "ClientToScreen failed");
+
+ rect.left += origin.x;
+ rect.right += origin.x;
+ rect.top += origin.y;
+ rect.bottom += origin.y;
+ }
+
+ anyhow::ensure!(
+ ClipCursor(if capture { &rect } else { std::ptr::null() }) != 0,
+ "ClipCursor failed"
+ );
+ }
+
+ Ok(())
+}
+
+#[cfg(not(any(target_os = "linux", target_os = "windows")))]
+fn confine(_capture_state: &mut MouseCapture, _window: &dyn Window, _capture: bool) -> anyhow::Result<()> {
+ anyhow::bail!("mouse confinement is unsupported on this window system")
+}
+
+#[cfg(target_os = "windows")]
+impl Drop for MouseCapture {
+ fn drop(&mut self) {
+ if self.active {
+ // SAFETY: a null rectangle releases this process's cursor clip on window close.
+ unsafe {
+ windows_sys::Win32::UI::WindowsAndMessaging::ClipCursor(std::ptr::null());
+ }
+ }
+ }
+}
+
+#[cfg(target_os = "linux")]
+mod wayland {
+ use iced::window::Window;
+ use iced::window::raw_window_handle::{RawDisplayHandle, WaylandWindowHandle};
+ use wayland_client::backend::{Backend, ObjectId};
+ use wayland_client::globals::{GlobalListContents, registry_queue_init};
+ use wayland_client::protocol::{wl_pointer, wl_registry, wl_seat, wl_surface};
+ use wayland_client::{Connection, Dispatch, EventQueue, Proxy, QueueHandle, delegate_noop};
+ use wayland_protocols::wp::pointer_constraints::zv1::client::zwp_confined_pointer_v1::ZwpConfinedPointerV1;
+ use wayland_protocols::wp::pointer_constraints::zv1::client::zwp_pointer_constraints_v1::{
+ Lifetime, ZwpPointerConstraintsV1,
+ };
+
+ pub struct Capture {
+ connection: Connection,
+ queue: EventQueue<State>,
+ constraints: ZwpPointerConstraintsV1,
+ seat: wl_seat::WlSeat,
+ surface: wl_surface::WlSurface,
+ confined: Option<(ZwpConfinedPointerV1, wl_pointer::WlPointer)>,
+ }
+
+ impl Capture {
+ pub fn new(window: &dyn Window, surface: WaylandWindowHandle) -> anyhow::Result<Self> {
+ let RawDisplayHandle::Wayland(display) = window.display_handle()?.as_raw() else {
+ anyhow::bail!("missing Wayland display");
+ };
+
+ // SAFETY: the handles belong to iced's live window/display. This backend borrows
+ // the display, and never destroys the foreign surface.
+ let backend = unsafe { Backend::from_foreign_display(display.display.as_ptr().cast()) };
+ let connection = Connection::from_backend(backend);
+
+ let surface_id =
+ unsafe { ObjectId::from_ptr(wl_surface::WlSurface::interface(), surface.surface.as_ptr().cast())? };
+ let surface = wl_surface::WlSurface::from_id(&connection, surface_id)?;
+
+ let (globals, queue) = registry_queue_init::<State>(&connection)?;
+ let qh = queue.handle();
+
+ let constraints = globals
+ .bind(&qh, 1..=1, ())
+ .map_err(|_| anyhow::anyhow!("compositor does not support pointer confinement"))?;
+
+ let seat = globals
+ .bind(&qh, 5..=5, ())
+ .map_err(|_| anyhow::anyhow!("missing Wayland seat"))?;
+
+ Ok(Self {
+ connection,
+ queue,
+ constraints,
+ seat,
+ surface,
+ confined: None,
+ })
+ }
+
+ pub fn set(&mut self, capture: bool) -> anyhow::Result<()> {
+ match capture {
+ true if self.confined.is_none() => {
+ let qh = self.queue.handle();
+ let pointer = self.seat.get_pointer(&qh, ());
+ let confined =
+ self.constraints
+ .confine_pointer(&self.surface, &pointer, None, Lifetime::Persistent, &qh, ());
+
+ self.confined = Some((confined, pointer));
+ }
+ false => self::release(&mut self.confined),
+ true => {}
+ }
+
+ self.connection.flush()?;
+
+ Ok(())
+ }
+
+ pub fn poll(&mut self) {
+ let _ = self.queue.dispatch_pending(&mut State);
+ }
+ }
+
+ impl Drop for Capture {
+ fn drop(&mut self) {
+ self::release(&mut self.confined);
+
+ self.constraints.destroy();
+ self.seat.release();
+
+ let _ = self.connection.flush();
+ }
+ }
+
+ fn release(confined: &mut Option<(ZwpConfinedPointerV1, wl_pointer::WlPointer)>) {
+ if let Some((confined, pointer)) = confined.take() {
+ confined.destroy();
+ pointer.release();
+ }
+ }
+
+ struct State;
+
+ impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for State {
+ fn event(
+ _: &mut Self,
+ _: &wl_registry::WlRegistry,
+ _: wl_registry::Event,
+ _: &GlobalListContents,
+ _: &Connection,
+ _: &QueueHandle<Self>,
+ ) {
+ }
+ }
+
+ delegate_noop!(State: ignore wl_seat::WlSeat);
+ delegate_noop!(State: ignore wl_pointer::WlPointer);
+ delegate_noop!(State: ignore ZwpPointerConstraintsV1);
+ delegate_noop!(State: ignore ZwpConfinedPointerV1);
+}
diff --git a/crates/app/src/player/state.rs b/crates/app/src/player/state.rs
index b97bc3b..e16a9f5 100644
--- a/crates/app/src/player/state.rs
+++ b/crates/app/src/player/state.rs
@@ -205,6 +205,11 @@ impl PlayerState {
}
}
+ pub fn clear_input(&self) {
+ let mut input = self.input.lock().unwrap();
+ *input = input.cleared();
+ }
+
pub fn hotkey(&self, key: Code) -> Option<Hotkey> {
self.keymap.lock().unwrap().hotkeys.lookup(key)
}
diff --git a/crates/app/src/player/widget.rs b/crates/app/src/player/widget.rs
index 32979f8..c967056 100644
--- a/crates/app/src/player/widget.rs
+++ b/crates/app/src/player/widget.rs
@@ -6,12 +6,17 @@ use iced::{Rectangle, mouse, window};
use crate::app::Message;
use crate::keybinds::Hotkey;
+use crate::player::MouseCapture;
use crate::player::state::{self, PlayerState};
-pub fn shader_widget(state: Arc<PlayerState>, window: window::Id) -> Shader<Message, PlayerProgram> {
- Shader::new(PlayerProgram { state, window })
- .width(iced::Length::Fill)
- .height(iced::Length::Fill)
+pub fn shader_widget(state: Arc<PlayerState>, window: window::Id, captured: bool) -> Shader<Message, PlayerProgram> {
+ Shader::new(PlayerProgram {
+ state,
+ window,
+ captured,
+ })
+ .width(iced::Length::Fill)
+ .height(iced::Length::Fill)
}
fn hotkey_message(hotkey: Hotkey, window: window::Id) -> Message {
@@ -23,12 +28,14 @@ fn hotkey_message(hotkey: Hotkey, window: window::Id) -> Message {
Hotkey::Screenshot => Message::PlayerScreenshot(window),
Hotkey::SaveState => Message::PlayerSaveState(window),
Hotkey::LoadState => Message::PlayerLoadState(window),
+ Hotkey::ReleaseMouse => Message::PlayerCaptureMouse(window, false),
}
}
pub struct PlayerProgram {
state: Arc<PlayerState>,
window: window::Id,
+ captured: bool,
}
impl Program<Message> for PlayerProgram {
@@ -53,7 +60,7 @@ impl Program<Message> for PlayerProgram {
iced::keyboard::Event::KeyPressed {
physical_key, repeat, ..
} => {
- let hotkey = match state::physical_to_code(physical_key) {
+ let message = match state::physical_to_code(physical_key) {
Some(_) if *repeat => None,
Some(code) => match self.state.hotkey(code) {
Some(hotkey) => Some(self::hotkey_message(hotkey, self.window)),
@@ -65,7 +72,7 @@ impl Program<Message> for PlayerProgram {
None => None,
};
- match hotkey {
+ match message {
Some(message) => Some(shader::Action::publish(message)),
None => Some(shader::Action::request_redraw()),
}
@@ -96,6 +103,9 @@ impl Program<Message> for PlayerProgram {
iced::mouse::Event::ButtonPressed(button) => {
if cursor.is_over(bounds) {
self.state.handle_mouse_button(*button, true);
+ if !self.captured {
+ return Some(shader::Action::publish(Message::PlayerCaptureMouse(self.window, true)));
+ }
}
Some(shader::Action::request_redraw())
}
@@ -105,12 +115,23 @@ impl Program<Message> for PlayerProgram {
}
_ => None,
},
+ iced::Event::Window(window::Event::Moved(_) | window::Event::Resized(_))
+ if self.captured && MouseCapture::RECLIP_ON_MOVE =>
+ {
+ Some(shader::Action::publish(Message::PlayerCaptureMouse(self.window, true)))
+ }
+ iced::Event::Window(window::Event::Focused) => {
+ Some(shader::Action::publish(Message::PlayerFocused(self.window, true)))
+ }
+ iced::Event::Window(window::Event::Unfocused) => {
+ Some(shader::Action::publish(Message::PlayerFocused(self.window, false)))
+ }
_ => None,
}
}
fn mouse_interaction(&self, _state: &Self::State, bounds: Rectangle, cursor: mouse::Cursor) -> mouse::Interaction {
- if cursor.is_over(bounds) {
+ if self.captured && cursor.is_over(bounds) {
mouse::Interaction::Hidden
} else {
mouse::Interaction::None