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.

234 lines
6.3KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "CarlaBridgeFormat.hpp"
  4. #include "CarlaBridgeToolkit.hpp"
  5. #include "CarlaMainLoop.hpp"
  6. #include "CarlaPluginUI.hpp"
  7. #include "CarlaUtils.h"
  8. #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA)
  9. # include "CarlaMacUtils.hpp"
  10. #endif
  11. #include "extra/Sleep.hpp"
  12. #if defined(HAVE_X11) && defined(BRIDGE_X11)
  13. # include <X11/Xlib.h>
  14. #endif
  15. CARLA_BRIDGE_UI_START_NAMESPACE
  16. using CARLA_BACKEND_NAMESPACE::runMainLoopOnce;
  17. // -------------------------------------------------------------------------
  18. class CarlaBridgeToolkitNative : public CarlaBridgeToolkit,
  19. private CarlaPluginUI::Callback
  20. {
  21. public:
  22. CarlaBridgeToolkitNative(CarlaBridgeFormat* const format)
  23. : CarlaBridgeToolkit(format),
  24. fHostUI(nullptr),
  25. fIdling(false)
  26. {
  27. carla_debug("CarlaBridgeToolkitNative::CarlaBridgeToolkitNative(%p)", format);
  28. }
  29. ~CarlaBridgeToolkitNative() override
  30. {
  31. CARLA_SAFE_ASSERT_RETURN(fHostUI == nullptr,);
  32. carla_debug("CarlaBridgeToolkitNative::~CarlaBridgeToolkitNative()");
  33. }
  34. bool init(const int /*argc*/, const char** /*argv[]*/) override
  35. {
  36. CARLA_SAFE_ASSERT_RETURN(fHostUI == nullptr, false);
  37. carla_debug("CarlaBridgeToolkitNative::init()");
  38. const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
  39. #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA)
  40. CARLA_BACKEND_NAMESPACE::initStandaloneApplication();
  41. fHostUI = CarlaPluginUI::newCocoa(this, 0, options.isStandalone, options.isResizable);
  42. #elif defined(CARLA_OS_WIN) && defined(BRIDGE_HWND)
  43. fHostUI = CarlaPluginUI::newWindows(this, 0, options.isStandalone, options.isResizable);
  44. #elif defined(HAVE_X11) && defined(BRIDGE_X11)
  45. XInitThreads();
  46. fHostUI = CarlaPluginUI::newX11(this, 0, options.isStandalone, options.isResizable, true);
  47. #endif
  48. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, false);
  49. #ifndef CARLA_OS_MAC
  50. if (options.isStandalone)
  51. fPlugin->setScaleFactor(carla_get_desktop_scale_factor());
  52. #endif
  53. fHostUI->setTitle(options.windowTitle.buffer());
  54. #ifndef CARLA_OS_MAC
  55. if (options.transientWindowId != 0)
  56. {
  57. fHostUI->setTransientWinId(options.transientWindowId);
  58. }
  59. else if (const char* const winIdStr = std::getenv("ENGINE_OPTION_FRONTEND_WIN_ID"))
  60. {
  61. if (const long long winId = std::strtoll(winIdStr, nullptr, 16))
  62. fHostUI->setTransientWinId(static_cast<uintptr_t>(winId));
  63. }
  64. #endif
  65. return true;
  66. }
  67. void exec(const bool showUI) override
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  70. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  71. carla_debug("CarlaBridgeToolkitNative::exec(%s)", bool2str(showUI));
  72. if (showUI)
  73. {
  74. fHostUI->show();
  75. fHostUI->focus();
  76. }
  77. fIdling = true;
  78. for (; runMainLoopOnce() && fIdling;)
  79. {
  80. if (fPlugin->isPipeRunning())
  81. fPlugin->idlePipe();
  82. fPlugin->idleUI();
  83. fHostUI->idle();
  84. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  85. // MacOS and Win32 have event-loops to run, so minimize sleep time
  86. d_msleep(1);
  87. #else
  88. d_msleep(33);
  89. #endif
  90. }
  91. }
  92. void quit() override
  93. {
  94. carla_debug("CarlaBridgeToolkitNative::quit()");
  95. fIdling = false;
  96. if (fHostUI != nullptr)
  97. {
  98. fHostUI->hide();
  99. delete fHostUI;
  100. fHostUI = nullptr;
  101. }
  102. }
  103. void show() override
  104. {
  105. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  106. carla_debug("CarlaBridgeToolkitNative::show()");
  107. fHostUI->show();
  108. }
  109. void focus() override
  110. {
  111. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  112. carla_debug("CarlaBridgeToolkitNative::focus()");
  113. fHostUI->focus();
  114. }
  115. void hide() override
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  118. carla_debug("CarlaBridgeToolkitNative::hide()");
  119. fHostUI->hide();
  120. }
  121. void setChildWindow(void* const ptr) override
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  124. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  125. carla_debug("CarlaBridgeToolkitNative::setChildWindow(%p)", ptr);
  126. fHostUI->setChildWindow(ptr);
  127. }
  128. void setSize(const uint width, const uint height) override
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  131. CARLA_SAFE_ASSERT_RETURN(width > 0,);
  132. CARLA_SAFE_ASSERT_RETURN(height > 0,);
  133. carla_debug("CarlaBridgeToolkitNative::resize(%i, %i)", width, height);
  134. fHostUI->setSize(width, height, false, false);
  135. }
  136. void setTitle(const char* const title) override
  137. {
  138. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  139. carla_debug("CarlaBridgeToolkitNative::setTitle(\"%s\")", title);
  140. fHostUI->setTitle(title);
  141. }
  142. void* getContainerId() const override
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  145. return fHostUI->getPtr();
  146. }
  147. #ifdef HAVE_X11
  148. void* getContainerId2() const override
  149. {
  150. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  151. return fHostUI->getDisplay();
  152. }
  153. #endif
  154. // ---------------------------------------------------------------------
  155. protected:
  156. void handlePluginUIClosed() override
  157. {
  158. fIdling = false;
  159. }
  160. void handlePluginUIResized(const uint width, const uint height) override
  161. {
  162. fPlugin->uiResized(width, height);
  163. }
  164. // ---------------------------------------------------------------------
  165. private:
  166. CarlaPluginUI* fHostUI;
  167. bool fIdling;
  168. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitNative)
  169. };
  170. // -------------------------------------------------------------------------
  171. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
  172. {
  173. return new CarlaBridgeToolkitNative(format);
  174. }
  175. // -------------------------------------------------------------------------
  176. CARLA_BRIDGE_UI_END_NAMESPACE
  177. #define CARLA_PLUGIN_UI_CLASS_PREFIX ToolkitNative
  178. #include "CarlaPluginUI.cpp"
  179. #include "utils/Windows.cpp"
  180. // -------------------------------------------------------------------------