Skip to content

Commit

Permalink
Merge pull request #1 from sirWest/master
Browse files Browse the repository at this point in the history
MERGE
  • Loading branch information
ExcuseMi authored Jun 14, 2017
2 parents 02657f4 + b5ad8ea commit ce020ea
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 176 deletions.
2 changes: 1 addition & 1 deletion AudioSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<Compile Include="Classes\EndPoints.cs" />
<Compile Include="Classes\OSDskin.cs" />
<Compile Include="Classes\ScrollVolume.cs" />
<Compile Include="Classes\StartupDeviceTask,.cs" />
<Compile Include="Classes\StartupDeviceTask.cs" />
<Compile Include="Classes\Win32.cs" />
<Compile Include="Classes\GlobalHotkeys.cs" />
<Compile Include="Classes\Hotkey.cs" />
Expand Down
2 changes: 1 addition & 1 deletion AudioSwitchSetupAndDesign/AudioSwitchSetup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Name: "startupicon"; Description: "Start AudioSwitch when I log in to Windows";
[Icons]
Name: "{group}\AudioSwitch"; Filename: "{app}\AudioSwitch.exe"
Name: "{group}\Uninstall"; Filename: "{uninstallexe}"
Name: "{userstartup}\AudioSwitch"; Filename: "{app}\AudioSwitch.exe"; Tasks: startupicon
Name: "{userstartup}\AudioSwitch"; Filename: "{app}\AudioSwitch.exe"; Tasks: startupicon; Parameters: "/startup"

[Run]
Filename: "{app}\AudioSwitch.exe"; Description: "Run AudioSwitch"; Flags: runasoriginaluser postinstall nowait skipifsilent
Expand Down
6 changes: 0 additions & 6 deletions Classes/Device.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using AudioSwitch.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AudioSwitch.Classes.Settings;

