Skip to content

Commit

Permalink
Add spec links, queue gamepad updates on task source
Browse files Browse the repository at this point in the history
  • Loading branch information
msub2 committed Feb 8, 2024
1 parent caad7ee commit 98c45b1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
42 changes: 30 additions & 12 deletions components/script/dom/document.rs
Expand Up @@ -1922,20 +1922,38 @@ impl Document {
)
.unwrap();
},
// https://www.w3.org/TR/gamepad/#receiving-inputs
script_traits::GamepadEvent::Updated(index, update_type) => {
let gamepad_list = self.window().Navigator().GetGamepads();
if let Some(gamepad) = gamepad_list.IndexedGetter(index.0 as u32) {
match update_type {
GamepadUpdateType::Axis(index, value) => {
gamepad.update_axis(index, value);
},
GamepadUpdateType::Button(index, value) => {
let pressed = value == 1.0;
let touched = value > 0.0;
gamepad.update_button(index, pressed, touched, value);
let document = Trusted::new(self);
// 2. Queue a task on the gamepad task source to update gamepad state for gamepad.
self.window().task_manager().gamepad_task_source().queue(
task!(gamepad_updated: move || {
let document = document.root();
let window = document.window();
let gamepad_list = window.Navigator().GetGamepads();
if let Some(gamepad) = gamepad_list.IndexedGetter(index.0 as u32) {
// 1. Let now be the current high resolution time.
let current_time = time::get_time();
let now = (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as f64;
// 2. Set gamepad.[[timestamp]] to now.
gamepad.update_timestamp(now);
match update_type {
GamepadUpdateType::Axis(index, value) => {
// 3. Run the steps to map and normalize axes for gamepad.
gamepad.update_axis(index, value);
},
GamepadUpdateType::Button(index, value) => {
// 4. Run the steps to map and normalize buttons for gamepad.
let pressed = value == 1.0;
let touched = value > 0.0;
gamepad.update_button(index, pressed, touched, value);
}
};
}
};
}
}),
&global
)
.unwrap();
},
};
}
Expand Down
18 changes: 13 additions & 5 deletions components/script/dom/gamepad.rs
Expand Up @@ -83,6 +83,8 @@ impl Gamepad {
gamepad_id: u32,
id: String
) -> DomRoot<Gamepad> {
// https://www.w3.org/TR/gamepad/#dfn-initializing-buttons
// https://www.w3.org/TR/gamepad/#fingerprinting-mitigation
// Initialize the number of buttons in the "standard" gamepad mapping.
// The spec says UAs *may* do this for fingerprint mitigation, and it also
// happens to simplify implementation
Expand Down Expand Up @@ -200,13 +202,19 @@ impl Gamepad {
self.index.set(index);
}

pub fn update_timestamp(&self, timestamp: f64) {
self.timestamp.set(timestamp);
}

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>());
}

// https://www.w3.org/TR/gamepad/#dfn-initializing-axes
// https://www.w3.org/TR/gamepad/#fingerprinting-mitigation
pub fn init_axes(&self) {
// Initialize the number of axes in the "standard" gamepad mapping.
// See above comment on buttons
Expand All @@ -221,12 +229,12 @@ impl Gamepad {
.expect("Failed to set axes data on gamepad.")
}

#[allow(unsafe_code)]
pub fn update_axis(&self, axis_index: usize, value: f32) {
let mut axis_vec = self.axes.get_internal().unwrap().to_vec();
axis_vec[axis_index] = value as f64;
self.axes
.set_data(GlobalScope::get_cx(), &axis_vec)
.expect("Failed to update axis info on gamepad.")
let mut axis_vec = self.axes.get_internal().unwrap();
unsafe {
axis_vec.as_mut_slice()[axis_index] = value as f64;
}
}

pub fn update_button(&self, button_index: usize, pressed: bool, touched: bool, value: f32) {
Expand Down
1 change: 0 additions & 1 deletion ports/servoshell/webview.rs
Expand Up @@ -178,7 +178,6 @@ where
}
},
EventType::Connected => {
println!("Gamepad was connected, firing GamepadEvent::Connected");
let name = String::from(name);
let event = GamepadEvent::Connected(index, name);
self.event_queue.push(EmbedderEvent::Gamepad(event));
Expand Down

0 comments on commit 98c45b1

Please sign in to comment.