Files
HyperHeadset/src/main.rs
Lennard Kittner 352180568d Refactor changing headset properties
Tray app can now change headset properties
2026-03-18 19:32:55 +01:00

114 lines
4.1 KiB
Rust

#[cfg(target_os = "linux")]
mod status_tray;
#[cfg(not(target_os = "linux"))]
fn main() {
eprintln!("The tray app currently only supports Linux. Please use hyper_headset_cli instead.");
}
#[cfg(target_os = "linux")]
fn main() {
use clap::{Arg, Command};
use enigo::{Direction, Enigo, Key, Keyboard, Settings};
use std::sync::mpsc;
use std::time::Duration;
use hyper_headset::devices::connect_compatible_device;
use status_tray::{StatusTray, TrayHandler};
#[cfg(target_os = "linux")]
{
use hyper_headset::act_as_askpass_handler;
use hyper_headset::prompt_user_for_udev_rule;
if let Ok(name) = std::env::current_exe() {
if let Some(name) = name.to_str() {
if let Ok(askpass) = std::env::var("SUDO_ASKPASS") {
if name == askpass {
act_as_askpass_handler();
}
}
}
}
prompt_user_for_udev_rule();
}
let matches = Command::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about("A tray application for monitoring HyperX headsets.")
.arg(
Arg::new("refresh_interval")
.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")
.long("press_mute_key")
.required(false)
.help("The app will simulate pressing the microphone mute 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").unwrap_or(&true);
let refresh_interval = Duration::from_secs(refresh_interval);
let (tx, rx) = mpsc::channel();
let tray_handler = TrayHandler::new(StatusTray::new(tx));
loop {
let mut device = loop {
match connect_compatible_device() {
Ok(d) => break d,
Err(e) => {
tray_handler.clear_state();
eprintln!("Connecting failed with error: {e}")
}
}
std::thread::sleep(Duration::from_secs(1));
};
// Run loop
let mut run_counter = 0;
loop {
let mute_state = device.get_device_state().device_properties.muted;
match if run_counter % 30 == 0 {
device.active_refresh_state()
} else {
device.passive_refresh_state()
} {
Ok(()) => (),
Err(error) => {
eprintln!("{error}");
tray_handler.update(device.get_device_state());
break; // try to reconnect
}
};
if mute_state.is_some()
&& mute_state != device.get_device_state().device_properties.muted
{
//TODO: macOS and windows have to use another key
if press_mute_key {
enigo.key(Key::MicMute, Direction::Click).unwrap();
}
}
// with the default refresh_interval the state is only actively queried every 3min
// querying the device to frequently can lead to instability
let first = rx.recv_timeout(refresh_interval);
for command in first.into_iter().chain(rx.try_iter()) {
let _ = device.try_apply(command);
std::thread::sleep(hyper_headset::devices::RESPONSE_DELAY);
let _ = device.active_refresh_state();
}
tray_handler.update(device.get_device_state());
run_counter += 1;
}
}
}