~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/Modules/vibration/Vibration.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
27
namespace WebCore {
 
28
 
 
29
Vibration::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
 
 
37
Vibration::~Vibration()
 
38
{
 
39
    m_vibrationClient->vibrationDestroyed();
 
40
}
 
41
 
 
42
PassOwnPtr<Vibration> Vibration::create(VibrationClient* client)
 
43
{
 
44
    return adoptPtr(new Vibration(client));
 
45
}
 
46
 
 
47
void 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
 
 
57
void 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
 
 
77
void 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
 
 
87
void 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
 
 
97
void Vibration::resumeVibration()
 
98
{
 
99
    m_timerStart.startOneShot(0);
 
100
}
 
101
 
 
102
void 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
 
 
116
void 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
 
 
129
const AtomicString& Vibration::supplementName()
 
130
{
 
131
    DEFINE_STATIC_LOCAL(AtomicString, name, ("Vibration", AtomicString::ConstructFromLiteral));
 
132
    return name;
 
133
}
 
134
 
 
135
bool Vibration::isActive(Page* page)
 
136
{
 
137
    return static_cast<bool>(Vibration::from(page));
 
138
}
 
139
 
 
140
void provideVibrationTo(Page* page, VibrationClient* client)
 
141
{
 
142
    Vibration::provideTo(page, Vibration::supplementName(), Vibration::create(client));
 
143
}
 
144
 
 
145
} // namespace WebCore
 
146
 
 
147
#endif // ENABLE(VIBRATION)
 
148