Bumping manifests a=b2g-bump
[gecko.git] / dom / gamepad / Gamepad.cpp
blobf2cff65d2ea7a3c9d784433a7fb7b8cab13629e7
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "Gamepad.h"
6 #include "nsAutoPtr.h"
7 #include "nsTArray.h"
8 #include "nsVariant.h"
9 #include "mozilla/dom/GamepadBinding.h"
11 namespace mozilla {
12 namespace dom {
14 NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad)
15 NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad)
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad)
18 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
19 NS_INTERFACE_MAP_ENTRY(nsISupports)
20 NS_INTERFACE_MAP_END
22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Gamepad, mParent, mButtons)
24 Gamepad::Gamepad(nsISupports* aParent,
25 const nsAString& aID, uint32_t aIndex,
26 GamepadMappingType aMapping,
27 uint32_t aNumButtons, uint32_t aNumAxes)
28 : mParent(aParent),
29 mID(aID),
30 mIndex(aIndex),
31 mMapping(aMapping),
32 mConnected(true),
33 mButtons(aNumButtons),
34 mAxes(aNumAxes)
36 SetIsDOMBinding();
37 for (unsigned i = 0; i < aNumButtons; i++) {
38 mButtons.InsertElementAt(i, new GamepadButton(mParent));
40 mAxes.InsertElementsAt(0, aNumAxes, 0.0f);
43 void
44 Gamepad::SetIndex(uint32_t aIndex)
46 mIndex = aIndex;
49 void
50 Gamepad::SetConnected(bool aConnected)
52 mConnected = aConnected;
55 void
56 Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue)
58 MOZ_ASSERT(aButton < mButtons.Length());
59 mButtons[aButton]->SetPressed(aPressed);
60 mButtons[aButton]->SetValue(aValue);
63 void
64 Gamepad::SetAxis(uint32_t aAxis, double aValue)
66 MOZ_ASSERT(aAxis < mAxes.Length());
67 if (mAxes[aAxis] != aValue) {
68 mAxes[aAxis] = aValue;
69 GamepadBinding::ClearCachedAxesValue(this);
73 void
74 Gamepad::SyncState(Gamepad* aOther)
76 if (mButtons.Length() != aOther->mButtons.Length() ||
77 mAxes.Length() != aOther->mAxes.Length()) {
78 return;
81 mConnected = aOther->mConnected;
82 for (uint32_t i = 0; i < mButtons.Length(); ++i) {
83 mButtons[i]->SetPressed(aOther->mButtons[i]->Pressed());
84 mButtons[i]->SetValue(aOther->mButtons[i]->Value());
86 bool changed = false;
87 for (uint32_t i = 0; i < mAxes.Length(); ++i) {
88 changed = changed || (mAxes[i] != aOther->mAxes[i]);
89 mAxes[i] = aOther->mAxes[i];
91 if (changed) {
92 GamepadBinding::ClearCachedAxesValue(this);
96 already_AddRefed<Gamepad>
97 Gamepad::Clone(nsISupports* aParent)
99 nsRefPtr<Gamepad> out =
100 new Gamepad(aParent, mID, mIndex, mMapping,
101 mButtons.Length(), mAxes.Length());
102 out->SyncState(this);
103 return out.forget();
106 /* virtual */ JSObject*
107 Gamepad::WrapObject(JSContext* aCx)
109 return GamepadBinding::Wrap(aCx, this);
112 } // namespace dom
113 } // namespace mozilla