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/. */
7 #include "mozilla/dom/GamepadHapticActuator.h"
8 #include "mozilla/dom/GamepadManager.h"
9 #include "mozilla/dom/Promise.h"
11 namespace mozilla::dom
{
13 NS_IMPL_CYCLE_COLLECTING_ADDREF(GamepadHapticActuator
)
14 NS_IMPL_CYCLE_COLLECTING_RELEASE(GamepadHapticActuator
)
16 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadHapticActuator
)
17 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
18 NS_INTERFACE_MAP_ENTRY(nsISupports
)
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator
, mParent
)
23 GamepadHapticActuator::GamepadHapticActuator(nsISupports
* aParent
,
24 GamepadHandle aGamepadHandle
,
27 mGamepadHandle(aGamepadHandle
),
28 mType(GamepadHapticActuatorType::Vibration
),
32 JSObject
* GamepadHapticActuator::WrapObject(JSContext
* aCx
,
33 JS::Handle
<JSObject
*> aGivenProto
) {
34 return GamepadHapticActuator_Binding::Wrap(aCx
, this, aGivenProto
);
37 nsISupports
* GamepadHapticActuator::GetParentObject() const { return mParent
; }
39 #define CLAMP(f, min, max) (((f) < min) ? min : (((f) > max) ? max : (f)))
41 already_AddRefed
<Promise
> GamepadHapticActuator::Pulse(double aValue
,
44 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
47 RefPtr
<GamepadManager
> gamepadManager(GamepadManager::GetService());
48 MOZ_ASSERT(gamepadManager
);
50 // Clamp intensity aValue to be 0~1.
51 double value
= CLAMP(aValue
, 0, 1);
52 // aDuration should be always positive.
53 double duration
= CLAMP(aDuration
, 0, aDuration
);
56 case GamepadHapticActuatorType::Vibration
: {
57 RefPtr
<Promise
> promise
= gamepadManager
->VibrateHaptic(
58 mGamepadHandle
, mIndex
, value
, duration
, global
, aRv
);
62 return promise
.forget();
65 // We need to implement other types of haptic
72 GamepadHapticActuatorType
GamepadHapticActuator::Type() const { return mType
; }
74 void GamepadHapticActuator::Set(const GamepadHapticActuator
* aOther
) {
75 mGamepadHandle
= aOther
->mGamepadHandle
;
76 mType
= aOther
->mType
;
77 mIndex
= aOther
->mIndex
;
80 } // namespace mozilla::dom