1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/DeviceMotionEvent.h"
8 #include "nsContentUtils.h"
10 namespace mozilla::dom
{
12 /******************************************************************************
14 *****************************************************************************/
16 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent
, Event
, mAcceleration
,
17 mAccelerationIncludingGravity
, mRotationRate
)
19 NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent
, Event
)
20 NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent
, Event
)
22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeviceMotionEvent
)
23 NS_INTERFACE_MAP_END_INHERITING(Event
)
25 void DeviceMotionEvent::InitDeviceMotionEvent(
26 const nsAString
& aType
, bool aCanBubble
, bool aCancelable
,
27 const DeviceAccelerationInit
& aAcceleration
,
28 const DeviceAccelerationInit
& aAccelIncludingGravity
,
29 const DeviceRotationRateInit
& aRotationRate
,
30 const Nullable
<double>& aInterval
) {
31 InitDeviceMotionEvent(aType
, aCanBubble
, aCancelable
, aAcceleration
,
32 aAccelIncludingGravity
, aRotationRate
, aInterval
,
33 Nullable
<uint64_t>());
36 void DeviceMotionEvent::InitDeviceMotionEvent(
37 const nsAString
& aType
, bool aCanBubble
, bool aCancelable
,
38 const DeviceAccelerationInit
& aAcceleration
,
39 const DeviceAccelerationInit
& aAccelIncludingGravity
,
40 const DeviceRotationRateInit
& aRotationRate
,
41 const Nullable
<double>& aInterval
, const Nullable
<uint64_t>& aTimeStamp
) {
42 NS_ENSURE_TRUE_VOID(!mEvent
->mFlags
.mIsBeingDispatched
);
44 Event::InitEvent(aType
, aCanBubble
, aCancelable
);
46 mAcceleration
= new DeviceAcceleration(this, aAcceleration
.mX
,
47 aAcceleration
.mY
, aAcceleration
.mZ
);
49 mAccelerationIncludingGravity
= new DeviceAcceleration(
50 this, aAccelIncludingGravity
.mX
, aAccelIncludingGravity
.mY
,
51 aAccelIncludingGravity
.mZ
);
53 mRotationRate
= new DeviceRotationRate(
54 this, aRotationRate
.mAlpha
, aRotationRate
.mBeta
, aRotationRate
.mGamma
);
55 mInterval
= aInterval
;
56 if (!aTimeStamp
.IsNull()) {
57 mEvent
->mTime
= aTimeStamp
.Value();
59 static mozilla::TimeStamp sInitialNow
= mozilla::TimeStamp::Now();
60 static uint64_t sInitialEventTime
= aTimeStamp
.Value();
62 sInitialNow
+ mozilla::TimeDuration::FromMicroseconds(
63 aTimeStamp
.Value() - sInitialEventTime
);
67 already_AddRefed
<DeviceMotionEvent
> DeviceMotionEvent::Constructor(
68 const GlobalObject
& aGlobal
, const nsAString
& aType
,
69 const DeviceMotionEventInit
& aEventInitDict
) {
70 nsCOMPtr
<EventTarget
> t
= do_QueryInterface(aGlobal
.GetAsSupports());
71 RefPtr
<DeviceMotionEvent
> e
= new DeviceMotionEvent(t
, nullptr, nullptr);
72 e
->InitEvent(aType
, aEventInitDict
.mBubbles
, aEventInitDict
.mCancelable
);
73 bool trusted
= e
->Init(t
);
75 e
->mAcceleration
= new DeviceAcceleration(e
, aEventInitDict
.mAcceleration
.mX
,
76 aEventInitDict
.mAcceleration
.mY
,
77 aEventInitDict
.mAcceleration
.mZ
);
79 e
->mAccelerationIncludingGravity
=
80 new DeviceAcceleration(e
, aEventInitDict
.mAccelerationIncludingGravity
.mX
,
81 aEventInitDict
.mAccelerationIncludingGravity
.mY
,
82 aEventInitDict
.mAccelerationIncludingGravity
.mZ
);
84 e
->mRotationRate
= new DeviceRotationRate(
85 e
, aEventInitDict
.mRotationRate
.mAlpha
,
86 aEventInitDict
.mRotationRate
.mBeta
, aEventInitDict
.mRotationRate
.mGamma
);
88 e
->mInterval
= aEventInitDict
.mInterval
;
89 e
->SetTrusted(trusted
);
90 e
->SetComposed(aEventInitDict
.mComposed
);
94 /******************************************************************************
96 *****************************************************************************/
98 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration
, mOwner
)
100 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration
, AddRef
)
101 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration
, Release
)
103 DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent
* aOwner
,
104 const Nullable
<double>& aX
,
105 const Nullable
<double>& aY
,
106 const Nullable
<double>& aZ
)
107 : mOwner(aOwner
), mX(aX
), mY(aY
), mZ(aZ
) {}
109 DeviceAcceleration::~DeviceAcceleration() = default;
111 /******************************************************************************
113 *****************************************************************************/
115 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate
, mOwner
)
117 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate
, AddRef
)
118 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate
, Release
)
120 DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent
* aOwner
,
121 const Nullable
<double>& aAlpha
,
122 const Nullable
<double>& aBeta
,
123 const Nullable
<double>& aGamma
)
124 : mOwner(aOwner
), mAlpha(aAlpha
), mBeta(aBeta
), mGamma(aGamma
) {}
126 DeviceRotationRate::~DeviceRotationRate() = default;
128 } // namespace mozilla::dom
130 using namespace mozilla
;
131 using namespace mozilla::dom
;
133 already_AddRefed
<DeviceMotionEvent
> NS_NewDOMDeviceMotionEvent(
134 EventTarget
* aOwner
, nsPresContext
* aPresContext
, WidgetEvent
* aEvent
) {
135 RefPtr
<DeviceMotionEvent
> it
=
136 new DeviceMotionEvent(aOwner
, aPresContext
, aEvent
);