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.

124 lines
4.2KB

  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. struct MIDIDeviceConnection : public PhysicalTopologySource::DeviceConnection,
  20. public juce::MidiInputCallback
  21. {
  22. MIDIDeviceConnection() {}
  23. ~MIDIDeviceConnection()
  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. if (interprocessLock != nullptr)
  30. interprocessLock->exit();
  31. }
  32. bool lockAgainstOtherProcesses (const String& midiInName, const String& midiOutName)
  33. {
  34. interprocessLock.reset (new juce::InterProcessLock ("blocks_sdk_"
  35. + File::createLegalFileName (midiInName)
  36. + "_" + File::createLegalFileName (midiOutName)));
  37. if (interprocessLock->enter (500))
  38. return true;
  39. interprocessLock = nullptr;
  40. return false;
  41. }
  42. struct Listener
  43. {
  44. virtual ~Listener() {}
  45. virtual void handleIncomingMidiMessage (const juce::MidiMessage& message) = 0;
  46. virtual void connectionBeingDeleted (const MIDIDeviceConnection&) = 0;
  47. };
  48. void addListener (Listener* l)
  49. {
  50. juce::ScopedLock scopedLock (criticalSecton);
  51. listeners.add (l);
  52. }
  53. void removeListener (Listener* l)
  54. {
  55. juce::ScopedLock scopedLock (criticalSecton);
  56. listeners.remove (l);
  57. }
  58. bool sendMessageToDevice (const void* data, size_t dataSize) override
  59. {
  60. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED // This method must only be called from the message thread!
  61. jassert (dataSize > sizeof (BlocksProtocol::roliSysexHeader) + 2);
  62. jassert (memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader) - 1) == 0);
  63. jassert (static_cast<const uint8*> (data)[dataSize - 1] == 0xf7);
  64. if (midiOutput != nullptr)
  65. {
  66. midiOutput->sendMessageNow (juce::MidiMessage (data, (int) dataSize));
  67. return true;
  68. }
  69. return false;
  70. }
  71. void handleIncomingMidiMessage (juce::MidiInput*, const juce::MidiMessage& message) override
  72. {
  73. juce::ScopedTryLock lock (criticalSecton);
  74. if (lock.isLocked())
  75. {
  76. const auto data = message.getRawData();
  77. const int dataSize = message.getRawDataSize();
  78. const int bodySize = dataSize - (int) (sizeof (BlocksProtocol::roliSysexHeader) + 1);
  79. if (bodySize > 0 && memcmp (data, BlocksProtocol::roliSysexHeader, sizeof (BlocksProtocol::roliSysexHeader)) == 0)
  80. if (handleMessageFromDevice != nullptr)
  81. handleMessageFromDevice (data + sizeof (BlocksProtocol::roliSysexHeader), (size_t) bodySize);
  82. listeners.call ([&] (Listener& l) { l.handleIncomingMidiMessage (message); });
  83. }
  84. }
  85. std::unique_ptr<juce::MidiInput> midiInput;
  86. std::unique_ptr<juce::MidiOutput> midiOutput;
  87. juce::CriticalSection criticalSecton;
  88. private:
  89. juce::ListenerList<Listener> listeners;
  90. std::unique_ptr<juce::InterProcessLock> interprocessLock;
  91. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceConnection)
  92. };
  93. } // namespace juce