Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/DeviceMotionEvent.h"
8 : #include "nsContentUtils.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 : /******************************************************************************
14 : * DeviceMotionEvent
15 : *****************************************************************************/
16 :
17 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event,
18 : mAcceleration,
19 : mAccelerationIncludingGravity,
20 : mRotationRate)
21 :
22 0 : NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event)
23 0 : NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event)
24 :
25 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent)
26 0 : NS_INTERFACE_MAP_END_INHERITING(Event)
27 :
28 : void
29 0 : DeviceMotionEvent::InitDeviceMotionEvent(
30 : const nsAString& aType,
31 : bool aCanBubble,
32 : bool aCancelable,
33 : const DeviceAccelerationInit& aAcceleration,
34 : const DeviceAccelerationInit& aAccelIncludingGravity,
35 : const DeviceRotationRateInit& aRotationRate,
36 : const Nullable<double>& aInterval)
37 : {
38 0 : InitDeviceMotionEvent(aType, aCanBubble, aCancelable, aAcceleration,
39 : aAccelIncludingGravity, aRotationRate, aInterval,
40 0 : Nullable<uint64_t>());
41 0 : }
42 :
43 : void
44 0 : DeviceMotionEvent::InitDeviceMotionEvent(
45 : const nsAString& aType,
46 : bool aCanBubble,
47 : bool aCancelable,
48 : const DeviceAccelerationInit& aAcceleration,
49 : const DeviceAccelerationInit& aAccelIncludingGravity,
50 : const DeviceRotationRateInit& aRotationRate,
51 : const Nullable<double>& aInterval,
52 : const Nullable<uint64_t>& aTimeStamp)
53 : {
54 0 : NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
55 :
56 0 : Event::InitEvent(aType, aCanBubble, aCancelable);
57 :
58 : mAcceleration = new DeviceAcceleration(this, aAcceleration.mX,
59 : aAcceleration.mY,
60 0 : aAcceleration.mZ);
61 :
62 : mAccelerationIncludingGravity =
63 : new DeviceAcceleration(this, aAccelIncludingGravity.mX,
64 : aAccelIncludingGravity.mY,
65 0 : aAccelIncludingGravity.mZ);
66 :
67 : mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha,
68 : aRotationRate.mBeta,
69 0 : aRotationRate.mGamma);
70 0 : mInterval = aInterval;
71 0 : if (!aTimeStamp.IsNull()) {
72 0 : mEvent->mTime = aTimeStamp.Value();
73 :
74 0 : static mozilla::TimeStamp sInitialNow = mozilla::TimeStamp::Now();
75 0 : static uint64_t sInitialEventTime = aTimeStamp.Value();
76 0 : mEvent->mTimeStamp = sInitialNow + mozilla::TimeDuration::FromMicroseconds(
77 0 : aTimeStamp.Value() - sInitialEventTime);
78 : }
79 : }
80 :
81 : already_AddRefed<DeviceMotionEvent>
82 0 : DeviceMotionEvent::Constructor(const GlobalObject& aGlobal,
83 : const nsAString& aType,
84 : const DeviceMotionEventInit& aEventInitDict,
85 : ErrorResult& aRv)
86 : {
87 0 : nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
88 0 : RefPtr<DeviceMotionEvent> e = new DeviceMotionEvent(t, nullptr, nullptr);
89 0 : e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
90 0 : bool trusted = e->Init(t);
91 :
92 0 : e->mAcceleration = new DeviceAcceleration(e,
93 : aEventInitDict.mAcceleration.mX,
94 : aEventInitDict.mAcceleration.mY,
95 0 : aEventInitDict.mAcceleration.mZ);
96 :
97 0 : e->mAccelerationIncludingGravity = new DeviceAcceleration(e,
98 : aEventInitDict.mAccelerationIncludingGravity.mX,
99 : aEventInitDict.mAccelerationIncludingGravity.mY,
100 0 : aEventInitDict.mAccelerationIncludingGravity.mZ);
101 :
102 0 : e->mRotationRate = new DeviceRotationRate(e,
103 : aEventInitDict.mRotationRate.mAlpha,
104 : aEventInitDict.mRotationRate.mBeta,
105 0 : aEventInitDict.mRotationRate.mGamma);
106 :
107 0 : e->mInterval = aEventInitDict.mInterval;
108 0 : e->SetTrusted(trusted);
109 0 : e->SetComposed(aEventInitDict.mComposed);
110 0 : return e.forget();
111 : }
112 :
113 : /******************************************************************************
114 : * DeviceAcceleration
115 : *****************************************************************************/
116 :
117 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration, mOwner)
118 :
119 0 : NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef)
120 0 : NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release)
121 :
122 0 : DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner,
123 : const Nullable<double>& aX,
124 : const Nullable<double>& aY,
125 0 : const Nullable<double>& aZ)
126 : : mOwner(aOwner)
127 : , mX(aX)
128 : , mY(aY)
129 0 : , mZ(aZ)
130 : {
131 0 : }
132 :
133 0 : DeviceAcceleration::~DeviceAcceleration()
134 : {
135 0 : }
136 :
137 : /******************************************************************************
138 : * DeviceRotationRate
139 : *****************************************************************************/
140 :
141 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate, mOwner)
142 :
143 0 : NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef)
144 0 : NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release)
145 :
146 0 : DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner,
147 : const Nullable<double>& aAlpha,
148 : const Nullable<double>& aBeta,
149 0 : const Nullable<double>& aGamma)
150 : : mOwner(aOwner)
151 : , mAlpha(aAlpha)
152 : , mBeta(aBeta)
153 0 : , mGamma(aGamma)
154 : {
155 0 : }
156 :
157 0 : DeviceRotationRate::~DeviceRotationRate()
158 : {
159 0 : }
160 :
161 : } // namespace dom
162 : } // namespace mozilla
163 :
164 : using namespace mozilla;
165 : using namespace mozilla::dom;
166 :
167 : already_AddRefed<DeviceMotionEvent>
168 0 : NS_NewDOMDeviceMotionEvent(EventTarget* aOwner,
169 : nsPresContext* aPresContext,
170 : WidgetEvent* aEvent)
171 : {
172 : RefPtr<DeviceMotionEvent> it =
173 0 : new DeviceMotionEvent(aOwner, aPresContext, aEvent);
174 0 : return it.forget();
175 : }
|