Commit 9556845a authored by Bill Orr's avatar Bill Orr Committed by Commit Bot

Only expose VR controllers as Gamepads when appropriate

VR runtime-backed controllers should only be exposed when WebXR
or WebVR APIs are available.

BUG=830935

Change-Id: I354be9e62e90e7acac3fd712fc3497c141373a47
Reviewed-on: https://chromium-review.googlesource.com/1003732
Commit-Queue: David Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarBrandon Jones <bajones@chromium.org>
Reviewed-by: default avatarBill Budge <bbudge@chromium.org>
Reviewed-by: default avatarScott Graham <scottmg@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550094}
parent 10edbe72
......@@ -138,6 +138,8 @@ class Gamepad {
GamepadHand hand;
unsigned display_id;
bool is_xr = false;
};
#pragma pack(pop)
......
......@@ -85,6 +85,7 @@ void CardboardGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) {
// This is the first time we've seen this device, so do some one-time
// initialization
pad.connected = true;
pad.is_xr = true;
CopyToUString(pad.id, Gamepad::kIdLengthCap,
base::UTF8ToUTF16("Cardboard Button"));
CopyToUString(pad.mapping, Gamepad::kMappingLengthCap,
......
......@@ -95,6 +95,8 @@ void GvrGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) {
pad.display_id = display_id_;
pad.is_xr = true;
pad.hand =
provided_data.right_handed ? GamepadHand::kRight : GamepadHand::kLeft;
}
......
......@@ -77,6 +77,7 @@ void SetTouchData(PadState* state,
if (!state->is_initialized) {
state->is_initialized = true;
pad.connected = true;
pad.is_xr = true;
pad.pose.not_null = true;
pad.pose.has_orientation = true;
pad.pose.has_position = true;
......@@ -225,6 +226,7 @@ void OculusGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) {
state->is_initialized = true;
swprintf(pad.id, Gamepad::kIdLengthCap, L"Oculus Remote");
pad.connected = true;
pad.is_xr = true;
pad.display_id = display_id_;
}
pad.timestamp = input_state.TimeInSeconds;
......
......@@ -94,6 +94,7 @@ void OpenVRGamepadDataFetcher::GetGamepadData(bool devices_changed_hint) {
sizeof(controller_state))) {
pad.timestamp = controller_state.unPacketNum;
pad.connected = true;
pad.is_xr = true;
pad.pose.not_null = true;
......
......@@ -112,8 +112,12 @@ struct WebKitGamepad {
// Gamepad Extensions member, unused by ppapi.
WebKitGamepadHand hand;
// ID of the VRDisplay this gamepad is associated with, if any.
// ID of the VRDisplay this gamepad is associated with, if any. Unused by
// ppapi.
unsigned display_id;
// True if this controller is backed by VR APIs, unused by ppapi.
bool is_xr;
};
// This must match the definition of blink::Gamepads. The GamepadHost unit
......
......@@ -31,6 +31,7 @@
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/modules/gamepad/gamepad_dispatcher.h"
#include "third_party/blink/renderer/modules/gamepad/gamepad_event.h"
......@@ -87,14 +88,31 @@ static void SampleGamepad(unsigned index,
}
template <typename GamepadType, typename ListType>
static void SampleGamepads(ListType* into) {
static void SampleGamepads(ListType* into, const ExecutionContext* context) {
device::Gamepads gamepads;
GamepadDispatcher::Instance().SampleGamepads(gamepads);
for (unsigned i = 0; i < device::Gamepads::kItemsLengthCap; ++i) {
device::Gamepad& web_gamepad = gamepads.items[i];
if (web_gamepad.connected) {
bool hide_xr_gamepad = false;
if (web_gamepad.is_xr) {
bool webxr_enabled =
(context && OriginTrials::webXRGamepadSupportEnabled(context) &&
OriginTrials::webXREnabled(context));
bool webvr_enabled = (context && OriginTrials::webVREnabled(context));
if (!webxr_enabled && !webvr_enabled) {
// If neither WebXR nor WebVR are enabled, we should not expose XR-
// backed gamepads.
hide_xr_gamepad = true;
}
}
if (hide_xr_gamepad) {
into->Set(i, nullptr);
} else if (web_gamepad.connected) {
GamepadType* gamepad = into->item(i);
if (!gamepad)
gamepad = GamepadType::Create();
......@@ -261,6 +279,9 @@ void NavigatorGamepad::DidRemoveGamepadEventListeners() {
}
void NavigatorGamepad::SampleAndCheckConnectedGamepads() {
ExecutionContext* execution_context =
DomWindow() ? DomWindow()->GetExecutionContext() : nullptr;
if (StartUpdatingIfAttached()) {
if (!gamepads_)
gamepads_ = GamepadList::Create();
......@@ -270,7 +291,7 @@ void NavigatorGamepad::SampleAndCheckConnectedGamepads() {
// Compare the current sample with the old data and enqueue connection
// events for any differences.
SampleGamepads<Gamepad>(gamepads_back_.Get());
SampleGamepads<Gamepad>(gamepads_back_.Get(), execution_context);
if (CheckConnectedGamepads(gamepads_.Get(), gamepads_back_.Get())) {
// If we had any disconnected gamepads, we can't overwrite gamepads_
// because the Gamepad object from the old buffer is reused as the
......@@ -283,7 +304,7 @@ void NavigatorGamepad::SampleAndCheckConnectedGamepads() {
dispatch_one_event_runner_->RunAsync();
}
}
SampleGamepads<Gamepad>(gamepads_.Get());
SampleGamepads<Gamepad>(gamepads_.Get(), execution_context);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment