diff options
Diffstat (limited to 'components/script/dom/gamepad.rs')
-rw-r--r-- | components/script/dom/gamepad.rs | 158 |
1 files changed, 51 insertions, 107 deletions
diff --git a/components/script/dom/gamepad.rs b/components/script/dom/gamepad.rs index f9f28fc8c51..b81d6a7b228 100644 --- a/components/script/dom/gamepad.rs +++ b/components/script/dom/gamepad.rs @@ -1,27 +1,24 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -use core::nonzero::NonZero; -use dom::bindings::codegen::Bindings::GamepadBinding; -use dom::bindings::codegen::Bindings::GamepadBinding::GamepadMethods; -use dom::bindings::inheritance::Castable; -use dom::bindings::js::{JS, Root}; -use dom::bindings::num::Finite; -use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::bindings::str::DOMString; -use dom::event::Event; -use dom::eventtarget::EventTarget; -use dom::gamepadbuttonlist::GamepadButtonList; -use dom::gamepadevent::{GamepadEvent, GamepadEventType}; -use dom::globalscope::GlobalScope; -use dom::vrpose::VRPose; + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::codegen::Bindings::GamepadBinding::GamepadHand; +use crate::dom::bindings::codegen::Bindings::GamepadBinding::GamepadMethods; +use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::num::Finite; +use crate::dom::bindings::reflector::{DomObject, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::DOMString; +use crate::dom::event::Event; +use crate::dom::eventtarget::EventTarget; +use crate::dom::gamepadbuttonlist::GamepadButtonList; +use crate::dom::gamepadevent::{GamepadEvent, GamepadEventType}; +use crate::dom::gamepadpose::GamepadPose; +use crate::script_runtime::JSContext; use dom_struct::dom_struct; -use js::jsapi::{Heap, JSContext, JSObject}; -use js::typedarray::{Float64Array, CreateWith}; +use js::jsapi::{Heap, JSObject}; use std::cell::Cell; -use std::ptr; -use webvr_traits::{WebVRGamepadData, WebVRGamepadHand, WebVRGamepadState}; +use std::ptr::NonNull; #[dom_struct] pub struct Gamepad { @@ -32,26 +29,28 @@ pub struct Gamepad { connected: Cell<bool>, timestamp: Cell<f64>, mapping_type: String, + #[ignore_malloc_size_of = "mozjs"] axes: Heap<*mut JSObject>, - buttons: JS<GamepadButtonList>, - pose: Option<JS<VRPose>>, - #[ignore_heap_size_of = "Defined in rust-webvr"] - hand: WebVRGamepadHand, - display_id: u32 + buttons: Dom<GamepadButtonList>, + pose: Option<Dom<GamepadPose>>, + #[ignore_malloc_size_of = "Defined in rust-webvr"] + hand: GamepadHand, } +// TODO: support gamepad discovery +#[allow(dead_code)] impl Gamepad { - fn new_inherited(gamepad_id: u32, - id: String, - index: i32, - connected: bool, - timestamp: f64, - mapping_type: String, - axes: *mut JSObject, - buttons: &GamepadButtonList, - pose: Option<&VRPose>, - hand: WebVRGamepadHand, - display_id: u32) -> Gamepad { + fn new_inherited( + gamepad_id: u32, + id: String, + index: i32, + connected: bool, + timestamp: f64, + mapping_type: String, + buttons: &GamepadButtonList, + pose: Option<&GamepadPose>, + hand: GamepadHand, + ) -> Gamepad { Self { reflector_: Reflector::new(), gamepad_id: gamepad_id, @@ -60,44 +59,12 @@ impl Gamepad { connected: Cell::new(connected), timestamp: Cell::new(timestamp), mapping_type: mapping_type, - axes: Heap::new(axes), - buttons: JS::from_ref(buttons), - pose: pose.map(JS::from_ref), + axes: Heap::default(), + buttons: Dom::from_ref(buttons), + pose: pose.map(Dom::from_ref), hand: hand, - display_id: display_id } } - - #[allow(unsafe_code)] - pub fn new_from_vr(global: &GlobalScope, - index: i32, - data: &WebVRGamepadData, - state: &WebVRGamepadState) -> Root<Gamepad> { - let buttons = GamepadButtonList::new_from_vr(&global, &state.buttons); - let pose = VRPose::new(&global, &state.pose); - let cx = global.get_cx(); - rooted!(in (cx) let mut axes = ptr::null_mut()); - unsafe { - let _ = Float64Array::create(cx, - CreateWith::Slice(&state.axes), - axes.handle_mut()); - } - - reflect_dom_object(box Gamepad::new_inherited(state.gamepad_id, - data.name.clone(), - index, - state.connected, - state.timestamp, - "".into(), - axes.get(), - &buttons, - Some(&pose), - data.hand.clone(), - data.display_id), - global, - GamepadBinding::Wrap) - - } } impl GamepadMethods for Gamepad { @@ -128,54 +95,29 @@ impl GamepadMethods for Gamepad { #[allow(unsafe_code)] // https://w3c.github.io/gamepad/#dom-gamepad-axes - unsafe fn Axes(&self, _cx: *mut JSContext) -> NonZero<*mut JSObject> { - NonZero::new(self.axes.get()) + fn Axes(&self, _cx: JSContext) -> NonNull<JSObject> { + unsafe { NonNull::new_unchecked(self.axes.get()) } } // https://w3c.github.io/gamepad/#dom-gamepad-buttons - fn Buttons(&self) -> Root<GamepadButtonList> { - Root::from_ref(&*self.buttons) + fn Buttons(&self) -> DomRoot<GamepadButtonList> { + DomRoot::from_ref(&*self.buttons) } // https://w3c.github.io/gamepad/extensions.html#gamepadhand-enum - fn Hand(&self) -> DOMString { - let value = match self.hand { - WebVRGamepadHand::Unknown => "", - WebVRGamepadHand::Left => "left", - WebVRGamepadHand::Right => "right" - }; - value.into() + fn Hand(&self) -> GamepadHand { + self.hand } // https://w3c.github.io/gamepad/extensions.html#dom-gamepad-pose - fn GetPose(&self) -> Option<Root<VRPose>> { - self.pose.as_ref().map(|p| Root::from_ref(&**p)) - } - - // https://w3c.github.io/webvr/spec/1.1/#gamepad-getvrdisplays-attribute - fn DisplayId(&self) -> u32 { - self.display_id + fn GetPose(&self) -> Option<DomRoot<GamepadPose>> { + self.pose.as_ref().map(|p| DomRoot::from_ref(&**p)) } } +// TODO: support gamepad discovery +#[allow(dead_code)] impl Gamepad { - #[allow(unsafe_code)] - pub fn update_from_vr(&self, state: &WebVRGamepadState) { - self.timestamp.set(state.timestamp); - unsafe { - let cx = self.global().get_cx(); - typedarray!(in(cx) let axes: Float64Array = self.axes.get()); - if let Ok(mut array) = axes { - array.update(&state.axes); - } - } - self.buttons.sync_from_vr(&state.buttons); - if let Some(ref pose) = self.pose { - pose.update(&state.pose); - } - self.update_connected(state.connected); - } - pub fn gamepad_id(&self) -> u32 { self.gamepad_id } @@ -201,6 +143,8 @@ impl Gamepad { pub fn notify_event(&self, event_type: GamepadEventType) { let event = GamepadEvent::new_with_type(&self.global(), event_type, &self); - event.upcast::<Event>().fire(self.global().as_window().upcast::<EventTarget>()); + event + .upcast::<Event>() + .fire(self.global().as_window().upcast::<EventTarget>()); } } |