Files
moonmix/tabs/tabs.mbt
T
nandiandClaude Opus 5.5 e16cc1b2ad Name Chrome streams after their browser tabs
A browser extension reports audible tabs to moonmix-tabs-host, which matches
them to PipeWire streams by time and publishes the names for the mixer; streams
of one tab share a row. Ambiguous streams keep the browser's name. Without the
extension, a browser's only running stream shows its MPRIS title.

The smoke test no longer claims real audio hardware.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 19:26:29 -08:00

135 lines
2.6 KiB
MoonBit

// MoonBit side of tabs_stub.c.
///|
pub(all) enum Event {
/// A native-messaging message arrived: `message`.
Message
/// The browser closed the connection, or asked us to stop.
Eof
/// The published files changed: `names`.
Files
/// What browsers play changed: `media_titles`.
Media
}
///|
extern "C" fn ffi_set_handler(
call : FuncRef[((Int32) -> Unit, Int32) -> Unit],
closure : (Int32) -> Unit,
) = "mm_tabs_set_handler"
///|
extern "C" fn ffi_now_ms() -> Int64 = "mm_tabs_now_ms"
///|
extern "C" fn ffi_getppid() -> Int32 = "mm_tabs_getppid"
///|
extern "C" fn ffi_listen() = "mm_tabs_listen"
///|
extern "C" fn ffi_message() -> Bytes = "mm_tabs_message"
///|
#borrow(text)
extern "C" fn ffi_publish(text : Bytes) -> Int32 = "mm_tabs_publish"
///|
extern "C" fn ffi_unpublish() = "mm_tabs_unpublish"
///|
extern "C" fn ffi_prune() = "mm_tabs_prune"
///|
extern "C" fn ffi_read() -> Bytes = "mm_tabs_read"
///|
extern "C" fn ffi_watch() = "mm_tabs_watch"
///|
extern "C" fn ffi_watch_media() = "mm_tabs_watch_media"
///|
extern "C" fn ffi_media() -> Bytes = "mm_tabs_media"
///|
/// Register the single event handler. Call before `listen` or `watch`.
pub fn on_event(f : (Event) -> Unit) -> Unit {
ffi_set_handler(fn(g, kind) { g(kind) }, fn(kind) {
match kind {
1 => f(Message)
2 => f(Eof)
3 => f(Files)
4 => f(Media)
_ => ()
}
})
}
///|
pub fn now_ms() -> Int64 {
ffi_now_ms()
}
///|
/// The browser: it starts native-messaging hosts itself.
pub fn browser_pid() -> Int32 {
ffi_getppid()
}
///|
/// Read native messages from stdin.
pub fn listen() -> Unit {
ffi_listen()
}
///|
pub fn message() -> String {
@utf8.decode_lossy(ffi_message())
}
///|
/// Publish this host's matches for running mixers.
pub fn publish(file : @model.TabFile) -> Bool {
let b = @utf8.encode(file.to_text())
let n = b.length()
ffi_publish(Bytes::makei(n + 1, fn(i) { if i < n { b[i] } else { b'\x00' } })) ==
0
}
///|
pub fn unpublish() -> Unit {
ffi_unpublish()
}
///|
/// Remove files left by hosts that died.
pub fn prune() -> Unit {
ffi_prune()
}
///|
/// Everything published by hosts right now.
pub fn files() -> Array[@model.TabFile] {
let out = []
for line in @utf8.decode_lossy(ffi_read()).split("\n") {
match @model.TabFile::parse(line.to_owned()) {
Some(f) => out.push(f)
None => ()
}
}
out
}
///|
/// Report changes of the published files and of browsers' MPRIS titles.
pub fn watch() -> Unit {
ffi_watch()
ffi_watch_media()
}
///|
pub fn media_titles() -> Map[Int32, String] {
@model.parse_media_titles(@utf8.decode_lossy(ffi_media()))
}