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.

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