Skip to content

Commit

Permalink
fixed asking for save bug, fixed dark mode window title bar
Browse files Browse the repository at this point in the history
  • Loading branch information
nefares committed Jun 20, 2022
1 parent c826e7a commit 57fbe11
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,5 @@ $RECYCLE.BIN/
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,windows
/build.cmd
/build_and_run.cmd
4 changes: 2 additions & 2 deletions WinBGMute/LoggingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static void DefaultLogLineFunction(object input, object? color = null, ob
{

}
}

}

public static void RestoreDefault()
{
Expand Down Expand Up @@ -89,6 +90,5 @@ public static void LogLine(object input, object? color = null, object? font = nu
m_logLineFunction(input, color, font);
}


}
}
81 changes: 76 additions & 5 deletions WinBGMute/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,55 @@

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace WinBGMuter
{
public partial class MainForm : Form
{
/// <summary>
/// used for dark mode
/// </summary>
public enum DWMWINDOWATTRIBUTE
{
DWMWA_NCRENDERING_ENABLED,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH,
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST
};

// used for dark mode
[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);

private VolumeMixer m_volumeMixer;
private ForegroundProcessManager m_processManager;

private string m_neverMuteList;
private bool m_settingsChanged = false;
private bool m_enableMiniStart = false;
private bool m_enableDemo = false;

// @todo untested whether this works
private static string m_previous_fname = "wininit";
Expand Down Expand Up @@ -208,8 +246,18 @@ private void EnableAutoStart(bool isEnabled)
}

}

/// <summary>
/// Recursively set dark mode for all underlaying controls by storing the original colors in the control's tags
/// </summary>
/// <param name="parent"></param>
/// <param name="dark"></param>
private void SetDark(Control parent, bool dark)
{
// only works on main window
int USE_DARK_MODE = dark ? 1 : 0;
DwmSetWindowAttribute(parent.Handle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, ref USE_DARK_MODE, sizeof(int));

Color bgcolor;
Color fgcolor;

Expand Down Expand Up @@ -255,6 +303,15 @@ private void SetDark(Control parent, bool dark)

if (c.Controls.Count > 0)
SetDark(c, dark);

parent.Refresh();

// if main window, force redraw as refresh does not work
if (parent.Parent == null)
{
parent.Hide();
parent.Show();
}
}
}

Expand All @@ -269,6 +326,9 @@ public MainForm(string[] args)
case "--startMinimized":
m_enableMiniStart = true;
break;
case "--demo":
m_enableDemo = true;
break;
default:
MessageBox.Show($"Unknown argument {arg}");
break;
Expand All @@ -285,10 +345,16 @@ public MainForm(string[] args)

private void OnProcessExit(object sender, EventArgs e)
{
m_processManager.CleanUp();
foreach (var pid in m_volumeMixer.GetPIDs())
try
{
m_volumeMixer.SetApplicationMute(pid, false);
m_processManager.CleanUp();
foreach (var pid in m_volumeMixer.GetPIDs())
{
m_volumeMixer.SetApplicationMute(pid, false);
}
}
catch (Exception ex) {
MessageBox.Show($"Cleanup failed: {ex.Message}", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

Expand Down Expand Up @@ -330,6 +396,11 @@ private void MainForm_Load(object sender, EventArgs e)

SaveChangesButton.Enabled = false;

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);

this.Text += " - v" + fvi.ProductVersion;

if (m_enableMiniStart)
{
this.WindowState = FormWindowState.Minimized;
Expand Down Expand Up @@ -384,9 +455,9 @@ private void NeverMuteTextBox_TextChanged(object sender, EventArgs e)
private void SaveChangesButton_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();

m_settingsChanged = false;
this.SaveChangesButton.Enabled = false;

}

private void MainForm_Resize(object sender, EventArgs e)
Expand Down
20 changes: 19 additions & 1 deletion WinBGMute/ShortcutManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
using System;
/*
* Background Muter - Automatically mute background applications
* Copyright (C) 2022 Nefares ([email protected]) github.com/nefares
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
Expand Down
81 changes: 45 additions & 36 deletions WinBGMute/VolumeMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,59 +125,68 @@ public int[] GetPIDs()
}
public void ReloadAudio(bool reloadDevice = false)
{
if (reloadDevice)
try
{
AudioDevice.volumeSessionList = new Dictionary<int, ISimpleAudioVolume?>();

AudioDevice.deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
//not efficient
AudioDevice.deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out AudioDevice.speakers);


// activate the session manager. we need the enumerator
AudioDevice.IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
if (reloadDevice)
{
AudioDevice.volumeSessionList = new Dictionary<int, ISimpleAudioVolume?>();

AudioDevice.speakers.Activate(ref AudioDevice.IID_IAudioSessionManager2, 0, IntPtr.Zero, out AudioDevice.o);
AudioDevice.mgr = (IAudioSessionManager2)AudioDevice.o;
AudioDevice.deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
//not efficient
AudioDevice.deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out AudioDevice.speakers);


// activate the session manager. we need the enumerator
AudioDevice.IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;

}
AudioDevice.speakers.Activate(ref AudioDevice.IID_IAudioSessionManager2, 0, IntPtr.Zero, out AudioDevice.o);
AudioDevice.mgr = (IAudioSessionManager2)AudioDevice.o;
}

//code takes too long
AudioDevice.mgr.GetSessionEnumerator(out AudioDevice.sessionEnumerator);
AudioDevice.sessionEnumerator.GetCount(out AudioDevice.count);
//code takes too long
AudioDevice.mgr.GetSessionEnumerator(out AudioDevice.sessionEnumerator);
AudioDevice.sessionEnumerator.GetCount(out AudioDevice.count);



AudioDevice.volumeSessionList.Clear();
ISimpleAudioVolume volumeControl = null;
for (int i = 0; i < AudioDevice.count; i++)
AudioDevice.volumeSessionList.Clear();
ISimpleAudioVolume volumeControl = null;
for (int i = 0; i < AudioDevice.count; i++)
{
IAudioSessionControl2 ctl;
AudioDevice.sessionEnumerator.GetSession(i, out ctl);
int cpid;
ctl.GetProcessId(out cpid);

volumeControl = ctl as ISimpleAudioVolume;

if (AudioDevice.volumeSessionList.ContainsKey(cpid))
{
AudioDevice.volumeSessionList[cpid] = volumeControl;

/*
LoggingEngine.LogLine($"[!] PID {cpid} Exists ");
AudioDevice.volumeSessionList.Remove(cpid);
*/
}
else
{
AudioDevice.volumeSessionList.Add(cpid, volumeControl);

}
}
} catch (Exception ex)
{
IAudioSessionControl2 ctl;
AudioDevice.sessionEnumerator.GetSession(i, out ctl);
int cpid;
ctl.GetProcessId(out cpid);

volumeControl = ctl as ISimpleAudioVolume;

if (AudioDevice.volumeSessionList.ContainsKey(cpid))
if (MessageBox.Show($"Audio Initiatlization failed: {ex.Message}","Error",MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
{
AudioDevice.volumeSessionList[cpid] = volumeControl;

/*
LoggingEngine.LogLine($"[!] PID {cpid} Exists ");
AudioDevice.volumeSessionList.Remove(cpid);
*/
ReloadAudio(reloadDevice);
}
else
{
AudioDevice.volumeSessionList.Add(cpid, volumeControl);

System.Windows.Forms.Application.Exit();
}
}


}
private ISimpleAudioVolume GetVolumeObject(int pid)
{
Expand Down

0 comments on commit 57fbe11

Please sign in to comment.