From 739dd6613bce8a3460c765899f010a216d9b6aa0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 Feb 2023 22:59:22 -0500 Subject: [PATCH] Add mute.txt file reading --- Cargo.lock | 24 +++++++++++------------ Cargo.toml | 4 ++-- mute.txt | 16 +++++++++++++++ src/main.rs | 56 ++++++++++++++++++++--------------------------------- 4 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 mute.txt diff --git a/Cargo.lock b/Cargo.lock index 795b473..6a22809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,33 +13,33 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.12.0" +version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "proc-macro2" -version = "1.0.39" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.18" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" dependencies = [ "proc-macro2", ] [[package]] name = "syn" -version = "1.0.96" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ "proc-macro2", "quote", @@ -48,15 +48,15 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.0" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "widestring" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5babd2d3fcd28bcf9712ef93e437684156f5c46173a9718829aa57aa4aa37a8" +checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8" [[package]] name = "windows" diff --git a/Cargo.toml b/Cargo.toml index 9f6a2bd..54f117f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -widestring = "1.0.0" -once_cell = "1.12.0" +widestring = "1.0.2" +once_cell = "1.17.1" [dependencies.windows] version = "0.44.0" diff --git a/mute.txt b/mute.txt new file mode 100644 index 0000000..3055131 --- /dev/null +++ b/mute.txt @@ -0,0 +1,16 @@ +Baba is You.exe +Celeste.exe +Cemu.exe +DarkSoulsII.exe +GRIS.exe +Hades.exe +LWIW.exe +Super Mario 64 Plus.exe +Trials of Mana-Win64-Shipping.exe +VampireSurvivors.exe +cod.exe +hollow_knight.exe +sm64.us.f3dex2e.exe +teardown.exe +underrail.exe +valheim.exe diff --git a/src/main.rs b/src/main.rs index eecdc43..cf5f37a 100644 --- a/src/main.rs +++ b/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, ) -> 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>>, device_enumerator: IMMDeviceEnumerator, - dnc: IMMNotificationClient, + device_notification_client: IMMNotificationClient, session_notification: IAudioSessionNotification, - good_files: HashSet, + mute_executables: HashSet, mute_flag: bool, session_managers: Vec } +fn load_mute_txt() -> HashSet { + 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> { + fn boot_devices(self: &mut SessionMuter) -> Result<(), Box> { 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(()); } }