Mute the mic in the system when the headset mute button is being pressed
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -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",
|
||||
]
|
||||
|
||||
@@ -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"
|
||||
|
||||
30
src/main.rs
30
src/main.rs
@@ -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,7 +45,21 @@ fn is_sink_headset(sink: &DeviceInfo) -> bool {
|
||||
}
|
||||
|
||||
fn handle_report(device: &HidDevice, report: &[u8]) -> Result<(), HidError> {
|
||||
if report[0] == 17 {
|
||||
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();
|
||||
|
||||
// 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();
|
||||
@@ -55,7 +69,7 @@ fn handle_report(device: &HidDevice, report: &[u8]) -> Result<(), HidError> {
|
||||
|
||||
// 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();
|
||||
let new_sink = sinks.iter().find(|sink| !is_device_headset(*sink)).unwrap();
|
||||
sink_controller
|
||||
.set_default_device(new_sink.name.as_ref().unwrap())
|
||||
.unwrap();
|
||||
@@ -64,19 +78,21 @@ fn handle_report(device: &HidDevice, report: &[u8]) -> Result<(), HidError> {
|
||||
|
||||
// Turn off the lights
|
||||
device.write(&[
|
||||
0x11, 0xff, 0x04, 0x3c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
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_sink_headset(*sink)).unwrap();
|
||||
let headset = sinks.iter().find(|sink| is_device_headset(*sink)).unwrap();
|
||||
sink_controller
|
||||
.set_default_device(headset.name.as_ref().unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user