| 1 | //! `budctl`: control wireless earbuds from Linux. |
| 2 | //! |
| 3 | //! A thin layer over budctl-core: parse the command line, run one command, print the result, |
| 4 | //! turn errors into the exit codes listed in CLAUDE.md. |
| 5 | |
| 6 | mod output; |
| 7 | |
| 8 | use std::io::{self, Write}; |
| 9 | use std::process::ExitCode; |
| 10 | |
| 11 | use budctl_core::bt::BdAddr; |
| 12 | use budctl_core::status::{AncLevel, NoiseMode}; |
| 13 | use budctl_core::{Error, Kind}; |
| 14 | use clap::{Parser, Subcommand, ValueEnum}; |
| 15 | |
| 16 | /// Control wireless earbuds from Linux, without the vendor app. |
| 17 | #[derive(Parser)] |
| 18 | #[command(name = "budctl", version)] |
| 19 | struct Cli { |
| 20 | /// The buds to use, when more than one supported pair is paired. |
| 21 | #[arg(long, global = true, value_name = "MAC")] |
| 22 | device: Option<BdAddr>, |
| 23 | |
| 24 | /// Machine-readable output (JSON with "schema": 1). |
| 25 | #[arg(long, global = true)] |
| 26 | json: bool, |
| 27 | |
| 28 | /// Hex-dump every frame sent and received on stderr. |
| 29 | #[arg(long, global = true)] |
| 30 | debug: bool, |
| 31 | |
| 32 | #[command(subcommand)] |
| 33 | command: Command, |
| 34 | } |
| 35 | |
| 36 | #[derive(Subcommand)] |
| 37 | enum Command { |
| 38 | /// Paired devices and whether budctl recognizes them. |
| 39 | List, |
| 40 | /// Battery, noise control, EQ and switches. |
| 41 | Status, |
| 42 | /// Touch controls (gesture table) of each bud. |
| 43 | Touch, |
| 44 | /// Set noise control, then read it back. |
| 45 | Anc { |
| 46 | #[arg(value_enum)] |
| 47 | mode: AncArg, |
| 48 | }, |
| 49 | /// Select an EQ preset (pure-raw, deep-bass, pure-vocals, pure-bass), then read it back. |
| 50 | Eq { preset: String }, |
| 51 | /// Turn game mode (low latency) on or off, then read it back. |
| 52 | Game { |
| 53 | #[arg(value_enum)] |
| 54 | state: OnOff, |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | #[derive(Clone, Copy, ValueEnum)] |
| 59 | enum OnOff { |
| 60 | On, |
| 61 | Off, |
| 62 | } |
| 63 | |
| 64 | #[derive(Clone, Copy, ValueEnum)] |
| 65 | enum AncArg { |
| 66 | Off, |
| 67 | Transparency, |
| 68 | /// Noise cancelling at the deep level (the buds use the same value for both). |
| 69 | On, |
| 70 | Light, |
| 71 | Medium, |
| 72 | Deep, |
| 73 | } |
| 74 | |
| 75 | impl From<AncArg> for NoiseMode { |
| 76 | fn from(arg: AncArg) -> Self { |
| 77 | match arg { |
| 78 | AncArg::Off => NoiseMode::Off, |
| 79 | AncArg::On | AncArg::Deep => NoiseMode::Anc(AncLevel::Deep), |
| 80 | AncArg::Light => NoiseMode::Anc(AncLevel::Light), |
| 81 | AncArg::Medium => NoiseMode::Anc(AncLevel::Medium), |
| 82 | AncArg::Transparency => NoiseMode::Transparency, |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn main() -> ExitCode { |
| 88 | let cli = match Cli::try_parse() { |
| 89 | Ok(cli) => cli, |
| 90 | Err(error) => { |
| 91 | // Help and version are not errors; everything else is a usage error (exit 1). |
| 92 | let code = if error.use_stderr() { 1 } else { 0 }; |
| 93 | // Nothing useful can be done if printing the usage message fails. |
| 94 | let _ = error.print(); |
| 95 | return ExitCode::from(code); |
| 96 | } |
| 97 | }; |
| 98 | if cli.debug { |
| 99 | budctl_core::debug::enable(); |
| 100 | } |
| 101 | |
| 102 | let mut stdout = io::stdout().lock(); |
| 103 | let result = run(&cli, &mut stdout).and_then(|()| stdout.flush().map_err(output::Failure::Io)); |
| 104 | match result { |
| 105 | Ok(()) => ExitCode::SUCCESS, |
| 106 | // `budctl status | head -1` is fine. |
| 107 | Err(output::Failure::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe => ExitCode::SUCCESS, |
| 108 | Err(output::Failure::Io(e)) => { |
| 109 | eprintln!("budctl: cannot write output: {e}"); |
| 110 | ExitCode::from(1) |
| 111 | } |
| 112 | Err(output::Failure::Budctl(e)) => { |
| 113 | eprintln!("budctl: {e}"); |
| 114 | ExitCode::from(exit_code(&e)) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn run(cli: &Cli, out: &mut impl Write) -> Result<(), output::Failure> { |
| 120 | let found = budctl_core::scan()?; |
| 121 | if let Command::List = cli.command { |
| 122 | return output::list(out, &found, cli.json); |
| 123 | } |
| 124 | |
| 125 | let (device, model) = budctl_core::select(found, cli.device)?; |
| 126 | let mut session = budctl_core::connect(&device, model)?; |
| 127 | match cli.command { |
| 128 | Command::List => Ok(()), |
| 129 | Command::Status => { |
| 130 | let status = session.status()?; |
| 131 | output::status(out, &device, model, &status, cli.json) |
| 132 | } |
| 133 | Command::Touch => { |
| 134 | let touch = session.touch_controls()?; |
| 135 | output::touch(out, &device, model, &touch, cli.json) |
| 136 | } |
| 137 | Command::Anc { mode } => { |
| 138 | let now = session.set_noise_control(mode.into())?; |
| 139 | output::noise_control(out, &device, model, &now, cli.json) |
| 140 | } |
| 141 | Command::Eq { ref preset } => { |
| 142 | let now = session.set_eq(preset)?; |
| 143 | output::eq(out, &device, model, &now, cli.json) |
| 144 | } |
| 145 | Command::Game { state } => { |
| 146 | let now = session.set_switch("game", matches!(state, OnOff::On))?; |
| 147 | output::switch(out, &device, model, &now, cli.json) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Exit codes from CLAUDE.md: 1 usage, 2 not found, 3 busy, 4 rejected or timed out. |
| 153 | fn exit_code(error: &Error) -> u8 { |
| 154 | match error.kind() { |
| 155 | Kind::Usage => 1, |
| 156 | Kind::NotFound => 2, |
| 157 | Kind::Busy => 3, |
| 158 | Kind::Failed => 4, |
| 159 | } |
| 160 | } |