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/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)) diff --git a/src/main.rs b/src/main.rs index ab4da61..b1f6938 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,65 @@ 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") => {} + // 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") => { + args.next(); + } + _ => 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 +244,25 @@ 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_eq!(parse(&["--after-update"]), CliAction::Launch); + assert_eq!(parse(&["--after-update", "nope"]), CliAction::Launch); + } + #[test] fn restored_windows_fit_smaller_screens_in_both_directions() { assert_eq!(