From acbe81e622a236336f7ec94cd4523dedf834a793 Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 20:55:05 -0500 Subject: [PATCH 1/3] Handle command-line help and version before startup --- Cargo.toml | 1 + src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index aad1bfc..1675ca4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ windows = { version = "0.62", features = [ "Win32_Security", "Win32_System_Com", "Win32_System_Com_StructuredStorage", + "Win32_System_Console", "Win32_System_Threading", "Win32_System_Variant", "Win32_UI_HiDpi", diff --git a/src/main.rs b/src/main.rs index ab4da61..32abc96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,68 @@ use std::sync::Arc; use eframe::egui; +#[derive(Debug, PartialEq, Eq)] +enum CliAction { + Launch, + Help, + Version, + Error(String), +} + +fn parse_args(args: impl IntoIterator) -> CliAction { + let mut args = args.into_iter(); + let mut action = CliAction::Launch; + while let Some(arg) = args.next() { + match arg.to_str() { + Some("--help" | "-h") => action = CliAction::Help, + Some("--version" | "-V") if action != CliAction::Help => action = CliAction::Version, + Some("--version" | "-V" | "--minimized") => {} + Some("--after-update") => { + if !args + .next() + .is_some_and(|pid| pid.to_str().is_some_and(|pid| pid.parse::().is_ok())) + { + return CliAction::Error("--after-update requires a process ID".into()); + } + } + _ => return CliAction::Error(format!("unknown argument: {}", arg.to_string_lossy())), + } + } + action +} + +const USAGE: &str = "Usage: openmic [--minimized]\n\nOptions:\n -h, --help Print help\n -V, --version Print version\n --minimized Start in the notification area when enabled\n"; + +#[cfg(windows)] +fn attach_parent_console() { + use windows::Win32::System::Console::{ATTACH_PARENT_PROCESS, AttachConsole}; + // SAFETY: attaching to the parent console takes no pointers and is best effort. + let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) }; +} + +#[cfg(not(windows))] +fn attach_parent_console() {} + fn main() -> eframe::Result<()> { + match parse_args(std::env::args_os().skip(1)) { + CliAction::Launch => {} + CliAction::Help => { + attach_parent_console(); + print!("{USAGE}"); + return Ok(()); + } + CliAction::Version => { + attach_parent_console(); + println!("openmic {}", env!("CARGO_PKG_VERSION")); + return Ok(()); + } + CliAction::Error(error) => { + attach_parent_console(); + eprintln!("error: {error}\n\n{USAGE}"); + std::process::exit(2); + } + } + // After an update, wait for the old copy to quit before claiming the // single-instance lock and the audio devices. let updated = update::wait_for_previous_version(); @@ -186,6 +247,28 @@ fn work_area_size() -> Option<[f32; 2]> { mod tests { use super::*; + #[test] + fn command_line_actions_and_internal_launch_arguments() { + let parse = |args: &[&str]| parse_args(args.iter().map(std::ffi::OsString::from)); + assert_eq!(parse(&[]), CliAction::Launch); + assert_eq!( + parse(&["--minimized", "--after-update", "123"]), + CliAction::Launch + ); + for flag in ["--help", "-h"] { + assert_eq!(parse(&[flag]), CliAction::Help); + } + for flag in ["--version", "-V"] { + assert_eq!(parse(&[flag]), CliAction::Version); + } + assert!(matches!(parse(&["--unknown"]), CliAction::Error(_))); + assert!(matches!(parse(&["--after-update"]), CliAction::Error(_))); + assert!(matches!( + parse(&["--after-update", "nope"]), + CliAction::Error(_) + )); + } + #[test] fn restored_windows_fit_smaller_screens_in_both_directions() { assert_eq!( From bac675028ac9fad6387244983b2d5116da427b74 Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 20:55:23 -0500 Subject: [PATCH 2/3] Hide ALSA no-op microphone devices --- src/engine.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/engine.rs b/src/engine.rs index 3a6822a..b97eb27 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -97,6 +97,22 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec { }; if let Ok(devices) = iter { for device in devices { + #[cfg(target_os = "linux")] + if !output && host.id() == cpal::HostId::Alsa { + let id = device.id().map(|id| id.id().to_owned()).unwrap_or_default(); + let description = device.description().ok(); + let name = device.to_string(); + if is_noop_alsa_input( + &id, + std::iter::once(name.as_str()).chain( + description + .iter() + .flat_map(|description| description.extended()), + ), + ) { + continue; + } + } listed.push(ListedDevice { name: device.to_string(), #[cfg(not(windows))] @@ -107,6 +123,33 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec { listed } +/// ALSA's null PCM can be exposed under an alias, so inspect its description too. +#[cfg(target_os = "linux")] +fn is_noop_alsa_input<'a>(id: &str, descriptions: impl IntoIterator) -> bool { + id.trim().eq_ignore_ascii_case("null") + || descriptions.into_iter().any(|description| { + let text = description.trim().to_ascii_lowercase(); + text == "null" + || (text.contains("discard all samples") && text.contains("generate zero samples")) + }) +} + +#[cfg(all(test, target_os = "linux"))] +#[test] +fn filters_alsa_noop_inputs_by_id_or_description() { + assert!(is_noop_alsa_input("null", ["Anything"])); + assert!(is_noop_alsa_input("alias", ["Null"])); + assert!(is_noop_alsa_input( + "alias", + [ + "Dummy", + "Discard all samples (playback) or generate zero samples (capture)" + ] + )); + assert!(!is_noop_alsa_input("hw:0", ["USB microphone"])); + assert!(!is_noop_alsa_input("hw:1", ["Null Audio USB microphone"])); +} + /// Deduped friendly names for a direction. The first device keeps the name. pub fn list_devices(host: &Host, output: bool) -> Vec { device_names(list_device_details(host, output)) From ab944ccc9b862ea48049ec2cb7730bf6a289af68 Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:02:00 -0500 Subject: [PATCH 3/3] Keep --after-update lenient like the updater's own parser --- src/main.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 32abc96..b1f6938 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,13 +62,10 @@ fn parse_args(args: impl IntoIterator) -> CliAction { Some("--help" | "-h") => action = CliAction::Help, Some("--version" | "-V") if action != CliAction::Help => action = CliAction::Version, Some("--version" | "-V" | "--minimized") => {} + // Passed by the updater with the old copy's process ID. A missing + // or bad ID only skips the wait, as it always has. Some("--after-update") => { - if !args - .next() - .is_some_and(|pid| pid.to_str().is_some_and(|pid| pid.parse::().is_ok())) - { - return CliAction::Error("--after-update requires a process ID".into()); - } + args.next(); } _ => return CliAction::Error(format!("unknown argument: {}", arg.to_string_lossy())), } @@ -262,11 +259,8 @@ mod tests { assert_eq!(parse(&[flag]), CliAction::Version); } assert!(matches!(parse(&["--unknown"]), CliAction::Error(_))); - assert!(matches!(parse(&["--after-update"]), CliAction::Error(_))); - assert!(matches!( - parse(&["--after-update", "nope"]), - CliAction::Error(_) - )); + assert_eq!(parse(&["--after-update"]), CliAction::Launch); + assert_eq!(parse(&["--after-update", "nope"]), CliAction::Launch); } #[test]