Files
nandi 02b08cb471
CI / check (push) Successful in 10s
CI / native (push) Successful in 1m10s
Better icons: device/app icon chains, volume icons, app icon (#2)
2026-11-23 01:07:41 +00:00

330 lines
7.7 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"
///|
extern "C" fn ffi_meter_start(id : Int32, kind : Int32) -> Int32 = "mm_wp_meter_start"
///|
extern "C" fn ffi_meter_stop(id : Int32) = "mm_wp_meter_stop"
///|
extern "C" fn ffi_meter_stop_all() = "mm_wp_meter_stop_all"
///|
extern "C" fn ffi_meter_take(id : Int32) -> Double = "mm_wp_meter_take"
///|
/// 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? {
// Peak-meter streams (ours, pavucontrol's via pipewire-pulse) are not
// something to show or control.
if ffi_node_exists(id) == 0 || node_prop(id, "stream.monitor") == "true" {
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_hints = []
for
key in [
"application.icon-name", "media.icon-name", "node.icon-name", "device.icon-name",
] {
let v = node_prop(id, key)
if v != "" && !icon_hints.contains(v) {
icon_hints.push(v)
}
}
let app_id = {
let a = node_prop(id, "pipewire.access.portal.app_id")
if a != "" {
a
} else {
node_prop(id, "application.id")
}
}
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_hints~,
form_factor=node_prop(id, "device.form-factor"),
bus=node_prop(id, "device.bus"),
app_binary=node_prop(id, "application.process.binary"),
app_id~,
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
}
///|
/// Start a live peak meter on a sink, source or playback stream (other
/// kinds have nothing to meter). True if one is running for `id`.
pub fn meter_start(id : Int32, kind : @model.NodeKind) -> Bool {
let k = match kind {
Sink => 0
Source => 1
PlaybackStream => 2
RecordStream | Other => return false
}
ffi_meter_start(id, k) != 0
}
///|
pub fn meter_stop(id : Int32) -> Unit {
ffi_meter_stop(id)
}
///|
pub fn meter_stop_all() -> Unit {
ffi_meter_stop_all()
}
///|
/// Linear peak (0..1, may exceed 1 when clipping) since the last call, or
/// None if `id` has no meter.
pub fn meter_take(id : Int32) -> Double? {
let p = ffi_meter_take(id)
if p < 0.0 {
None
} else {
Some(p)
}
}