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.

2734 lines
96KB

  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. /** These can be useful when debugging the topology. */
  21. #define LOG_BLOCKS_CONNECTIVITY 0
  22. #define LOG_BLOCKS_PINGS 0
  23. #define DUMP_BANDWIDTH_STATS 0
  24. #define TOPOLOGY_LOG(text) \
  25. JUCE_BLOCK_WITH_FORCED_SEMICOLON (juce::String buf ("Topology Src: "); \
  26. juce::Logger::outputDebugString (buf << text);)
  27. #if LOG_BLOCKS_CONNECTIVITY
  28. #define LOG_CONNECTIVITY(text) TOPOLOGY_LOG(text)
  29. #else
  30. #define LOG_CONNECTIVITY(text)
  31. #endif
  32. #if LOG_BLOCKS_PINGS
  33. #define LOG_PING(text) TOPOLOGY_LOG(text)
  34. #else
  35. #define LOG_PING(text)
  36. #endif
  37. #if DUMP_BANDWIDTH_STATS
  38. namespace
  39. {
  40. struct PortIOStats
  41. {
  42. PortIOStats (const char* nm) : name (nm) {}
  43. const char* const name;
  44. int byteCount = 0;
  45. int messageCount = 0;
  46. int bytesPerSec = 0;
  47. int largestMessageBytes = 0;
  48. int lastMessageBytes = 0;
  49. void update (double elapsedSec)
  50. {
  51. if (byteCount > 0)
  52. {
  53. bytesPerSec = (int) (byteCount / elapsedSec);
  54. byteCount = 0;
  55. juce::Logger::writeToLog (getString());
  56. }
  57. }
  58. juce::String getString() const
  59. {
  60. return juce::String (name) + ": "
  61. + "count=" + juce::String (messageCount).paddedRight (' ', 7)
  62. + "rate=" + (juce::String (bytesPerSec / 1024.0f, 1) + " Kb/sec").paddedRight (' ', 11)
  63. + "largest=" + (juce::String (largestMessageBytes) + " bytes").paddedRight (' ', 11)
  64. + "last=" + (juce::String (lastMessageBytes) + " bytes").paddedRight (' ', 11);
  65. }
  66. void registerMessage (int numBytes) noexcept
  67. {
  68. byteCount += numBytes;
  69. ++messageCount;
  70. lastMessageBytes = numBytes;
  71. largestMessageBytes = juce::jmax (largestMessageBytes, numBytes);
  72. }
  73. };
  74. static PortIOStats inputStats { "Input" }, outputStats { "Output" };
  75. static uint32 startTime = 0;
  76. static inline void resetOnSecondBoundary()
  77. {
  78. auto now = juce::Time::getMillisecondCounter();
  79. double elapsedSec = (now - startTime) / 1000.0;
  80. if (elapsedSec >= 1.0)
  81. {
  82. inputStats.update (elapsedSec);
  83. outputStats.update (elapsedSec);
  84. startTime = now;
  85. }
  86. }
  87. static inline void registerBytesOut (int numBytes)
  88. {
  89. outputStats.registerMessage (numBytes);
  90. resetOnSecondBoundary();
  91. }
  92. static inline void registerBytesIn (int numBytes)
  93. {
  94. inputStats.registerMessage (numBytes);
  95. resetOnSecondBoundary();
  96. }
  97. }
  98. juce::String getMidiIOStats()
  99. {
  100. return inputStats.getString() + " " + outputStats.getString();
  101. }
  102. #endif
  103. //==============================================================================
  104. struct PhysicalTopologySource::Internal
  105. {
  106. struct Detector;
  107. struct BlockImplementation;
  108. struct ControlButtonImplementation;
  109. struct RotaryDialImplementation;
  110. struct TouchSurfaceImplementation;
  111. struct LEDGridImplementation;
  112. struct LEDRowImplementation;
  113. //==============================================================================
  114. struct MIDIDeviceConnection : public DeviceConnection,
  115. public juce::MidiInputCallback
  116. {
  117. MIDIDeviceConnection() {}
  118. ~MIDIDeviceConnection()
  119. {
  120. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  121. listeners.call ([this] (Listener& l) { l.connectionBeingDeleted (*this); });
  122. if (midiInput != nullptr)
  123. midiInput->stop();
  124. if (interprocessLock != nullptr)
  125. interprocessLock->exit();
  126. }
  127. bool lockAgainstOtherProcesses (const String& midiInName, const String& midiOutName)
  128. {
  129. interprocessLock.reset (new juce::InterProcessLock ("blocks_sdk_"
  130. + File::createLegalFileName (midiInName)
  131. + "_" + File::createLegalFileName (midiOutName)));
  132. if (interprocessLock->enter (500))
  133. return true;
  134. interprocessLock = nullptr;
  135. return false;
  136. }
  137. struct Listener
  138. {
  139. virtual ~Listener() {}
  140. virtual void handleIncomingMidiMessage (const juce::MidiMessage& message) = 0;
  141. virtual void connectionBeingDeleted (const MIDIDeviceConnection&) = 0;
  142. };
  143. void addListener (Listener* l)
  144. {
  145. listeners.add (l);
  146. }
  147. void removeListener (Listener* l)
  148. {
  149. listeners.remove (l);
  150. }
  151. bool sendMessageToDevice (const void* data, size_t dataSize) override
  152. {
  153. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  154. jassert (dataSize > sizeof (BlocksProtocol::roliSysexHeader) + 2);
  155. jassert (memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader)) == 0);
  156. jassert (static_cast<const uint8*> (data)[dataSize - 1] == 0xf7);
  157. if (midiOutput != nullptr)
  158. {
  159. midiOutput->sendMessageNow (juce::MidiMessage (data, (int) dataSize));
  160. return true;
  161. }
  162. return false;
  163. }
  164. void handleIncomingMidiMessage (juce::MidiInput*, const juce::MidiMessage& message) override
  165. {
  166. const auto data = message.getRawData();
  167. const int dataSize = message.getRawDataSize();
  168. const int bodySize = dataSize - (int) (sizeof (BlocksProtocol::roliSysexHeader) + 1);
  169. if (bodySize > 0 && memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader)) == 0)
  170. if (handleMessageFromDevice != nullptr)
  171. handleMessageFromDevice (data + sizeof (BlocksProtocol::roliSysexHeader), (size_t) bodySize);
  172. listeners.call ([&] (Listener& l) { l.handleIncomingMidiMessage (message); });
  173. }
  174. std::unique_ptr<juce::MidiInput> midiInput;
  175. std::unique_ptr<juce::MidiOutput> midiOutput;
  176. private:
  177. juce::ListenerList<Listener> listeners;
  178. std::unique_ptr<juce::InterProcessLock> interprocessLock;
  179. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceConnection)
  180. };
  181. //==============================================================================
  182. struct MIDIDeviceDetector : public DeviceDetector
  183. {
  184. MIDIDeviceDetector() {}
  185. juce::StringArray scanForDevices() override
  186. {
  187. juce::StringArray result;
  188. for (auto& pair : findDevices())
  189. result.add (pair.inputName + " & " + pair.outputName);
  190. return result;
  191. }
  192. DeviceConnection* openDevice (int index) override
  193. {
  194. auto pair = findDevices()[index];
  195. if (pair.inputIndex >= 0 && pair.outputIndex >= 0)
  196. {
  197. std::unique_ptr<MIDIDeviceConnection> dev (new MIDIDeviceConnection());
  198. if (dev->lockAgainstOtherProcesses (pair.inputName, pair.outputName))
  199. {
  200. lockedFromOutside = false;
  201. dev->midiInput.reset (juce::MidiInput::openDevice (pair.inputIndex, dev.get()));
  202. dev->midiOutput.reset (juce::MidiOutput::openDevice (pair.outputIndex));
  203. if (dev->midiInput != nullptr)
  204. {
  205. dev->midiInput->start();
  206. return dev.release();
  207. }
  208. }
  209. else
  210. {
  211. lockedFromOutside = true;
  212. }
  213. }
  214. return nullptr;
  215. }
  216. bool isLockedFromOutside() const override
  217. {
  218. return lockedFromOutside && ! findDevices().isEmpty();
  219. }
  220. static bool isBlocksMidiDeviceName (const juce::String& name)
  221. {
  222. return name.indexOf (" BLOCK") > 0 || name.indexOf (" Block") > 0;
  223. }
  224. static String cleanBlocksDeviceName (juce::String name)
  225. {
  226. name = name.trim();
  227. if (name.endsWith (" IN)"))
  228. return name.dropLastCharacters (4);
  229. if (name.endsWith (" OUT)"))
  230. return name.dropLastCharacters (5);
  231. const int openBracketPosition = name.lastIndexOfChar ('[');
  232. if (openBracketPosition != -1 && name.endsWith ("]"))
  233. return name.dropLastCharacters (name.length() - openBracketPosition);
  234. return name;
  235. }
  236. struct MidiInputOutputPair
  237. {
  238. juce::String outputName, inputName;
  239. int outputIndex = -1, inputIndex = -1;
  240. };
  241. static juce::Array<MidiInputOutputPair> findDevices()
  242. {
  243. juce::Array<MidiInputOutputPair> result;
  244. auto midiInputs = juce::MidiInput::getDevices();
  245. auto midiOutputs = juce::MidiOutput::getDevices();
  246. for (int j = 0; j < midiInputs.size(); ++j)
  247. {
  248. if (isBlocksMidiDeviceName (midiInputs[j]))
  249. {
  250. MidiInputOutputPair pair;
  251. pair.inputName = midiInputs[j];
  252. pair.inputIndex = j;
  253. String cleanedInputName = cleanBlocksDeviceName (pair.inputName);
  254. for (int i = 0; i < midiOutputs.size(); ++i)
  255. {
  256. if (cleanBlocksDeviceName (midiOutputs[i]) == cleanedInputName)
  257. {
  258. pair.outputName = midiOutputs[i];
  259. pair.outputIndex = i;
  260. break;
  261. }
  262. }
  263. result.add (pair);
  264. }
  265. }
  266. return result;
  267. }
  268. private:
  269. bool lockedFromOutside = true;
  270. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceDetector)
  271. };
  272. //==============================================================================
  273. struct DeviceInfo
  274. {
  275. // VS2015 requires a constructor to avoid aggregate initialization
  276. DeviceInfo (Block::UID buid, BlocksProtocol::TopologyIndex tidx, BlocksProtocol::BlockSerialNumber s,
  277. BlocksProtocol::VersionNumber v, BlocksProtocol::BlockName n, bool master = false)
  278. : uid (buid), index (tidx), serial (s), version (v), name (n), isMaster (master)
  279. {
  280. }
  281. Block::UID uid {};
  282. BlocksProtocol::TopologyIndex index;
  283. BlocksProtocol::BlockSerialNumber serial;
  284. BlocksProtocol::VersionNumber version;
  285. BlocksProtocol::BlockName name;
  286. bool isMaster {};
  287. };
  288. static juce::String getVersionString (const BlocksProtocol::VersionNumber& v)
  289. {
  290. return juce::String (reinterpret_cast<const char*> (v.version),
  291. std::min (sizeof (v.version), static_cast<size_t> (v.length)));
  292. }
  293. static juce::String getNameString (const BlocksProtocol::BlockName& n)
  294. {
  295. return juce::String (reinterpret_cast<const char*> (n.name),
  296. std::min (sizeof (n.name), static_cast<size_t> (n.length)));
  297. }
  298. static Block::Timestamp deviceTimestampToHost (uint32 timestamp) noexcept
  299. {
  300. return static_cast<Block::Timestamp> (timestamp);
  301. }
  302. static juce::Array<DeviceInfo> getArrayOfDeviceInfo (const juce::Array<BlocksProtocol::DeviceStatus>& devices)
  303. {
  304. juce::Array<DeviceInfo> result;
  305. bool isFirst = true; // TODO: First block not always master block! Assumption violated.
  306. for (auto& device : devices)
  307. {
  308. BlocksProtocol::VersionNumber version;
  309. BlocksProtocol::BlockName name;
  310. result.add ({ getBlockUIDFromSerialNumber (device.serialNumber),
  311. device.index,
  312. device.serialNumber,
  313. version,
  314. name,
  315. isFirst });
  316. isFirst = false;
  317. }
  318. return result;
  319. }
  320. static bool containsBlockWithUID (const Block::Array& blocks, Block::UID uid) noexcept
  321. {
  322. for (auto&& block : blocks)
  323. if (block->uid == uid)
  324. return true;
  325. return false;
  326. }
  327. static bool versionNumberChanged (const DeviceInfo& device, juce::String version) noexcept
  328. {
  329. auto deviceVersion = getVersionString (device.version);
  330. return deviceVersion != version && deviceVersion.isNotEmpty();
  331. }
  332. static bool nameIsValid (const DeviceInfo& device)
  333. {
  334. return device.name.length > 0;
  335. }
  336. static void setVersionNumberForBlock (const DeviceInfo& deviceInfo, Block& block) noexcept
  337. {
  338. jassert (deviceInfo.uid == block.uid);
  339. block.versionNumber = getVersionString (deviceInfo.version);
  340. }
  341. static void setNameForBlock (const DeviceInfo& deviceInfo, Block& block)
  342. {
  343. jassert (deviceInfo.uid == block.uid);
  344. block.name = getNameString (deviceInfo.name);
  345. }
  346. //==============================================================================
  347. struct ConnectedDeviceGroup : private juce::AsyncUpdater,
  348. private juce::Timer
  349. {
  350. ConnectedDeviceGroup (Detector& d, const juce::String& name, DeviceConnection* connection)
  351. : detector (d), deviceName (name), deviceConnection (connection)
  352. {
  353. deviceConnection->handleMessageFromDevice = [this] (const void* data, size_t dataSize)
  354. {
  355. this->handleIncomingMessage (data, dataSize);
  356. };
  357. startTimer (200);
  358. sendTopologyRequest();
  359. }
  360. bool isStillConnected (const juce::StringArray& detectedDevices) const noexcept
  361. {
  362. return detectedDevices.contains (deviceName)
  363. && ! failedToGetTopology();
  364. }
  365. int getIndexFromDeviceID (Block::UID uid) const noexcept
  366. {
  367. for (auto& d : currentDeviceInfo)
  368. if (d.uid == uid)
  369. return d.index;
  370. return -1;
  371. }
  372. const DeviceInfo* getDeviceInfoFromUID (Block::UID uid) const noexcept
  373. {
  374. for (auto& d : currentDeviceInfo)
  375. if (d.uid == uid)
  376. return &d;
  377. return nullptr;
  378. }
  379. const BlocksProtocol::DeviceStatus* getLastStatus (Block::UID deviceID) const noexcept
  380. {
  381. for (auto&& status : currentTopologyDevices)
  382. if (getBlockUIDFromSerialNumber (status.serialNumber) == deviceID)
  383. return &status;
  384. return nullptr;
  385. }
  386. void notifyBlockIsRestarting (Block::UID deviceID)
  387. {
  388. forceApiDisconnected (deviceID);
  389. }
  390. //==============================================================================
  391. // The following methods will be called by the HostPacketDecoder:
  392. void beginTopology (int numDevices, int numConnections)
  393. {
  394. incomingTopologyDevices.clearQuick();
  395. incomingTopologyDevices.ensureStorageAllocated (numDevices);
  396. incomingTopologyConnections.clearQuick();
  397. incomingTopologyConnections.ensureStorageAllocated (numConnections);
  398. }
  399. void extendTopology (int numDevices, int numConnections)
  400. {
  401. incomingTopologyDevices.ensureStorageAllocated (incomingTopologyDevices.size() + numDevices);
  402. incomingTopologyConnections.ensureStorageAllocated (incomingTopologyConnections.size() + numConnections);
  403. }
  404. void handleTopologyDevice (BlocksProtocol::DeviceStatus status)
  405. {
  406. incomingTopologyDevices.add (status);
  407. }
  408. void handleTopologyConnection (BlocksProtocol::DeviceConnection connection)
  409. {
  410. incomingTopologyConnections.add (connection);
  411. }
  412. void endTopology()
  413. {
  414. currentDeviceInfo = getArrayOfDeviceInfo (incomingTopologyDevices);
  415. currentDeviceConnections = getArrayOfConnections (incomingTopologyConnections);
  416. currentTopologyDevices = incomingTopologyDevices;
  417. lastTopologyReceiveTime = juce::Time::getCurrentTime();
  418. const int numRemoved = blockPings.removeIf ([this] (auto& ping)
  419. {
  420. for (auto& info : currentDeviceInfo)
  421. if (info.uid == ping.blockUID)
  422. return false;
  423. LOG_CONNECTIVITY ("API Disconnected by topology update " << ping.blockUID);
  424. return true;
  425. });
  426. if (numRemoved > 0)
  427. detector.handleTopologyChange();
  428. }
  429. void handleVersion (BlocksProtocol::DeviceVersion version)
  430. {
  431. for (auto& d : currentDeviceInfo)
  432. if (d.index == version.index && version.version.length > 1)
  433. d.version = version.version;
  434. }
  435. void handleName (BlocksProtocol::DeviceName name)
  436. {
  437. for (auto& d : currentDeviceInfo)
  438. if (d.index == name.index && name.name.length > 1)
  439. d.name = name.name;
  440. }
  441. void handleControlButtonUpDown (BlocksProtocol::TopologyIndex deviceIndex, uint32 timestamp,
  442. BlocksProtocol::ControlButtonID buttonID, bool isDown)
  443. {
  444. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  445. detector.handleButtonChange (deviceID, deviceTimestampToHost (timestamp), buttonID.get(), isDown);
  446. }
  447. void handleCustomMessage (BlocksProtocol::TopologyIndex deviceIndex, uint32 timestamp, const int32* data)
  448. {
  449. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  450. detector.handleCustomMessage (deviceID, deviceTimestampToHost (timestamp), data);
  451. }
  452. void handleTouchChange (BlocksProtocol::TopologyIndex deviceIndex,
  453. uint32 timestamp,
  454. BlocksProtocol::TouchIndex touchIndex,
  455. BlocksProtocol::TouchPosition position,
  456. BlocksProtocol::TouchVelocity velocity,
  457. bool isStart, bool isEnd)
  458. {
  459. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  460. {
  461. TouchSurface::Touch touch;
  462. touch.index = (int) touchIndex.get();
  463. touch.x = position.x.toUnipolarFloat();
  464. touch.y = position.y.toUnipolarFloat();
  465. touch.z = position.z.toUnipolarFloat();
  466. touch.xVelocity = velocity.vx.toBipolarFloat();
  467. touch.yVelocity = velocity.vy.toBipolarFloat();
  468. touch.zVelocity = velocity.vz.toBipolarFloat();
  469. touch.eventTimestamp = deviceTimestampToHost (timestamp);
  470. touch.isTouchStart = isStart;
  471. touch.isTouchEnd = isEnd;
  472. touch.blockUID = deviceID;
  473. setTouchStartPosition (touch);
  474. detector.handleTouchChange (deviceID, touch);
  475. }
  476. }
  477. void setTouchStartPosition (TouchSurface::Touch& touch)
  478. {
  479. auto& startPos = touchStartPositions.getValue (touch);
  480. if (touch.isTouchStart)
  481. startPos = { touch.x, touch.y };
  482. touch.startX = startPos.x;
  483. touch.startY = startPos.y;
  484. }
  485. void handlePacketACK (BlocksProtocol::TopologyIndex deviceIndex,
  486. BlocksProtocol::PacketCounter counter)
  487. {
  488. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  489. {
  490. detector.handleSharedDataACK (deviceID, counter);
  491. updateApiPing (deviceID);
  492. }
  493. }
  494. void handleFirmwareUpdateACK (BlocksProtocol::TopologyIndex deviceIndex,
  495. BlocksProtocol::FirmwareUpdateACKCode resultCode,
  496. BlocksProtocol::FirmwareUpdateACKDetail resultDetail)
  497. {
  498. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  499. {
  500. detector.handleFirmwareUpdateACK (deviceID, (uint8) resultCode.get(), (uint32) resultDetail.get());
  501. updateApiPing (deviceID);
  502. }
  503. }
  504. void handleConfigUpdateMessage (BlocksProtocol::TopologyIndex deviceIndex,
  505. int32 item, int32 value, int32 min, int32 max)
  506. {
  507. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  508. detector.handleConfigUpdateMessage (deviceID, item, value, min, max);
  509. }
  510. void handleConfigSetMessage (BlocksProtocol::TopologyIndex deviceIndex,
  511. int32 item, int32 value)
  512. {
  513. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  514. detector.handleConfigSetMessage (deviceID, item, value);
  515. }
  516. void handleConfigFactorySyncEndMessage (BlocksProtocol::TopologyIndex deviceIndex)
  517. {
  518. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  519. detector.handleConfigFactorySyncEndMessage (deviceID);
  520. }
  521. void handleConfigFactorySyncResetMessage (BlocksProtocol::TopologyIndex deviceIndex)
  522. {
  523. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  524. detector.handleConfigFactorySyncResetMessage (deviceID);
  525. }
  526. void handleLogMessage (BlocksProtocol::TopologyIndex deviceIndex, const String& message)
  527. {
  528. if (auto deviceID = getDeviceIDFromMessageIndex (deviceIndex))
  529. detector.handleLogMessage (deviceID, message);
  530. }
  531. //==============================================================================
  532. template <typename PacketBuilder>
  533. bool sendMessageToDevice (const PacketBuilder& builder) const
  534. {
  535. if (deviceConnection->sendMessageToDevice (builder.getData(), (size_t) builder.size()))
  536. {
  537. #if DUMP_BANDWIDTH_STATS
  538. registerBytesOut (builder.size());
  539. #endif
  540. return true;
  541. }
  542. return false;
  543. }
  544. DeviceConnection* getDeviceConnection()
  545. {
  546. return deviceConnection.get();
  547. }
  548. juce::Array<DeviceInfo> getCurrentDeviceInfo()
  549. {
  550. auto blocks = currentDeviceInfo;
  551. blocks.removeIf ([this] (DeviceInfo& info) { return ! isApiConnected (info.uid); });
  552. return blocks;
  553. }
  554. juce::Array<BlockDeviceConnection> getCurrentDeviceConnections()
  555. {
  556. auto connections = currentDeviceConnections;
  557. connections.removeIf ([this] (BlockDeviceConnection& c) { return ! isApiConnected (c.device1) || ! isApiConnected (c.device2); });
  558. return connections;
  559. }
  560. Detector& detector;
  561. juce::String deviceName;
  562. static constexpr double pingTimeoutSeconds = 6.0;
  563. private:
  564. //==============================================================================
  565. juce::Array<DeviceInfo> currentDeviceInfo;
  566. juce::Array<BlockDeviceConnection> currentDeviceConnections;
  567. std::unique_ptr<DeviceConnection> deviceConnection;
  568. juce::Array<BlocksProtocol::DeviceStatus> incomingTopologyDevices, currentTopologyDevices;
  569. juce::Array<BlocksProtocol::DeviceConnection> incomingTopologyConnections;
  570. juce::CriticalSection incomingPacketLock;
  571. juce::Array<juce::MemoryBlock> incomingPackets;
  572. struct TouchStart { float x, y; };
  573. TouchList<TouchStart> touchStartPositions;
  574. //==============================================================================
  575. juce::Time lastTopologyRequestTime, lastTopologyReceiveTime;
  576. int numTopologyRequestsSent = 0;
  577. void scheduleNewTopologyRequest()
  578. {
  579. numTopologyRequestsSent = 0;
  580. lastTopologyReceiveTime = juce::Time();
  581. lastTopologyRequestTime = juce::Time::getCurrentTime();
  582. }
  583. void sendTopologyRequest()
  584. {
  585. ++numTopologyRequestsSent;
  586. lastTopologyRequestTime = juce::Time::getCurrentTime();
  587. sendCommandMessage (0, BlocksProtocol::requestTopologyMessage);
  588. }
  589. void timerCallback() override
  590. {
  591. const auto now = juce::Time::getCurrentTime();
  592. if ((now > lastTopologyReceiveTime + juce::RelativeTime::seconds (30.0))
  593. && now > lastTopologyRequestTime + juce::RelativeTime::seconds (1.0)
  594. && numTopologyRequestsSent < 4)
  595. sendTopologyRequest();
  596. checkApiTimeouts (now);
  597. startApiModeOnConnectedBlocks();
  598. }
  599. bool failedToGetTopology() const noexcept
  600. {
  601. return numTopologyRequestsSent > 4 && lastTopologyReceiveTime == juce::Time();
  602. }
  603. bool sendCommandMessage (BlocksProtocol::TopologyIndex deviceIndex, uint32 commandID) const
  604. {
  605. BlocksProtocol::HostPacketBuilder<64> p;
  606. p.writePacketSysexHeaderBytes (deviceIndex);
  607. p.deviceControlMessage (commandID);
  608. p.writePacketSysexFooter();
  609. return sendMessageToDevice (p);
  610. }
  611. //==============================================================================
  612. struct BlockPingTime
  613. {
  614. Block::UID blockUID;
  615. juce::Time lastPing;
  616. };
  617. juce::Array<BlockPingTime> blockPings;
  618. void updateApiPing (Block::UID uid)
  619. {
  620. const auto now = juce::Time::getCurrentTime();
  621. if (auto* ping = getPing (uid))
  622. {
  623. LOG_PING ("Ping: " << uid << " " << now.formatted ("%Mm %Ss"));
  624. ping->lastPing = now;
  625. }
  626. else
  627. {
  628. LOG_CONNECTIVITY ("API Connected " << uid);
  629. blockPings.add ({ uid, now });
  630. detector.handleTopologyChange();
  631. }
  632. }
  633. BlockPingTime* getPing (Block::UID uid)
  634. {
  635. for (auto& ping : blockPings)
  636. if (uid == ping.blockUID)
  637. return &ping;
  638. return nullptr;
  639. }
  640. void removeDeviceInfo (Block::UID uid)
  641. {
  642. currentDeviceInfo.removeIf ([uid] (DeviceInfo& info) { return uid == info.uid; });
  643. }
  644. bool isApiConnected (Block::UID uid)
  645. {
  646. return getPing (uid) != nullptr;
  647. }
  648. void forceApiDisconnected (Block::UID uid)
  649. {
  650. if (isApiConnected (uid))
  651. {
  652. // Clear all known API connections and broadcast an empty topology,
  653. // as DNA blocks connected to the restarting block may be offline.
  654. LOG_CONNECTIVITY ("API Disconnected " << uid << ", re-probing topology");
  655. currentDeviceInfo.clearQuick();
  656. blockPings.clearQuick();
  657. detector.handleTopologyChange();
  658. scheduleNewTopologyRequest();
  659. }
  660. }
  661. void checkApiTimeouts (juce::Time now)
  662. {
  663. const auto timedOut = [this, now] (BlockPingTime& ping)
  664. {
  665. if (ping.lastPing >= now - juce::RelativeTime::seconds (pingTimeoutSeconds))
  666. return false;
  667. LOG_CONNECTIVITY ("Ping timeout: " << ping.blockUID);
  668. removeDeviceInfo (ping.blockUID);
  669. return true;
  670. };
  671. if (blockPings.removeIf (timedOut) > 0)
  672. {
  673. scheduleNewTopologyRequest();
  674. detector.handleTopologyChange();
  675. }
  676. }
  677. void startApiModeOnConnectedBlocks()
  678. {
  679. for (auto& info : currentDeviceInfo)
  680. {
  681. if (! isApiConnected (info.uid))
  682. {
  683. LOG_CONNECTIVITY ("API Try " << info.uid);
  684. sendCommandMessage (info.index, BlocksProtocol::endAPIMode);
  685. sendCommandMessage (info.index, BlocksProtocol::beginAPIMode);
  686. }
  687. }
  688. }
  689. //==============================================================================
  690. Block::UID getDeviceIDFromIndex (BlocksProtocol::TopologyIndex index) const noexcept
  691. {
  692. for (auto& d : currentDeviceInfo)
  693. if (d.index == index)
  694. return d.uid;
  695. return {};
  696. }
  697. Block::UID getDeviceIDFromMessageIndex (BlocksProtocol::TopologyIndex index) noexcept
  698. {
  699. const auto uid = getDeviceIDFromIndex (index);
  700. // re-request topology if we get an event from an unknown block
  701. if (uid == Block::UID())
  702. scheduleNewTopologyRequest();
  703. return uid;
  704. }
  705. juce::Array<BlockDeviceConnection> getArrayOfConnections (const juce::Array<BlocksProtocol::DeviceConnection>& connections)
  706. {
  707. juce::Array<BlockDeviceConnection> result;
  708. for (auto&& c : connections)
  709. {
  710. BlockDeviceConnection dc;
  711. dc.device1 = getDeviceIDFromIndex (c.device1);
  712. dc.device2 = getDeviceIDFromIndex (c.device2);
  713. if (dc.device1 <= 0 || dc.device2 <= 0)
  714. continue;
  715. dc.connectionPortOnDevice1 = convertConnectionPort (dc.device1, c.port1);
  716. dc.connectionPortOnDevice2 = convertConnectionPort (dc.device2, c.port2);
  717. result.add (dc);
  718. }
  719. return result;
  720. }
  721. Block::ConnectionPort convertConnectionPort (Block::UID uid, BlocksProtocol::ConnectorPort p) noexcept
  722. {
  723. if (auto* info = getDeviceInfoFromUID (uid))
  724. return BlocksProtocol::BlockDataSheet (info->serial).convertPortIndexToConnectorPort (p);
  725. jassertfalse;
  726. return { Block::ConnectionPort::DeviceEdge::north, 0 };
  727. }
  728. //==============================================================================
  729. void handleIncomingMessage (const void* data, size_t dataSize)
  730. {
  731. juce::MemoryBlock mb (data, dataSize);
  732. {
  733. const juce::ScopedLock sl (incomingPacketLock);
  734. incomingPackets.add (std::move (mb));
  735. }
  736. triggerAsyncUpdate();
  737. #if DUMP_BANDWIDTH_STATS
  738. registerBytesIn ((int) dataSize);
  739. #endif
  740. }
  741. void handleAsyncUpdate() override
  742. {
  743. juce::Array<juce::MemoryBlock> packets;
  744. packets.ensureStorageAllocated (32);
  745. {
  746. const juce::ScopedLock sl (incomingPacketLock);
  747. incomingPackets.swapWith (packets);
  748. }
  749. for (auto& packet : packets)
  750. {
  751. auto data = static_cast<const uint8*> (packet.getData());
  752. BlocksProtocol::HostPacketDecoder<ConnectedDeviceGroup>
  753. ::processNextPacket (*this, *data, data + 1, (int) packet.getSize() - 1);
  754. }
  755. }
  756. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectedDeviceGroup)
  757. };
  758. //==============================================================================
  759. /** This is the main singleton object that keeps track of connected blocks */
  760. struct Detector : public juce::ReferenceCountedObject,
  761. private juce::Timer
  762. {
  763. Detector() : defaultDetector (new MIDIDeviceDetector()), deviceDetector (*defaultDetector)
  764. {
  765. startTimer (10);
  766. }
  767. Detector (DeviceDetector& dd) : deviceDetector (dd)
  768. {
  769. startTimer (10);
  770. }
  771. ~Detector()
  772. {
  773. jassert (activeTopologySources.isEmpty());
  774. }
  775. using Ptr = juce::ReferenceCountedObjectPtr<Detector>;
  776. static Detector::Ptr getDefaultDetector()
  777. {
  778. auto& d = getDefaultDetectorPointer();
  779. if (d == nullptr)
  780. d = new Detector();
  781. return d;
  782. }
  783. static Detector::Ptr& getDefaultDetectorPointer()
  784. {
  785. static Detector::Ptr defaultDetector;
  786. return defaultDetector;
  787. }
  788. void detach (PhysicalTopologySource* pts)
  789. {
  790. activeTopologySources.removeAllInstancesOf (pts);
  791. if (activeTopologySources.isEmpty())
  792. {
  793. for (auto& b : currentTopology.blocks)
  794. if (auto bi = BlockImplementation::getFrom (*b))
  795. bi->sendCommandMessage (BlocksProtocol::endAPIMode);
  796. currentTopology = {};
  797. lastTopology = {};
  798. auto& d = getDefaultDetectorPointer();
  799. if (d != nullptr && d->getReferenceCount() == 2)
  800. getDefaultDetectorPointer() = nullptr;
  801. }
  802. }
  803. bool isConnected (Block::UID deviceID) const noexcept
  804. {
  805. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  806. for (auto&& b : currentTopology.blocks)
  807. if (b->uid == deviceID)
  808. return true;
  809. return false;
  810. }
  811. const BlocksProtocol::DeviceStatus* getLastStatus (Block::UID deviceID) const noexcept
  812. {
  813. for (auto d : connectedDeviceGroups)
  814. if (auto status = d->getLastStatus (deviceID))
  815. return status;
  816. return nullptr;
  817. }
  818. void handleTopologyChange()
  819. {
  820. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  821. {
  822. juce::Array<DeviceInfo> newDeviceInfo;
  823. juce::Array<BlockDeviceConnection> newDeviceConnections;
  824. for (auto d : connectedDeviceGroups)
  825. {
  826. newDeviceInfo.addArray (d->getCurrentDeviceInfo());
  827. newDeviceConnections.addArray (d->getCurrentDeviceConnections());
  828. }
  829. for (int i = currentTopology.blocks.size(); --i >= 0;)
  830. {
  831. auto currentBlock = currentTopology.blocks.getUnchecked (i);
  832. auto newDeviceIter = std::find_if (newDeviceInfo.begin(), newDeviceInfo.end(),
  833. [&] (DeviceInfo& info) { return info.uid == currentBlock->uid; });
  834. auto* blockImpl = BlockImplementation::getFrom (*currentBlock);
  835. if (newDeviceIter == newDeviceInfo.end())
  836. {
  837. if (blockImpl != nullptr)
  838. blockImpl->markDisconnected();
  839. disconnectedBlocks.addIfNotAlreadyThere (currentTopology.blocks.removeAndReturn (i).get());
  840. }
  841. else
  842. {
  843. if (blockImpl != nullptr && blockImpl->wasPowerCycled())
  844. {
  845. blockImpl->resetPowerCycleFlag();
  846. blockImpl->markReconnected (newDeviceIter->version, newDeviceIter->name, newDeviceIter->isMaster);
  847. }
  848. updateCurrentBlockInfo (currentBlock, *newDeviceIter);
  849. }
  850. }
  851. static const int maxBlocksToSave = 100;
  852. if (disconnectedBlocks.size() > maxBlocksToSave)
  853. disconnectedBlocks.removeRange (0, 2 * (disconnectedBlocks.size() - maxBlocksToSave));
  854. for (auto& info : newDeviceInfo)
  855. if (info.serial.isValid() && ! containsBlockWithUID (currentTopology.blocks, getBlockUIDFromSerialNumber (info.serial)))
  856. addBlock (info);
  857. currentTopology.connections.swapWith (newDeviceConnections);
  858. }
  859. broadcastTopology();
  860. }
  861. void notifyBlockIsRestarting (Block::UID deviceID)
  862. {
  863. for (auto& group : connectedDeviceGroups)
  864. group->notifyBlockIsRestarting (deviceID);
  865. }
  866. void handleSharedDataACK (Block::UID deviceID, uint32 packetCounter) const
  867. {
  868. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  869. if (auto* bi = getBlockImplementationWithUID (deviceID))
  870. bi->handleSharedDataACK (packetCounter);
  871. }
  872. void handleFirmwareUpdateACK (Block::UID deviceID, uint8 resultCode, uint32 resultDetail)
  873. {
  874. if (auto* bi = getBlockImplementationWithUID (deviceID))
  875. bi->handleFirmwareUpdateACK (resultCode, resultDetail);
  876. }
  877. void handleConfigUpdateMessage (Block::UID deviceID, int32 item, int32 value, int32 min, int32 max)
  878. {
  879. if (auto* bi = getBlockImplementationWithUID (deviceID))
  880. bi->handleConfigUpdateMessage (item, value, min, max);
  881. }
  882. void notifyBlockOfConfigChange (BlockImplementation& bi, uint32 item)
  883. {
  884. if (auto configChangedCallback = bi.configChangedCallback)
  885. {
  886. if (item >= bi.getMaxConfigIndex())
  887. configChangedCallback (bi, {}, item);
  888. else
  889. configChangedCallback (bi, bi.getLocalConfigMetaData (item), item);
  890. }
  891. }
  892. void handleConfigSetMessage (Block::UID deviceID, int32 item, int32 value)
  893. {
  894. if (auto* bi = getBlockImplementationWithUID (deviceID))
  895. {
  896. bi->handleConfigSetMessage (item, value);
  897. notifyBlockOfConfigChange (*bi, uint32 (item));
  898. }
  899. }
  900. void handleConfigFactorySyncEndMessage (Block::UID deviceID)
  901. {
  902. if (auto* bi = getBlockImplementationWithUID (deviceID))
  903. notifyBlockOfConfigChange (*bi, bi->getMaxConfigIndex());
  904. }
  905. void handleConfigFactorySyncResetMessage (Block::UID deviceID)
  906. {
  907. if (auto* bi = getBlockImplementationWithUID (deviceID))
  908. bi->resetConfigListActiveStatus();
  909. }
  910. void handleLogMessage (Block::UID deviceID, const String& message) const
  911. {
  912. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  913. if (auto* bi = getBlockImplementationWithUID (deviceID))
  914. bi->handleLogMessage (message);
  915. }
  916. void handleButtonChange (Block::UID deviceID, Block::Timestamp timestamp, uint32 buttonIndex, bool isDown) const
  917. {
  918. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  919. if (auto* bi = getBlockImplementationWithUID (deviceID))
  920. {
  921. bi->pingFromDevice();
  922. if (isPositiveAndBelow (buttonIndex, bi->getButtons().size()))
  923. if (auto* cbi = dynamic_cast<ControlButtonImplementation*> (bi->getButtons().getUnchecked (int (buttonIndex))))
  924. cbi->broadcastButtonChange (timestamp, bi->modelData.buttons[(int) buttonIndex].type, isDown);
  925. }
  926. }
  927. void handleTouchChange (Block::UID deviceID, const TouchSurface::Touch& touchEvent)
  928. {
  929. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  930. auto block = currentTopology.getBlockWithUID (deviceID);
  931. if (block != nullptr)
  932. {
  933. if (auto* surface = dynamic_cast<TouchSurfaceImplementation*> (block->getTouchSurface()))
  934. {
  935. TouchSurface::Touch scaledEvent (touchEvent);
  936. scaledEvent.x *= block->getWidth();
  937. scaledEvent.y *= block->getHeight();
  938. scaledEvent.startX *= block->getWidth();
  939. scaledEvent.startY *= block->getHeight();
  940. surface->broadcastTouchChange (scaledEvent);
  941. }
  942. }
  943. }
  944. void cancelAllActiveTouches() noexcept
  945. {
  946. for (auto& block : currentTopology.blocks)
  947. if (auto* surface = block->getTouchSurface())
  948. surface->cancelAllActiveTouches();
  949. }
  950. void handleCustomMessage (Block::UID deviceID, Block::Timestamp timestamp, const int32* data)
  951. {
  952. if (auto* bi = getBlockImplementationWithUID (deviceID))
  953. bi->handleCustomMessage (timestamp, data);
  954. }
  955. //==============================================================================
  956. int getIndexFromDeviceID (Block::UID deviceID) const noexcept
  957. {
  958. for (auto* c : connectedDeviceGroups)
  959. {
  960. auto index = c->getIndexFromDeviceID (deviceID);
  961. if (index >= 0)
  962. return index;
  963. }
  964. return -1;
  965. }
  966. template <typename PacketBuilder>
  967. bool sendMessageToDevice (Block::UID deviceID, const PacketBuilder& builder) const
  968. {
  969. for (auto* c : connectedDeviceGroups)
  970. if (c->getIndexFromDeviceID (deviceID) >= 0)
  971. return c->sendMessageToDevice (builder);
  972. return false;
  973. }
  974. static Detector* getFrom (Block& b) noexcept
  975. {
  976. if (auto* bi = BlockImplementation::getFrom (b))
  977. return (bi->detector);
  978. jassertfalse;
  979. return nullptr;
  980. }
  981. DeviceConnection* getDeviceConnectionFor (const Block& b)
  982. {
  983. for (const auto& d : connectedDeviceGroups)
  984. {
  985. for (const auto& info : d->getCurrentDeviceInfo())
  986. {
  987. if (info.uid == b.uid)
  988. return d->getDeviceConnection();
  989. }
  990. }
  991. return nullptr;
  992. }
  993. const DeviceConnection* getDeviceConnectionFor (const Block& b) const
  994. {
  995. for (const auto& d : connectedDeviceGroups)
  996. {
  997. for (const auto& info : d->getCurrentDeviceInfo())
  998. {
  999. if (info.uid == b.uid)
  1000. return d->getDeviceConnection();
  1001. }
  1002. }
  1003. return nullptr;
  1004. }
  1005. std::unique_ptr<MIDIDeviceDetector> defaultDetector;
  1006. DeviceDetector& deviceDetector;
  1007. juce::Array<PhysicalTopologySource*> activeTopologySources;
  1008. BlockTopology currentTopology, lastTopology;
  1009. juce::ReferenceCountedArray<Block, CriticalSection> disconnectedBlocks;
  1010. private:
  1011. void timerCallback() override
  1012. {
  1013. startTimer (1500);
  1014. auto detectedDevices = deviceDetector.scanForDevices();
  1015. handleDevicesRemoved (detectedDevices);
  1016. handleDevicesAdded (detectedDevices);
  1017. }
  1018. void handleDevicesRemoved (const juce::StringArray& detectedDevices)
  1019. {
  1020. bool anyDevicesRemoved = false;
  1021. for (int i = connectedDeviceGroups.size(); --i >= 0;)
  1022. {
  1023. if (! connectedDeviceGroups.getUnchecked(i)->isStillConnected (detectedDevices))
  1024. {
  1025. connectedDeviceGroups.remove (i);
  1026. anyDevicesRemoved = true;
  1027. }
  1028. }
  1029. if (anyDevicesRemoved)
  1030. handleTopologyChange();
  1031. }
  1032. void handleDevicesAdded (const juce::StringArray& detectedDevices)
  1033. {
  1034. for (const auto& devName : detectedDevices)
  1035. {
  1036. if (! hasDeviceFor (devName))
  1037. {
  1038. if (auto d = deviceDetector.openDevice (detectedDevices.indexOf (devName)))
  1039. {
  1040. connectedDeviceGroups.add (new ConnectedDeviceGroup (*this, devName, d));
  1041. }
  1042. }
  1043. }
  1044. }
  1045. bool hasDeviceFor (const juce::String& devName) const
  1046. {
  1047. for (auto d : connectedDeviceGroups)
  1048. if (d->deviceName == devName)
  1049. return true;
  1050. return false;
  1051. }
  1052. void addBlock (DeviceInfo info)
  1053. {
  1054. if (! reactivateBlockIfKnown (info))
  1055. addNewBlock (info);
  1056. }
  1057. bool reactivateBlockIfKnown (DeviceInfo info)
  1058. {
  1059. const auto uid = getBlockUIDFromSerialNumber (info.serial);
  1060. for (int i = disconnectedBlocks.size(); --i >= 0;)
  1061. {
  1062. if (uid != disconnectedBlocks.getUnchecked (i)->uid)
  1063. continue;
  1064. auto block = disconnectedBlocks.removeAndReturn (i);
  1065. if (auto* blockImpl = BlockImplementation::getFrom (*block))
  1066. {
  1067. blockImpl->markReconnected (info.version, info.name, info.isMaster);
  1068. currentTopology.blocks.add (block);
  1069. return true;
  1070. }
  1071. }
  1072. return false;
  1073. }
  1074. void addNewBlock (DeviceInfo info)
  1075. {
  1076. currentTopology.blocks.add (new BlockImplementation (info.serial, *this, info.version,
  1077. info.name, info.isMaster));
  1078. }
  1079. void updateCurrentBlockInfo (Block::Ptr blockToUpdate, DeviceInfo& updatedInfo)
  1080. {
  1081. if (versionNumberChanged (updatedInfo, blockToUpdate->versionNumber))
  1082. setVersionNumberForBlock (updatedInfo, *blockToUpdate);
  1083. if (nameIsValid (updatedInfo))
  1084. setNameForBlock (updatedInfo, *blockToUpdate);
  1085. if (updatedInfo.isMaster != blockToUpdate->isMasterBlock())
  1086. BlockImplementation::getFrom (*blockToUpdate)->setToMaster (updatedInfo.isMaster);
  1087. }
  1088. BlockImplementation* getBlockImplementationWithUID (Block::UID deviceID) const noexcept
  1089. {
  1090. if (auto&& block = currentTopology.getBlockWithUID (deviceID))
  1091. return BlockImplementation::getFrom (*block);
  1092. return nullptr;
  1093. }
  1094. juce::OwnedArray<ConnectedDeviceGroup> connectedDeviceGroups;
  1095. //==============================================================================
  1096. void broadcastTopology()
  1097. {
  1098. if (currentTopology != lastTopology)
  1099. {
  1100. lastTopology = currentTopology;
  1101. BlocksTraverser traverser;
  1102. traverser.traverseBlockArray (currentTopology);
  1103. for (auto* d : activeTopologySources)
  1104. d->listeners.call ([] (TopologySource::Listener& l) { l.topologyChanged(); });
  1105. #if DUMP_TOPOLOGY
  1106. dumpTopology (lastTopology);
  1107. #endif
  1108. }
  1109. }
  1110. JUCE_DECLARE_WEAK_REFERENCEABLE (Detector)
  1111. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Detector)
  1112. };
  1113. //==============================================================================
  1114. /** This is a friend of the BlocksImplementation that will scan and set the
  1115. physical positions of the blocks */
  1116. struct BlocksTraverser
  1117. {
  1118. void traverseBlockArray (const BlockTopology& topology)
  1119. {
  1120. juce::Array<Block::UID> visited;
  1121. for (auto& block : topology.blocks)
  1122. {
  1123. if (block->isMasterBlock() && ! visited.contains (block->uid))
  1124. {
  1125. if (auto* bi = dynamic_cast<BlockImplementation*> (block))
  1126. {
  1127. bi->masterUID = {};
  1128. bi->position = {};
  1129. bi->rotation = 0;
  1130. }
  1131. layoutNeighbours (*block, topology, block->uid, visited);
  1132. }
  1133. }
  1134. }
  1135. // returns the distance from corner clockwise
  1136. int getUnitForIndex (Block::Ptr block, Block::ConnectionPort::DeviceEdge edge, int index)
  1137. {
  1138. if (block->getType() == Block::seaboardBlock)
  1139. {
  1140. if (edge == Block::ConnectionPort::DeviceEdge::north)
  1141. {
  1142. if (index == 0) return 1;
  1143. if (index == 1) return 4;
  1144. }
  1145. else if (edge != Block::ConnectionPort::DeviceEdge::south)
  1146. {
  1147. return 1;
  1148. }
  1149. }
  1150. if (edge == Block::ConnectionPort::DeviceEdge::south)
  1151. return block->getWidth() - (index + 1);
  1152. if (edge == Block::ConnectionPort::DeviceEdge::west)
  1153. return block->getHeight() - (index + 1);
  1154. return index;
  1155. }
  1156. // returns how often north needs to rotate by 90 degrees
  1157. int getRotationForEdge (Block::ConnectionPort::DeviceEdge edge)
  1158. {
  1159. switch (edge)
  1160. {
  1161. case Block::ConnectionPort::DeviceEdge::north: return 0;
  1162. case Block::ConnectionPort::DeviceEdge::east: return 1;
  1163. case Block::ConnectionPort::DeviceEdge::south: return 2;
  1164. case Block::ConnectionPort::DeviceEdge::west: return 3;
  1165. }
  1166. jassertfalse;
  1167. return 0;
  1168. }
  1169. void layoutNeighbours (Block::Ptr block, const BlockTopology& topology,
  1170. Block::UID masterUid, juce::Array<Block::UID>& visited)
  1171. {
  1172. visited.add (block->uid);
  1173. for (auto& connection : topology.connections)
  1174. {
  1175. if ((connection.device1 == block->uid && ! visited.contains (connection.device2))
  1176. || (connection.device2 == block->uid && ! visited.contains (connection.device1)))
  1177. {
  1178. const auto theirUid = connection.device1 == block->uid ? connection.device2 : connection.device1;
  1179. const auto neighbourPtr = topology.getBlockWithUID (theirUid);
  1180. if (auto* neighbour = dynamic_cast<BlockImplementation*> (neighbourPtr.get()))
  1181. {
  1182. const auto myBounds = block->getBlockAreaWithinLayout();
  1183. const auto& myPort = connection.device1 == block->uid ? connection.connectionPortOnDevice1 : connection.connectionPortOnDevice2;
  1184. const auto& theirPort = connection.device1 == block->uid ? connection.connectionPortOnDevice2 : connection.connectionPortOnDevice1;
  1185. const auto myOffset = getUnitForIndex (block, myPort.edge, myPort.index);
  1186. const auto theirOffset = getUnitForIndex (neighbourPtr, theirPort.edge, theirPort.index);
  1187. neighbour->masterUID = masterUid;
  1188. neighbour->rotation = (2 + block->getRotation()
  1189. + getRotationForEdge (myPort.edge)
  1190. - getRotationForEdge (theirPort.edge)) % 4;
  1191. Point<int> delta;
  1192. const auto theirBounds = neighbour->getBlockAreaWithinLayout();
  1193. switch ((block->getRotation() + getRotationForEdge (myPort.edge)) % 4)
  1194. {
  1195. case 0: // over me
  1196. delta = { myOffset - (theirBounds.getWidth() - (theirOffset + 1)), -theirBounds.getHeight() };
  1197. break;
  1198. case 1: // right of me
  1199. delta = { myBounds.getWidth(), myOffset - (theirBounds.getHeight() - (theirOffset + 1)) };
  1200. break;
  1201. case 2: // under me
  1202. delta = { (myBounds.getWidth() - (myOffset + 1)) - theirOffset, myBounds.getHeight() };
  1203. break;
  1204. case 3: // left of me
  1205. delta = { -theirBounds.getWidth(), (myBounds.getHeight() - (myOffset + 1)) - theirOffset };
  1206. break;
  1207. }
  1208. neighbour->position = myBounds.getPosition() + delta;
  1209. }
  1210. layoutNeighbours (neighbourPtr, topology, masterUid, visited);
  1211. }
  1212. }
  1213. }
  1214. };
  1215. //==============================================================================
  1216. struct BlockImplementation : public Block,
  1217. private MIDIDeviceConnection::Listener,
  1218. private Timer
  1219. {
  1220. BlockImplementation (const BlocksProtocol::BlockSerialNumber& serial,
  1221. Detector& detectorToUse,
  1222. BlocksProtocol::VersionNumber version,
  1223. BlocksProtocol::BlockName blockName,
  1224. bool isMasterBlock)
  1225. : Block (juce::String ((const char*) serial.serial, sizeof (serial.serial)),
  1226. juce::String ((const char*) version.version, version.length),
  1227. juce::String ((const char*) blockName.name, blockName.length)),
  1228. modelData (serial),
  1229. remoteHeap (modelData.programAndHeapSize),
  1230. detector (&detectorToUse),
  1231. isMaster (isMasterBlock)
  1232. {
  1233. if (modelData.hasTouchSurface)
  1234. touchSurface.reset (new TouchSurfaceImplementation (*this));
  1235. int i = 0;
  1236. for (auto&& b : modelData.buttons)
  1237. controlButtons.add (new ControlButtonImplementation (*this, i++, b));
  1238. if (modelData.lightGridWidth > 0 && modelData.lightGridHeight > 0)
  1239. ledGrid.reset (new LEDGridImplementation (*this));
  1240. for (auto&& s : modelData.statusLEDs)
  1241. statusLights.add (new StatusLightImplementation (*this, s));
  1242. updateMidiConnectionListener();
  1243. }
  1244. ~BlockImplementation()
  1245. {
  1246. if (listenerToMidiConnection != nullptr)
  1247. {
  1248. config.setDeviceComms (nullptr);
  1249. listenerToMidiConnection->removeListener (this);
  1250. }
  1251. }
  1252. void markDisconnected()
  1253. {
  1254. if (auto surface = dynamic_cast<TouchSurfaceImplementation*> (touchSurface.get()))
  1255. surface->disableTouchSurface();
  1256. }
  1257. void markReconnected (BlocksProtocol::VersionNumber newVersion, BlocksProtocol::BlockName newName, bool master)
  1258. {
  1259. versionNumber = getVersionString (newVersion);
  1260. name = getNameString (newName);
  1261. isMaster = master;
  1262. setProgram (nullptr);
  1263. remoteHeap.resetDeviceStateToUnknown();
  1264. if (auto surface = dynamic_cast<TouchSurfaceImplementation*> (touchSurface.get()))
  1265. surface->activateTouchSurface();
  1266. updateMidiConnectionListener();
  1267. }
  1268. void setToMaster (bool shouldBeMaster)
  1269. {
  1270. isMaster = shouldBeMaster;
  1271. }
  1272. void updateMidiConnectionListener()
  1273. {
  1274. if (detector == nullptr)
  1275. return;
  1276. listenerToMidiConnection = dynamic_cast<MIDIDeviceConnection*> (detector->getDeviceConnectionFor (*this));
  1277. if (listenerToMidiConnection != nullptr)
  1278. listenerToMidiConnection->addListener (this);
  1279. config.setDeviceComms (listenerToMidiConnection);
  1280. }
  1281. Type getType() const override { return modelData.apiType; }
  1282. juce::String getDeviceDescription() const override { return modelData.description; }
  1283. int getWidth() const override { return modelData.widthUnits; }
  1284. int getHeight() const override { return modelData.heightUnits; }
  1285. float getMillimetersPerUnit() const override { return 47.0f; }
  1286. bool isHardwareBlock() const override { return true; }
  1287. juce::Array<Block::ConnectionPort> getPorts() const override { return modelData.ports; }
  1288. bool isConnected() const override { return detector && detector->isConnected (uid); }
  1289. bool isMasterBlock() const override { return isMaster; }
  1290. Block::UID getConnectedMasterUID() const override { return masterUID; }
  1291. int getRotation() const override { return rotation; }
  1292. Rectangle<int> getBlockAreaWithinLayout() const override
  1293. {
  1294. if (rotation % 2 == 0)
  1295. return { position.getX(), position.getY(), modelData.widthUnits, modelData.heightUnits };
  1296. return { position.getX(), position.getY(), modelData.heightUnits, modelData.widthUnits };
  1297. }
  1298. TouchSurface* getTouchSurface() const override { return touchSurface.get(); }
  1299. LEDGrid* getLEDGrid() const override { return ledGrid.get(); }
  1300. LEDRow* getLEDRow() override
  1301. {
  1302. if (ledRow == nullptr && modelData.numLEDRowLEDs > 0)
  1303. ledRow.reset (new LEDRowImplementation (*this));
  1304. return ledRow.get();
  1305. }
  1306. juce::Array<ControlButton*> getButtons() const override
  1307. {
  1308. juce::Array<ControlButton*> result;
  1309. result.addArray (controlButtons);
  1310. return result;
  1311. }
  1312. juce::Array<StatusLight*> getStatusLights() const override
  1313. {
  1314. juce::Array<StatusLight*> result;
  1315. result.addArray (statusLights);
  1316. return result;
  1317. }
  1318. float getBatteryLevel() const override
  1319. {
  1320. if (detector == nullptr)
  1321. return 0.0f;
  1322. if (auto status = detector->getLastStatus (uid))
  1323. return status->batteryLevel.toUnipolarFloat();
  1324. return 0.0f;
  1325. }
  1326. bool isBatteryCharging() const override
  1327. {
  1328. if (detector == nullptr)
  1329. return false;
  1330. if (auto status = detector->getLastStatus (uid))
  1331. return status->batteryCharging.get() != 0;
  1332. return false;
  1333. }
  1334. bool supportsGraphics() const override
  1335. {
  1336. return false;
  1337. }
  1338. int getDeviceIndex() const noexcept
  1339. {
  1340. if (detector == nullptr)
  1341. return -1;
  1342. return isConnected() ? detector->getIndexFromDeviceID (uid) : -1;
  1343. }
  1344. template <typename PacketBuilder>
  1345. bool sendMessageToDevice (const PacketBuilder& builder)
  1346. {
  1347. if (detector != nullptr)
  1348. {
  1349. lastMessageSendTime = juce::Time::getCurrentTime();
  1350. return detector->sendMessageToDevice (uid, builder);
  1351. }
  1352. return false;
  1353. }
  1354. bool sendCommandMessage (uint32 commandID)
  1355. {
  1356. return buildAndSendPacket<64> ([commandID] (BlocksProtocol::HostPacketBuilder<64>& p)
  1357. { return p.deviceControlMessage (commandID); });
  1358. }
  1359. void handleCustomMessage (Block::Timestamp, const int32* data)
  1360. {
  1361. ProgramEventMessage m;
  1362. for (uint32 i = 0; i < BlocksProtocol::numProgramMessageInts; ++i)
  1363. m.values[i] = data[i];
  1364. programEventListeners.call ([&] (ProgramEventListener& l) { l.handleProgramEvent (*this, m); });
  1365. }
  1366. static BlockImplementation* getFrom (Block& b) noexcept
  1367. {
  1368. jassert (dynamic_cast<BlockImplementation*> (&b) != nullptr);
  1369. return dynamic_cast<BlockImplementation*> (&b);
  1370. }
  1371. //==============================================================================
  1372. std::function<void(const String&)> logger;
  1373. void setLogger (std::function<void(const String&)> newLogger) override
  1374. {
  1375. logger = newLogger;
  1376. }
  1377. void handleLogMessage (const String& message) const
  1378. {
  1379. if (logger != nullptr)
  1380. logger (message);
  1381. }
  1382. //==============================================================================
  1383. juce::Result setProgram (Program* newProgram) override
  1384. {
  1385. if (newProgram != nullptr && program.get() == newProgram)
  1386. {
  1387. jassertfalse;
  1388. return juce::Result::ok();
  1389. }
  1390. stopTimer();
  1391. {
  1392. std::unique_ptr<Program> p (newProgram);
  1393. if (program != nullptr
  1394. && newProgram != nullptr
  1395. && program->getLittleFootProgram() == newProgram->getLittleFootProgram())
  1396. return juce::Result::ok();
  1397. std::swap (program, p);
  1398. }
  1399. programSize = 0;
  1400. isProgramLoaded = shouldSaveProgramAsDefault = false;
  1401. if (program == nullptr)
  1402. {
  1403. remoteHeap.clear();
  1404. return juce::Result::ok();
  1405. }
  1406. littlefoot::Compiler compiler;
  1407. compiler.addNativeFunctions (PhysicalTopologySource::getStandardLittleFootFunctions());
  1408. const auto err = compiler.compile (program->getLittleFootProgram(), 512, program->getSearchPaths());
  1409. if (err.failed())
  1410. return err;
  1411. DBG ("Compiled littlefoot program, space needed: "
  1412. << (int) compiler.getCompiledProgram().getTotalSpaceNeeded() << " bytes");
  1413. if (compiler.getCompiledProgram().getTotalSpaceNeeded() > getMemorySize())
  1414. return Result::fail ("Program too large!");
  1415. const auto size = (size_t) compiler.compiledObjectCode.size();
  1416. programSize = (uint32) size;
  1417. remoteHeap.resetDataRangeToUnknown (0, remoteHeap.blockSize);
  1418. remoteHeap.clear();
  1419. remoteHeap.sendChanges (*this, true);
  1420. remoteHeap.resetDataRangeToUnknown (0, (uint32) size);
  1421. remoteHeap.setBytes (0, compiler.compiledObjectCode.begin(), size);
  1422. remoteHeap.sendChanges (*this, true);
  1423. this->resetConfigListActiveStatus();
  1424. if (auto changeCallback = this->configChangedCallback)
  1425. changeCallback (*this, {}, this->getMaxConfigIndex());
  1426. startTimer (20);
  1427. return juce::Result::ok();
  1428. }
  1429. Program* getProgram() const override { return program.get(); }
  1430. void sendProgramEvent (const ProgramEventMessage& message) override
  1431. {
  1432. static_assert (sizeof (ProgramEventMessage::values) == 4 * BlocksProtocol::numProgramMessageInts,
  1433. "Need to keep the internal and external messages structures the same");
  1434. if (remoteHeap.isProgramLoaded())
  1435. {
  1436. buildAndSendPacket<128> ([&message] (BlocksProtocol::HostPacketBuilder<128>& p)
  1437. { return p.addProgramEventMessage (message.values); });
  1438. }
  1439. }
  1440. void timerCallback() override
  1441. {
  1442. if (remoteHeap.isFullySynced() && remoteHeap.isProgramLoaded())
  1443. {
  1444. isProgramLoaded = true;
  1445. stopTimer();
  1446. if (shouldSaveProgramAsDefault)
  1447. doSaveProgramAsDefault();
  1448. if (programLoadedCallback != nullptr)
  1449. programLoadedCallback (*this);
  1450. }
  1451. else
  1452. {
  1453. startTimer (100);
  1454. }
  1455. }
  1456. void saveProgramAsDefault() override
  1457. {
  1458. shouldSaveProgramAsDefault = true;
  1459. if (! isTimerRunning() && isProgramLoaded)
  1460. doSaveProgramAsDefault();
  1461. }
  1462. uint32 getMemorySize() override
  1463. {
  1464. return modelData.programAndHeapSize;
  1465. }
  1466. uint32 getHeapMemorySize() override
  1467. {
  1468. jassert (isPositiveAndNotGreaterThan (programSize, modelData.programAndHeapSize));
  1469. return modelData.programAndHeapSize - programSize;
  1470. }
  1471. void setDataByte (size_t offset, uint8 value) override
  1472. {
  1473. remoteHeap.setByte (programSize + offset, value);
  1474. }
  1475. void setDataBytes (size_t offset, const void* newData, size_t num) override
  1476. {
  1477. remoteHeap.setBytes (programSize + offset, static_cast<const uint8*> (newData), num);
  1478. }
  1479. void setDataBits (uint32 startBit, uint32 numBits, uint32 value) override
  1480. {
  1481. remoteHeap.setBits (programSize * 8 + startBit, numBits, value);
  1482. }
  1483. uint8 getDataByte (size_t offset) override
  1484. {
  1485. return remoteHeap.getByte (programSize + offset);
  1486. }
  1487. void handleSharedDataACK (uint32 packetCounter) noexcept
  1488. {
  1489. pingFromDevice();
  1490. remoteHeap.handleACKFromDevice (*this, packetCounter);
  1491. }
  1492. bool sendFirmwareUpdatePacket (const uint8* data, uint8 size, std::function<void (uint8, uint32)> callback) override
  1493. {
  1494. firmwarePacketAckCallback = {};
  1495. if (buildAndSendPacket<256> ([data, size] (BlocksProtocol::HostPacketBuilder<256>& p)
  1496. { return p.addFirmwareUpdatePacket (data, size); }))
  1497. {
  1498. firmwarePacketAckCallback = callback;
  1499. return true;
  1500. }
  1501. return false;
  1502. }
  1503. void handleFirmwareUpdateACK (uint8 resultCode, uint32 resultDetail)
  1504. {
  1505. if (firmwarePacketAckCallback != nullptr)
  1506. {
  1507. firmwarePacketAckCallback (resultCode, resultDetail);
  1508. firmwarePacketAckCallback = {};
  1509. }
  1510. }
  1511. void handleConfigUpdateMessage (int32 item, int32 value, int32 min, int32 max)
  1512. {
  1513. config.handleConfigUpdateMessage (item, value, min, max);
  1514. }
  1515. void handleConfigSetMessage(int32 item, int32 value)
  1516. {
  1517. config.handleConfigSetMessage (item, value);
  1518. }
  1519. void pingFromDevice()
  1520. {
  1521. lastMessageReceiveTime = juce::Time::getCurrentTime();
  1522. }
  1523. void addDataInputPortListener (DataInputPortListener* listener) override
  1524. {
  1525. Block::addDataInputPortListener (listener);
  1526. if (auto midiInput = getMidiInput())
  1527. midiInput->start();
  1528. }
  1529. void sendMessage (const void* message, size_t messageSize) override
  1530. {
  1531. if (auto midiOutput = getMidiOutput())
  1532. midiOutput->sendMessageNow ({ message, (int) messageSize });
  1533. }
  1534. void handleTimerTick()
  1535. {
  1536. if (ledGrid != nullptr)
  1537. if (auto renderer = ledGrid->getRenderer())
  1538. renderer->renderLEDGrid (*ledGrid);
  1539. remoteHeap.sendChanges (*this, false);
  1540. if (lastMessageSendTime < juce::Time::getCurrentTime() - juce::RelativeTime::milliseconds (pingIntervalMs))
  1541. sendCommandMessage (BlocksProtocol::ping);
  1542. }
  1543. //==============================================================================
  1544. int32 getLocalConfigValue (uint32 item) override
  1545. {
  1546. initialiseDeviceIndexAndConnection();
  1547. return config.getItemValue ((BlocksProtocol::ConfigItemId) item);
  1548. }
  1549. void setLocalConfigValue (uint32 item, int32 value) override
  1550. {
  1551. initialiseDeviceIndexAndConnection();
  1552. config.setItemValue ((BlocksProtocol::ConfigItemId) item, value);
  1553. }
  1554. void setLocalConfigRange (uint32 item, int32 min, int32 max) override
  1555. {
  1556. initialiseDeviceIndexAndConnection();
  1557. config.setItemMin ((BlocksProtocol::ConfigItemId) item, min);
  1558. config.setItemMax ((BlocksProtocol::ConfigItemId) item, max);
  1559. }
  1560. void setLocalConfigItemActive (uint32 item, bool isActive) override
  1561. {
  1562. initialiseDeviceIndexAndConnection();
  1563. config.setItemActive ((BlocksProtocol::ConfigItemId) item, isActive);
  1564. }
  1565. bool isLocalConfigItemActive (uint32 item) override
  1566. {
  1567. initialiseDeviceIndexAndConnection();
  1568. return config.getItemActive ((BlocksProtocol::ConfigItemId) item);
  1569. }
  1570. uint32 getMaxConfigIndex() override
  1571. {
  1572. return uint32 (BlocksProtocol::maxConfigIndex);
  1573. }
  1574. bool isValidUserConfigIndex (uint32 item) override
  1575. {
  1576. return item >= (uint32) BlocksProtocol::ConfigItemId::user0
  1577. && item < (uint32) (BlocksProtocol::ConfigItemId::user0 + numberOfUserConfigs);
  1578. }
  1579. ConfigMetaData getLocalConfigMetaData (uint32 item) override
  1580. {
  1581. initialiseDeviceIndexAndConnection();
  1582. return config.getMetaData ((BlocksProtocol::ConfigItemId) item);
  1583. }
  1584. void requestFactoryConfigSync() override
  1585. {
  1586. initialiseDeviceIndexAndConnection();
  1587. config.requestFactoryConfigSync();
  1588. }
  1589. void resetConfigListActiveStatus() override
  1590. {
  1591. config.resetConfigListActiveStatus();
  1592. }
  1593. void setConfigChangedCallback (std::function<void(Block&, const ConfigMetaData&, uint32)> configChanged) override
  1594. {
  1595. configChangedCallback = std::move (configChanged);
  1596. }
  1597. void setProgramLoadedCallback (std::function<void(Block&)> programLoaded) override
  1598. {
  1599. programLoadedCallback = std::move (programLoaded);
  1600. }
  1601. bool setName (const juce::String& newName) override
  1602. {
  1603. return buildAndSendPacket<128> ([&newName] (BlocksProtocol::HostPacketBuilder<128>& p)
  1604. { return p.addSetBlockName (newName); });
  1605. }
  1606. void factoryReset() override
  1607. {
  1608. buildAndSendPacket<32> ([] (BlocksProtocol::HostPacketBuilder<32>& p)
  1609. { return p.addFactoryReset(); });
  1610. }
  1611. void blockReset() override
  1612. {
  1613. if (buildAndSendPacket<32> ([] (BlocksProtocol::HostPacketBuilder<32>& p)
  1614. { return p.addBlockReset(); }))
  1615. {
  1616. hasBeenPowerCycled = true;
  1617. if (detector != nullptr)
  1618. detector->notifyBlockIsRestarting (uid);
  1619. }
  1620. }
  1621. bool wasPowerCycled() const { return hasBeenPowerCycled; }
  1622. void resetPowerCycleFlag() { hasBeenPowerCycled = false; }
  1623. //==============================================================================
  1624. std::unique_ptr<TouchSurface> touchSurface;
  1625. juce::OwnedArray<ControlButton> controlButtons;
  1626. std::unique_ptr<LEDGridImplementation> ledGrid;
  1627. std::unique_ptr<LEDRowImplementation> ledRow;
  1628. juce::OwnedArray<StatusLight> statusLights;
  1629. BlocksProtocol::BlockDataSheet modelData;
  1630. MIDIDeviceConnection* listenerToMidiConnection = nullptr;
  1631. static constexpr int pingIntervalMs = 400;
  1632. static constexpr uint32 maxBlockSize = BlocksProtocol::padBlockProgramAndHeapSize;
  1633. static constexpr uint32 maxPacketCounter = BlocksProtocol::PacketCounter::maxValue;
  1634. static constexpr uint32 maxPacketSize = 200;
  1635. using PacketBuilder = BlocksProtocol::HostPacketBuilder<maxPacketSize>;
  1636. using RemoteHeapType = littlefoot::LittleFootRemoteHeap<BlockImplementation>;
  1637. RemoteHeapType remoteHeap;
  1638. WeakReference<Detector> detector;
  1639. juce::Time lastMessageSendTime, lastMessageReceiveTime;
  1640. BlockConfigManager config;
  1641. std::function<void(Block&, const ConfigMetaData&, uint32)> configChangedCallback;
  1642. std::function<void(Block&)> programLoadedCallback;
  1643. private:
  1644. std::unique_ptr<Program> program;
  1645. uint32 programSize = 0;
  1646. std::function<void(uint8, uint32)> firmwarePacketAckCallback;
  1647. bool isMaster = false;
  1648. Block::UID masterUID = {};
  1649. Point<int> position;
  1650. int rotation = 0;
  1651. friend BlocksTraverser;
  1652. bool isProgramLoaded = false;
  1653. bool shouldSaveProgramAsDefault = false;
  1654. bool hasBeenPowerCycled = false;
  1655. void initialiseDeviceIndexAndConnection()
  1656. {
  1657. config.setDeviceIndex ((TopologyIndex) getDeviceIndex());
  1658. config.setDeviceComms (listenerToMidiConnection);
  1659. }
  1660. const juce::MidiInput* getMidiInput() const
  1661. {
  1662. if (detector != nullptr)
  1663. if (auto c = dynamic_cast<const MIDIDeviceConnection*> (detector->getDeviceConnectionFor (*this)))
  1664. return c->midiInput.get();
  1665. jassertfalse;
  1666. return nullptr;
  1667. }
  1668. juce::MidiInput* getMidiInput()
  1669. {
  1670. return const_cast<juce::MidiInput*> (static_cast<const BlockImplementation&>(*this).getMidiInput());
  1671. }
  1672. const juce::MidiOutput* getMidiOutput() const
  1673. {
  1674. if (detector != nullptr)
  1675. if (auto c = dynamic_cast<const MIDIDeviceConnection*> (detector->getDeviceConnectionFor (*this)))
  1676. return c->midiOutput.get();
  1677. jassertfalse;
  1678. return nullptr;
  1679. }
  1680. juce::MidiOutput* getMidiOutput()
  1681. {
  1682. return const_cast<juce::MidiOutput*> (static_cast<const BlockImplementation&>(*this).getMidiOutput());
  1683. }
  1684. void handleIncomingMidiMessage (const juce::MidiMessage& message) override
  1685. {
  1686. dataInputPortListeners.call ([&] (DataInputPortListener& l) { l.handleIncomingDataPortMessage (*this, message.getRawData(),
  1687. (size_t) message.getRawDataSize()); });
  1688. }
  1689. void connectionBeingDeleted (const MIDIDeviceConnection& c) override
  1690. {
  1691. jassert (listenerToMidiConnection == &c);
  1692. juce::ignoreUnused (c);
  1693. listenerToMidiConnection->removeListener (this);
  1694. listenerToMidiConnection = nullptr;
  1695. config.setDeviceComms (nullptr);
  1696. }
  1697. void doSaveProgramAsDefault()
  1698. {
  1699. sendCommandMessage (BlocksProtocol::saveProgramAsDefault);
  1700. }
  1701. template<int packetBytes, typename PacketBuilderFn>
  1702. bool buildAndSendPacket (PacketBuilderFn buildFn)
  1703. {
  1704. auto index = getDeviceIndex();
  1705. if (index < 0)
  1706. {
  1707. jassertfalse;
  1708. return false;
  1709. }
  1710. BlocksProtocol::HostPacketBuilder<packetBytes> p;
  1711. p.writePacketSysexHeaderBytes ((BlocksProtocol::TopologyIndex) index);
  1712. if (! buildFn (p))
  1713. return false;
  1714. p.writePacketSysexFooter();
  1715. return sendMessageToDevice (p);
  1716. }
  1717. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BlockImplementation)
  1718. };
  1719. //==============================================================================
  1720. struct LEDRowImplementation : public LEDRow,
  1721. private Timer
  1722. {
  1723. LEDRowImplementation (BlockImplementation& b) : LEDRow (b)
  1724. {
  1725. startTimer (300);
  1726. }
  1727. void setButtonColour (uint32 index, LEDColour colour)
  1728. {
  1729. if (index < 10)
  1730. {
  1731. colours[index] = colour;
  1732. flush();
  1733. }
  1734. }
  1735. int getNumLEDs() const override
  1736. {
  1737. return static_cast<const BlockImplementation&> (block).modelData.numLEDRowLEDs;
  1738. }
  1739. void setLEDColour (int index, LEDColour colour) override
  1740. {
  1741. if ((uint32) index < 15u)
  1742. {
  1743. colours[10 + index] = colour;
  1744. flush();
  1745. }
  1746. }
  1747. void setOverlayColour (LEDColour colour) override
  1748. {
  1749. colours[25] = colour;
  1750. flush();
  1751. }
  1752. void resetOverlayColour() override
  1753. {
  1754. setOverlayColour ({});
  1755. }
  1756. private:
  1757. LEDColour colours[26];
  1758. void timerCallback() override
  1759. {
  1760. stopTimer();
  1761. loadProgramOntoBlock();
  1762. flush();
  1763. }
  1764. void loadProgramOntoBlock()
  1765. {
  1766. if (block.getProgram() == nullptr)
  1767. {
  1768. auto err = block.setProgram (new DefaultLEDGridProgram (block));
  1769. if (err.failed())
  1770. {
  1771. DBG (err.getErrorMessage());
  1772. jassertfalse;
  1773. }
  1774. }
  1775. }
  1776. void flush()
  1777. {
  1778. if (block.getProgram() != nullptr)
  1779. for (uint32 i = 0; i < (uint32) numElementsInArray (colours); ++i)
  1780. write565Colour (16 * i, colours[i]);
  1781. }
  1782. void write565Colour (uint32 bitIndex, LEDColour colour)
  1783. {
  1784. block.setDataBits (bitIndex, 5, colour.getRed() >> 3);
  1785. block.setDataBits (bitIndex + 5, 6, colour.getGreen() >> 2);
  1786. block.setDataBits (bitIndex + 11, 5, colour.getBlue() >> 3);
  1787. }
  1788. struct DefaultLEDGridProgram : public Block::Program
  1789. {
  1790. DefaultLEDGridProgram (Block& b) : Block::Program (b) {}
  1791. juce::String getLittleFootProgram() override
  1792. {
  1793. /* Data format:
  1794. 0: 10 x 5-6-5 bits for button LED RGBs
  1795. 20: 15 x 5-6-5 bits for LED row colours
  1796. 50: 1 x 5-6-5 bits for LED row overlay colour
  1797. */
  1798. return R"littlefoot(
  1799. #heapsize: 128
  1800. int getColour (int bitIndex)
  1801. {
  1802. return makeARGB (255,
  1803. getHeapBits (bitIndex, 5) << 3,
  1804. getHeapBits (bitIndex + 5, 6) << 2,
  1805. getHeapBits (bitIndex + 11, 5) << 3);
  1806. }
  1807. int getButtonColour (int index)
  1808. {
  1809. return getColour (16 * index);
  1810. }
  1811. int getLEDColour (int index)
  1812. {
  1813. if (getHeapInt (50))
  1814. return getColour (50 * 8);
  1815. return getColour (20 * 8 + 16 * index);
  1816. }
  1817. void repaint()
  1818. {
  1819. for (int x = 0; x < 15; ++x)
  1820. fillPixel (getLEDColour (x), x, 0);
  1821. for (int i = 0; i < 10; ++i)
  1822. fillPixel (getButtonColour (i), i, 1);
  1823. }
  1824. void handleMessage (int p1, int p2) {}
  1825. )littlefoot";
  1826. }
  1827. };
  1828. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LEDRowImplementation)
  1829. };
  1830. //==============================================================================
  1831. struct TouchSurfaceImplementation : public TouchSurface,
  1832. private juce::Timer
  1833. {
  1834. TouchSurfaceImplementation (BlockImplementation& b) : TouchSurface (b), blockImpl (b)
  1835. {
  1836. activateTouchSurface();
  1837. }
  1838. ~TouchSurfaceImplementation()
  1839. {
  1840. disableTouchSurface();
  1841. }
  1842. void activateTouchSurface()
  1843. {
  1844. startTimer (500);
  1845. }
  1846. void disableTouchSurface()
  1847. {
  1848. stopTimer();
  1849. }
  1850. int getNumberOfKeywaves() const noexcept override
  1851. {
  1852. return blockImpl.modelData.numKeywaves;
  1853. }
  1854. void broadcastTouchChange (const TouchSurface::Touch& touchEvent)
  1855. {
  1856. auto& status = touches.getValue (touchEvent);
  1857. // Fake a touch end if we receive a duplicate touch-start with no preceding touch-end (ie: comms error)
  1858. if (touchEvent.isTouchStart && status.isActive)
  1859. killTouch (touchEvent, status, juce::Time::getMillisecondCounter());
  1860. // Fake a touch start if we receive an unexpected event with no matching start event. (ie: comms error)
  1861. if (! touchEvent.isTouchStart && ! status.isActive)
  1862. {
  1863. TouchSurface::Touch t (touchEvent);
  1864. t.isTouchStart = true;
  1865. t.isTouchEnd = false;
  1866. if (t.zVelocity <= 0) t.zVelocity = status.lastStrikePressure;
  1867. if (t.zVelocity <= 0) t.zVelocity = t.z;
  1868. if (t.zVelocity <= 0) t.zVelocity = 0.9f;
  1869. listeners.call ([&] (TouchSurface::Listener& l) { l.touchChanged (*this, t); });
  1870. }
  1871. // Normal handling:
  1872. status.lastEventTime = juce::Time::getMillisecondCounter();
  1873. status.isActive = ! touchEvent.isTouchEnd;
  1874. if (touchEvent.isTouchStart)
  1875. status.lastStrikePressure = touchEvent.zVelocity;
  1876. listeners.call ([&] (TouchSurface::Listener& l) { l.touchChanged (*this, touchEvent); });
  1877. }
  1878. void timerCallback() override
  1879. {
  1880. // Find touches that seem to have become stuck, and fake a touch-end for them..
  1881. static const uint32 touchTimeOutMs = 500;
  1882. for (auto& t : touches)
  1883. {
  1884. auto& status = t.value;
  1885. auto now = juce::Time::getMillisecondCounter();
  1886. if (status.isActive && now > status.lastEventTime + touchTimeOutMs)
  1887. killTouch (t.touch, status, now);
  1888. }
  1889. }
  1890. struct TouchStatus
  1891. {
  1892. uint32 lastEventTime = 0;
  1893. float lastStrikePressure = 0;
  1894. bool isActive = false;
  1895. };
  1896. void killTouch (const TouchSurface::Touch& touch, TouchStatus& status, uint32 timeStamp) noexcept
  1897. {
  1898. jassert (status.isActive);
  1899. TouchSurface::Touch killTouch (touch);
  1900. killTouch.z = 0;
  1901. killTouch.xVelocity = 0;
  1902. killTouch.yVelocity = 0;
  1903. killTouch.zVelocity = -1.0f;
  1904. killTouch.eventTimestamp = timeStamp;
  1905. killTouch.isTouchStart = false;
  1906. killTouch.isTouchEnd = true;
  1907. listeners.call ([&] (TouchSurface::Listener& l) { l.touchChanged (*this, killTouch); });
  1908. status.isActive = false;
  1909. }
  1910. void cancelAllActiveTouches() noexcept override
  1911. {
  1912. const auto now = juce::Time::getMillisecondCounter();
  1913. for (auto& t : touches)
  1914. if (t.value.isActive)
  1915. killTouch (t.touch, t.value, now);
  1916. touches.clear();
  1917. }
  1918. BlockImplementation& blockImpl;
  1919. TouchList<TouchStatus> touches;
  1920. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TouchSurfaceImplementation)
  1921. };
  1922. //==============================================================================
  1923. struct ControlButtonImplementation : public ControlButton
  1924. {
  1925. ControlButtonImplementation (BlockImplementation& b, int index, BlocksProtocol::BlockDataSheet::ButtonInfo info)
  1926. : ControlButton (b), blockImpl (b), buttonInfo (info), buttonIndex (index)
  1927. {
  1928. }
  1929. ~ControlButtonImplementation()
  1930. {
  1931. }
  1932. ButtonFunction getType() const override { return buttonInfo.type; }
  1933. juce::String getName() const override { return BlocksProtocol::getButtonNameForFunction (buttonInfo.type); }
  1934. float getPositionX() const override { return buttonInfo.x; }
  1935. float getPositionY() const override { return buttonInfo.y; }
  1936. bool hasLight() const override { return blockImpl.isControlBlock(); }
  1937. bool setLightColour (LEDColour colour) override
  1938. {
  1939. if (hasLight())
  1940. {
  1941. if (auto row = blockImpl.ledRow.get())
  1942. {
  1943. row->setButtonColour ((uint32) buttonIndex, colour);
  1944. return true;
  1945. }
  1946. }
  1947. return false;
  1948. }
  1949. void broadcastButtonChange (Block::Timestamp timestamp, ControlButton::ButtonFunction button, bool isDown)
  1950. {
  1951. if (button == buttonInfo.type)
  1952. {
  1953. if (wasDown == isDown)
  1954. sendButtonChangeToListeners (timestamp, ! isDown);
  1955. sendButtonChangeToListeners (timestamp, isDown);
  1956. wasDown = isDown;
  1957. }
  1958. }
  1959. void sendButtonChangeToListeners (Block::Timestamp timestamp, bool isDown)
  1960. {
  1961. if (isDown)
  1962. listeners.call ([&] (ControlButton::Listener& l) { l.buttonPressed (*this, timestamp); });
  1963. else
  1964. listeners.call ([&] (ControlButton::Listener& l) { l.buttonReleased (*this, timestamp); });
  1965. }
  1966. BlockImplementation& blockImpl;
  1967. BlocksProtocol::BlockDataSheet::ButtonInfo buttonInfo;
  1968. int buttonIndex;
  1969. bool wasDown = false;
  1970. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ControlButtonImplementation)
  1971. };
  1972. //==============================================================================
  1973. struct StatusLightImplementation : public StatusLight
  1974. {
  1975. StatusLightImplementation (Block& b, BlocksProtocol::BlockDataSheet::StatusLEDInfo i) : StatusLight (b), info (i)
  1976. {
  1977. }
  1978. juce::String getName() const override { return info.name; }
  1979. bool setColour (LEDColour newColour) override
  1980. {
  1981. // XXX TODO!
  1982. juce::ignoreUnused (newColour);
  1983. return false;
  1984. }
  1985. BlocksProtocol::BlockDataSheet::StatusLEDInfo info;
  1986. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StatusLightImplementation)
  1987. };
  1988. //==============================================================================
  1989. struct LEDGridImplementation : public LEDGrid
  1990. {
  1991. LEDGridImplementation (BlockImplementation& b) : LEDGrid (b), blockImpl (b)
  1992. {
  1993. }
  1994. int getNumColumns() const override { return blockImpl.modelData.lightGridWidth; }
  1995. int getNumRows() const override { return blockImpl.modelData.lightGridHeight; }
  1996. BlockImplementation& blockImpl;
  1997. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LEDGridImplementation)
  1998. };
  1999. //==============================================================================
  2000. #if DUMP_TOPOLOGY
  2001. static juce::String idToSerialNum (const BlockTopology& topology, Block::UID uid)
  2002. {
  2003. for (auto* b : topology.blocks)
  2004. if (b->uid == uid)
  2005. return b->serialNumber;
  2006. return "???";
  2007. }
  2008. static juce::String portEdgeToString (Block::ConnectionPort port)
  2009. {
  2010. switch (port.edge)
  2011. {
  2012. case Block::ConnectionPort::DeviceEdge::north: return "north";
  2013. case Block::ConnectionPort::DeviceEdge::south: return "south";
  2014. case Block::ConnectionPort::DeviceEdge::east: return "east";
  2015. case Block::ConnectionPort::DeviceEdge::west: return "west";
  2016. }
  2017. return {};
  2018. }
  2019. static juce::String portToString (Block::ConnectionPort port)
  2020. {
  2021. return portEdgeToString (port) + "_" + juce::String (port.index);
  2022. }
  2023. static void dumpTopology (const BlockTopology& topology)
  2024. {
  2025. MemoryOutputStream m;
  2026. m << "=============================================================================" << newLine
  2027. << "Topology: " << topology.blocks.size() << " device(s)" << newLine
  2028. << newLine;
  2029. int index = 0;
  2030. for (auto block : topology.blocks)
  2031. {
  2032. m << "Device " << index++ << (block->isMasterBlock() ? ": (MASTER)" : ":") << newLine;
  2033. m << " Description: " << block->getDeviceDescription() << newLine
  2034. << " Serial: " << block->serialNumber << newLine;
  2035. if (auto bi = BlockImplementation::getFrom (*block))
  2036. m << " Short address: " << (int) bi->getDeviceIndex() << newLine;
  2037. m << " Battery level: " + juce::String (juce::roundToInt (100.0f * block->getBatteryLevel())) + "%" << newLine
  2038. << " Battery charging: " + juce::String (block->isBatteryCharging() ? "y" : "n") << newLine
  2039. << " Width: " << block->getWidth() << newLine
  2040. << " Height: " << block->getHeight() << newLine
  2041. << " Millimeters per unit: " << block->getMillimetersPerUnit() << newLine
  2042. << newLine;
  2043. }
  2044. for (auto& connection : topology.connections)
  2045. {
  2046. m << idToSerialNum (topology, connection.device1)
  2047. << ":" << portToString (connection.connectionPortOnDevice1)
  2048. << " <-> "
  2049. << idToSerialNum (topology, connection.device2)
  2050. << ":" << portToString (connection.connectionPortOnDevice2) << newLine;
  2051. }
  2052. m << "=============================================================================" << newLine;
  2053. Logger::outputDebugString (m.toString());
  2054. }
  2055. #endif
  2056. };
  2057. //==============================================================================
  2058. struct PhysicalTopologySource::DetectorHolder : private juce::Timer
  2059. {
  2060. DetectorHolder (PhysicalTopologySource& pts)
  2061. : topologySource (pts),
  2062. detector (Internal::Detector::getDefaultDetector())
  2063. {
  2064. startTimerHz (30);
  2065. }
  2066. DetectorHolder (PhysicalTopologySource& pts, DeviceDetector& dd)
  2067. : topologySource (pts),
  2068. detector (new Internal::Detector (dd))
  2069. {
  2070. startTimerHz (30);
  2071. }
  2072. void timerCallback() override
  2073. {
  2074. if (! topologySource.hasOwnServiceTimer())
  2075. handleTimerTick();
  2076. }
  2077. void handleTimerTick()
  2078. {
  2079. for (auto& b : detector->currentTopology.blocks)
  2080. if (auto bi = Internal::BlockImplementation::getFrom (*b))
  2081. bi->handleTimerTick();
  2082. }
  2083. PhysicalTopologySource& topologySource;
  2084. Internal::Detector::Ptr detector;
  2085. };
  2086. //==============================================================================
  2087. PhysicalTopologySource::PhysicalTopologySource (bool startDetached)
  2088. {
  2089. if (! startDetached)
  2090. setActive (true);
  2091. }
  2092. PhysicalTopologySource::PhysicalTopologySource (DeviceDetector& detectorToUse, bool startDetached)
  2093. : customDetector (&detectorToUse)
  2094. {
  2095. if (! startDetached)
  2096. setActive (true);
  2097. }
  2098. PhysicalTopologySource::~PhysicalTopologySource()
  2099. {
  2100. setActive (false);
  2101. }
  2102. void PhysicalTopologySource::setActive (bool shouldBeActive)
  2103. {
  2104. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  2105. if (isActive() == shouldBeActive)
  2106. return;
  2107. if (shouldBeActive)
  2108. {
  2109. if (customDetector == nullptr)
  2110. detector = std::make_unique<DetectorHolder>(*this);
  2111. else
  2112. detector = std::make_unique<DetectorHolder>(*this, *customDetector);
  2113. detector->detector->activeTopologySources.add (this);
  2114. }
  2115. else
  2116. {
  2117. detector->detector->detach (this);
  2118. detector.reset();
  2119. }
  2120. listeners.call ([](TopologySource::Listener& l){ l.topologyChanged(); });
  2121. }
  2122. bool PhysicalTopologySource::isActive() const
  2123. {
  2124. return detector != nullptr;
  2125. }
  2126. bool PhysicalTopologySource::isLockedFromOutside() const
  2127. {
  2128. if (detector != nullptr && detector->detector != nullptr)
  2129. return detector->detector->deviceDetector.isLockedFromOutside();
  2130. return false;
  2131. }
  2132. BlockTopology PhysicalTopologySource::getCurrentTopology() const
  2133. {
  2134. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  2135. if (detector != nullptr)
  2136. return detector->detector->currentTopology;
  2137. return {};
  2138. }
  2139. void PhysicalTopologySource::cancelAllActiveTouches() noexcept
  2140. {
  2141. if (detector != nullptr)
  2142. detector->detector->cancelAllActiveTouches();
  2143. }
  2144. bool PhysicalTopologySource::hasOwnServiceTimer() const { return false; }
  2145. void PhysicalTopologySource::handleTimerTick()
  2146. {
  2147. if (detector != nullptr)
  2148. detector->handleTimerTick();
  2149. }
  2150. PhysicalTopologySource::DeviceConnection::DeviceConnection() {}
  2151. PhysicalTopologySource::DeviceConnection::~DeviceConnection() {}
  2152. PhysicalTopologySource::DeviceDetector::DeviceDetector() {}
  2153. PhysicalTopologySource::DeviceDetector::~DeviceDetector() {}
  2154. const char* const* PhysicalTopologySource::getStandardLittleFootFunctions() noexcept
  2155. {
  2156. return BlocksProtocol::ledProgramLittleFootFunctions;
  2157. }
  2158. template <typename ListType>
  2159. static bool collectionsMatch (const ListType& list1, const ListType& list2) noexcept
  2160. {
  2161. if (list1.size() != list2.size())
  2162. return false;
  2163. for (auto&& b : list1)
  2164. if (! list2.contains (b))
  2165. return false;
  2166. return true;
  2167. }
  2168. bool BlockTopology::operator== (const BlockTopology& other) const noexcept
  2169. {
  2170. return collectionsMatch (connections, other.connections) && collectionsMatch (blocks, other.blocks);
  2171. }
  2172. bool BlockTopology::operator!= (const BlockTopology& other) const noexcept
  2173. {
  2174. return ! operator== (other);
  2175. }
  2176. bool BlockDeviceConnection::operator== (const BlockDeviceConnection& other) const noexcept
  2177. {
  2178. return (device1 == other.device1 && device2 == other.device2
  2179. && connectionPortOnDevice1 == other.connectionPortOnDevice1
  2180. && connectionPortOnDevice2 == other.connectionPortOnDevice2)
  2181. || (device1 == other.device2 && device2 == other.device1
  2182. && connectionPortOnDevice1 == other.connectionPortOnDevice2
  2183. && connectionPortOnDevice2 == other.connectionPortOnDevice1);
  2184. }
  2185. bool BlockDeviceConnection::operator!= (const BlockDeviceConnection& other) const noexcept
  2186. {
  2187. return ! operator== (other);
  2188. }
  2189. } // namespace juce