Files
moonmix/cmd/moonmix-probe/main.mbt
T
nandi 92ae5cdbfe
CI / check (push) Successful in 9s
CI / native (push) Successful in 1m1s
Add moonmix: pavucontrol-style PipeWire mixer in MoonBit (WirePlumber + GTK4) (#1)
2026-11-22 23:52:37 +00:00

147 lines
3.7 KiB
MoonBit

// moonmix-probe: headless companion to the GUI. Connects through the same
// WirePlumber binding and model, prints what each tab would show, and can
// apply one action. Used for smoke tests and debugging without a display.
//
// moonmix-probe print all tabs
// <id> may be a numeric node id or a node.name.
//
// moonmix-probe volume <id> <pct> set linked volume (cubic %)
// moonmix-probe mute <id> <0|1>
// moonmix-probe default <id> make sink/source the default
// moonmix-probe move <stream> <dev> route a stream to a device
///|
extern "C" fn c_exit(code : Int32) = "exit"
///|
fn print_state(state : @model.State) -> Unit {
for tab in @model.Tab::all() {
println("== \{tab.label()}")
let rows = @model.rows(state, tab)
if rows.length() == 0 {
println(" (none)")
}
for r in rows {
let flags = []
if r.is_default {
flags.push("default")
}
if r.mute {
flags.push("muted")
}
if r.current_choice >= 0 {
flags.push("-> " + r.choices[r.current_choice].label)
}
let vol = if r.has_volume { r.volume_text } else { "(no volume)" }
let f = if flags.length() > 0 {
" [" + flags.join(", ") + "]"
} else {
""
}
println(" \{r.id}: \{r.title} | \{vol} | ch=\{r.volumes.length()}\{f}")
}
}
}
///|
fn parse(s : String) -> Double {
@string.parse_double(s) catch {
_ => {
println("moonmix-probe: not a number: \{s}")
c_exit(2)
0.0
}
}
}
///|
/// Node id from a number or a node.name.
fn resolve(state : @model.State, s : String) -> Int32 {
for id, n in state.nodes {
if n.name == s {
return id
}
}
parse(s).to_int()
}
///|
fn act(state : @model.State, args : Array[String]) -> Bool {
match args {
["volume", id, pct] => {
let id = resolve(state, id)
let vols = match state.nodes.get(id) {
Some(n) => n.volumes
None => []
}
@wp.set_volumes(id, @model.scale_linked(vols, parse(pct)))
}
["mute", id, on] => @wp.set_mute(resolve(state, id), on == "1")
["default", id] => {
let id = resolve(state, id)
match state.nodes.get(id).map(fn(n) { n.kind() }) {
Some(Sink) => @wp.set_default_sink(id)
Some(Source) => @wp.set_default_source(id)
_ => false
}
}
["move", stream, dev] =>
@wp.move_stream(resolve(state, stream), resolve(state, dev))
_ => {
println("moonmix-probe: unknown command")
false
}
}
}
///|
fn main {
let argv = @env.args()
let args = if argv.length() > 1 { argv[1:].to_owned() } else { [] }
let state = @model.State::new()
@wp.on_event(fn(ev) {
for e in @wp.to_model_events(ev) {
let _ = state.apply(e)
}
match ev {
// default-nodes-api reports defaults shortly after Ready: settle first.
Ready => @wp.timeout(300, 2)
Timeout(2) => {
print_state(state)
if args.length() > 0 {
let ok = act(state, args)
println(
"== action \{args.join(" ")}: \{if ok { "ok" } else { "FAILED" }}",
)
@wp.timeout(800, 1)
} else {
@wp.quit()
}
}
Timeout(1) => {
print_state(state)
@wp.quit()
}
PluginError => {
println("moonmix-probe: WirePlumber API modules failed to load")
c_exit(3)
}
Disconnected => {
println("moonmix-probe: disconnected")
c_exit(4)
}
Timeout(99) => {
println("moonmix-probe: timed out waiting for WirePlumber")
c_exit(5)
}
_ => ()
}
})
if !@wp.start() {
println("moonmix-probe: cannot connect to PipeWire")
c_exit(1)
}
@wp.timeout(10000, 99)
@wp.run()
}