From 23e9f624f4f46842b2350cd3ed6e7e420fd2b479 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 Feb 2023 10:10:12 -0500 Subject: [PATCH] Code cleanup part 1 --- Cargo.toml | 1 - src/main.rs | 83 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54f117f..b90d0c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,5 +24,4 @@ features = [ "Win32_UI_WindowsAndMessaging", "Win32_System_Com", "Win32_System_Com_StructuredStorage" - ] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index cf5f37a..85d1446 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,12 @@ use once_cell::sync::Lazy; use std::{ collections::HashSet, error::Error, - ffi::{OsStr, OsString}, + ffi::OsString, + fs::File, + io::{BufRead, BufReader}, path::Path, ptr::null_mut, - sync::{Arc, Mutex}, fs::File, io::{BufReader, BufRead}, + sync::{Arc, Mutex}, }; use widestring::U16CStr; @@ -25,9 +27,9 @@ use windows::{ UI::{ Accessibility::{SetWinEventHook, HWINEVENTHOOK}, WindowsAndMessaging::{ - DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage, - EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT, - WINEVENT_SKIPOWNPROCESS, GetForegroundWindow, + DispatchMessageW, GetForegroundWindow, GetMessageW, GetWindowThreadProcessId, + TranslateMessage, EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, + WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS, }, }, }, @@ -48,9 +50,16 @@ unsafe extern "system" fn win_event_proc( let mut pid: u32 = 0; // Instead of using the hwnd passed to us in the wineventproc, call GetForegroundWindow again because Hollow Knight does something weird // and immediately fires again with the hwnd for explorer? - GetWindowThreadProcessId(GetForegroundWindow(), Some(&mut pid)); - let path = pid_to_exe_path(pid).unwrap_or("".to_string()); - MUTER.lock().unwrap().notify_window_changed(&path); + let hwnd = GetForegroundWindow(); + GetWindowThreadProcessId(hwnd, Some(&mut pid)); + pid_to_exe_path(pid) + .map(|path| MUTER.lock().unwrap().notify_window_changed(&path)) + .unwrap_or_else(|err| { + println!( + "Error finding process with pid {} for hwnd: {:?}: {:?}", + pid, hwnd, err + ) + }); } } @@ -92,7 +101,8 @@ impl IAudioSessionNotification_Impl for SessionNotification { newsession: &core::option::Option, ) -> windows::core::Result<()> { let ses: IAudioSessionControl2 = newsession.as_ref().unwrap().cast().unwrap(); - MUTER.lock() + MUTER + .lock() .unwrap() .add_session_if_interesting(ses) .unwrap(); @@ -125,14 +135,13 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient { } fn OnDeviceAdded(&self, pwstrdeviceid: &windows::core::PCWSTR) -> windows::core::Result<()> { - println!("OnDeviceAdded"); - MUTER.lock() + MUTER + .lock() .unwrap() .add_device_by_id(pwstrdeviceid) - .map_err(|error| { + .unwrap_or_else(|error| { println!("Error adding device: {:?}", error); - }) - .unwrap_or(()); + }); Ok(()) } @@ -167,7 +176,7 @@ struct SessionMuter { session_notification: IAudioSessionNotification, mute_executables: HashSet, mute_flag: bool, - session_managers: Vec + session_managers: Vec, } fn load_mute_txt() -> HashSet { @@ -227,13 +236,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 binding = Path::new(path) + .file_name() + .expect("failed to extract filename from path"); let file_name = binding.to_os_string().to_string_lossy().to_string(); 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); + println!( + "Mute set to {} due to foreground window: {}", + self.mute_flag, file_name + ); } } @@ -244,13 +258,11 @@ impl SessionMuter { let device_collection = self .device_enumerator .EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?; - let dc = 0..device_collection.GetCount()?; - let devices = dc.map(|x| device_collection.Item(x)); - for x in devices { - let mmdevice = x?; + for device in (0..device_collection.GetCount()?).map(|x| device_collection.Item(x)) { + let mmdevice = device?; self.add_device(mmdevice)?; } - println!("Boot done"); + println!("All devices initialized."); return Ok(()); } } @@ -281,24 +293,24 @@ impl SessionMuter { unsafe fn add_device( self: &mut SessionMuter, - mmdevice: windows::Win32::Media::Audio::IMMDevice, + device: windows::Win32::Media::Audio::IMMDevice, ) -> Result<(), Box> { - let sm: IAudioSessionManager2 = mmdevice.Activate(CLSCTX_ALL, None)?; + let sm: IAudioSessionManager2 = device.Activate(CLSCTX_ALL, None)?; sm.RegisterSessionNotification(&self.session_notification)?; - let sesenum = sm.GetSessionEnumerator()?; - let sn_count = sesenum.GetCount()?; - let device_sessions = (0..sn_count) - .map(|idx| sesenum.GetSession(idx)) - .map(|x| match x { - Err(e) => Err(e), - Ok(v) => v.cast(), + let session_enumerator = sm.GetSessionEnumerator()?; + let session_count = session_enumerator.GetCount()?; + let device_sessions = (0..session_count) + .map(|idx| { + session_enumerator + .GetSession(idx) + .and_then(|session| session.cast()) }) .collect::, _>>()?; for session in device_sessions { self.add_session_if_interesting(session)?; } - let str = U16CStr::from_ptr_str(mmdevice.GetId()?.0).to_string_lossy(); - println!("Device Added: {} {}", str, sn_count); + let str = U16CStr::from_ptr_str(device.GetId()?.0).to_string_lossy(); + println!("Device Added: {} {}", str, session_count); self.session_managers.push(sm); Ok(()) } @@ -307,12 +319,13 @@ impl SessionMuter { unsafe impl Send for SessionMuter {} unsafe impl Sync for SessionMuter {} -static MUTER: Lazy>> = Lazy::new(|| Arc::new(Mutex::new(SessionMuter::new()))); +static MUTER: Lazy>> = + Lazy::new(|| Arc::new(Mutex::new(SessionMuter::new()))); fn main() { unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).unwrap(); } - MUTER.lock().unwrap().boot_devices().unwrap(); + MUTER.lock().unwrap().boot_devices().expect("failed to initialize devices"); win_event_hook_loop(); }