namespace AudioSwitch.Classes
{
Expand Down
53 changes: 16 additions & 37 deletions Classes/DeviceRepository.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
using AudioSwitch.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AudioSwitch.Classes
{
class DeviceRepository
static class DeviceRepository
{

static DeviceRepository()
{
DeviceEnumerator = new MMDeviceEnumerator();
//NotifyClient = new MMDeviceNotifyClient();
//DeviceEnumerator.RegisterEndpointNotificationCallback(NotifyClient);
}
internal static readonly MMDeviceEnumerator DeviceEnumerator;

internal static List<Device> FindAll()
private static readonly MMDeviceEnumerator DeviceEnumerator;

internal static IEnumerable<Device> FindAll()
{
List<Device> deviceList = new List<Device>();
deviceList.AddRange(createDeviceList(EDataFlow.eCapture));
deviceList.AddRange(createDeviceList(EDataFlow.eRender));
var deviceList = new List<Device>();
deviceList.AddRange(CreateDeviceList(EDataFlow.eCapture));
deviceList.AddRange(CreateDeviceList(EDataFlow.eRender));
return deviceList;
}

internal static List<Device> FindCaptureDevices()
{
List<Device> deviceList = new List<Device>();
deviceList.AddRange(createDeviceList(EDataFlow.eCapture));
var deviceList = new List<Device>();
deviceList.AddRange(CreateDeviceList(EDataFlow.eCapture));
return deviceList;
}

internal static List<Device> FindPlayBackDevices()
{
List<Device> deviceList = new List<Device>();
deviceList.AddRange(createDeviceList(EDataFlow.eRender));
var deviceList = new List<Device>();
deviceList.AddRange(CreateDeviceList(EDataFlow.eRender));
return deviceList;
}
private static List<Device> createDeviceList(EDataFlow dataFlow)
private static IEnumerable<Device> CreateDeviceList(EDataFlow dataFlow)
{
var devices = DeviceEnumerator.EnumerateAudioEndPoints(dataFlow, EDeviceState.Active);
var count = devices.Count;
List<Device> deviceList = new List<Device>();
var deviceList = new List<Device>();
for (var i = 0; i < count; i++)
{
var device = createDevice(devices[i], dataFlow);
var device = CreateDevice(devices[i], dataFlow);
if (device != null)
{
deviceList.Add(device);
Expand All @@ -55,27 +49,12 @@ private static List<Device> createDeviceList(EDataFlow dataFlow)
return deviceList;
}

private static Device createDevice(MMDevice mmDevice, EDataFlow dataFlow)
private static Device CreateDevice(MMDevice mmDevice, EDataFlow dataFlow)
{
var devId = (string)mmDevice.ID;
var devId = mmDevice.ID;
var devSettings = Program.settings.Device.Find(x => x.DeviceID == devId);
if (devSettings == null || !devSettings.HideFromList)
{
if (devSettings == null)
{
devSettings = new Settings.CDevice
{
DeviceID = devId,
HideFromList = false,
Brightness = 0,
Hue = 0,
Saturation = 0,
UseCustomName = false,
CustomName = "",
DefaultMultimediaDevice = false,
DefaultCommunicationsDevice = false
};
}
var device = new Device
{
MMDevice = mmDevice,
Expand Down
28 changes: 14 additions & 14 deletions Classes/Hotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,50 @@ public sealed class Hotkey : IDisposable
[XmlIgnore]
public string Key
{
get { return HotKey.ToString(); }
set { HotKey = (Keys) Enum.Parse(typeof (Keys), value); }
get => HotKey.ToString();
set => HotKey = (Keys) Enum.Parse(typeof (Keys), value);
}

[XmlIgnore]
public string HKFunction
{
get { return Function.ToString(); }
set { Function = (HotkeyFunction)Enum.Parse(typeof(HotkeyFunction), value); }
get => Function.ToString();
set => Function = (HotkeyFunction)Enum.Parse(typeof(HotkeyFunction), value);
}

[XmlIgnore]
public bool Control
{
get { return ModifierKeys.HasFlag(HotModifierKeys.Control); }
set { ModHotFlag(value, HotModifierKeys.Control); }
get => ModifierKeys.HasFlag(HotModifierKeys.Control);
set => ModHotFlag(value, HotModifierKeys.Control);
}

[XmlIgnore]
public bool Alt
{
get { return ModifierKeys.HasFlag(HotModifierKeys.Alt); }
set { ModHotFlag(value, HotModifierKeys.Alt); }
get => ModifierKeys.HasFlag(HotModifierKeys.Alt);
set => ModHotFlag(value, HotModifierKeys.Alt);
}

[XmlIgnore]
public bool Shift
{
get { return ModifierKeys.HasFlag(HotModifierKeys.Shift); }
set { ModHotFlag(value, HotModifierKeys.Shift); }
get => ModifierKeys.HasFlag(HotModifierKeys.Shift);
set => ModHotFlag(value, HotModifierKeys.Shift);
}

[XmlIgnore]
public bool LWin
{
get { return ModifierKeys.HasFlag(HotModifierKeys.LWin); }
set { ModHotFlag(value, HotModifierKeys.LWin); }
get => ModifierKeys.HasFlag(HotModifierKeys.LWin);
set => ModHotFlag(value, HotModifierKeys.LWin);
}

[XmlIgnore]
public bool RWin
{
get { return ModifierKeys.HasFlag(HotModifierKeys.RWin); }
set { ModHotFlag(value, HotModifierKeys.RWin); }
get => ModifierKeys.HasFlag(HotModifierKeys.RWin);
set => ModHotFlag(value, HotModifierKeys.RWin);
}

public void Dispose()
Expand Down
7 changes: 4 additions & 3 deletions Classes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void Main(string[] args)
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
settings = Settings.Load();

if (args.Length > 0)
if (args.Length > 0 && args[0] != "/startup")
{
if (!AttachConsole(-1))
AllocConsole();
Expand Down Expand Up @@ -85,6 +85,7 @@ private static void Main(string[] args)
Console.WriteLine(" /k - set a key for a new hotkey. Case sensitive.");
Console.WriteLine(" /f - choose a function for the new hotkey. Case sensitive.");
Console.WriteLine();
Console.WriteLine(" /startup - Runs after user logs in and sets any preferred default devices that may have been set.");
Console.WriteLine(" /m, /k, /f (blank) - shows all possible values for the parameter.");
break;

Expand Down Expand Up @@ -214,9 +215,9 @@ private static void Main(string[] args)
frmOSD = new FormOSD();
var formSwitcher = new FormSwitcher();

if (args.Length > 0 && args[0] == "/startup")
new StartupDeviceTask().Run();

StartupDeviceTask startupDeviceTask = new StartupDeviceTask();
startupDeviceTask.Run();
Application.Run();

mutex.Close();
Expand Down
46 changes: 16 additions & 30 deletions Classes/StartupDeviceTask,.cs → Classes/StartupDeviceTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using static AudioSwitch.Classes.Settings;

namespace AudioSwitch.Classes
{
Expand All @@ -15,7 +11,7 @@ class StartupDeviceTask
public void Run()
{
var devices = DeviceRepository.FindAll();
List<DeviceChanges> devicesSet = new List<DeviceChanges>();
var devicesSet = new List<DeviceChanges>();
foreach (var dev in devices)
{
var devID = dev.MMDevice.ID;
Expand All @@ -42,41 +38,38 @@ public void Run()
if (devicesSet.Count > 0) {
ShowBalloonTip(devicesSet);
}

}

private DeviceChanges switchDevice(CDevice devSettings, Device dev, CoreAudioApi.ERole role)
private DeviceChanges switchDevice(Settings.CDevice devSettings, Device dev, ERole role)
{
var defaultDevice = EndPoints.GetDefaultMMDevice(dev.DataFlow, role);
if (defaultDevice == null || defaultDevice.ID == null || !defaultDevice.ID.Equals(devSettings.DeviceID))
if (defaultDevice?.ID == null || !defaultDevice.ID.Equals(devSettings.DeviceID))
{
EndPoints.SetDefaultDevice(devSettings.DeviceID, role);
string deviceName = devSettings.UseCustomName && devSettings.CustomName != null && devSettings.CustomName != "" ? devSettings.CustomName : dev.MMDevice.FriendlyName;
string deviceName = devSettings.UseCustomName && !string.IsNullOrEmpty(devSettings.CustomName) ? devSettings.CustomName : dev.MMDevice.FriendlyName;
return new DeviceChanges { deviceName = deviceName, role = role, dataflow = dev.DataFlow };
}
return null;
}

private void ShowBalloonTip(List<DeviceChanges> deviceNames)
private void ShowBalloonTip(IEnumerable<DeviceChanges> deviceNames)
{
var attributes = typeof(Program).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute));
var assemblyTitleAttribute = attributes.SingleOrDefault() as AssemblyTitleAttribute;
var text = String.Join("\n", deviceNames.Select(n => "Switching to " +
var text = string.Join("\n", deviceNames.Select(n => "Switching to " +
(EDataFlow.eRender.Equals(n.dataflow) ? "Multimedia" : "Recording")
+ " \"" + n.deviceName + "\" as default " +
(ERole.eMultimedia.Equals(n.role) ? "multimedia" : "communications") + " device."));
var notification = new System.Windows.Forms.NotifyIcon()
(ERole.eMultimedia.Equals(n.role) ? "multimedia" : "communications") + " device"));
using (var notification = new System.Windows.Forms.NotifyIcon
{
Visible = true,
Icon = System.Drawing.SystemIcons.Information,
BalloonTipTitle = assemblyTitleAttribute.Title,
BalloonTipText = text,
BalloonTipTitle = typeof(Program).Assembly.GetName().Name,
BalloonTipText = text

};
notification.BalloonTipClicked += new EventHandler(TrayNotifyIcon_BalloonClick);
notification.ShowBalloonTip(2);
DisposeBalloon(notification).ContinueWith(t => Console.WriteLine(t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
})
{
notification.BalloonTipClicked += TrayNotifyIcon_BalloonClick;
notification.ShowBalloonTip(2);
}
}

private void TrayNotifyIcon_BalloonClick(object sender, EventArgs e)
Expand All @@ -85,14 +78,7 @@ private void TrayNotifyIcon_BalloonClick(object sender, EventArgs e)
cfg.ShowDialog();
}


private async Task DisposeBalloon(System.Windows.Forms.NotifyIcon notification)
{
System.Threading.Thread.Sleep(2000);
notification.Dispose();
}

internal class DeviceChanges
private class DeviceChanges
{
internal string deviceName { get; set; }
internal ERole role { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Controls/Devices.Designer.cs

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

21 changes: 8 additions & 13 deletions Controls/Devices.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using AudioSwitch.Classes;
using AudioSwitch.CoreAudioApi;
using System;
using System.Collections.Generic;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AudioSwitch.Classes;
using AudioSwitch.CoreAudioApi;
using AudioSwitch.Forms;

namespace AudioSwitch.Forms
namespace AudioSwitch.Controls
{
public partial class Devices : UserControl
{
Expand Down Expand Up @@ -38,8 +38,7 @@ public void PostConstructor(EDataFlow dataFlow)
cnt++;
}
}



private void Devices_Load(object sender, EventArgs e)
{
pictureModded.Image = new Bitmap(Properties.Resources._66_100_highDPI);
Expand All @@ -62,8 +61,6 @@ private void Devices_Load(object sender, EventArgs e)
ImageSize = size,
ColorDepth = ColorDepth.Depth32Bit
};


}

private void buttonRemove_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -94,7 +91,7 @@ private void buttonSave_Click(object sender, EventArgs e)

listDevices.SelectedItems[0].Font = new Font(listDevices.SelectedItems[0].Font,
checkHideDevice.Checked ? FontStyle.Italic : FontStyle.Bold);
IEnumerable<ListViewItem> lv = listDevices.Items.Cast<ListViewItem>();
var lv = listDevices.Items.Cast<ListViewItem>().ToArray();

if (checkEnableAsMultimediaOnStartup.Checked)
{
Expand Down Expand Up @@ -148,8 +145,7 @@ private void buttonSave_Click(object sender, EventArgs e)
}

}



private void listDevices_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (listDevices.SelectedItems.Count == 0) return;
Expand Down Expand Up @@ -188,7 +184,6 @@ private void listDevices_SelectedIndexChanged_1(object sender, EventArgs e)
private void checkCustomName_CheckedChanged_1(object sender, EventArgs e)
{
textCustomName.Enabled = checkCustomName.Checked;

}

private void trackBarsHSB_Scroll()
Expand Down
Loading

0 comments on commit ce020ea

Please sign in to comment.