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.

2474 lines
86KB

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