tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GamepadHapticActuator.cpp (2829B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/GamepadHapticActuator.h"
      8 
      9 #include "mozilla/dom/GamepadManager.h"
     10 #include "mozilla/dom/Promise.h"
     11 
     12 namespace mozilla::dom {
     13 
     14 NS_IMPL_CYCLE_COLLECTING_ADDREF(GamepadHapticActuator)
     15 NS_IMPL_CYCLE_COLLECTING_RELEASE(GamepadHapticActuator)
     16 
     17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadHapticActuator)
     18  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     19  NS_INTERFACE_MAP_ENTRY(nsISupports)
     20 NS_INTERFACE_MAP_END
     21 
     22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator, mParent)
     23 
     24 GamepadHapticActuator::GamepadHapticActuator(nsISupports* aParent,
     25                                             GamepadHandle aGamepadHandle,
     26                                             uint32_t aIndex)
     27    : mParent(aParent),
     28      mGamepadHandle(aGamepadHandle),
     29      mType(GamepadHapticActuatorType::Vibration),
     30      mIndex(aIndex) {}
     31 
     32 /* virtual */
     33 JSObject* GamepadHapticActuator::WrapObject(JSContext* aCx,
     34                                            JS::Handle<JSObject*> aGivenProto) {
     35  return GamepadHapticActuator_Binding::Wrap(aCx, this, aGivenProto);
     36 }
     37 
     38 nsISupports* GamepadHapticActuator::GetParentObject() const { return mParent; }
     39 
     40 #define CLAMP(f, min, max) (((f) < min) ? min : (((f) > max) ? max : (f)))
     41 
     42 already_AddRefed<Promise> GamepadHapticActuator::Pulse(double aValue,
     43                                                       double aDuration,
     44                                                       ErrorResult& aRv) {
     45  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
     46  MOZ_ASSERT(global);
     47 
     48  RefPtr<GamepadManager> gamepadManager(GamepadManager::GetService());
     49  MOZ_ASSERT(gamepadManager);
     50 
     51  // Clamp intensity aValue to be 0~1.
     52  double value = CLAMP(aValue, 0, 1);
     53  // aDuration should be always positive.
     54  double duration = CLAMP(aDuration, 0, aDuration);
     55 
     56  switch (mType) {
     57    case GamepadHapticActuatorType::Vibration: {
     58      RefPtr<Promise> promise = gamepadManager->VibrateHaptic(
     59          mGamepadHandle, mIndex, value, duration, global, aRv);
     60      if (!promise) {
     61        return nullptr;
     62      }
     63      return promise.forget();
     64    }
     65    default: {
     66      // We need to implement other types of haptic
     67      MOZ_ASSERT(false);
     68      return nullptr;
     69    }
     70  }
     71 }
     72 
     73 GamepadHapticActuatorType GamepadHapticActuator::Type() const { return mType; }
     74 
     75 void GamepadHapticActuator::Set(const GamepadHapticActuator* aOther) {
     76  mGamepadHandle = aOther->mGamepadHandle;
     77  mType = aOther->mType;
     78  mIndex = aOther->mIndex;
     79 }
     80 
     81 }  // namespace mozilla::dom