diff --git a/gfx/vr/VRManager.cpp b/gfx/vr/VRManager.cpp index 795ea465d74f..108165f6ba84 100644 --- a/gfx/vr/VRManager.cpp +++ b/gfx/vr/VRManager.cpp @@ -176,6 +176,9 @@ VRManager::NotifyVsync(const TimeStamp& aVsyncTimestamp) } if (bHaveEventListener) { + for (uint32_t i = 0; i < mControllerManagers.Length(); ++i) { + mControllerManagers[i]->HandleInput(); + } // If content has set an EventHandler to be notified of VR display events // we must continually refresh the VR display enumeration to check // for events that we must fire such as Window.onvrdisplayconnect @@ -359,10 +362,6 @@ VRManager::RefreshVRControllers() controller); } } - - for (uint32_t i = 0; i < mControllerManagers.Length(); ++i) { - mControllerManagers[i]->HandleInput(); - } } void diff --git a/gfx/vr/gfxVR.h b/gfx/vr/gfxVR.h index b00ae0b41206..9f4d099772f6 100644 --- a/gfx/vr/gfxVR.h +++ b/gfx/vr/gfxVR.h @@ -262,6 +262,10 @@ protected: bool mInstalled; uint32_t mControllerCount; static Atomic sControllerBase; + +private: + virtual void HandleButtonPress(uint32_t aControllerIdx, + uint64_t aButtonPressed) = 0; }; } // namespace gfx diff --git a/gfx/vr/gfxVROpenVR.cpp b/gfx/vr/gfxVROpenVR.cpp index 6037edee003d..7bc80e13c874 100644 --- a/gfx/vr/gfxVROpenVR.cpp +++ b/gfx/vr/gfxVROpenVR.cpp @@ -54,6 +54,35 @@ static pfn_VR_IsRuntimeInstalled vr_IsRuntimeInstalled = nullptr; static pfn_VR_GetStringForHmdError vr_GetStringForHmdError = nullptr; static pfn_VR_GetGenericInterface vr_GetGenericInterface = nullptr; +// EButton_System, EButton_DPad_xx, and EButton_A +// can not be triggered in Steam Vive in OpenVR SDK 1.0.3. +const uint64_t gOpenVRButtonMask[] = { + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_System), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_ApplicationMenu), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Grip), + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_DPad_Left), + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_DPad_Up), + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_DPad_Right), + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_DPad_Down), + // vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_A), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_SteamVR_Touchpad), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_SteamVR_Trigger) +}; + +const uint32_t gNumOpenVRButtonMask = sizeof(gOpenVRButtonMask) / + sizeof(uint64_t); + +const uint64_t gOpenVRAxisMask[] = { + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Axis0), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Axis1), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Axis2), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Axis3), + vr::ButtonMaskFromId(vr::EVRButtonId::k_EButton_Axis4) +}; + +const uint32_t gNumOpenVRAxisMask = sizeof(gOpenVRAxisMask) / + sizeof(uint64_t); + bool LoadOpenVRRuntime() { @@ -448,6 +477,9 @@ VRControllerOpenVR::VRControllerOpenVR() { MOZ_COUNT_CTOR_INHERITED(VRControllerOpenVR, VRControllerHost); mControllerInfo.mControllerName.AssignLiteral("OpenVR HMD"); + mControllerInfo.mMappingType = dom::GamepadMappingType::_empty; + mControllerInfo.mNumButtons = gNumOpenVRButtonMask; + mControllerInfo.mNumAxes = gNumOpenVRAxisMask; } VRControllerOpenVR::~VRControllerOpenVR() @@ -525,30 +557,38 @@ VRControllerManagerOpenVR::Destroy() void VRControllerManagerOpenVR::HandleInput() { + RefPtr controller; + vr::VRControllerState_t state; + MOZ_ASSERT(mVRSystem); // Process OpenVR controller state - for (vr::TrackedDeviceIndex_t trackedDevice = 0; - trackedDevice < vr::k_unMaxTrackedDeviceCount; trackedDevice++ ) { - vr::VRControllerState_t state; + for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) { + controller = mOpenVRController[i]; - if (mVRSystem->GetTrackedDeviceClass(trackedDevice) - != vr::TrackedDeviceClass_Controller) { - continue; - } + MOZ_ASSERT(mVRSystem->GetTrackedDeviceClass(controller->GetTrackedIndex()) + == vr::TrackedDeviceClass_Controller); - if (mVRSystem->GetControllerState(trackedDevice, &state)) { + if (mVRSystem->GetControllerState(controller->GetTrackedIndex(), &state)) { if (state.ulButtonPressed) { - // TODO: For Bug 1299929 after landing, convert the button mask to an ID button - // NewButtonEvent(1, - // 0, - // 0, - // true); + HandleButtonPress(controller->GetIndex(), state.ulButtonPressed); } + + // Handle Axis support in Bug 1299930 } } +} - return; +void +VRControllerManagerOpenVR::HandleButtonPress(uint32_t aControllerIdx, + uint64_t aButtonPressed) +{ + uint64_t buttonMask = 0; + + for (uint32_t i = 0; i < gNumOpenVRButtonMask; ++i) { + buttonMask = gOpenVRButtonMask[i]; + NewButtonEvent(aControllerIdx, i, aButtonPressed & buttonMask); + } } void @@ -591,7 +631,7 @@ VRControllerManagerOpenVR::ScanForDevices() // Not already present, add it. AddGamepad("OpenVR Gamepad", GamepadMappingType::_empty, - kOpenVRControllerAxes, kOpenVRControllerButtons); + gNumOpenVRButtonMask, gNumOpenVRAxisMask); ++mControllerCount; } } \ No newline at end of file diff --git a/gfx/vr/gfxVROpenVR.h b/gfx/vr/gfxVROpenVR.h index c9e02d13ca19..636b7e72b714 100644 --- a/gfx/vr/gfxVROpenVR.h +++ b/gfx/vr/gfxVROpenVR.h @@ -120,12 +120,12 @@ private: VRControllerManagerOpenVR(); ~VRControllerManagerOpenVR(); + virtual void HandleButtonPress(uint32_t aControllerIdx, + uint64_t aButtonPressed) override; + bool mOpenVRInstalled; nsTArray> mOpenVRController; vr::IVRSystem *mVRSystem; - - const uint32_t kOpenVRControllerButtons = 8; - const uint32_t kOpenVRControllerAxes = 5; }; } // namespace gfx