|
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 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event, |
|
18 mAcceleration, |
|
19 mAccelerationIncludingGravity, |
|
20 mRotationRate) |
|
21 |
|
22 NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event) |
|
23 NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event) |
|
24 |
|
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent) |
|
26 NS_INTERFACE_MAP_END_INHERITING(Event) |
|
27 |
|
28 void |
|
29 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 Nullable<double> aInterval, |
|
37 ErrorResult& aRv) |
|
38 { |
|
39 aRv = Event::InitEvent(aType, aCanBubble, aCancelable); |
|
40 if (aRv.Failed()) { |
|
41 return; |
|
42 } |
|
43 |
|
44 mAcceleration = new DeviceAcceleration(this, aAcceleration.mX, |
|
45 aAcceleration.mY, |
|
46 aAcceleration.mZ); |
|
47 |
|
48 mAccelerationIncludingGravity = |
|
49 new DeviceAcceleration(this, aAccelIncludingGravity.mX, |
|
50 aAccelIncludingGravity.mY, |
|
51 aAccelIncludingGravity.mZ); |
|
52 |
|
53 mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha, |
|
54 aRotationRate.mBeta, |
|
55 aRotationRate.mGamma); |
|
56 mInterval = aInterval; |
|
57 } |
|
58 |
|
59 already_AddRefed<DeviceMotionEvent> |
|
60 DeviceMotionEvent::Constructor(const GlobalObject& aGlobal, |
|
61 const nsAString& aType, |
|
62 const DeviceMotionEventInit& aEventInitDict, |
|
63 ErrorResult& aRv) |
|
64 { |
|
65 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports()); |
|
66 nsRefPtr<DeviceMotionEvent> e = new DeviceMotionEvent(t, nullptr, nullptr); |
|
67 aRv = e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable); |
|
68 if (aRv.Failed()) { |
|
69 return nullptr; |
|
70 } |
|
71 bool trusted = e->Init(t); |
|
72 |
|
73 e->mAcceleration = new DeviceAcceleration(e, |
|
74 aEventInitDict.mAcceleration.mX, |
|
75 aEventInitDict.mAcceleration.mY, |
|
76 aEventInitDict.mAcceleration.mZ); |
|
77 |
|
78 e->mAccelerationIncludingGravity = new DeviceAcceleration(e, |
|
79 aEventInitDict.mAccelerationIncludingGravity.mX, |
|
80 aEventInitDict.mAccelerationIncludingGravity.mY, |
|
81 aEventInitDict.mAccelerationIncludingGravity.mZ); |
|
82 |
|
83 e->mRotationRate = new DeviceRotationRate(e, |
|
84 aEventInitDict.mRotationRate.mAlpha, |
|
85 aEventInitDict.mRotationRate.mBeta, |
|
86 aEventInitDict.mRotationRate.mGamma); |
|
87 |
|
88 e->mInterval = aEventInitDict.mInterval; |
|
89 e->SetTrusted(trusted); |
|
90 |
|
91 return e.forget(); |
|
92 } |
|
93 |
|
94 /****************************************************************************** |
|
95 * DeviceAcceleration |
|
96 *****************************************************************************/ |
|
97 |
|
98 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DeviceAcceleration, mOwner) |
|
99 |
|
100 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef) |
|
101 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release) |
|
102 |
|
103 DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner, |
|
104 Nullable<double> aX, |
|
105 Nullable<double> aY, |
|
106 Nullable<double> aZ) |
|
107 : mOwner(aOwner) |
|
108 , mX(aX) |
|
109 , mY(aY) |
|
110 , mZ(aZ) |
|
111 { |
|
112 SetIsDOMBinding(); |
|
113 } |
|
114 |
|
115 DeviceAcceleration::~DeviceAcceleration() |
|
116 { |
|
117 } |
|
118 |
|
119 /****************************************************************************** |
|
120 * DeviceRotationRate |
|
121 *****************************************************************************/ |
|
122 |
|
123 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DeviceRotationRate, mOwner) |
|
124 |
|
125 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef) |
|
126 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release) |
|
127 |
|
128 DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner, |
|
129 Nullable<double> aAlpha, |
|
130 Nullable<double> aBeta, |
|
131 Nullable<double> aGamma) |
|
132 : mOwner(aOwner) |
|
133 , mAlpha(aAlpha) |
|
134 , mBeta(aBeta) |
|
135 , mGamma(aGamma) |
|
136 { |
|
137 SetIsDOMBinding(); |
|
138 } |
|
139 |
|
140 DeviceRotationRate::~DeviceRotationRate() |
|
141 { |
|
142 } |
|
143 |
|
144 } // namespace dom |
|
145 } // namespace mozilla |
|
146 |
|
147 using namespace mozilla; |
|
148 using namespace mozilla::dom; |
|
149 |
|
150 nsresult |
|
151 NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aInstancePtrResult, |
|
152 EventTarget* aOwner, |
|
153 nsPresContext* aPresContext, |
|
154 WidgetEvent* aEvent) |
|
155 { |
|
156 NS_ENSURE_ARG_POINTER(aInstancePtrResult); |
|
157 |
|
158 DeviceMotionEvent* it = new DeviceMotionEvent(aOwner, aPresContext, aEvent); |
|
159 NS_ADDREF(it); |
|
160 *aInstancePtrResult = static_cast<Event*>(it); |
|
161 return NS_OK; |
|
162 } |