diff --git a/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.cpp b/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.cpp index c0d00a8388..a00e48b5e7 100644 --- a/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.cpp +++ b/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.cpp @@ -2599,38 +2599,72 @@ struct PhysicalTopologySource::DetectorHolder : private juce::Timer }; //============================================================================== -PhysicalTopologySource::PhysicalTopologySource() - : detector (new DetectorHolder (*this)) +PhysicalTopologySource::PhysicalTopologySource (bool startDetached) { - detector->detector->activeTopologySources.add (this); + if (! startDetached) + setActive (true); } -PhysicalTopologySource::PhysicalTopologySource (DeviceDetector& detectorToUse) - : detector (new DetectorHolder (*this, detectorToUse)) +PhysicalTopologySource::PhysicalTopologySource (DeviceDetector& detectorToUse, bool startDetached) + : customDetector (&detectorToUse) { - detector->detector->activeTopologySources.add (this); + if (! startDetached) + setActive (true); } PhysicalTopologySource::~PhysicalTopologySource() { - detector->detector->detach (this); - detector = nullptr; + setActive (false); +} + +void PhysicalTopologySource::setActive (bool shouldBeActive) +{ + if (isActive() == shouldBeActive) + return; + + if (shouldBeActive) + { + if (customDetector == nullptr) + detector = std::make_unique(*this); + else + detector = std::make_unique(*this, *customDetector); + + detector->detector->activeTopologySources.add (this); + } + else + { + detector->detector->detach (this); + detector.reset(); + } +} + +bool PhysicalTopologySource::isActive() const +{ + return detector != nullptr; } BlockTopology PhysicalTopologySource::getCurrentTopology() const { JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread! - return detector->detector->currentTopology; + if (detector != nullptr) + return detector->detector->currentTopology; + + return {}; } void PhysicalTopologySource::cancelAllActiveTouches() noexcept { - detector->detector->cancelAllActiveTouches(); + if (detector != nullptr) + detector->detector->cancelAllActiveTouches(); } bool PhysicalTopologySource::hasOwnServiceTimer() const { return false; } -void PhysicalTopologySource::handleTimerTick() { detector->handleTimerTick(); } +void PhysicalTopologySource::handleTimerTick() +{ + if (detector != nullptr) + detector->handleTimerTick(); +} PhysicalTopologySource::DeviceConnection::DeviceConnection() {} PhysicalTopologySource::DeviceConnection::~DeviceConnection() {} diff --git a/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.h b/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.h index 8e4ea6120d..a96cff3e6e 100644 --- a/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.h +++ b/modules/juce_blocks_basics/topology/juce_PhysicalTopologySource.h @@ -34,7 +34,7 @@ class PhysicalTopologySource : public TopologySource { public: /** Constructor. */ - PhysicalTopologySource(); + PhysicalTopologySource (bool startDetached = false); /** Destructor. */ ~PhysicalTopologySource(); @@ -45,6 +45,12 @@ public: /** Reset all touches */ void cancelAllActiveTouches() noexcept override; + /** Sets the TopologySource as active, occupying the midi port and trying to connect to the block devices */ + void setActive (bool shouldBeActive) override; + + /** Returns true, if the TopologySource is currently trying to connect the block devices */ + bool isActive() const override; + //========================================================================== /** For custom transport systems, this represents a connected device */ @@ -68,7 +74,7 @@ public: }; /** Constructor for custom transport systems. */ - PhysicalTopologySource (DeviceDetector& detectorToUse); + PhysicalTopologySource (DeviceDetector& detectorToUse, bool startDetached = false); static const char* const* getStandardLittleFootFunctions() noexcept; @@ -78,6 +84,7 @@ protected: private: //========================================================================== + DeviceDetector* customDetector = nullptr; struct Internal; struct DetectorHolder; std::unique_ptr detector; diff --git a/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.cpp b/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.cpp index 0074034318..088db2090b 100644 --- a/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.cpp +++ b/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.cpp @@ -79,6 +79,16 @@ struct RuleBasedTopologySource::Internal : public TopologySource::Listener, } } + void setActive (bool shouldBeActive) + { + detector.setActive (shouldBeActive); + } + + bool isActive() const + { + return detector.isActive(); + } + RuleBasedTopologySource& owner; TopologySource& detector; @@ -103,4 +113,14 @@ BlockTopology RuleBasedTopologySource::getCurrentTopology() const { void RuleBasedTopologySource::clearRules() { internal->clearRules(); } void RuleBasedTopologySource::addRule (Rule* r) { internal->addRule (r); } +void RuleBasedTopologySource::setActive (bool shouldBeActive) +{ + internal->setActive (shouldBeActive); +} + +bool RuleBasedTopologySource::isActive() const +{ + return internal->isActive(); +} + } // namespace juce diff --git a/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.h b/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.h index 975e08e0a3..1d47bf3e07 100644 --- a/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.h +++ b/modules/juce_blocks_basics/topology/juce_RuleBasedTopologySource.h @@ -45,7 +45,7 @@ public: //========================================================================== /** Returns the currently active topology. */ - BlockTopology getCurrentTopology() const; + BlockTopology getCurrentTopology() const override; /** A rule that can transform parts of a topology. */ struct Rule @@ -73,6 +73,12 @@ public: */ void addRule (Rule*); + /** Sets the TopologySource as active, occupying the midi port and trying to connect to the block devices */ + void setActive (bool shouldBeActive) override; + + /** Returns true, if the TopologySource is currently trying to connect the block devices */ + bool isActive() const override; + private: //========================================================================== struct Internal; diff --git a/modules/juce_blocks_basics/topology/juce_TopologySource.h b/modules/juce_blocks_basics/topology/juce_TopologySource.h index f558c6decb..065bc9b0c8 100644 --- a/modules/juce_blocks_basics/topology/juce_TopologySource.h +++ b/modules/juce_blocks_basics/topology/juce_TopologySource.h @@ -37,6 +37,12 @@ public: /** Returns the current topology that this object manages. */ virtual BlockTopology getCurrentTopology() const = 0; + /** Sets the TopologySource as active, occupying the midi port and trying to connect to the block devices */ + virtual void setActive (bool shouldBeActive) = 0; + + /** Returns true, if the TopologySource is currently trying to connect the block devices */ + virtual bool isActive() const = 0; + //========================================================================== /** Used to receive callbacks for topology changes */ struct Listener