Audio plugin host https://kx.studio/carla
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.

106 lines
3.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /* An internal message pump class used in OSX and iOS. */
  21. class MessageQueue
  22. {
  23. public:
  24. MessageQueue()
  25. {
  26. #if JUCE_IOS
  27. runLoop = CFRunLoopGetCurrent();
  28. #else
  29. runLoop = CFRunLoopGetMain();
  30. #endif
  31. CFRunLoopSourceContext sourceContext;
  32. zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
  33. sourceContext.info = this;
  34. sourceContext.perform = runLoopSourceCallback;
  35. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  36. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  37. }
  38. ~MessageQueue() noexcept
  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. wakeUp();
  48. }
  49. private:
  50. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
  51. CFRunLoopRef runLoop;
  52. CFRunLoopSourceRef runLoopSource;
  53. void wakeUp() noexcept
  54. {
  55. CFRunLoopSourceSignal (runLoopSource);
  56. CFRunLoopWakeUp (runLoop);
  57. }
  58. bool deliverNextMessage()
  59. {
  60. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  61. if (nextMessage == nullptr)
  62. return false;
  63. JUCE_AUTORELEASEPOOL
  64. {
  65. JUCE_TRY
  66. {
  67. nextMessage->messageCallback();
  68. }
  69. JUCE_CATCH_EXCEPTION
  70. }
  71. return true;
  72. }
  73. void runLoopCallback() noexcept
  74. {
  75. for (int i = 4; --i >= 0;)
  76. if (! deliverNextMessage())
  77. return;
  78. wakeUp();
  79. }
  80. static void runLoopSourceCallback (void* info) noexcept
  81. {
  82. static_cast<MessageQueue*> (info)->runLoopCallback();
  83. }
  84. };
  85. } // namespace juce