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.

235 lines
6.5KB

  1. /*
  2. * Carla Bridge UI
  3. * Copyright (C) 2014-2018 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. #if (defined(CARLA_OS_WIN) && defined(BRIDGE_HWND)) || (defined(HAVE_X11) && defined(BRIDGE_X11))
  59. if (options.transientWindowId != 0)
  60. {
  61. fHostUI->setTransientWinId(options.transientWindowId);
  62. }
  63. else if (const char* const winIdStr = std::getenv("ENGINE_OPTION_FRONTEND_WIN_ID"))
  64. {
  65. if (const long long winId = std::strtoll(winIdStr, nullptr, 16))
  66. fHostUI->setTransientWinId(static_cast<uintptr_t>(winId));
  67. }
  68. #endif
  69. return true;
  70. }
  71. void exec(const bool showUI) override
  72. {
  73. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  74. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  75. carla_debug("CarlaBridgeToolkitNative::exec(%s)", bool2str(showUI));
  76. if (showUI)
  77. {
  78. fHostUI->show();
  79. fHostUI->focus();
  80. }
  81. fIdling = true;
  82. for (; runMainLoopOnce() && fIdling;)
  83. {
  84. if (fPlugin->isPipeRunning())
  85. fPlugin->idlePipe();
  86. fPlugin->idleUI();
  87. fHostUI->idle();
  88. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  89. // MacOS and Win32 have event-loops to run, so minimize sleep time
  90. carla_msleep(1);
  91. #else
  92. carla_msleep(33);
  93. #endif
  94. }
  95. }
  96. void quit() override
  97. {
  98. carla_debug("CarlaBridgeToolkitNative::quit()");
  99. fIdling = false;
  100. if (fHostUI != nullptr)
  101. {
  102. fHostUI->hide();
  103. delete fHostUI;
  104. fHostUI = nullptr;
  105. }
  106. }
  107. void show() override
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  110. carla_debug("CarlaBridgeToolkitNative::show()");
  111. fHostUI->show();
  112. }
  113. void focus() override
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  116. carla_debug("CarlaBridgeToolkitNative::focus()");
  117. fHostUI->focus();
  118. }
  119. void hide() override
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  122. carla_debug("CarlaBridgeToolkitNative::hide()");
  123. fHostUI->hide();
  124. }
  125. void setChildWindow(void* const ptr) override
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  128. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  129. carla_debug("CarlaBridgeToolkitNative::setChildWindow(%p)", ptr);
  130. fHostUI->setChildWindow(ptr);
  131. }
  132. void setSize(const uint width, const uint height) override
  133. {
  134. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  135. CARLA_SAFE_ASSERT_RETURN(width > 0,);
  136. CARLA_SAFE_ASSERT_RETURN(height > 0,);
  137. carla_debug("CarlaBridgeToolkitNative::resize(%i, %i)", width, height);
  138. fHostUI->setSize(width, height, false);
  139. }
  140. void setTitle(const char* const title) override
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  143. carla_debug("CarlaBridgeToolkitNative::setTitle(\"%s\")", title);
  144. fHostUI->setTitle(title);
  145. }
  146. void* getContainerId() const override
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  149. return fHostUI->getPtr();
  150. }
  151. #ifdef HAVE_X11
  152. void* getContainerId2() const override
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  155. return fHostUI->getDisplay();
  156. }
  157. #endif
  158. // ---------------------------------------------------------------------
  159. protected:
  160. void handlePluginUIClosed() override
  161. {
  162. fIdling = false;
  163. }
  164. void handlePluginUIResized(const uint width, const uint height) override
  165. {
  166. fPlugin->uiResized(width, height);
  167. }
  168. // ---------------------------------------------------------------------
  169. private:
  170. CarlaPluginUI* fHostUI;
  171. bool fIdling;
  172. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitNative)
  173. };
  174. // -------------------------------------------------------------------------
  175. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
  176. {
  177. return new CarlaBridgeToolkitNative(format);
  178. }
  179. // -------------------------------------------------------------------------
  180. CARLA_BRIDGE_UI_END_NAMESPACE
  181. #define CARLA_PLUGIN_UI_CLASS_PREFIX ToolkitNative
  182. #include "CarlaPluginUI.cpp"
  183. #include "CarlaMacUtils.cpp"
  184. // -------------------------------------------------------------------------