Bug 1352410 - Add a preference to enable or disable haptic feedback for gamepads; r?lenzak draft
authorDaosheng Mu <daoshengmu@gmail.com>
Thu, 27 Jul 2017 17:55:16 +0800
changeset 616661 e77019903ea17f349dacc411f02e7d599c13e954
parent 616205 f1693d664f8e8ee4c79801630c181c28095cad56
child 639548 5c84ff66557868082e043254dd2e8e21c94009c6
push id70764
push userbmo:dmu@mozilla.com
push dateThu, 27 Jul 2017 09:55:48 +0000
reviewerslenzak
bugs1352410
milestone56.0a1
Bug 1352410 - Add a preference to enable or disable haptic feedback for gamepads; r?lenzak MozReview-Commit-ID: 70FFdcd3n5q
dom/gamepad/GamepadManager.cpp
modules/libpref/init/all.js
--- a/dom/gamepad/GamepadManager.cpp
+++ b/dom/gamepad/GamepadManager.cpp
@@ -636,47 +636,54 @@ GamepadManager::SetGamepadByEvent(const 
   return ret;
 }
 
 already_AddRefed<Promise>
 GamepadManager::VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
                               double aIntensity, double aDuration,
                               nsIGlobalObject* aGlobal, ErrorResult& aRv)
 {
+  const char* kGamepadHapticEnabledPref = "dom.gamepad.haptic_feedback.enabled";
   RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     aRv.Throw(NS_ERROR_FAILURE);
     return nullptr;
   }
-
-  if (aControllerIdx >= VR_GAMEPAD_IDX_OFFSET) {
-    if (gfx::VRManagerChild::IsCreated()) {
-      const uint32_t index = aControllerIdx - VR_GAMEPAD_IDX_OFFSET;
-      gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
-      vm->AddPromise(mPromiseID, promise);
-      vm->SendVibrateHaptic(index, aHapticIndex,
-                            aIntensity, aDuration,
-                            mPromiseID);
-    }
-  } else {
-    for (const auto& channelChild: mChannelChildren) {
-      channelChild->AddPromise(mPromiseID, promise);
-      channelChild->SendVibrateHaptic(aControllerIdx, aHapticIndex,
-                                      aIntensity, aDuration,
-                                      mPromiseID);
+  if (Preferences::GetBool(kGamepadHapticEnabledPref)) {
+    if (aControllerIdx >= VR_GAMEPAD_IDX_OFFSET) {
+      if (gfx::VRManagerChild::IsCreated()) {
+        const uint32_t index = aControllerIdx - VR_GAMEPAD_IDX_OFFSET;
+        gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
+        vm->AddPromise(mPromiseID, promise);
+        vm->SendVibrateHaptic(index, aHapticIndex,
+                              aIntensity, aDuration,
+                              mPromiseID);
+      }
+    } else {
+      for (const auto& channelChild: mChannelChildren) {
+        channelChild->AddPromise(mPromiseID, promise);
+        channelChild->SendVibrateHaptic(aControllerIdx, aHapticIndex,
+                                        aIntensity, aDuration,
+                                        mPromiseID);
+      }
     }
   }
 
   ++mPromiseID;
   return promise.forget();
 }
 
 void
 GamepadManager::StopHaptics()
 {
+  const char* kGamepadHapticEnabledPref = "dom.gamepad.haptic_feedback.enabled";
+  if (!Preferences::GetBool(kGamepadHapticEnabledPref)) {
+    return;
+  }
+
   for (auto iter = mGamepads.Iter(); !iter.Done(); iter.Next()) {
     const uint32_t gamepadIndex = iter.UserData()->HashKey();
     if (gamepadIndex >= VR_GAMEPAD_IDX_OFFSET) {
       if (gfx::VRManagerChild::IsCreated()) {
         const uint32_t index = gamepadIndex - VR_GAMEPAD_IDX_OFFSET;
         gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
         vm->SendStopVibrateHaptic(index);
       }
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -197,16 +197,17 @@ pref("dom.requestIdleCallback.enabled", 
 pref("dom.gamepad.enabled", true);
 pref("dom.gamepad.test.enabled", false);
 #ifdef RELEASE_OR_BETA
 pref("dom.gamepad.non_standard_events.enabled", false);
 #else
 pref("dom.gamepad.non_standard_events.enabled", true);
 #endif
 pref("dom.gamepad.extensions.enabled", true);
+pref("dom.gamepad.haptic_feedback.enabled", true);
 
 // If this is true, TextEventDispatcher dispatches keydown and keyup events
 // even during composition (keypress events are never fired during composition
 // even if this is true).
 pref("dom.keyboardevent.dispatch_during_composition", false);
 
 // Whether to run add-on code in different compartments from browser code. This
 // causes a separate compartment for each (addon, global) combination, which may