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.

215 lines
5.8KB

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