Bug 1386664 - Add Knuckles string id for SteamVR Knuckles controllers; r?kip draft
authorDaosheng Mu <daoshengmu@gmail.com>
Fri, 04 Aug 2017 14:52:01 +0800
changeset 620999 4a53db299645cbf9ca6e570aaa0cec6aa5de46ae
parent 620862 32083f24a1bb2c33050b4c972783f066432194eb
child 640865 f8475554b8f234b745c92b5999cc98fb2e6fba35
push id72219
push userbmo:dmu@mozilla.com
push dateFri, 04 Aug 2017 06:54:43 +0000
reviewerskip
bugs1386664
milestone57.0a1
Bug 1386664 - Add Knuckles string id for SteamVR Knuckles controllers; r?kip MozReview-Commit-ID: H8fX21j0u6F
gfx/vr/gfxVROpenVR.cpp
gfx/vr/gfxVROpenVR.h
--- a/gfx/vr/gfxVROpenVR.cpp
+++ b/gfx/vr/gfxVROpenVR.cpp
@@ -373,39 +373,28 @@ VRDisplayOpenVR::NotifyVSync()
   // Make sure we respond to OpenVR events even when not presenting
   PollEvents();
 
   VRDisplayHost::NotifyVSync();
 }
 
 VRControllerOpenVR::VRControllerOpenVR(dom::GamepadHand aHand, uint32_t aDisplayID,
                                        uint32_t aNumButtons, uint32_t aNumTriggers,
-                                       uint32_t aNumAxes, ::vr::ETrackedDeviceClass aDeviceType)
+                                       uint32_t aNumAxes, const nsCString& aId)
   : VRControllerHost(VRDeviceType::OpenVR, aHand, aDisplayID)
   , mTrigger(aNumTriggers)
   , mAxisMove(aNumAxes)
   , mVibrateThread(nullptr)
   , mIsVibrateStopped(false)
 {
   MOZ_COUNT_CTOR_INHERITED(VRControllerOpenVR, VRControllerHost);
 
-  switch (aDeviceType) {
-    case ::vr::TrackedDeviceClass_Controller:
-      mControllerInfo.mControllerName.AssignLiteral("OpenVR Gamepad");
-      break;
-    case ::vr::TrackedDeviceClass_GenericTracker:
-      mControllerInfo.mControllerName.AssignLiteral("OpenVR Tracker");
-      break;
-    default:
-      MOZ_ASSERT(false);
-      break;
-  }
-
   mAxisMove.SetLengthAndRetainStorage(aNumAxes);
   mTrigger.SetLengthAndRetainStorage(aNumTriggers);
+  mControllerInfo.mControllerName = aId;
   mControllerInfo.mNumButtons = aNumButtons;
   mControllerInfo.mNumAxes = aNumAxes;
   mControllerInfo.mNumHaptics = kNumOpenVRHaptcs;
 }
 
 VRControllerOpenVR::~VRControllerOpenVR()
 {
   if (mVibrateThread) {
@@ -1074,19 +1063,21 @@ VRSystemManagerOpenVR::ScanForController
           BTN_MASK_FROM_ID(k_EButton_DPad_Right)) {
         ++numButtons;
       }
       if (supportButtons &
           BTN_MASK_FROM_ID(k_EButton_DPad_Down)) {
         ++numButtons;
       }
 
+      nsCString deviceId;
+      GetControllerDeviceId(deviceType, trackedDevice, deviceId);
       RefPtr<VRControllerOpenVR> openVRController =
         new VRControllerOpenVR(hand, mOpenVRHMD->GetDisplayInfo().GetDisplayID(),
-                               numButtons, numTriggers, numAxes, deviceType);
+                               numButtons, numTriggers, numAxes, deviceId);
       openVRController->SetTrackedIndex(trackedDevice);
       mOpenVRController.AppendElement(openVRController);
 
       // Not already present, add it.
       AddGamepad(openVRController->GetControllerInfo());
       ++mControllerCount;
     }
   }
@@ -1098,8 +1089,42 @@ VRSystemManagerOpenVR::RemoveControllers
   // The controller count is changed, removing the existing gamepads first.
   for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
     RemoveGamepad(i);
   }
   mOpenVRController.Clear();
   mControllerCount = 0;
 }
 
+void
+VRSystemManagerOpenVR::GetControllerDeviceId(::vr::ETrackedDeviceClass aDeviceType,
+                                             ::vr::TrackedDeviceIndex_t aDeviceIndex, nsCString& aId)
+{
+  switch (aDeviceType) {
+    case ::vr::TrackedDeviceClass_Controller:
+    {
+      ::vr::ETrackedPropertyError err;
+      uint32_t requiredBufferLen;
+      char charBuf[128];
+      requiredBufferLen = mVRSystem->GetStringTrackedDeviceProperty(aDeviceIndex,
+                          ::vr::Prop_RenderModelName_String, charBuf, 128, &err);
+      if (requiredBufferLen > 128) {
+        MOZ_CRASH("Larger than the buffer size.");
+      }
+      MOZ_ASSERT(requiredBufferLen && err == ::vr::TrackedProp_Success);
+      nsCString deviceId(charBuf);
+      if (deviceId.Find("knuckles") != kNotFound) {
+        aId.AssignLiteral("OpenVR Knuckles");
+      } else {
+        aId.AssignLiteral("OpenVR Gamepad");
+      }
+      break;
+    }
+    case ::vr::TrackedDeviceClass_GenericTracker:
+    {
+      aId.AssignLiteral("OpenVR Tracker");
+      break;
+    }
+    default:
+      MOZ_ASSERT(false);
+      break;
+  }
+}
\ No newline at end of file
--- a/gfx/vr/gfxVROpenVR.h
+++ b/gfx/vr/gfxVROpenVR.h
@@ -74,17 +74,17 @@ protected:
                    const gfx::Rect& aRightEyeRect);
 };
 
 class VRControllerOpenVR : public VRControllerHost
 {
 public:
   explicit VRControllerOpenVR(dom::GamepadHand aHand, uint32_t aDisplayID, uint32_t aNumButtons,
                               uint32_t aNumTriggers, uint32_t aNumAxes,
-                              ::vr::ETrackedDeviceClass aDeviceType);
+                              const nsCString& aId);
   void SetTrackedIndex(uint32_t aTrackedIndex);
   uint32_t GetTrackedIndex();
   float GetAxisMove(uint32_t aAxis);
   void SetAxisMove(uint32_t aAxis, float aValue);
   void SetTrigger(uint32_t aButton, float aValue);
   float GetTrigger(uint32_t aButton);
   void SetHand(dom::GamepadHand aHand);
   void VibrateHaptic(::vr::IVRSystem* aVRSystem,
@@ -152,16 +152,19 @@ private:
                           float aValue);
   void HandleAxisMove(uint32_t aControllerIdx, uint32_t aAxis,
                       float aValue);
   void HandlePoseTracking(uint32_t aControllerIdx,
                           const dom::GamepadPoseState& aPose,
                           VRControllerHost* aController);
   dom::GamepadHand GetGamepadHandFromControllerRole(
                           ::vr::ETrackedControllerRole aRole);
+  void GetControllerDeviceId(::vr::ETrackedDeviceClass aDeviceType,
+                             ::vr::TrackedDeviceIndex_t aDeviceIndex,
+                             nsCString& aId);
 
   // there can only be one
   RefPtr<impl::VRDisplayOpenVR> mOpenVRHMD;
   nsTArray<RefPtr<impl::VRControllerOpenVR>> mOpenVRController;
   ::vr::IVRSystem *mVRSystem;
 };
 
 } // namespace gfx