From cf94c7b1ba474809626eef8ed69b4796459ce1fb Mon Sep 17 00:00:00 2001 From: jimmy Date: Fri, 23 Oct 2026 13:57:22 +1300 Subject: [PATCH] Press the colours again when a restarted QLC+ links itself to the pad controller. The pad controller only took a QLC+ reload as one when it had to make the link to QLC+ itself. A restarted QLC+ often subscribes to the virtual port on its own as it opens the show, before the next check, so the colours stayed off. Each check now also notes the QLC+ process and port it feeds (qlc_feeds), and a new one counts as a reload too. Co-Authored-By: Claude Opus 5.5 --- padcontroller/midi.py | 46 ++++++++++++++++++++++---------- tests/test_padcontroller_midi.py | 22 ++++++++++++++- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/padcontroller/midi.py b/padcontroller/midi.py index 452dad7..2d4dabf 100644 --- a/padcontroller/midi.py +++ b/padcontroller/midi.py @@ -15,8 +15,12 @@ turns up) and re-links QLC+ to the virtual port after a restart, so QLC+ can sta QLC+ reopens its MIDI port when it loads a show, so a new link to QLC+ while the pad controller runs means the show has just loaded with every button off. -REOPEN_DELAY later it presses the last colour pads again (see PlayLayer.colours), -which it keeps in COLOURS_FILE so they outlive a restart of the pad controller. +That is a link the pad controller had to make, or one QLC+ made itself: a +restarted QLC+ subscribes to the virtual port on its own as it opens the show, +before the next check, so a QLC+ process or port fed that wasn't at the last +check counts too (qlc_feeds). REOPEN_DELAY later it presses the last colour pads +again (see PlayLayer.colours), which it keeps in COLOURS_FILE so they outlive a +restart of the pad controller. When one of the movers' movement pads goes off, SETTLE_DELAY later it nudges the Pan and Tilt knobs a step and puts them back, so QLC+ drops @@ -95,6 +99,7 @@ class AlsaPort: addr: str # "client:port" client: str name: str + pid: str | None = None # the client's process, for user clients to: set[str] = field(default_factory=set) sources: set[str] = field(default_factory=set) @@ -102,15 +107,15 @@ class AlsaPort: def parse_aconnect(text: str) -> list[AlsaPort]: """The ports in `aconnect -l` output, with who they connect to and from.""" ports: list[AlsaPort] = [] - client = client_name = None + client = client_name = pid = None for line in text.splitlines(): - m = re.match(r"client (\d+): '([^']+)'", line) + m = re.match(r"client (\d+): '([^']+)'(?:.*\bpid=(\d+))?", line) if m: - client, client_name = m.group(1), m.group(2) + client, client_name, pid = m.groups() continue m = re.match(r"\s+(\d+)\s+'([^']+)'", line) if m and client is not None: - ports.append(AlsaPort(f"{client}:{m.group(1)}", client_name, m.group(2).strip())) + ports.append(AlsaPort(f"{client}:{m.group(1)}", client_name, m.group(2).strip(), pid)) continue m = re.match(r"\s+(Connecting To|Connected From):(.*)", line) if m and ports: @@ -139,20 +144,31 @@ def missing_links(ports: list[AlsaPort], src_name: str | None) -> list[tuple[str return links -def ensure_links(src_name: str | None) -> bool: - """Make the missing links. True when one of them is to QLC+.""" +def qlc_feeds(ports: list[AlsaPort]) -> set[tuple[str | None, str]]: + """(pid, address) of each QLC+ port the pad controller's output is linked to.""" + ours_out = next((p for p in ports if p.client == "padcontroller-out"), None) + if ours_out is None: + return set() + return {(p.pid, p.addr) for p in ports if p.client == "qlcplus" and p.addr in ours_out.to} + + +def ensure_links(src_name: str | None) -> tuple[bool, set[tuple[str | None, str]]]: + """Make the missing links. Whether one of them was to QLC+, and the QLC+ ports fed after.""" try: out = subprocess.check_output(["aconnect", "-l"], text=True) except (FileNotFoundError, subprocess.CalledProcessError): - return False + return False, set() ports = parse_aconnect(out) - qlc = {p.addr for p in ports if p.client == "qlcplus"} + qlc = {p.addr: p.pid for p in ports if p.client == "qlcplus"} + fed = qlc_feeds(ports) to_qlc = False for src, dst in missing_links(ports, src_name): if subprocess.run(["aconnect", src, dst], capture_output=True).returncode == 0: print(f"Linked {src} -> {dst}", flush=True) - to_qlc = to_qlc or dst in qlc - return to_qlc + if dst in qlc: + to_qlc = True + fed.add((qlc[dst], dst)) + return to_qlc, fed def load_colours(layer: PlayLayer) -> None: @@ -208,7 +224,7 @@ def main() -> None: print(f"Source: {src_name}", flush=True) print(f"Virtual: {VIRTUAL_NAME} (tau={args.tau}s @ {args.rate} Hz)", flush=True) print("Pass notes through (Play held shifts bank 2 up 16); smooth CC. Ctrl+C to stop.", flush=True) - ensure_links(src_name) + _, fed = ensure_links(src_name) if not args.no_lamps: set_lamps() @@ -232,8 +248,10 @@ def main() -> None: else: print("Source: (pad gone, waiting)", flush=True) src_name = best_name - if ensure_links(src_name) and now >= reopen_from: + linked, now_fed = ensure_links(src_name) + if (linked or now_fed - fed) and now >= reopen_from: reopen_at = now + REOPEN_DELAY + fed = now_fed next_link_check = now + 2.0 if reopen_at is not None and now >= reopen_at: diff --git a/tests/test_padcontroller_midi.py b/tests/test_padcontroller_midi.py index b2f00e2..a5d0a2e 100644 --- a/tests/test_padcontroller_midi.py +++ b/tests/test_padcontroller_midi.py @@ -8,7 +8,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) -from padcontroller.midi import missing_links, parse_aconnect, pick_source # noqa: E402 +from padcontroller.midi import missing_links, parse_aconnect, pick_source, qlc_feeds # noqa: E402 BOTH = [ "Midi Through:Midi Through Port-0 14:0", @@ -71,3 +71,23 @@ def test_nothing_missing_when_linked(): text = ACONNECT.replace(" 0 'padcontroller '\n", " 0 'padcontroller '\n\tConnecting To: 131:0\n") assert missing_links(parse_aconnect(text), "SINCO:SINCO SMC-PAD-Master 20:1") == [] assert missing_links(parse_aconnect(text), None) == [] + + +LINKED = ACONNECT.replace(" 0 'padcontroller '\n", " 0 'padcontroller '\n\tConnecting To: 131:0\n") + + +def test_parse_aconnect_reads_pids(): + ports = {p.addr: p for p in parse_aconnect(ACONNECT)} + assert ports["131:0"].pid == "57635" + assert ports["20:0"].pid is None # a kernel client + + +def test_qlc_feeds_names_the_qlc_process(): + assert qlc_feeds(parse_aconnect(LINKED)) == {("57635", "131:0")} + assert qlc_feeds(parse_aconnect(ACONNECT)) == set() + + +def test_a_restarted_qlc_that_linked_itself_is_a_new_feed(): + before = qlc_feeds(parse_aconnect(LINKED)) + after = qlc_feeds(parse_aconnect(LINKED.replace("pid=57635", "pid=60001"))) + assert after - before == {("60001", "131:0")}