Mute the mic in the system when the headset mute button is being pressed

This commit is contained in:
2022-02-17 00:35:25 +01:00
parent 7844d4f6eb
commit fee6f14ab0
3 changed files with 45 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
use hidapi::{HidApi, HidDevice, HidError};
use libpulse_binding::proplist::Proplist;
use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController};
use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController};
trait Headset {
const VENDOR_ID: u16;
@@ -33,7 +33,7 @@ fn hex_prop_is(proplist: &Proplist, prop_name: &str, expected: u16) -> bool {
.unwrap_or(false)
}
fn is_sink_headset(sink: &DeviceInfo) -> bool {
fn is_device_headset(sink: &DeviceInfo) -> bool {
let proplist = &sink.proplist;
if !hex_prop_is(proplist, "device.vendor.id", G733::VENDOR_ID) {
return false;
@@ -45,37 +45,53 @@ fn is_sink_headset(sink: &DeviceInfo) -> bool {
}
fn handle_report(device: &HidDevice, report: &[u8]) -> Result<(), HidError> {
if report[0] == 17 {
if report[2] == 8 {
// Power event
let mut sink_controller = SinkController::create().unwrap();
match report[0] {
8 => {
// Mute button
let mut source_controller = SourceController::create().unwrap();
let sources = source_controller.list_devices().unwrap();
let mic = sources
.iter()
.find(|source| source.monitor.is_none() && is_device_headset(*source))
.unwrap();
if report[4] == 0 {
// Power off
// Invert mute state of pulse device
println!("{:#?}", mic);
source_controller.set_device_mute_by_index(mic.index, !mic.mute);
}
17 => {
if report[2] == 8 {
// Power event
let mut sink_controller = SinkController::create().unwrap();
// Set default sink to non headset
let sinks = sink_controller.list_devices().unwrap();
let new_sink = sinks.iter().find(|sink| !is_sink_headset(*sink)).unwrap();
sink_controller
.set_default_device(new_sink.name.as_ref().unwrap())
.unwrap();
} else {
// Power on event
if report[4] == 0 {
// Power off
// Turn off the lights
device.write(&[
0x11, 0xff, 0x04, 0x3c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
])?;
// Set default sink to non headset
let sinks = sink_controller.list_devices().unwrap();
let new_sink = sinks.iter().find(|sink| !is_device_headset(*sink)).unwrap();
sink_controller
.set_default_device(new_sink.name.as_ref().unwrap())
.unwrap();
} else {
// Power on event
// Set default sink to the headset
let sinks = sink_controller.list_devices().unwrap();
let headset = sinks.iter().find(|sink| is_sink_headset(*sink)).unwrap();
sink_controller
.set_default_device(headset.name.as_ref().unwrap())
.unwrap();
// Turn off the lights
device.write(&[
0x11, 0xff, 0x04, 0x3c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
])?;
// Set default sink to the headset
let sinks = sink_controller.list_devices().unwrap();
let headset = sinks.iter().find(|sink| is_device_headset(*sink)).unwrap();
sink_controller
.set_default_device(headset.name.as_ref().unwrap())
.unwrap();
}
}
}
_ => {}
}
Ok(())
}