From f2d5263244ea5fc17562b867db32e359bb544b92 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 Feb 2023 11:20:21 -0500 Subject: [PATCH] Improve session filename error handling --- src/main.rs | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 60ee95b..c4325bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,7 +105,7 @@ impl IAudioSessionNotification_Impl for SessionNotification { MUTER .lock() .unwrap() - .add_session_if_interesting(ses) + .add_session(ses) .unwrap(); Ok(()) } @@ -201,28 +201,34 @@ impl SessionMuter { } } - fn add_session_if_interesting( + fn add_session( self: &mut SessionMuter, session: IAudioSessionControl2, ) -> Result<(), Box> { - let file_name = self.session_to_filename(&session); - if file_name.is_err() { - return Ok(()); - } - let fn_str = file_name - .unwrap() - .to_os_string() - .to_string_lossy() - .to_string(); - if self.mute_executables.contains(&fn_str) { - println!("Adding session from: {:?}", fn_str); - unsafe { - let volume: ISimpleAudioVolume = session.cast()?; - volume.SetMute(self.mute_flag, null_mut())?; + unsafe { + if session.IsSystemSoundsSession().is_ok() { + return Ok(()); } - self.sessions.lock().unwrap().push(session); } - return Ok(()); + match self.session_to_filename(&session) { + Ok(file_name) => { + let fn_str = file_name + .to_string_lossy() + .to_string(); + if self.mute_executables.contains(&fn_str) { + println!("Adding session from: {:?}", fn_str); + unsafe { + let volume: ISimpleAudioVolume = session.cast()?; + volume.SetMute(self.mute_flag, null_mut())?; + } + self.sessions.lock().unwrap().push(session); + } + }, + Err(err) => { + println!("Unable to get filename for session {:?}", err); + }, + } + Ok(()) } unsafe fn set_mute_all(self: &mut SessionMuter, mute: bool) { @@ -308,7 +314,7 @@ impl SessionMuter { }) .collect::, _>>()?; for session in device_sessions { - self.add_session_if_interesting(session)?; + self.add_session(session)?; } let prop_store = device.OpenPropertyStore(STGM_READ)?; // this definition isn't in windows-rs yet apparently :(