The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

765 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /** This is the main singleton object that keeps track of connected blocks */
  21. struct Detector : public ReferenceCountedObject,
  22. private Timer,
  23. private AsyncUpdater
  24. {
  25. using BlockImpl = BlockImplementation<Detector>;
  26. Detector() : defaultDetector (new MIDIDeviceDetector()), deviceDetector (*defaultDetector)
  27. {
  28. startTimer (10);
  29. }
  30. Detector (PhysicalTopologySource::DeviceDetector& dd) : deviceDetector (dd)
  31. {
  32. startTimer (10);
  33. }
  34. ~Detector() override
  35. {
  36. jassert (activeTopologySources.isEmpty());
  37. }
  38. using Ptr = ReferenceCountedObjectPtr<Detector>;
  39. static Detector::Ptr getDefaultDetector()
  40. {
  41. auto& d = getDefaultDetectorPointer();
  42. if (d == nullptr)
  43. d = new Detector();
  44. return d;
  45. }
  46. static Detector::Ptr& getDefaultDetectorPointer()
  47. {
  48. static Detector::Ptr defaultDetector;
  49. return defaultDetector;
  50. }
  51. void detach (PhysicalTopologySource* pts)
  52. {
  53. activeTopologySources.removeAllInstancesOf (pts);
  54. if (activeTopologySources.isEmpty())
  55. {
  56. for (auto& b : currentTopology.blocks)
  57. if (auto bi = BlockImpl::getFrom (b))
  58. bi->sendCommandMessage (BlocksProtocol::endAPIMode);
  59. currentTopology = {};
  60. auto& d = getDefaultDetectorPointer();
  61. if (d != nullptr && d->getReferenceCount() == 2)
  62. getDefaultDetectorPointer() = nullptr;
  63. }
  64. }
  65. bool isConnected (Block::UID deviceID) const noexcept
  66. {
  67. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  68. for (auto&& b : currentTopology.blocks)
  69. if (b->uid == deviceID)
  70. return true;
  71. return false;
  72. }
  73. bool isConnectedViaBluetooth (const Block& block) const noexcept
  74. {
  75. if (const auto connection = getDeviceConnectionFor (block))
  76. if (const auto midiConnection = dynamic_cast<const MIDIDeviceConnection*> (connection))
  77. if (midiConnection->midiInput != nullptr)
  78. return midiConnection->midiInput->getName().containsIgnoreCase ("bluetooth");
  79. return false;
  80. }
  81. void handleDeviceAdded (const DeviceInfo& info)
  82. {
  83. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  84. const auto blockWasRemoved = containsBlockWithUID (blocksToRemove, info.uid);
  85. const auto knownBlock = std::find_if (previouslySeenBlocks.begin(), previouslySeenBlocks.end(),
  86. [uid = info.uid] (Block::Ptr block) { return uid == block->uid; });
  87. Block::Ptr block;
  88. if (knownBlock != previouslySeenBlocks.end())
  89. {
  90. block = *knownBlock;
  91. if (auto* blockImpl = BlockImpl::getFrom (*block))
  92. {
  93. blockImpl->markReconnected (info);
  94. previouslySeenBlocks.removeObject (block);
  95. }
  96. }
  97. else
  98. {
  99. block = new BlockImpl (*this, info);
  100. }
  101. currentTopology.blocks.addIfNotAlreadyThere (block);
  102. if (blockWasRemoved)
  103. {
  104. blocksToUpdate.addIfNotAlreadyThere (block);
  105. blocksToAdd.removeObject (block);
  106. }
  107. else
  108. {
  109. blocksToAdd.addIfNotAlreadyThere (block);
  110. blocksToUpdate.removeObject (block);
  111. }
  112. blocksToRemove.removeObject (block);
  113. triggerAsyncUpdate();
  114. }
  115. void handleDeviceRemoved (const DeviceInfo& info)
  116. {
  117. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  118. const auto blockIt = std::find_if (currentTopology.blocks.begin(), currentTopology.blocks.end(),
  119. [uid = info.uid] (Block::Ptr block) { return uid == block->uid; });
  120. if (blockIt != currentTopology.blocks.end())
  121. {
  122. const Block::Ptr block { *blockIt };
  123. if (auto blockImpl = BlockImpl::getFrom (block.get()))
  124. blockImpl->markDisconnected();
  125. currentTopology.blocks.removeObject (block);
  126. previouslySeenBlocks.addIfNotAlreadyThere (block);
  127. blocksToRemove.addIfNotAlreadyThere (block);
  128. blocksToUpdate.removeObject (block);
  129. blocksToAdd.removeObject (block);
  130. triggerAsyncUpdate();
  131. }
  132. }
  133. void handleConnectionsChanged()
  134. {
  135. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  136. triggerAsyncUpdate();
  137. }
  138. void handleDeviceUpdated (const DeviceInfo& info)
  139. {
  140. if (containsBlockWithUID (blocksToRemove, info.uid))
  141. return;
  142. const auto blockIt = std::find_if (currentTopology.blocks.begin(), currentTopology.blocks.end(),
  143. [uid = info.uid] (Block::Ptr block) { return uid == block->uid; });
  144. if (blockIt != currentTopology.blocks.end())
  145. {
  146. const Block::Ptr block { *blockIt };
  147. if (auto blockImpl = BlockImpl::getFrom (block.get()))
  148. blockImpl->markReconnected (info);
  149. if (! containsBlockWithUID (blocksToAdd, info.uid))
  150. {
  151. blocksToUpdate.addIfNotAlreadyThere (block);
  152. triggerAsyncUpdate();
  153. }
  154. }
  155. }
  156. void handleBatteryChargingChanged (Block::UID deviceID, const BlocksProtocol::BatteryCharging isCharging)
  157. {
  158. if (auto block = currentTopology.getBlockWithUID (deviceID))
  159. if (auto blockImpl = BlockImpl::getFrom (*block))
  160. blockImpl->batteryCharging = isCharging;
  161. }
  162. void handleBatteryLevelChanged (Block::UID deviceID, const BlocksProtocol::BatteryLevel batteryLevel)
  163. {
  164. if (auto block = currentTopology.getBlockWithUID (deviceID))
  165. if (auto blockImpl = BlockImpl::getFrom (*block))
  166. blockImpl->batteryLevel = batteryLevel;
  167. }
  168. void handleIndexChanged (Block::UID deviceID, const BlocksProtocol::TopologyIndex index)
  169. {
  170. if (auto block = currentTopology.getBlockWithUID (deviceID))
  171. if (auto blockImpl = BlockImpl::getFrom (*block))
  172. blockImpl->topologyIndex = index;
  173. }
  174. void notifyBlockIsRestarting (Block::UID deviceID)
  175. {
  176. for (auto& group : connectedDeviceGroups)
  177. group->handleBlockRestarting (deviceID);
  178. }
  179. Array<Block::UID> getDnaDependentDeviceUIDs (Block::UID uid)
  180. {
  181. JUCE_ASSERT_MESSAGE_THREAD
  182. Array<Block::UID> dependentDeviceUIDs;
  183. if (auto block = getBlockImplementationWithUID (uid))
  184. {
  185. if (auto master = getBlockImplementationWithUID (block->masterUID))
  186. {
  187. auto graph = BlockGraph (currentTopology, [uid] (Block::Ptr b) { return b->uid != uid; });
  188. const auto pathWithoutBlock = graph.getTraversalPathFromMaster (master);
  189. for (const auto b : currentTopology.blocks)
  190. {
  191. if (b->uid != uid && ! pathWithoutBlock.contains (b))
  192. {
  193. TOPOLOGY_LOG ( "Dependent device: " + b->name);
  194. dependentDeviceUIDs.add (b->uid);
  195. }
  196. }
  197. }
  198. }
  199. return dependentDeviceUIDs;
  200. }
  201. void handleSharedDataACK (Block::UID deviceID, uint32 packetCounter) const
  202. {
  203. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  204. if (auto* bi = getBlockImplementationWithUID (deviceID))
  205. bi->handleSharedDataACK (packetCounter);
  206. }
  207. void handleFirmwareUpdateACK (Block::UID deviceID, uint8 resultCode, uint32 resultDetail)
  208. {
  209. if (auto* bi = getBlockImplementationWithUID (deviceID))
  210. bi->handleFirmwareUpdateACK (resultCode, resultDetail);
  211. }
  212. void handleConfigUpdateMessage (Block::UID deviceID, int32 item, int32 value, int32 min, int32 max)
  213. {
  214. if (auto* bi = getBlockImplementationWithUID (deviceID))
  215. bi->handleConfigUpdateMessage (item, value, min, max);
  216. }
  217. void notifyBlockOfConfigChange (BlockImpl& bi, uint32 item)
  218. {
  219. if (auto configChangedCallback = bi.configChangedCallback)
  220. {
  221. if (item >= bi.getMaxConfigIndex())
  222. configChangedCallback (bi, {}, item);
  223. else
  224. configChangedCallback (bi, bi.getLocalConfigMetaData (item), item);
  225. }
  226. }
  227. void handleConfigSetMessage (Block::UID deviceID, int32 item, int32 value)
  228. {
  229. if (auto* bi = getBlockImplementationWithUID (deviceID))
  230. {
  231. bi->handleConfigSetMessage (item, value);
  232. notifyBlockOfConfigChange (*bi, uint32 (item));
  233. }
  234. }
  235. void handleConfigFactorySyncEndMessage (Block::UID deviceID)
  236. {
  237. if (auto* bi = getBlockImplementationWithUID (deviceID))
  238. notifyBlockOfConfigChange (*bi, bi->getMaxConfigIndex());
  239. }
  240. void handleConfigFactorySyncResetMessage (Block::UID deviceID)
  241. {
  242. if (auto* bi = getBlockImplementationWithUID (deviceID))
  243. bi->resetConfigListActiveStatus();
  244. }
  245. void handleLogMessage (Block::UID deviceID, const String& message) const
  246. {
  247. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  248. if (auto* bi = getBlockImplementationWithUID (deviceID))
  249. bi->handleLogMessage (message);
  250. }
  251. void handleButtonChange (Block::UID deviceID, Block::Timestamp timestamp, uint32 buttonIndex, bool isDown) const
  252. {
  253. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  254. if (auto* bi = getBlockImplementationWithUID (deviceID))
  255. {
  256. bi->pingFromDevice();
  257. if (isPositiveAndBelow (buttonIndex, bi->getButtons().size()))
  258. if (auto* cbi = dynamic_cast<BlockImpl::ControlButtonImplementation*> (bi->getButtons().getUnchecked (int (buttonIndex))))
  259. cbi->broadcastButtonChange (timestamp, bi->modelData.buttons[(int) buttonIndex].type, isDown);
  260. }
  261. }
  262. void handleTouchChange (Block::UID deviceID, const TouchSurface::Touch& touchEvent)
  263. {
  264. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  265. auto block = currentTopology.getBlockWithUID (deviceID);
  266. if (block != nullptr)
  267. {
  268. if (auto* surface = dynamic_cast<BlockImpl::TouchSurfaceImplementation*> (block->getTouchSurface()))
  269. {
  270. TouchSurface::Touch scaledEvent (touchEvent);
  271. scaledEvent.x *= block->getWidth();
  272. scaledEvent.y *= block->getHeight();
  273. scaledEvent.startX *= block->getWidth();
  274. scaledEvent.startY *= block->getHeight();
  275. surface->broadcastTouchChange (scaledEvent);
  276. }
  277. }
  278. }
  279. void cancelAllActiveTouches() noexcept
  280. {
  281. for (auto& block : currentTopology.blocks)
  282. if (auto* surface = block->getTouchSurface())
  283. surface->cancelAllActiveTouches();
  284. }
  285. void handleCustomMessage (Block::UID deviceID, Block::Timestamp timestamp, const int32* data)
  286. {
  287. if (auto* bi = getBlockImplementationWithUID (deviceID))
  288. bi->handleCustomMessage (timestamp, data);
  289. }
  290. //==============================================================================
  291. template <typename PacketBuilder>
  292. bool sendMessageToDevice (Block::UID deviceID, const PacketBuilder& builder) const
  293. {
  294. for (auto* c : connectedDeviceGroups)
  295. if (c->contains (deviceID))
  296. return c->sendMessageToDevice (builder);
  297. return false;
  298. }
  299. static Detector* getFrom (Block& b) noexcept
  300. {
  301. if (auto* bi = BlockImpl::getFrom (b))
  302. return (bi->detector);
  303. jassertfalse;
  304. return nullptr;
  305. }
  306. PhysicalTopologySource::DeviceConnection* getDeviceConnectionFor (const Block& b)
  307. {
  308. for (const auto& d : connectedDeviceGroups)
  309. {
  310. if (d->contains (b.uid))
  311. return d->getDeviceConnection();
  312. }
  313. return nullptr;
  314. }
  315. const PhysicalTopologySource::DeviceConnection* getDeviceConnectionFor (const Block& b) const
  316. {
  317. for (const auto& d : connectedDeviceGroups)
  318. {
  319. if (d->contains (b.uid))
  320. return d->getDeviceConnection();
  321. }
  322. return nullptr;
  323. }
  324. std::unique_ptr<MIDIDeviceDetector> defaultDetector;
  325. PhysicalTopologySource::DeviceDetector& deviceDetector;
  326. Array<PhysicalTopologySource*> activeTopologySources;
  327. BlockTopology currentTopology;
  328. private:
  329. Block::Array previouslySeenBlocks, blocksToAdd, blocksToRemove, blocksToUpdate;
  330. void timerCallback() override
  331. {
  332. startTimer (1500);
  333. auto detectedDevices = deviceDetector.scanForDevices();
  334. handleDevicesRemoved (detectedDevices);
  335. handleDevicesAdded (detectedDevices);
  336. }
  337. bool containsBlockWithUID (const Block::Array& blocks, Block::UID uid)
  338. {
  339. for (const auto block : blocks)
  340. if (block->uid == uid)
  341. return true;
  342. return false;
  343. }
  344. void handleDevicesRemoved (const StringArray& detectedDevices)
  345. {
  346. for (int i = connectedDeviceGroups.size(); --i >= 0;)
  347. if (! connectedDeviceGroups.getUnchecked(i)->isStillConnected (detectedDevices))
  348. connectedDeviceGroups.remove (i);
  349. }
  350. void handleDevicesAdded (const StringArray& detectedDevices)
  351. {
  352. for (const auto& devName : detectedDevices)
  353. {
  354. if (! hasDeviceFor (devName))
  355. {
  356. if (auto d = deviceDetector.openDevice (detectedDevices.indexOf (devName)))
  357. {
  358. connectedDeviceGroups.add (new ConnectedDeviceGroup<Detector> (*this, devName, d));
  359. }
  360. }
  361. }
  362. }
  363. bool hasDeviceFor (const String& devName) const
  364. {
  365. for (auto d : connectedDeviceGroups)
  366. if (d->deviceName == devName)
  367. return true;
  368. return false;
  369. }
  370. BlockImpl* getBlockImplementationWithUID (Block::UID deviceID) const noexcept
  371. {
  372. if (auto block = currentTopology.getBlockWithUID (deviceID))
  373. return BlockImpl::getFrom (*block);
  374. return nullptr;
  375. }
  376. OwnedArray<ConnectedDeviceGroup<Detector>> connectedDeviceGroups;
  377. //==============================================================================
  378. /** This is a friend of the BlocksImplementation that will scan and set the
  379. physical positions of the blocks.
  380. Returns an array of blocks that were updated.
  381. */
  382. struct BlocksLayoutTraverser
  383. {
  384. static Block::Array updateBlocks (const BlockTopology& topology)
  385. {
  386. Block::Array updated;
  387. Array<Block::UID> visited;
  388. for (auto& block : topology.blocks)
  389. {
  390. if (block->isMasterBlock() && ! visited.contains (block->uid))
  391. {
  392. if (auto* bi = BlockImpl::getFrom (block))
  393. {
  394. if (bi->rotation != 0 || bi->position.first != 0 || bi->position.second != 0)
  395. {
  396. bi->rotation = 0;
  397. bi->position = {};
  398. updated.add (block);
  399. }
  400. }
  401. layoutNeighbours (*block, topology, visited, updated);
  402. }
  403. }
  404. return updated;
  405. }
  406. private:
  407. // returns the distance from corner clockwise
  408. static int getUnitForIndex (Block::Ptr block, Block::ConnectionPort::DeviceEdge edge, int index)
  409. {
  410. if (block->getType() == Block::seaboardBlock)
  411. {
  412. if (edge == Block::ConnectionPort::DeviceEdge::north)
  413. {
  414. if (index == 0) return 1;
  415. if (index == 1) return 4;
  416. }
  417. else if (edge != Block::ConnectionPort::DeviceEdge::south)
  418. {
  419. return 1;
  420. }
  421. }
  422. if (edge == Block::ConnectionPort::DeviceEdge::south)
  423. return block->getWidth() - (index + 1);
  424. if (edge == Block::ConnectionPort::DeviceEdge::west)
  425. return block->getHeight() - (index + 1);
  426. return index;
  427. }
  428. // returns how often north needs to rotate by 90 degrees
  429. static int getRotationForEdge (Block::ConnectionPort::DeviceEdge edge)
  430. {
  431. switch (edge)
  432. {
  433. case Block::ConnectionPort::DeviceEdge::north: return 0;
  434. case Block::ConnectionPort::DeviceEdge::east: return 1;
  435. case Block::ConnectionPort::DeviceEdge::south: return 2;
  436. case Block::ConnectionPort::DeviceEdge::west: return 3;
  437. }
  438. jassertfalse;
  439. return 0;
  440. }
  441. static void layoutNeighbours (const Block::Ptr block,
  442. const BlockTopology& topology,
  443. Array<Block::UID>& visited,
  444. Block::Array& updated)
  445. {
  446. visited.add (block->uid);
  447. for (auto& connection : topology.connections)
  448. {
  449. if ((connection.device1 == block->uid && ! visited.contains (connection.device2))
  450. || (connection.device2 == block->uid && ! visited.contains (connection.device1)))
  451. {
  452. const auto theirUid = connection.device1 == block->uid ? connection.device2 : connection.device1;
  453. const auto neighbourPtr = topology.getBlockWithUID (theirUid);
  454. if (auto* neighbour = dynamic_cast<BlockImpl*> (neighbourPtr.get()))
  455. {
  456. const auto myBounds = block->getBlockAreaWithinLayout();
  457. const auto& myPort = connection.device1 == block->uid ? connection.connectionPortOnDevice1 : connection.connectionPortOnDevice2;
  458. const auto& theirPort = connection.device1 == block->uid ? connection.connectionPortOnDevice2 : connection.connectionPortOnDevice1;
  459. const auto myOffset = getUnitForIndex (block, myPort.edge, myPort.index);
  460. const auto theirOffset = getUnitForIndex (neighbourPtr, theirPort.edge, theirPort.index);
  461. {
  462. const auto neighbourRotation = (2 + block->getRotation()
  463. + getRotationForEdge (myPort.edge)
  464. - getRotationForEdge (theirPort.edge)) % 4;
  465. if (neighbour->rotation != neighbourRotation)
  466. {
  467. neighbour->rotation = neighbourRotation;
  468. updated.addIfNotAlreadyThere (neighbourPtr);
  469. }
  470. }
  471. std::pair<int, int> delta;
  472. const auto theirBounds = neighbour->getBlockAreaWithinLayout();
  473. switch ((block->getRotation() + getRotationForEdge (myPort.edge)) % 4)
  474. {
  475. case 0: // over me
  476. delta = { myOffset - (theirBounds.width - (theirOffset + 1)), -theirBounds.height };
  477. break;
  478. case 1: // right of me
  479. delta = { myBounds.width, myOffset - (theirBounds.height - (theirOffset + 1)) };
  480. break;
  481. case 2: // under me
  482. delta = { (myBounds.width - (myOffset + 1)) - theirOffset, myBounds.height };
  483. break;
  484. case 3: // left of me
  485. delta = { -theirBounds.width, (myBounds.height - (myOffset + 1)) - theirOffset };
  486. break;
  487. }
  488. {
  489. const auto neighbourX = myBounds.x + delta.first;
  490. const auto neighbourY = myBounds.y + delta.second;
  491. if (neighbour->position.first != neighbourX
  492. || neighbour->position.second != neighbourY)
  493. {
  494. neighbour->position.first = neighbourX;
  495. neighbour->position.second = neighbourY;
  496. updated.addIfNotAlreadyThere (neighbourPtr);
  497. }
  498. }
  499. layoutNeighbours (neighbourPtr, topology, visited, updated);
  500. }
  501. }
  502. }
  503. }
  504. };
  505. //==============================================================================
  506. #if DUMP_TOPOLOGY
  507. static String idToSerialNum (const BlockTopology& topology, Block::UID uid)
  508. {
  509. for (auto* b : topology.blocks)
  510. if (b->uid == uid)
  511. return b->serialNumber;
  512. return "???";
  513. }
  514. static String portEdgeToString (Block::ConnectionPort port)
  515. {
  516. switch (port.edge)
  517. {
  518. case Block::ConnectionPort::DeviceEdge::north: return "north";
  519. case Block::ConnectionPort::DeviceEdge::south: return "south";
  520. case Block::ConnectionPort::DeviceEdge::east: return "east";
  521. case Block::ConnectionPort::DeviceEdge::west: return "west";
  522. }
  523. return {};
  524. }
  525. static String portToString (Block::ConnectionPort port)
  526. {
  527. return portEdgeToString (port) + "_" + String (port.index);
  528. }
  529. static void dumpTopology (const BlockTopology& topology)
  530. {
  531. MemoryOutputStream m;
  532. m << "=============================================================================" << newLine
  533. << "Topology: " << topology.blocks.size() << " device(s)" << newLine
  534. << newLine;
  535. int index = 0;
  536. for (auto block : topology.blocks)
  537. {
  538. m << "Device " << index++ << (block->isMasterBlock() ? ": (MASTER)" : ":") << newLine;
  539. m << " Description: " << block->getDeviceDescription() << newLine
  540. << " Serial: " << block->serialNumber << newLine;
  541. if (auto bi = BlockImplementation<Detector>::getFrom (*block))
  542. m << " Short address: " << (int) bi->getDeviceIndex() << newLine;
  543. m << " Battery level: " + String (roundToInt (100.0f * block->getBatteryLevel())) + "%" << newLine
  544. << " Battery charging: " + String (block->isBatteryCharging() ? "y" : "n") << newLine
  545. << " Width: " << block->getWidth() << newLine
  546. << " Height: " << block->getHeight() << newLine
  547. << " Millimeters per unit: " << block->getMillimetersPerUnit() << newLine
  548. << newLine;
  549. }
  550. for (auto& connection : topology.connections)
  551. {
  552. m << idToSerialNum (topology, connection.device1)
  553. << ":" << portToString (connection.connectionPortOnDevice1)
  554. << " <-> "
  555. << idToSerialNum (topology, connection.device2)
  556. << ":" << portToString (connection.connectionPortOnDevice2) << newLine;
  557. }
  558. m << "=============================================================================" << newLine;
  559. Logger::outputDebugString (m.toString());
  560. }
  561. #endif
  562. //==============================================================================
  563. void updateBlockPositions()
  564. {
  565. const auto updated = BlocksLayoutTraverser::updateBlocks (currentTopology);
  566. for (const auto block : updated)
  567. {
  568. if (containsBlockWithUID (blocksToAdd, block->uid) || containsBlockWithUID (blocksToRemove, block->uid))
  569. continue;
  570. blocksToUpdate.addIfNotAlreadyThere (block);
  571. }
  572. }
  573. void updateBlockConnections()
  574. {
  575. currentTopology.connections.clearQuick();
  576. for (auto d : connectedDeviceGroups)
  577. currentTopology.connections.addArray (d->getCurrentDeviceConnections());
  578. }
  579. void handleAsyncUpdate() override
  580. {
  581. updateBlockConnections();
  582. updateBlockPositions();
  583. for (auto* d : activeTopologySources)
  584. {
  585. for (const auto block : blocksToAdd)
  586. d->listeners.call ([&block] (TopologySource::Listener& l) { l.blockAdded (block); });
  587. for (const auto block : blocksToRemove)
  588. d->listeners.call ([&block] (TopologySource::Listener& l) { l.blockRemoved (block); });
  589. for (const auto block : blocksToUpdate)
  590. d->listeners.call ([&block] (TopologySource::Listener& l) { l.blockUpdated (block); });
  591. }
  592. const auto topologyChanged = blocksToAdd.size() > 0 || blocksToRemove.size() > 0 || blocksToUpdate.size() > 0;
  593. if (topologyChanged)
  594. {
  595. #if DUMP_TOPOLOGY
  596. dumpTopology (currentTopology);
  597. #endif
  598. for (auto* d : activeTopologySources)
  599. d->listeners.call ([] (TopologySource::Listener& l) { l.topologyChanged(); });
  600. }
  601. blocksToUpdate.clear();
  602. blocksToAdd.clear();
  603. blocksToRemove.clear();
  604. static const int maxBlocksToSave = 100;
  605. if (previouslySeenBlocks.size() > maxBlocksToSave)
  606. previouslySeenBlocks.removeRange (0, 2 * (previouslySeenBlocks.size() - maxBlocksToSave));
  607. }
  608. //==============================================================================
  609. JUCE_DECLARE_WEAK_REFERENCEABLE (Detector)
  610. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Detector)
  611. };
  612. } // namespace juce