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
|
|
|
|
|
2013-10-17 12:07:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(Gamepad, mParent, mButtonsVariant)
|
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-04-09 05:43:25 -07:00
|
|
|
SetIsDOMBinding();
|
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());
|
|
|
|
mAxes[aAxis] = aValue;
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
nsresult
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::GetButtons(nsIVariant** aButtons)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
2013-10-17 12:07:19 -07:00
|
|
|
if (mButtonsVariant) {
|
|
|
|
NS_ADDREF(*aButtons = mButtonsVariant);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-03 11:12:08 -07:00
|
|
|
nsRefPtr<nsVariant> out = new nsVariant();
|
|
|
|
NS_ENSURE_STATE(out);
|
|
|
|
|
|
|
|
if (mButtons.Length() == 0) {
|
|
|
|
nsresult rv = out->SetAsEmptyArray();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
// Note: The resulting nsIVariant dupes both the array and its elements.
|
2013-10-17 12:07:19 -07:00
|
|
|
GamepadButton** array = reinterpret_cast<GamepadButton**>
|
|
|
|
(NS_Alloc(mButtons.Length() * sizeof(GamepadButton*)));
|
2011-08-03 11:12:08 -07:00
|
|
|
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mButtons.Length(); ++i) {
|
2013-10-17 12:07:19 -07:00
|
|
|
array[i] = mButtons[i].get();
|
2011-08-03 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2013-10-17 12:07:19 -07:00
|
|
|
nsresult rv = out->SetAsArray(nsIDataType::VTYPE_INTERFACE,
|
2011-08-03 11:12:08 -07:00
|
|
|
nullptr,
|
|
|
|
mButtons.Length(),
|
|
|
|
reinterpret_cast<void*>(array));
|
|
|
|
NS_Free(array);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2013-10-17 12:07:19 -07:00
|
|
|
mButtonsVariant = out;
|
2011-08-03 11:12:08 -07:00
|
|
|
*aButtons = out.forget().get();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
nsresult
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::GetAxes(nsIVariant** aAxes)
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<nsVariant> out = new nsVariant();
|
|
|
|
NS_ENSURE_STATE(out);
|
|
|
|
|
|
|
|
if (mAxes.Length() == 0) {
|
|
|
|
nsresult rv = out->SetAsEmptyArray();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
// Note: The resulting nsIVariant dupes both the array and its elements.
|
|
|
|
double* array = reinterpret_cast<double*>
|
|
|
|
(NS_Alloc(mAxes.Length() * sizeof(double)));
|
|
|
|
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mAxes.Length(); ++i) {
|
|
|
|
array[i] = mAxes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = out->SetAsArray(nsIDataType::VTYPE_DOUBLE,
|
|
|
|
nullptr,
|
|
|
|
mAxes.Length(),
|
|
|
|
reinterpret_cast<void*>(array));
|
|
|
|
NS_Free(array);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aAxes = out.forget().get();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < mAxes.Length(); ++i) {
|
|
|
|
mAxes[i] = aOther->mAxes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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*
|
2012-02-15 16:47:13 -08:00
|
|
|
Gamepad::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-04-09 05:43:25 -07:00
|
|
|
{
|
|
|
|
return GamepadBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
2012-02-15 16:47:13 -08:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|