2011-08-03 22:12:08 +04: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-16 04:47:13 +04:00
|
|
|
#ifndef mozilla_dom_gamepad_Gamepad_h
|
|
|
|
#define mozilla_dom_gamepad_Gamepad_h
|
2011-08-03 22:12:08 +04:00
|
|
|
|
2013-04-09 16:43:25 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-05-23 15:39:13 +04:00
|
|
|
#include "mozilla/dom/GamepadBinding.h"
|
2013-10-17 23:07:19 +04:00
|
|
|
#include "mozilla/dom/GamepadButton.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2013-04-09 16:43:25 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2011-08-03 22:12:08 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
2013-04-09 16:43:25 +04:00
|
|
|
#include "nsWrapperCache.h"
|
2015-01-07 22:17:04 +03:00
|
|
|
#include "nsPerformance.h"
|
2011-08-03 22:12:08 +04:00
|
|
|
|
2012-02-16 04:47:13 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-17 03:43:39 +04:00
|
|
|
// Per spec:
|
|
|
|
// https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
|
|
|
|
const int kStandardGamepadButtons = 17;
|
|
|
|
const int kStandardGamepadAxes = 4;
|
|
|
|
|
|
|
|
const int kButtonLeftTrigger = 6;
|
|
|
|
const int kButtonRightTrigger = 7;
|
|
|
|
|
|
|
|
const int kLeftStickXAxis = 0;
|
|
|
|
const int kLeftStickYAxis = 1;
|
|
|
|
const int kRightStickXAxis = 2;
|
|
|
|
const int kRightStickYAxis = 3;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Gamepad final : public nsISupports,
|
2014-11-25 21:56:07 +03:00
|
|
|
public nsWrapperCache
|
2011-08-03 22:12:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-02-16 04:47:13 +04:00
|
|
|
Gamepad(nsISupports* aParent,
|
|
|
|
const nsAString& aID, uint32_t aIndex,
|
2013-04-09 16:43:25 +04:00
|
|
|
GamepadMappingType aMapping,
|
2012-02-16 04:47:13 +04:00
|
|
|
uint32_t aNumButtons, uint32_t aNumAxes);
|
2013-04-09 16:43:25 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2012-02-16 04:47:13 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
void SetConnected(bool aConnected);
|
2013-04-09 16:43:25 +04:00
|
|
|
void SetButton(uint32_t aButton, bool aPressed, double aValue);
|
2011-08-03 22:12:08 +04:00
|
|
|
void SetAxis(uint32_t aAxis, double aValue);
|
|
|
|
void SetIndex(uint32_t aIndex);
|
|
|
|
|
|
|
|
// Make the state of this gamepad equivalent to other.
|
2012-02-16 04:47:13 +04:00
|
|
|
void SyncState(Gamepad* aOther);
|
2011-08-03 22:12:08 +04:00
|
|
|
|
2012-02-16 04:47:13 +04:00
|
|
|
// Return a new Gamepad containing the same data as this object,
|
2013-04-09 16:43:25 +04:00
|
|
|
// parented to aParent.
|
2012-02-16 04:47:13 +04:00
|
|
|
already_AddRefed<Gamepad> Clone(nsISupports* aParent);
|
2013-04-09 16:43:25 +04:00
|
|
|
|
|
|
|
nsISupports* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-09 16:43:25 +04:00
|
|
|
|
|
|
|
void GetId(nsAString& aID) const
|
|
|
|
{
|
|
|
|
aID = mID;
|
|
|
|
}
|
|
|
|
|
2015-01-07 22:17:04 +03:00
|
|
|
DOMHighResTimeStamp Timestamp() const
|
|
|
|
{
|
|
|
|
return mTimestamp;
|
|
|
|
}
|
|
|
|
|
2014-05-23 15:39:13 +04:00
|
|
|
GamepadMappingType Mapping()
|
2013-04-09 16:43:25 +04:00
|
|
|
{
|
2014-05-23 15:39:13 +04:00
|
|
|
return mMapping;
|
2013-04-09 16:43:25 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 16:43:25 +04:00
|
|
|
bool Connected() const
|
|
|
|
{
|
|
|
|
return mConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Index() const
|
|
|
|
{
|
|
|
|
return mIndex;
|
|
|
|
}
|
|
|
|
|
2013-12-06 20:41:13 +04:00
|
|
|
void GetButtons(nsTArray<nsRefPtr<GamepadButton>>& aButtons) const
|
2013-04-09 16:43:25 +04:00
|
|
|
{
|
2013-12-06 20:41:13 +04:00
|
|
|
aButtons = mButtons;
|
2013-04-09 16:43:25 +04:00
|
|
|
}
|
|
|
|
|
2013-12-06 20:41:13 +04:00
|
|
|
void GetAxes(nsTArray<double>& aAxes) const
|
2013-04-09 16:43:25 +04:00
|
|
|
{
|
2013-12-06 20:41:13 +04:00
|
|
|
aAxes = mAxes;
|
2013-04-09 16:43:25 +04:00
|
|
|
}
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
private:
|
2012-02-16 04:47:13 +04:00
|
|
|
virtual ~Gamepad() {}
|
2015-01-07 22:17:04 +03:00
|
|
|
void UpdateTimestamp();
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
protected:
|
2013-04-09 16:43:25 +04:00
|
|
|
nsCOMPtr<nsISupports> mParent;
|
2011-08-03 22:12:08 +04:00
|
|
|
nsString mID;
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
2013-04-09 16:43:25 +04:00
|
|
|
// The mapping in use.
|
|
|
|
GamepadMappingType mMapping;
|
|
|
|
|
2011-08-03 22:12:08 +04:00
|
|
|
// true if this gamepad is currently connected.
|
|
|
|
bool mConnected;
|
|
|
|
|
|
|
|
// Current state of buttons, axes.
|
2013-10-17 23:07:19 +04:00
|
|
|
nsTArray<nsRefPtr<GamepadButton>> mButtons;
|
2011-08-03 22:12:08 +04:00
|
|
|
nsTArray<double> mAxes;
|
2015-01-07 22:17:04 +03:00
|
|
|
DOMHighResTimeStamp mTimestamp;
|
2011-08-03 22:12:08 +04:00
|
|
|
};
|
|
|
|
|
2012-02-16 04:47:13 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_gamepad_Gamepad_h
|