Remove unnecessary threading/channel
This commit is contained in:
18
src/main.rs
18
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<String>, Receiver<String>) = 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();
|
||||
}
|
||||
|
||||
@@ -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<Option<Sender<String>>> = Mutex::new(None);
|
||||
pub static WIN_CHANGE_CHANNEL_TX: Mutex<Option<Box<dyn Fn(&str)+Send>>> = 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: {:?}: {:?}",
|
||||
|
||||
Reference in New Issue
Block a user