/src/mozilla-central/dom/gamepad/GamepadHapticActuator.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(GamepadHapticActuator) |
15 | | 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 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GamepadHapticActuator, mParent) |
23 | | |
24 | | GamepadHapticActuator::GamepadHapticActuator(nsISupports* aParent, uint32_t aGamepadId, |
25 | | uint32_t aIndex) |
26 | | : mParent(aParent), mGamepadId(aGamepadId), |
27 | | mType(GamepadHapticActuatorType::Vibration), mIndex(aIndex) |
28 | 0 | { |
29 | 0 |
|
30 | 0 | } |
31 | | |
32 | | /* virtual */ JSObject* |
33 | | GamepadHapticActuator::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
34 | 0 | { |
35 | 0 | return GamepadHapticActuator_Binding::Wrap(aCx, this, aGivenProto); |
36 | 0 | } |
37 | | |
38 | | nsISupports* |
39 | | GamepadHapticActuator::GetParentObject() const |
40 | 0 | { |
41 | 0 | return mParent; |
42 | 0 | } |
43 | | |
44 | | #define CLAMP(f, min, max) \ |
45 | 0 | (((f) < min)? min : (((f) > max) ? max : (f))) |
46 | | |
47 | | already_AddRefed<Promise> |
48 | | GamepadHapticActuator::Pulse(double aValue, double aDuration, ErrorResult& aRv) |
49 | 0 | { |
50 | 0 | nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject()); |
51 | 0 | MOZ_ASSERT(global); |
52 | 0 |
|
53 | 0 | RefPtr<GamepadManager> gamepadManager(GamepadManager::GetService()); |
54 | 0 | MOZ_ASSERT(gamepadManager); |
55 | 0 |
|
56 | 0 | // Clamp intensity aValue to be 0~1. |
57 | 0 | double value = CLAMP(aValue, 0, 1); |
58 | 0 | // aDuration should be always positive. |
59 | 0 | double duration = CLAMP(aDuration, 0, aDuration); |
60 | 0 |
|
61 | 0 | switch (mType) { |
62 | 0 | case GamepadHapticActuatorType::Vibration: |
63 | 0 | { |
64 | 0 | RefPtr<Promise> promise = |
65 | 0 | gamepadManager->VibrateHaptic( |
66 | 0 | mGamepadId, mIndex, value, duration, global, aRv); |
67 | 0 | if (!promise) { |
68 | 0 | return nullptr; |
69 | 0 | } |
70 | 0 | return promise.forget(); |
71 | 0 | } |
72 | 0 | default: |
73 | 0 | { |
74 | 0 | // We need to implement other types of haptic |
75 | 0 | MOZ_ASSERT(false); |
76 | 0 | return nullptr; |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | GamepadHapticActuatorType |
82 | | GamepadHapticActuator::Type() const |
83 | 0 | { |
84 | 0 | return mType; |
85 | 0 | } |
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 |