Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / gamepad / GamepadHapticActuator.cpp
blob462e386c8e5f30e4bcb91b74db4fc4e0d7bedac9
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)
19 NS_INTERFACE_MAP_END
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator, mParent)
23 GamepadHapticActuator::GamepadHapticActuator(nsISupports* aParent,
24 GamepadHandle aGamepadHandle,
25 uint32_t aIndex)
26 : mParent(aParent),
27 mGamepadHandle(aGamepadHandle),
28 mType(GamepadHapticActuatorType::Vibration),
29 mIndex(aIndex) {}
31 /* virtual */
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,
42 double aDuration,
43 ErrorResult& aRv) {
44 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
45 MOZ_ASSERT(global);
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);
55 switch (mType) {
56 case GamepadHapticActuatorType::Vibration: {
57 RefPtr<Promise> promise = gamepadManager->VibrateHaptic(
58 mGamepadHandle, mIndex, value, duration, global, aRv);
59 if (!promise) {
60 return nullptr;
62 return promise.forget();
64 default: {
65 // We need to implement other types of haptic
66 MOZ_ASSERT(false);
67 return nullptr;
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