2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2016-10-24 13:04:41 +03:00
|
|
|
#include "mozilla/dom/GamepadPose.h"
|
2017-02-02 09:30:58 +03:00
|
|
|
#include "mozilla/dom/GamepadHapticActuator.h"
|
2019-05-30 01:44:24 +03:00
|
|
|
#include "mozilla/dom/GamepadLightIndicator.h"
|
|
|
|
#include "mozilla/dom/GamepadTouch.h"
|
2016-06-09 20:04:42 +03:00
|
|
|
#include "mozilla/dom/Performance.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"
|
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, public nsWrapperCache {
|
2011-08-03 22:12:08 +04:00
|
|
|
public:
|
2020-04-07 12:13:50 +03:00
|
|
|
Gamepad(nsISupports* aParent, const nsAString& aID, int32_t aIndex,
|
2016-11-14 12:28:48 +03:00
|
|
|
uint32_t aHashKey, GamepadMappingType aMapping, GamepadHand aHand,
|
2017-07-14 11:53:03 +03:00
|
|
|
uint32_t aDisplayID, uint32_t aNumButtons, uint32_t aNumAxes,
|
2019-05-30 01:44:24 +03:00
|
|
|
uint32_t aNumHaptics, uint32_t aNumLightIndicator,
|
|
|
|
uint32_t aNumTouchEvents);
|
2017-02-02 09:30:58 +03:00
|
|
|
|
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);
|
2017-04-06 13:36:46 +03:00
|
|
|
void SetButton(uint32_t aButton, bool aPressed, bool aTouched, double aValue);
|
2011-08-03 22:12:08 +04:00
|
|
|
void SetAxis(uint32_t aAxis, double aValue);
|
2020-04-07 12:13:50 +03:00
|
|
|
void SetIndex(int32_t aIndex);
|
2016-10-24 13:04:41 +03:00
|
|
|
void SetPose(const GamepadPoseState& aPose);
|
2019-05-30 01:44:24 +03:00
|
|
|
void SetLightIndicatorType(uint32_t aLightIndex,
|
|
|
|
GamepadLightIndicatorType aType);
|
|
|
|
void SetTouchEvent(uint32_t aTouchIndex, const GamepadTouchState& aTouch);
|
2017-04-18 10:56:08 +03:00
|
|
|
void SetHand(GamepadHand aHand);
|
2011-08-03 22:12:08 +04:00
|
|
|
|
|
|
|
// 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() { return mMapping; }
|
2013-04-09 16:43:25 +04:00
|
|
|
|
2017-07-14 11:53:03 +03:00
|
|
|
uint32_t DisplayId() const { return mDisplayId; }
|
|
|
|
|
2016-11-14 12:28:48 +03:00
|
|
|
GamepadHand Hand() { return mHand; }
|
|
|
|
|
2013-04-09 16:43:25 +04:00
|
|
|
bool Connected() const { return mConnected; }
|
|
|
|
|
2020-04-07 12:13:50 +03:00
|
|
|
int32_t Index() const { return mIndex; }
|
2013-04-09 16:43:25 +04:00
|
|
|
|
2017-02-02 09:30:58 +03:00
|
|
|
uint32_t HashKey() const { return mHashKey; }
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void GetButtons(nsTArray<RefPtr<GamepadButton>>& aButtons) const {
|
2020-05-11 11:20:52 +03:00
|
|
|
aButtons = mButtons.Clone();
|
2013-04-09 16:43:25 +04:00
|
|
|
}
|
|
|
|
|
2020-05-11 11:20:52 +03:00
|
|
|
void GetAxes(nsTArray<double>& aAxes) const { aAxes = mAxes.Clone(); }
|
2011-08-03 22:12:08 +04:00
|
|
|
|
2016-10-24 13:04:41 +03:00
|
|
|
GamepadPose* GetPose() const { return mPose; }
|
|
|
|
|
2017-02-02 09:30:58 +03:00
|
|
|
void GetHapticActuators(
|
|
|
|
nsTArray<RefPtr<GamepadHapticActuator>>& aHapticActuators) const {
|
2020-05-11 11:20:52 +03:00
|
|
|
aHapticActuators = mHapticActuators.Clone();
|
2017-02-02 09:30:58 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 01:44:24 +03:00
|
|
|
void GetLightIndicators(
|
|
|
|
nsTArray<RefPtr<GamepadLightIndicator>>& aLightIndicators) const {
|
2020-05-11 11:20:52 +03:00
|
|
|
aLightIndicators = mLightIndicators.Clone();
|
2019-05-30 01:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetTouchEvents(nsTArray<RefPtr<GamepadTouch>>& aTouchEvents) const {
|
2020-05-11 11:20:52 +03:00
|
|
|
aTouchEvents = mTouchEvents.Clone();
|
2019-05-30 01:44:24 +03:00
|
|
|
}
|
|
|
|
|
2011-08-03 22:12:08 +04:00
|
|
|
private:
|
2020-02-20 19:03:33 +03:00
|
|
|
virtual ~Gamepad() = default;
|
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;
|
2020-04-07 12:13:50 +03:00
|
|
|
int32_t mIndex;
|
2017-02-02 09:30:58 +03:00
|
|
|
// the gamepad hash key in GamepadManager
|
|
|
|
uint32_t mHashKey;
|
2017-07-14 11:53:03 +03:00
|
|
|
uint32_t mDisplayId;
|
2019-05-30 01:44:24 +03:00
|
|
|
uint32_t mTouchIdHashValue;
|
2013-04-09 16:43:25 +04:00
|
|
|
// The mapping in use.
|
|
|
|
GamepadMappingType mMapping;
|
2016-11-14 12:28:48 +03:00
|
|
|
GamepadHand mHand;
|
2013-04-09 16:43:25 +04:00
|
|
|
|
2011-08-03 22:12:08 +04:00
|
|
|
// true if this gamepad is currently connected.
|
|
|
|
bool mConnected;
|
|
|
|
|
|
|
|
// Current state of buttons, axes.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<GamepadButton>> mButtons;
|
2011-08-03 22:12:08 +04:00
|
|
|
nsTArray<double> mAxes;
|
2015-01-07 22:17:04 +03:00
|
|
|
DOMHighResTimeStamp mTimestamp;
|
2016-10-24 13:04:41 +03:00
|
|
|
RefPtr<GamepadPose> mPose;
|
2017-02-02 09:30:58 +03:00
|
|
|
nsTArray<RefPtr<GamepadHapticActuator>> mHapticActuators;
|
2019-05-30 01:44:24 +03:00
|
|
|
nsTArray<RefPtr<GamepadLightIndicator>> mLightIndicators;
|
|
|
|
nsTArray<RefPtr<GamepadTouch>> mTouchEvents;
|
|
|
|
nsDataHashtable<nsUint32HashKey, uint32_t> mTouchIdHash;
|
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
|