Press mute key when the headset gets muted / unmuted

This commit is contained in:
Lennard Kittner
2026-02-25 19:38:09 +01:00
parent a5fa443bff
commit 23edae7395
5 changed files with 418 additions and 6 deletions

View File

@@ -17,6 +17,8 @@ use hidapi::{HidApi, HidDevice, HidError};
use std::{collections::HashSet, fmt::Display, time::Duration};
use thistermination::TerminationFull;
const PASSIVE_REFRESH_TIME_OUT: Duration = Duration::from_secs(2);
type DeviceFactory = fn(DeviceState) -> Box<dyn Device>;
struct DeviceEntry {
@@ -660,7 +662,7 @@ pub trait Device {
/// Only the battery level is actively queried because it is not communicated by the device on its own
fn passive_refresh_state(&mut self) -> Result<(), DeviceError> {
if self.allow_passive_refresh() {
if let Some(events) = self.wait_for_updates(Duration::from_secs(1)) {
if let Some(events) = self.wait_for_updates(PASSIVE_REFRESH_TIME_OUT) {
for event in events {
self.get_device_state_mut().update_self_with_event(&event);
}

View File

@@ -1,4 +1,5 @@
use clap::{Arg, Command};
use enigo::{Direction, Enigo, Key, Keyboard, Settings};
use std::time::Duration;
mod status_tray;
@@ -15,10 +16,24 @@ fn main() {
.long("refresh_interval")
.required(false)
.help("Set the refresh interval (in seconds)")
.default_value("3")
.value_parser(clap::value_parser!(u64)),
)
.arg(
Arg::new("press_mute_key_on_headset_mute")
.long("press_mute_key_on_headset_mute")
.required(false)
.help("The app will simulate pressing the microphone key whoever the headsets is muted or unmuted.")
.default_value("true")
.value_parser(clap::value_parser!(bool)),
)
.get_matches();
let mut enigo = Enigo::new(&Settings::default()).unwrap();
let refresh_interval = *matches.get_one::<u64>("refresh_interval").unwrap_or(&3);
let press_mute_key = *matches
.get_one::<bool>("press_mute_key_on_headset_mute")
.unwrap_or(&true);
let refresh_interval = Duration::from_secs(refresh_interval);
let tray_handler = TrayHandler::new(StatusTray::new());
loop {
@@ -39,6 +54,8 @@ fn main() {
std::thread::sleep(refresh_interval);
// with the default refresh_interval the state is only actively queried every 3min
// querying the device to frequently can lead to instability
let mute_state = device.get_device_state().muted.clone();
match if run_counter % 30 == 0 {
device.active_refresh_state()
} else {
@@ -51,6 +68,12 @@ fn main() {
break; // try to reconnect
}
};
if mute_state.is_some() && mute_state != device.get_device_state().muted {
//TODO: macOS and windows have to use another key
if press_mute_key {
enigo.key(Key::MicMute, Direction::Click).unwrap();
}
}
tray_handler.update(device.get_device_state());
run_counter += 1;
}