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.

160 lines
4.4KB

  1. /*
  2. * Fake juce event thread needed for juce_audio_processors
  3. * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaJuceUtils.hpp"
  18. // -------------------------------------------------------------------------------------------------------------------
  19. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  20. #undef KeyPress
  21. #include "juce_audio_processors.h"
  22. #include "juce_events.h"
  23. namespace juce {
  24. #include "juce_events/broadcasters/juce_ActionBroadcaster.cpp"
  25. #include "juce_events/broadcasters/juce_AsyncUpdater.cpp"
  26. #include "juce_events/messages/juce_DeletedAtShutdown.cpp"
  27. #include "juce_events/messages/juce_MessageManager.cpp"
  28. #include "juce_audio_processors/processors/juce_AudioProcessor.cpp"
  29. #include "juce_audio_processors/processors/juce_AudioProcessorGraph.cpp"
  30. class JuceEventsThread : public Thread
  31. {
  32. public:
  33. JuceEventsThread()
  34. : Thread("JuceEventsThread"),
  35. fInitializing(false),
  36. fLock(),
  37. fQueue(),
  38. leakDetector_JuceEventsThread() {}
  39. ~JuceEventsThread()
  40. {
  41. signalThreadShouldExit();
  42. stopThread(2000);
  43. const ScopedLock sl(fLock);
  44. fQueue.clear();
  45. }
  46. bool postMessage(MessageManager::MessageBase* const msg)
  47. {
  48. const ScopedLock sl(fLock);
  49. fQueue.add(msg);
  50. return true;
  51. }
  52. bool isInitializing() const noexcept
  53. {
  54. return fInitializing;
  55. }
  56. protected:
  57. void run() override
  58. {
  59. /*
  60. * We need to know when we're initializing because MessageManager::setCurrentThreadAsMessageThread()
  61. * calls doPlatformSpecificInitialisation/Shutdown, in which we started this thread previously.
  62. * To avoid a deadlock we do not call start/stopThread if still initializing.
  63. */
  64. fInitializing = true;
  65. if (MessageManager* const msgMgr = MessageManager::getInstance())
  66. msgMgr->setCurrentThreadAsMessageThread();
  67. fInitializing = false;
  68. for (; ! threadShouldExit();)
  69. {
  70. // dispatch messages until no more present, then sleep
  71. for (; dispatchNextInternalMessage();) {}
  72. sleep(25);
  73. }
  74. }
  75. private:
  76. volatile bool fInitializing;
  77. CriticalSection fLock;
  78. ReferenceCountedArray<MessageManager::MessageBase> fQueue;
  79. MessageManager::MessageBase::Ptr popNextMessage()
  80. {
  81. const ScopedLock sl(fLock);
  82. return fQueue.removeAndReturn(0);
  83. }
  84. bool dispatchNextInternalMessage()
  85. {
  86. if (const MessageManager::MessageBase::Ptr msg = popNextMessage())
  87. {
  88. JUCE_TRY
  89. {
  90. msg->messageCallback();
  91. return true;
  92. }
  93. JUCE_CATCH_EXCEPTION
  94. }
  95. return false;
  96. }
  97. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JuceEventsThread)
  98. };
  99. static JuceEventsThread& getJuceEventsThreadInstance()
  100. {
  101. static JuceEventsThread sJuceEventsThread;
  102. return sJuceEventsThread;
  103. }
  104. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
  105. void MessageManager::doPlatformSpecificInitialisation()
  106. {
  107. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  108. if (! juceEventsThread.isInitializing())
  109. juceEventsThread.startThread();
  110. }
  111. void MessageManager::doPlatformSpecificShutdown()
  112. {
  113. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  114. if (! juceEventsThread.isInitializing())
  115. juceEventsThread.stopThread(-1);
  116. }
  117. bool MessageManager::postMessageToSystemQueue(MessageManager::MessageBase* const message)
  118. {
  119. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  120. return juceEventsThread.postMessage(message);
  121. }
  122. bool MessageManager::dispatchNextMessageOnSystemQueue(bool)
  123. {
  124. carla_stderr2("MessageManager::dispatchNextMessageOnSystemQueue() unsupported");
  125. return false;
  126. }
  127. } // namespace juce
  128. #endif // ! CARLA_OS_MAC || CARLA_OS_WIN