| 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 | /// Turn game mode (low latency) on or off, then read it back. |
| 50 | Game { |
| 51 | #[arg(value_enum)] |
| 52 | state: OnOff, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | #[derive(Clone, Copy, ValueEnum)] |
| 57 | enum OnOff { |
| 58 | On, |
| 59 | Off, |
| 60 | } |
| 61 | |
| 62 | #[derive(Clone, Copy, ValueEnum)] |
| 63 | enum AncArg { |
| 64 | Off, |
| 65 | Transparency, |
| 66 | /// Noise cancelling at the deep level (the buds use the same value for both). |
| 67 | On, |
| 68 | Light, |
| 69 | Medium, |
| 70 | Deep, |
| 71 | } |
| 72 | |
| 73 | impl From<AncArg> for NoiseMode { |
| 74 | fn from(arg: AncArg) -> Self { |
| 75 | match arg { |
| 76 | AncArg::Off => NoiseMode::Off, |
| 77 | AncArg::On | AncArg::Deep => NoiseMode::Anc(AncLevel::Deep), |
| 78 | AncArg::Light => NoiseMode::Anc(AncLevel::Light), |
| 79 | AncArg::Medium => NoiseMode::Anc(AncLevel::Medium), |
| 80 | AncArg::Transparency => NoiseMode::Transparency, |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn main() -> ExitCode { |
| 86 | let cli = match Cli::try_parse() { |
| 87 | Ok(cli) => cli, |
| 88 | Err(error) => { |
| 89 | // Help and version are not errors; everything else is a usage error (exit 1). |
| 90 | let code = if error.use_stderr() { 1 } else { 0 }; |
| 91 | // Nothing useful can be done if printing the usage message fails. |
| 92 | let _ = error.print(); |
| 93 | return ExitCode::from(code); |
| 94 | } |
| 95 | }; |
| 96 | if cli.debug { |
| 97 | budctl_core::debug::enable(); |
| 98 | } |
| 99 | |
| 100 | let mut stdout = io::stdout().lock(); |
| 101 | let result = run(&cli, &mut stdout).and_then(|()| stdout.flush().map_err(output::Failure::Io)); |
| 102 | match result { |
| 103 | Ok(()) => ExitCode::SUCCESS, |
| 104 | // `budctl status | head -1` is fine. |
| 105 | Err(output::Failure::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe => ExitCode::SUCCESS, |
| 106 | Err(output::Failure::Io(e)) => { |
| 107 | eprintln!("budctl: cannot write output: {e}"); |
| 108 | ExitCode::from(1) |
| 109 | } |
| 110 | Err(output::Failure::Budctl(e)) => { |
| 111 | eprintln!("budctl: {e}"); |
| 112 | ExitCode::from(exit_code(&e)) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn run(cli: &Cli, out: &mut impl Write) -> Result<(), output::Failure> { |
| 118 | let found = budctl_core::scan()?; |
| 119 | if let Command::List = cli.command { |
| 120 | return output::list(out, &found, cli.json); |
| 121 | } |
| 122 | |
| 123 | let (device, model) = budctl_core::select(found, cli.device)?; |
| 124 | let mut session = budctl_core::connect(&device, model)?; |
| 125 | match cli.command { |
| 126 | Command::List => Ok(()), |
| 127 | Command::Status => { |
| 128 | let status = session.status()?; |
| 129 | output::status(out, &device, model, &status, cli.json) |
| 130 | } |
| 131 | Command::Touch => { |
| 132 | let touch = session.touch_controls()?; |
| 133 | output::touch(out, &device, model, &touch, cli.json) |
| 134 | } |
| 135 | Command::Anc { mode } => { |
| 136 | let now = session.set_noise_control(mode.into())?; |
| 137 | output::noise_control(out, &device, model, &now, cli.json) |
| 138 | } |
| 139 | Command::Game { state } => { |
| 140 | let now = session.set_switch("game", matches!(state, OnOff::On))?; |
| 141 | output::switch(out, &device, model, &now, cli.json) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /// Exit codes from CLAUDE.md: 1 usage, 2 not found, 3 busy, 4 rejected or timed out. |
| 147 | fn exit_code(error: &Error) -> u8 { |
| 148 | match error.kind() { |
| 149 | Kind::Usage => 1, |
| 150 | Kind::NotFound => 2, |
| 151 | Kind::Busy => 3, |
| 152 | Kind::Failed => 4, |
| 153 | } |
| 154 | } |