diff --git a/README.md b/README.md index dcc730f..b529a88 100644 --- a/README.md +++ b/README.md @@ -117,12 +117,41 @@ To install it for your user: ```sh install -Dm755 _build/native/release/build/cmd/moonmix/moonmix.exe ~/.local/bin/moonmix install -Dm644 data/org.latha.moonmix.desktop ~/.local/share/applications/org.latha.moonmix.desktop +install -Dm644 data/icons/hicolor/scalable/apps/org.latha.moonmix.svg ~/.local/share/icons/hicolor/scalable/apps/org.latha.moonmix.svg +install -Dm644 data/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg ~/.local/share/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg +gtk-update-icon-cache -f -t ~/.local/share/icons/hicolor # optional ``` +The app icon is `org.latha.moonmix` (a speaker with a volume slider), shipped +as scalable and symbolic SVGs under `data/icons/hicolor/`. The window uses it +as its icon and the program name is `org.latha.moonmix`, which matches the +`.desktop` file so Wayland shells show it too. Run from a checkout, moonmix +also searches `./data/icons` and `$MOONMIX_ICON_DIR`. + +### Icons + +Everything else uses the desktop's icon theme (Adwaita, Breeze, Papirus... +any theme with the standard freedesktop names; install `adwaita-icon-theme` +if icons are missing). The names are chosen in `model/icons.mbt` and tested +with `moon test`; the GTK layer only asks the theme `has_icon`: + +- **Devices**: the device's own `device.icon-name` / `node.icon-name`, then + `device.form-factor` (headphone, headset, speaker, microphone, webcam, + handset, tv, hdmi...), HDMI/webcam/Bluetooth/USB hints, then the direction + (speakers or microphone), then `audio-card-symbolic` and + `audio-x-generic-symbolic`. Symbolic variants are tried first so they follow + the theme's foreground colour. `image-missing` is the very last resort. +- **Streams**: `application.icon-name` / `media.icon-name`, then the app's + launcher icon found in the installed `.desktop` files (by + `pipewire.access.portal.app_id`, `application.process.binary` or + `application.name`), then `application-x-executable-symbolic`. +- **Mute button**: `audio-volume-high/medium/low/muted-symbolic` following the + volume and mute state. Row icons are 32 px, button and tab icons 16 px. + Pkg-config flags: moon does not expand `$(pkg-config ...)` in `moon.pkg` link options. Instead, the packages point `link.native.stub-cc` and `cc` at `tools/pkg-cc`, a wrapper that adds -`pkg-config --cflags --libs libpipewire-0.3 wireplumber-0.5 gtk4`. moon +`pkg-config --cflags --libs libpipewire-0.3 wireplumber-0.5 gtk4 gio-unix-2.0`. moon derives the archiver name from the compiler name, so `tools/pkg-ar` is a passthrough to `ar`. Set `MOONMIX_CC` or `MOONMIX_AR` to use another compiler or archiver. diff --git a/cmd/moonmix/main.mbt b/cmd/moonmix/main.mbt index 2d39b66..3a3ff69 100644 --- a/cmd/moonmix/main.mbt +++ b/cmd/moonmix/main.mbt @@ -54,6 +54,10 @@ struct App { meters_paused : Ref[Bool] } +///| +/// Device / application icon in each row; buttons use 16 px icons. +const ROW_ICON_SIZE : Int32 = 32 + ///| const METER_INTERVAL_MS : Int32 = 40 @@ -66,15 +70,6 @@ const CSS : String = #|levelbar.mm-meter trough, levelbar.mm-meter block { min-height: 4px; } #|levelbar.mm-meter { margin: 0 10px; } -///| -fn mute_icon(muted : Bool) -> String { - if muted { - "audio-volume-muted-symbolic" - } else { - "audio-volume-high-symbolic" - } -} - ///| fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView { let id = row.id @@ -82,7 +77,7 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView { let root = @gtk.vbox(4) root.add_css_class("mm-row") let header = @gtk.hbox(8) - let icon = @gtk.image(row.icon_name, 32) + let icon = @gtk.image(@gtk.resolve_icon(row.icons), ROW_ICON_SIZE) let texts = @gtk.vbox(0) texts.set_hexpand(true) texts.set_valign_center() @@ -122,17 +117,21 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView { } let default_btn = match tab { OutputDevices | InputDevices => { - let (b, tag) = @gtk.toggle("object-select-symbolic", "Default", fn(on) { - if on { - let _ = if tab is OutputDevices { - @wp.set_default_sink(id) - } else { - @wp.set_default_source(id) + let (b, tag) = @gtk.toggle( + @gtk.resolve_icon(@model.default_button_icons()), + "Default", + fn(on) { + if on { + let _ = if tab is OutputDevices { + @wp.set_default_sink(id) + } else { + @wp.set_default_source(id) + } } - } - // The button reflects the actual default; re-sync it. - app.sync_default_button(tab, id) - }) + // The button reflects the actual default; re-sync it. + app.sync_default_button(tab, id) + }, + ) tags.push(tag) b.set_tooltip("Set as default (fallback) device") header.append(b) @@ -140,9 +139,11 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView { } _ => None } - let (mute, mute_tag) = @gtk.toggle(mute_icon(row.mute), "", fn(on) { - let _ = @wp.set_mute(id, on) - }) + let (mute, mute_tag) = @gtk.toggle( + @gtk.resolve_icon([row.volume_icon, "audio-volume-high"]), + "", + fn(on) { let _ = @wp.set_mute(id, on) }, + ) tags.push(mute_tag) mute.set_tooltip("Mute") header.append(mute) @@ -208,8 +209,8 @@ fn apply_row(view : RowView, row : @model.Row, force~ : Bool = false) -> Unit { view.subtitle.set_text(row.subtitle) view.subtitle.set_visible(row.subtitle != "") } - if force || old.icon_name != row.icon_name { - view.icon.set_icon(row.icon_name) + if force || old.icons != row.icons { + view.icon.set_icon(@gtk.resolve_icon(row.icons)) } view.scale.set_sensitive(row.has_volume) if force || @model.should_apply_remote(view.last_local_us, @gtk.now_us()) { @@ -217,7 +218,9 @@ fn apply_row(view : RowView, row : @model.Row, force~ : Bool = false) -> Unit { view.volume_label.set_text(row.volume_text) } view.mute.set_active(row.mute) - view.mute.set_button_icon(mute_icon(row.mute)) + view.mute.set_button_icon( + @gtk.resolve_icon([row.volume_icon, "audio-volume-high"]), + ) match view.default_btn { Some(b) => b.set_active(row.is_default) None => () @@ -436,7 +439,15 @@ fn build_ui() -> App { empty.set_margins(24) empty.add_css_class("dim-label") list.append(empty) - nb.append_page(@gtk.scrolled(list), tab.label()) + nb.append_page( + @gtk.scrolled(list), + tab.label(), + icon=@gtk.resolve_icon([ + tab.icon(), + "audio-card-symbolic", + "audio-x-generic", + ]), + ) tabs.push({ tab, list, empty, views: Map([]), rows: [], }) } outer.append(nb) diff --git a/data/icons/hicolor/scalable/apps/org.latha.moonmix.svg b/data/icons/hicolor/scalable/apps/org.latha.moonmix.svg new file mode 100644 index 0000000..c180983 --- /dev/null +++ b/data/icons/hicolor/scalable/apps/org.latha.moonmix.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/data/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg b/data/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg new file mode 100644 index 0000000..c1faff4 --- /dev/null +++ b/data/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/data/org.latha.moonmix.desktop b/data/org.latha.moonmix.desktop index e3dee2b..8e3b3e4 100644 --- a/data/org.latha.moonmix.desktop +++ b/data/org.latha.moonmix.desktop @@ -5,8 +5,9 @@ Name=moonmix GenericName=Volume Control Comment=Adjust volumes of PipeWire devices and applications Exec=moonmix -Icon=multimedia-volume-control +Icon=org.latha.moonmix Terminal=false Categories=AudioVideo;Audio;Mixer;GTK;Settings;HardwareSettings; Keywords=pipewire;wireplumber;volume;mixer;sound;audio;pavucontrol; StartupNotify=true +StartupWMClass=org.latha.moonmix diff --git a/docs/screenshots/input-devices.png b/docs/screenshots/input-devices.png index 8bb3f34..12cf641 100644 Binary files a/docs/screenshots/input-devices.png and b/docs/screenshots/input-devices.png differ diff --git a/docs/screenshots/output-devices.png b/docs/screenshots/output-devices.png index 0ed3adc..03908af 100644 Binary files a/docs/screenshots/output-devices.png and b/docs/screenshots/output-devices.png differ diff --git a/docs/screenshots/playback.png b/docs/screenshots/playback.png index bf27e4b..6db8606 100644 Binary files a/docs/screenshots/playback.png and b/docs/screenshots/playback.png differ diff --git a/docs/screenshots/recording.png b/docs/screenshots/recording.png index 10e9c4d..3b764ce 100644 Binary files a/docs/screenshots/recording.png and b/docs/screenshots/recording.png differ diff --git a/gtk/gtk.mbt b/gtk/gtk.mbt index 14360d3..5af6a04 100644 --- a/gtk/gtk.mbt +++ b/gtk/gtk.mbt @@ -77,8 +77,13 @@ extern "C" fn ffi_scrolled_new(child : Widget) -> Widget = "mm_gtk_scrolled_new" extern "C" fn ffi_notebook_new(tag : Int32) -> Widget = "mm_gtk_notebook_new" ///| -#borrow(label) -extern "C" fn ffi_notebook_append(nb : Widget, child : Widget, label : Bytes) = "mm_gtk_notebook_append" +#borrow(label, icon) +extern "C" fn ffi_notebook_append( + nb : Widget, + child : Widget, + label : Bytes, + icon : Bytes, +) = "mm_gtk_notebook_append" ///| extern "C" fn ffi_notebook_set_page(nb : Widget, i : Int32) = "mm_gtk_notebook_set_page" @@ -105,6 +110,14 @@ extern "C" fn ffi_image_new(icon : Bytes, size : Int32) -> Widget = "mm_gtk_imag #borrow(icon) extern "C" fn ffi_image_set_icon(img : Widget, icon : Bytes) = "mm_gtk_image_set_icon" +///| +#borrow(name) +extern "C" fn ffi_has_icon(name : Bytes) -> Int32 = "mm_gtk_has_icon" + +///| +#borrow(key) +extern "C" fn ffi_app_icon(key : Bytes) -> Bytes = "mm_gtk_app_icon" + ///| extern "C" fn ffi_scale_new(max : Double, norm : Double, tag : Int32) -> Widget = "mm_gtk_scale_new" @@ -345,8 +358,9 @@ pub fn Widget::append_page( self : Widget, child : Widget, label : String, + icon~ : String = "", ) -> Unit { - ffi_notebook_append(self, child, cstr(label)) + ffi_notebook_append(self, child, cstr(label), cstr(icon)) } ///| @@ -493,3 +507,37 @@ pub fn Widget::set_valign_center(self : Widget) -> Unit { pub fn Widget::save_png(self : Widget, path : String) -> Bool { ffi_save_png(self, cstr(path)) != 0 } + +///| +/// Does the current icon theme have this icon? +pub fn has_icon(name : String) -> Bool { + ffi_has_icon(cstr(name)) != 0 +} + +///| +/// Icon of the installed application matching `key` (desktop id, binary or +/// name) per its .desktop file, "" if there is none or the theme lacks it. +pub fn app_icon(key : String) -> String { + @utf8.decode_lossy(ffi_app_icon(cstr(key))) +} + +///| +/// First entry of an icon chain (see @model.device_icon_chain) that the theme +/// has. "app:" entries are looked up in the .desktop files. The last +/// entry is returned if nothing matches, so the result is never empty. +pub fn resolve_icon(chain : Array[String]) -> String { + for name in chain { + if name.has_prefix("app:") { + let icon = app_icon(name[4:].to_owned()) + if icon != "" { + return icon + } + } else if has_icon(name) { + return name + } + } + match chain.last() { + Some(n) => n + None => "image-missing" + } +} diff --git a/gtk/gtk_stub.c b/gtk/gtk_stub.c index 289b3a4..2a8ff1c 100644 --- a/gtk/gtk_stub.c +++ b/gtk/gtk_stub.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "moonbit.h" typedef void (*mm_gcall_t)(void *closure, int32_t tag, double value); @@ -42,7 +43,84 @@ MOONBIT_FFI_EXPORT void mm_gtk_set_handler(mm_gcall_t call, void *closure) { g_closure = closure; } -MOONBIT_FFI_EXPORT int32_t mm_gtk_init(void) { return gtk_init_check(); } +MOONBIT_FFI_EXPORT int32_t mm_gtk_init(void) { + /* The Wayland app id comes from the program name; match the .desktop file + * so the compositor shows the app icon. */ + g_set_prgname("org.latha.moonmix"); + if (!gtk_init_check()) + return 0; + /* Let an uninstalled checkout find data/icons/hicolor/... too: + * $MOONMIX_ICON_DIR, then ./data/icons. */ + GtkIconTheme *theme = gtk_icon_theme_get_for_display(gdk_display_get_default()); + const char *dir = g_getenv("MOONMIX_ICON_DIR"); + if (dir && *dir) + gtk_icon_theme_add_search_path(theme, dir); + gtk_icon_theme_add_search_path(theme, "data/icons"); + gtk_window_set_default_icon_name("org.latha.moonmix"); + return 1; +} + +/* 1 if the icon theme has `name`. */ +MOONBIT_FFI_EXPORT int32_t mm_gtk_has_icon(moonbit_bytes_t name) { + GdkDisplay *d = gdk_display_get_default(); + if (!d) + return 0; + return gtk_icon_theme_has_icon(gtk_icon_theme_get_for_display(d), s(name)); +} + +static int theme_icon_of(GAppInfo *info, GtkIconTheme *theme, char **out) { + GIcon *icon = g_app_info_get_icon(info); + if (icon && G_IS_THEMED_ICON(icon)) { + const char *const *names = g_themed_icon_get_names(G_THEMED_ICON(icon)); + for (int i = 0; names && names[i]; i++) + if (gtk_icon_theme_has_icon(theme, names[i])) { + *out = g_strdup(names[i]); + return 1; + } + } + return 0; +} + +/* Look an application up in the installed .desktop files by desktop id + * ("org.mozilla.firefox"), executable basename ("firefox") or display name + * ("Firefox"), and return its icon name if the theme has it. "" if none. */ +MOONBIT_FFI_EXPORT moonbit_bytes_t mm_gtk_app_icon(moonbit_bytes_t key) { + const char *k = s(key); + char *found = NULL; + GdkDisplay *d = gdk_display_get_default(); + if (d && *k) { + GtkIconTheme *theme = gtk_icon_theme_get_for_display(d); + char *id = g_strconcat(k, ".desktop", NULL); + GDesktopAppInfo *di = g_desktop_app_info_new(id); + g_free(id); + if (di) { + theme_icon_of(G_APP_INFO(di), theme, &found); + g_object_unref(di); + } + if (!found) { + GList *all = g_app_info_get_all(); + for (GList *l = all; l && !found; l = l->next) { + GAppInfo *info = l->data; + if (!g_app_info_should_show(info)) + continue; + const char *exe = g_app_info_get_executable(info); + char *base = exe ? g_path_get_basename(exe) : NULL; + const char *nm = g_app_info_get_name(info); + if ((base && g_ascii_strcasecmp(base, k) == 0) || + (nm && g_ascii_strcasecmp(nm, k) == 0)) + theme_icon_of(info, theme, &found); + g_free(base); + } + g_list_free_full(all, g_object_unref); + } + } + size_t n = found ? strlen(found) : 0; + moonbit_bytes_t out = moonbit_make_bytes(n + 1, 0); + if (found) + memcpy(out, found, n); + g_free(found); + return out; +} MOONBIT_FFI_EXPORT void mm_gtk_run(void) { if (!g_loop) @@ -102,7 +180,7 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_window_new(moonbit_bytes_t title, GtkWidget *win = gtk_window_new(); gtk_window_set_title(GTK_WINDOW(win), s(title)); gtk_window_set_default_size(GTK_WINDOW(win), w, h); - gtk_window_set_icon_name(GTK_WINDOW(win), "multimedia-volume-control"); + gtk_window_set_icon_name(GTK_WINDOW(win), "org.latha.moonmix"); set_tag(win, tag); g_signal_connect(win, "close-request", G_CALLBACK(on_close), NULL); return win; @@ -168,8 +246,16 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_notebook_new(int32_t tag) { } MOONBIT_FFI_EXPORT void mm_gtk_notebook_append(GtkWidget *nb, GtkWidget *child, - moonbit_bytes_t label) { - gtk_notebook_append_page(GTK_NOTEBOOK(nb), child, gtk_label_new(s(label))); + moonbit_bytes_t label, + moonbit_bytes_t icon) { + GtkWidget *tab = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); + if (s(icon)[0]) { + GtkWidget *img = gtk_image_new_from_icon_name(s(icon)); + gtk_image_set_pixel_size(GTK_IMAGE(img), 16); + gtk_box_append(GTK_BOX(tab), img); + } + gtk_box_append(GTK_BOX(tab), gtk_label_new(s(label))); + gtk_notebook_append_page(GTK_NOTEBOOK(nb), child, tab); } MOONBIT_FFI_EXPORT void mm_gtk_notebook_set_page(GtkWidget *nb, int32_t i) { @@ -253,7 +339,10 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_toggle_new(moonbit_bytes_t icon, GtkWidget *b = gtk_toggle_button_new(); if (s(icon)[0] && s(label)[0]) { GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); - gtk_box_append(GTK_BOX(box), gtk_image_new_from_icon_name(s(icon))); + GtkWidget *img = gtk_image_new_from_icon_name(s(icon)); + gtk_image_set_pixel_size(GTK_IMAGE(img), 16); + g_object_set_data(G_OBJECT(b), "mm-img", img); + gtk_box_append(GTK_BOX(box), img); gtk_box_append(GTK_BOX(box), gtk_label_new(s(label))); gtk_button_set_child(GTK_BUTTON(b), box); } else if (s(icon)[0]) { @@ -275,7 +364,11 @@ MOONBIT_FFI_EXPORT void mm_gtk_toggle_set(GtkWidget *b, int32_t active) { MOONBIT_FFI_EXPORT void mm_gtk_button_set_icon(GtkWidget *b, moonbit_bytes_t icon) { - gtk_button_set_icon_name(GTK_BUTTON(b), s(icon)); + GtkWidget *img = g_object_get_data(G_OBJECT(b), "mm-img"); + if (img) + gtk_image_set_from_icon_name(GTK_IMAGE(img), s(icon)); + else + gtk_button_set_icon_name(GTK_BUTTON(b), s(icon)); } static void on_selected(GObject *dd, GParamSpec *p, gpointer d) { diff --git a/gtk/pkg.generated.mbti b/gtk/pkg.generated.mbti index e3c3159..808813d 100644 --- a/gtk/pkg.generated.mbti +++ b/gtk/pkg.generated.mbti @@ -2,8 +2,12 @@ package "geordi/moonmix/gtk" // Values +pub fn app_icon(String) -> String + pub fn dropdown((Int32) -> Unit) -> (Widget, Int32) +pub fn has_icon(String) -> Bool + pub fn hbox(Int32) -> Widget pub fn image(String, Int32) -> Widget @@ -22,6 +26,8 @@ pub fn now_us() -> Int64 pub fn quit() -> Unit +pub fn resolve_icon(Array[String]) -> String + pub fn run() -> Unit pub fn scale(Double, Double, (Double) -> Unit) -> (Widget, Int32) @@ -49,7 +55,7 @@ pub fn window(String, Int32, Int32, on_close? : () -> Unit) -> Widget pub type Widget pub fn Widget::add_css_class(Self, String) -> Unit pub fn Widget::append(Self, Self) -> Unit -pub fn Widget::append_page(Self, Self, String) -> Unit +pub fn Widget::append_page(Self, Self, String, icon? : String) -> Unit pub fn Widget::present(Self) -> Unit pub fn Widget::remove(Self, Self) -> Unit pub fn Widget::reorder_after(Self, Self, Self?) -> Unit diff --git a/model/icons.mbt b/model/icons.mbt new file mode 100644 index 0000000..35b56c9 --- /dev/null +++ b/model/icons.mbt @@ -0,0 +1,200 @@ +// Icon selection, kept pure: PipeWire properties -> an ordered chain of +// icon-theme names. The GTK layer walks a chain and takes the first name the +// theme has (`gtk_icon_theme_has_icon`), so it never shows the broken-image +// placeholder unless even the last resort is missing. +// +// Chain entries starting with `APP_LOOKUP_PREFIX` ("app:") ask the GUI +// to look the application up in the installed .desktop files and use that +// launcher's icon. + +///| +pub fn icon_fallback_device() -> Array[String] { + ["audio-card-symbolic", "audio-x-generic-symbolic"] +} + +///| +pub fn icon_fallback_stream() -> Array[String] { + ["application-x-executable-symbolic", "audio-x-generic-symbolic"] +} + +///| +/// Absolute last resort, only used when the theme lacks everything above. +pub const ICON_MISSING : String = "image-missing" + +///| +pub const APP_LOOKUP_PREFIX : String = "app:" + +///| +/// "Default" toggle: first icon the theme has. +pub fn default_button_icons() -> Array[String] { + ["emblem-ok-symbolic", "object-select-symbolic", "emblem-default-symbolic"] +} + +///| +pub fn symbolic(name : String) -> String { + if name.has_suffix("-symbolic") { + name + } else { + name + "-symbolic" + } +} + +///| +fn push_unique(out : Array[String], name : String) -> Unit { + if name != "" && !out.contains(name) { + out.push(name) + } +} + +///| +/// `name` as a themed icon, symbolic variant first (it follows the theme's +/// foreground colour). +fn push_symbolic_first(out : Array[String], name : String) -> Unit { + if name == "" { + return + } + push_unique(out, symbolic(name)) + push_unique(out, name) +} + +///| +fn has_any(haystack : String, needles : Array[String]) -> Bool { + let h = haystack.to_lower() + needles.any(fn(n) { h.contains(n) }) +} + +///| +/// Icon (without suffix) for a `device.form-factor` value, "" if unknown. +pub fn form_factor_icon(form_factor : String, kind : NodeKind) -> String { + match form_factor { + "headphone" => "audio-headphones" + "headset" => "audio-headset" + "speaker" | "hifi" => "audio-speakers" + "microphone" => "audio-input-microphone" + "webcam" => "camera-web" + "handset" | "hands-free" => "phone" + "portable" => "multimedia-player" + "car" => + if kind is Source { + "audio-input-microphone" + } else { + "audio-speakers" + } + "computer" => "computer" + "tv" | "hdmi" => "video-display" + _ => "" + } +} + +///| +/// The bus of a device, from `device.bus` or else guessed from the +/// node.name prefix ("alsa_input.usb-...", "bluez_output..."). +pub fn device_bus(n : Node) -> String { + if n.bus != "" { + return n.bus + } + let name = n.name + if name.has_prefix("bluez_") { + "bluetooth" + } else if has_any(name, [".usb-"]) { + "usb" + } else if has_any(name, [".pci-"]) { + "pci" + } else { + "" + } +} + +///| +/// Icon chain for a sink or source. +pub fn device_icon_chain(n : Node) -> Array[String] { + let kind = n.kind() + let out : Array[String] = [] + // 1. what the device itself says + for h in n.icon_hints { + push_symbolic_first(out, h) + } + // 2. form factor, then HDMI / webcam heuristics, then bus + push_symbolic_first(out, form_factor_icon(n.form_factor, kind)) + let text = n.name + " " + n.description + if has_any(text, ["hdmi", "displayport", "display port"]) { + push_symbolic_first(out, "video-display") + } + let bus = device_bus(n) + if kind is Source && + ( + has_any(n.description, ["webcam", "camera"]) || + (bus == "usb" && n.description.to_lower().contains("cam")) + ) { + push_symbolic_first(out, "camera-web") + } + match (bus, kind) { + ("bluetooth", Source) => push_symbolic_first(out, "audio-headset") + ("bluetooth", _) => push_symbolic_first(out, "audio-headphones") + _ => () + } + // 3. by direction + match kind { + Source => push_symbolic_first(out, "audio-input-microphone") + _ => push_symbolic_first(out, "audio-speakers") + } + // 4. generic cards, then the placeholder + for f in icon_fallback_device() { + push_unique(out, f) + } + push_unique(out, ICON_MISSING) + out +} + +///| +/// Icon chain for an application stream: the icons the app announces, its +/// launcher's icon from the installed .desktop files, names derived from the +/// binary / app name, then generic ones. +pub fn stream_icon_chain(n : Node) -> Array[String] { + let out : Array[String] = [] + for h in n.icon_hints { + push_unique(out, h) + } + for key in [n.app_id, n.app_binary, n.app_name] { + if key != "" { + push_unique(out, APP_LOOKUP_PREFIX + key) + } + } + for key in [n.app_id, n.app_binary, n.app_name.to_lower()] { + // Many apps ship an icon named after their binary or app id. + if key != "" && !key.contains(" ") && !key.contains("/") { + push_unique(out, key) + } + } + if n.kind() is RecordStream { + push_unique(out, "audio-input-microphone-symbolic") + } + for f in icon_fallback_stream() { + push_unique(out, f) + } + push_unique(out, ICON_MISSING) + out +} + +///| +/// Speaker icon for a volume (linked percent, 100 = 0 dB) and mute state. +pub fn volume_icon(percent : Double, mute : Bool) -> String { + if mute || percent <= 0.0 { + "audio-volume-muted-symbolic" + } else if percent < 34.0 { + "audio-volume-low-symbolic" + } else if percent < 67.0 { + "audio-volume-medium-symbolic" + } else { + "audio-volume-high-symbolic" + } +} + +///| +/// Small symbolic icon next to a tab's label. +pub fn Tab::icon(self : Tab) -> String { + match self { + Playback | OutputDevices => "audio-speakers-symbolic" + Recording | InputDevices => "audio-input-microphone-symbolic" + } +} diff --git a/model/icons_test.mbt b/model/icons_test.mbt new file mode 100644 index 0000000..2db6498 --- /dev/null +++ b/model/icons_test.mbt @@ -0,0 +1,122 @@ +///| +fn dev( + media_class : String, + name~ : String = "", + description~ : String = "", + form_factor~ : String = "", + bus~ : String = "", + hints~ : Array[String] = [], +) -> @model.Node { + @model.Node::new( + 1, + media_class, + name~, + description~, + form_factor~, + bus~, + icon_hints=hints, + ) +} + +///| +test "device hint comes first, symbolic variant before plain" { + let c = @model.device_icon_chain( + dev("Audio/Sink", hints=["audio-card-pci"], form_factor="headphone"), + ) + assert_eq(c[0], "audio-card-pci-symbolic") + assert_eq(c[1], "audio-card-pci") + assert_eq(c[2], "audio-headphones-symbolic") +} + +///| +test "device form factors" { + let first = fn(ff, cls) { + @model.device_icon_chain(dev(cls, form_factor=ff))[0] + } + assert_eq(first("headset", "Audio/Sink"), "audio-headset-symbolic") + assert_eq(first("speaker", "Audio/Sink"), "audio-speakers-symbolic") + assert_eq( + first("microphone", "Audio/Source"), + "audio-input-microphone-symbolic", + ) + assert_eq(first("webcam", "Audio/Source"), "camera-web-symbolic") + assert_eq(first("handset", "Audio/Sink"), "phone-symbolic") + assert_eq(first("tv", "Audio/Sink"), "video-display-symbolic") + assert_eq(first("car", "Audio/Source"), "audio-input-microphone-symbolic") +} + +///| +test "hdmi, webcam and bluetooth heuristics" { + let hdmi = @model.device_icon_chain( + dev( + "Audio/Sink", + name="alsa_output.pci-0000_01_00.1.hdmi-stereo", + description="HDMI / DisplayPort", + ), + ) + assert_eq(hdmi[0], "video-display-symbolic") + let cam = @model.device_icon_chain( + dev( + "Audio/Source", + name="alsa_input.usb-EMEET_SmartCam_C960.mono-fallback", + description="EMEET SmartCam C960 Mono", + ), + ) + assert_eq(cam[0], "camera-web-symbolic") + let bt = @model.device_icon_chain( + dev("Audio/Sink", name="bluez_output.AA_BB.1"), + ) + assert_eq(bt[0], "audio-headphones-symbolic") + assert_eq( + @model.device_bus(dev("Audio/Sink", name="alsa_output.usb-X.analog-stereo")), + "usb", + ) +} + +///| +test "device chain always ends in fallbacks" { + let c = @model.device_icon_chain(dev("Audio/Sink")) + assert_eq(c, [ + "audio-speakers-symbolic", "audio-speakers", "audio-card-symbolic", "audio-x-generic-symbolic", + "image-missing", + ]) + let s = @model.device_icon_chain(dev("Audio/Source")) + assert_eq(s[0], "audio-input-microphone-symbolic") + assert_eq(s[s.length() - 1], "image-missing") +} + +///| +test "stream chain: hints, desktop lookup, binary, fallbacks" { + let n = @model.Node::new( + 9, + "Stream/Output/Audio", + app_name="Firefox", + app_binary="firefox", + app_id="org.mozilla.firefox", + icon_hints=["firefox-esr"], + ) + assert_eq(@model.stream_icon_chain(n), [ + "firefox-esr", "app:org.mozilla.firefox", "app:firefox", "app:Firefox", "org.mozilla.firefox", + "firefox", "application-x-executable-symbolic", "audio-x-generic-symbolic", "image-missing", + ]) + let bare = @model.Node::new(10, "Stream/Input/Audio") + assert_eq(@model.stream_icon_chain(bare), [ + "audio-input-microphone-symbolic", "application-x-executable-symbolic", "audio-x-generic-symbolic", + "image-missing", + ]) +} + +///| +test "volume icon follows volume and mute" { + assert_eq(@model.volume_icon(100.0, false), "audio-volume-high-symbolic") + assert_eq(@model.volume_icon(50.0, false), "audio-volume-medium-symbolic") + assert_eq(@model.volume_icon(10.0, false), "audio-volume-low-symbolic") + assert_eq(@model.volume_icon(0.0, false), "audio-volume-muted-symbolic") + assert_eq(@model.volume_icon(100.0, true), "audio-volume-muted-symbolic") +} + +///| +test "tab icons" { + assert_eq(@model.Tab::Playback.icon(), "audio-speakers-symbolic") + assert_eq(@model.Tab::InputDevices.icon(), "audio-input-microphone-symbolic") +} diff --git a/model/pkg.generated.mbti b/model/pkg.generated.mbti index a0c4120..07b7533 100644 --- a/model/pkg.generated.mbti +++ b/model/pkg.generated.mbti @@ -6,6 +6,10 @@ import { } // Values +pub const APP_LOOKUP_PREFIX : String = "app:" + +pub const ICON_MISSING : String = "image-missing" + pub const MAX_PERCENT : Double = 150.0 pub const METER_DECAY : Double = 0.04 @@ -16,16 +20,28 @@ pub fn clamp_percent(Double) -> Double pub fn compare_text(String, String) -> Int32 +pub fn default_button_icons() -> Array[String] + +pub fn device_bus(Node) -> String + +pub fn device_icon_chain(Node) -> Array[String] + pub fn diff_rows(Array[Row], Array[Row]) -> Diff pub fn display_title(Node) -> String +pub fn form_factor_icon(String, NodeKind) -> String + pub fn format_db(Double) -> String pub fn format_percent(Double) -> String pub fn format_volume(Array[Double]) -> String +pub fn icon_fallback_device() -> Array[String] + +pub fn icon_fallback_stream() -> Array[String] + pub fn linear_to_db(Double) -> Double? pub fn linear_to_percent(Double) -> Double @@ -44,6 +60,12 @@ pub fn scale_linked(Array[Double], Double) -> Array[Double] pub fn should_apply_remote(Int64, Int64, window_us? : Int64) -> Bool +pub fn stream_icon_chain(Node) -> Array[String] + +pub fn symbolic(String) -> String + +pub fn volume_icon(Double, Bool) -> String + // Errors // Types and methods @@ -84,12 +106,16 @@ pub(all) struct Node { media_class : String app_name : String media_name : String - icon_name : String + icon_hints : Array[String] + form_factor : String + bus : String + app_binary : String + app_id : String volumes : Array[Double] mute : Bool } pub fn Node::kind(Self) -> NodeKind -pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_name? : String, volumes? : Array[Double], mute? : Bool) -> Self +pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_hints? : Array[String], form_factor? : String, bus? : String, app_binary? : String, app_id? : String, volumes? : Array[Double], mute? : Bool) -> Self #derive(Eq, @debug.Debug) pub(all) enum NodeKind { @@ -106,7 +132,8 @@ pub(all) struct Row { id : Int32 title : String subtitle : String - icon_name : String + icons : Array[String] + volume_icon : String volumes : Array[Double] percent : Double volume_text : String @@ -137,6 +164,7 @@ pub(all) enum Tab { } pub fn Tab::all() -> Array[Self] pub fn Tab::empty_text(Self) -> String +pub fn Tab::icon(Self) -> String pub fn Tab::label(Self) -> String pub fn Tab::node_kind(Self) -> NodeKind diff --git a/model/state.mbt b/model/state.mbt index 296861f..9f12dce 100644 --- a/model/state.mbt +++ b/model/state.mbt @@ -33,7 +33,13 @@ pub(all) struct Node { media_class : String app_name : String // application.name (streams) media_name : String // media.name (streams) - icon_name : String // application.icon-name / device.icon-name + /// Icon names PipeWire/the app suggested, best first (application.icon-name, + /// media.icon-name for streams; device.icon-name, node.icon-name for devices). + icon_hints : Array[String] + form_factor : String // device.form-factor: "headset", "webcam", ... + bus : String // device.bus: "usb", "bluetooth", "pci", ... + app_binary : String // application.process.binary + app_id : String // pipewire.access.portal.app_id / application.id volumes : Array[Double] // linear, one per channel; empty = no volume mute : Bool } @@ -51,7 +57,11 @@ pub fn Node::new( description~ : String = "", app_name~ : String = "", media_name~ : String = "", - icon_name~ : String = "", + icon_hints~ : Array[String] = [], + form_factor~ : String = "", + bus~ : String = "", + app_binary~ : String = "", + app_id~ : String = "", volumes~ : Array[Double] = [], mute~ : Bool = false, ) -> Node { @@ -62,7 +72,11 @@ pub fn Node::new( media_class, app_name, media_name, - icon_name, + icon_hints, + form_factor, + bus, + app_binary, + app_id, volumes, mute, } diff --git a/model/view.mbt b/model/view.mbt index c500a28..e362990 100644 --- a/model/view.mbt +++ b/model/view.mbt @@ -60,7 +60,9 @@ pub(all) struct Row { id : Int32 title : String subtitle : String - icon_name : String + /// Icon-theme names to try in order (see icons.mbt). + icons : Array[String] + volume_icon : String // speaker icon matching volume + mute volumes : Array[Double] percent : Double // linked slider value (cubic %) volume_text : String // "75% (-7.50 dB)" @@ -125,11 +127,12 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] { id: n.id, title: display_title(n), subtitle, - icon_name: if n.icon_name != "" { - n.icon_name + icons: if is_stream { + stream_icon_chain(n) } else { - default_icon(kind) + device_icon_chain(n) }, + volume_icon: volume_icon(linked_percent(n.volumes), n.mute), volumes: n.volumes, percent: linked_percent(n.volumes), volume_text: if n.volumes.length() > 0 { @@ -150,17 +153,6 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] { }) } -///| -fn default_icon(kind : NodeKind) -> String { - match kind { - Sink => "audio-speakers-symbolic" - Source => "audio-input-microphone-symbolic" - PlaybackStream => "audio-x-generic-symbolic" - RecordStream => "audio-input-microphone-symbolic" - Other => "audio-x-generic-symbolic" - } -} - ///| /// Result of reconciling the rows shown on screen against new rows: which /// row ids to remove, which to add (with their index) and which to update. diff --git a/model/view_test.mbt b/model/view_test.mbt index cb9881a..8a26cee 100644 --- a/model/view_test.mbt +++ b/model/view_test.mbt @@ -33,7 +33,8 @@ test "device rows" { assert_eq(ins.length(), 1) assert_true(ins[0].mute) assert_true(ins[0].is_default) - assert_eq(ins[0].icon_name, "audio-input-microphone-symbolic") + assert_eq(ins[0].icons[0], "audio-input-microphone-symbolic") + assert_eq(ins[0].volume_icon, "audio-volume-muted-symbolic") } ///| diff --git a/tools/pkg-cc b/tools/pkg-cc index 8ab738d..ebaacd3 100755 --- a/tools/pkg-cc +++ b/tools/pkg-cc @@ -3,5 +3,5 @@ # not expand $(pkg-config ...) in flags, so append them here. Works for both # compiling stubs and linking the final executable. set -e -PKGS="${MOONMIX_PKGS:-libpipewire-0.3 wireplumber-0.5 gtk4}" +PKGS="${MOONMIX_PKGS:-libpipewire-0.3 wireplumber-0.5 gtk4 gio-unix-2.0}" exec "${MOONMIX_CC:-cc}" "$@" $(pkg-config --cflags --libs $PKGS) diff --git a/tools/smoke-test.sh b/tools/smoke-test.sh index 9f0c792..dac4642 100755 --- a/tools/smoke-test.sh +++ b/tools/smoke-test.sh @@ -25,11 +25,13 @@ context.objects = [ { factory = adapter args = { factory.name = support.null-audio-sink node.name = "test_speakers" node.description = "Test Speakers" + device.form-factor = "speaker" media.class = "Audio/Sink" audio.position = [ FL FR ] monitor.channel-volumes = true object.linger = true } } { factory = adapter args = { factory.name = support.null-audio-sink node.name = "test_headphones" node.description = "Test Headphones" + device.form-factor = "headphone" media.class = "Audio/Sink" audio.position = [ FL FR ] object.linger = true } } ] @@ -39,7 +41,7 @@ context.modules = [ args = { node.description = "Test Microphone" capture.props = { node.name = "test_mic_capture" node.passive = true audio.position = [ MONO ] } - playback.props = { node.name = "test_mic" media.class = "Audio/Source" audio.position = [ MONO ] } + playback.props = { node.name = "test_mic" device.form-factor = "microphone" media.class = "Audio/Source" audio.position = [ MONO ] } } } ] CONF @@ -87,10 +89,10 @@ DATA=23040000 { printf 'RIFF'; le32 $((DATA + 36)); printf 'WAVEfmt '; le32 16 printf '\001\000\002\000'; le32 48000; le32 192000; printf '\004\000\020\000' printf 'data'; le32 $DATA; head -c $DATA /dev/urandom; } > "$LOGS/noise.wav" -{ pw-play -P '{ application.name = "Test Player" media.name = "Smoke test tone" }' \ +{ pw-play -P '{ application.name = "Test Player" application.icon-name = "applications-multimedia-symbolic" media.name = "Smoke test tone" }' \ "$LOGS/noise.wav"; echo "pw-play exited with $?"; } >"$LOGS/play.log" 2>&1 & PIDS="$PIDS $!" pw-cat --record --raw --rate 48000 --channels 1 --format s16 \ - -P '{ application.name = "Test Recorder" media.name = "Mic capture" }' \ + -P '{ application.name = "Test Recorder" application.icon-name = "audio-input-microphone" media.name = "Mic capture" }' \ /dev/null >"$LOGS/record.log" 2>&1 & PIDS="$PIDS $!" wait_for 'Test Player | .*-> ' || true @@ -154,7 +156,7 @@ if [ -n "$SHOTS" ] && [ -x "$GUI" ]; then if command -v gtk4-broadwayd >/dev/null; then gtk4-broadwayd :5 >/dev/null 2>&1 & PIDS="$PIDS $!" sleep 1 - GDK_BACKEND=broadway BROADWAY_DISPLAY=:5 MOONMIX_SCREENSHOT="$SHOTS" \ + MOONMIX_ICON_DIR="$ROOT/data/icons" GDK_BACKEND=broadway BROADWAY_DISPLAY=:5 MOONMIX_SCREENSHOT="$SHOTS" \ timeout 30 "$GUI" || fail=1 else echo "gtk4-broadwayd not found; skipping GUI" diff --git a/wp/wp.mbt b/wp/wp.mbt index 36dcf9c..2644590 100644 --- a/wp/wp.mbt +++ b/wp/wp.mbt @@ -186,12 +186,22 @@ pub fn read_node(id : Int32) -> @model.Node? { node_prop(id, "node.nick") } } - let icon = { - let a = node_prop(id, "application.icon-name") + 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, "device.icon-name") + node_prop(id, "application.id") } } Some( @@ -202,7 +212,11 @@ pub fn read_node(id : Int32) -> @model.Node? { description~, app_name=node_prop(id, "application.name"), media_name=node_prop(id, "media.name"), - icon_name=icon, + 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~, ),