Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec<ListedDevice> {
};
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))]
Expand All @@ -107,6 +123,33 @@ pub fn list_device_details(host: &Host, output: bool) -> Vec<ListedDevice> {
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<Item = &'a str>) -> 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<String> {
device_names(list_device_details(host, output))
Expand Down
77 changes: 77 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = std::ffi::OsString>) -> 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();
Expand Down Expand Up @@ -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!(
Expand Down
Loading