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.

221 lines
5.9KB

  1. /*
  2. * Carla Bridge Toolkit, Plugin version
  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 "CarlaBridgeUI.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include "CarlaPluginUI.hpp"
  20. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  21. # include "juce_events.h"
  22. using juce::MessageManager;
  23. using juce::ScopedJuceInitialiser_GUI;
  24. #endif
  25. CARLA_BRIDGE_START_NAMESPACE
  26. // -------------------------------------------------------------------------
  27. class CarlaBridgeToolkitPlugin : public CarlaBridgeToolkit,
  28. private CarlaPluginUI::CloseCallback
  29. {
  30. public:
  31. CarlaBridgeToolkitPlugin(CarlaBridgeUI* const ui)
  32. : CarlaBridgeToolkit(ui),
  33. fUI(nullptr),
  34. fIdling(false),
  35. leakDetector_CarlaBridgeToolkitPlugin()
  36. {
  37. carla_debug("CarlaBridgeToolkitPlugin::CarlaBridgeToolkitPlugin(%p)", ui);
  38. }
  39. ~CarlaBridgeToolkitPlugin() override
  40. {
  41. CARLA_SAFE_ASSERT_RETURN(fUI == nullptr,);
  42. carla_debug("CarlaBridgeToolkitPlugin::~CarlaBridgeToolkitPlugin()");
  43. }
  44. bool init(const int /*argc*/, const char** /*argv[]*/) override
  45. {
  46. CARLA_SAFE_ASSERT_RETURN(fUI == nullptr, false);
  47. carla_debug("CarlaBridgeToolkitPlugin::init()");
  48. const CarlaBridgeUI::Options& options(ui->getOptions());
  49. #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA)
  50. fUI = CarlaPluginUI::newCocoa(this, 0, options.isResizable);
  51. #elif defined(CARLA_OS_WIN) && defined(BRIDGE_HWND)
  52. fUI = CarlaPluginUI::newWindows(this, 0, options.isResizable);
  53. #elif defined(HAVE_X11) && defined(BRIDGE_X11)
  54. fUI = CarlaPluginUI::newX11(this, 0, options.isResizable);
  55. #endif
  56. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr, false);
  57. fUI->setTitle(options.windowTitle.buffer());
  58. fUI->setTransientWinId(options.transientWindowId);
  59. return true;
  60. }
  61. void exec(const bool showUI) override
  62. {
  63. CARLA_SAFE_ASSERT_RETURN(ui != nullptr,);
  64. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  65. carla_debug("CarlaBridgeToolkitPlugin::exec(%s)", bool2str(showUI));
  66. if (const char* const winIdStr = std::getenv("ENGINE_OPTION_FRONTEND_WIN_ID"))
  67. {
  68. if (const long long winId = std::strtoll(winIdStr, nullptr, 16))
  69. fUI->setTransientWinId(static_cast<uintptr_t>(winId));
  70. }
  71. if (showUI)
  72. fUI->show();
  73. fIdling = true;
  74. for (; fIdling;)
  75. {
  76. if (ui->isPipeRunning())
  77. ui->idlePipe();
  78. ui->idleUI();
  79. fUI->idle();
  80. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  81. if (MessageManager* const msgMgr = MessageManager::getInstance())
  82. msgMgr->runDispatchLoopUntil(20);
  83. #else
  84. carla_msleep(20);
  85. #endif
  86. }
  87. }
  88. void quit() override
  89. {
  90. carla_debug("CarlaBridgeToolkitPlugin::quit()");
  91. fIdling = false;
  92. if (fUI != nullptr)
  93. {
  94. fUI->hide();
  95. delete fUI;
  96. fUI = nullptr;
  97. }
  98. }
  99. void show() override
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  102. carla_debug("CarlaBridgeToolkitPlugin::show()");
  103. fUI->show();
  104. }
  105. void focus() override
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  108. carla_debug("CarlaBridgeToolkitPlugin::focus()");
  109. fUI->focus();
  110. }
  111. void hide() override
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  114. carla_debug("CarlaBridgeToolkitPlugin::hide()");
  115. fUI->hide();
  116. }
  117. void setSize(const uint width, const uint height) override
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  120. CARLA_SAFE_ASSERT_RETURN(width > 0,);
  121. CARLA_SAFE_ASSERT_RETURN(height > 0,);
  122. carla_debug("CarlaBridgeToolkitPlugin::resize(%i, %i)", width, height);
  123. fUI->setSize(width, height, false);
  124. }
  125. void setTitle(const char* const title) override
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr,);
  128. carla_debug("CarlaBridgeToolkitPlugin::setTitle(\"%s\")", title);
  129. fUI->setTitle(title);
  130. }
  131. void* getContainerId() const override
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr, nullptr);
  134. return fUI->getPtr();
  135. }
  136. #ifdef HAVE_X11
  137. void* getContainerId2() const override
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(fUI != nullptr, nullptr);
  140. return fUI->getDisplay();
  141. }
  142. #endif
  143. // ---------------------------------------------------------------------
  144. protected:
  145. void handlePluginUIClosed() override
  146. {
  147. fIdling = false;
  148. }
  149. void handlePluginUIResized(uint,uint) override
  150. {
  151. }
  152. // ---------------------------------------------------------------------
  153. private:
  154. CarlaPluginUI* fUI;
  155. bool fIdling;
  156. #if defined(CARLA_OS_WIN) || defined(CARLA_OS_MAC)
  157. const ScopedJuceInitialiser_GUI kJuceInit;
  158. #endif
  159. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitPlugin)
  160. };
  161. // -------------------------------------------------------------------------
  162. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeUI* const ui)
  163. {
  164. return new CarlaBridgeToolkitPlugin(ui);
  165. }
  166. // -------------------------------------------------------------------------
  167. CARLA_BRIDGE_END_NAMESPACE
  168. #define CARLA_PLUGIN_UI_WITHOUT_JUCE_PROCESSORS
  169. #include "CarlaPluginUI.cpp"
  170. // -------------------------------------------------------------------------