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.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. fHostUI->show();
  78. fIdling = true;
  79. for (; runMainLoopOnce() && fIdling;)
  80. {
  81. if (fPlugin->isPipeRunning())
  82. fPlugin->idlePipe();
  83. fPlugin->idleUI();
  84. fHostUI->idle();
  85. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  86. // MacOS and Win32 have event-loops to run, so minimize sleep time
  87. carla_msleep(1);
  88. #else
  89. carla_msleep(20);
  90. #endif
  91. }
  92. }
  93. void quit() override
  94. {
  95. carla_debug("CarlaBridgeToolkitNative::quit()");
  96. fIdling = false;
  97. if (fHostUI != nullptr)
  98. {
  99. fHostUI->hide();
  100. delete fHostUI;
  101. fHostUI = nullptr;
  102. }
  103. }
  104. void show() override
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  107. carla_debug("CarlaBridgeToolkitNative::show()");
  108. fHostUI->show();
  109. }
  110. void focus() override
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  113. carla_debug("CarlaBridgeToolkitNative::focus()");
  114. fHostUI->focus();
  115. }
  116. void hide() override
  117. {
  118. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  119. carla_debug("CarlaBridgeToolkitNative::hide()");
  120. fHostUI->hide();
  121. }
  122. void setChildWindow(void* const ptr) override
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  125. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  126. carla_debug("CarlaBridgeToolkitNative::setChildWindow(%p)", ptr);
  127. fHostUI->setChildWindow(ptr);
  128. }
  129. void setSize(const uint width, const uint height) override
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  132. CARLA_SAFE_ASSERT_RETURN(width > 0,);
  133. CARLA_SAFE_ASSERT_RETURN(height > 0,);
  134. carla_debug("CarlaBridgeToolkitNative::resize(%i, %i)", width, height);
  135. fHostUI->setSize(width, height, false);
  136. }
  137. void setTitle(const char* const title) override
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
  140. carla_debug("CarlaBridgeToolkitNative::setTitle(\"%s\")", title);
  141. fHostUI->setTitle(title);
  142. }
  143. void* getContainerId() const override
  144. {
  145. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  146. return fHostUI->getPtr();
  147. }
  148. #ifdef HAVE_X11
  149. void* getContainerId2() const override
  150. {
  151. CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
  152. return fHostUI->getDisplay();
  153. }
  154. #endif
  155. // ---------------------------------------------------------------------
  156. protected:
  157. void handlePluginUIClosed() override
  158. {
  159. fIdling = false;
  160. }
  161. void handlePluginUIResized(const uint width, const uint height) override
  162. {
  163. fPlugin->uiResized(width, height);
  164. }
  165. // ---------------------------------------------------------------------
  166. private:
  167. CarlaPluginUI* fHostUI;
  168. bool fIdling;
  169. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitNative)
  170. };
  171. // -------------------------------------------------------------------------
  172. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
  173. {
  174. return new CarlaBridgeToolkitNative(format);
  175. }
  176. // -------------------------------------------------------------------------
  177. CARLA_BRIDGE_UI_END_NAMESPACE
  178. #define CARLA_PLUGIN_UI_CLASS_PREFIX ToolkitNative
  179. #include "CarlaPluginUI.cpp"
  180. // -------------------------------------------------------------------------