Skip to main content

script/dom/webxr/
xrinputsourcearray.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::cell::DomRefCell;
8use script_bindings::reflector::{Reflector, reflect_dom_object};
9use script_bindings::root::DomSlice;
10use webxr_api::{InputId, InputSource};
11
12use crate::dom::bindings::codegen::Bindings::XRInputSourceArrayBinding::XRInputSourceArrayMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::reflector::DomGlobal;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::event::Event;
17use crate::dom::window::Window;
18use crate::dom::xrinputsource::XRInputSource;
19use crate::dom::xrinputsourceschangeevent::XRInputSourcesChangeEvent;
20use crate::dom::xrsession::XRSession;
21
22#[dom_struct]
23pub(crate) struct XRInputSourceArray {
24    reflector_: Reflector,
25    input_sources: DomRefCell<Vec<Dom<XRInputSource>>>,
26}
27
28impl XRInputSourceArray {
29    fn new_inherited() -> XRInputSourceArray {
30        XRInputSourceArray {
31            reflector_: Reflector::new(),
32            input_sources: DomRefCell::new(vec![]),
33        }
34    }
35
36    pub(crate) fn new(cx: &mut JSContext, window: &Window) -> DomRoot<XRInputSourceArray> {
37        reflect_dom_object(cx, Box::new(XRInputSourceArray::new_inherited()), window)
38    }
39
40    pub(crate) fn add_input_sources(
41        &self,
42        cx: &mut JSContext,
43        session: &XRSession,
44        inputs: &[InputSource],
45    ) {
46        let global = self.global();
47        let window = global.as_window();
48
49        rooted_vec!(let mut added);
50        for info in inputs {
51            // This is quadratic, but won't be a problem for the only case
52            // where we add multiple input sources (the initial input sources case)
53            debug_assert!(
54                !self
55                    .input_sources
56                    .borrow()
57                    .iter()
58                    .any(|i| i.id() == info.id),
59                "Should never add a duplicate input id!"
60            );
61            let input = XRInputSource::new(cx, window, session, info.clone());
62            self.input_sources
63                .safe_borrow_mut(cx.no_gc())
64                .push(Dom::from_ref(&input));
65            added.push(input.as_traced());
66        }
67
68        let event = XRInputSourcesChangeEvent::new(
69            cx,
70            window,
71            atom!("inputsourceschange"),
72            false,
73            true,
74            session,
75            added.as_ref_slice(),
76            &[],
77        );
78        event.upcast::<Event>().fire(cx, session.upcast());
79    }
80
81    pub(crate) fn remove_input_source(&self, cx: &mut JSContext, session: &XRSession, id: InputId) {
82        let global = self.global();
83        let window = global.as_window();
84
85        rooted_vec!(let mut removed);
86        if let Some(i) = self.input_sources.borrow().iter().find(|i| i.id() == id) {
87            i.gamepad().update_connected(false);
88            removed.push(Dom::from_ref(&**i));
89        } else {
90            return;
91        }
92
93        let event = XRInputSourcesChangeEvent::new(
94            cx,
95            window,
96            atom!("inputsourceschange"),
97            false,
98            true,
99            session,
100            &[],
101            removed.as_ref_slice(),
102        );
103        self.input_sources
104            .safe_borrow_mut(cx.no_gc())
105            .retain(|i| i.id() != id);
106        event.upcast::<Event>().fire(cx, session.upcast());
107    }
108
109    pub(crate) fn add_remove_input_source(
110        &self,
111        cx: &mut JSContext,
112        session: &XRSession,
113        id: InputId,
114        info: InputSource,
115    ) {
116        let global = self.global();
117        let window = global.as_window();
118
119        rooted_vec!(let mut removed);
120        if let Some(i) = self.input_sources.borrow().iter().find(|i| i.id() == id) {
121            i.gamepad().update_connected(false);
122            removed.push(Dom::from_ref(&**i));
123        } else {
124            warn!("Could not find removed input source with id {:?}", id);
125        }
126
127        self.input_sources
128            .safe_borrow_mut(cx.no_gc())
129            .retain(|i| i.id() != id);
130        let input = XRInputSource::new(cx, window, session, info);
131        self.input_sources
132            .safe_borrow_mut(cx.no_gc())
133            .push(Dom::from_ref(&input));
134
135        let event = XRInputSourcesChangeEvent::new(
136            cx,
137            window,
138            atom!("inputsourceschange"),
139            false,
140            true,
141            session,
142            &[&*input],
143            removed.as_ref_slice(),
144        );
145        event.upcast::<Event>().fire(cx, session.upcast());
146    }
147
148    pub(crate) fn find(&self, id: InputId) -> Option<DomRoot<XRInputSource>> {
149        self.input_sources
150            .borrow()
151            .iter()
152            .find(|x| x.id() == id)
153            .map(|x| DomRoot::from_ref(&**x))
154    }
155}
156
157impl XRInputSourceArrayMethods<crate::DomTypeHolder> for XRInputSourceArray {
158    /// <https://immersive-web.github.io/webxr/#dom-xrinputsourcearray-length>
159    fn Length(&self) -> u32 {
160        self.input_sources.borrow().len() as u32
161    }
162
163    /// <https://immersive-web.github.io/webxr/#xrinputsourcearray>
164    fn IndexedGetter(&self, n: u32) -> Option<DomRoot<XRInputSource>> {
165        self.input_sources
166            .borrow()
167            .get(n as usize)
168            .map(|x| DomRoot::from_ref(&**x))
169    }
170}