From 48fa77df6710a89e156c8cebb7dec10c8cda4ae6 Mon Sep 17 00:00:00 2001 From: Daniel Adams <70986246+msub2@users.noreply.github.com> Date: Tue, 12 Mar 2024 08:32:30 -0400 Subject: 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 --- components/script/dom/navigator.rs | 42 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'components/script/dom/navigator.rs') diff --git a/components/script/dom/navigator.rs b/components/script/dom/navigator.rs index 612d2e68ff4..d16b4f263ae 100644 --- a/components/script/dom/navigator.rs +++ b/components/script/dom/navigator.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use std::cell::Cell; use std::convert::TryInto; use dom_struct::dom_struct; @@ -9,11 +10,13 @@ use js::jsval::JSVal; use lazy_static::lazy_static; use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods; +use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods; use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; use crate::dom::bindings::root::{DomRoot, MutNullableDom}; use crate::dom::bindings::str::DOMString; use crate::dom::bindings::utils::to_frozen_array; use crate::dom::bluetooth::Bluetooth; +use crate::dom::gamepad::Gamepad; use crate::dom::gamepadlist::GamepadList; use crate::dom::gpu::GPU; use crate::dom::mediadevices::MediaDevices; @@ -43,10 +46,13 @@ pub struct Navigator { service_worker: MutNullableDom, xr: MutNullableDom, mediadevices: MutNullableDom, + /// gamepads: MutNullableDom, permissions: MutNullableDom, mediasession: MutNullableDom, gpu: MutNullableDom, + /// + has_gamepad_gesture: Cell, } impl Navigator { @@ -63,6 +69,7 @@ impl Navigator { permissions: Default::default(), mediasession: Default::default(), gpu: Default::default(), + has_gamepad_gesture: Cell::new(false), } } @@ -73,6 +80,21 @@ impl Navigator { pub fn xr(&self) -> Option> { self.xr.get() } + + pub fn gamepads(&self) -> DomRoot { + let gamepads = self + .gamepads + .or_init(|| GamepadList::new(&self.global(), &[])); + gamepads + } + + pub fn has_gamepad_gesture(&self) -> bool { + self.has_gamepad_gesture.get() + } + + pub fn set_has_gamepad_gesture(&self, has_gamepad_gesture: bool) { + self.has_gamepad_gesture.set(has_gamepad_gesture); + } } impl NavigatorMethods for Navigator { @@ -169,14 +191,20 @@ impl NavigatorMethods for Navigator { true } - // https://www.w3.org/TR/gamepad/#navigator-interface-extension - fn GetGamepads(&self) -> DomRoot { - let root = self - .gamepads - .or_init(|| GamepadList::new(&self.global(), &[])); + /// + fn GetGamepads(&self) -> Vec>> { + let global = self.global(); + let window = global.as_window(); + let doc = window.Document(); + + // TODO: Handle permissions policy once implemented + if !doc.is_fully_active() || !self.has_gamepad_gesture.get() { + return Vec::new(); + } + + let root = self.gamepads.or_init(|| GamepadList::new(&global, &[])); - // TODO: Add gamepads - root + root.list() } // https://w3c.github.io/permissions/#navigator-and-workernavigator-extension fn Permissions(&self) -> DomRoot { -- cgit v1.2.3