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 COMPUTER, 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 COMPUTER, 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 "modules/device_orientation/DeviceMotionEvent.h" 28 29 #include "modules/device_orientation/DeviceAcceleration.h" 30 #include "modules/device_orientation/DeviceMotionData.h" 31 #include "modules/device_orientation/DeviceRotationRate.h" 32 33 namespace blink { 34 35 DeviceMotionEvent::~DeviceMotionEvent() 36 { 37 } 38 39 DeviceMotionEvent::DeviceMotionEvent() 40 : m_deviceMotionData(DeviceMotionData::create()) 41 { 42 } 43 44 DeviceMotionEvent::DeviceMotionEvent(const AtomicString& eventType, DeviceMotionData* deviceMotionData) 45 : Event(eventType, false, false) // Can't bubble, not cancelable 46 , m_deviceMotionData(deviceMotionData) 47 { 48 } 49 50 void DeviceMotionEvent::initDeviceMotionEvent(const AtomicString& type, bool bubbles, bool cancelable, DeviceMotionData* deviceMotionData) 51 { 52 if (dispatched()) 53 return; 54 55 initEvent(type, bubbles, cancelable); 56 m_deviceMotionData = deviceMotionData; 57 58 m_acceleration.clear(); 59 m_accelerationIncludingGravity.clear(); 60 m_rotationRate.clear(); 61 } 62 63 DeviceAcceleration* DeviceMotionEvent::acceleration() 64 { 65 if (!m_deviceMotionData->acceleration()) 66 return 0; 67 68 if (!m_acceleration) 69 m_acceleration = DeviceAcceleration::create(m_deviceMotionData->acceleration()); 70 71 return m_acceleration.get(); 72 } 73 74 DeviceAcceleration* DeviceMotionEvent::accelerationIncludingGravity() 75 { 76 if (!m_deviceMotionData->accelerationIncludingGravity()) 77 return 0; 78 79 if (!m_accelerationIncludingGravity) 80 m_accelerationIncludingGravity = DeviceAcceleration::create(m_deviceMotionData->accelerationIncludingGravity()); 81 82 return m_accelerationIncludingGravity.get(); 83 } 84 85 DeviceRotationRate* DeviceMotionEvent::rotationRate() 86 { 87 if (!m_deviceMotionData->rotationRate()) 88 return 0; 89 90 if (!m_rotationRate) 91 m_rotationRate = DeviceRotationRate::create(m_deviceMotionData->rotationRate()); 92 93 return m_rotationRate.get(); 94 } 95 96 double DeviceMotionEvent::interval(bool& isNull) const 97 { 98 if (m_deviceMotionData->canProvideInterval()) 99 return m_deviceMotionData->interval(); 100 101 isNull = true; 102 return 0; 103 } 104 105 const AtomicString& DeviceMotionEvent::interfaceName() const 106 { 107 return EventNames::DeviceMotionEvent; 108 } 109 110 void DeviceMotionEvent::trace(Visitor* visitor) 111 { 112 visitor->trace(m_deviceMotionData); 113 visitor->trace(m_acceleration); 114 visitor->trace(m_accelerationIncludingGravity); 115 visitor->trace(m_rotationRate); 116 Event::trace(visitor); 117 } 118 119 } // namespace blink 120