From 5d9c8b4d0c30272143efd40d8ea14ab9dd6f7035 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 23 Feb 2023 19:06:34 -0500 Subject: [PATCH] Remove extraneous logging Version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- mute.txt | 3 ++- src/device_notification_client.rs | 6 ------ src/main.rs | 24 ++++++++++++++---------- src/session_notification.rs | 6 ------ src/window_change.rs | 9 +++++---- 7 files changed, 23 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e1ec73..425b450 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "auto_mute_tool" -version = "0.1.0" +version = "0.2.0" dependencies = [ "windows", ] diff --git a/Cargo.toml b/Cargo.toml index 39a418a..b0ffade 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "auto_mute_tool" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/mute.txt b/mute.txt index 45696cc..2caf36a 100644 --- a/mute.txt +++ b/mute.txt @@ -13,4 +13,5 @@ hollow_knight.exe sm64.us.f3dex2e.exe teardown.exe underrail.exe -valheim.exe \ No newline at end of file +valheim.exe +Spotify.exe \ No newline at end of file diff --git a/src/device_notification_client.rs b/src/device_notification_client.rs index 6fe8df6..4b65e5d 100644 --- a/src/device_notification_client.rs +++ b/src/device_notification_client.rs @@ -50,9 +50,3 @@ impl IMMNotificationClient_Impl for DeviceNotificationClient { Ok(()) } } - -impl Drop for DeviceNotificationClient { - fn drop(&mut self) { - println!("DNC Drop"); - } -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ffab3c5..e9dacfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::{ collections::HashSet, error::Error, ffi::OsString, - fs::File, + fs::{File, self}, io::{BufRead, BufReader}, path::Path, ptr::null_mut, @@ -22,7 +22,7 @@ use windows::{ core::{Interface}, Win32::{ Media::Audio::{IAudioSessionControl2, ISimpleAudioVolume}, - System::{Com::{CoInitializeEx, COINIT_MULTITHREADED}, Threading::WaitForSingleObject, WindowsProgramming::INFINITE}, Storage::FileSystem::{FindFirstChangeNotificationW, FILE_NOTIFY_CHANGE_LAST_WRITE, FindCloseChangeNotification}, Foundation::HANDLE, + System::{Com::{CoInitializeEx, COINIT_MULTITHREADED}, Threading::WaitForSingleObject, WindowsProgramming::INFINITE}, Storage::FileSystem::{FindFirstChangeNotificationW, FILE_NOTIFY_CHANGE_LAST_WRITE, FindCloseChangeNotification, FindNextChangeNotification}, Foundation::HANDLE, }, w, }; @@ -176,15 +176,20 @@ impl Drop for MuterThread { } } -fn load_mute_txt() -> HashSet { - let file = File::open("mute.txt").unwrap(); +fn load_mute_txt(file_name: &str) -> HashSet { + let file = File::open(file_name).unwrap(); return HashSet::from_iter(BufReader::new(file).lines().map(|line| line.unwrap())); } -fn await_file_change() -> Result<(), Box> { +fn await_file_change(file_name: &str) -> Result<(), Box> { unsafe { + let md = fs::metadata(file_name)?.modified()?; let handle = FindFirstChangeNotificationW(w!("."), false, FILE_NOTIFY_CHANGE_LAST_WRITE)?; - WaitForSingleObject(HANDLE{0: handle.0}, INFINITE).ok()?; + while md == fs::metadata(file_name)?.modified()? { + WaitForSingleObject(HANDLE{0: handle.0}, INFINITE).ok()?; + FindNextChangeNotification(handle); + } + println!("File change detected, restarting"); FindCloseChangeNotification(handle).ok()?; Ok(()) } @@ -194,10 +199,9 @@ fn main() { unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).unwrap(); } + let mute_file = "mute.txt"; loop { - let mut _mt = MuterThread::new(load_mute_txt()); - await_file_change().unwrap(); + let mut _mt = MuterThread::new(load_mute_txt(mute_file)); + await_file_change(mute_file).unwrap(); } - - } diff --git a/src/session_notification.rs b/src/session_notification.rs index ad4fed6..5a18f20 100644 --- a/src/session_notification.rs +++ b/src/session_notification.rs @@ -27,9 +27,3 @@ impl<'a> IAudioSessionNotification_Impl for SessionNotification { Ok(()) } } - -impl<'a> Drop for SessionNotification { - fn drop(&mut self) { - println!("SN drop"); - } -} \ No newline at end of file diff --git a/src/window_change.rs b/src/window_change.rs index 7c51bbb..1981a56 100644 --- a/src/window_change.rs +++ b/src/window_change.rs @@ -3,7 +3,7 @@ use std::{sync::{Mutex, atomic::AtomicU32, Arc}, thread::{self, JoinHandle}}; use windows::Win32::{ Foundation::{HINSTANCE, HWND, WPARAM, LPARAM}, UI::{ - Accessibility::{SetWinEventHook, HWINEVENTHOOK}, + Accessibility::{SetWinEventHook, HWINEVENTHOOK, UnhookWinEvent}, WindowsAndMessaging::{ DispatchMessageW, GetForegroundWindow, GetMessageW, GetWindowThreadProcessId, TranslateMessage, EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND, MSG, @@ -53,7 +53,7 @@ type WinCallback = Box; pub fn await_win_change_events(callback: WinCallback) { *WIN_CHANGE_CALLBACK.lock().unwrap() = Some(callback); unsafe { - SetWinEventHook( + let fg_event = SetWinEventHook( EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, HINSTANCE::default(), @@ -62,7 +62,7 @@ pub fn await_win_change_events(callback: WinCallback) { 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS, ); - SetWinEventHook( + let min_event = SetWinEventHook( EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZEEND, HINSTANCE::default(), @@ -77,6 +77,8 @@ pub fn await_win_change_events(callback: WinCallback) { TranslateMessage(&msg); DispatchMessageW(&msg); } + UnhookWinEvent(fg_event); + UnhookWinEvent(min_event); } } @@ -98,7 +100,6 @@ impl Drop for WindowChangeMonitor { join_handle.join().expect("Unable to terminate window change thread"); } - println!("Joined window change thread"); } }