aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/gamepad.rs
diff options
context:
space:
mode:
authorDaniel Adams <70986246+msub2@users.noreply.github.com>2024-03-12 08:32:30 -0400
committerGitHub <noreply@github.com>2024-03-12 12:32:30 +0000
commit48fa77df6710a89e156c8cebb7dec10c8cda4ae6 (patch)
tree20cd807a4ba546a9c9060096615364c8588b7ed1 /components/script/dom/gamepad.rs
parent31a50feb4a61707d661c6b72fe6479666a5c9832 (diff)
downloadservo-48fa77df6710a89e156c8cebb7dec10c8cda4ae6.tar.gz
servo-48fa77df6710a89e156c8cebb7dec10c8cda4ae6.zip
Gamepad: Align closer to spec and implement missing slots (#31385)
* Implement missing gamepad slots, align to spec more - Fixes TODO's from initial gamepad implementation - Adds some missing spec steps * Only handle gamepad events when pref is enabled * Return empty list in getGamepads if document not active * ./mach fmt * Update getGamepads to return an array instead of GamepadList * Add spec link for [[exposed]] slot * Remove failing test expectations for not-fully-active * A few fixes - Change should_notify to has_gesture - Add spec links and TODO to navigator - Remove unneeded clone from GamepadList::list - Move gamepadconnected event firing into has_gesture block * Use queue_with_canceller for tasks and add expects * Explicitly check for gamepad user gesture * Move user gesture check into separate function * Change contains_user_gesture to be a gamepad function * mach fmt * Change axis/button threshold constants to be private to module
Diffstat (limited to 'components/script/dom/gamepad.rs')
-rw-r--r--components/script/dom/gamepad.rs36
1 files changed, 33 insertions, 3 deletions
diff --git a/components/script/dom/gamepad.rs b/components/script/dom/gamepad.rs
index 2cf327cd06e..68e7a1de2e5 100644
--- a/components/script/dom/gamepad.rs
+++ b/components/script/dom/gamepad.rs
@@ -6,6 +6,7 @@ use std::cell::Cell;
use dom_struct::dom_struct;
use js::typedarray::{Float64, Float64Array};
+use script_traits::GamepadUpdateType;
use super::bindings::buffer_source::HeapBufferSource;
use crate::dom::bindings::codegen::Bindings::GamepadBinding::{GamepadHand, GamepadMethods};
@@ -23,6 +24,9 @@ use crate::dom::gamepadpose::GamepadPose;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
+// This value is for determining when to consider a gamepad as having a user gesture
+// from an axis tilt. This matches the threshold in Chromium.
+const AXIS_TILT_THRESHOLD: f64 = 0.5;
// This value is for determining when to consider a non-digital button "pressed".
// Like Gecko and Chromium it derives from the XInput trigger threshold.
const BUTTON_PRESS_THRESHOLD: f64 = 30.0 / 255.0;
@@ -44,6 +48,7 @@ pub struct Gamepad {
hand: GamepadHand,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
+ exposed: Cell<bool>,
}
impl Gamepad {
@@ -74,6 +79,7 @@ impl Gamepad {
hand: hand,
axis_bounds: axis_bounds,
button_bounds: button_bounds,
+ exposed: Cell::new(false),
}
}
@@ -105,7 +111,7 @@ impl Gamepad {
gamepad_id,
id,
0,
- false,
+ true,
0.,
String::from("standard"),
&button_list,
@@ -175,7 +181,7 @@ impl Gamepad {
self.gamepad_id
}
- pub fn update_connected(&self, connected: bool) {
+ pub fn update_connected(&self, connected: bool, has_gesture: bool) {
if self.connected.get() == connected {
return;
}
@@ -187,7 +193,9 @@ impl Gamepad {
GamepadEventType::Disconnected
};
- self.notify_event(event_type);
+ if has_gesture {
+ self.notify_event(event_type);
+ }
}
pub fn update_index(&self, index: i32) {
@@ -263,4 +271,26 @@ impl Gamepad {
warn!("Button bounds difference is either 0 or non-finite!");
}
}
+
+ /// <https://www.w3.org/TR/gamepad/#dfn-exposed>
+ pub fn exposed(&self) -> bool {
+ self.exposed.get()
+ }
+
+ /// <https://www.w3.org/TR/gamepad/#dfn-exposed>
+ pub fn set_exposed(&self, exposed: bool) {
+ self.exposed.set(exposed);
+ }
+}
+
+/// <https://www.w3.org/TR/gamepad/#dfn-gamepad-user-gesture>
+pub fn contains_user_gesture(update_type: GamepadUpdateType) -> bool {
+ match update_type {
+ GamepadUpdateType::Axis(_, value) => {
+ return value.abs() > AXIS_TILT_THRESHOLD;
+ },
+ GamepadUpdateType::Button(_, value) => {
+ return value > BUTTON_PRESS_THRESHOLD;
+ },
+ };
}