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.

217 lines
6.0KB

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