Bug 1320633 - Refresh for new controllers coming in gfxVROpenVR; r=kip
authorDaosheng Mu <daoshengmu@gmail.com>
Mon, 28 Nov 2016 16:57:58 +0800
changeset 324577 a662d5cccbe5e80d31a3538b71a5953999dcad50
parent 324576 1a5839f86e7936d208929eb2e929f99421ebc871
child 324578 0f7296d6e742645e856c00c94034fb3df59a9c3c
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerskip
bugs1320633
milestone53.0a1
Bug 1320633 - Refresh for new controllers coming in gfxVROpenVR; r=kip MozReview-Commit-ID: Lpvawepnk0u
gfx/vr/VRManager.cpp
gfx/vr/gfxVROpenVR.cpp
--- a/gfx/vr/VRManager.cpp
+++ b/gfx/vr/VRManager.cpp
@@ -334,16 +334,18 @@ VRManager::GetVRControllerInfo(nsTArray<
   }
 }
 
 void
 VRManager::RefreshVRControllers()
 {
   nsTArray<RefPtr<gfx::VRControllerHost>> controllers;
 
+  ScanForDevices();
+
   for (uint32_t i = 0; i < mControllerManagers.Length()
       && controllers.Length() == 0; ++i) {
     mControllerManagers[i]->GetControllers(controllers);
   }
 
   bool controllerInfoChanged = false;
 
   if (controllers.Length() != mVRControllers.Count()) {
--- a/gfx/vr/gfxVROpenVR.cpp
+++ b/gfx/vr/gfxVROpenVR.cpp
@@ -708,43 +708,56 @@ VRControllerManagerOpenVR::GetController
   for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
     aControllerResult.AppendElement(mOpenVRController[i]);
   }
 }
 
 void
 VRControllerManagerOpenVR::ScanForDevices()
 {
-  // Remove the existing gamepads
-  for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
-    RemoveGamepad(mOpenVRController[i]->GetIndex());
-  }
-  mControllerCount = 0;
-  mOpenVRController.Clear();
-
   if (!mVRSystem)
     return;
 
-  // 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 ) {
+  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) {
+
     if (!mVRSystem->IsTrackedDeviceConnected(trackedDevice)) {
       continue;
     }
+    if (mVRSystem->GetTrackedDeviceClass(trackedDevice)
+        != vr::TrackedDeviceClass_Controller) {
+      continue;
+    }
 
-    if (mVRSystem->GetTrackedDeviceClass(trackedDevice) != vr::TrackedDeviceClass_Controller) {
-      continue;
+    trackedIndexArray[newControllerCount] = trackedDevice;
+    ++newControllerCount;
+  }
+
+  if (newControllerCount != mControllerCount) {
+    // controller count is changed, removing the existing gamepads first.
+    for (uint32_t i = 0; i < mOpenVRController.Length(); ++i) {
+      RemoveGamepad(mOpenVRController[i]->GetIndex());
     }
+    mControllerCount = 0;
+    mOpenVRController.Clear();
 
-    RefPtr<VRControllerOpenVR> openVRController = new VRControllerOpenVR();
-    openVRController->SetIndex(mControllerCount);
-    openVRController->SetTrackedIndex(trackedDevice);
-    mOpenVRController.AppendElement(openVRController);
+    // Re-adding controllers to VRControllerManager.
+    for (vr::TrackedDeviceIndex_t i = 0; i < newControllerCount; ++i) {
+      vr::TrackedDeviceIndex_t trackedDevice = trackedIndexArray[i];
+      RefPtr<VRControllerOpenVR> openVRController = new VRControllerOpenVR();
+      openVRController->SetIndex(mControllerCount);
+      openVRController->SetTrackedIndex(trackedDevice);
+      mOpenVRController.AppendElement(openVRController);
 
-// Only in MOZ_GAMEPAD platform, We add gamepads.
-#ifdef MOZ_GAMEPAD
-    // Not already present, add it.
-    AddGamepad("OpenVR Gamepad", static_cast<uint32_t>(GamepadMappingType::_empty),
-               gNumOpenVRButtonMask, gNumOpenVRAxis);
-    ++mControllerCount;
-#endif
+  // Only in MOZ_GAMEPAD platform, We add gamepads.
+  #ifdef MOZ_GAMEPAD
+      // Not already present, add it.
+      AddGamepad("OpenVR Gamepad", static_cast<uint32_t>(GamepadMappingType::_empty),
+                 gNumOpenVRButtonMask, gNumOpenVRAxis);
+      ++mControllerCount;
+  #endif
+    }
   }
 }
\ No newline at end of file