Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay registering the Gamepad Hook #340

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/livesplit-hotkey/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ impl Hook {
});

Ok(Hook {
sender: sender,
ping: ping,
sender,
ping,
_registration: registration,
join_handle: Some(join_handle),
})
Expand Down
83 changes: 46 additions & 37 deletions crates/livesplit-hotkey/src/wasm_web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{window, Gamepad, GamepadButton, KeyboardEvent};

use std::collections::hash_map::{Entry, HashMap};
use std::sync::{Arc, Mutex};
use std::{
cell::Cell,
collections::hash_map::{Entry, HashMap},
sync::{Arc, Mutex},
};

#[derive(Debug, snafu::Snafu)]
pub enum Error {
Expand All @@ -20,8 +23,8 @@ pub type Result<T> = std::result::Result<T, Error>;
pub struct Hook {
hotkeys: Arc<Mutex<HashMap<KeyCode, Box<dyn FnMut() + Send + 'static>>>>,
keyboard_callback: Closure<dyn FnMut(KeyboardEvent)>,
_gamepad_callback: Closure<dyn FnMut()>,
interval_id: i32,
gamepad_callback: Closure<dyn FnMut()>,
interval_id: Cell<Option<i32>>,
}

impl Drop for Hook {
Expand All @@ -31,11 +34,37 @@ impl Drop for Hook {
"keypress",
self.keyboard_callback.as_ref().unchecked_ref(),
);
window.clear_interval_with_handle(self.interval_id);
if let Some(interval_id) = self.interval_id.get() {
window.clear_interval_with_handle(interval_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely an edge case, but should we also be calling this in unregister if there's no more gamepad buttons registered anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that case matters much, as I think it's a super rare situation that someone was using gamepad, then stops using gamepad. And even then, the cost for the gamepad hook is still very minimal, and it stops being a cost the next time they reload. So overall, I'd say yes, we could possibly remove the hook when unregistering all gamepad buttons, but it has a much lesser impact.

}
}
}
}

const TOTAL_BUTTONS: usize = 20;
static GAMEPAD_BUTTONS: [KeyCode; TOTAL_BUTTONS] = [
KeyCode::Gamepad0,
KeyCode::Gamepad1,
KeyCode::Gamepad2,
KeyCode::Gamepad3,
KeyCode::Gamepad4,
KeyCode::Gamepad5,
KeyCode::Gamepad6,
KeyCode::Gamepad7,
KeyCode::Gamepad8,
KeyCode::Gamepad9,
KeyCode::Gamepad10,
KeyCode::Gamepad11,
KeyCode::Gamepad12,
KeyCode::Gamepad13,
KeyCode::Gamepad14,
KeyCode::Gamepad15,
KeyCode::Gamepad16,
KeyCode::Gamepad17,
KeyCode::Gamepad18,
KeyCode::Gamepad19,
];

impl Hook {
pub fn new() -> Result<Self> {
let hotkeys = Arc::new(Mutex::new(HashMap::<
Expand Down Expand Up @@ -63,29 +92,6 @@ impl Hook {

let hotkey_map = hotkeys.clone();

const TOTAL_BUTTONS: usize = 20;
static GAMEPAD_BUTTONS: [KeyCode; TOTAL_BUTTONS] = [
KeyCode::Gamepad0,
KeyCode::Gamepad1,
KeyCode::Gamepad2,
KeyCode::Gamepad3,
KeyCode::Gamepad4,
KeyCode::Gamepad5,
KeyCode::Gamepad6,
KeyCode::Gamepad7,
KeyCode::Gamepad8,
KeyCode::Gamepad9,
KeyCode::Gamepad10,
KeyCode::Gamepad11,
KeyCode::Gamepad12,
KeyCode::Gamepad13,
KeyCode::Gamepad14,
KeyCode::Gamepad15,
KeyCode::Gamepad16,
KeyCode::Gamepad17,
KeyCode::Gamepad18,
KeyCode::Gamepad19,
];
let mut states = Vec::new();
let navigator = window.navigator();

Expand Down Expand Up @@ -120,18 +126,11 @@ impl Hook {
}
}) as Box<dyn FnMut()>);

let interval_id = window
.set_interval_with_callback_and_timeout_and_arguments_0(
gamepad_callback.as_ref().unchecked_ref(),
1000 / 60,
)
.map_err(|_| Error::FailedToCreateHook)?;

Ok(Hook {
hotkeys,
keyboard_callback,
_gamepad_callback: gamepad_callback,
interval_id,
gamepad_callback,
interval_id: Cell::new(None),
})
}

Expand All @@ -140,6 +139,16 @@ impl Hook {
F: FnMut() + Send + 'static,
{
if let Entry::Vacant(vacant) = self.hotkeys.lock().unwrap().entry(hotkey) {
if GAMEPAD_BUTTONS.contains(&hotkey) && self.interval_id.get().is_none() {
let interval_id = window()
.ok_or(Error::FailedToCreateHook)?
.set_interval_with_callback_and_timeout_and_arguments_0(
self.gamepad_callback.as_ref().unchecked_ref(),
1000 / 60,
)
.map_err(|_| Error::FailedToCreateHook)?;
self.interval_id.set(Some(interval_id));
}
vacant.insert(Box::new(callback));
Ok(())
} else {
Expand Down