3 * Copyright (C) 2011-2022 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 "CarlaEngineRunner.hpp"
19 #include "CarlaEngineInternal.hpp"
20 #include "CarlaPlugin.hpp"
22 #include "water/misc/Time.h"
24 CARLA_BACKEND_START_NAMESPACE
26 // -----------------------------------------------------------------------
28 CarlaEngineRunner::CarlaEngineRunner(CarlaEngine
* const engine
) noexcept
29 : CarlaRunner("CarlaEngineRunner"),
31 fEngineHasIdleOnMainThread(false),
32 fIsAlwaysRunning(false),
35 CARLA_SAFE_ASSERT(engine
!= nullptr);
36 carla_debug("CarlaEngineRunner::CarlaEngineRunner(%p)", engine
);
39 CarlaEngineRunner::~CarlaEngineRunner() noexcept
41 carla_debug("CarlaEngineRunner::~CarlaEngineRunner()");
44 void CarlaEngineRunner::start()
46 carla_debug("CarlaEngineRunner::start()");
50 fEngineHasIdleOnMainThread
= kEngine
->hasIdleOnMainThread();
51 fIsPlugin
= kEngine
->getType() == kEngineTypePlugin
;
52 fIsAlwaysRunning
= kEngine
->getType() == kEngineTypeBridge
|| fIsPlugin
;
57 void CarlaEngineRunner::stop()
59 carla_debug("CarlaEngineRunner::stop()");
63 // -----------------------------------------------------------------------
65 bool CarlaEngineRunner::run() noexcept
67 CARLA_SAFE_ASSERT_RETURN(kEngine
!= nullptr, false);
71 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
72 // int64_t lastPingTime = 0;
73 const CarlaEngineOsc
& engineOsc(kEngine
->pData
->osc
);
76 // runner must do something...
77 CARLA_SAFE_ASSERT_RETURN(fIsAlwaysRunning
|| kEngine
->isRunning(), false);
79 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
80 const bool oscRegistedForUDP
= engineOsc
.isControlRegisteredForUDP();
82 const bool oscRegistedForUDP
= false;
85 #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
90 for (uint i
=0, count
= kEngine
->getCurrentPluginCount(); i
< count
; ++i
)
92 const CarlaPluginPtr plugin
= kEngine
->getPluginUnchecked(i
);
94 CARLA_SAFE_ASSERT_CONTINUE(plugin
.get() != nullptr && plugin
->isEnabled());
95 CARLA_SAFE_ASSERT_UINT2(i
== plugin
->getId(), i
, plugin
->getId());
97 const uint hints
= plugin
->getHints();
98 const bool useIdle
= (hints
& PLUGIN_NEEDS_MAIN_THREAD_IDLE
) == 0 || !fEngineHasIdleOnMainThread
;
99 const bool updateUI
= (hints
& PLUGIN_HAS_CUSTOM_UI
) != 0 && (hints
& PLUGIN_NEEDS_UI_MAIN_THREAD
) == 0;
101 // -----------------------------------------------------------
108 } CARLA_SAFE_EXCEPTION("idle()")
111 // -----------------------------------------------------------
114 if (oscRegistedForUDP
|| updateUI
)
116 // -------------------------------------------------------
117 // Update parameter outputs
119 for (uint32_t j
=0, pcount
=plugin
->getParameterCount(); j
< pcount
; ++j
)
121 if (! plugin
->isParameterOutput(j
))
124 value
= plugin
->getParameterValue(j
);
126 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
127 // Update OSC engine client
128 if (oscRegistedForUDP
)
129 engineOsc
.sendParameterValue(i
, j
, value
);
133 plugin
->uiParameterChange(j
, value
);
140 } CARLA_SAFE_EXCEPTION("uiIdle()")
144 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
145 // -----------------------------------------------------------
146 // Update OSC control client peaks
148 if (oscRegistedForUDP
)
149 engineOsc
.sendPeaks(i
, kEngine
->getPeaks(i
));
153 #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
154 if (oscRegistedForUDP
)
155 engineOsc
.sendRuntimeInfo();
158 if (engineOsc.isControlRegisteredForTCP())
160 const int64_t timeNow = water::Time::currentTimeMillis();
162 if (timeNow - lastPingTime > 1000)
164 engineOsc.sendPing();
165 lastPingTime = timeNow;
174 // -----------------------------------------------------------------------
176 CARLA_BACKEND_END_NAMESPACE