Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGamepad API implementation #16260
Gamepad API implementation #16260
Conversation
highfive
commented
Apr 4, 2017
|
Heads up! This PR modifies the following files:
|
highfive
commented
Apr 4, 2017
|
First review pass. |
| #[dom_struct] | ||
| pub struct Gamepad { | ||
| reflector_: Reflector, | ||
| gamepad_id: Cell<u64>, |
This comment has been minimized.
This comment has been minimized.
| pub struct Gamepad { | ||
| reflector_: Reflector, | ||
| gamepad_id: Cell<u64>, | ||
| id: DOMRefCell<String>, |
This comment has been minimized.
This comment has been minimized.
| reflector_: Reflector, | ||
| gamepad_id: Cell<u64>, | ||
| id: DOMRefCell<String>, | ||
| index: Cell<u32>, |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 6, 2017
Author
Contributor
Now it's mutable to ensure that the index is correct if more Gamepad providers are added in the future.
This comment has been minimized.
This comment has been minimized.
nox
Apr 6, 2017
Member
If more gamepad providers are added, why would the index of this particular gamepad change?
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 6, 2017
•
Author
Contributor
I'm talking about not WebVR related gamepad providers. The index is unkown until it is added to the global gamepad array here: 42f8a81#diff-4ca88ffda625c3517cee6640aac60b99R140 The index must match the final position in the array.
This comment has been minimized.
This comment has been minimized.
nox
Apr 6, 2017
Member
I guess I don't understand why this field is needed then. Could you document it?
This comment has been minimized.
This comment has been minimized.
nox
Apr 6, 2017
Member
Oh I understand now. Could you find a different design for this? I'm pretty sure we don't even need the index at all for now, it would matter only if you had hundreds of gamepads. No need to keep the index in the list in mind, just linearly search for it in the GamepadList object.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 6, 2017
•
Author
Contributor
In order to do the dynamic search I would need to add a reference to the Gamepadlist in a Gamepad object (Gamepads are unaware of the container) which seems worse than a plain i32 automatically updated by the gamepadlist. Another way would be to get the gamepadlist from window->navigator->gamepadlist. Not too fond on both ideas, but I prefer the second solution. What do you think?
| id: DOMRefCell<String>, | ||
| index: Cell<u32>, | ||
| connected: Cell<bool>, | ||
| timestamp: Cell<f64>, |
This comment has been minimized.
This comment has been minimized.
| index: Cell<u32>, | ||
| connected: Cell<bool>, | ||
| timestamp: Cell<f64>, | ||
| mapping_type: DOMRefCell<String>, |
This comment has been minimized.
This comment has been minimized.
| .map(|g| Root::from_ref(&**g)) | ||
| } | ||
|
|
||
| fn sync_gamepad(&self, data: Option<WebVRGamepadData>, state: &WebVRGamepadState) { |
This comment has been minimized.
This comment has been minimized.
nox
Apr 5, 2017
Member
Take a Option<&WebVRGamepadData> here, this avoids a cloning operation when handling WebVRGamepadEvent::Connect above.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 6, 2017
Author
Contributor
Changed to use move semantics, so the clone above is not required either
| /// Notifies the script thread of a WebVR device event | ||
| WebVREvent(PipelineId, WebVREventMsg) | ||
| /// Notifies the script thread of WebVR events. | ||
| WebVREvent(PipelineId, Vec<WebVREventMsg>) |
This comment has been minimized.
This comment has been minimized.
| /// Dispatch a WebVR event to the subscribed script threads. | ||
| WebVREvent(Vec<PipelineId>, WebVREventMsg), | ||
| /// Dispatch WebVR events to the subscribed script threads. | ||
| WebVREvent(Vec<PipelineId>, Vec<WebVREventMsg>), |
This comment has been minimized.
This comment has been minimized.
| match ev { | ||
| VREvent::Display(ev) => WebVREventMsg::DisplayEvent(ev), | ||
| VREvent::Gamepad(ev) => WebVREventMsg::GamepadEvent(ev), | ||
| } |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 6, 2017
•
Author
Contributor
I did that to have the possibility to add some internal events in the future. But it's not needed now, so I unified the event and will only change that if required in the future.
| @@ -74,6 +74,7 @@ | |||
| "//url.spec.whatwg.org", | |||
| "//xhr.spec.whatwg.org", | |||
| "//w3c.github.io", | |||
| "//www.w3.org", | |||
This comment has been minimized.
This comment has been minimized.
dd6acf8
to
42f8a81
|
What is the point of the GamepadList object if it doesn't hold onto the gamepads? What hold onto them currently?
|
|
The GamepadList object has 2 main purposes:
In the prev comment I meant that Gamepad objects don't have a "parent" equivalent reference now, So I need to add the reference or go throught window.navigator to perform the linear search for the index property. |
|
Sorry, I still don't understand why you need this index. I feel like this lacks a lot of documentation. |
|
OOOH, disregard everything I just said, only now I realise the index is actually exposed to the Web and it is a concept defined in the spec. I guess storing the index isn't a bad idea then. Will review the rest of the changes you made. |
|
Ok, I'll store it as i32 field then. I was confused when reading the spec too, it feels weird to expose the index of an external array inside the gamepad object, but it's defined that way: https://w3c.github.io/gamepad/#dom-gamepad-index |
| unsafe_no_jsmanaged_fields!(WebVRGamepadHand); | ||
|
|
||
| impl Gamepad { | ||
| pub fn new_inherited(gamepad_id: u32, |
This comment has been minimized.
This comment has been minimized.
| display_id: u32 | ||
| } | ||
|
|
||
| unsafe_no_jsmanaged_fields!(WebVRGamepadHand); |
This comment has been minimized.
This comment has been minimized.
| use dom_struct::dom_struct; | ||
| use webvr_traits::WebVRGamepadButton; | ||
|
|
||
| // https://www.w3.org/TR/GamepadButton/ |
This comment has been minimized.
This comment has been minimized.
|
|
||
| impl GamepadButtonList { | ||
| #[allow(unrooted_must_root)] | ||
| fn new_inherited(list: Vec<JS<GamepadButton>>) -> GamepadButtonList { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| } | ||
|
|
||
| fn add(&self, gamepad: &Gamepad) { | ||
| self.list.borrow_mut().push(JS::from_ref(&*gamepad)); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 7, 2017
Author
Contributor
This method was only called internally by add_if_not_exists. I have removed it to avoid confusions with the add_if_not_exists method.
|
|
||
| pub fn add_if_not_exists(&self, gamepads: &[Root<Gamepad>]) { | ||
| for gamepad in gamepads { | ||
| if self.list.borrow().iter().any(|g| g.gamepad_id() == gamepad.gamepad_id()) { |
This comment has been minimized.
This comment has been minimized.
| // https://w3c.github.io/gamepad/#dom-navigator-getgamepads | ||
| fn Item(&self, index: u32) -> Option<Root<Gamepad>> { | ||
| if (index as usize) < self.list.borrow().len() { | ||
| Some(Root::from_ref(&*(self.list.borrow()[index as usize]))) |
This comment has been minimized.
This comment has been minimized.
|
Cc @larsbergstrom or anyone else with a VR kit that want to manually test this branch. |
|
This needs a squash, and I would like @larsbergstrom to take a quick look. LGTM otherwise. |
| @@ -27,3 +27,5 @@ Servo.app | |||
|
|
|||
| # VSCode | |||
| .vscode | |||
|
|
|||
| *.iml | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MortimerGoro
Apr 12, 2017
Author
Contributor
It's a file automatically created by Android Studio (I use it to test on Android).
I'll remove it from here because that change belongs more to #15773, which is about to be merged
|
Squash ready ;) |
|
@bors-servo r=nox,larsbergstrom I've tested the gamekit stuff and can confirm controllers work :-) |
|
|
Gamepad API implementation <!-- Please describe your changes on the following line: --> Gamepad API implementation. Tested with HTC Vive and Daydream controllers ;) --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #10977 <!-- Either: --> - [ ] There are tests for these changes OR - [X] These changes do not require tests because : current gamepad tests are manual (https://github.com/w3c/web-platform-tests/tree/master/gamepad). There is a open issue about the best way to test WebVR/Gamepad without real devices immersive-web/webxr#187. We'll work on the testing suite & mock devices/data on a separate issue. <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16260) <!-- Reviewable:end -->
|
|
danShumway
commented
Jan 10, 2019
|
Did this ever make its way into master? I'm still getting |
|
It's prefed off via the dom.gamepad.enabled preference. That can be controlled from prefs.json or running with |
danShumway
commented
Jan 10, 2019
|
Thanks! Just for future reference so I don't need to bother people as much, I assume this is the list of existing preferences? |
|
Generally yes. |
MortimerGoro commentedApr 4, 2017
•
edited
Gamepad API implementation. Tested with HTC Vive and Daydream controllers ;)
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is