Code cleanup part 1

This commit is contained in:
Your Name
2023-02-19 10:10:12 -05:00
parent 739dd6613b
commit 23e9f624f4
2 changed files with 48 additions and 36 deletions

View File

@@ -24,5 +24,4 @@ features = [
"Win32_UI_WindowsAndMessaging", "Win32_UI_WindowsAndMessaging",
"Win32_System_Com", "Win32_System_Com",
"Win32_System_Com_StructuredStorage" "Win32_System_Com_StructuredStorage"
] ]

View File

@@ -4,10 +4,12 @@ use once_cell::sync::Lazy;
use std::{ use std::{
collections::HashSet, collections::HashSet,
error::Error, error::Error,
ffi::{OsStr, OsString}, ffi::OsString,
fs::File,
io::{BufRead, BufReader},
path::Path, path::Path,
ptr::null_mut, ptr::null_mut,
sync::{Arc, Mutex}, fs::File, io::{BufReader, BufRead}, sync::{Arc, Mutex},
}; };
use widestring::U16CStr; use widestring::U16CStr;
@@ -25,9 +27,9 @@ use windows::{
UI::{ UI::{
Accessibility::{SetWinEventHook, HWINEVENTHOOK}, Accessibility::{SetWinEventHook, HWINEVENTHOOK},
WindowsAndMessaging::{ WindowsAndMessaging::{
DispatchMessageW, GetMessageW, GetWindowThreadProcessId, TranslateMessage, DispatchMessageW, GetForegroundWindow, GetMessageW, GetWindowThreadProcessId,
EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, WINEVENT_OUTOFCONTEXT, TranslateMessage, EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG,
WINEVENT_SKIPOWNPROCESS, GetForegroundWindow, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS,
}, },
}, },
}, },
@@ -48,9 +50,16 @@ unsafe extern "system" fn win_event_proc(
let mut pid: u32 = 0; 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 // 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? // and immediately fires again with the hwnd for explorer?
GetWindowThreadProcessId(GetForegroundWindow(), Some(&mut pid)); let hwnd = GetForegroundWindow();
let path = pid_to_exe_path(pid).unwrap_or("".to_string()); GetWindowThreadProcessId(hwnd, Some(&mut pid));
MUTER.lock().unwrap().notify_window_changed(&path); 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<IAudioSessionControl>, newsession: &core::option::Option<IAudioSessionControl>,
) -> windows::core::Result<()> { ) -> windows::core::Result<()> {
let ses: IAudioSessionControl2 = newsession.as_ref().unwrap().cast().unwrap(); let ses: IAudioSessionControl2 = newsession.as_ref().unwrap().cast().unwrap();
MUTER.lock() MUTER
.lock()
.unwrap() .unwrap()
.add_session_if_interesting(ses) .add_session_if_interesting(ses)
.unwrap(); .unwrap();
@@ -125,14 +135,13 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient {
} }
fn OnDeviceAdded(&self, pwstrdeviceid: &windows::core::PCWSTR) -> windows::core::Result<()> { fn OnDeviceAdded(&self, pwstrdeviceid: &windows::core::PCWSTR) -> windows::core::Result<()> {
println!("OnDeviceAdded"); MUTER
MUTER.lock() .lock()
.unwrap() .unwrap()
.add_device_by_id(pwstrdeviceid) .add_device_by_id(pwstrdeviceid)
.map_err(|error| { .unwrap_or_else(|error| {
println!("Error adding device: {:?}", error); println!("Error adding device: {:?}", error);
}) });
.unwrap_or(());
Ok(()) Ok(())
} }
@@ -167,7 +176,7 @@ struct SessionMuter {
session_notification: IAudioSessionNotification, session_notification: IAudioSessionNotification,
mute_executables: HashSet<String>, mute_executables: HashSet<String>,
mute_flag: bool, mute_flag: bool,
session_managers: Vec<IAudioSessionManager2> session_managers: Vec<IAudioSessionManager2>,
} }
fn load_mute_txt() -> HashSet<String> { fn load_mute_txt() -> HashSet<String> {
@@ -227,13 +236,18 @@ impl SessionMuter {
} }
unsafe fn notify_window_changed(self: &mut SessionMuter, path: &str) { 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 file_name = binding.to_os_string().to_string_lossy().to_string();
let mute_flag = !self.mute_executables.contains(&file_name); let mute_flag = !self.mute_executables.contains(&file_name);
if mute_flag != self.mute_flag { if mute_flag != self.mute_flag {
self.mute_flag = mute_flag; self.mute_flag = mute_flag;
self.set_mute_all(self.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 let device_collection = self
.device_enumerator .device_enumerator
.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?; .EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?;
let dc = 0..device_collection.GetCount()?; for device in (0..device_collection.GetCount()?).map(|x| device_collection.Item(x)) {
let devices = dc.map(|x| device_collection.Item(x)); let mmdevice = device?;
for x in devices {
let mmdevice = x?;
self.add_device(mmdevice)?; self.add_device(mmdevice)?;
} }
println!("Boot done"); println!("All devices initialized.");
return Ok(()); return Ok(());
} }
} }
@@ -281,24 +293,24 @@ impl SessionMuter {
unsafe fn add_device( unsafe fn add_device(
self: &mut SessionMuter, self: &mut SessionMuter,
mmdevice: windows::Win32::Media::Audio::IMMDevice, device: windows::Win32::Media::Audio::IMMDevice,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let sm: IAudioSessionManager2 = mmdevice.Activate(CLSCTX_ALL, None)?; let sm: IAudioSessionManager2 = device.Activate(CLSCTX_ALL, None)?;
sm.RegisterSessionNotification(&self.session_notification)?; sm.RegisterSessionNotification(&self.session_notification)?;
let sesenum = sm.GetSessionEnumerator()?; let session_enumerator = sm.GetSessionEnumerator()?;
let sn_count = sesenum.GetCount()?; let session_count = session_enumerator.GetCount()?;
let device_sessions = (0..sn_count) let device_sessions = (0..session_count)
.map(|idx| sesenum.GetSession(idx)) .map(|idx| {
.map(|x| match x { session_enumerator
Err(e) => Err(e), .GetSession(idx)
Ok(v) => v.cast(), .and_then(|session| session.cast())
}) })
.collect::<Result<Vec<IAudioSessionControl2>, _>>()?; .collect::<Result<Vec<IAudioSessionControl2>, _>>()?;
for session in device_sessions { for session in device_sessions {
self.add_session_if_interesting(session)?; self.add_session_if_interesting(session)?;
} }
let str = U16CStr::from_ptr_str(mmdevice.GetId()?.0).to_string_lossy(); let str = U16CStr::from_ptr_str(device.GetId()?.0).to_string_lossy();
println!("Device Added: {} {}", str, sn_count); println!("Device Added: {} {}", str, session_count);
self.session_managers.push(sm); self.session_managers.push(sm);
Ok(()) Ok(())
} }
@@ -307,12 +319,13 @@ impl SessionMuter {
unsafe impl Send for SessionMuter {} unsafe impl Send for SessionMuter {}
unsafe impl Sync for SessionMuter {} unsafe impl Sync for SessionMuter {}
static MUTER: Lazy<Arc<Mutex<SessionMuter>>> = Lazy::new(|| Arc::new(Mutex::new(SessionMuter::new()))); static MUTER: Lazy<Arc<Mutex<SessionMuter>>> =
Lazy::new(|| Arc::new(Mutex::new(SessionMuter::new())));
fn main() { fn main() {
unsafe { unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap(); 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(); win_event_hook_loop();
} }