diff --git a/WinBGMute/LoggingEngine.cs b/WinBGMute/LoggingEngine.cs index 1450f31..2aebab9 100644 --- a/WinBGMute/LoggingEngine.cs +++ b/WinBGMute/LoggingEngine.cs @@ -28,8 +28,11 @@ internal class LoggingEngine public static _LogFunction m_logFunction = DefaultLogFunction; public static _LogLineFunction m_logLineFunction = DefaultLogLineFunction; + public enum LOG_LEVEL_TYPE {LOG_NONE, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG }; public static bool Enabled { get; set; } + public static LOG_LEVEL_TYPE LogLevel { get; set; } + [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AllocConsole(); @@ -67,6 +70,7 @@ public static void RestoreDefault() AllocConsole(); m_logFunction = DefaultLogFunction; m_logLineFunction = DefaultLogLineFunction; + LogLevel = LOG_LEVEL_TYPE.LOG_DEBUG; } public static void SetEngine(_LogFunction logfn, _LogLineFunction loglinefn) @@ -76,17 +80,28 @@ public static void SetEngine(_LogFunction logfn, _LogLineFunction loglinefn) m_logLineFunction = loglinefn; } - public static void Log(object input, object? color = null, object? font = null) + public static void Log(object input, object? color = null, object? font = null, LOG_LEVEL_TYPE loglevel = LOG_LEVEL_TYPE.LOG_DEBUG) { if (!Enabled) return; + + //todo: add a separate function which does this instead of duplicating it + if ((loglevel > LogLevel) || (LogLevel == LOG_LEVEL_TYPE.LOG_NONE)) + { + return; + } m_logFunction(input, color, font); } - public static void LogLine(object input, object? color = null, object? font = null) + public static void LogLine(object input, object? color = null, object? font = null, LOG_LEVEL_TYPE loglevel=LOG_LEVEL_TYPE.LOG_DEBUG) { if (!Enabled) return; + + if ((loglevel > LogLevel) || (LogLevel == LOG_LEVEL_TYPE.LOG_NONE)) + { + return; + } m_logLineFunction(input, color, font); } diff --git a/WinBGMute/MainForm.Designer.cs b/WinBGMute/MainForm.Designer.cs index c65719d..080f087 100644 --- a/WinBGMute/MainForm.Designer.cs +++ b/WinBGMute/MainForm.Designer.cs @@ -74,6 +74,9 @@ private void InitializeComponent() this.LoggerCheckbox = new System.Windows.Forms.CheckBox(); this.DarkModeCheckbox = new System.Windows.Forms.CheckBox(); this.AutostartCheckbox = new System.Windows.Forms.CheckBox(); + this.MuteConditionGroupBox = new System.Windows.Forms.GroupBox(); + this.MinimizedRadioButton = new System.Windows.Forms.RadioButton(); + this.BackGroundRadioButton = new System.Windows.Forms.RadioButton(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.LastLogLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.StatusBox = new System.Windows.Forms.ToolStripStatusLabel(); @@ -95,6 +98,7 @@ private void InitializeComponent() this.tableLayoutPanel2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.tableLayoutPanel5.SuspendLayout(); + this.MuteConditionGroupBox.SuspendLayout(); this.statusStrip1.SuspendLayout(); this.TrayContextMenu.SuspendLayout(); this.SuspendLayout(); @@ -355,6 +359,7 @@ private void InitializeComponent() this.tableLayoutPanel5.Controls.Add(this.LoggerCheckbox, 0, 1); this.tableLayoutPanel5.Controls.Add(this.DarkModeCheckbox, 0, 0); this.tableLayoutPanel5.Controls.Add(this.AutostartCheckbox, 0, 3); + this.tableLayoutPanel5.Controls.Add(this.MuteConditionGroupBox, 0, 4); this.tableLayoutPanel5.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel5.Location = new System.Drawing.Point(3, 19); this.tableLayoutPanel5.Name = "tableLayoutPanel5"; @@ -371,7 +376,7 @@ private void InitializeComponent() // this.ConsoleLogging.AutoSize = true; this.ConsoleLogging.Dock = System.Windows.Forms.DockStyle.Fill; - this.ConsoleLogging.Location = new System.Drawing.Point(3, 53); + this.ConsoleLogging.Location = new System.Drawing.Point(3, 28); this.ConsoleLogging.Name = "ConsoleLogging"; this.ConsoleLogging.Size = new System.Drawing.Size(119, 19); this.ConsoleLogging.TabIndex = 14; @@ -383,7 +388,7 @@ private void InitializeComponent() // this.LoggerCheckbox.AutoSize = true; this.LoggerCheckbox.Dock = System.Windows.Forms.DockStyle.Fill; - this.LoggerCheckbox.Location = new System.Drawing.Point(3, 28); + this.LoggerCheckbox.Location = new System.Drawing.Point(3, 53); this.LoggerCheckbox.Name = "LoggerCheckbox"; this.LoggerCheckbox.Size = new System.Drawing.Size(119, 19); this.LoggerCheckbox.TabIndex = 13; @@ -414,6 +419,42 @@ private void InitializeComponent() this.AutostartCheckbox.UseVisualStyleBackColor = true; this.AutostartCheckbox.CheckedChanged += new System.EventHandler(this.AutostartCheckbox_CheckedChanged); // + // MuteConditionGroupBox + // + this.MuteConditionGroupBox.Controls.Add(this.MinimizedRadioButton); + this.MuteConditionGroupBox.Controls.Add(this.BackGroundRadioButton); + this.MuteConditionGroupBox.Location = new System.Drawing.Point(3, 104); + this.MuteConditionGroupBox.Name = "MuteConditionGroupBox"; + this.MuteConditionGroupBox.Size = new System.Drawing.Size(113, 62); + this.MuteConditionGroupBox.TabIndex = 16; + this.MuteConditionGroupBox.TabStop = false; + this.MuteConditionGroupBox.Text = "MuteCondition"; + this.MuteConditionGroupBox.Enter += new System.EventHandler(this.MuteConditionGroupBox_Enter); + // + // MinimizedRadioButton + // + this.MinimizedRadioButton.AutoSize = true; + this.MinimizedRadioButton.Location = new System.Drawing.Point(10, 36); + this.MinimizedRadioButton.Name = "MinimizedRadioButton"; + this.MinimizedRadioButton.Size = new System.Drawing.Size(81, 19); + this.MinimizedRadioButton.TabIndex = 1; + this.MinimizedRadioButton.TabStop = true; + this.MinimizedRadioButton.Text = "Minimized"; + this.MinimizedRadioButton.UseVisualStyleBackColor = true; + this.MinimizedRadioButton.CheckedChanged += new System.EventHandler(this.MinimizedRadioButton_CheckedChanged); + // + // BackGroundRadioButton + // + this.BackGroundRadioButton.AutoSize = true; + this.BackGroundRadioButton.Location = new System.Drawing.Point(10, 16); + this.BackGroundRadioButton.Name = "BackGroundRadioButton"; + this.BackGroundRadioButton.Size = new System.Drawing.Size(90, 19); + this.BackGroundRadioButton.TabIndex = 0; + this.BackGroundRadioButton.TabStop = true; + this.BackGroundRadioButton.Text = "BackGround"; + this.BackGroundRadioButton.UseVisualStyleBackColor = true; + this.BackGroundRadioButton.CheckedChanged += new System.EventHandler(this.BackGroundRadioButton_CheckedChanged); + // // statusStrip1 // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -515,6 +556,8 @@ private void InitializeComponent() this.groupBox3.ResumeLayout(false); this.tableLayoutPanel5.ResumeLayout(false); this.tableLayoutPanel5.PerformLayout(); + this.MuteConditionGroupBox.ResumeLayout(false); + this.MuteConditionGroupBox.PerformLayout(); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); this.TrayContextMenu.ResumeLayout(false); @@ -559,5 +602,8 @@ private void InitializeComponent() private CheckBox ConsoleLogging; private CheckBox LoggerCheckbox; private CheckBox AutostartCheckbox; + private GroupBox MuteConditionGroupBox; + private RadioButton MinimizedRadioButton; + private RadioButton BackGroundRadioButton; } } \ No newline at end of file diff --git a/WinBGMute/MainForm.cs b/WinBGMute/MainForm.cs index c7051bf..09836fb 100644 --- a/WinBGMute/MainForm.cs +++ b/WinBGMute/MainForm.cs @@ -69,9 +69,12 @@ public enum DWMWINDOWATTRIBUTE private bool m_enableMiniStart = false; private bool m_enableDemo = false; private int m_errorCount = 0; + private bool m_isMuteConditionBackground = true; // @todo untested whether this works private static string m_previous_fname = "wininit"; + private static int m_previous_fpid = -1; + private void InternalLog(object olog, object? ocolor = null, object? ofont = null) { @@ -115,7 +118,7 @@ private void InternalLogLine(object olog, object? ocolor = null, object? ofont = InternalLog(log + Environment.NewLine, color, font); } - private void HandleError(Exception ex, object? data=null) + private void HandleError(Exception ex, object? data = null) { m_errorCount += 1; if (ex.InnerException is InvalidOperationException) @@ -134,6 +137,10 @@ private void HandleError(Exception ex, object? data=null) } } + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool IsIconic(IntPtr hWnd); // stores previous foreground process name for fallback in case of error private void RunMuter(int fpid, bool doMute = true) { @@ -193,6 +200,7 @@ private void RunMuter(int fpid, bool doMute = true) Process fproc = Process.GetProcessById(fpid); fname = fproc.ProcessName; m_previous_fname = fname; + m_previous_fpid = fpid; } catch(Exception ex) { @@ -241,8 +249,44 @@ private void RunMuter(int fpid, bool doMute = true) } else { - m_volumeMixer.SetApplicationMute(audio_pid, true); - InlineMuteProcList(audio_proc_list, true); + // if not on mute list and + if (!m_isMuteConditionBackground) + { + //lamrongol + bool force_mute; + + // if minimize option AND iconic + // TODO: fix multi window muting + IntPtr handle = Process.GetProcessById(audio_pid).MainWindowHandle;//Error occurs for "Handle", not "MainWindowHandle" + if (!IsIconic(handle)) + { + // if minimize option AND NOT minimized: SKIP + force_mute = false; + log_skipped += "[M]" + audio_pname + ", "; + + } + else + { + // if minimize option and minimzed, do mute + force_mute = true; + } + + //if mute condition is minimized and not on mute list + m_volumeMixer.SetApplicationMute(audio_pid, force_mute); + InlineMuteProcList(audio_proc_list, force_mute); + + + } + else + { + //if mute condition is background and not on mute list + m_volumeMixer.SetApplicationMute(audio_pid, true); + InlineMuteProcList(audio_proc_list, true); + } + + + + log_muted += audio_pname + ", "; } } @@ -251,6 +295,16 @@ private void RunMuter(int fpid, bool doMute = true) LoggingEngine.LogLine($"[+] Summary: skipped ({log_skipped}) and muted ({log_muted})"); } + + private void ReloadMuter() + { + LoggingEngine.Log("[R]",Color.Aqua, null,LoggingEngine.LOG_LEVEL_TYPE.LOG_DEBUG); + LoggingEngine.LOG_LEVEL_TYPE currentLogLevel = LoggingEngine.LogLevel; + LoggingEngine.LogLevel = LoggingEngine.LOG_LEVEL_TYPE.LOG_NONE; + RunMuter(Environment.ProcessId); + LoggingEngine.LogLevel = currentLogLevel; + + } private void MuterCallback(object state) { var result = m_processManager.GetJobThreadSafe(); @@ -416,6 +470,18 @@ private void ReloadSettings(object sender, EventArgs e) DarkModeCheckbox.Checked = Properties.Settings.Default.EnableDarkMode; AutostartCheckbox.Checked = Properties.Settings.Default.EnableAutostart; + if (Properties.Settings.Default.IsMuteConditionBackground == true) + { + BackGroundRadioButton.Checked = true; + m_isMuteConditionBackground = true; + } + + else + { + MinimizedRadioButton.Checked = true; + m_isMuteConditionBackground = false; + + } LoggerCheckbox_CheckedChanged(sender, EventArgs.Empty); ConsoleLogging_CheckedChanged(sender, EventArgs.Empty); @@ -427,6 +493,7 @@ private void ReloadSettings(object sender, EventArgs e) private void MainForm_Load(object sender, EventArgs e) { + LoggingEngine.LogLevel = LoggingEngine.LOG_LEVEL_TYPE.LOG_DEBUG; LoggingEngine.LogLine("Initializing..."); m_volumeMixer = new VolumeMixer(); @@ -505,7 +572,7 @@ private void NeverMuteTextBox_TextChanged(object sender, EventArgs e) PopulateNeverMuteListBox(); m_neverMuteList = NeverMuteTextBox.Text; Properties.Settings.Default.NeverMuteProcs = m_neverMuteList; - RunMuter(-1, false); + ReloadMuter(); } private void SaveChangesButton_Click(object sender, EventArgs e) @@ -617,6 +684,7 @@ private void ReloadAudioButton_Click(object sender, EventArgs e) { m_volumeMixer.UnloadAudio(true); m_volumeMixer.ReloadAudio(true); + ReloadMuter(); } @@ -701,6 +769,23 @@ private void tableLayoutPanel3_Paint(object sender, PaintEventArgs e) } + private void BackGroundRadioButton_CheckedChanged(object sender, EventArgs e) + { + Properties.Settings.Default.IsMuteConditionBackground = true; + m_isMuteConditionBackground = true; + ReloadMuter(); + } + private void MinimizedRadioButton_CheckedChanged(object sender, EventArgs e) + { + Properties.Settings.Default.IsMuteConditionBackground = false; + m_isMuteConditionBackground = false; + ReloadMuter(); + } + + private void MuteConditionGroupBox_Enter(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/WinBGMute/MainForm.resx b/WinBGMute/MainForm.resx index 06f51eb..4785dd0 100644 --- a/WinBGMute/MainForm.resx +++ b/WinBGMute/MainForm.resx @@ -66,9 +66,6 @@ 133, 17 - - 17, 17 - 306, 17 diff --git a/WinBGMute/Properties/Settings.Designer.cs b/WinBGMute/Properties/Settings.Designer.cs index ea508c9..5fa5f3e 100644 --- a/WinBGMute/Properties/Settings.Designer.cs +++ b/WinBGMute/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace WinBGMuter.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.0.3.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")] public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -82,5 +82,17 @@ public bool EnableAutostart { this["EnableAutostart"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool IsMuteConditionBackground { + get { + return ((bool)(this["IsMuteConditionBackground"])); + } + set { + this["IsMuteConditionBackground"] = value; + } + } } } diff --git a/WinBGMute/Properties/Settings.settings b/WinBGMute/Properties/Settings.settings index 7bb6f78..5b72498 100644 --- a/WinBGMute/Properties/Settings.settings +++ b/WinBGMute/Properties/Settings.settings @@ -17,5 +17,8 @@ True + + True + \ No newline at end of file diff --git a/WinBGMute/Settings.cs b/WinBGMute/Settings.cs new file mode 100644 index 0000000..c4cca12 --- /dev/null +++ b/WinBGMute/Settings.cs @@ -0,0 +1,28 @@ +namespace WinBGMuter.Properties { + + + // This class allows you to handle specific events on the settings class: + // The SettingChanging event is raised before a setting's value is changed. + // The PropertyChanged event is raised after a setting's value is changed. + // The SettingsLoaded event is raised after the setting values are loaded. + // The SettingsSaving event is raised before the setting values are saved. + public sealed partial class Settings { + + public Settings() { + // // To add event handlers for saving and changing settings, uncomment the lines below: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // Add code to handle the SettingChangingEvent event here. + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // Add code to handle the SettingsSaving event here. + } + } +}