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.

121 lines
3.2KB

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