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.

136 lines
5.2KB

  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. #ifndef __JUCE_TIMER_JUCEHEADER__
  19. #define __JUCE_TIMER_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Makes repeated callbacks to a virtual method at a specified time interval.
  23. A Timer's timerCallback() method will be repeatedly called at a given
  24. interval. When you create a Timer object, it will do nothing until the
  25. startTimer() method is called, which will cause the message thread to
  26. start making callbacks at the specified interval, until stopTimer() is called
  27. or the object is deleted.
  28. The time interval isn't guaranteed to be precise to any more than maybe
  29. 10-20ms, and the intervals may end up being much longer than requested if the
  30. system is busy. Because the callbacks are made by the main message thread,
  31. anything that blocks the message queue for a period of time will also prevent
  32. any timers from running until it can carry on.
  33. If you need to have a single callback that is shared by multiple timers with
  34. different frequencies, then the MultiTimer class allows you to do that - its
  35. structure is very similar to the Timer class, but contains multiple timers
  36. internally, each one identified by an ID number.
  37. @see MultiTimer
  38. */
  39. class JUCE_API Timer
  40. {
  41. protected:
  42. //==============================================================================
  43. /** Creates a Timer.
  44. When created, the timer is stopped, so use startTimer() to get it going.
  45. */
  46. Timer() noexcept;
  47. /** Creates a copy of another timer.
  48. Note that this timer won't be started, even if the one you're copying
  49. is running.
  50. */
  51. Timer (const Timer& other) noexcept;
  52. public:
  53. //==============================================================================
  54. /** Destructor. */
  55. virtual ~Timer();
  56. //==============================================================================
  57. /** The user-defined callback routine that actually gets called periodically.
  58. It's perfectly ok to call startTimer() or stopTimer() from within this
  59. callback to change the subsequent intervals.
  60. */
  61. virtual void timerCallback() = 0;
  62. //==============================================================================
  63. /** Starts the timer and sets the length of interval required.
  64. If the timer is already started, this will reset it, so the
  65. time between calling this method and the next timer callback
  66. will not be less than the interval length passed in.
  67. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  68. rounded up to 1)
  69. */
  70. void startTimer (int intervalInMilliseconds) noexcept;
  71. /** Stops the timer.
  72. No more callbacks will be made after this method returns.
  73. If this is called from a different thread, any callbacks that may
  74. be currently executing may be allowed to finish before the method
  75. returns.
  76. */
  77. void stopTimer() noexcept;
  78. //==============================================================================
  79. /** Checks if the timer has been started.
  80. @returns true if the timer is running.
  81. */
  82. bool isTimerRunning() const noexcept { return periodMs > 0; }
  83. /** Returns the timer's interval.
  84. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  85. */
  86. int getTimerInterval() const noexcept { return periodMs; }
  87. //==============================================================================
  88. /** For internal use only: invokes any timers that need callbacks.
  89. Don't call this unless you really know what you're doing!
  90. */
  91. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  92. private:
  93. class TimerThread;
  94. friend class TimerThread;
  95. int countdownMs, periodMs;
  96. Timer* previous;
  97. Timer* next;
  98. Timer& operator= (const Timer&);
  99. };
  100. #endif // __JUCE_TIMER_JUCEHEADER__