2011-08-03 11:12:08 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
#include "Gamepad.h"
|
2013-04-09 05:43:25 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsVariant.h"
|
2013-04-09 05:43:25 -07:00
|
|
|
#include "mozilla/dom/GamepadBinding.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad)
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad)
|
2013-04-09 05:43:25 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2011-08-03 11:12:08 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Gamepad, mParent, mButtons)
|
2013-04-09 05:43:25 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::Gamepad(nsISupports* aParent,
|
|
|
|
const nsAString& aID, uint32_t aIndex,
|
2013-04-09 05:43:25 -07:00
|
|
|
GamepadMappingType aMapping,
|
2012-02-15 16:47:13 -08:00
|
|
|
uint32_t aNumButtons, uint32_t aNumAxes)
|
2013-04-09 05:43:25 -07:00
|
|
|
: mParent(aParent),
|
|
|
|
mID(aID),
|
2011-08-03 11:12:08 -07:00
|
|
|
mIndex(aIndex),
|
2013-04-09 05:43:25 -07:00
|
|
|
mMapping(aMapping),
|
2013-10-17 12:07:19 -07:00
|
|
|
mConnected(true),
|
|
|
|
mButtons(aNumButtons),
|
|
|
|
mAxes(aNumAxes)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
2013-10-17 12:07:19 -07:00
|
|
|
for (unsigned i = 0; i < aNumButtons; i++) {
|
|
|
|
mButtons.InsertElementAt(i, new GamepadButton(mParent));
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
mAxes.InsertElementsAt(0, aNumAxes, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::SetIndex(uint32_t aIndex)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
mIndex = aIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::SetConnected(bool aConnected)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
mConnected = aConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-09 05:43:25 -07:00
|
|
|
Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aButton < mButtons.Length());
|
2013-10-17 12:07:19 -07:00
|
|
|
mButtons[aButton]->SetPressed(aPressed);
|
|
|
|
mButtons[aButton]->SetValue(aValue);
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::SetAxis(uint32_t aAxis, double aValue)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aAxis < mAxes.Length());
|
2013-12-06 08:41:13 -08:00
|
|
|
if (mAxes[aAxis] != aValue) {
|
|
|
|
mAxes[aAxis] = aValue;
|
|
|
|
GamepadBinding::ClearCachedAxesValue(this);
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::SyncState(Gamepad* aOther)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
if (mButtons.Length() != aOther->mButtons.Length() ||
|
|
|
|
mAxes.Length() != aOther->mAxes.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mConnected = aOther->mConnected;
|
|
|
|
for (uint32_t i = 0; i < mButtons.Length(); ++i) {
|
2013-10-17 12:07:19 -07:00
|
|
|
mButtons[i]->SetPressed(aOther->mButtons[i]->Pressed());
|
|
|
|
mButtons[i]->SetValue(aOther->mButtons[i]->Value());
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
2013-12-06 08:41:13 -08:00
|
|
|
bool changed = false;
|
2011-08-03 11:12:08 -07:00
|
|
|
for (uint32_t i = 0; i < mAxes.Length(); ++i) {
|
2013-12-06 08:41:13 -08:00
|
|
|
changed = changed || (mAxes[i] != aOther->mAxes[i]);
|
2011-08-03 11:12:08 -07:00
|
|
|
mAxes[i] = aOther->mAxes[i];
|
|
|
|
}
|
2013-12-06 08:41:13 -08:00
|
|
|
if (changed) {
|
|
|
|
GamepadBinding::ClearCachedAxesValue(this);
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
already_AddRefed<Gamepad>
|
|
|
|
Gamepad::Clone(nsISupports* aParent)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
2012-02-15 16:47:13 -08:00
|
|
|
nsRefPtr<Gamepad> out =
|
2013-04-09 05:43:25 -07:00
|
|
|
new Gamepad(aParent, mID, mIndex, mMapping,
|
|
|
|
mButtons.Length(), mAxes.Length());
|
2011-08-03 11:12:08 -07:00
|
|
|
out->SyncState(this);
|
|
|
|
return out.forget();
|
|
|
|
}
|
2013-04-09 05:43:25 -07:00
|
|
|
|
|
|
|
/* virtual */ JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
Gamepad::WrapObject(JSContext* aCx)
|
2013-04-09 05:43:25 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return GamepadBinding::Wrap(aCx, this);
|
2013-04-09 05:43:25 -07:00
|
|
|
}
|
2012-02-15 16:47:13 -08:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|