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
|
|
|
#ifndef mozilla_dom_gamepad_Gamepad_h
|
|
|
|
#define mozilla_dom_gamepad_Gamepad_h
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-10-17 12:07:19 -07:00
|
|
|
#include "mozilla/dom/GamepadButton.h"
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2013-04-09 05:43:25 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2012-02-15 16:47:13 -08:00
|
|
|
#include "nsIVariant.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
2013-04-09 05:43:25 -07:00
|
|
|
#include "nsWrapperCache.h"
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
enum GamepadMappingType
|
|
|
|
{
|
|
|
|
NoMapping = 0,
|
|
|
|
StandardMapping = 1
|
|
|
|
};
|
|
|
|
|
2013-10-17 12:07:16 -07:00
|
|
|
class Gamepad : public nsISupports,
|
|
|
|
public nsWrapperCache
|
2011-08-03 11:12:08 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-02-15 16:47:13 -08:00
|
|
|
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
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2012-02-15 16:47:13 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
void SetConnected(bool aConnected);
|
2013-04-09 05:43:25 -07:00
|
|
|
void SetButton(uint32_t aButton, bool aPressed, double aValue);
|
2011-08-03 11:12:08 -07:00
|
|
|
void SetAxis(uint32_t aAxis, double aValue);
|
|
|
|
void SetIndex(uint32_t aIndex);
|
|
|
|
|
|
|
|
// Make the state of this gamepad equivalent to other.
|
2012-02-15 16:47:13 -08:00
|
|
|
void SyncState(Gamepad* aOther);
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
// Return a new Gamepad containing the same data as this object,
|
2013-04-09 05:43:25 -07:00
|
|
|
// parented to aParent.
|
2012-02-15 16:47:13 -08:00
|
|
|
already_AddRefed<Gamepad> Clone(nsISupports* aParent);
|
2013-04-09 05:43:25 -07:00
|
|
|
|
|
|
|
nsISupports* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2013-04-25 09:29:54 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
2013-04-09 05:43:25 -07:00
|
|
|
|
|
|
|
void GetId(nsAString& aID) const
|
|
|
|
{
|
|
|
|
aID = mID;
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
void GetMapping(nsAString& aMapping) const
|
|
|
|
{
|
|
|
|
if (mMapping == StandardMapping) {
|
|
|
|
aMapping = NS_LITERAL_STRING("standard");
|
|
|
|
} else {
|
|
|
|
aMapping = NS_LITERAL_STRING("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
bool Connected() const
|
|
|
|
{
|
|
|
|
return mConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Index() const
|
|
|
|
{
|
|
|
|
return mIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIVariant> GetButtons(mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> buttons;
|
|
|
|
aRv = GetButtons(getter_AddRefs(buttons));
|
|
|
|
return buttons.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIVariant> GetAxes(mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> axes;
|
|
|
|
aRv = GetAxes(getter_AddRefs(axes));
|
|
|
|
return axes.forget();
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
|
|
|
|
private:
|
2012-02-15 16:47:13 -08:00
|
|
|
virtual ~Gamepad() {}
|
2011-08-03 11:12:08 -07:00
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
nsresult GetButtons(nsIVariant** aButtons);
|
|
|
|
nsresult GetAxes(nsIVariant** aAxes);
|
|
|
|
|
2011-08-03 11:12:08 -07:00
|
|
|
protected:
|
2013-04-09 05:43:25 -07:00
|
|
|
nsCOMPtr<nsISupports> mParent;
|
2011-08-03 11:12:08 -07:00
|
|
|
nsString mID;
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
2013-04-09 05:43:25 -07:00
|
|
|
// The mapping in use.
|
|
|
|
GamepadMappingType mMapping;
|
|
|
|
|
2011-08-03 11:12:08 -07:00
|
|
|
// true if this gamepad is currently connected.
|
|
|
|
bool mConnected;
|
|
|
|
|
|
|
|
// Current state of buttons, axes.
|
2013-10-17 12:07:19 -07:00
|
|
|
nsTArray<nsRefPtr<GamepadButton>> mButtons;
|
2011-08-03 11:12:08 -07:00
|
|
|
nsTArray<double> mAxes;
|
2013-10-17 12:07:19 -07:00
|
|
|
|
|
|
|
// Cached variant array.
|
|
|
|
nsCOMPtr<nsIVariant> mButtonsVariant;
|
2011-08-03 11:12:08 -07:00
|
|
|
};
|
|
|
|
|
2012-02-15 16:47:13 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_gamepad_Gamepad_h
|