Files
moonmix/wp/wp.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

267 lines
6.1 KiB
MoonBit

// MoonBit side of the WirePlumber binding (see wp_stub.c).
//
// `start` connects to PipeWire through libwireplumber and loads the
// mixer-api and default-nodes-api plugins. Every change arrives as an
// `Event` through the handler; `read_node`, `read_links` and
// `read_defaults` turn the current WirePlumber state into @model events.
///|
pub(all) enum Event {
NodeAdded(Int32)
NodeChanged(Int32)
NodeRemoved(Int32)
LinksChanged
DefaultsChanged
/// Plugins loaded and initial object set known: do a full sync.
Ready
Disconnected
/// A plugin failed to load (wireplumber modules missing?).
PluginError
/// One-shot timer from `timeout`, carrying its tag.
Timeout(Int32)
}
///|
fn decode_event(kind : Int32, id : Int32) -> Event? {
match kind {
1 => Some(NodeAdded(id))
2 => Some(NodeChanged(id))
3 => Some(NodeRemoved(id))
4 => Some(LinksChanged)
5 => Some(DefaultsChanged)
6 => Some(Ready)
7 => Some(Disconnected)
8 => Some(PluginError)
9 => Some(Timeout(id))
_ => None
}
}
///|
extern "C" fn ffi_set_handler(
call : FuncRef[((Int32, Int32) -> Unit, Int32, Int32) -> Unit],
closure : (Int32, Int32) -> Unit,
) = "mm_wp_set_handler"
///|
extern "C" fn ffi_start() -> Int32 = "mm_wp_start"
///|
extern "C" fn ffi_timeout(ms : Int32, tag : Int32) = "mm_wp_timeout"
///|
extern "C" fn ffi_run() = "mm_wp_run"
///|
extern "C" fn ffi_quit() = "mm_wp_quit"
///|
extern "C" fn ffi_node_count() -> Int32 = "mm_wp_node_count"
///|
extern "C" fn ffi_node_id_at(i : Int32) -> Int32 = "mm_wp_node_id_at"
///|
extern "C" fn ffi_node_exists(id : Int32) -> Int32 = "mm_wp_node_exists"
///|
#borrow(key)
extern "C" fn ffi_node_prop(id : Int32, key : Bytes) -> Bytes = "mm_wp_node_prop"
///|
extern "C" fn ffi_query_volume(id : Int32) -> Int32 = "mm_wp_query_volume"
///|
extern "C" fn ffi_volume_at(ch : Int32) -> Double = "mm_wp_volume_at"
///|
extern "C" fn ffi_mute() -> Int32 = "mm_wp_mute"
///|
extern "C" fn ffi_link_count() -> Int32 = "mm_wp_link_count"
///|
extern "C" fn ffi_link_end(i : Int32, input : Int32) -> Int32 = "mm_wp_link_end"
///|
extern "C" fn ffi_default_node(kind : Int32) -> Int32 = "mm_wp_default_node"
///|
#borrow(vols)
extern "C" fn ffi_set_volumes(
id : Int32,
vols : FixedArray[Double],
n : Int32,
) -> Int32 = "mm_wp_set_volumes"
///|
extern "C" fn ffi_set_mute(id : Int32, mute : Int32) -> Int32 = "mm_wp_set_mute"
///|
extern "C" fn ffi_set_default(kind : Int32, id : Int32) -> Int32 = "mm_wp_set_default"
///|
extern "C" fn ffi_move_stream(stream : Int32, device : Int32) -> Int32 = "mm_wp_move_stream"
///|
/// NUL-terminated UTF-8 for C.
fn cstr(s : String) -> Bytes {
let b = @utf8.encode(s)
let n = b.length()
Bytes::makei(n + 1, fn(i) { if i < n { b[i] } else { b'\x00' } })
}
///|
/// Register the single event handler. Call before `start`.
pub fn on_event(f : (Event) -> Unit) -> Unit {
ffi_set_handler(fn(g, kind, id) { g(kind, id) }, fn(kind, id) {
match decode_event(kind, id) {
Some(ev) => f(ev)
None => ()
}
})
}
///|
/// Connect to PipeWire. False if the daemon is not reachable.
pub fn start() -> Bool {
ffi_start() == 0
}
///|
pub fn timeout(ms : Int32, tag : Int32) -> Unit {
ffi_timeout(ms, tag)
}
///|
/// Run a plain GLib main loop (headless tools; the GUI runs GTK's loop).
pub fn run() -> Unit {
ffi_run()
}
///|
pub fn quit() -> Unit {
ffi_quit()
}
///|
pub fn node_ids() -> Array[Int32] {
let n = ffi_node_count()
Array::makei(n, fn(i) { ffi_node_id_at(i) })
}
///|
pub fn node_prop(id : Int32, key : String) -> String {
@utf8.decode_lossy(ffi_node_prop(id, cstr(key)))
}
///|
/// Current state of a node as a model node, or None if it is gone.
pub fn read_node(id : Int32) -> @model.Node? {
if ffi_node_exists(id) == 0 {
return None
}
let nch = ffi_query_volume(id)
let volumes = Array::makei(nch, fn(ch) { ffi_volume_at(ch) })
let mute = ffi_mute() != 0
let description = {
let d = node_prop(id, "node.description")
if d != "" {
d
} else {
node_prop(id, "node.nick")
}
}
let icon = {
let a = node_prop(id, "application.icon-name")
if a != "" {
a
} else {
node_prop(id, "device.icon-name")
}
}
Some(
@model.Node::new(
id,
node_prop(id, "media.class"),
name=node_prop(id, "node.name"),
description~,
app_name=node_prop(id, "application.name"),
media_name=node_prop(id, "media.name"),
icon_name=icon,
volumes~,
mute~,
),
)
}
///|
pub fn read_links() -> Array[@model.Link] {
let n = ffi_link_count()
Array::makei(n, fn(i) {
{ output_node: ffi_link_end(i, 0), input_node: ffi_link_end(i, 1), }
})
}
///|
pub fn read_defaults() -> @model.Event {
Defaults(sink=ffi_default_node(0), source=ffi_default_node(1))
}
///|
/// Translate a WirePlumber event into model events (pulling fresh data).
pub fn to_model_events(ev : Event) -> Array[@model.Event] {
match ev {
NodeAdded(id) | NodeChanged(id) =>
match read_node(id) {
Some(n) => [Upsert(n)]
None => []
}
NodeRemoved(id) => [Remove(id)]
LinksChanged => [Links(read_links())]
DefaultsChanged => [read_defaults()]
Ready => {
let evs : Array[@model.Event] = [Reset]
for id in node_ids() {
match read_node(id) {
Some(n) => evs.push(Upsert(n))
None => ()
}
}
evs.push(Links(read_links()))
evs.push(read_defaults())
evs
}
Disconnected => [Reset]
PluginError | Timeout(_) => []
}
}
///|
/// Set linear channel volumes.
pub fn set_volumes(id : Int32, volumes : Array[Double]) -> Bool {
let arr = FixedArray::makei(volumes.length(), fn(i) { volumes[i] })
ffi_set_volumes(id, arr, arr.length()) != 0
}
///|
pub fn set_mute(id : Int32, mute : Bool) -> Bool {
ffi_set_mute(id, if mute { 1 } else { 0 }) != 0
}
///|
pub fn set_default_sink(id : Int32) -> Bool {
ffi_set_default(0, id) != 0
}
///|
pub fn set_default_source(id : Int32) -> Bool {
ffi_set_default(1, id) != 0
}
///|
/// Route a playback/record stream to another sink/source.
pub fn move_stream(stream_id : Int32, device_id : Int32) -> Bool {
ffi_move_stream(stream_id, device_id) != 0
}