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 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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 "modules/device_orientation/DeviceMotionData.h"
28#include "public/platform/WebDeviceMotionData.h"
29
30namespace blink {
31
32DeviceMotionData::Acceleration* DeviceMotionData::Acceleration::create(
33    bool canProvideX, double x, bool canProvideY, double y, bool canProvideZ, double z)
34{
35    return new DeviceMotionData::Acceleration(canProvideX, x, canProvideY, y, canProvideZ, z);
36}
37
38DeviceMotionData::Acceleration::Acceleration(bool canProvideX, double x, bool canProvideY, double y, bool canProvideZ, double z)
39    : m_x(x)
40    , m_y(y)
41    , m_z(z)
42    , m_canProvideX(canProvideX)
43    , m_canProvideY(canProvideY)
44    , m_canProvideZ(canProvideZ)
45
46{
47}
48
49DeviceMotionData::RotationRate* DeviceMotionData::RotationRate::create(
50    bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
51{
52    return new DeviceMotionData::RotationRate(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
53}
54
55DeviceMotionData::RotationRate::RotationRate(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
56    : m_alpha(alpha)
57    , m_beta(beta)
58    , m_gamma(gamma)
59    , m_canProvideAlpha(canProvideAlpha)
60    , m_canProvideBeta(canProvideBeta)
61    , m_canProvideGamma(canProvideGamma)
62{
63}
64
65DeviceMotionData* DeviceMotionData::create()
66{
67    return new DeviceMotionData;
68}
69
70DeviceMotionData* DeviceMotionData::create(
71    Acceleration* acceleration,
72    Acceleration* accelerationIncludingGravity,
73    RotationRate* rotationRate,
74    bool canProvideInterval,
75    double interval)
76{
77    return new DeviceMotionData(acceleration, accelerationIncludingGravity, rotationRate, canProvideInterval, interval);
78}
79
80DeviceMotionData* DeviceMotionData::create(const WebDeviceMotionData& data)
81{
82    return DeviceMotionData::create(
83        DeviceMotionData::Acceleration::create(
84            data.hasAccelerationX, data.accelerationX,
85            data.hasAccelerationY, data.accelerationY,
86            data.hasAccelerationZ, data.accelerationZ),
87        DeviceMotionData::Acceleration::create(
88            data.hasAccelerationIncludingGravityX, data.accelerationIncludingGravityX,
89            data.hasAccelerationIncludingGravityY, data.accelerationIncludingGravityY,
90            data.hasAccelerationIncludingGravityZ, data.accelerationIncludingGravityZ),
91        DeviceMotionData::RotationRate::create(
92            data.hasRotationRateAlpha, data.rotationRateAlpha,
93            data.hasRotationRateBeta, data.rotationRateBeta,
94            data.hasRotationRateGamma, data.rotationRateGamma),
95        true, data.interval);
96}
97
98DeviceMotionData::DeviceMotionData()
99    : m_canProvideInterval(false)
100    , m_interval(0)
101{
102}
103
104DeviceMotionData::DeviceMotionData(
105    Acceleration* acceleration,
106    Acceleration* accelerationIncludingGravity,
107    RotationRate* rotationRate,
108    bool canProvideInterval,
109    double interval)
110    : m_acceleration(acceleration)
111    , m_accelerationIncludingGravity(accelerationIncludingGravity)
112    , m_rotationRate(rotationRate)
113    , m_canProvideInterval(canProvideInterval)
114    , m_interval(interval)
115{
116}
117
118void DeviceMotionData::trace(Visitor* visitor)
119{
120    visitor->trace(m_acceleration);
121    visitor->trace(m_accelerationIncludingGravity);
122    visitor->trace(m_rotationRate);
123}
124
125bool DeviceMotionData::canProvideEventData() const
126{
127    const bool hasAcceleration = m_acceleration && (m_acceleration->canProvideX() || m_acceleration->canProvideY() || m_acceleration->canProvideZ());
128    const bool hasAccelerationIncludingGravity = m_accelerationIncludingGravity && (m_accelerationIncludingGravity->canProvideX() || m_accelerationIncludingGravity->canProvideY() || m_accelerationIncludingGravity->canProvideZ());
129    const bool hasRotationRate = m_rotationRate && (m_rotationRate->canProvideAlpha() || m_rotationRate->canProvideBeta() || m_rotationRate->canProvideGamma());
130
131    return hasAcceleration || hasAccelerationIncludingGravity || hasRotationRate;
132}
133
134} // namespace blink
135