Add mute change notification, remove nwg trash, package updates

This commit is contained in:
Your Name
2025-02-16 11:19:29 -05:00
parent 5f69fc664a
commit 989c5c05a5
7 changed files with 182 additions and 318 deletions

315
Cargo.lock generated
View File

@@ -15,8 +15,9 @@ name = "auto_mute_gui"
version = "0.2.0"
dependencies = [
"auto_mute_lib",
"native-windows-derive",
"native-windows-gui",
"windows",
"windows-core",
"winresource",
]
[[package]]
@@ -28,134 +29,32 @@ dependencies = [
]
[[package]]
name = "autocfg"
version = "1.1.0"
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "bitflags"
version = "1.3.2"
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "bumpalo"
version = "3.14.0"
name = "indexmap"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "js-sys"
version = "0.3.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a"
checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
dependencies = [
"wasm-bindgen",
"equivalent",
"hashbrown",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libm"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "native-windows-derive"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76134ae81020d89d154f619fd2495a2cecad204276b1dc21174b55e4d0975edd"
dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "native-windows-gui"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f7003a669f68deb6b7c57d74fff4f8e533c44a3f0b297492440ef4ff5a28454"
dependencies = [
"bitflags",
"lazy_static",
"newline-converter",
"plotters",
"plotters-backend",
"stretch",
"winapi",
"winapi-build",
]
[[package]]
name = "newline-converter"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f71d09d5c87634207f894c6b31b6a2b2c64ea3bdcf71bd5599fdbbe1600c00f"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "plotters"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45"
dependencies = [
"num-traits",
"plotters-backend",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609"
[[package]]
name = "proc-macro-crate"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
dependencies = [
"toml",
]
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "proc-macro2"
@@ -177,43 +76,31 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.188"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.188"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
name = "stretch"
version = "0.3.2"
name = "serde_spanned"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0dc6d20ce137f302edf90f9cd3d278866fd7fb139efca6f246161222ad6d87"
checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
dependencies = [
"lazy_static",
"libm",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
"serde",
]
[[package]]
@@ -229,11 +116,36 @@ dependencies = [
[[package]]
name = "toml"
version = "0.5.11"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
@@ -243,102 +155,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.38",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "web-sys"
version = "0.3.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "windows"
@@ -371,7 +191,7 @@ checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@@ -382,7 +202,7 @@ checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.38",
"syn",
]
[[package]]
@@ -467,3 +287,22 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [
"memchr",
]
[[package]]
name = "winresource"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77e2aaaf8cfa92078c0c0375423d631f82f2f57979c2884fdd5f604a11e45329"
dependencies = [
"toml",
"version_check",
]

View File

@@ -3,7 +3,7 @@ use std::{
env,
error::Error,
fs::{self, File},
io::{BufRead, BufReader},
io::{BufRead, BufReader}
};
use auto_mute_lib::muter::MuterThread;
@@ -14,18 +14,10 @@ use windows::{
FindCloseChangeNotification, FindFirstChangeNotificationW, FindNextChangeNotification,
FILE_NOTIFY_CHANGE_LAST_WRITE,
},
System::{
Com::{CoInitializeEx, COINIT_MULTITHREADED},
Threading::{WaitForSingleObject, INFINITE}
},
System::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()?;
@@ -40,13 +32,17 @@ pub fn await_file_change(file_name: &str) -> Result<(), Box<dyn Error>> {
}
}
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()))
}
fn main() {
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED).unwrap();
}
MuterThread::com_init();
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));
let mut _mt = MuterThread::new(load_mute_txt(&mute_file), None);
await_file_change(&mute_file).unwrap();
}
}

View File

@@ -3,8 +3,20 @@ name = "auto_mute_gui"
version = "0.2.0"
edition = "2021"
[build-dependencies]
winresource = "0.1.17"
[dependencies]
auto_mute_lib = { path = "../auto_mute_lib" }
native-windows-gui = "1.0.13"
native-windows-derive = "1.0.5"
windows-core = "0.58.0"
[dependencies.windows]
version = "0.58.0"
features = [
"Win32_UI",
"Win32_UI_Accessibility",
"Win32_UI_WindowsAndMessaging",
"Win32_UI_Shell",
"Win32_Foundation",
"Win32_System_LibraryLoader",
]

