using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovablePython;
using CoreAudioApi;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
namespace BluetoothMuter
{
///
/// The main form for the application.
///
public partial class MainForm : Form
{
private Hotkey muteHotkey;
private Hotkey volumeUpHotkey;
private Hotkey volumeDownHotkey;
private const float volumeChangeSize = 0.05f;
private bool muted = false;
private object hotKeyLock = new object();
///
/// Initializes a new instance of the class.
///
public MainForm()
{
InitializeComponent();
aboutMenuItem.Click += new EventHandler(aboutMenuItem_Click);
exitMenuItem.Click += new EventHandler(exitMenuItem_Click);
}
///
/// Handles the Click event of the exitMenuItem control.
///
/// The source of the event.
/// The instance containing the event data.
void exitMenuItem_Click(object sender, EventArgs e)
{
Close();
}
///
/// Handles the Click event of the aboutMenuItem control.
///
/// The source of the event.
/// The instance containing the event data.
void aboutMenuItem_Click(object sender, EventArgs e)
{
Opacity = 100;
Show();
}
///
/// Handles the Load event of the MainForm control.
///
/// The source of the event.
/// The instance containing the event data.
private void MainForm_Load(object sender, EventArgs e)
{
notifyIcon.Visible = true;
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
this.Text += String.Format(" - {0}", version);
bool registeredOk = false;
if (ConfigureMuteHotkey())
{
if (ConfigureVolumeUpHotkey())
{
if (ConfigureVolumeDownHotkey())
{
registeredOk = true;
}
}
}
if (!registeredOk)
{
Close();
}
}
///
/// Gets the default audio device.
///
private MMDevice GetDefaultAudioDevice()
{
MMDevice defaultDevice;
try
{
MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
}
catch (Exception ex)
{
MessageBox.Show("Failed to get default audio device. Please ensure you are running Windows 7.\n\nError: " + ex.Message, "Bluetooth Muter", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
return null;
}
return defaultDevice;
}
///
/// Configures the mute hotkey.
///
private bool ConfigureMuteHotkey()
{
muteHotkey = new Hotkey();
muteHotkey.KeyCode = Keys.NumPad0;
muteHotkey.Control = true;
muteHotkey.Alt = true;
muteHotkey.Pressed += delegate
{
if (Monitor.TryEnter(hotKeyLock))
{
try
{
// Get a new audio device each time to ensure sessions are re-enumerated.
var device = GetDefaultAudioDevice();
if (device != null)
{
muted = !muted;
var sessionCount = device.AudioSessionManager.Sessions.Count;
for (int i = 0; i < sessionCount; i++)
{
var session = device.AudioSessionManager.Sessions[i];
session.SimpleAudioVolume.Mute = muted;
}
}
}
finally
{
Monitor.Exit(hotKeyLock);
}
}
};
if (!muteHotkey.GetCanRegister(this))
{
MessageBox.Show("Failed to register Control+NumPad0 hotkey. Is another instance of BluetoothMuter.exe already running?", "Bluetooth Muter", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
muteHotkey.Register(this);
return true;
}
}
///
/// Configures the volume up hotkey.
///
private bool ConfigureVolumeUpHotkey()
{
volumeUpHotkey = new Hotkey();
volumeUpHotkey.KeyCode = Keys.Add;
volumeUpHotkey.Control = true;
volumeUpHotkey.Alt = true;
volumeUpHotkey.Pressed += delegate
{
if (Monitor.TryEnter(hotKeyLock))
{
try
{
// Get a new audio device each time to ensure sessions are re-enumerated.
var device = GetDefaultAudioDevice();
if (device != null)
{
float currentVolume = device.AudioEndpointVolume.MasterVolumeLevelScalar;
currentVolume += volumeChangeSize;
if (currentVolume > 1.0f)
{
currentVolume = 1.0f;
}
device.AudioEndpointVolume.MasterVolumeLevelScalar = currentVolume;
}
}
finally
{
Monitor.Exit(hotKeyLock);
}
}
};
if (!volumeUpHotkey.GetCanRegister(this))
{
MessageBox.Show("Failed to register Control+Alt++ hotkey. Is another instance of BluetoothMuter.exe already running?", "Bluetooth Muter", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
volumeUpHotkey.Register(this);
return true;
}
}
///
/// Configures the volume down hotkey.
///
private bool ConfigureVolumeDownHotkey()
{
volumeDownHotkey = new Hotkey();
volumeDownHotkey.KeyCode = Keys.Subtract;
volumeDownHotkey.Control = true;
volumeDownHotkey.Alt = true;
volumeDownHotkey.Pressed += delegate
{
if (Monitor.TryEnter(hotKeyLock))
{
try
{
// Get a new audio device each time to ensure sessions are re-enumerated.
var device = GetDefaultAudioDevice();
if (device != null)
{
float currentVolume = device.AudioEndpointVolume.MasterVolumeLevelScalar;
currentVolume -= volumeChangeSize;
if (currentVolume < 0.0f)
{
currentVolume = 0.0f;
}
device.AudioEndpointVolume.MasterVolumeLevelScalar = currentVolume;
}
}
finally
{
Monitor.Exit(hotKeyLock);
}
}
};
if (!volumeDownHotkey.GetCanRegister(this))
{
MessageBox.Show("Failed to register Control+Alt+- hotkey. Is another instance of BluetoothMuter.exe already running?", "Bluetooth Muter", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
volumeDownHotkey.Register(this);
return true;
}
}
///
/// Handles the FormClosing event of the MainForm control.
///
/// The source of the event.
/// The instance containing the event data.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (muteHotkey != null && muteHotkey.Registered)
{
muteHotkey.Unregister();
}
if (volumeUpHotkey != null && volumeUpHotkey.Registered)
{
volumeUpHotkey.Unregister();
}
if (volumeDownHotkey != null && volumeDownHotkey.Registered)
{
volumeDownHotkey.Unregister();
}
}
///
/// Handles the LinkClicked event of the websiteLink control.
///
/// The source of the event.
/// The instance containing the event data.
private void websiteLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(websiteLink.Text);
}
///
/// Handles the Click event of the btnHide control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnHide_Click(object sender, EventArgs e)
{
Hide();
}
///
/// Handles the Click event of the btnExit control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
}
}