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.

247 lines
6.9KB

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