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.

103 lines
3.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. #pragma once
  18. //==============================================================================
  19. /* An internal message pump class used in OSX and iOS. */
  20. class MessageQueue
  21. {
  22. public:
  23. MessageQueue()
  24. {
  25. #if JUCE_IOS
  26. runLoop = CFRunLoopGetCurrent();
  27. #else
  28. runLoop = CFRunLoopGetMain();
  29. #endif
  30. CFRunLoopSourceContext sourceContext;
  31. zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
  32. sourceContext.info = this;
  33. sourceContext.perform = runLoopSourceCallback;
  34. runLoopSource = CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext);
  35. CFRunLoopAddSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  36. }
  37. ~MessageQueue() noexcept
  38. {
  39. CFRunLoopRemoveSource (runLoop, runLoopSource, kCFRunLoopCommonModes);
  40. CFRunLoopSourceInvalidate (runLoopSource);
  41. CFRelease (runLoopSource);
  42. }
  43. void post (MessageManager::MessageBase* const message)
  44. {
  45. messages.add (message);
  46. wakeUp();
  47. }
  48. private:
  49. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
  50. CFRunLoopRef runLoop;
  51. CFRunLoopSourceRef runLoopSource;
  52. void wakeUp() noexcept
  53. {
  54. CFRunLoopSourceSignal (runLoopSource);
  55. CFRunLoopWakeUp (runLoop);
  56. }
  57. bool deliverNextMessage()
  58. {
  59. const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
  60. if (nextMessage == nullptr)
  61. return false;
  62. JUCE_AUTORELEASEPOOL
  63. {
  64. JUCE_TRY
  65. {
  66. nextMessage->messageCallback();
  67. }
  68. JUCE_CATCH_EXCEPTION
  69. }
  70. return true;
  71. }
  72. void runLoopCallback() noexcept
  73. {
  74. for (int i = 4; --i >= 0;)
  75. if (! deliverNextMessage())
  76. return;
  77. wakeUp();
  78. }
  79. static void runLoopSourceCallback (void* info) noexcept
  80. {
  81. static_cast<MessageQueue*> (info)->runLoopCallback();
  82. }
  83. };