From e58ec85eb2749750371789ec7b9a786d672dd822 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Wed, 9 Sep 2026 22:44:16 -0600 Subject: [PATCH 1/2] fix(qt): restore the Exclusive mode row on macOS (#748) System default on macOS is the CoreAudio exclusive backend: with `exclusive_mode` set, qbz-audio takes Hog Mode, pins the device and switches its nominal rate to the track (PR #391, shipped in 1.2.11 and never removed). The Tauri settings gated the toggle on `ALSA || (macos && SystemDefault)`; the Slint port copied only the ALSA half, and fc0d35e29 then hid the row on macOS on the premise that no backend there could honour it. So 2.0.x showed a grey row and 2.1.0 none, while the DB flag still engaged Hog Mode. - SettingsDoc publishes `backendIsCoreAudio` (macOS && System default); the row is visible and enabled on it. Linux and Windows are unchanged. - The backend-switch cascade goes through one predicate (`output_labels::backend_honours_exclusive`), so re-selecting Auto or System default on macOS no longer clears the flag. - The output LED reads EXCL on macOS System default + exclusive_mode instead of DEFAULT while the device is hogged. - Node test `scripts/test_qt_exclusive_gate.mjs` evaluates the row's real gate expressions across platforms; added to qt-runtime-gate.sh. Rust tests cover the predicate, the LED and the serde name QML reads. Verified by smoke on the Mac mini with a USB DAC: the row appears, Hog Mode is acquired, and browser audio moves to the built-in speakers while QBZ plays. qbz-qt suite 782/782 on Linux; 13/13 with cfg(macos) on the Mac. cc --- crates/qbz-qt/qml/settings/AudioSettings.qml | 24 +++-- crates/qbz-qt/src/output_labels.rs | 106 ++++++++++++++++++- crates/qbz-qt/src/settings_qt.rs | 49 ++++++++- scripts/qt-runtime-gate.sh | 1 + scripts/test_qt_exclusive_gate.mjs | 95 +++++++++++++++++ 5 files changed, 265 insertions(+), 10 deletions(-) create mode 100644 scripts/test_qt_exclusive_gate.mjs diff --git a/crates/qbz-qt/qml/settings/AudioSettings.qml b/crates/qbz-qt/qml/settings/AudioSettings.qml index 77390bd1b..c531d284a 100644 --- a/crates/qbz-qt/qml/settings/AudioSettings.qml +++ b/crates/qbz-qt/qml/settings/AudioSettings.qml @@ -272,10 +272,20 @@ Column { // =========================== BIT-PERFECT ============================= GroupHeader { kioskHost: root.kioskHost; text: QbzSession.tr("BIT-PERFECT", QbzSession.trRev) } SettingRow { kioskHost: root.kioskHost; - // Hidden where no backend can honour it: on Windows until WASAPI - // exclusive lands (Phase B publishes backendIsWasapi), and on macOS, - // whose only backend is System default. Linux is unchanged - visible - // always, enabled only on ALSA. + // Visible wherever a backend can honour it, enabled only on that + // backend. Per platform: + // + // LINUX: unchanged since 2026-08-27 - always visible, enabled on ALSA. + // + // MACOS: System default IS the CoreAudio exclusive backend. With + // `exclusive_mode` set, qbz-audio backend.rs + // `create_output_stream_with_exclusive_guard` pins the device, takes + // Hog Mode and switches its nominal rate to the track (PR #391, + // shipped in 1.2.11 and never removed). fc0d35e29 hid this row on + // macOS on the wrong premise that no backend there could honour it; + // #748 restored it. `backendIsCoreAudio` is published by + // settings_qt.rs only on macOS, so the `=== true` idiom keeps every + // other platform exactly where it was. // // WINDOWS: hidden on purpose, even now that Phase B publishes // `backendIsWasapi`. The exclusive path is chosen by the BACKEND @@ -286,13 +296,13 @@ Column { // control at all when the thing being judged IS bit-perfect. Whether // Windows should instead mirror the Linux split (backend = transport, // toggle = hw/plughw) is an open design question for the owner. - visible: QbzShell.isLinux + visible: QbzShell.isLinux || root.doc.backendIsCoreAudio === true label: QbzSession.tr("Exclusive mode", QbzSession.trRev) description: QbzSession.tr("Lock the device so no other app can resample it.", QbzSession.trRev) - rowEnabled: root.doc.backendIsAlsa === true || root.doc.backendIsWasapi === true + rowEnabled: root.doc.backendIsAlsa === true || root.doc.backendIsWasapi === true || root.doc.backendIsCoreAudio === true QbzToggle { kioskHost: root.kioskHost; checked: root.doc.exclusiveMode === true - enabled: root.doc.backendIsAlsa === true || root.doc.backendIsWasapi === true + enabled: root.doc.backendIsAlsa === true || root.doc.backendIsWasapi === true || root.doc.backendIsCoreAudio === true onToggled: function (v) { QbzBridge.settingsBool("exclusive-mode", v) } } } diff --git a/crates/qbz-qt/src/output_labels.rs b/crates/qbz-qt/src/output_labels.rs index a14be22f2..429b82035 100644 --- a/crates/qbz-qt/src/output_labels.rs +++ b/crates/qbz-qt/src/output_labels.rs @@ -62,9 +62,37 @@ impl Default for OutputLabels { } } +/// Whether `exclusive_mode` means anything on this backend — the ONE +/// predicate behind the Settings row's enabled state, the backend-switch +/// cascade and the EXCL LED. It mirrors the engine, which is the authority: +/// ALSA hw honours it everywhere (`alsa_backend.rs`); on macOS the System +/// default backend takes CoreAudio Hog Mode when it is set (PR #391, +/// `qbz-audio backend.rs::create_output_stream_with_exclusive_guard`), and +/// `None` (Auto) resolves to System default there exactly as qbz-player's +/// `using_coreaudio_exclusive` reads it. PipeWire / Pulse / JACK are +/// multiplexed servers; WASAPI's exclusive path is the backend choice itself +/// (5ba49a739), so the toggle is inert there and stays hidden. +/// +/// `macos` is a parameter rather than `cfg!` so both arms run on the Linux +/// gate; callers pass `cfg!(target_os = "macos")`. #748. +pub fn backend_honours_exclusive(backend: Option, macos: bool) -> bool { + match backend { + Some(AudioBackendType::Alsa) => true, + Some(AudioBackendType::SystemDefault) | None => macos, + Some(_) => false, + } +} + /// `settings.rs::output_labels`, ported verbatim — this mapping IS the -/// contract for the two LEDs; do not "improve" it. +/// contract for the two LEDs; do not "improve" it. The single addition since +/// the port is the macOS arm of System default (#748), documented inline. pub fn output_labels(audio: &AudioSettings) -> OutputLabels { + output_labels_for(audio, cfg!(target_os = "macos")) +} + +/// `output_labels` with the platform as a parameter so the macOS arm is +/// covered by the Linux gate. +pub fn output_labels_for(audio: &AudioSettings, macos: bool) -> OutputLabels { let (backend, backend_active) = match audio.backend_type { Some(AudioBackendType::PipeWire) => ("PIPEWIRE", true), Some(AudioBackendType::Alsa) => ("ALSA", true), @@ -112,7 +140,17 @@ pub fn output_labels(audio: &AudioSettings) -> OutputLabels { ("SHARED", false) } } - Some(AudioBackendType::SystemDefault) | None => ("DEFAULT", false), + // #748: on macOS, System default + `exclusive_mode` IS CoreAudio Hog + // Mode — the device is claimed and its nominal rate follows the track + // — so the LED lights EXCL exactly when the engine takes the guard. + // Off macOS the flag is inert on this backend and nothing lights. + Some(AudioBackendType::SystemDefault) | None => { + if macos && audio.exclusive_mode { + ("EXCL", true) + } else { + ("DEFAULT", false) + } + } }; OutputLabels { backend, @@ -323,4 +361,68 @@ mod tests { assert_eq!((l.backend, l.mode), ("SYST", "DEFAULT")); assert!(!l.backend_active && !l.mode_active); } + + #[test] + fn alsa_honours_the_exclusive_toggle_on_every_platform() { + for macos in [false, true] { + assert!(backend_honours_exclusive(Some(AudioBackendType::Alsa), macos)); + } + } + + #[test] + fn coreaudio_system_default_honours_the_toggle_only_on_macos() { + // #748: on macOS the System default backend takes CoreAudio Hog Mode + // when `exclusive_mode` is set (PR #391). `None` (Auto) resolves to + // System default there, exactly as qbz-player's + // `using_coreaudio_exclusive` reads it. + for backend in [Some(AudioBackendType::SystemDefault), None] { + assert!(backend_honours_exclusive(backend, true), "{backend:?} on macOS"); + assert!(!backend_honours_exclusive(backend, false), "{backend:?} off macOS"); + } + } + + #[test] + fn shared_servers_and_wasapi_never_honour_the_toggle() { + // WASAPI: the exclusive path is the backend choice itself; the toggle + // is hidden there (5ba49a739) and the cascade keeps clearing it. + for backend in [ + AudioBackendType::PipeWire, + AudioBackendType::Pulse, + AudioBackendType::Jack, + AudioBackendType::WasapiExclusive, + ] { + for macos in [false, true] { + assert!(!backend_honours_exclusive(Some(backend), macos), "{backend:?}"); + } + } + } + + #[test] + fn coreaudio_exclusive_lights_excl_only_on_macos() { + let mut s = settings(Some(AudioBackendType::SystemDefault)); + s.exclusive_mode = true; + let mac = output_labels_for(&s, true); + assert_eq!((mac.backend, mac.mode), ("SYST", "EXCL")); + assert!(mac.mode_active && !mac.backend_active); + // Auto on macOS resolves to System default in the player: same LED. + s.backend_type = None; + assert_eq!(output_labels_for(&s, true).mode, "EXCL"); + // Off macOS the flag is inert on System default: nothing lights up. + s.backend_type = Some(AudioBackendType::SystemDefault); + let linux = output_labels_for(&s, false); + assert_eq!(linux.mode, "DEFAULT"); + assert!(!linux.mode_active); + // Without the flag macOS stays on the unlit default. + s.exclusive_mode = false; + assert_eq!(output_labels_for(&s, true).mode, "DEFAULT"); + } + + #[test] + fn coreaudio_exclusive_keeps_the_software_slider_live() { + // Pin: exclusive on macOS drives the DAC's hardware volume and falls + // back to software on knob-only DACs, so the slider is never inert. + let mut s = settings(Some(AudioBackendType::SystemDefault)); + s.exclusive_mode = true; + assert!(!volume_locked(&s)); + } } diff --git a/crates/qbz-qt/src/settings_qt.rs b/crates/qbz-qt/src/settings_qt.rs index 20eafed68..a62652f8e 100644 --- a/crates/qbz-qt/src/settings_qt.rs +++ b/crates/qbz-qt/src/settings_qt.rs @@ -1943,6 +1943,12 @@ pub struct SettingsDoc { /// for why they are hidden rather than disabled. #[serde(rename = "backendIsWasapi")] pub backend_is_wasapi: bool, + /// True on macOS when the active backend is System default, i.e. the + /// CoreAudio path that honours `exclusive_mode` through Hog Mode + /// (PR #391). AudioSettings QML shows and enables the Exclusive-mode row + /// on it (#748). Never true off macOS, so `=== true` gates stay inert. + #[serde(rename = "backendIsCoreAudio")] + pub backend_is_coreaudio: bool, #[serde(rename = "backendIsPipewire")] pub backend_is_pipewire: bool, #[serde(rename = "backendIsJack")] @@ -2493,6 +2499,8 @@ pub async fn publish_snapshot() { backend_index: backend_index as i32 + 1, backend_is_alsa: active_backend == AudioBackendType::Alsa, backend_is_wasapi: active_backend == AudioBackendType::WasapiExclusive, + backend_is_coreaudio: cfg!(target_os = "macos") + && active_backend == AudioBackendType::SystemDefault, backend_is_pipewire: active_backend == AudioBackendType::PipeWire, backend_is_jack: active_backend == AudioBackendType::Jack, devices, @@ -3801,6 +3809,16 @@ async fn select_alsa_hardware_volume_control( publish_snapshot().await; } +/// Backend-switch cascade for `exclusive_mode`: the flag survives a switch +/// only onto a backend that honours it. That used to read "ALSA alone", which +/// on macOS wiped the CoreAudio Hog Mode setting every time Auto / System +/// default was re-selected — the #748 reporter's DB workaround died to +/// exactly this. One predicate with the Settings row and the EXCL LED +/// (`output_labels::backend_honours_exclusive`). +fn backend_switch_clears_exclusive(backend: AudioBackendType) -> bool { + !crate::output_labels::backend_honours_exclusive(Some(backend), cfg!(target_os = "macos")) +} + pub async fn settings_select(runtime: &Arc>, key: &str, index: usize) { match key { "alsa-hardware-volume-control" => { @@ -3859,7 +3877,7 @@ pub async fn settings_select(runtime: &Arc>, key: &st let _ = with_audio(|s| s.set_dac_passthrough(false)); let _ = with_audio(|s| s.set_pw_force_bitperfect(false)); } - if backend != AudioBackendType::Alsa { + if backend_switch_clears_exclusive(backend) { let _ = with_audio(|s| s.set_exclusive_mode(false)); } // GAPLESS IS DELIBERATELY NOT CASCADED — owner decision, 2026-07-31: @@ -4558,3 +4576,32 @@ mod local_tab_order_tests { assert!(!requires_alsa_direct_unity(&audio)); } } + +#[cfg(test)] +mod exclusive_gate_tests { + use super::*; + + #[test] + fn settings_doc_publishes_backend_is_coreaudio_under_the_name_qml_reads() { + // AudioSettings.qml gates the Exclusive-mode row on + // `root.doc.backendIsCoreAudio === true`; the serde rename is the + // contract between the two files. + let doc = SettingsDoc { + backend_is_coreaudio: true, + ..Default::default() + }; + let json = serde_json::to_value(&doc).expect("SettingsDoc serialises"); + assert_eq!(json["backendIsCoreAudio"], serde_json::Value::Bool(true)); + } + + #[test] + fn backend_switch_clears_exclusive_unless_the_backend_honours_it() { + assert!(!backend_switch_clears_exclusive(AudioBackendType::Alsa)); + assert!(backend_switch_clears_exclusive(AudioBackendType::PipeWire)); + // #748: System default is CoreAudio exclusive on macOS and inert elsewhere. + assert_eq!( + backend_switch_clears_exclusive(AudioBackendType::SystemDefault), + !cfg!(target_os = "macos") + ); + } +} diff --git a/scripts/qt-runtime-gate.sh b/scripts/qt-runtime-gate.sh index cca1be52a..635e016f3 100644 --- a/scripts/qt-runtime-gate.sh +++ b/scripts/qt-runtime-gate.sh @@ -7,6 +7,7 @@ node scripts/test_qt_local_views.mjs node scripts/test_qt_kiosk_art.mjs node scripts/test_qt_kiosk_navigation.mjs node scripts/test_qt_kiosk_feedback.mjs +node scripts/test_qt_exclusive_gate.mjs python3 scripts/qt-cargo.py test --manifest-path crates/Cargo.toml -p qbz-qt --no-fail-fast target_dir="${CARGO_TARGET_DIR:-$PWD/crates/target}" logs="$(mktemp -d "${TMPDIR:-/tmp}/qbz-qt-gate-XXXXXX")" diff --git a/scripts/test_qt_exclusive_gate.mjs b/scripts/test_qt_exclusive_gate.mjs new file mode 100644 index 000000000..7eeb271c6 --- /dev/null +++ b/scripts/test_qt_exclusive_gate.mjs @@ -0,0 +1,95 @@ +#!/usr/bin/env node +// Executes the REAL gate expressions of the Settings > Audio "Exclusive mode" +// row (visible / rowEnabled / toggle enabled) against a platform x backend +// matrix, the way test_qt_release_sort.mjs evaluates LibraryView bindings. +// +// #748: the row must be reachable on macOS, whose only backend (System +// default) honours `exclusive_mode` through CoreAudio Hog Mode (PR #391, +// qbz-audio backend.rs `create_output_stream_with_exclusive_guard`). It was +// hidden there by fc0d35e29 on the wrong premise that no macOS backend could +// honour it. Linux and Windows must keep their 2026-08-27 behaviour. +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import vm from 'node:vm'; + +const qml = fs.readFileSync( + new URL('../crates/qbz-qt/qml/settings/AudioSettings.qml', import.meta.url), + 'utf8', +); + +// Locate the SettingRow that carries the "Exclusive mode" label and return +// its full block (balanced braces), with // comments stripped so a comment +// can never satisfy a binding match. +function exclusiveRowBlock(source) { + const label = source.indexOf('QbzSession.tr("Exclusive mode"'); + assert.notEqual(label, -1, 'Exclusive mode row must exist'); + const start = source.lastIndexOf('SettingRow {', label); + assert.notEqual(start, -1, 'Exclusive mode label must sit inside a SettingRow'); + let end = source.indexOf('{', start) + 1; + let depth = 1; + while (depth && end < source.length) { + if (source[end] === '{') ++depth; + if (source[end] === '}') --depth; + ++end; + } + assert.equal(depth, 0, 'SettingRow block must be balanced'); + return source + .slice(start, end) + .split('\n') + .map(line => line.replace(/\/\/.*$/, '')) + .join('\n'); +} + +const block = exclusiveRowBlock(qml); +const binding = (text, name) => { + const m = text.match(new RegExp(`(?:^|[\\s;{])${name}:\\s*([^\\n]+)`)); + assert.ok(m, `binding "${name}" must be present`); + return m[1].trim(); +}; +const toggleStart = block.indexOf('QbzToggle {'); +assert.notEqual(toggleStart, -1, 'row must contain a QbzToggle'); +const rowPart = block.slice(0, toggleStart); +const togglePart = block.slice(toggleStart); + +const visibleExpr = binding(rowPart, 'visible'); +const rowEnabledExpr = binding(rowPart, 'rowEnabled'); +const toggleEnabledExpr = binding(togglePart, 'enabled'); + +// Evaluate a binding as QML would: an absent document field reads as +// undefined, so `undefined === true` is false and the row stays hidden. +function evaluate(expr, platform, doc) { + const QbzShell = { + isLinux: platform === 'linux', + isMacos: platform === 'macos', + isWindows: platform === 'windows', + }; + return Boolean(vm.runInNewContext(expr, { QbzShell, root: { doc } })); +} + +const cases = [ + // Linux: unchanged since 2026-08-27 — always visible, enabled only on ALSA. + { platform: 'linux', doc: { backendIsAlsa: true }, visible: true, enabled: true }, + { platform: 'linux', doc: { backendIsPipewire: true }, visible: true, enabled: false }, + { platform: 'linux', doc: {}, visible: true, enabled: false }, + // macOS: System default IS the CoreAudio exclusive backend (Hog Mode). + { platform: 'macos', doc: { backendIsCoreAudio: true }, visible: true, enabled: true }, + // macOS with the flag absent (older document): stays hidden, never a grey row. + { platform: 'macos', doc: {}, visible: false, enabled: false }, + // Windows: hidden on purpose (5ba49a739) — the exclusive path is the + // backend dropdown, the toggle changes nothing there. + { platform: 'windows', doc: {}, visible: false, enabled: false }, + { platform: 'windows', doc: { backendIsWasapi: true }, visible: false, enabled: true }, +]; + +for (const c of cases) { + const tag = `${c.platform} ${JSON.stringify(c.doc)}`; + assert.equal(evaluate(visibleExpr, c.platform, c.doc), c.visible, `visible on ${tag}`); + assert.equal(evaluate(rowEnabledExpr, c.platform, c.doc), c.enabled, `rowEnabled on ${tag}`); + assert.equal(evaluate(toggleEnabledExpr, c.platform, c.doc), c.enabled, `toggle enabled on ${tag}`); +} + +// The toggle must keep writing the same settings key the bridge dispatches +// ("exclusive-mode" -> AudioSettingsStore::set_exclusive_mode, Apply::Reinit). +assert.match(togglePart, /QbzBridge\.settingsBool\("exclusive-mode", v\)/); + +console.log('Exclusive mode row gates: Linux unchanged, macOS CoreAudio reachable, Windows hidden: passed'); From 7f10782e793823bb6237d946d566d4ae08d2dbba Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Wed, 9 Sep 2026 22:44:16 -0600 Subject: [PATCH 2/2] fix(qbzd): keep exclusive_mode across a backend change on macOS The TUI cascade cleared `exclusive_mode` on any non-ALSA backend. On macOS System default honours it through CoreAudio Hog Mode, so mirror the Qt shell's cascade (#748): the flag survives onto ALSA and, on macOS, System default; every other backend still resets it. The platform is a parameter so the macOS arm runs on the Linux gate. cc --- crates/qbzd/src/tui/screens/audio.rs | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/qbzd/src/tui/screens/audio.rs b/crates/qbzd/src/tui/screens/audio.rs index ba9f3b233..4a6369014 100644 --- a/crates/qbzd/src/tui/screens/audio.rs +++ b/crates/qbzd/src/tui/screens/audio.rs @@ -202,12 +202,24 @@ pub fn cascade_on_toggle(a: &mut StagedAudio, field: AField) { /// Backend-switch cascades (items 4-7), fired when Backend changes. The device /// reset (item 7) means the caller must re-enumerate for the new backend. pub fn cascade_on_backend_change(a: &mut StagedAudio) { + cascade_on_backend_change_for(a, cfg!(target_os = "macos")) +} + +/// `cascade_on_backend_change` with the platform as a parameter so the macOS +/// arm of item 5 runs on the Linux gate. +pub fn cascade_on_backend_change_for(a: &mut StagedAudio, macos: bool) { if a.backend != AudioBackendType::PipeWire { a.dac_passthrough = false; // item 4 a.pw_force_bitperfect = false; } - if a.backend != AudioBackendType::Alsa { - a.exclusive_mode = false; // item 5 + // Item 5: exclusive survives only onto a backend that honours it — ALSA, + // and on macOS System default, whose `exclusive_mode` is CoreAudio Hog + // Mode (PR #391). #748 parity with the Qt shell's cascade + // (`settings_qt::backend_switch_clears_exclusive`). + let honours_exclusive = a.backend == AudioBackendType::Alsa + || (macos && a.backend == AudioBackendType::SystemDefault); + if !honours_exclusive { + a.exclusive_mode = false; } if a.backend == AudioBackendType::Alsa { a.gapless_enabled = false; // item 6 @@ -1621,4 +1633,18 @@ mod tests { assert_eq!(keys[1].0, "audio.alsa_hardware_volume"); assert_eq!(keys[1].1, "true"); } + + #[test] + fn macos_system_default_keeps_exclusive_across_a_backend_change() { + // #748 parity with the Qt shell: System default on macOS honours + // exclusive_mode through CoreAudio Hog Mode, so the ALSA-only cascade + // must not clear it there. Off macOS the flag is inert and still resets. + let mut a = base(); + a.exclusive_mode = true; + a.backend = AudioBackendType::SystemDefault; + cascade_on_backend_change_for(&mut a, true); + assert!(a.exclusive_mode, "macOS keeps exclusive on System default"); + cascade_on_backend_change_for(&mut a, false); + assert!(!a.exclusive_mode, "off macOS the cascade still clears it"); + } }