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,
|
path::Path,
|
||||||
ptr::null_mut,
|
ptr::null_mut,
|
||||||
sync::{
|
sync::{
|
||||||
mpsc::{self, Receiver, Sender},
|
|
||||||
Arc, Mutex,
|
Arc, Mutex,
|
||||||
},
|
},
|
||||||
thread,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use sm_session_notifier::SMSessionNotifier;
|
use sm_session_notifier::SMSessionNotifier;
|
||||||
@@ -116,6 +114,8 @@ impl SessionMuter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for SessionMuter {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
unsafe {
|
unsafe {
|
||||||
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap();
|
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap();
|
||||||
@@ -133,15 +133,9 @@ fn main() {
|
|||||||
.boot_devices()
|
.boot_devices()
|
||||||
.expect("failed to get initial audio devices and sessions");
|
.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(
|
||||||
*WIN_CHANGE_CHANNEL_TX.lock().unwrap() = Some(tx);
|
Box::new(move |s| muter.lock().unwrap().notify_window_changed(&s))
|
||||||
|
);
|
||||||
|
|
||||||
thread::spawn(move || {
|
win_event_hook_loop();
|
||||||
win_event_hook_loop();
|
|
||||||
});
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let path = rx.recv().unwrap();
|
|
||||||
muter.lock().unwrap().notify_window_changed(&path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::sync::Mutex;
|
||||||
sync::{mpsc::Sender, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
use windows::Win32::{
|
use windows::Win32::{
|
||||||
Foundation::{HINSTANCE, HWND},
|
Foundation::{HINSTANCE, HWND},
|
||||||
@@ -17,7 +14,7 @@ use windows::Win32::{
|
|||||||
|
|
||||||
use crate::pid_to_exe::pid_to_exe_path;
|
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(
|
unsafe extern "system" fn win_event_proc(
|
||||||
_hook: HWINEVENTHOOK,
|
_hook: HWINEVENTHOOK,
|
||||||
@@ -35,15 +32,13 @@ unsafe extern "system" fn win_event_proc(
|
|||||||
let hwnd = GetForegroundWindow();
|
let hwnd = GetForegroundWindow();
|
||||||
GetWindowThreadProcessId(hwnd, Some(&mut pid));
|
GetWindowThreadProcessId(hwnd, Some(&mut pid));
|
||||||
pid_to_exe_path(pid)
|
pid_to_exe_path(pid)
|
||||||
.and_then(|path| {
|
.map(|path|
|
||||||
WIN_CHANGE_CHANNEL_TX
|
WIN_CHANGE_CHANNEL_TX
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.send(path)
|
(&path))
|
||||||
.map_err(|err| err.into())
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|err| {
|
.unwrap_or_else(|err| {
|
||||||
println!(
|
println!(
|
||||||
"Error finding process with pid {} for hwnd: {:?}: {:?}",
|
"Error finding process with pid {} for hwnd: {:?}: {:?}",
|
||||||
|
|||||||
Reference in New Issue
Block a user