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.

114 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. struct MIDIDeviceConnection : public PhysicalTopologySource::DeviceConnection,
  20. public MidiInputCallback
  21. {
  22. MIDIDeviceConnection() {}
  23. ~MIDIDeviceConnection() override
  24. {
  25. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  26. listeners.call ([this] (Listener& l) { l.connectionBeingDeleted (*this); });
  27. if (midiInput != nullptr)
  28. midiInput->stop();
  29. }
  30. void setLockAgainstOtherProcesses (std::shared_ptr<InterProcessLock> newLock)
  31. {
  32. midiPortLock = newLock;
  33. }
  34. struct Listener
  35. {
  36. virtual ~Listener() {}
  37. virtual void handleIncomingMidiMessage (const MidiMessage& message) = 0;
  38. virtual void connectionBeingDeleted (const MIDIDeviceConnection&) = 0;
  39. };
  40. void addListener (Listener* l)
  41. {
  42. ScopedLock scopedLock (criticalSecton);
  43. listeners.add (l);
  44. }
  45. void removeListener (Listener* l)
  46. {
  47. ScopedLock scopedLock (criticalSecton);
  48. listeners.remove (l);
  49. }
  50. bool sendMessageToDevice (const void* data, size_t dataSize) override
  51. {
  52. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  53. jassert (dataSize > sizeof (BlocksProtocol::roliSysexHeader) + 1);
  54. jassert (memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader) - 1) == 0);
  55. jassert (static_cast<const uint8*> (data)[dataSize - 1] == 0xf7);
  56. if (midiOutput != nullptr)
  57. {
  58. midiOutput->sendMessageNow (MidiMessage (data, (int) dataSize));
  59. return true;
  60. }
  61. return false;
  62. }
  63. void handleIncomingMidiMessage (MidiInput*, const MidiMessage& message) override
  64. {
  65. ScopedTryLock lock (criticalSecton);
  66. if (lock.isLocked())
  67. {
  68. const auto data = message.getRawData();
  69. const int dataSize = message.getRawDataSize();
  70. const int bodySize = dataSize - (int) (sizeof (BlocksProtocol::roliSysexHeader) + 1);
  71. if (bodySize > 0 && memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader)) == 0)
  72. if (handleMessageFromDevice != nullptr)
  73. handleMessageFromDevice (data + sizeof (BlocksProtocol::roliSysexHeader), (size_t) bodySize);
  74. listeners.call ([&] (Listener& l) { l.handleIncomingMidiMessage (message); });
  75. }
  76. }
  77. std::unique_ptr<MidiInput> midiInput;
  78. std::unique_ptr<MidiOutput> midiOutput;
  79. CriticalSection criticalSecton;
  80. private:
  81. ListenerList<Listener> listeners;
  82. std::shared_ptr<InterProcessLock> midiPortLock;
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceConnection)
  84. };
  85. } // namespace juce