From 472625301e9a8a7b2b7e7f1786492d287ba4bf6b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 00:17:36 +0200 Subject: [PATCH] feat(M8.1): connect the mixer TUI to IPC Translate revisioned server snapshots into the event-driven TUI and send selected stream volume and mute actions through correlated IPC requests. Tests: cargo test -p fader_master --all-targets --all-features --locked Tests: cargo clippy -p fader_master --all-targets --all-features --locked -- -D warnings Manual: live PTY rendered the ready Pulse snapshot and restored the terminal on quit. --- fader_master/src/lib.rs | 1 + fader_master/src/main.rs | 6 +- fader_master/src/tui_client.rs | 101 +++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 fader_master/src/tui_client.rs diff --git a/fader_master/src/lib.rs b/fader_master/src/lib.rs index 9965fba..17502b7 100644 --- a/fader_master/src/lib.rs +++ b/fader_master/src/lib.rs @@ -6,6 +6,7 @@ pub mod cli; pub mod ipc; pub mod live; pub mod local; +pub mod tui_client; use std::{collections::BTreeMap, error::Error, fmt}; diff --git a/fader_master/src/main.rs b/fader_master/src/main.rs index 052351e..82b737b 100644 --- a/fader_master/src/main.rs +++ b/fader_master/src/main.rs @@ -9,13 +9,17 @@ use fader_master::{ cli::{Cli, Command}, ipc::{ClientRequest, PROTOCOL_VERSION, RequestCommand, ResponseResult}, local::{LocalServer, client_request, default_socket_path}, + tui_client::IpcMixerClient, }; use midi::{LinuxIdentityCatalog, MidiInputCatalog}; use volume_lib::{AudioBackend, CompiledAdapterProbe, TargetRequirements, select_adapter}; fn main() -> Result<()> { match Cli::parse().command { - Command::Tui { socket: None } => tui::run().context("terminal UI failed"), + Command::Tui { socket } => { + let socket = socket.map_or_else(default_socket_path, Ok)?; + tui::run_live(IpcMixerClient::new(socket)).context("terminal UI failed") + } Command::Validate { config } => { let profile = load_profile(&config)?; println!("profile {:?} is valid", profile.id().as_str()); diff --git a/fader_master/src/tui_client.rs b/fader_master/src/tui_client.rs new file mode 100644 index 0000000..34d4697 --- /dev/null +++ b/fader_master/src/tui_client.rs @@ -0,0 +1,101 @@ +use std::{io, path::PathBuf}; + +use tui::{MixerClient, MixerItem, MixerSnapshot, StreamId}; + +use crate::{ + ipc::{ClientRequest, PROTOCOL_VERSION, RequestCommand, ResponseResult, StreamKey}, + local::client_request, +}; + +/// Short-lived request client used by the refresh-driven terminal UI. +pub struct IpcMixerClient { + socket: PathBuf, + next_request: u64, +} + +impl IpcMixerClient { + #[must_use] + pub fn new(socket: impl Into) -> Self { + Self { + socket: socket.into(), + next_request: 1, + } + } + + fn send(&mut self, command: RequestCommand) -> io::Result { + let request_id = self.next_request; + self.next_request = self + .next_request + .checked_add(1) + .ok_or_else(|| io::Error::other("TUI request counter exhausted"))?; + let response = client_request( + &self.socket, + &ClientRequest { + version: PROTOCOL_VERSION, + request_id, + command, + }, + ) + .map_err(io::Error::other)?; + match response.result { + ResponseResult::Snapshot { snapshot } => Ok(MixerSnapshot { + revision: snapshot.revision, + backend: snapshot.backend, + status: snapshot.status, + items: snapshot + .streams + .into_iter() + .map(|stream| MixerItem { + id: StreamId { + epoch: stream.stream.epoch, + backend_id: stream.stream.backend_id, + incarnation: stream.stream.incarnation, + }, + label: stream.label, + process: stream.process, + gain: stream.gain, + muted: stream.muted, + pending: stream.pending, + writable: stream.writable, + error: stream.error, + }) + .collect(), + }), + ResponseResult::Error { message, .. } => Err(io::Error::other(message)), + } + } +} + +impl MixerClient for IpcMixerClient { + fn snapshot(&mut self) -> io::Result { + self.send(RequestCommand::Snapshot) + } + + fn set_volume( + &mut self, + id: StreamId, + gain: f64, + expected_revision: u64, + ) -> io::Result { + self.send(RequestCommand::SetVolume { + stream: key(id), + gain, + expected_revision, + }) + } + + fn toggle_mute(&mut self, id: StreamId, expected_revision: u64) -> io::Result { + self.send(RequestCommand::ToggleMute { + stream: key(id), + expected_revision, + }) + } +} + +const fn key(id: StreamId) -> StreamKey { + StreamKey { + epoch: id.epoch, + backend_id: id.backend_id, + incarnation: id.incarnation, + } +}