From 671814fc81f9d97766a0b3d19060107055ca3228 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 Feb 2023 20:39:02 -0500 Subject: [PATCH] Better isolation of window change event handling code --- src/main.rs | 6 ++---- src/window_change.rs | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1d39759..eae51c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::{ }; use sm_session_notifier::SMSessionNotifier; -use window_change::{win_event_hook_loop, WIN_CHANGE_CALLBACK}; +use window_change::{await_win_change_events}; use windows::{ core::Interface, Win32::{ @@ -131,9 +131,7 @@ fn main() { .boot_devices() .expect("failed to get initial audio devices and sessions"); - *WIN_CHANGE_CALLBACK.lock().unwrap() = Some(Box::new(move |s| { + await_win_change_events(Box::new(move |s| { muter.lock().unwrap().notify_window_changed(&s) })); - - win_event_hook_loop(); } diff --git a/src/window_change.rs b/src/window_change.rs index b6914b0..0108f5c 100644 --- a/src/window_change.rs +++ b/src/window_change.rs @@ -14,7 +14,7 @@ use windows::Win32::{ use crate::pid_to_exe::pid_to_exe_path; -pub static WIN_CHANGE_CALLBACK: Mutex>> = Mutex::new(None); +static WIN_CHANGE_CALLBACK: Mutex>> = Mutex::new(None); unsafe extern "system" fn win_event_proc( _hook: HWINEVENTHOOK, @@ -48,7 +48,8 @@ unsafe extern "system" fn win_event_proc( } } -pub fn win_event_hook_loop() { +pub fn await_win_change_events(callback: Box) { + *WIN_CHANGE_CALLBACK.lock().unwrap() = Some(callback); unsafe { SetWinEventHook( EVENT_SYSTEM_FOREGROUND,