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.

98 lines
3.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_OSX_MESSAGEQUEUE_JUCEHEADER__
  19. #define __JUCE_OSX_MESSAGEQUEUE_JUCEHEADER__
  20. //==============================================================================
  21. /* An internal message pump class used in OSX and iOS. */
  22. class MessageQueue
  23. {
  24. public:
  25. MessageQueue()
  26. {
  27. #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 && ! JUCE_IOS
  28. runLoop = CFRunLoopGetMain();
  29. #else
  30. runLoop = CFRunLoopGetCurrent();
  31. #endif
  32. CFRunLoopSourceContext sourceContext = { 0 };
  33. sourceContext.info = this;
  34. sourceContext.perform = runLoopSourceCallback;
  35. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  36. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  37. }
  38. ~MessageQueue()
  39. {
  40. CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  41. CFRunLoopSourceInvalidate (runLoopSource);
  42. CFRelease (runLoopSource);
  43. }
  44. void post (Message* const message)
  45. {
  46. messages.add (message);
  47. CFRunLoopSourceSignal (runLoopSource);
  48. CFRunLoopWakeUp (runLoop);
  49. }
  50. private:
  51. ReferenceCountedArray <Message, CriticalSection> messages;
  52. CriticalSection lock;
  53. CFRunLoopRef runLoop;
  54. CFRunLoopSourceRef runLoopSource;
  55. bool deliverNextMessage()
  56. {
  57. const Message::Ptr nextMessage (messages.removeAndReturn (0));
  58. if (nextMessage == nullptr)
  59. return false;
  60. JUCE_AUTORELEASEPOOL
  61. MessageManager::getInstance()->deliverMessage (nextMessage);
  62. return true;
  63. }
  64. void runLoopCallback()
  65. {
  66. for (int i = 4; --i >= 0;)
  67. if (! deliverNextMessage())
  68. return;
  69. CFRunLoopSourceSignal (runLoopSource);
  70. CFRunLoopWakeUp (runLoop);
  71. }
  72. static void runLoopSourceCallback (void* info)
  73. {
  74. static_cast <MessageQueue*> (info)->runLoopCallback();
  75. }
  76. };
  77. #endif // __JUCE_OSX_MESSAGEQUEUE_JUCEHEADER__