View File

@@ -0,0 +1,11 @@
extern crate winresource;
fn main() {
if cfg!(target_os = "windows") {
winresource::WindowsResource::new()
.set_icon(r"C:\Users\ultra\ownCloud\ShitMuter\ShitMuter\icon1.ico")
.set_icon_with_id(r"C:\Users\ultra\ownCloud\ShitMuter\ShitMuter\icon1.ico", "ICO_UNMUTE")
.set_icon_with_id(r"C:\Users\ultra\ownCloud\ShitMuter\ShitMuter\icon2.ico", "ICO_MUTE")
.compile().unwrap();
}
}

View File

@@ -1,70 +1,55 @@
extern crate native_windows_gui as nwg;
extern crate native_windows_derive as nwd;
use std::{collections::HashSet, env, fs::File, io::{BufRead, BufReader}};
use nwd::NwgUi;
use nwg::NativeUi;
use auto_mute_lib::muter::MuterThread;
use windows::Win32::{Foundation::HWND, System::LibraryLoader::GetModuleHandleW, UI::{Shell::{Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIM_ADD, NOTIFYICONDATAW}, WindowsAndMessaging::{LoadIconW, WM_USER}}};
use windows_core::w;
const WM_TRAY_MENU: u32 = WM_USER;
#[derive(Default, NwgUi)]
pub struct BasicApp {
#[nwg_control(size: (300, 515), position: (300, 300), title: "Basic example", flags: "WINDOW")]
window: nwg::Window,
#[nwg_resource(source_file: Some("./res/muted.ico"))]
muted_icon: nwg::Icon,
#[nwg_resource(source_file: Some("./res/unmuted.ico"))]
unmuted_icon: nwg::Icon,
#[nwg_layout(parent: window, spacing: 1)]
grid: nwg::GridLayout,
#[nwg_control()]
#[nwg_layout_item(layout: grid, row: 0, col: 0)]
listbox: nwg::ListBox<String>,
#[nwg_control(text: "Test", size: (50, 50))]
#[nwg_layout_item(layout: grid, col: 0, row: 1, row_span: 2)]
hello_button: nwg::Button,
#[nwg_control(parent: window, popup: true)]
tray_menu: nwg::Menu,
#[nwg_control(parent: tray_menu, text: "Show")]
#[nwg_events(OnMenuItemSelected: [BasicApp::on_show])]
tray_show: nwg::MenuItem,
#[nwg_control(parent: tray_menu)]
tray_sep: nwg::MenuSeparator,
#[nwg_control(parent: tray_menu, text: "Exit")]
#[nwg_events(OnMenuItemSelected: [BasicApp::on_close])]
tray_exit: nwg::MenuItem,
#[nwg_control(icon: Some(&data.muted_icon))]
#[nwg_events( OnContextMenu: [BasicApp::on_menu] )]
tray: nwg::TrayNotification,
pub struct MagicTray {
pub notifdata: NOTIFYICONDATAW
}
impl BasicApp {
fn on_close(&self) {
nwg::stop_thread_dispatch();
}
fn on_show(&self) {
self.window.set_visible(true);
self.listbox.set_collection(vec!["potato".to_string(), "good".to_string()]);
impl MagicTray {
fn new() -> MagicTray {
unsafe {
let mut mt = MagicTray {
notifdata: NOTIFYICONDATAW {
cbSize: size_of::<NOTIFYICONDATAW>().try_into().unwrap(),
hWnd: HWND::default(),
uID: 1,
uFlags: NIF_ICON | NIF_MESSAGE,
uCallbackMessage: WM_TRAY_MENU,
hIcon: LoadIconW(GetModuleHandleW(None).unwrap(), w!("ICO_UNMUTE")).unwrap(),
..Default::default()
}
};
mt.init();
mt
}
}
fn init(&mut self) {
unsafe {
Shell_NotifyIconW(NIM_ADD, &self.notifdata).unwrap();
}
}
}
fn on_menu(&self) {
let (x, y) = nwg::GlobalCursor::position();
self.tray_menu.popup(x, y);
impl Default for MagicTray {
fn default() -> Self {
Self::new()
}
}
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()))
}
fn main() {
nwg::init().expect("Failed to init Native Windows GUI");
nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
let _app = BasicApp::build_ui(Default::default()).expect("Failed to build UI");
nwg::dispatch_thread_events();
MuterThread::com_init();
let mute_file: String = env::args().nth(1).unwrap_or("mute.txt".to_string());
let _mt = MuterThread::new(load_mute_txt(&mute_file), None);
}

View File

@@ -1,4 +1,3 @@
use std::{
collections::HashSet,
error::Error,
@@ -13,10 +12,12 @@ use crate::sm_session_notifier::SMSessionNotifierThread;
use crate::window_change::WindowChangeMonitor;
use windows::{
core::Interface,
Win32::Media::Audio::{IAudioSessionControl2, ISimpleAudioVolume},
Win32::{
Media::Audio::{IAudioSessionControl2, ISimpleAudioVolume},
System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
},
};
use crate::pid_to_exe::pid_to_exe_path;
enum MuterMessage {
WindowChange(String),
@@ -24,6 +25,10 @@ enum MuterMessage {
Exit(),
}
pub enum MuteChangeNotification {
MuteChanged(bool),
}
unsafe impl Send for MuterMessage {}
struct SessionMuter {
@@ -33,6 +38,7 @@ struct SessionMuter {
_session_notifier: SMSessionNotifierThread,
_win_change_mon: WindowChangeMonitor,
rx: Receiver<MuterMessage>,
notify_tx: Option<Sender<MuteChangeNotification>>,
}
impl SessionMuter {
@@ -40,6 +46,7 @@ impl SessionMuter {
mute_executables: HashSet<String>,
rx: Receiver<MuterMessage>,
tx: Sender<MuterMessage>,
notify_tx: Option<Sender<MuteChangeNotification>>,
) -> SessionMuter {
SessionMuter {
sessions: Vec::new(),
@@ -57,6 +64,7 @@ impl SessionMuter {
}))
},
rx,
notify_tx,
}
}
@@ -84,6 +92,8 @@ impl SessionMuter {
volume.SetMute(self.mute_flag, null_mut())?;
}
self.sessions.push(session);
} else {
println!("Skipping session from: {:?}", fn_str);
}
}
Ok(())
@@ -91,6 +101,9 @@ impl SessionMuter {
fn set_mute_all(self: &mut SessionMuter, mute: bool) {
unsafe {
self.notify_tx
.as_ref()
.and_then(|x| x.send(MuteChangeNotification::MuteChanged(mute)).ok());
let results = self
.sessions
.iter()
@@ -141,12 +154,18 @@ pub struct MuterThread {
}
impl MuterThread {
pub fn new(s: HashSet<String>) -> MuterThread {
pub fn com_init() {
unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).unwrap() };
}
pub fn new(
s: HashSet<String>,
notify_tx: Option<Sender<MuteChangeNotification>>,
) -> MuterThread {
let (sender, receiver) = mpsc::channel::<MuterMessage>();
MuterThread {
sender: sender.clone(),
handle: Some(thread::spawn(move || {
let mut muter = SessionMuter::new(s, receiver, sender);
let mut muter = SessionMuter::new(s, receiver, sender, notify_tx);
muter.run();
})),
}

View File

@@ -14,6 +14,8 @@ sm64.us.f3dex2e.exe
teardown.exe
underrail.exe
valheim.exe
Spotify.exe
Balatro.exe
Tactical Breach Wizards.exe
Tactical Breach Wizards.exe
eldenring.exe
ACValhalla.exe
DQMonsters3.exe