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.

CarlaJuceEvents.cpp 3.9KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. fInitializing = true;
  60. if (MessageManager* const msgMgr = MessageManager::getInstance())
  61. msgMgr->setCurrentThreadAsMessageThread();
  62. fInitializing = false;
  63. for (; ! threadShouldExit();)
  64. {
  65. // dispatch messages until no more present, then sleep
  66. for (; dispatchNextInternalMessage();) {}
  67. sleep(25);
  68. }
  69. }
  70. private:
  71. bool fInitializing;
  72. CriticalSection fLock;
  73. ReferenceCountedArray<MessageManager::MessageBase> fQueue;
  74. MessageManager::MessageBase::Ptr popNextMessage()
  75. {
  76. const ScopedLock sl(fLock);
  77. return fQueue.removeAndReturn(0);
  78. }
  79. bool dispatchNextInternalMessage()
  80. {
  81. if (const MessageManager::MessageBase::Ptr msg = popNextMessage())
  82. {
  83. JUCE_TRY
  84. {
  85. msg->messageCallback();
  86. return true;
  87. }
  88. JUCE_CATCH_EXCEPTION
  89. }
  90. return false;
  91. }
  92. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JuceEventsThread)
  93. };
  94. static JuceEventsThread& getJuceEventsThreadInstance()
  95. {
  96. static JuceEventsThread sJuceEventsThread;
  97. return sJuceEventsThread;
  98. }
  99. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
  100. void MessageManager::doPlatformSpecificInitialisation()
  101. {
  102. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  103. if (! juceEventsThread.isInitializing())
  104. juceEventsThread.startThread();
  105. }
  106. void MessageManager::doPlatformSpecificShutdown()
  107. {
  108. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  109. if (! juceEventsThread.isInitializing())
  110. juceEventsThread.stopThread(-1);
  111. }
  112. bool MessageManager::postMessageToSystemQueue(MessageManager::MessageBase* const message)
  113. {
  114. JuceEventsThread& juceEventsThread(getJuceEventsThreadInstance());
  115. return juceEventsThread.postMessage(message);
  116. }
  117. } // namespace juce
  118. #endif // ! CARLA_OS_MAC || CARLA_OS_WIN