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.

104 lines
3.3KB

  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 (MessageManager::MessageBase* const message)
  45. {
  46. messages.add (message);
  47. CFRunLoopSourceSignal (runLoopSource);
  48. CFRunLoopWakeUp (runLoop);
  49. }
  50. private:
  51. ReferenceCountedArray <MessageManager::MessageBase, CriticalSection> messages;
  52. CriticalSection lock;
  53. CFRunLoopRef runLoop;
  54. CFRunLoopSourceRef runLoopSource;
  55. bool deliverNextMessage()
  56. {
  57. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  58. if (nextMessage == nullptr)
  59. return false;
  60. JUCE_AUTORELEASEPOOL
  61. JUCE_TRY
  62. {
  63. nextMessage->messageCallback();
  64. }
  65. JUCE_CATCH_EXCEPTION
  66. return true;
  67. }
  68. void runLoopCallback()
  69. {
  70. for (int i = 4; --i >= 0;)
  71. if (! deliverNextMessage())
  72. return;
  73. CFRunLoopSourceSignal (runLoopSource);
  74. CFRunLoopWakeUp (runLoop);
  75. }
  76. static void runLoopSourceCallback (void* info)
  77. {
  78. static_cast <MessageQueue*> (info)->runLoopCallback();
  79. }
  80. };
  81. #endif // __JUCE_OSX_MESSAGEQUEUE_JUCEHEADER__