Big refactor, file change notifications added
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{Arc, Mutex},
|
||||
sync::mpsc::{self, Receiver, Sender},
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
device_notification_client::DeviceNotificationClient, session_notification::SessionNotification,
|
||||
device_notification_client::{DeviceNotificationClient, DeviceNotificationObserver},
|
||||
session_notification::{SessionNotification, SessionObserver},
|
||||
};
|
||||
use windows::{
|
||||
core::{Interface, PCWSTR},
|
||||
@@ -17,46 +19,72 @@ use windows::{
|
||||
System::Com::{CoCreateInstance, CLSCTX_ALL, STGM_READ},
|
||||
},
|
||||
};
|
||||
|
||||
pub enum SMMessage {
|
||||
Session(IAudioSessionControl2),
|
||||
Device(PCWSTR),
|
||||
Exit(),
|
||||
}
|
||||
|
||||
unsafe impl Send for SMMessage {}
|
||||
|
||||
struct SessionToMessage {
|
||||
sender: Sender<SMMessage>,
|
||||
}
|
||||
|
||||
impl SessionObserver for SessionToMessage {
|
||||
fn add_session(&self, session: IAudioSessionControl2) {
|
||||
self.sender
|
||||
.send(SMMessage::Session(session))
|
||||
.unwrap_or_else(|_| println!("Failed to add new session"));
|
||||
}
|
||||
}
|
||||
|
||||
struct DeviceToMessage {
|
||||
sender: Sender<SMMessage>,
|
||||
}
|
||||
|
||||
impl DeviceNotificationObserver for DeviceToMessage {
|
||||
fn add_device(&self, device_id: &windows::core::PCWSTR) {
|
||||
self.sender
|
||||
.send(SMMessage::Device(*device_id))
|
||||
.unwrap_or_else(|_| println!("Failed to add new device"));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct SMSessionNotifier {
|
||||
device_enumerator: IMMDeviceEnumerator,
|
||||
device_notification_client: IMMNotificationClient,
|
||||
session_notification: IAudioSessionNotification,
|
||||
session_managers: Vec<IAudioSessionManager2>,
|
||||
notification_function: Box<dyn Fn(IAudioSessionControl2)>,
|
||||
receiver: Receiver<SMMessage>,
|
||||
}
|
||||
|
||||
impl SMSessionNotifier {
|
||||
pub(crate) fn new(
|
||||
callback: Box<dyn Fn(IAudioSessionControl2)>,
|
||||
) -> Arc<Mutex<Option<SMSessionNotifier>>> {
|
||||
let session_notifier_arc: Arc<Mutex<Option<SMSessionNotifier>>> =
|
||||
Arc::new(Mutex::new(None));
|
||||
let sn2 = Arc::downgrade(&session_notifier_arc);
|
||||
let sn3 = sn2.clone();
|
||||
let notifier = SMSessionNotifier {
|
||||
sender: mpsc::Sender<SMMessage>,
|
||||
receiver: mpsc::Receiver<SMMessage>,
|
||||
) -> SMSessionNotifier {
|
||||
SMSessionNotifier {
|
||||
session_managers: Vec::new(),
|
||||
device_enumerator: unsafe {
|
||||
CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).unwrap()
|
||||
},
|
||||
device_notification_client: IMMNotificationClient::from(DeviceNotificationClient {
|
||||
callback: Box::new(move |id| {
|
||||
match sn2.upgrade().unwrap().lock().unwrap().as_mut().unwrap().add_device_by_id(id) {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
println!("Failed to add new device: {:?}", e);
|
||||
},
|
||||
}
|
||||
observer: Box::new(DeviceToMessage {
|
||||
sender: sender.clone(),
|
||||
}),
|
||||
}),
|
||||
session_notification: IAudioSessionNotification::from(SessionNotification {
|
||||
callback: Box::new(move |session| {
|
||||
(sn3.upgrade().unwrap().lock().unwrap().as_mut().unwrap().notification_function)(session)
|
||||
observer: Box::new(SessionToMessage {
|
||||
sender: sender.clone(),
|
||||
}),
|
||||
}),
|
||||
notification_function: callback,
|
||||
};
|
||||
*session_notifier_arc.lock().unwrap() = Some(notifier);
|
||||
return session_notifier_arc;
|
||||
receiver,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn boot_devices(self: &mut SMSessionNotifier) -> Result<(), Box<dyn Error>> {
|
||||
@@ -105,8 +133,57 @@ impl SMSessionNotifier {
|
||||
}
|
||||
let prop_store = device.OpenPropertyStore(STGM_READ)?;
|
||||
let prop_var = prop_store.GetValue(&PKEY_Device_FriendlyName)?;
|
||||
println!("Device Added: {} Existing Sessions: {}", prop_var.Anonymous.Anonymous.Anonymous.pwszVal.to_string()?, session_count);
|
||||
println!(
|
||||
"Device Added: {} Existing Sessions: {}",
|
||||
prop_var.Anonymous.Anonymous.Anonymous.pwszVal.to_string()?,
|
||||
session_count
|
||||
);
|
||||
self.session_managers.push(sm);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
self.boot_devices()?;
|
||||
|
||||
loop {
|
||||
let msg = self.receiver.recv()?;
|
||||
match msg {
|
||||
SMMessage::Session(session) => {
|
||||
(self.notification_function)(session);
|
||||
}
|
||||
SMMessage::Device(id) => {
|
||||
self.add_device_by_id(&id)?;
|
||||
}
|
||||
SMMessage::Exit() => break,
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SMSessionNotifierThread {
|
||||
handle: Option<JoinHandle<()>>,
|
||||
sender: Sender<SMMessage>,
|
||||
}
|
||||
|
||||
impl SMSessionNotifierThread {
|
||||
pub fn new(s: Box<dyn Fn(IAudioSessionControl2) + Send>) -> SMSessionNotifierThread {
|
||||
let (sender, receiver) = mpsc::channel::<SMMessage>();
|
||||
SMSessionNotifierThread {
|
||||
sender: sender.clone(),
|
||||
handle: Some(thread::spawn(move || {
|
||||
let mut session_notifier = SMSessionNotifier::new(s, sender, receiver);
|
||||
session_notifier.run().unwrap();
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for SMSessionNotifierThread {
|
||||
fn drop(&mut self) {
|
||||
if let Some(handle) = self.handle.take() {
|
||||
self.sender.send(SMMessage::Exit()).unwrap();
|
||||
handle.join().unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user