Vibration.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "config.h"
  20. #include "Vibration.h"
  21. #if ENABLE(VIBRATION)
  22. #include "VibrationClient.h"
  23. namespace WebCore {
  24. Vibration::Vibration(VibrationClient* client)
  25. : m_vibrationClient(client)
  26. , m_timerStart(this, &Vibration::timerStartFired)
  27. , m_timerStop(this, &Vibration::timerStopFired)
  28. , m_isVibrating(false)
  29. {
  30. }
  31. Vibration::~Vibration()
  32. {
  33. m_vibrationClient->vibrationDestroyed();
  34. }
  35. PassOwnPtr<Vibration> Vibration::create(VibrationClient* client)
  36. {
  37. return adoptPtr(new Vibration(client));
  38. }
  39. void Vibration::vibrate(const unsigned& time)
  40. {
  41. if (!time) {
  42. cancelVibration();
  43. return;
  44. }
  45. m_pattern.append(time);
  46. m_timerStart.startOneShot(0);
  47. }
  48. void Vibration::vibrate(const VibrationPattern& pattern)
  49. {
  50. int length = pattern.size();
  51. // Cancel the pre-existing instance of vibration patterns, if the pattern is 0 or an empty list.
  52. if (!length || (length == 1 && !pattern[0])) {
  53. cancelVibration();
  54. return;
  55. }
  56. if (m_isVibrating)
  57. cancelVibration();
  58. if (m_timerStart.isActive())
  59. m_timerStart.stop();
  60. m_pattern = pattern;
  61. m_timerStart.startOneShot(0);
  62. }
  63. void Vibration::cancelVibration()
  64. {
  65. m_pattern.clear();
  66. if (m_isVibrating) {
  67. m_vibrationClient->cancelVibration();
  68. m_isVibrating = false;
  69. m_timerStop.stop();
  70. }
  71. }
  72. void Vibration::suspendVibration()
  73. {
  74. if (!m_isVibrating)
  75. return;
  76. m_pattern.insert(0, m_timerStop.nextFireInterval());
  77. m_timerStop.stop();
  78. cancelVibration();
  79. }
  80. void Vibration::resumeVibration()
  81. {
  82. m_timerStart.startOneShot(0);
  83. }
  84. void Vibration::timerStartFired(Timer<Vibration>* timer)
  85. {
  86. ASSERT_UNUSED(timer, timer == &m_timerStart);
  87. m_timerStart.stop();
  88. if (m_pattern.size()) {
  89. m_isVibrating = true;
  90. m_vibrationClient->vibrate(m_pattern[0]);
  91. m_timerStop.startOneShot(m_pattern[0] / 1000.0);
  92. m_pattern.remove(0);
  93. }
  94. }
  95. void Vibration::timerStopFired(Timer<Vibration>* timer)
  96. {
  97. ASSERT_UNUSED(timer, timer == &m_timerStop);
  98. m_timerStop.stop();
  99. m_isVibrating = false;
  100. if (m_pattern.size()) {
  101. m_timerStart.startOneShot(m_pattern[0] / 1000.0);
  102. m_pattern.remove(0);
  103. }
  104. }
  105. const char* Vibration::supplementName()
  106. {
  107. return "Vibration";
  108. }
  109. bool Vibration::isActive(Page* page)
  110. {
  111. return static_cast<bool>(Vibration::from(page));
  112. }
  113. void provideVibrationTo(Page* page, VibrationClient* client)
  114. {
  115. Vibration::provideTo(page, Vibration::supplementName(), Vibration::create(client));
  116. }
  117. } // namespace WebCore
  118. #endif // ENABLE(VIBRATION)