From 3bb1ca9b1cfd29f985a2a6c1ccfb77f06d540d46 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 Feb 2023 23:27:49 -0500 Subject: [PATCH] Remove unnecessary threading/channel --- src/main.rs | 18 ++++++------------ src/window_change.rs | 13 ++++--------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 855b552..864cc2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,10 +13,8 @@ use std::{ path::Path, ptr::null_mut, sync::{ - mpsc::{self, Receiver, Sender}, Arc, Mutex, }, - thread, }; use sm_session_notifier::SMSessionNotifier; @@ -116,6 +114,8 @@ impl SessionMuter { } } +unsafe impl Send for SessionMuter {} + fn main() { unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).unwrap(); @@ -133,15 +133,9 @@ fn main() { .boot_devices() .expect("failed to get initial audio devices and sessions"); - let (tx, rx): (Sender, Receiver) = mpsc::channel(); - *WIN_CHANGE_CHANNEL_TX.lock().unwrap() = Some(tx); + *WIN_CHANGE_CHANNEL_TX.lock().unwrap() = Some( + Box::new(move |s| muter.lock().unwrap().notify_window_changed(&s)) + ); - thread::spawn(move || { - win_event_hook_loop(); - }); - - loop { - let path = rx.recv().unwrap(); - muter.lock().unwrap().notify_window_changed(&path); - } + win_event_hook_loop(); } diff --git a/src/window_change.rs b/src/window_change.rs index 618919b..2d759a8 100644 --- a/src/window_change.rs +++ b/src/window_change.rs @@ -1,7 +1,4 @@ -use std::{ - sync::{mpsc::Sender, Mutex}, -}; - +use std::sync::Mutex; use windows::Win32::{ Foundation::{HINSTANCE, HWND}, @@ -17,7 +14,7 @@ use windows::Win32::{ use crate::pid_to_exe::pid_to_exe_path; -pub static WIN_CHANGE_CHANNEL_TX: Mutex>> = Mutex::new(None); +pub static WIN_CHANGE_CHANNEL_TX: Mutex>> = Mutex::new(None); unsafe extern "system" fn win_event_proc( _hook: HWINEVENTHOOK, @@ -35,15 +32,13 @@ unsafe extern "system" fn win_event_proc( let hwnd = GetForegroundWindow(); GetWindowThreadProcessId(hwnd, Some(&mut pid)); pid_to_exe_path(pid) - .and_then(|path| { + .map(|path| WIN_CHANGE_CHANNEL_TX .lock() .unwrap() .as_ref() .unwrap() - .send(path) - .map_err(|err| err.into()) - }) + (&path)) .unwrap_or_else(|err| { println!( "Error finding process with pid {} for hwnd: {:?}: {:?}",