1/* 2 * Copyright (C) 2012 Samsung Electronics 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20#include "config.h" 21#include "Vibration.h" 22 23#if ENABLE(VIBRATION) 24 25#include "VibrationClient.h" 26 27namespace WebCore { 28 29Vibration::Vibration(VibrationClient* client) 30 : m_vibrationClient(client) 31 , m_timerStart(this, &Vibration::timerStartFired) 32 , m_timerStop(this, &Vibration::timerStopFired) 33 , m_isVibrating(false) 34{ 35} 36 37Vibration::~Vibration() 38{ 39 m_vibrationClient->vibrationDestroyed(); 40} 41 42PassOwnPtr<Vibration> Vibration::create(VibrationClient* client) 43{ 44 return adoptPtr(new Vibration(client)); 45} 46 47void Vibration::vibrate(const unsigned& time) 48{ 49 if (!time) { 50 cancelVibration(); 51 return; 52 } 53 m_pattern.append(time); 54 m_timerStart.startOneShot(0); 55} 56 57void Vibration::vibrate(const VibrationPattern& pattern) 58{ 59 int length = pattern.size(); 60 61 // Cancel the pre-existing instance of vibration patterns, if the pattern is 0 or an empty list. 62 if (!length || (length == 1 && !pattern[0])) { 63 cancelVibration(); 64 return; 65 } 66 67 if (m_isVibrating) 68 cancelVibration(); 69 70 if (m_timerStart.isActive()) 71 m_timerStart.stop(); 72 73 m_pattern = pattern; 74 m_timerStart.startOneShot(0); 75} 76 77void Vibration::cancelVibration() 78{ 79 m_pattern.clear(); 80 if (m_isVibrating) { 81 m_vibrationClient->cancelVibration(); 82 m_isVibrating = false; 83 m_timerStop.stop(); 84 } 85} 86 87void Vibration::suspendVibration() 88{ 89 if (!m_isVibrating) 90 return; 91 92 m_pattern.insert(0, m_timerStop.nextFireInterval()); 93 m_timerStop.stop(); 94 cancelVibration(); 95} 96 97void Vibration::resumeVibration() 98{ 99 m_timerStart.startOneShot(0); 100} 101 102void Vibration::timerStartFired(Timer<Vibration>* timer) 103{ 104 ASSERT_UNUSED(timer, timer == &m_timerStart); 105 106 m_timerStart.stop(); 107 108 if (m_pattern.size()) { 109 m_isVibrating = true; 110 m_vibrationClient->vibrate(m_pattern[0]); 111 m_timerStop.startOneShot(m_pattern[0] / 1000.0); 112 m_pattern.remove(0); 113 } 114} 115 116void Vibration::timerStopFired(Timer<Vibration>* timer) 117{ 118 ASSERT_UNUSED(timer, timer == &m_timerStop); 119 120 m_timerStop.stop(); 121 m_isVibrating = false; 122 123 if (m_pattern.size()) { 124 m_timerStart.startOneShot(m_pattern[0] / 1000.0); 125 m_pattern.remove(0); 126 } 127} 128 129const char* Vibration::supplementName() 130{ 131 return "Vibration"; 132} 133 134bool Vibration::isActive(Page* page) 135{ 136 return static_cast<bool>(Vibration::from(page)); 137} 138 139void provideVibrationTo(Page* page, VibrationClient* client) 140{ 141 Vibration::provideTo(page, Vibration::supplementName(), Vibration::create(client)); 142} 143 144} // namespace WebCore 145 146#endif // ENABLE(VIBRATION) 147 148