Add mute.txt file reading
This commit is contained in:
56
src/main.rs
56
src/main.rs
@@ -7,7 +7,7 @@ use std::{
|
||||
ffi::{OsStr, OsString},
|
||||
path::Path,
|
||||
ptr::null_mut,
|
||||
sync::{Arc, Mutex},
|
||||
sync::{Arc, Mutex}, fs::File, io::{BufReader, BufRead},
|
||||
};
|
||||
|
||||
use widestring::U16CStr;
|
||||
@@ -83,20 +83,6 @@ fn win_event_hook_loop() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Okay, the main way this will function:
|
||||
- MMDeviceEnumerator - get all current devices
|
||||
- RegisterEndpointNotificationCallback - get updates on device add/remove
|
||||
- For each new device found
|
||||
- Activate Session Manager on Device
|
||||
- Register Session Notification on Session Manager
|
||||
- Get session enumerator from session manager
|
||||
- Detect sessions from applications we care about by getting session PID and mapping to process names using existing code
|
||||
|
||||
- This should really work based on PID and unmute only the open process's PID when focused? Though maybe not due to the weird nature of electron apps and shit?
|
||||
- Keep it the way it is for now I guess
|
||||
*/
|
||||
|
||||
#[windows::core::implement(IAudioSessionNotification)]
|
||||
struct SessionNotification {}
|
||||
|
||||
@@ -106,11 +92,6 @@ impl IAudioSessionNotification_Impl for SessionNotification {
|
||||
newsession: &core::option::Option<IAudioSessionControl>,
|
||||
) -> windows::core::Result<()> {
|
||||
let ses: IAudioSessionControl2 = newsession.as_ref().unwrap().cast().unwrap();
|
||||
println!("New session created: {}", unsafe {
|
||||
ses.GetProcessId()
|
||||
.and_then(|x| Ok(pid_to_exe_path(x).unwrap().to_string()))
|
||||
.unwrap_or("UNKNOWN".to_string())
|
||||
});
|
||||
MUTER.lock()
|
||||
.unwrap()
|
||||
.add_session_if_interesting(ses)
|
||||
@@ -140,7 +121,6 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient {
|
||||
_pwstrdeviceid: &windows::core::PCWSTR,
|
||||
_dwnewstate: u32,
|
||||
) -> windows::core::Result<()> {
|
||||
println!("OnDeviceStateChanged");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -168,7 +148,6 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient {
|
||||
_role: windows::Win32::Media::Audio::ERole,
|
||||
_pwstrdefaultdeviceid: &windows::core::PCWSTR,
|
||||
) -> windows::core::Result<()> {
|
||||
println!("OnDefaultDeviceChanged");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -177,7 +156,6 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient {
|
||||
_pwstrdeviceid: &windows::core::PCWSTR,
|
||||
_key: &windows::Win32::UI::Shell::PropertiesSystem::PROPERTYKEY,
|
||||
) -> windows::core::Result<()> {
|
||||
println!("OnPropertyValueChanged");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -185,13 +163,18 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient {
|
||||
struct SessionMuter {
|
||||
sessions: Arc<Mutex<Vec<IAudioSessionControl2>>>,
|
||||
device_enumerator: IMMDeviceEnumerator,
|
||||
dnc: IMMNotificationClient,
|
||||
device_notification_client: IMMNotificationClient,
|
||||
session_notification: IAudioSessionNotification,
|
||||
good_files: HashSet<String>,
|
||||
mute_executables: HashSet<String>,
|
||||
mute_flag: bool,
|
||||
session_managers: Vec<IAudioSessionManager2>
|
||||
}
|
||||
|
||||
fn load_mute_txt() -> HashSet<String> {
|
||||
let file = File::open("mute.txt").unwrap();
|
||||
return HashSet::from_iter(BufReader::new(file).lines().map(|line| line.unwrap()));
|
||||
}
|
||||
|
||||
impl SessionMuter {
|
||||
fn new() -> SessionMuter {
|
||||
let s = Arc::new(Mutex::new(Vec::new()));
|
||||
@@ -201,9 +184,9 @@ impl SessionMuter {
|
||||
device_enumerator: unsafe {
|
||||
CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).unwrap()
|
||||
},
|
||||
dnc: IMMNotificationClient::from(DeviceNotificationClient {}),
|
||||
device_notification_client: IMMNotificationClient::from(DeviceNotificationClient {}),
|
||||
session_notification: IAudioSessionNotification::from(SessionNotification {}),
|
||||
good_files: HashSet::from(["hollow_knight.exe"].map(|s| s.to_string())),
|
||||
mute_executables: load_mute_txt(),
|
||||
mute_flag: true,
|
||||
}
|
||||
}
|
||||
@@ -221,8 +204,8 @@ impl SessionMuter {
|
||||
.to_os_string()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
println!("Got session: {:?}", fn_str);
|
||||
if self.good_files.contains(&fn_str) {
|
||||
println!("Adding session from: {:?}", fn_str);
|
||||
if self.mute_executables.contains(&fn_str) {
|
||||
unsafe {
|
||||
let volume: ISimpleAudioVolume = session.cast()?;
|
||||
volume.SetMute(self.mute_flag, null_mut())?;
|
||||
@@ -246,15 +229,18 @@ impl SessionMuter {
|
||||
unsafe fn notify_window_changed(self: &mut SessionMuter, path: &str) {
|
||||
let binding = Path::new(path).file_name().unwrap_or(OsStr::new(""));
|
||||
let file_name = binding.to_os_string().to_string_lossy().to_string();
|
||||
self.mute_flag = !self.good_files.contains(&file_name);
|
||||
self.set_mute_all(self.mute_flag);
|
||||
println!("Mute set to {} due to selection of {}", self.mute_flag, file_name);
|
||||
let mute_flag = !self.mute_executables.contains(&file_name);
|
||||
if mute_flag != self.mute_flag {
|
||||
self.mute_flag = mute_flag;
|
||||
self.set_mute_all(self.mute_flag);
|
||||
println!("Mute set to {} due to foreground window: {}", self.mute_flag, file_name);
|
||||
}
|
||||
}
|
||||
|
||||
fn boot_devices(self: &mut SessionMuter) -> Result<String, Box<dyn Error>> {
|
||||
fn boot_devices(self: &mut SessionMuter) -> Result<(), Box<dyn Error>> {
|
||||
unsafe {
|
||||
self.device_enumerator
|
||||
.RegisterEndpointNotificationCallback(&self.dnc)?;
|
||||
.RegisterEndpointNotificationCallback(&self.device_notification_client)?;
|
||||
let device_collection = self
|
||||
.device_enumerator
|
||||
.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?;
|
||||
@@ -265,7 +251,7 @@ impl SessionMuter {
|
||||
self.add_device(mmdevice)?;
|
||||
}
|
||||
println!("Boot done");
|
||||
return Ok("Done".to_string());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user