Krolyxon
Commit 52014a4

Branches

Refs →

budctlcratesbudctl-clisrcmain.rs

171 lines · 5.2 KB · Rust

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