Skip to content
Permalink
Browse files

Handle AddInput/RemoveInput events

  • Loading branch information
Manishearth committed Jan 8, 2020
1 parent a90a420 commit f721113f8d2b6bae00b9be280d80c6a7d16861b6
@@ -44,6 +44,7 @@ iceconnectionstatechange
icegatheringstatechange
image
input
inputsourceschange
invalid
keydown
keypress
@@ -40,7 +40,7 @@ interface XRSession : EventTarget {
attribute EventHandler onend;
attribute EventHandler onselect;
attribute EventHandler onsqueeze;
// attribute EventHandler oninputsourceschange;
attribute EventHandler oninputsourceschange;
attribute EventHandler onselectstart;
attribute EventHandler onselectend;
attribute EventHandler onsqueezestart;
@@ -5,13 +5,16 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::XRInputSourceArrayBinding;
use crate::dom::bindings::codegen::Bindings::XRInputSourceArrayBinding::XRInputSourceArrayMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::event::Event;
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrinputsource::XRInputSource;
use crate::dom::xrinputsourceschangeevent::XRInputSourcesChangeEvent;
use crate::dom::xrsession::XRSession;
use dom_struct::dom_struct;
use webxr_api::InputId;
use webxr_api::{InputId, InputSource};

#[dom_struct]
pub struct XRInputSourceArray {
@@ -48,6 +51,56 @@ impl XRInputSourceArray {
});
}

pub fn add_input_source(&self, session: &XRSession, info: InputSource) {
let mut input_sources = self.input_sources.borrow_mut();
let global = self.global();
let input = XRInputSource::new(&global, &session, info);
debug_assert!(
input_sources.iter().find(|i| i.id() == info.id).is_none(),
"Should never add a duplicate input id!"
);
input_sources.push(Dom::from_ref(&input));

let added = [input];

let event = XRInputSourcesChangeEvent::new(
&global,
atom!("inputsourceschange"),
false,
true,
session,
&added,
&[],
);
// Release the refcell guard
drop(input_sources);
event.upcast::<Event>().fire(session.upcast());
}

pub fn remove_input_source(&self, session: &XRSession, id: InputId) {
let mut input_sources = self.input_sources.borrow_mut();
let global = self.global();
let removed = if let Some(i) = input_sources.iter().find(|i| i.id() == id) {
[DomRoot::from_ref(&**i)]
} else {
return;
};

let event = XRInputSourcesChangeEvent::new(
&global,
atom!("inputsourceschange"),
false,
true,
session,
&[],
&removed,
);
input_sources.retain(|i| i.id() != id);
// release the refcell guard
drop(input_sources);
event.upcast::<Event>().fire(session.upcast());
}

pub fn find(&self, id: InputId) -> Option<DomRoot<XRInputSource>> {
self.input_sources
.borrow()
@@ -66,10 +66,10 @@ impl XRInputSourcesChangeEvent {
let cx = global.get_cx();
unsafe {
rooted!(in(*cx) let mut added_val = UndefinedValue());
rooted!(in(*cx) let mut removed_val = UndefinedValue());
added.to_jsval(*cx, added_val.handle_mut());
removed.to_jsval(*cx, removed_val.handle_mut());
changeevent.added.set(added_val.get());
rooted!(in(*cx) let mut removed_val = UndefinedValue());
removed.to_jsval(*cx, removed_val.handle_mut());
changeevent.added.set(removed_val.get());
}

@@ -289,7 +289,12 @@ impl XRSession {
);
event.upcast::<Event>().fire(self.upcast());
},
_ => (), // XXXManishearth TBD
XREvent::AddInput(info) => {
self.input_sources.add_input_source(self, info);
},
XREvent::RemoveInput(id) => {
self.input_sources.remove_input_source(self, id);
},
}
}

@@ -432,6 +437,13 @@ impl XRSessionMethods for XRSession {
SetOnvisibilitychange
);

/// https://immersive-web.github.io/webxr/#eventdef-xrsession-inputsourceschange
event_handler!(
inputsourceschange,
GetOninputsourceschange,
SetOninputsourceschange
);

// https://immersive-web.github.io/webxr/#dom-xrsession-renderstate
fn RenderState(&self) -> DomRoot<XRRenderState> {
self.active_render_state.get()

0 comments on commit f721113

Please sign in to comment.
You can’t perform that action at this time.