Skip to content

Commit

Permalink
Merge pull request #19 from nefares/lamrongol-lamrongol-feature-add-m…
Browse files Browse the repository at this point in the history
…inimized-condition

Lamrongol lamrongol feature add minimized condition
  • Loading branch information
nefares authored Nov 26, 2022
2 parents 8fd91c6 + 3bccdc2 commit 90a8178
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 12 deletions.
19 changes: 17 additions & 2 deletions WinBGMute/LoggingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
50 changes: 48 additions & 2 deletions WinBGMute/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 89 additions & 4 deletions WinBGMute/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 + ", ";
}
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -617,6 +684,7 @@ private void ReloadAudioButton_Click(object sender, EventArgs e)
{
m_volumeMixer.UnloadAudio(true);
m_volumeMixer.ReloadAudio(true);
ReloadMuter();

}

Expand Down Expand Up @@ -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)
{

}
}
}
3 changes: 0 additions & 3 deletions WinBGMute/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@
<metadata name="settingsBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>133, 17</value>
</metadata>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="TrayIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>306, 17</value>
</metadata>
Expand Down
14 changes: 13 additions & 1 deletion WinBGMute/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions WinBGMute/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<Setting Name="EnableAutostart" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="IsMuteConditionBackground" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
Loading

0 comments on commit 90a8178

Please sign in to comment.