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.

116 lines
2.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #if JUCE_LINUX || JUCE_BSD
  19. namespace juce::detail
  20. {
  21. // Implemented in juce_Messaging_linux.cpp
  22. bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages);
  23. class MessageThread : public Thread
  24. {
  25. public:
  26. MessageThread() : Thread ("JUCE Plugin Message Thread")
  27. {
  28. start();
  29. }
  30. ~MessageThread() override
  31. {
  32. MessageManager::getInstance()->stopDispatchLoop();
  33. stop();
  34. }
  35. void start()
  36. {
  37. startThread (Priority::high);
  38. // Wait for setCurrentThreadAsMessageThread() and getInstance to be executed
  39. // before leaving this method
  40. threadInitialised.wait (10000);
  41. }
  42. void stop()
  43. {
  44. signalThreadShouldExit();
  45. stopThread (-1);
  46. }
  47. bool isRunning() const noexcept { return isThreadRunning(); }
  48. void run() override
  49. {
  50. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  51. XWindowSystem::getInstance();
  52. threadInitialised.signal();
  53. while (! threadShouldExit())
  54. {
  55. if (! dispatchNextMessageOnSystemQueue (true))
  56. Thread::sleep (1);
  57. }
  58. }
  59. private:
  60. WaitableEvent threadInitialised;
  61. JUCE_DECLARE_NON_MOVEABLE (MessageThread)
  62. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageThread)
  63. };
  64. //==============================================================================
  65. class HostDrivenEventLoop
  66. {
  67. public:
  68. HostDrivenEventLoop()
  69. {
  70. messageThread->stop();
  71. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  72. }
  73. void processPendingEvents()
  74. {
  75. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  76. for (;;)
  77. if (! dispatchNextMessageOnSystemQueue (true))
  78. return;
  79. }
  80. ~HostDrivenEventLoop()
  81. {
  82. messageThread->start();
  83. }
  84. private:
  85. SharedResourcePointer<MessageThread> messageThread;
  86. };
  87. } // namespace juce::detail
  88. #endif