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

3
Cargo.lock generated
View File

@@ -112,8 +112,7 @@ dependencies = [
[[package]]
name = "pulsectl-rs"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06a988bceed1981b2c5fc4a3da0e4e073fdaff8e6bd022b089f54bc573dc3cfc"
source = "git+https://github.com/manuelVo/pulsectl-rs.git?branch=sink-source-name-mixup#7fca2b319d840a51fd852f3a471d0f5ebb37b1b0"
dependencies = [
"libpulse-binding",
]

View File

@@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
hidapi = {version="1.3.3", default-features=false, features=["linux-shared-hidraw"]}
libpulse-binding = "2.26.0"
pulsectl-rs = "0.3.2"
pulsectl-rs = {git = "https://github.com/manuelVo/pulsectl-rs.git", branch="sink-source-name-mixup"}
thiserror = "1.0.30"

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(())
}