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 long& 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 if (m_isVibrating)
62 cancelVibration();
63
64 if (!length || (length == 1 && !pattern[0]))
65 return;
66
67 if (m_timerStart.isActive())
68 m_timerStart.stop();
69
70 m_pattern = pattern;
71 m_timerStart.startOneShot(0);
72}
73
74void Vibration::cancelVibration()
75{
76 if (m_isVibrating) {
77 m_vibrationClient->cancelVibration();
78 m_isVibrating = false;
79 m_timerStop.stop();
80 }
81}
82
83void Vibration::suspendVibration()
84{
85 if (!m_isVibrating)
86 return;
87
88 m_pattern.insert(0, m_timerStop.nextFireInterval());
89 m_timerStop.stop();
90 cancelVibration();
91}
92
93void Vibration::resumeVibration()
94{
95 m_timerStart.startOneShot(0);
96}
97
98void Vibration::timerStartFired(Timer<Vibration>* timer)
99{
100 ASSERT_UNUSED(timer, timer == &m_timerStart);
101
102 m_timerStart.stop();
103
104 if (m_pattern.size()) {
105 m_isVibrating = true;
106 m_vibrationClient->vibrate(m_pattern[0]);
107 m_timerStop.startOneShot(m_pattern[0] / 1000.0);
108 m_pattern.remove(0);
109 }
110}
111
112void Vibration::timerStopFired(Timer<Vibration>* timer)
113{
114 ASSERT_UNUSED(timer, timer == &m_timerStop);
115
116 m_timerStop.stop();
117 m_isVibrating = false;
118
119 if (m_pattern.size()) {
120 m_timerStart.startOneShot(m_pattern[0] / 1000.0);
121 m_pattern.remove(0);
122 }
123}
124
125const AtomicString& Vibration::supplementName()
126{
127 DEFINE_STATIC_LOCAL(AtomicString, name, ("vibration"));
128 return name;
129}
130
131bool Vibration::isActive(Page* page)
132{
133 return static_cast<bool>(Vibration::from(page));
134}
135
136void provideVibrationTo(Page* page, VibrationClient* client)
137{
138 PageSupplement::provideTo(page, Vibration::supplementName(), Vibration::create(client));
139}
140
141} // namespace WebCore
142
143#endif // ENABLE(VIBRATION)
144