diff options
Diffstat (limited to 'Source/WebCore/Modules/vibration')
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.cpp | 60 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.h | 46 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.idl | 26 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/Vibration.cpp | 130 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/Vibration.h | 60 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/VibrationClient.h | 38 |
6 files changed, 360 insertions, 0 deletions
diff --git a/Source/WebCore/Modules/vibration/NavigatorVibration.cpp b/Source/WebCore/Modules/vibration/NavigatorVibration.cpp new file mode 100644 index 000000000..f56e5401d --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.cpp @@ -0,0 +1,60 @@ +/* + * 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 "NavigatorVibration.h" + +#if ENABLE(VIBRATION) + +#include "Frame.h" +#include "Navigator.h" +#include "Page.h" +#include "Vibration.h" +#include <runtime/Uint32Array.h> + +namespace WebCore { + +NavigatorVibration::NavigatorVibration() +{ +} + +NavigatorVibration::~NavigatorVibration() +{ +} + +bool NavigatorVibration::vibrate(Navigator& navigator, unsigned time) +{ + return NavigatorVibration::vibrate(navigator, VibrationPattern(1, time)); +} + +bool NavigatorVibration::vibrate(Navigator& navigator, const VibrationPattern& pattern) +{ + if (!navigator.frame()->page()) + return false; + + if (navigator.frame()->page()->visibilityState() == PageVisibilityState::Hidden) + return false; + + return Vibration::from(navigator.frame()->page())->vibrate(pattern); +} + +} // namespace WebCore + +#endif // ENABLE(VIBRATION) + diff --git a/Source/WebCore/Modules/vibration/NavigatorVibration.h b/Source/WebCore/Modules/vibration/NavigatorVibration.h new file mode 100644 index 000000000..3d4ffdc3a --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.h @@ -0,0 +1,46 @@ +/* + * 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. + */ + +#pragma once + +#if ENABLE(VIBRATION) + +#include "ExceptionCode.h" +#include <wtf/Vector.h> + +namespace WebCore { + +class Navigator; + +class NavigatorVibration { +public: + typedef Vector<unsigned> VibrationPattern; + + ~NavigatorVibration(); + + static bool vibrate(Navigator&, unsigned time); + static bool vibrate(Navigator&, const VibrationPattern&); + +private: + NavigatorVibration(); +}; + +} // namespace WebCore + +#endif // ENABLE(VIBRATION) diff --git a/Source/WebCore/Modules/vibration/NavigatorVibration.idl b/Source/WebCore/Modules/vibration/NavigatorVibration.idl new file mode 100644 index 000000000..92150149f --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.idl @@ -0,0 +1,26 @@ +/* + * 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. + */ + +[ + Conditional=VIBRATION, +] partial interface Navigator { + boolean vibrate(sequence<unsigned long> pattern); + boolean vibrate(unsigned long time); +}; + diff --git a/Source/WebCore/Modules/vibration/Vibration.cpp b/Source/WebCore/Modules/vibration/Vibration.cpp new file mode 100644 index 000000000..860914209 --- /dev/null +++ b/Source/WebCore/Modules/vibration/Vibration.cpp @@ -0,0 +1,130 @@ +/* + * 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() +{ + 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) + diff --git a/Source/WebCore/Modules/vibration/Vibration.h b/Source/WebCore/Modules/vibration/Vibration.h new file mode 100644 index 000000000..aa1311f34 --- /dev/null +++ b/Source/WebCore/Modules/vibration/Vibration.h @@ -0,0 +1,60 @@ +/* + * 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. + */ + +#pragma once + +#if ENABLE(VIBRATION) + +#include "Page.h" +#include "Timer.h" + +namespace WebCore { + +class VibrationClient; + +class Vibration : public Supplement<Page> { +public: + typedef Vector<unsigned> VibrationPattern; + + explicit Vibration(VibrationClient*); + ~Vibration(); + + bool vibrate(const VibrationPattern&); + // FIXME: When a visibilitychange event is dispatched while vibrating, the vibration should be canceled. + void cancelVibration(); + + void timerFired(); + + static const char* supplementName(); + static Vibration* from(Page* page) { return static_cast<Vibration*>(Supplement<Page>::from(page, supplementName())); } + + bool isVibrating() { return m_state != State::Idle; } + +private: + enum class State { Idle, Vibrating, Waiting }; + + VibrationClient* m_vibrationClient; + Timer m_timer; + State m_state; + VibrationPattern m_pattern; +}; + +} // namespace WebCore + +#endif // ENABLE(VIBRATION) diff --git a/Source/WebCore/Modules/vibration/VibrationClient.h b/Source/WebCore/Modules/vibration/VibrationClient.h new file mode 100644 index 000000000..383445f48 --- /dev/null +++ b/Source/WebCore/Modules/vibration/VibrationClient.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#pragma once + +namespace WebCore { + +class Page; + +class VibrationClient { +public: + virtual ~VibrationClient() { } + + virtual void vibrate(const unsigned& time) = 0; + virtual void cancelVibration() = 0; + + virtual void vibrationDestroyed() = 0; +}; + +void provideVibrationTo(Page*, VibrationClient*); + +} // namespace WebCore |