The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class MultiTimer::MultiTimerCallback : public Timer
  21. {
  22. public:
  23. MultiTimerCallback (const int timerId_, MultiTimer& owner_)
  24. : timerId (timerId_),
  25. owner (owner_)
  26. {
  27. }
  28. void timerCallback()
  29. {
  30. owner.timerCallback (timerId);
  31. }
  32. const int timerId;
  33. private:
  34. MultiTimer& owner;
  35. };
  36. //==============================================================================
  37. MultiTimer::MultiTimer() noexcept
  38. {
  39. }
  40. MultiTimer::MultiTimer (const MultiTimer&) noexcept
  41. {
  42. }
  43. MultiTimer::~MultiTimer()
  44. {
  45. const SpinLock::ScopedLockType sl (timerListLock);
  46. timers.clear();
  47. }
  48. //==============================================================================
  49. void MultiTimer::startTimer (const int timerId, const int intervalInMilliseconds) noexcept
  50. {
  51. const SpinLock::ScopedLockType sl (timerListLock);
  52. for (int i = timers.size(); --i >= 0;)
  53. {
  54. MultiTimerCallback* const t = timers.getUnchecked(i);
  55. if (t->timerId == timerId)
  56. {
  57. t->startTimer (intervalInMilliseconds);
  58. return;
  59. }
  60. }
  61. MultiTimerCallback* const newTimer = new MultiTimerCallback (timerId, *this);
  62. timers.add (newTimer);
  63. newTimer->startTimer (intervalInMilliseconds);
  64. }
  65. void MultiTimer::stopTimer (const int timerId) noexcept
  66. {
  67. const SpinLock::ScopedLockType sl (timerListLock);
  68. for (int i = timers.size(); --i >= 0;)
  69. {
  70. MultiTimerCallback* const t = timers.getUnchecked(i);
  71. if (t->timerId == timerId)
  72. t->stopTimer();
  73. }
  74. }
  75. bool MultiTimer::isTimerRunning (const int timerId) const noexcept
  76. {
  77. const SpinLock::ScopedLockType sl (timerListLock);
  78. for (int i = timers.size(); --i >= 0;)
  79. {
  80. const MultiTimerCallback* const t = timers.getUnchecked(i);
  81. if (t->timerId == timerId)
  82. return t->isTimerRunning();
  83. }
  84. return false;
  85. }
  86. int MultiTimer::getTimerInterval (const int timerId) const noexcept
  87. {
  88. const SpinLock::ScopedLockType sl (timerListLock);
  89. for (int i = timers.size(); --i >= 0;)
  90. {
  91. const MultiTimerCallback* const t = timers.getUnchecked(i);
  92. if (t->timerId == timerId)
  93. return t->getTimerInterval();
  94. }
  95. return 0;
  96. }
  97. END_JUCE_NAMESPACE