1 | /* |
2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "DeviceMotionEvent.h" |
28 | |
29 | #include "DeviceMotionData.h" |
30 | |
31 | namespace WebCore { |
32 | |
33 | DeviceMotionEvent::~DeviceMotionEvent() = default; |
34 | |
35 | DeviceMotionEvent::DeviceMotionEvent() |
36 | : m_deviceMotionData(DeviceMotionData::create()) |
37 | { |
38 | } |
39 | |
40 | DeviceMotionEvent::DeviceMotionEvent(const AtomicString& eventType, DeviceMotionData* deviceMotionData) |
41 | : Event(eventType, CanBubble::No, IsCancelable::No) |
42 | , m_deviceMotionData(deviceMotionData) |
43 | { |
44 | } |
45 | |
46 | static Optional<DeviceMotionEvent::Acceleration> convert(const DeviceMotionData::Acceleration* acceleration) |
47 | { |
48 | if (!acceleration) |
49 | return WTF::nullopt; |
50 | |
51 | return DeviceMotionEvent::Acceleration { acceleration->x(), acceleration->y(), acceleration->z() }; |
52 | } |
53 | |
54 | static Optional<DeviceMotionEvent::RotationRate> convert(const DeviceMotionData::RotationRate* rotationRate) |
55 | { |
56 | if (!rotationRate) |
57 | return WTF::nullopt; |
58 | |
59 | return DeviceMotionEvent::RotationRate { rotationRate->alpha(), rotationRate->beta(), rotationRate->gamma() }; |
60 | } |
61 | |
62 | static RefPtr<DeviceMotionData::Acceleration> convert(Optional<DeviceMotionEvent::Acceleration>&& acceleration) |
63 | { |
64 | if (!acceleration) |
65 | return nullptr; |
66 | |
67 | if (!acceleration->x && !acceleration->y && !acceleration->z) |
68 | return nullptr; |
69 | |
70 | return DeviceMotionData::Acceleration::create(acceleration->x, acceleration->y, acceleration->z); |
71 | } |
72 | |
73 | static RefPtr<DeviceMotionData::RotationRate> convert(Optional<DeviceMotionEvent::RotationRate>&& rotationRate) |
74 | { |
75 | if (!rotationRate) |
76 | return nullptr; |
77 | |
78 | if (!rotationRate->alpha && !rotationRate->beta && !rotationRate->gamma) |
79 | return nullptr; |
80 | |
81 | return DeviceMotionData::RotationRate::create(rotationRate->alpha, rotationRate->beta, rotationRate->gamma); |
82 | } |
83 | |
84 | Optional<DeviceMotionEvent::Acceleration> DeviceMotionEvent::acceleration() const |
85 | { |
86 | return convert(m_deviceMotionData->acceleration()); |
87 | } |
88 | |
89 | Optional<DeviceMotionEvent::Acceleration> DeviceMotionEvent::accelerationIncludingGravity() const |
90 | { |
91 | return convert(m_deviceMotionData->accelerationIncludingGravity()); |
92 | } |
93 | |
94 | Optional<DeviceMotionEvent::RotationRate> DeviceMotionEvent::rotationRate() const |
95 | { |
96 | return convert(m_deviceMotionData->rotationRate()); |
97 | } |
98 | |
99 | Optional<double> DeviceMotionEvent::interval() const |
100 | { |
101 | return m_deviceMotionData->interval(); |
102 | } |
103 | |
104 | void DeviceMotionEvent::initDeviceMotionEvent(const AtomicString& type, bool bubbles, bool cancelable, Optional<DeviceMotionEvent::Acceleration>&& acceleration, Optional<DeviceMotionEvent::Acceleration>&& accelerationIncludingGravity, Optional<DeviceMotionEvent::RotationRate>&& rotationRate, Optional<double> interval) |
105 | { |
106 | if (isBeingDispatched()) |
107 | return; |
108 | |
109 | initEvent(type, bubbles, cancelable); |
110 | m_deviceMotionData = DeviceMotionData::create(convert(WTFMove(acceleration)), convert(WTFMove(accelerationIncludingGravity)), convert(WTFMove(rotationRate)), interval); |
111 | } |
112 | |
113 | EventInterface DeviceMotionEvent::eventInterface() const |
114 | { |
115 | #if ENABLE(DEVICE_ORIENTATION) |
116 | return DeviceMotionEventInterfaceType; |
117 | #else |
118 | // FIXME: ENABLE(DEVICE_ORIENTATION) seems to be in a strange state where |
119 | // it is half-guarded by #ifdefs. DeviceMotionEvent.idl is guarded |
120 | // but DeviceMotionEvent.cpp itself is required by ungarded code. |
121 | return EventInterfaceType; |
122 | #endif |
123 | } |
124 | |
125 | } // namespace WebCore |
126 | |