From c0129cecfa19021aedc1a630598d662c04deb8b8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 22:24:07 +0200 Subject: [PATCH] feat: add configurable default MIDI controller Persist and validate ui.default_controller, expose it in Config, and inherit it for unassigned volume and mute MIDI learn actions while preserving explicit binding surfaces. Bump the local IPC protocol for the new snapshot and edit fields. Tests: cargo fmt --all -- --check; cargo check --workspace --all-targets --all-features; cargo test --workspace --all-targets --all-features; cargo test --workspace --doc --all-features; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo build --workspace --release --all-features; cargo check --workspace; cargo check --workspace --no-default-features; cargo check -p midi --no-default-features --features midir; cargo check -p volume_lib --no-default-features --features pulse --- README.md | 11 ++- architecture.md | 8 +- config/src/document.rs | 19 ++++ config/src/profile.rs | 16 +++ config/tests/editor.rs | 20 ++++ config/tests/schema.rs | 24 ++++- examples/profiles/arch-pipewire.toml | 3 + examples/profiles/desktop.toml | 3 + fader_master/src/editor.rs | 16 ++- fader_master/src/ipc.rs | 6 +- fader_master/src/live.rs | 1 + fader_master/src/tui_client.rs | 4 + task.md | 4 +- tui/src/lib.rs | 142 +++++++++++++++++++++++++-- 14 files changed, 256 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8b1652a..9aac2db 100644 --- a/README.md +++ b/README.md @@ -56,11 +56,12 @@ provides per-fader Size (`Slim`, `Standard`, or `Thick`) and visibility. Slim faders occupy one terminal row; Standard remains the compatible default. The separator name is entered in a centered popup for both add and rename actions. The Config tab is a settings/status page: change the session theme, switch the -persisted mixer layout between Vertical and Horizontal, restore individual -hidden faders, inspect backend and MIDI-device state, and apply or save the -server-owned draft. Horizontal mode packs fader cards side by side and keeps -separators full-width. The old `Shift+h` hidden-list mode and pipe-delimited -object editors are no longer used. +persisted mixer layout between Vertical and Horizontal, select the profile's +default MIDI controller, restore individual hidden faders, inspect backend and +MIDI-device state, and apply or save the server-owned draft. Unassigned volume +and mute controls inherit that default for MIDI learn. Horizontal mode packs +fader cards side by side and keeps separators full-width. The old `Shift+h` +hidden-list mode and pipe-delimited object editors are no longer used. # References diff --git a/architecture.md b/architecture.md index 995f692..7cb17ce 100644 --- a/architecture.md +++ b/architecture.md @@ -233,6 +233,9 @@ id = "desktop" startup = "adopt" new_stream = "adopt" +[ui] +default_controller = "desk-left" + [runtime] poll_interval_ms = 10 @@ -446,7 +449,10 @@ Every value presented as configuration is read from the server-owned validated profile draft and every edit targets that same profile field. The TUI must not invent parallel configuration defaults or overwrite unrelated fields; local state is limited to clearly transient navigation and presentation choices such -as selection, open dialogs, and the session theme. +as selection, open dialogs, and the session theme. `ui.default_controller` +references a configured MIDI surface. Unassigned volume and mute controls use +that surface for MIDI learn; an explicit binding continues to name its own +surface. The Config tab shows and changes this persisted default. Draft changes do not affect live routing or audio. Apply and Save remain distinct server-owned revisioned operations, validation errors preserve the diff --git a/config/src/document.rs b/config/src/document.rs index 4c9be66..98b9067 100644 --- a/config/src/document.rs +++ b/config/src/document.rs @@ -56,6 +56,25 @@ impl ProfileDocument { }) } + /// Selects the configured surface inherited by new MIDI assignments. + pub fn set_default_controller(&mut self, surface: Option<&str>) -> Result<(), ConfigError> { + self.transaction(|document| { + if document.get("ui").is_none() { + document["ui"] = Item::Table(Table::new()); + } + let ui = document + .get_mut("ui") + .and_then(Item::as_table_mut) + .ok_or_else(|| ConfigError::validation("ui", "must be a table"))?; + if let Some(surface) = surface { + ui["default_controller"] = value(surface); + } else { + ui.remove("default_controller"); + } + Ok(()) + }) + } + /// Changes one application's display label without rebuilding its table. pub fn set_application_label( &mut self, diff --git a/config/src/profile.rs b/config/src/profile.rs index 317fc9f..37cbe8d 100644 --- a/config/src/profile.rs +++ b/config/src/profile.rs @@ -39,6 +39,8 @@ const fn default_poll_interval_ms() -> u64 { struct UiSettings { #[serde(default)] mixer_orientation: MixerOrientation, + #[serde(default, skip_serializing_if = "Option::is_none")] + default_controller: Option, } /// Direction used to arrange fader displays in the mixer. @@ -391,6 +393,11 @@ impl Profile { self.raw.ui.mixer_orientation } + /// Surface selected automatically for new MIDI assignments in the TUI. + pub fn default_controller(&self) -> Option<&str> { + self.raw.ui.default_controller.as_deref() + } + /// Validated bindings. pub fn bindings(&self) -> &[Binding] { &self.raw.bindings @@ -724,6 +731,15 @@ fn validate_profile_members(raw: &RawProfile) -> Result<(), ConfigError> { let surfaces = unique_ids::(raw.surfaces.iter().map(|item| item.id.as_str()), "surfaces")?; + if let Some(default_controller) = &raw.ui.default_controller { + parse_id::(default_controller, "ui.default_controller")?; + if !surfaces.contains_key(default_controller) { + return Err(ConfigError::validation( + "ui.default_controller", + format!("unknown surface {default_controller:?}"), + )); + } + } for (index, surface) in raw.surfaces.iter().enumerate() { if surface.input_port.is_empty() { return Err(ConfigError::validation( diff --git a/config/tests/editor.rs b/config/tests/editor.rs index 8d0db7b..4061b6d 100644 --- a/config/tests/editor.rs +++ b/config/tests/editor.rs @@ -128,6 +128,26 @@ fn editing_group_members_preserves_the_configured_volume_mode_field() { ); } +#[test] +fn draft_sets_and_unsets_the_default_controller() { + let source = PROFILE.replace("[ui]\ndefault_controller = \"desk-left\"\n\n", ""); + let mut draft = ProfileDocument::parse(&source).unwrap(); + draft.set_default_controller(Some("desk-left")).unwrap(); + assert_eq!( + draft.preview().unwrap().default_controller(), + Some("desk-left") + ); + + let before_invalid = draft.source(); + let error = draft.set_default_controller(Some("missing")).unwrap_err(); + assert!(error.to_string().contains("unknown surface")); + assert_eq!(draft.source(), before_invalid); + + draft.set_default_controller(None).unwrap(); + assert_eq!(draft.preview().unwrap().default_controller(), None); + assert!(!draft.source().contains("default_controller")); +} + #[test] fn draft_adds_bindings_and_rejects_duplicate_physical_controls() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); diff --git a/config/tests/schema.rs b/config/tests/schema.rs index 2657b64..30621e5 100644 --- a/config/tests/schema.rs +++ b/config/tests/schema.rs @@ -98,10 +98,9 @@ fn mixer_layout_defaults_to_declared_faders_and_validates_explicit_entries() { let dangling = explicit.replace("fader = \"media-fader\"", "fader = \"missing\""); assert_error_contains(Profile::parse(&dangling), "unknown fader"); - let horizontal = EXAMPLE.replacen( - "[runtime]", - "[ui]\nmixer_orientation = \"horizontal\"\n\n[runtime]", - 1, + let horizontal = EXAMPLE.replace( + "[ui]\ndefault_controller = \"desk-left\"", + "[ui]\ndefault_controller = \"desk-left\"\nmixer_orientation = \"horizontal\"", ); assert_eq!( Profile::parse(&horizontal).unwrap().mixer_orientation(), @@ -109,6 +108,23 @@ fn mixer_layout_defaults_to_declared_faders_and_validates_explicit_entries() { ); } +#[test] +fn default_controller_is_optional_and_must_reference_a_surface() { + let profile = Profile::parse(EXAMPLE).expect("configured default controller must validate"); + assert_eq!(profile.default_controller(), Some("desk-left")); + + let missing = EXAMPLE.replace( + "default_controller = \"desk-left\"", + "default_controller = \"missing\"", + ); + assert_error_contains(Profile::parse(&missing), "unknown surface \"missing\""); + + let without_default = EXAMPLE.replace("[ui]\ndefault_controller = \"desk-left\"\n\n", ""); + let profile = + Profile::parse(&without_default).expect("the default controller remains optional"); + assert_eq!(profile.default_controller(), None); +} + #[test] fn runtime_poll_interval_defaults_and_validates_safe_overrides() { let without_runtime = EXAMPLE.replace("[runtime]\npoll_interval_ms = 10\n\n", ""); diff --git a/examples/profiles/arch-pipewire.toml b/examples/profiles/arch-pipewire.toml index 6277586..be7ec0b 100644 --- a/examples/profiles/arch-pipewire.toml +++ b/examples/profiles/arch-pipewire.toml @@ -3,6 +3,9 @@ id = "arch-pipewire" startup = "adopt" new_stream = "adopt" +[ui] +default_controller = "apc-mini" + [runtime] poll_interval_ms = 10 diff --git a/examples/profiles/desktop.toml b/examples/profiles/desktop.toml index 8277ba1..951cf5d 100644 --- a/examples/profiles/desktop.toml +++ b/examples/profiles/desktop.toml @@ -3,6 +3,9 @@ id = "desktop" startup = "adopt" new_stream = "adopt" +[ui] +default_controller = "desk-left" + [runtime] poll_interval_ms = 10 diff --git a/fader_master/src/editor.rs b/fader_master/src/editor.rs index bd135cd..67fdb48 100644 --- a/fader_master/src/editor.rs +++ b/fader_master/src/editor.rs @@ -214,6 +214,9 @@ impl ConfigurationEditor { } })? } + ConfigurationEdit::SetDefaultController { surface } => { + self.document.set_default_controller(surface.as_deref())? + } ConfigurationEdit::AddSeparator { id, label, @@ -396,7 +399,7 @@ mod tests { }, ) .unwrap(); - let profile = editor + editor .apply( 3, ConfigurationEdit::SetMixerOrientation { @@ -404,6 +407,14 @@ mod tests { }, ) .unwrap(); + let profile = editor + .apply( + 4, + ConfigurationEdit::SetDefaultController { + surface: Some("desk-left".into()), + }, + ) + .unwrap(); assert_eq!(profile.faders().len(), 1); assert_eq!(profile.mixer_layout().len(), 2); assert!(matches!( @@ -417,7 +428,8 @@ mod tests { profile.mixer_orientation(), fader_config::MixerOrientation::Horizontal ); - assert_eq!(editor.revision(), 4); + assert_eq!(profile.default_controller(), Some("desk-left")); + assert_eq!(editor.revision(), 5); } #[test] diff --git a/fader_master/src/ipc.rs b/fader_master/src/ipc.rs index be9dcc5..287579e 100644 --- a/fader_master/src/ipc.rs +++ b/fader_master/src/ipc.rs @@ -3,7 +3,7 @@ use std::io::{self, Read, Write}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use thiserror::Error; -pub const PROTOCOL_VERSION: u16 = 14; +pub const PROTOCOL_VERSION: u16 = 15; pub const MAX_FRAME_BYTES: usize = 64 * 1024; /// Kind of mixer object addressed by a client. @@ -164,6 +164,9 @@ pub enum ConfigurationEdit { SetMixerOrientation { orientation: MixerOrientation, }, + SetDefaultController { + surface: Option, + }, AddSeparator { id: String, label: String, @@ -212,6 +215,7 @@ pub struct ConfigurationSnapshot { pub error: Option, pub profile_id: String, pub mixer_orientation: MixerOrientation, + pub default_controller: Option, pub applications: Vec, pub groups: Vec, pub faders: Vec, diff --git a/fader_master/src/live.rs b/fader_master/src/live.rs index a1bb66a..fb40c7b 100644 --- a/fader_master/src/live.rs +++ b/fader_master/src/live.rs @@ -120,6 +120,7 @@ pub(crate) fn configuration_snapshot( fader_config::MixerOrientation::Vertical => crate::ipc::MixerOrientation::Vertical, fader_config::MixerOrientation::Horizontal => crate::ipc::MixerOrientation::Horizontal, }, + default_controller: profile.default_controller().map(str::to_owned), applications: profile .applications() .iter() diff --git a/fader_master/src/tui_client.rs b/fader_master/src/tui_client.rs index 763d853..b2c4ea2 100644 --- a/fader_master/src/tui_client.rs +++ b/fader_master/src/tui_client.rs @@ -103,6 +103,7 @@ impl IpcMixerClient { tui::MixerOrientation::Horizontal } }, + default_controller: snapshot.configuration.default_controller, applications: snapshot .configuration .applications @@ -369,6 +370,9 @@ fn configuration_edit(edit: TuiConfigurationEdit) -> crate::ipc::ConfigurationEd tui::MixerOrientation::Horizontal => crate::ipc::MixerOrientation::Horizontal, }, }, + TuiConfigurationEdit::SetDefaultController { surface } => { + IpcEdit::SetDefaultController { surface } + } TuiConfigurationEdit::AddSeparator { id, label, diff --git a/task.md b/task.md index a36bc64..4dbcac1 100644 --- a/task.md +++ b/task.md @@ -391,7 +391,9 @@ Dependencies: M8, M2, M1. conflicts retain the draft. Visibility uses the same form. The form also persists a per-fader Slim, Standard, or Thick size in the mixer layout. Existing profiles default to Standard, while Slim renders in one - terminal row. + terminal row. Config stores a validated default controller; unassigned volume + and mute learn actions inherit it while explicit bindings keep their selected + surface. - [ ] **M9.6 Single-screen configuration workflow:** remove the normalized Config table browser once all its operations are reachable from the mixer, keep Apply/Save and draft/live/durable status visible, and expose hidden-item diff --git a/tui/src/lib.rs b/tui/src/lib.rs index 33bb7c1..7dcf631 100644 --- a/tui/src/lib.rs +++ b/tui/src/lib.rs @@ -88,6 +88,7 @@ pub struct ConfigurationSnapshot { pub error: Option, pub profile_id: String, pub mixer_orientation: MixerOrientation, + pub default_controller: Option, pub applications: Vec, pub groups: Vec, pub faders: Vec, @@ -255,6 +256,9 @@ pub enum ConfigurationEdit { SetMixerOrientation { orientation: MixerOrientation, }, + SetDefaultController { + surface: Option, + }, AddSeparator { id: String, label: String, @@ -840,6 +844,7 @@ struct EditorInput { enum SettingsRow { Theme, MixerOrientation, + DefaultController, HiddenFader { id: String, label: String }, CancelMidiLearn, Apply, @@ -1263,7 +1268,11 @@ impl LiveTui { } fn settings_rows(&self) -> Vec { - let mut rows = vec![SettingsRow::Theme, SettingsRow::MixerOrientation]; + let mut rows = vec![ + SettingsRow::Theme, + SettingsRow::MixerOrientation, + SettingsRow::DefaultController, + ]; rows.extend(self.snapshot.configuration.mixer_layout.iter().filter_map( |entry| match entry { MixerLayoutEntry::Fader { @@ -1338,6 +1347,31 @@ impl LiveTui { orientation: self.snapshot.configuration.mixer_orientation.toggled(), }); } + SettingsRow::DefaultController => { + let devices = &self.snapshot.devices; + let current = self.snapshot.configuration.default_controller.as_deref(); + let next = if devices.is_empty() { + None + } else if forward { + current + .and_then(|id| devices.iter().position(|device| device.id == id)) + .and_then(|index| devices.get(index + 1)) + .map(|device| device.id.clone()) + .or_else(|| current.is_none().then(|| devices[0].id.clone())) + } else { + current + .and_then(|id| devices.iter().position(|device| device.id == id)) + .and_then(|index| index.checked_sub(1)) + .and_then(|index| devices.get(index)) + .map(|device| device.id.clone()) + .or_else(|| { + current + .is_none() + .then(|| devices[devices.len() - 1].id.clone()) + }) + }; + self.apply_layout_edit(ConfigurationEdit::SetDefaultController { surface: next }); + } SettingsRow::HiddenFader { id, .. } if forward => { self.apply_layout_edit(ConfigurationEdit::SetFaderHidden { id, hidden: false }); self.clamp_selection(); @@ -1457,12 +1491,16 @@ impl LiveTui { .iter() .find(|binding| binding.fader == editor.fader && binding.action == action); let key = (editor.fader.clone(), action.to_owned()); + let inherited_default = !self.controller_selections.contains_key(&key) + && binding.is_none() + && self.snapshot.configuration.default_controller.is_some(); let surface = self .controller_selections .get(&key) .cloned() - .or_else(|| binding.map(|binding| binding.surface.clone())); - let controller = surface.as_deref().map_or_else( + .or_else(|| binding.map(|binding| binding.surface.clone())) + .or_else(|| self.snapshot.configuration.default_controller.clone()); + let mut controller = surface.as_deref().map_or_else( || "Not selected".to_owned(), |surface| { self.snapshot @@ -1472,6 +1510,9 @@ impl LiveTui { .map_or_else(|| surface.to_owned(), |device| device.label.clone()) }, ); + if inherited_default { + controller = format!("Default · {controller}"); + } let message = binding.map_or_else( || "Unassigned".to_owned(), |binding| binding.message.clone(), @@ -2432,6 +2473,19 @@ impl LiveTui { SettingsRow::MixerOrientation => { format!("Mixer layout {} [←→]", config.mixer_orientation.label()) } + SettingsRow::DefaultController => { + let controller = config.default_controller.as_deref().map_or_else( + || "None".to_owned(), + |id| { + self.snapshot + .devices + .iter() + .find(|device| device.id == id) + .map_or_else(|| id.to_owned(), |device| device.label.clone()) + }, + ); + format!("Default controller {controller} [←→]") + } SettingsRow::HiddenFader { label, .. } => { format!("Show hidden fader {label} [Enter]") } @@ -2967,6 +3021,9 @@ mod tests { ConfigurationEdit::SetMixerOrientation { orientation } => { self.snapshot.configuration.mixer_orientation = *orientation; } + ConfigurationEdit::SetDefaultController { surface } => { + self.snapshot.configuration.default_controller.clone_from(surface); + } ConfigurationEdit::AddSeparator { id, label, @@ -3320,6 +3377,7 @@ mod tests { assert!(hidden_rendered.contains("Show hidden fader Music")); assert!(hidden_rendered.contains("Show hidden fader Browser")); + app.handle_key_event(press(KeyCode::Down)); app.handle_key_event(press(KeyCode::Down)); app.handle_key_event(press(KeyCode::Down)); app.handle_key_event(press(KeyCode::Enter)); @@ -3383,6 +3441,7 @@ mod tests { channel: 1, number: 7, }]; + snapshot.configuration.default_controller = Some("spare".into()); snapshot.devices = vec![ MidiDevice { id: "desk".into(), @@ -3421,6 +3480,7 @@ mod tests { assert!(rendered.contains("Learn volume MIDI")); assert!(rendered.contains("Unlearn volume MIDI")); assert!(rendered.contains("Mute controller")); + assert!(rendered.contains("Mute controller Default · Spare controller")); assert!(rendered.contains("Learn mute MIDI")); assert!(rendered.contains("Unlearn mute MIDI")); assert!(rendered.contains("Size Standard")); @@ -3492,6 +3552,53 @@ mod tests { ))); } + #[test] + fn unassigned_midi_actions_learn_from_the_configured_default_controller() { + let mut snapshot = DemoClient::default().view(); + snapshot.items[0].id.id = "music".into(); + snapshot.configuration.applications = vec![ConfiguredApplication { + id: "music".into(), + label: "Music".into(), + selectors: Vec::new(), + }]; + snapshot.configuration.faders = vec![ConfiguredFader { + id: "music-fader".into(), + target: snapshot.items[0].id.clone(), + pickup: true, + pickup_tolerance: 0.02, + }]; + snapshot.configuration.mixer_layout = vec![MixerLayoutEntry::Fader { + fader: "music-fader".into(), + hidden: false, + density: FaderDensity::Standard, + }]; + snapshot.configuration.default_controller = Some("desk".into()); + snapshot.devices = vec![MidiDevice { + id: "desk".into(), + label: "Desk controller".into(), + status: "connected".into(), + backend_id: Some("20:0".into()), + error: None, + }]; + let edits = Arc::new(Mutex::new(Vec::new())); + let mut app = LiveTui::new(LayoutClient { snapshot, edits }).unwrap(); + + app.handle_key_event(press(KeyCode::Char('e'))); + let rows = app.fader_editor_rows(); + assert!(matches!( + &rows[0], + FaderEditorRow::BindingController { value, .. } + if value == "Default · Desk controller" + )); + app.fader_editor.as_mut().unwrap().selected = 1; + app.handle_key_event(press(KeyCode::Enter)); + + assert_eq!( + app.snapshot.configuration.learning.as_deref(), + Some("desk -> music-fader (volume)") + ); + } + #[test] fn fader_density_cycles_and_slim_rows_use_one_terminal_line() { let mut snapshot = DemoClient::default().view(); @@ -3702,6 +3809,7 @@ mod tests { error: None, profile_id: "desktop".into(), mixer_orientation: MixerOrientation::Vertical, + default_controller: Some("desk-left".into()), applications: vec![ConfiguredApplication { id: "browser".into(), label: "Browser".into(), @@ -3765,6 +3873,7 @@ mod tests { assert!(rendered.contains("Profile: desktop")); assert!(rendered.contains("unsaved draft")); assert!(rendered.contains("Theme Default")); + assert!(rendered.contains("Default controller Desk controller")); assert!(rendered.contains("Hidden faders none")); assert!(rendered.contains("Apply draft")); assert!(rendered.contains("Save draft")); @@ -3775,8 +3884,16 @@ mod tests { #[test] fn configuration_tab_changes_theme_without_raw_object_editors() { let edits = Arc::new(Mutex::new(Vec::new())); + let mut snapshot = DemoClient::default().view(); + snapshot.devices.push(MidiDevice { + id: "desk".into(), + label: "Desk controller".into(), + status: "connected".into(), + backend_id: None, + error: None, + }); let mut app = LiveTui::new(LayoutClient { - snapshot: DemoClient::default().view(), + snapshot, edits: Arc::clone(&edits), }) .unwrap(); @@ -3789,15 +3906,26 @@ mod tests { app.snapshot.configuration.mixer_orientation, MixerOrientation::Horizontal ); + app.handle_key_event(press(KeyCode::Down)); + app.handle_key_event(press(KeyCode::Right)); + assert_eq!( + app.snapshot.configuration.default_controller.as_deref(), + Some("desk") + ); app.handle_key_event(press(KeyCode::Char('g'))); app.handle_key_event(press(KeyCode::Char('a'))); assert!(app.editor.is_none()); assert_eq!( edits.lock().unwrap().as_slice(), - &[ConfigurationEdit::SetMixerOrientation { - orientation: MixerOrientation::Horizontal - }] + &[ + ConfigurationEdit::SetMixerOrientation { + orientation: MixerOrientation::Horizontal + }, + ConfigurationEdit::SetDefaultController { + surface: Some("desk".into()) + } + ] ); }