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.

105 lines
3.4KB

  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;
  33. zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
  34. sourceContext.info = this;
  35. sourceContext.perform = runLoopSourceCallback;
  36. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  37. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  38. }
  39. ~MessageQueue()
  40. {
  41. CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  42. CFRunLoopSourceInvalidate (runLoopSource);
  43. CFRelease (runLoopSource);
  44. }
  45. void post (MessageManager::MessageBase* const message)
  46. {
  47. messages.add (message);
  48. CFRunLoopSourceSignal (runLoopSource);
  49. CFRunLoopWakeUp (runLoop);
  50. }
  51. private:
  52. ReferenceCountedArray <MessageManager::MessageBase, CriticalSection> messages;
  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. {
  62. JUCE_TRY
  63. {
  64. nextMessage->messageCallback();
  65. }
  66. JUCE_CATCH_EXCEPTION
  67. }
  68. return true;
  69. }
  70. void runLoopCallback()
  71. {
  72. for (int i = 4; --i >= 0;)
  73. if (! deliverNextMessage())
  74. return;
  75. CFRunLoopSourceSignal (runLoopSource);
  76. CFRunLoopWakeUp (runLoop);
  77. }
  78. static void runLoopSourceCallback (void* info)
  79. {
  80. static_cast <MessageQueue*> (info)->runLoopCallback();
  81. }
  82. };
  83. #endif // __JUCE_OSX_MESSAGEQUEUE_JUCEHEADER__