diff options
Diffstat (limited to 'Source/WebCore/Modules/vibration')
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.cpp | 81 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.h | 50 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/NavigatorVibration.idl | 30 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/Vibration.cpp | 144 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/Vibration.h | 71 | ||||
-rw-r--r-- | Source/WebCore/Modules/vibration/VibrationClient.h | 42 |
6 files changed, 418 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..ae46bff9c --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.cpp @@ -0,0 +1,81 @@ +/* + * 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 "ExceptionCode.h" +#include "Frame.h" +#include "Navigator.h" +#include "Page.h" +#include "Vibration.h" +#include <wtf/Uint32Array.h> + +namespace WebCore { + +NavigatorVibration::NavigatorVibration() +{ +} + +NavigatorVibration::~NavigatorVibration() +{ +} + +void NavigatorVibration::webkitVibrate(Navigator* navigator, unsigned long time, ExceptionCode& ec) +{ + if (!navigator->frame()->page()) + return; + +#if ENABLE(PAGE_VISIBILITY_API) + if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidden) + return; +#endif + + if (!Vibration::isActive(navigator->frame()->page())) { + ec = NOT_SUPPORTED_ERR; + return; + } + + Vibration::from(navigator->frame()->page())->vibrate(time); +} + +void NavigatorVibration::webkitVibrate(Navigator* navigator, const VibrationPattern& pattern, ExceptionCode& ec) +{ + if (!navigator->frame()->page()) + return; + +#if ENABLE(PAGE_VISIBILITY_API) + if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidden) + return; +#endif + + if (!Vibration::isActive(navigator->frame()->page())) { + ec = NOT_SUPPORTED_ERR; + 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..098f6ad5a --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.h @@ -0,0 +1,50 @@ +/* + * 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. + */ + +#ifndef NavigatorVibration_h +#define NavigatorVibration_h + +#if ENABLE(VIBRATION) + +#include "ExceptionCode.h" +#include <wtf/Vector.h> + +namespace WebCore { + +class Navigator; +class Uint32Array; + +class NavigatorVibration { +public: + typedef Vector<unsigned long> VibrationPattern; + + static void webkitVibrate(Navigator*, unsigned long time, ExceptionCode&); + static void webkitVibrate(Navigator*, const VibrationPattern&, ExceptionCode&); + +private: + NavigatorVibration(); + ~NavigatorVibration(); +}; + +} // namespace WebCore + +#endif // ENABLE(VIBRATION) + +#endif // NavigatorVibration_h + diff --git a/Source/WebCore/Modules/vibration/NavigatorVibration.idl b/Source/WebCore/Modules/vibration/NavigatorVibration.idl new file mode 100644 index 000000000..624845d81 --- /dev/null +++ b/Source/WebCore/Modules/vibration/NavigatorVibration.idl @@ -0,0 +1,30 @@ +/* + * 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. + */ + +module window { + + interface [ + Conditional=VIBRATION, + Supplemental=Navigator + ] NavigatorVibration { + void webkitVibrate(in unsigned long[] pattern) raises(DOMException); + void webkitVibrate(in unsigned long time) raises(DOMException); + }; + +} diff --git a/Source/WebCore/Modules/vibration/Vibration.cpp b/Source/WebCore/Modules/vibration/Vibration.cpp new file mode 100644 index 000000000..9e52c3dfb --- /dev/null +++ b/Source/WebCore/Modules/vibration/Vibration.cpp @@ -0,0 +1,144 @@ +/* + * 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 { + +Vibration::Vibration(VibrationClient* client) + : m_vibrationClient(client) + , m_timerStart(this, &Vibration::timerStartFired) + , m_timerStop(this, &Vibration::timerStopFired) + , m_isVibrating(false) +{ +} + +Vibration::~Vibration() +{ + m_vibrationClient->vibrationDestroyed(); +} + +PassOwnPtr<Vibration> Vibration::create(VibrationClient* client) +{ + return adoptPtr(new Vibration(client)); +} + +void Vibration::vibrate(const unsigned long& time) +{ + if (!time) { + cancelVibration(); + return; + } + m_pattern.append(time); + m_timerStart.startOneShot(0); +} + +void Vibration::vibrate(const VibrationPattern& pattern) +{ + int length = pattern.size(); + + if (m_isVibrating) + cancelVibration(); + + if (!length || (length == 1 && !pattern[0])) + return; + + if (m_timerStart.isActive()) + m_timerStart.stop(); + + m_pattern = pattern; + m_timerStart.startOneShot(0); +} + +void Vibration::cancelVibration() +{ + if (m_isVibrating) { + m_vibrationClient->cancelVibration(); + m_isVibrating = false; + m_timerStop.stop(); + } +} + +void Vibration::suspendVibration() +{ + if (!m_isVibrating) + return; + + m_pattern.insert(0, m_timerStop.nextFireInterval()); + m_timerStop.stop(); + cancelVibration(); +} + +void Vibration::resumeVibration() +{ + m_timerStart.startOneShot(0); +} + +void Vibration::timerStartFired(Timer<Vibration>* timer) +{ + ASSERT_UNUSED(timer, timer == &m_timerStart); + + m_timerStart.stop(); + + if (m_pattern.size()) { + m_isVibrating = true; + m_vibrationClient->vibrate(m_pattern[0]); + m_timerStop.startOneShot(m_pattern[0] / 1000.0); + m_pattern.remove(0); + } +} + +void Vibration::timerStopFired(Timer<Vibration>* timer) +{ + ASSERT_UNUSED(timer, timer == &m_timerStop); + + m_timerStop.stop(); + m_isVibrating = false; + + if (m_pattern.size()) { + m_timerStart.startOneShot(m_pattern[0] / 1000.0); + m_pattern.remove(0); + } +} + +const AtomicString& Vibration::supplementName() +{ + DEFINE_STATIC_LOCAL(AtomicString, name, ("vibration")); + return name; +} + +bool Vibration::isActive(Page* page) +{ + return static_cast<bool>(Vibration::from(page)); +} + +void provideVibrationTo(Page* page, VibrationClient* client) +{ + PageSupplement::provideTo(page, Vibration::supplementName(), Vibration::create(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..27e0736be --- /dev/null +++ b/Source/WebCore/Modules/vibration/Vibration.h @@ -0,0 +1,71 @@ +/* + * 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. + */ + +#ifndef Vibration_h +#define Vibration_h + +#if ENABLE(VIBRATION) + +#include "PageSupplement.h" +#include "Timer.h" +#include <wtf/PassOwnPtr.h> + +namespace WebCore { + +class Navigator; +class Page; +class VibrationClient; + +class Vibration : public PageSupplement { +public: + typedef Vector<unsigned long> VibrationPattern; + + Vibration(VibrationClient*); + ~Vibration(); + + static PassOwnPtr<Vibration> create(VibrationClient*); + + void vibrate(const unsigned long& time); + void vibrate(const VibrationPattern&); + void cancelVibration(); + + // FIXME : Add suspendVibration() and resumeVibration() to the page visibility feature, when the document.hidden attribute is changed. + void suspendVibration(); + void resumeVibration(); + void timerStartFired(Timer<Vibration>*); + void timerStopFired(Timer<Vibration>*); + + static const AtomicString& supplementName(); + static Vibration* from(Page* page) { return static_cast<Vibration*>(PageSupplement::from(page, supplementName())); } + static bool isActive(Page*); + +private: + VibrationClient* m_vibrationClient; + Timer<Vibration> m_timerStart; + Timer<Vibration> m_timerStop; + bool m_isVibrating; + VibrationPattern m_pattern; +}; + +} // namespace WebCore + +#endif // ENABLE(VIBRATION) + +#endif // Vibration_h + diff --git a/Source/WebCore/Modules/vibration/VibrationClient.h b/Source/WebCore/Modules/vibration/VibrationClient.h new file mode 100644 index 000000000..ebe688927 --- /dev/null +++ b/Source/WebCore/Modules/vibration/VibrationClient.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef VibrationClient_h +#define VibrationClient_h + +namespace WebCore { + +class Page; + +class VibrationClient { +public: + virtual ~VibrationClient() { } + + virtual void vibrate(const unsigned long& time) = 0; + virtual void cancelVibration() = 0; + + virtual void vibrationDestroyed() = 0; +}; + +void provideVibrationTo(Page*, VibrationClient*); + +} // namespace WebCore + +#endif // VibrationClient_h + |