Update to current winapi, split project
This commit is contained in:
27
crates/auto_mute_cli/Cargo.toml
Normal file
27
crates/auto_mute_cli/Cargo.toml
Normal file
@@ -0,0 +1,27 @@
|
||||
[package]
|
||||
name = "auto_mute_cli"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies.windows]
|
||||
version = "0.58.0"
|
||||
features = [
|
||||
"implement",
|
||||
"Win32_Media_Audio",
|
||||
"Win32_UI_Shell_PropertiesSystem",
|
||||
"Data_Xml_Dom",
|
||||
"Win32_UI",
|
||||
"Win32_UI_Accessibility",
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_System_Threading",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
"Win32_System_Com",
|
||||
"Win32_System_Com_StructuredStorage",
|
||||
"Win32_Devices_FunctionDiscovery",
|
||||
"Win32_Storage_FileSystem",
|
||||
"Win32_System_WindowsProgramming",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
auto_mute_lib = { path = "../auto_mute_lib" }
|
||||
52
crates/auto_mute_cli/src/main.rs
Normal file
52
crates/auto_mute_cli/src/main.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
env,
|
||||
error::Error,
|
||||
fs::{self, File},
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
use auto_mute_lib::muter::MuterThread;
|
||||
use windows::{
|
||||
Win32::{
|
||||
Foundation::HANDLE,
|
||||
Storage::FileSystem::{
|
||||
FindCloseChangeNotification, FindFirstChangeNotificationW, FindNextChangeNotification,
|
||||
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
||||
},
|
||||
System::{
|
||||
Com::{CoInitializeEx, COINIT_MULTITHREADED},
|
||||
Threading::{WaitForSingleObject, INFINITE}
|
||||
},
|
||||
}, core::w,
|
||||
};
|
||||
|
||||
pub fn load_mute_txt(file_name: &str) -> HashSet<String> {
|
||||
let file = File::open(file_name).unwrap();
|
||||
HashSet::from_iter(BufReader::new(file).lines().map(|line| line.unwrap()))
|
||||
}
|
||||
|
||||
pub fn await_file_change(file_name: &str) -> Result<(), Box<dyn Error>> {
|
||||
unsafe {
|
||||
let md = fs::metadata(file_name)?.modified()?;
|
||||
let handle = FindFirstChangeNotificationW(w!("."), false, FILE_NOTIFY_CHANGE_LAST_WRITE)?;
|
||||
while md == fs::metadata(file_name)?.modified()? {
|
||||
WaitForSingleObject(HANDLE(handle.0), INFINITE);
|
||||
FindNextChangeNotification(handle)?;
|
||||
}
|
||||
println!("File change detected, restarting");
|
||||
FindCloseChangeNotification(handle)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap();
|
||||
}
|
||||
let mute_file: String = env::args().nth(1).unwrap_or("mute.txt".to_string());
|
||||
loop {
|
||||
let mut _mt = MuterThread::new(load_mute_txt(&mute_file));
|
||||
await_file_change(&mute_file).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user