From 7c99be8cb53fab5b3e9b150f3d58221e795d9299 Mon Sep 17 00:00:00 2001 From: Eric Dahl Date: Sun, 15 Nov 2026 23:58:15 -0500 Subject: [PATCH] A controller whose output port appears after it connected gets that port, and its LEDs paint into it (#148) devicesChanged only reconnected controllers with no source, so an output port that appeared after its input was never opened. Also binds the one #expect((x?.y ?? []).contains(z)) in the tests to a local: Swift Testing passes that shape when x is nil. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VCgWWsoPKWUFiYf5pcC5B5 --- .../ControllerBridgeCore/Coordinator.swift | 19 ++++++++++++++--- .../LayersTests.swift | 21 ++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift b/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift index 404faa9..5aca477 100644 --- a/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift +++ b/Packages/ControllerBridgeKit/Sources/ControllerBridgeCore/Coordinator.swift @@ -140,7 +140,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() @@ -225,6 +231,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. @@ -245,8 +260,6 @@ public final class Coordinator { log.log("Couldn't open MIDI output \(destination.name): \(error)") } } - 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 7ff0bf1..6f3ddd1 100644 --- a/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift +++ b/Packages/ControllerBridgeKit/Tests/ControllerBridgeCoreTests/LayersTests.swift @@ -451,6 +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))) + } + }