From c89dc821ba8c5aa696024ac0fd41816ad5c3b68b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 24 Apr 2020 09:12:58 -0700 Subject: [PATCH] Add XRHand interface --- components/script/dom/bindings/trace.rs | 54 ++++++++++++++++++- components/script/dom/mod.rs | 1 + components/script/dom/webidls/XRHand.webidl | 10 ++++ .../script/dom/webidls/XRInputSource.webidl | 3 ++ components/script/dom/xrhand.rs | 33 ++++++++++++ components/script/dom/xrinputsource.rs | 17 +++++- python/tidy/servo_tidy/tidy.py | 1 + 7 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 components/script/dom/webidls/XRHand.webidl create mode 100644 components/script/dom/xrhand.rs diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 97fffe52853..7a15870e917 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -157,8 +157,8 @@ use webgpu::{ WebGPUPipelineLayout, WebGPUQueue, WebGPUShaderModule, }; use webrender_api::{DocumentId, ImageKey}; -use webxr_api::Ray; use webxr_api::SwapChainId as WebXRSwapChainId; +use webxr_api::{Finger, Hand, Ray}; unsafe_no_jsmanaged_fields!(Tm); @@ -881,6 +881,58 @@ where } } +unsafe impl JSTraceable for Hand +where + J: JSTraceable, +{ + #[inline] + unsafe fn trace(&self, trc: *mut JSTracer) { + // exhaustive match so we don't miss new fields + let Hand { + ref wrist, + ref thumb_metacarpal, + ref thumb_phalanx_proximal, + ref thumb_phalanx_distal, + ref thumb_phalanx_tip, + ref index, + ref middle, + ref ring, + ref little, + } = *self; + wrist.trace(trc); + thumb_metacarpal.trace(trc); + thumb_phalanx_proximal.trace(trc); + thumb_phalanx_distal.trace(trc); + thumb_phalanx_tip.trace(trc); + index.trace(trc); + middle.trace(trc); + ring.trace(trc); + little.trace(trc); + } +} + +unsafe impl JSTraceable for Finger +where + J: JSTraceable, +{ + #[inline] + unsafe fn trace(&self, trc: *mut JSTracer) { + // exhaustive match so we don't miss new fields + let Finger { + ref metacarpal, + ref phalanx_proximal, + ref phalanx_intermediate, + ref phalanx_distal, + ref phalanx_tip, + } = *self; + metacarpal.trace(trc); + phalanx_proximal.trace(trc); + phalanx_intermediate.trace(trc); + phalanx_distal.trace(trc); + phalanx_tip.trace(trc); + } +} + /// Holds a set of JSTraceables that need to be rooted struct RootedTraceableSet { set: Vec<*const dyn JSTraceable>, diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index a410220b427..5c2ab978316 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -572,6 +572,7 @@ pub mod xmlhttprequesteventtarget; pub mod xmlhttprequestupload; pub mod xmlserializer; pub mod xrframe; +pub mod xrhand; pub mod xrhittestresult; pub mod xrhittestsource; pub mod xrinputsource; diff --git a/components/script/dom/webidls/XRHand.webidl b/components/script/dom/webidls/XRHand.webidl new file mode 100644 index 00000000000..3bcbacbc329 --- /dev/null +++ b/components/script/dom/webidls/XRHand.webidl @@ -0,0 +1,10 @@ +/* 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 https://mozilla.org/MPL/2.0/. */ + +// https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md + +[SecureContext, Exposed=Window, Pref="dom.webxr.hands.enabled"] +interface XRHand { +// getter XRJoint(unsigned short jointIndex); +}; diff --git a/components/script/dom/webidls/XRInputSource.webidl b/components/script/dom/webidls/XRInputSource.webidl index 487c959859d..b663666c872 100644 --- a/components/script/dom/webidls/XRInputSource.webidl +++ b/components/script/dom/webidls/XRInputSource.webidl @@ -24,4 +24,7 @@ interface XRInputSource { [SameObject] readonly attribute XRSpace? gripSpace; // [SameObject] readonly attribute Gamepad? gamepad; /* [SameObject] */ readonly attribute /* FrozenArray */ any profiles; + + [Pref="dom.webxr.hands.enabled"] + readonly attribute XRHand? hand; }; diff --git a/components/script/dom/xrhand.rs b/components/script/dom/xrhand.rs new file mode 100644 index 00000000000..a09966e3c75 --- /dev/null +++ b/components/script/dom/xrhand.rs @@ -0,0 +1,33 @@ +/* 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 https://mozilla.org/MPL/2.0/. */ + +use crate::dom::bindings::reflector::{reflect_dom_object, Reflector}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::globalscope::GlobalScope; +use crate::dom::xrinputsource::XRInputSource; +use dom_struct::dom_struct; +use webxr_api::Hand; + +#[dom_struct] +pub struct XRHand { + reflector_: Reflector, + #[ignore_malloc_size_of = "defined in webxr"] + source: Dom, + #[ignore_malloc_size_of = "partially defind in webxr"] + support: Hand<()>, +} + +impl XRHand { + fn new_inherited(source: &XRInputSource, support: Hand<()>) -> XRHand { + XRHand { + reflector_: Reflector::new(), + source: Dom::from_ref(source), + support, + } + } + + pub fn new(global: &GlobalScope, source: &XRInputSource, support: Hand<()>) -> DomRoot { + reflect_dom_object(Box::new(XRHand::new_inherited(source, support)), global) + } +} diff --git a/components/script/dom/xrinputsource.rs b/components/script/dom/xrinputsource.rs index ed8a3e6f1a5..bf9758f55a1 100644 --- a/components/script/dom/xrinputsource.rs +++ b/components/script/dom/xrinputsource.rs @@ -8,6 +8,7 @@ use crate::dom::bindings::codegen::Bindings::XRInputSourceBinding::{ use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom}; use crate::dom::globalscope::GlobalScope; +use crate::dom::xrhand::XRHand; use crate::dom::xrsession::XRSession; use crate::dom::xrspace::XRSpace; use crate::realms::enter_realm; @@ -24,10 +25,9 @@ pub struct XRInputSource { session: Dom, #[ignore_malloc_size_of = "Defined in rust-webxr"] info: InputSource, - #[ignore_malloc_size_of = "Defined in rust-webxr"] target_ray_space: MutNullableDom, - #[ignore_malloc_size_of = "Defined in rust-webxr"] grip_space: MutNullableDom, + hand: MutNullableDom, #[ignore_malloc_size_of = "mozjs"] profiles: Heap, } @@ -40,6 +40,7 @@ impl XRInputSource { info, target_ray_space: Default::default(), grip_space: Default::default(), + hand: Default::default(), profiles: Heap::default(), } } @@ -112,4 +113,16 @@ impl XRInputSourceMethods for XRInputSource { fn Profiles(&self, _cx: JSContext) -> JSVal { self.profiles.get() } + + // https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md + fn GetHand(&self) -> Option> { + if let Some(ref hand) = self.info.hand_support { + Some( + self.hand + .or_init(|| XRHand::new(&self.global(), &self, hand.clone())), + ) + } else { + None + } + } } diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index cfeef216869..168ff0a9b6c 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -102,6 +102,7 @@ WEBIDL_STANDARDS = [ b"//webaudio.github.io", b"//immersive-web.github.io/", b"//github.com/immersive-web/webxr-test-api/", + b"//github.com/immersive-web/webxr-hands-input/", b"//gpuweb.github.io", # Not a URL b"// This interface is entirely internal to Servo, and should not be" +