2016-07-22 22:41:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "prlink.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
#include "mozilla/gfx/Quaternion.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "CompositorD3D11.h"
|
|
|
|
#include "TextureD3D11.h"
|
|
|
|
#endif // XP_WIN
|
|
|
|
|
|
|
|
#include "gfxVROpenVR.h"
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIScreenManager.h"
|
|
|
|
#include "openvr/openvr.h"
|
2016-11-04 10:53:01 +03:00
|
|
|
|
|
|
|
#include "mozilla/dom/GamepadEventTypes.h"
|
|
|
|
#include "mozilla/dom/GamepadBinding.h"
|
2016-07-22 22:41:00 +03:00
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
# define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gfx::impl;
|
|
|
|
using namespace mozilla::layers;
|
2016-10-07 11:58:01 +03:00
|
|
|
using namespace mozilla::dom;
|
2016-07-22 22:41:00 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
extern "C" {
|
|
|
|
typedef uint32_t (VR_CALLTYPE * pfn_VR_InitInternal)(::vr::HmdError *peError, ::vr::EVRApplicationType eApplicationType);
|
|
|
|
typedef void (VR_CALLTYPE * pfn_VR_ShutdownInternal)();
|
|
|
|
typedef bool (VR_CALLTYPE * pfn_VR_IsHmdPresent)();
|
|
|
|
typedef bool (VR_CALLTYPE * pfn_VR_IsRuntimeInstalled)();
|
|
|
|
typedef const char * (VR_CALLTYPE * pfn_VR_GetStringForHmdError)(::vr::HmdError error);
|
|
|
|
typedef void * (VR_CALLTYPE * pfn_VR_GetGenericInterface)(const char *pchInterfaceVersion, ::vr::HmdError *peError);
|
|
|
|
} // extern "C"
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static pfn_VR_InitInternal vr_InitInternal = nullptr;
|
|
|
|
static pfn_VR_ShutdownInternal vr_ShutdownInternal = nullptr;
|
|
|
|
static pfn_VR_IsHmdPresent vr_IsHmdPresent = nullptr;
|
|
|
|
static pfn_VR_IsRuntimeInstalled vr_IsRuntimeInstalled = nullptr;
|
|
|
|
static pfn_VR_GetStringForHmdError vr_GetStringForHmdError = nullptr;
|
|
|
|
static pfn_VR_GetGenericInterface vr_GetGenericInterface = nullptr;
|
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
#define BTN_MASK_FROM_ID(_id) \
|
|
|
|
vr::ButtonMaskFromId(vr::EVRButtonId::_id)
|
2016-10-21 19:01:26 +03:00
|
|
|
|
2016-10-13 09:35:13 +03:00
|
|
|
|
2016-07-22 22:41:00 +03:00
|
|
|
bool
|
|
|
|
LoadOpenVRRuntime()
|
|
|
|
{
|
|
|
|
static PRLibrary *openvrLib = nullptr;
|
2016-11-23 04:40:54 +03:00
|
|
|
std::string openvrPath = gfxPrefs::VROpenVRRuntime();
|
2016-07-22 22:41:00 +03:00
|
|
|
|
2016-11-23 04:40:54 +03:00
|
|
|
if (!openvrPath.c_str())
|
2016-07-22 22:41:00 +03:00
|
|
|
return false;
|
|
|
|
|
2016-11-23 04:40:54 +03:00
|
|
|
openvrLib = PR_LoadLibrary(openvrPath.c_str());
|
2016-07-22 22:41:00 +03:00
|
|
|
if (!openvrLib)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#define REQUIRE_FUNCTION(_x) do { \
|
|
|
|
*(void **)&vr_##_x = (void *) PR_FindSymbol(openvrLib, "VR_" #_x); \
|
|
|
|
if (!vr_##_x) { printf_stderr("VR_" #_x " symbol missing\n"); return false; } \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
REQUIRE_FUNCTION(InitInternal);
|
|
|
|
REQUIRE_FUNCTION(ShutdownInternal);
|
|
|
|
REQUIRE_FUNCTION(IsHmdPresent);
|
|
|
|
REQUIRE_FUNCTION(IsRuntimeInstalled);
|
|
|
|
REQUIRE_FUNCTION(GetStringForHmdError);
|
|
|
|
REQUIRE_FUNCTION(GetGenericInterface);
|
|
|
|
|
|
|
|
#undef REQUIRE_FUNCTION
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VRDisplayOpenVR::VRDisplayOpenVR(::vr::IVRSystem *aVRSystem,
|
|
|
|
::vr::IVRChaperone *aVRChaperone,
|
|
|
|
::vr::IVRCompositor *aVRCompositor)
|
2016-10-04 11:22:42 +03:00
|
|
|
: VRDisplayHost(VRDeviceType::OpenVR)
|
2016-07-22 22:41:00 +03:00
|
|
|
, mVRSystem(aVRSystem)
|
|
|
|
, mVRChaperone(aVRChaperone)
|
|
|
|
, mVRCompositor(aVRCompositor)
|
|
|
|
, mIsPresenting(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(VRDisplayOpenVR, VRDisplayHost);
|
|
|
|
|
|
|
|
mDisplayInfo.mDisplayName.AssignLiteral("OpenVR HMD");
|
|
|
|
mDisplayInfo.mIsConnected = true;
|
2016-10-19 01:18:10 +03:00
|
|
|
mDisplayInfo.mIsMounted = false;
|
2016-07-22 22:41:00 +03:00
|
|
|
mDisplayInfo.mCapabilityFlags = VRDisplayCapabilityFlags::Cap_None |
|
|
|
|
VRDisplayCapabilityFlags::Cap_Orientation |
|
|
|
|
VRDisplayCapabilityFlags::Cap_Position |
|
|
|
|
VRDisplayCapabilityFlags::Cap_External |
|
|
|
|
VRDisplayCapabilityFlags::Cap_Present |
|
|
|
|
VRDisplayCapabilityFlags::Cap_StageParameters;
|
|
|
|
|
2016-10-19 01:18:10 +03:00
|
|
|
::vr::ETrackedPropertyError err;
|
|
|
|
bool bHasProximitySensor = mVRSystem->GetBoolTrackedDeviceProperty(::vr::k_unTrackedDeviceIndex_Hmd, ::vr::Prop_ContainsProximitySensor_Bool, &err);
|
|
|
|
if (err == ::vr::TrackedProp_Success && bHasProximitySensor) {
|
|
|
|
mDisplayInfo.mCapabilityFlags |= VRDisplayCapabilityFlags::Cap_MountDetection;
|
|
|
|
}
|
|
|
|
|
2016-07-22 22:41:00 +03:00
|
|
|
mVRCompositor->SetTrackingSpace(::vr::TrackingUniverseSeated);
|
|
|
|
|
|
|
|
uint32_t w, h;
|
|
|
|
mVRSystem->GetRecommendedRenderTargetSize(&w, &h);
|
|
|
|
mDisplayInfo.mEyeResolution.width = w;
|
|
|
|
mDisplayInfo.mEyeResolution.height = h;
|
|
|
|
|
|
|
|
// SteamVR gives the application a single FOV to use; it's not configurable as with Oculus
|
|
|
|
for (uint32_t eye = 0; eye < 2; ++eye) {
|
|
|
|
// get l/r/t/b clip plane coordinates
|
|
|
|
float l, r, t, b;
|
|
|
|
mVRSystem->GetProjectionRaw(static_cast<::vr::Hmd_Eye>(eye), &l, &r, &t, &b);
|
|
|
|
mDisplayInfo.mEyeFOV[eye].SetFromTanRadians(-t, r, b, -l);
|
|
|
|
|
|
|
|
::vr::HmdMatrix34_t eyeToHead = mVRSystem->GetEyeToHeadTransform(static_cast<::vr::Hmd_Eye>(eye));
|
|
|
|
|
|
|
|
mDisplayInfo.mEyeTranslation[eye].x = eyeToHead.m[0][3];
|
|
|
|
mDisplayInfo.mEyeTranslation[eye].y = eyeToHead.m[1][3];
|
|
|
|
mDisplayInfo.mEyeTranslation[eye].z = eyeToHead.m[2][3];
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateStageParameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
VRDisplayOpenVR::~VRDisplayOpenVR()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(VRDisplayOpenVR, VRDisplayHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::Destroy()
|
|
|
|
{
|
|
|
|
StopPresentation();
|
|
|
|
vr_ShutdownInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::UpdateStageParameters()
|
|
|
|
{
|
|
|
|
float sizeX = 0.0f;
|
|
|
|
float sizeZ = 0.0f;
|
|
|
|
if (mVRChaperone->GetPlayAreaSize(&sizeX, &sizeZ)) {
|
|
|
|
::vr::HmdMatrix34_t t = mVRSystem->GetSeatedZeroPoseToStandingAbsoluteTrackingPose();
|
|
|
|
mDisplayInfo.mStageSize.width = sizeX;
|
|
|
|
mDisplayInfo.mStageSize.height = sizeZ;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._11 = t.m[0][0];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._12 = t.m[1][0];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._13 = t.m[2][0];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._14 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._21 = t.m[0][1];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._22 = t.m[1][1];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._23 = t.m[2][1];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._24 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._31 = t.m[0][2];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._32 = t.m[1][2];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._33 = t.m[2][2];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._34 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._41 = t.m[0][3];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._42 = t.m[1][3];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._43 = t.m[2][3];
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._44 = 1.0f;
|
|
|
|
} else {
|
|
|
|
// If we fail, fall back to reasonable defaults.
|
|
|
|
// 1m x 1m space, 0.75m high in seated position
|
|
|
|
|
|
|
|
mDisplayInfo.mStageSize.width = 1.0f;
|
|
|
|
mDisplayInfo.mStageSize.height = 1.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._11 = 1.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._12 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._13 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._14 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._21 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._22 = 1.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._23 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._24 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._31 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._32 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._33 = 1.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._34 = 0.0f;
|
|
|
|
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._41 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._42 = 0.75f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._43 = 0.0f;
|
|
|
|
mDisplayInfo.mSittingToStandingTransform._44 = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::ZeroSensor()
|
|
|
|
{
|
|
|
|
mVRSystem->ResetSeatedZeroPose();
|
|
|
|
UpdateStageParameters();
|
|
|
|
}
|
|
|
|
|
|
|
|
VRHMDSensorState
|
|
|
|
VRDisplayOpenVR::GetSensorState()
|
|
|
|
{
|
|
|
|
return GetSensorState(0.0f);
|
|
|
|
}
|
|
|
|
|
2016-10-19 01:18:10 +03:00
|
|
|
void
|
|
|
|
VRDisplayOpenVR::PollEvents()
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
2016-10-19 01:18:10 +03:00
|
|
|
::vr::VREvent_t event;
|
|
|
|
while (mVRSystem->PollNextEvent(&event, sizeof(event))) {
|
|
|
|
if (event.trackedDeviceIndex == ::vr::k_unTrackedDeviceIndex_Hmd) {
|
|
|
|
switch (event.eventType) {
|
|
|
|
case ::vr::VREvent_TrackedDeviceUserInteractionStarted:
|
|
|
|
mDisplayInfo.mIsMounted = true;
|
|
|
|
break;
|
|
|
|
case ::vr::VREvent_TrackedDeviceUserInteractionEnded:
|
|
|
|
mDisplayInfo.mIsMounted = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// ignore
|
|
|
|
break;
|
|
|
|
}
|
2016-07-22 22:41:00 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-19 01:18:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VRHMDSensorState
|
|
|
|
VRDisplayOpenVR::GetSensorState(double timeOffset)
|
|
|
|
{
|
|
|
|
PollEvents();
|
2016-07-22 22:41:00 +03:00
|
|
|
|
|
|
|
::vr::TrackedDevicePose_t poses[::vr::k_unMaxTrackedDeviceCount];
|
|
|
|
// Note: We *must* call WaitGetPoses in order for any rendering to happen at all
|
|
|
|
mVRCompositor->WaitGetPoses(poses, ::vr::k_unMaxTrackedDeviceCount, nullptr, 0);
|
|
|
|
|
|
|
|
VRHMDSensorState result;
|
|
|
|
result.Clear();
|
2017-03-04 02:17:19 +03:00
|
|
|
|
|
|
|
::vr::Compositor_FrameTiming timing;
|
|
|
|
timing.m_nSize = sizeof(::vr::Compositor_FrameTiming);
|
|
|
|
if (mVRCompositor->GetFrameTiming(&timing)) {
|
|
|
|
result.timestamp = timing.m_flSystemTimeInSeconds;
|
|
|
|
} else {
|
|
|
|
// This should not happen, but log it just in case
|
|
|
|
NS_WARNING("OpenVR - IVRCompositor::GetFrameTiming failed");
|
|
|
|
}
|
2016-07-22 22:41:00 +03:00
|
|
|
|
|
|
|
if (poses[::vr::k_unTrackedDeviceIndex_Hmd].bDeviceIsConnected &&
|
|
|
|
poses[::vr::k_unTrackedDeviceIndex_Hmd].bPoseIsValid &&
|
|
|
|
poses[::vr::k_unTrackedDeviceIndex_Hmd].eTrackingResult == ::vr::TrackingResult_Running_OK)
|
|
|
|
{
|
|
|
|
const ::vr::TrackedDevicePose_t& pose = poses[::vr::k_unTrackedDeviceIndex_Hmd];
|
|
|
|
|
|
|
|
gfx::Matrix4x4 m;
|
|
|
|
// NOTE! mDeviceToAbsoluteTracking is a 3x4 matrix, not 4x4. But
|
|
|
|
// because of its arrangement, we can copy the 12 elements in and
|
|
|
|
// then transpose them to the right place. We do this so we can
|
|
|
|
// pull out a Quaternion.
|
|
|
|
memcpy(&m._11, &pose.mDeviceToAbsoluteTracking, sizeof(float) * 12);
|
|
|
|
m.Transpose();
|
|
|
|
|
|
|
|
gfx::Quaternion rot;
|
|
|
|
rot.SetFromRotationMatrix(m);
|
|
|
|
rot.Invert();
|
|
|
|
|
|
|
|
result.flags |= VRDisplayCapabilityFlags::Cap_Orientation;
|
|
|
|
result.orientation[0] = rot.x;
|
|
|
|
result.orientation[1] = rot.y;
|
|
|
|
result.orientation[2] = rot.z;
|
|
|
|
result.orientation[3] = rot.w;
|
|
|
|
result.angularVelocity[0] = pose.vAngularVelocity.v[0];
|
|
|
|
result.angularVelocity[1] = pose.vAngularVelocity.v[1];
|
|
|
|
result.angularVelocity[2] = pose.vAngularVelocity.v[2];
|
|
|
|
|
|
|
|
result.flags |= VRDisplayCapabilityFlags::Cap_Position;
|
|
|
|
result.position[0] = m._41;
|
|
|
|
result.position[1] = m._42;
|
|
|
|
result.position[2] = m._43;
|
|
|
|
result.linearVelocity[0] = pose.vVelocity.v[0];
|
|
|
|
result.linearVelocity[1] = pose.vVelocity.v[1];
|
|
|
|
result.linearVelocity[2] = pose.vVelocity.v[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::StartPresentation()
|
|
|
|
{
|
|
|
|
if (mIsPresenting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsPresenting = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::StopPresentation()
|
|
|
|
{
|
|
|
|
if (!mIsPresenting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVRCompositor->ClearLastSubmittedFrame();
|
|
|
|
|
|
|
|
mIsPresenting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::SubmitFrame(TextureSourceD3D11* aSource,
|
|
|
|
const IntSize& aSize,
|
|
|
|
const VRHMDSensorState& aSensorState,
|
|
|
|
const gfx::Rect& aLeftEyeRect,
|
|
|
|
const gfx::Rect& aRightEyeRect)
|
|
|
|
{
|
|
|
|
if (!mIsPresenting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
::vr::Texture_t tex;
|
|
|
|
tex.handle = (void *)aSource->GetD3D11Texture();
|
|
|
|
tex.eType = ::vr::EGraphicsAPIConvention::API_DirectX;
|
|
|
|
tex.eColorSpace = ::vr::EColorSpace::ColorSpace_Auto;
|
|
|
|
|
|
|
|
::vr::VRTextureBounds_t bounds;
|
|
|
|
bounds.uMin = aLeftEyeRect.x;
|
|
|
|
bounds.vMin = 1.0 - aLeftEyeRect.y;
|
|
|
|
bounds.uMax = aLeftEyeRect.x + aLeftEyeRect.width;
|
|
|
|
bounds.vMax = 1.0 - aLeftEyeRect.y - aLeftEyeRect.height;
|
|
|
|
|
|
|
|
::vr::EVRCompositorError err;
|
|
|
|
err = mVRCompositor->Submit(::vr::EVREye::Eye_Left, &tex, &bounds);
|
|
|
|
if (err != ::vr::EVRCompositorError::VRCompositorError_None) {
|
|
|
|
printf_stderr("OpenVR Compositor Submit() failed.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bounds.uMin = aRightEyeRect.x;
|
|
|
|
bounds.vMin = 1.0 - aRightEyeRect.y;
|
|
|
|
bounds.uMax = aRightEyeRect.x + aRightEyeRect.width;
|
|
|
|
bounds.vMax = 1.0 - aRightEyeRect.y - aRightEyeRect.height;
|
|
|
|
|
|
|
|
err = mVRCompositor->Submit(::vr::EVREye::Eye_Right, &tex, &bounds);
|
|
|
|
if (err != ::vr::EVRCompositorError::VRCompositorError_None) {
|
|
|
|
printf_stderr("OpenVR Compositor Submit() failed.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
mVRCompositor->PostPresentHandoff();
|
|
|
|
|
|
|
|
// Trigger the next VSync immediately
|
|
|
|
VRManager *vm = VRManager::Get();
|
|
|
|
MOZ_ASSERT(vm);
|
|
|
|
vm->NotifyVRVsync(mDisplayInfo.mDisplayID);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayOpenVR::NotifyVSync()
|
|
|
|
{
|
|
|
|
// We update mIsConneced once per frame.
|
|
|
|
mDisplayInfo.mIsConnected = vr_IsHmdPresent();
|
2016-10-19 01:18:10 +03:00
|
|
|
|
|
|
|
// Make sure we respond to OpenVR events even when not presenting
|
|
|
|
PollEvents();
|
2016-07-22 22:41:00 +03:00
|
|
|
}
|
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
VRControllerOpenVR::VRControllerOpenVR(dom::GamepadHand aHand, uint32_t aNumButtons,
|
|
|
|
uint32_t aNumAxes)
|
2017-02-06 11:12:52 +03:00
|
|
|
: VRControllerHost(VRDeviceType::OpenVR)
|
2017-03-16 10:57:04 +03:00
|
|
|
, mTrigger(0)
|
2017-02-06 11:12:52 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(VRControllerOpenVR, VRControllerHost);
|
2017-02-10 10:13:44 +03:00
|
|
|
mControllerInfo.mControllerName.AssignLiteral("OpenVR Gamepad");
|
2017-02-06 11:12:52 +03:00
|
|
|
mControllerInfo.mMappingType = GamepadMappingType::_empty;
|
2017-02-13 06:18:11 +03:00
|
|
|
mControllerInfo.mHand = aHand;
|
2017-03-16 10:57:04 +03:00
|
|
|
mControllerInfo.mNumButtons = aNumButtons;
|
|
|
|
mControllerInfo.mNumAxes = aNumAxes;
|
2017-02-06 11:12:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VRControllerOpenVR::~VRControllerOpenVR()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(VRControllerOpenVR, VRControllerHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRControllerOpenVR::SetTrackedIndex(uint32_t aTrackedIndex)
|
|
|
|
{
|
|
|
|
mTrackedIndex = aTrackedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
VRControllerOpenVR::GetTrackedIndex()
|
|
|
|
{
|
|
|
|
return mTrackedIndex;
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
void
|
|
|
|
VRControllerOpenVR::SetTrigger(float aValue)
|
|
|
|
{
|
|
|
|
mTrigger = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
VRControllerOpenVR::GetTrigger()
|
|
|
|
{
|
|
|
|
return mTrigger;
|
|
|
|
}
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::VRSystemManagerOpenVR()
|
2017-03-16 10:57:04 +03:00
|
|
|
: mVRSystem(nullptr)
|
|
|
|
, mOpenVRInstalled(false)
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
/*static*/ already_AddRefed<VRSystemManagerOpenVR>
|
|
|
|
VRSystemManagerOpenVR::Create()
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gfxPrefs::VREnabled() || !gfxPrefs::VROpenVREnabled()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LoadOpenVRRuntime()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
RefPtr<VRSystemManagerOpenVR> manager = new VRSystemManagerOpenVR();
|
2016-07-22 22:41:00 +03:00
|
|
|
return manager.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::Init()
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
|
|
|
if (mOpenVRInstalled)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!vr_IsRuntimeInstalled())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mOpenVRInstalled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::Destroy()
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
|
|
|
if (mOpenVRInstalled) {
|
|
|
|
if (mOpenVRHMD) {
|
|
|
|
mOpenVRHMD = nullptr;
|
|
|
|
}
|
2017-01-24 12:49:11 +03:00
|
|
|
RemoveControllers();
|
2017-02-06 11:12:52 +03:00
|
|
|
mVRSystem = nullptr;
|
2016-07-22 22:41:00 +03:00
|
|
|
mOpenVRInstalled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult)
|
2016-07-22 22:41:00 +03:00
|
|
|
{
|
|
|
|
if (!mOpenVRInstalled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vr_IsHmdPresent()) {
|
|
|
|
if (mOpenVRHMD) {
|
|
|
|
mOpenVRHMD = nullptr;
|
|
|
|
}
|
|
|
|
} else if (mOpenVRHMD == nullptr) {
|
|
|
|
::vr::HmdError err;
|
|
|
|
|
|
|
|
vr_InitInternal(&err, ::vr::EVRApplicationType::VRApplication_Scene);
|
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
::vr::IVRSystem *system = (::vr::IVRSystem *)vr_GetGenericInterface(::vr::IVRSystem_Version, &err);
|
|
|
|
if (err || !system) {
|
|
|
|
vr_ShutdownInternal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::vr::IVRChaperone *chaperone = (::vr::IVRChaperone *)vr_GetGenericInterface(::vr::IVRChaperone_Version, &err);
|
|
|
|
if (err || !chaperone) {
|
|
|
|
vr_ShutdownInternal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::vr::IVRCompositor *compositor = (::vr::IVRCompositor*)vr_GetGenericInterface(::vr::IVRCompositor_Version, &err);
|
|
|
|
if (err || !compositor) {
|
|
|
|
vr_ShutdownInternal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 12:49:11 +03:00
|
|
|
mVRSystem = system;
|
2016-07-22 22:41:00 +03:00
|
|
|
mOpenVRHMD = new VRDisplayOpenVR(system, chaperone, compositor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOpenVRHMD) {
|
|
|
|
aHMDResult.AppendElement(mOpenVRHMD);
|
|
|
|
}
|
|
|
|
}
|
2016-10-07 11:58:01 +03:00
|
|
|
|
2017-03-22 04:58:06 +03:00
|
|
|
bool
|
|
|
|
VRSystemManagerOpenVR::GetIsPresenting()
|
|
|
|
{
|
|
|
|
if (mOpenVRHMD) {
|
|
|
|
VRDisplayInfo displayInfo(mOpenVRHMD->GetDisplayInfo());
|
|
|
|
return displayInfo.GetIsPresenting();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:30:26 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::HandleInput()
|
2016-10-07 12:00:45 +03:00
|
|
|
{
|
2017-02-03 05:15:02 +03:00
|
|
|
// mVRSystem is available after VRDisplay is created
|
|
|
|
// at GetHMDs().
|
|
|
|
if (!mVRSystem) {
|
2016-10-20 07:01:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 05:15:02 +03:00
|
|
|
RefPtr<impl::VRControllerOpenVR> controller;
|
|
|
|
vr::VRControllerState_t state;
|
2016-10-24 13:09:11 +03:00
|
|
|
vr::TrackedDevicePose_t poses[vr::k_unMaxTrackedDeviceCount];
|
|
|
|
mVRSystem->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseSeated, 0.0f,
|
|
|
|
poses, vr::k_unMaxTrackedDeviceCount);
|
2016-10-07 12:00:45 +03:00
|
|
|
// Process OpenVR controller state
|
2016-10-13 09:35:13 +03:00
|
|
|
for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
|
2017-03-16 10:57:04 +03:00
|
|
|
uint32_t axisIdx = 0;
|
|
|
|
uint32_t buttonIdx = 0;
|
2016-10-13 09:35:13 +03:00
|
|
|
controller = mOpenVRController[i];
|
2017-03-16 10:57:04 +03:00
|
|
|
const uint32_t trackedIndex = controller->GetTrackedIndex();
|
2016-10-07 12:00:45 +03:00
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
MOZ_ASSERT(mVRSystem->GetTrackedDeviceClass(trackedIndex)
|
2016-10-13 09:35:13 +03:00
|
|
|
== vr::TrackedDeviceClass_Controller);
|
2016-10-07 12:00:45 +03:00
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
if (mVRSystem->GetControllerState(trackedIndex, &state)) {
|
|
|
|
for (uint32_t j = 0; j < vr::k_unControllerStateAxisCount; ++j) {
|
|
|
|
const uint32_t axisType = mVRSystem->GetInt32TrackedDeviceProperty(
|
|
|
|
trackedIndex,
|
|
|
|
static_cast<vr::TrackedDeviceProperty>(
|
|
|
|
vr::Prop_Axis0Type_Int32 + j));
|
|
|
|
switch (axisType) {
|
|
|
|
case vr::EVRControllerAxisType::k_eControllerAxis_Joystick:
|
|
|
|
case vr::EVRControllerAxisType::k_eControllerAxis_TrackPad:
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleAxisMove(i, axisIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
state.rAxis[j].x);
|
|
|
|
++axisIdx;
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleAxisMove(i, axisIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
state.rAxis[j].y);
|
|
|
|
++axisIdx;
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
vr::ButtonMaskFromId(
|
|
|
|
static_cast<vr::EVRButtonId>(vr::k_EButton_Axis0 + j)),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
break;
|
|
|
|
case vr::EVRControllerAxisType::k_eControllerAxis_Trigger:
|
|
|
|
HandleTriggerPress(i, buttonIdx,
|
|
|
|
vr::ButtonMaskFromId(
|
|
|
|
static_cast<vr::EVRButtonId>(vr::k_EButton_Axis0 + j)),
|
|
|
|
state.rAxis[j].x, state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(axisIdx ==
|
|
|
|
controller->GetControllerInfo().GetNumAxes());
|
|
|
|
|
|
|
|
const uint64_t supportedButtons = mVRSystem->GetUint64TrackedDeviceProperty(
|
|
|
|
trackedIndex, vr::Prop_SupportedButtons_Uint64);
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_A)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_A),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_Grip)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_Grip),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_ApplicationMenu)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_ApplicationMenu),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Left)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Left),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Up)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Up),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Right)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Right),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
if (supportedButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Down)) {
|
2017-03-17 06:09:24 +03:00
|
|
|
HandleButtonPress(i, buttonIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Down),
|
|
|
|
state.ulButtonPressed);
|
|
|
|
++buttonIdx;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(buttonIdx ==
|
|
|
|
controller->GetControllerInfo().GetNumButtons());
|
|
|
|
controller->SetButtonPressed(state.ulButtonPressed);
|
|
|
|
|
|
|
|
// Start to process pose
|
|
|
|
const ::vr::TrackedDevicePose_t& pose = poses[trackedIndex];
|
|
|
|
|
|
|
|
if (pose.bDeviceIsConnected && pose.bPoseIsValid &&
|
|
|
|
pose.eTrackingResult == vr::TrackingResult_Running_OK) {
|
|
|
|
gfx::Matrix4x4 m;
|
|
|
|
|
|
|
|
// NOTE! mDeviceToAbsoluteTracking is a 3x4 matrix, not 4x4. But
|
|
|
|
// because of its arrangement, we can copy the 12 elements in and
|
|
|
|
// then transpose them to the right place. We do this so we can
|
|
|
|
// pull out a Quaternion.
|
|
|
|
memcpy(&m.components, &pose.mDeviceToAbsoluteTracking, sizeof(float) * 12);
|
|
|
|
m.Transpose();
|
|
|
|
|
|
|
|
gfx::Quaternion rot;
|
|
|
|
rot.SetFromRotationMatrix(m);
|
|
|
|
rot.Invert();
|
|
|
|
|
|
|
|
GamepadPoseState poseState;
|
|
|
|
poseState.flags |= GamepadCapabilityFlags::Cap_Orientation;
|
|
|
|
poseState.orientation[0] = rot.x;
|
|
|
|
poseState.orientation[1] = rot.y;
|
|
|
|
poseState.orientation[2] = rot.z;
|
|
|
|
poseState.orientation[3] = rot.w;
|
|
|
|
poseState.angularVelocity[0] = pose.vAngularVelocity.v[0];
|
|
|
|
poseState.angularVelocity[1] = pose.vAngularVelocity.v[1];
|
|
|
|
poseState.angularVelocity[2] = pose.vAngularVelocity.v[2];
|
|
|
|
|
|
|
|
poseState.flags |= GamepadCapabilityFlags::Cap_Position;
|
|
|
|
poseState.position[0] = m._41;
|
|
|
|
poseState.position[1] = m._42;
|
|
|
|
poseState.position[2] = m._43;
|
|
|
|
poseState.linearVelocity[0] = pose.vVelocity.v[0];
|
|
|
|
poseState.linearVelocity[1] = pose.vVelocity.v[1];
|
|
|
|
poseState.linearVelocity[2] = pose.vVelocity.v[2];
|
2017-03-17 06:09:24 +03:00
|
|
|
HandlePoseTracking(i, poseState, controller);
|
2017-03-16 10:57:04 +03:00
|
|
|
}
|
2016-10-24 13:09:11 +03:00
|
|
|
}
|
2016-10-07 12:00:45 +03:00
|
|
|
}
|
2016-10-13 09:35:13 +03:00
|
|
|
}
|
2016-10-07 12:00:45 +03:00
|
|
|
|
2016-10-13 09:35:13 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::HandleButtonPress(uint32_t aControllerIdx,
|
2017-03-16 10:57:04 +03:00
|
|
|
uint32_t aButton,
|
|
|
|
uint64_t aButtonMask,
|
2017-01-24 12:49:11 +03:00
|
|
|
uint64_t aButtonPressed)
|
2016-10-13 09:35:13 +03:00
|
|
|
{
|
2017-03-16 10:57:04 +03:00
|
|
|
RefPtr<impl::VRControllerOpenVR> controller(mOpenVRController[aControllerIdx]);
|
|
|
|
MOZ_ASSERT(controller);
|
|
|
|
const uint64_t diff = (controller->GetButtonPressed() ^ aButtonPressed);
|
2016-11-08 03:15:01 +03:00
|
|
|
|
|
|
|
if (!diff) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 09:35:13 +03:00
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
if (diff & aButtonMask) {
|
|
|
|
// diff & aButtonPressed would be true while a new button press
|
|
|
|
// event, otherwise it is an old press event and needs to notify
|
|
|
|
// the button has been released.
|
|
|
|
NewButtonEvent(aControllerIdx, aButton, aButtonMask & aButtonPressed,
|
|
|
|
(aButtonMask & aButtonPressed) ? 1.0L : 0.0L);
|
2016-10-13 09:35:13 +03:00
|
|
|
}
|
2017-03-16 10:57:04 +03:00
|
|
|
}
|
2016-11-08 03:15:01 +03:00
|
|
|
|
2017-03-16 10:57:04 +03:00
|
|
|
void
|
|
|
|
VRSystemManagerOpenVR::HandleTriggerPress(uint32_t aControllerIdx,
|
|
|
|
uint32_t aButton,
|
|
|
|
uint64_t aButtonMask,
|
|
|
|
float aValue,
|
|
|
|
uint64_t aButtonPressed)
|
|
|
|
{
|
|
|
|
RefPtr<impl::VRControllerOpenVR> controller(mOpenVRController[aControllerIdx]);
|
|
|
|
MOZ_ASSERT(controller);
|
|
|
|
const uint64_t diff = (controller->GetButtonPressed() ^ aButtonPressed);
|
|
|
|
const float oldValue = controller->GetTrigger();
|
|
|
|
|
|
|
|
// Avoid sending duplicated events in IPC channels.
|
|
|
|
if ((oldValue != aValue) ||
|
|
|
|
(diff & aButtonMask)) {
|
|
|
|
NewButtonEvent(aControllerIdx, aButton, aButtonMask & aButtonPressed, aValue);
|
|
|
|
controller->SetTrigger(aValue);
|
|
|
|
}
|
2016-10-07 12:00:45 +03:00
|
|
|
}
|
|
|
|
|
2016-10-21 19:01:26 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::HandleAxisMove(uint32_t aControllerIdx, uint32_t aAxis,
|
|
|
|
float aValue)
|
2016-10-21 19:01:26 +03:00
|
|
|
{
|
|
|
|
if (aValue != 0.0f) {
|
|
|
|
NewAxisMove(aControllerIdx, aAxis, aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 13:09:11 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::HandlePoseTracking(uint32_t aControllerIdx,
|
|
|
|
const GamepadPoseState& aPose,
|
|
|
|
VRControllerHost* aController)
|
2016-10-24 13:09:11 +03:00
|
|
|
{
|
|
|
|
if (aPose != aController->GetPose()) {
|
|
|
|
aController->SetPose(aPose);
|
|
|
|
NewPoseState(aControllerIdx, aPose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 11:58:01 +03:00
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult)
|
2016-10-07 11:58:01 +03:00
|
|
|
{
|
|
|
|
if (!mOpenVRInstalled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aControllerResult.Clear();
|
|
|
|
for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
|
|
|
|
aControllerResult.AppendElement(mOpenVRController[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::ScanForControllers()
|
2016-10-07 11:58:01 +03:00
|
|
|
{
|
2017-01-30 12:37:16 +03:00
|
|
|
// mVRSystem is available after VRDisplay is created
|
|
|
|
// at GetHMDs().
|
|
|
|
if (!mVRSystem) {
|
2016-10-07 11:58:01 +03:00
|
|
|
return;
|
2017-01-27 06:52:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 11:57:58 +03:00
|
|
|
vr::TrackedDeviceIndex_t trackedIndexArray[vr::k_unMaxTrackedDeviceCount];
|
|
|
|
uint32_t newControllerCount = 0;
|
|
|
|
// Basically, we would have HMDs in the tracked devices,
|
|
|
|
// but we are just interested in the controllers.
|
|
|
|
for (vr::TrackedDeviceIndex_t trackedDevice = vr::k_unTrackedDeviceIndex_Hmd + 1;
|
|
|
|
trackedDevice < vr::k_unMaxTrackedDeviceCount; ++trackedDevice) {
|
|
|
|
|
2016-10-07 11:58:01 +03:00
|
|
|
if (!mVRSystem->IsTrackedDeviceConnected(trackedDevice)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-28 11:57:58 +03:00
|
|
|
if (mVRSystem->GetTrackedDeviceClass(trackedDevice)
|
|
|
|
!= vr::TrackedDeviceClass_Controller) {
|
2016-10-07 11:58:01 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-28 11:57:58 +03:00
|
|
|
trackedIndexArray[newControllerCount] = trackedDevice;
|
|
|
|
++newControllerCount;
|
|
|
|
}
|
2016-10-07 11:58:01 +03:00
|
|
|
|
2016-11-28 11:57:58 +03:00
|
|
|
if (newControllerCount != mControllerCount) {
|
2017-03-16 10:57:04 +03:00
|
|
|
// The controller count is changed, removing the existing gamepads first.
|
2016-11-28 11:57:58 +03:00
|
|
|
for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
|
2017-03-17 06:09:24 +03:00
|
|
|
RemoveGamepad(i);
|
2016-11-28 11:57:58 +03:00
|
|
|
}
|
|
|
|
mControllerCount = 0;
|
|
|
|
mOpenVRController.Clear();
|
|
|
|
|
|
|
|
// Re-adding controllers to VRControllerManager.
|
|
|
|
for (vr::TrackedDeviceIndex_t i = 0; i < newControllerCount; ++i) {
|
2017-03-16 10:57:04 +03:00
|
|
|
const vr::TrackedDeviceIndex_t trackedDevice = trackedIndexArray[i];
|
|
|
|
const vr::ETrackedControllerRole role = mVRSystem->
|
|
|
|
GetControllerRoleForTrackedDeviceIndex(
|
|
|
|
trackedDevice);
|
2016-11-14 12:35:54 +03:00
|
|
|
GamepadHand hand;
|
2017-03-16 10:57:04 +03:00
|
|
|
uint32_t numButtons = 0;
|
|
|
|
uint32_t numAxes = 0;
|
2016-11-14 12:35:54 +03:00
|
|
|
|
|
|
|
switch(role) {
|
|
|
|
case vr::ETrackedControllerRole::TrackedControllerRole_Invalid:
|
|
|
|
hand = GamepadHand::_empty;
|
|
|
|
break;
|
|
|
|
case vr::ETrackedControllerRole::TrackedControllerRole_LeftHand:
|
|
|
|
hand = GamepadHand::Left;
|
|
|
|
break;
|
|
|
|
case vr::ETrackedControllerRole::TrackedControllerRole_RightHand:
|
|
|
|
hand = GamepadHand::Right;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-16 10:57:04 +03:00
|
|
|
|
|
|
|
// Scan the axes that the controllers support
|
|
|
|
for (uint32_t j = 0; j < vr::k_unControllerStateAxisCount; ++j) {
|
|
|
|
const uint32_t supportAxis = mVRSystem->GetInt32TrackedDeviceProperty(trackedDevice,
|
|
|
|
static_cast<vr::TrackedDeviceProperty>(
|
|
|
|
vr::Prop_Axis0Type_Int32 + j));
|
|
|
|
switch (supportAxis) {
|
|
|
|
case vr::EVRControllerAxisType::k_eControllerAxis_Joystick:
|
|
|
|
case vr::EVRControllerAxisType::k_eControllerAxis_TrackPad:
|
|
|
|
numAxes += 2; // It has x and y axes.
|
|
|
|
++numButtons;
|
|
|
|
break;
|
|
|
|
case vr::k_eControllerAxis_Trigger:
|
|
|
|
++numButtons;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the buttons that the controllers support
|
|
|
|
const uint64_t supportButtons = mVRSystem->GetUint64TrackedDeviceProperty(
|
|
|
|
trackedDevice, vr::Prop_SupportedButtons_Uint64);
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_A)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_Grip)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_ApplicationMenu)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Left)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Up)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Right)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
if (supportButtons &
|
|
|
|
BTN_MASK_FROM_ID(k_EButton_DPad_Down)) {
|
|
|
|
++numButtons;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<VRControllerOpenVR> openVRController =
|
|
|
|
new VRControllerOpenVR(hand, numButtons, numAxes);
|
2016-11-28 11:57:58 +03:00
|
|
|
openVRController->SetTrackedIndex(trackedDevice);
|
|
|
|
mOpenVRController.AppendElement(openVRController);
|
|
|
|
|
|
|
|
// Not already present, add it.
|
2017-02-06 11:12:52 +03:00
|
|
|
AddGamepad(openVRController->GetControllerInfo());
|
2016-11-28 11:57:58 +03:00
|
|
|
++mControllerCount;
|
|
|
|
}
|
2016-10-07 11:58:01 +03:00
|
|
|
}
|
2016-12-01 09:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-24 12:49:11 +03:00
|
|
|
VRSystemManagerOpenVR::RemoveControllers()
|
2016-12-01 09:16:16 +03:00
|
|
|
{
|
|
|
|
mOpenVRController.Clear();
|
|
|
|
mControllerCount = 0;
|
2017-01-24 12:49:11 +03:00
|
|
|
}
|