diff --git a/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift b/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift index c600833..2914c7e 100644 --- a/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift +++ b/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift @@ -145,7 +145,13 @@ public final class Coordinator { refreshDevices() for index in controllers.indices { if let source = controllers[index].source { - if !midiSystem.sources.contains(where: { $0.id == source.id }) { disconnect(index, reason: .unplugged) } + if !midiSystem.sources.contains(where: { $0.id == source.id }) { + disconnect(index, reason: .unplugged) + } else if controllers[index].bridge.output == nil { + // Its input connected before its output port appeared (#148): open the port now and paint into it. + openOutput(index, for: source, profile: controllers[index].profile) + if controllers[index].bridge.output != nil { controllers[index].bridge.repaintLEDs() } + } } else if connect(index) { controllers[index].bridge.start() requestResend() @@ -230,6 +236,15 @@ public final class Coordinator { controllers[index].knownSourceID = source.id status.controllers[index].profile = profile.name status.controllers[index].lastProfile = profile.name + openOutput(index, for: source, profile: profile) + report(index, "Connected \(source.name)") + return true + } + + /// Opens the LED output for a connected controller: the chosen one, else the port named like its input, else + /// the mapping's hint, never a port the profile excludes. Logs where the LEDs go, or that they have nowhere to go. + private func openOutput(_ index: Int, for source: MIDIEndpoint, profile: ControllerProfile) { + let bridge = controllers[index].bridge let destinations = midiSystem.destinations let chosenOutput = controllers[index].chosenOutput.flatMap { id in destinations.first { $0.id == id } } // Layers of one unit share its port both ways (#79): a destination another layer has isn't taken from this one. @@ -257,8 +272,6 @@ public final class Coordinator { } else { log.log("No LED output found for \(source.name), so its LEDs stay dark") } - report(index, "Connected \(source.name)") - return true } /// Lists every MIDI source with the profile it matches. A source that matches none is listed, not bridged. diff --git a/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift b/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift index 824449a..2bce000 100644 --- a/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift +++ b/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift @@ -451,7 +451,25 @@ struct LayersTests { let (coordinator, midi) = try Self.realUnit(destinations: [Self.wdOut, renamedOut]) coordinator.feedback(OSCMessage("/Syndicate/Deck/1/Playing", [.int(1)])) #expect(midi.outputs[Self.wdOut.id]?.sent ?? [] == []) - #expect((midi.outputs[renamedOut.id]?.sent ?? []).contains(.noteOn(channel: 0, note: 1, velocity: 127))) + // Bound first: #expect((x?.y ?? []).contains(z)) passes when x is nil (Swift Testing's expansion of the call). + let sent = midi.outputs[renamedOut.id]?.sent ?? [] + #expect(sent.contains(.noteOn(channel: 0, note: 1, velocity: 127))) + } + + @Test("An output port that appears after its controller connected is opened, and the LEDs paint into it (#148)") + func lateOutputPortIsOpened() throws { + let midi = Rig.FakeMIDISystem(sources: [Self.controlsIn], destinations: []) + let coordinator = Coordinator( + profiles: try ControllerProfile.bundled(), midi: midi, osc: Rig.FakeOSC(), clock: Rig.FakeClock(), log: Rig.FakeLog() + ) + coordinator.start(controllers: [ControllerSettings(deck: 1, input: nil, output: nil, followLayers: false)], ledsEnabled: true) + coordinator.feedback(OSCMessage("/Syndicate/Deck/1/Playing", [.int(1)])) // no output yet: nowhere to send + + midi.destinations = [Self.controlsOut] + coordinator.devicesChanged() + coordinator.feedback(OSCMessage("/Syndicate/Deck/1/Playing", [.int(1)])) + let sent = midi.outputs[Self.controlsOut.id]?.sent ?? [] + #expect(sent.contains(.noteOn(channel: 0, note: 1, velocity: 127))) } @Test("Connecting says where the LEDs go, or that no output was found, so a dark unit can be diagnosed from the log")