VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / bridges-ui / CarlaBridgeToolkitNative.cpp
blob452598d8f171c2581c3be02f1c21fa85d9e722db
1 /*
2 * Carla Bridge UI
3 * Copyright (C) 2014-2023 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaBridgeFormat.hpp"
19 #include "CarlaBridgeToolkit.hpp"
21 #include "CarlaMainLoop.hpp"
22 #include "CarlaPluginUI.hpp"
23 #include "CarlaTimeUtils.hpp"
24 #include "CarlaUtils.h"
26 #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA)
27 # include "CarlaMacUtils.hpp"
28 #endif
30 #if defined(HAVE_X11) && defined(BRIDGE_X11)
31 # include <X11/Xlib.h>
32 #endif
34 CARLA_BRIDGE_UI_START_NAMESPACE
36 using CARLA_BACKEND_NAMESPACE::runMainLoopOnce;
38 // -------------------------------------------------------------------------
40 class CarlaBridgeToolkitNative : public CarlaBridgeToolkit,
41 private CarlaPluginUI::Callback
43 public:
44 CarlaBridgeToolkitNative(CarlaBridgeFormat* const format)
45 : CarlaBridgeToolkit(format),
46 fHostUI(nullptr),
47 fIdling(false)
49 carla_debug("CarlaBridgeToolkitNative::CarlaBridgeToolkitNative(%p)", format);
52 ~CarlaBridgeToolkitNative() override
54 CARLA_SAFE_ASSERT_RETURN(fHostUI == nullptr,);
55 carla_debug("CarlaBridgeToolkitNative::~CarlaBridgeToolkitNative()");
58 bool init(const int /*argc*/, const char** /*argv[]*/) override
60 CARLA_SAFE_ASSERT_RETURN(fHostUI == nullptr, false);
61 carla_debug("CarlaBridgeToolkitNative::init()");
63 const CarlaBridgeFormat::Options& options(fPlugin->getOptions());
65 #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA)
66 CARLA_BACKEND_NAMESPACE::initStandaloneApplication();
67 fHostUI = CarlaPluginUI::newCocoa(this, 0, options.isStandalone, options.isResizable);
68 #elif defined(CARLA_OS_WIN) && defined(BRIDGE_HWND)
69 fHostUI = CarlaPluginUI::newWindows(this, 0, options.isStandalone, options.isResizable);
70 #elif defined(HAVE_X11) && defined(BRIDGE_X11)
71 XInitThreads();
72 fHostUI = CarlaPluginUI::newX11(this, 0, options.isStandalone, options.isResizable, true);
73 #endif
74 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, false);
76 #ifndef CARLA_OS_MAC
77 if (options.isStandalone)
78 fPlugin->setScaleFactor(carla_get_desktop_scale_factor());
79 #endif
81 fHostUI->setTitle(options.windowTitle.buffer());
83 #ifndef CARLA_OS_MAC
84 if (options.transientWindowId != 0)
86 fHostUI->setTransientWinId(options.transientWindowId);
88 else if (const char* const winIdStr = std::getenv("ENGINE_OPTION_FRONTEND_WIN_ID"))
90 if (const long long winId = std::strtoll(winIdStr, nullptr, 16))
91 fHostUI->setTransientWinId(static_cast<uintptr_t>(winId));
93 #endif
95 return true;
98 void exec(const bool showUI) override
100 CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
101 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
102 carla_debug("CarlaBridgeToolkitNative::exec(%s)", bool2str(showUI));
104 if (showUI)
106 fHostUI->show();
107 fHostUI->focus();
110 fIdling = true;
112 for (; runMainLoopOnce() && fIdling;)
114 if (fPlugin->isPipeRunning())
115 fPlugin->idlePipe();
117 fPlugin->idleUI();
118 fHostUI->idle();
119 #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
120 // MacOS and Win32 have event-loops to run, so minimize sleep time
121 carla_msleep(1);
122 #else
123 carla_msleep(33);
124 #endif
128 void quit() override
130 carla_debug("CarlaBridgeToolkitNative::quit()");
132 fIdling = false;
134 if (fHostUI != nullptr)
136 fHostUI->hide();
137 delete fHostUI;
138 fHostUI = nullptr;
142 void show() override
144 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
145 carla_debug("CarlaBridgeToolkitNative::show()");
147 fHostUI->show();
150 void focus() override
152 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
153 carla_debug("CarlaBridgeToolkitNative::focus()");
155 fHostUI->focus();
158 void hide() override
160 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
161 carla_debug("CarlaBridgeToolkitNative::hide()");
163 fHostUI->hide();
166 void setChildWindow(void* const ptr) override
168 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
169 CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
170 carla_debug("CarlaBridgeToolkitNative::setChildWindow(%p)", ptr);
172 fHostUI->setChildWindow(ptr);
175 void setSize(const uint width, const uint height) override
177 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
178 CARLA_SAFE_ASSERT_RETURN(width > 0,);
179 CARLA_SAFE_ASSERT_RETURN(height > 0,);
180 carla_debug("CarlaBridgeToolkitNative::resize(%i, %i)", width, height);
182 fHostUI->setSize(width, height, false, false);
185 void setTitle(const char* const title) override
187 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr,);
188 carla_debug("CarlaBridgeToolkitNative::setTitle(\"%s\")", title);
190 fHostUI->setTitle(title);
193 void* getContainerId() const override
195 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
197 return fHostUI->getPtr();
200 #ifdef HAVE_X11
201 void* getContainerId2() const override
203 CARLA_SAFE_ASSERT_RETURN(fHostUI != nullptr, nullptr);
205 return fHostUI->getDisplay();
207 #endif
209 // ---------------------------------------------------------------------
211 protected:
212 void handlePluginUIClosed() override
214 fIdling = false;
217 void handlePluginUIResized(const uint width, const uint height) override
219 fPlugin->uiResized(width, height);
222 // ---------------------------------------------------------------------
224 private:
225 CarlaPluginUI* fHostUI;
226 bool fIdling;
228 CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitNative)
231 // -------------------------------------------------------------------------
233 CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeFormat* const format)
235 return new CarlaBridgeToolkitNative(format);
238 // -------------------------------------------------------------------------
240 CARLA_BRIDGE_UI_END_NAMESPACE
242 #define CARLA_PLUGIN_UI_CLASS_PREFIX ToolkitNative
243 #include "CarlaPluginUI.cpp"
244 #include "utils/Windows.cpp"
246 // -------------------------------------------------------------------------