1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_VRDevice_h_
7 #define mozilla_dom_VRDevice_h_
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/dom/TypedArray.h"
13 #include "mozilla/dom/VRDeviceBinding.h"
14 #include "mozilla/dom/DOMPoint.h"
15 #include "mozilla/dom/DOMRect.h"
20 #include "nsWrapperCache.h"
29 class VRFieldOfViewReadOnly
: public nsWrapperCache
32 VRFieldOfViewReadOnly(nsISupports
* aParent
,
33 double aUpDegrees
, double aRightDegrees
,
34 double aDownDegrees
, double aLeftDegrees
)
36 , mUpDegrees(aUpDegrees
)
37 , mRightDegrees(aRightDegrees
)
38 , mDownDegrees(aDownDegrees
)
39 , mLeftDegrees(aLeftDegrees
)
43 double UpDegrees() const { return mUpDegrees
; }
44 double RightDegrees() const { return mRightDegrees
; }
45 double DownDegrees() const { return mDownDegrees
; }
46 double LeftDegrees() const { return mLeftDegrees
; }
49 nsCOMPtr
<nsISupports
> mParent
;
56 class VRFieldOfView MOZ_FINAL
: public VRFieldOfViewReadOnly
60 explicit VRFieldOfView(nsISupports
* aParent
,
61 double aUpDegrees
= 0.0, double aRightDegrees
= 0.0,
62 double aDownDegrees
= 0.0, double aLeftDegrees
= 0.0)
63 : VRFieldOfViewReadOnly(aParent
, aUpDegrees
, aRightDegrees
, aDownDegrees
, aLeftDegrees
)
66 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRFieldOfView
)
67 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VRFieldOfView
)
69 static already_AddRefed
<VRFieldOfView
>
70 Constructor(const GlobalObject
& aGlobal
, const VRFieldOfViewInit
& aParams
,
73 static already_AddRefed
<VRFieldOfView
>
74 Constructor(const GlobalObject
& aGlobal
,
75 double aUpDegrees
, double aRightDegrees
,
76 double aDownDegrees
, double aLeftDegrees
,
79 nsISupports
* GetParentObject() const { return mParent
; }
80 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
82 void SetUpDegrees(double aVal
) { mUpDegrees
= aVal
; }
83 void SetRightDegrees(double aVal
) { mRightDegrees
= aVal
; }
84 void SetDownDegrees(double aVal
) { mDownDegrees
= aVal
; }
85 void SetLeftDegrees(double aVal
) { mLeftDegrees
= aVal
; }
88 class VRPositionState MOZ_FINAL
: public nsWrapperCache
92 explicit VRPositionState(nsISupports
* aParent
, const gfx::VRHMDSensorState
& aState
);
94 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRPositionState
)
95 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VRPositionState
)
97 double TimeStamp() const { return mTimeStamp
; }
99 bool HasPosition() const { return mPosition
!= nullptr; }
100 DOMPoint
* GetPosition() const { return mPosition
; }
102 bool HasOrientation() const { return mOrientation
!= nullptr; }
103 DOMPoint
* GetOrientation() const { return mOrientation
; }
105 // these are created lazily
106 DOMPoint
* GetLinearVelocity();
107 DOMPoint
* GetLinearAcceleration();
108 DOMPoint
* GetAngularVelocity();
109 DOMPoint
* GetAngularAcceleration();
111 nsISupports
* GetParentObject() const { return mParent
; }
112 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
115 nsCOMPtr
<nsISupports
> mParent
;
118 gfx::VRHMDSensorState mVRState
;
120 nsRefPtr
<DOMPoint
> mPosition
;
121 nsRefPtr
<DOMPoint
> mLinearVelocity
;
122 nsRefPtr
<DOMPoint
> mLinearAcceleration
;
124 nsRefPtr
<DOMPoint
> mOrientation
;
125 nsRefPtr
<DOMPoint
> mAngularVelocity
;
126 nsRefPtr
<DOMPoint
> mAngularAcceleration
;
129 class VRDevice
: public nsISupports
,
130 public nsWrapperCache
133 // create new VRDevice objects for all known underlying gfx::vr devices
134 static bool CreateAllKnownVRDevices(nsISupports
*aParent
, nsTArray
<nsRefPtr
<VRDevice
>>& aDevices
);
137 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
138 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(VRDevice
)
140 void GetHardwareUnitId(nsAString
& aHWID
) const { aHWID
= mHWID
; }
141 void GetDeviceId(nsAString
& aDeviceId
) const { aDeviceId
= mDeviceId
; }
142 void GetDeviceName(nsAString
& aDeviceName
) const { aDeviceName
= mDeviceName
; }
144 bool IsValid() { return mValid
; }
146 virtual void Shutdown() { }
148 nsISupports
* GetParentObject() const
158 VRDeviceType
GetType() const { return mType
; }
161 VRDevice(nsISupports
* aParent
, VRDeviceType aType
)
166 mHWID
.AssignLiteral("uknown");
167 mDeviceId
.AssignLiteral("unknown");
168 mDeviceName
.AssignLiteral("unknown");
171 virtual ~VRDevice() {
175 nsCOMPtr
<nsISupports
> mParent
;
178 nsString mDeviceName
;
185 class HMDVRDevice
: public VRDevice
188 virtual already_AddRefed
<DOMPoint
> GetEyeTranslation(VREye aEye
) = 0;
190 virtual void SetFieldOfView(const VRFieldOfViewInit
& aLeftFOV
,
191 const VRFieldOfViewInit
& aRightFOV
,
192 double zNear
, double zFar
) = 0;
193 virtual already_AddRefed
<VRFieldOfView
> GetCurrentEyeFieldOfView(VREye aEye
) = 0;
194 virtual already_AddRefed
<VRFieldOfView
> GetRecommendedEyeFieldOfView(VREye aEye
) = 0;
195 virtual already_AddRefed
<VRFieldOfView
> GetMaximumEyeFieldOfView(VREye aEye
) = 0;
196 virtual already_AddRefed
<DOMRect
> GetRecommendedEyeRenderRect(VREye aEye
) = 0;
198 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
200 void XxxToggleElementVR(Element
& aElement
);
202 gfx::VRHMDInfo
*GetHMD() { return mHMD
.get(); }
205 HMDVRDevice(nsISupports
* aParent
, gfx::VRHMDInfo
* aHMD
)
206 : VRDevice(aParent
, VRDevice::HMD
)
210 virtual ~HMDVRDevice() { }
212 nsRefPtr
<gfx::VRHMDInfo
> mHMD
;
215 class PositionSensorVRDevice
: public VRDevice
218 virtual already_AddRefed
<VRPositionState
> GetState(double timeOffset
) = 0;
220 virtual void ZeroSensor() = 0;
222 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
225 explicit PositionSensorVRDevice(nsISupports
* aParent
)
226 : VRDevice(aParent
, VRDevice::PositionSensor
)
229 virtual ~PositionSensorVRDevice() { }
233 } // namespace mozilla