1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* Copyright (C) 2012 Samsung Electronics
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "Vibration.h"
#if ENABLE(VIBRATION)
#include "VibrationClient.h"
namespace WebCore {
// Maximum number of entries in a vibration pattern.
const unsigned MaxVibrationPatternLength = 99;
// Maximum duration of a vibration in 10 seconds.
const unsigned MaxVibrationDuration = 10000;
Vibration::Vibration(VibrationClient* client)
: m_vibrationClient(client)
, m_timer(this, &Vibration::timerFired)
, m_state(State::Idle)
{
}
Vibration::~Vibration()
{
m_vibrationClient->vibrationDestroyed();
}
bool Vibration::vibrate(const VibrationPattern& pattern)
{
VibrationPattern& sanitized = const_cast<VibrationPattern&>(pattern);
size_t length = sanitized.size();
// If the pattern is too long then truncate it.
if (length > MaxVibrationPatternLength) {
sanitized.shrink(MaxVibrationPatternLength);
length = MaxVibrationPatternLength;
}
// If the length of pattern is even and is not zero, then remove the last entry in pattern.
if (length && !(length % 2)) {
sanitized.removeLast();
length--;
}
// If any pattern entry is too long then truncate it.
for (auto& duration : sanitized)
duration = std::min(duration, MaxVibrationDuration);
// Pre-exisiting instance need to be canceled when vibrate() is called.
if (isVibrating())
cancelVibration();
// If pattern is an empty list, then return true and terminate these steps.
if (!length || (length == 1 && !sanitized[0])) {
cancelVibration();
return true;
}
m_pattern = sanitized;
m_timer.startOneShot(0);
m_state = State::Waiting;
return true;
}
void Vibration::cancelVibration()
{
m_pattern.clear();
if (isVibrating()) {
m_timer.stop();
m_state = State::Idle;
m_vibrationClient->cancelVibration();
}
}
void Vibration::timerFired(Timer<Vibration>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
m_timer.stop();
if (m_pattern.isEmpty()) {
m_state = State::Idle;
return;
}
switch (m_state) {
case State::Vibrating:
m_state = State::Waiting;
break;
case State::Waiting:
case State::Idle:
m_state = State::Vibrating;
m_vibrationClient->vibrate(m_pattern[0]);
break;
}
m_timer.startOneShot(m_pattern[0] / 1000.0);
m_pattern.remove(0);
}
const char* Vibration::supplementName()
{
return "Vibration";
}
void provideVibrationTo(Page* page, VibrationClient* client)
{
Vibration::provideTo(page, Vibration::supplementName(), std::make_unique<Vibration>(client));
}
} // namespace WebCore
#endif // ENABLE(VIBRATION)
|