Line data Source code
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 : #include "mozilla/dom/GamepadManager.h"
9 : #include "mozilla/dom/Promise.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(GamepadHapticActuator)
15 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(GamepadHapticActuator)
16 :
17 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GamepadHapticActuator)
18 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
19 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
20 0 : NS_INTERFACE_MAP_END
21 :
22 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator, mParent)
23 :
24 0 : GamepadHapticActuator::GamepadHapticActuator(nsISupports* aParent, uint32_t aGamepadId,
25 0 : uint32_t aIndex)
26 : : mParent(aParent), mGamepadId(aGamepadId),
27 0 : mType(GamepadHapticActuatorType::Vibration), mIndex(aIndex)
28 : {
29 :
30 0 : }
31 :
32 : /* virtual */ JSObject*
33 0 : GamepadHapticActuator::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
34 : {
35 0 : return GamepadHapticActuatorBinding::Wrap(aCx, this, aGivenProto);
36 : }
37 :
38 : nsISupports*
39 0 : GamepadHapticActuator::GetParentObject() const
40 : {
41 0 : return mParent;
42 : }
43 :
44 : #define CLAMP(f, min, max) \
45 : (((f) < min)? min : (((f) > max) ? max : (f)))
46 :
47 : already_AddRefed<Promise>
48 0 : GamepadHapticActuator::Pulse(double aValue, double aDuration, ErrorResult& aRv)
49 : {
50 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
51 0 : MOZ_ASSERT(global);
52 :
53 0 : RefPtr<GamepadManager> gamepadManager(GamepadManager::GetService());
54 0 : MOZ_ASSERT(gamepadManager);
55 :
56 : // Clamp intensity aValue to be 0~1.
57 0 : double value = CLAMP(aValue, 0, 1);
58 : // aDuration should be always positive.
59 0 : double duration = CLAMP(aDuration, 0, aDuration);
60 :
61 0 : switch (mType) {
62 : case GamepadHapticActuatorType::Vibration:
63 : {
64 : RefPtr<Promise> promise =
65 0 : gamepadManager->VibrateHaptic(
66 0 : mGamepadId, mIndex, value, duration, global, aRv);
67 0 : if (!promise) {
68 0 : return nullptr;
69 : }
70 0 : return promise.forget();
71 : }
72 : default:
73 : {
74 : // We need to implement other types of haptic
75 0 : MOZ_ASSERT(false);
76 : return nullptr;
77 : }
78 : }
79 : }
80 :
81 : GamepadHapticActuatorType
82 0 : GamepadHapticActuator::Type() const
83 : {
84 0 : return mType;
85 : }
86 :
87 : void
88 0 : GamepadHapticActuator::Set(const GamepadHapticActuator* aOther) {
89 0 : mGamepadId = aOther->mGamepadId;
90 0 : mType = aOther->mType;
91 0 : mIndex = aOther->mIndex;
92 0 : }
93 :
94 : } // namespace dom
95 : } // namespace mozilla
|