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.

231 lines
6.4KB

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