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.

184 lines
5.8KB

  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 MIDIDeviceDetector : public PhysicalTopologySource::DeviceDetector
  20. {
  21. MIDIDeviceDetector() {}
  22. StringArray scanForDevices() override
  23. {
  24. StringArray result;
  25. for (auto& pair : findDevices())
  26. result.add (pair.input.identifier + " & " + pair.output.identifier);
  27. return result;
  28. }
  29. PhysicalTopologySource::DeviceConnection* openDevice (int index) override
  30. {
  31. const auto allDevices = findDevices();
  32. if (allDevices.size() > index)
  33. {
  34. const auto pair = allDevices[index];
  35. auto dev = std::make_unique<MIDIDeviceConnection>();
  36. if (auto lock = createMidiPortLock (pair.input.name, pair.output.name))
  37. {
  38. lockedFromOutside = false;
  39. dev->setLockAgainstOtherProcesses (lock);
  40. dev->midiInput = MidiInput::openDevice (pair.input.identifier, dev.get());
  41. dev->midiOutput = MidiOutput::openDevice (pair.output.identifier);
  42. if (dev->midiInput != nullptr)
  43. {
  44. dev->midiInput->start();
  45. return dev.release();
  46. }
  47. }
  48. else
  49. {
  50. lockedFromOutside = true;
  51. }
  52. }
  53. return nullptr;
  54. }
  55. bool isLockedFromOutside() const override
  56. {
  57. return lockedFromOutside && ! findDevices().isEmpty();
  58. }
  59. static bool isBlocksMidiDeviceName (const String& name)
  60. {
  61. return name.indexOf (" BLOCK") > 0 || name.indexOf (" Block") > 0;
  62. }
  63. static String cleanBlocksDeviceName (String name)
  64. {
  65. name = name.trim();
  66. if (name.endsWith (" IN)"))
  67. return name.dropLastCharacters (4);
  68. if (name.endsWith (" OUT)"))
  69. return name.dropLastCharacters (5);
  70. const int openBracketPosition = name.lastIndexOfChar ('[');
  71. if (openBracketPosition != -1 && name.endsWith ("]"))
  72. return name.dropLastCharacters (name.length() - openBracketPosition);
  73. return name;
  74. }
  75. struct MidiInputOutputPair
  76. {
  77. MidiDeviceInfo input, output;
  78. };
  79. static Array<MidiInputOutputPair> findDevices()
  80. {
  81. Array<MidiInputOutputPair> result;
  82. auto midiInputs = MidiInput::getAvailableDevices();
  83. auto midiOutputs = MidiOutput::getAvailableDevices();
  84. for (const auto& input : midiInputs)
  85. {
  86. if (isBlocksMidiDeviceName (input.name))
  87. {
  88. MidiInputOutputPair pair;
  89. pair.input = input;
  90. String cleanedInputName = cleanBlocksDeviceName (input.name);
  91. int inputOccurences = 0;
  92. int outputOccurences = 0;
  93. for (const auto& p : result)
  94. if (cleanBlocksDeviceName (p.input.name) == cleanedInputName)
  95. ++inputOccurences;
  96. for (const auto& output : midiOutputs)
  97. {
  98. if (cleanBlocksDeviceName (output.name) == cleanedInputName)
  99. {
  100. if (outputOccurences == inputOccurences)
  101. {
  102. pair.output = output;
  103. break;
  104. }
  105. ++outputOccurences;
  106. }
  107. }
  108. result.add (pair);
  109. }
  110. }
  111. return result;
  112. }
  113. private:
  114. bool lockedFromOutside = true;
  115. /** For backwards compatibility, the block interprocess lock has to use the midi input name.
  116. The below is necessary because blocks of the same type might duplicate a port name, so
  117. must share an interprocess lock.
  118. */
  119. std::shared_ptr<InterProcessLock> createMidiPortLock (const String& midiInName, const String& midiOutName)
  120. {
  121. const juce::String lockIdentifier = "blocks_sdk_"
  122. + File::createLegalFileName (midiInName)
  123. + "_" + File::createLegalFileName (midiOutName);
  124. const auto existingLock = midiPortLocks.find (lockIdentifier);
  125. if (existingLock != midiPortLocks.end())
  126. if (existingLock->second.use_count() > 0)
  127. return existingLock->second.lock();
  128. auto interprocessLock = std::make_shared<InterProcessLock> (lockIdentifier);
  129. if (interprocessLock->enter (500))
  130. {
  131. midiPortLocks[lockIdentifier] = interprocessLock;
  132. return interprocessLock;
  133. }
  134. return nullptr;
  135. }
  136. std::map<juce::String, std::weak_ptr<InterProcessLock>> midiPortLocks;
  137. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceDetector)
  138. };
  139. } // namespace juce