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.

161 lines
4.5KB

  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/juce_audio_processors.h"
  22. #include "juce_events/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_AudioChannelSet.cpp"
  29. #include "juce_audio_processors/processors/juce_AudioProcessor.cpp"
  30. #include "juce_audio_processors/processors/juce_AudioProcessorGraph.cpp"
  31. class JuceEventsThread : public Thread
  32. {
  33. public:
  34. JuceEventsThread()
  35. : Thread("JuceEventsThread"),
  36. fInitializing(false),
  37. fLock(),
  38. fQueue() {}
  39. ~JuceEventsThread()
  40. {
  41. signalThreadShouldExit();
  42. stopThread(2000);
  43. const ScopedLock sl(fLock);
  44. CARLA_SAFE_ASSERT(fQueue.size() == 0);
  45. fQueue.clear();
  46. }
  47. bool postMessage(MessageManager::MessageBase* const msg)
  48. {
  49. const ScopedLock sl(fLock);
  50. fQueue.add(msg);
  51. return true;
  52. }
  53. bool isInitializing() const noexcept
  54. {
  55. return fInitializing;
  56. }
  57. protected:
  58. void run() override
  59. {
  60. /*
  61. * We need to know when we're initializing because MessageManager::setCurrentThreadAsMessageThread()
  62. * calls doPlatformSpecificInitialisation/Shutdown, in which we started this thread previously.
  63. * To avoid a deadlock we do not call start/stopThread if still initializing.
  64. */
  65. fInitializing = true;
  66. if (MessageManager* const msgMgr = MessageManager::getInstance())
  67. msgMgr->setCurrentThreadAsMessageThread();
  68. fInitializing = false;
  69. for (; ! threadShouldExit();)
  70. {
  71. // dispatch messages until no more present, then sleep
  72. for (; dispatchNextInternalMessage();) {}
  73. sleep(25);
  74. }
  75. }
  76. private:
  77. volatile bool fInitializing;
  78. CriticalSection fLock;
  79. ReferenceCountedArray<MessageManager::MessageBase> fQueue;
  80. MessageManager::MessageBase::Ptr popNextMessage()
  81. {
  82. const ScopedLock sl(fLock);
  83. return fQueue.removeAndReturn(0);
  84. }
  85. bool dispatchNextInternalMessage()
  86. {
  87. if (const MessageManager::MessageBase::Ptr msg = popNextMessage())
  88. {
  89. JUCE_TRY
  90. {
  91. msg->messageCallback();
  92. return true;
  93. }
  94. JUCE_CATCH_EXCEPTION
  95. }
  96. return false;
  97. }
  98. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JuceEventsThread)
  99. };
  100. static JuceEventsThread& getJuceEventsThreadInstance()
  101. {
  102. static JuceEventsThread sJuceEventsThread;
  103. return sJuceEventsThread;
  104. }
  105. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
  106. void MessageManager::doPlatformSpecificInitialisation()
  107. {
  108. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  109. if (! juceEventsThread.isInitializing())
  110. juceEventsThread.startThread();
  111. }
  112. void MessageManager::doPlatformSpecificShutdown()
  113. {
  114. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  115. if (! juceEventsThread.isInitializing())
  116. juceEventsThread.stopThread(-1);
  117. }
  118. bool MessageManager::postMessageToSystemQueue(MessageManager::MessageBase* const message)
  119. {
  120. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  121. return juceEventsThread.postMessage(message);
  122. }
  123. bool MessageManager::dispatchNextMessageOnSystemQueue(bool)
  124. {
  125. carla_stderr2("MessageManager::dispatchNextMessageOnSystemQueue() unsupported");
  126. return false;
  127. }
  128. } // namespace juce
  129. #endif // ! CARLA_OS_MAC || CARLA_OS_WIN