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

Gamepad: Implement GamepadHapticActuator #32046

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions components/constellation/tracing.rs
Expand Up @@ -240,6 +240,8 @@ mod from_script {
Self::OnDevtoolsStarted(..) => target_variant!("OnDevtoolsStarted"),
Self::ReadyToPresent(..) => target_variant!("ReadyToPresent"),
Self::EventDelivered(..) => target_variant!("EventDelivered"),
Self::PlayGamepadHapticEffect(..) => target_variant!("PlayGamepadHapticEffect"),
Self::StopGamepadHapticEffect(..) => target_variant!("StopGamepadHapticEffect"),
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions components/script/dom/gamepad.rs
Expand Up @@ -6,7 +6,7 @@ use std::cell::Cell;

use dom_struct::dom_struct;
use js::typedarray::{Float64, Float64Array};
use script_traits::GamepadUpdateType;
use script_traits::{GamepadSupportedHapticEffects, GamepadUpdateType};

use super::bindings::buffer_source::HeapBufferSource;
use crate::dom::bindings::codegen::Bindings::GamepadBinding::{GamepadHand, GamepadMethods};
Expand All @@ -20,6 +20,7 @@ use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::gamepadbuttonlist::GamepadButtonList;
use crate::dom::gamepadevent::{GamepadEvent, GamepadEventType};
use crate::dom::gamepadhapticactuator::GamepadHapticActuator;
use crate::dom::gamepadpose::GamepadPose;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;
Expand Down Expand Up @@ -49,6 +50,7 @@ pub struct Gamepad {
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
exposed: Cell<bool>,
vibration_actuator: Dom<GamepadHapticActuator>,
}

impl Gamepad {
Expand All @@ -65,6 +67,7 @@ impl Gamepad {
hand: GamepadHand,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
vibration_actuator: &GamepadHapticActuator,
) -> Gamepad {
Self {
reflector_: Reflector::new(),
Expand All @@ -81,6 +84,7 @@ impl Gamepad {
axis_bounds,
button_bounds,
exposed: Cell::new(false),
vibration_actuator: Dom::from_ref(vibration_actuator),
}
}

Expand All @@ -90,8 +94,16 @@ impl Gamepad {
id: String,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
supported_haptic_effects: GamepadSupportedHapticEffects,
) -> DomRoot<Gamepad> {
Self::new_with_proto(global, gamepad_id, id, axis_bounds, button_bounds)
Self::new_with_proto(
global,
gamepad_id,
id,
axis_bounds,
button_bounds,
supported_haptic_effects,
)
}

/// When we construct a new gamepad, we initialize the number of buttons and
Expand All @@ -105,8 +117,11 @@ impl Gamepad {
id: String,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
supported_haptic_effects: GamepadSupportedHapticEffects,
) -> DomRoot<Gamepad> {
let button_list = GamepadButtonList::init_buttons(global);
let vibration_actuator =
GamepadHapticActuator::new(global, gamepad_id, supported_haptic_effects);
let gamepad = reflect_dom_object_with_proto(
Box::new(Gamepad::new_inherited(
gamepad_id,
Expand All @@ -120,6 +135,7 @@ impl Gamepad {
GamepadHand::_empty,
axis_bounds,
button_bounds,
&vibration_actuator,
)),
global,
None,
Expand Down Expand Up @@ -165,6 +181,11 @@ impl GamepadMethods for Gamepad {
DomRoot::from_ref(&*self.buttons)
}

// https://w3c.github.io/gamepad/#dom-gamepad-vibrationactuator
fn VibrationActuator(&self) -> DomRoot<GamepadHapticActuator> {
DomRoot::from_ref(&*self.vibration_actuator)
}

// https://w3c.github.io/gamepad/extensions.html#gamepadhand-enum
fn Hand(&self) -> GamepadHand {
self.hand
Expand Down Expand Up @@ -286,6 +307,10 @@ impl Gamepad {
pub fn set_exposed(&self, exposed: bool) {
self.exposed.set(exposed);
}

pub fn vibration_actuator(&self) -> &GamepadHapticActuator {
&*self.vibration_actuator
}
}

/// <https://www.w3.org/TR/gamepad/#dfn-gamepad-user-gesture>
Expand Down