private DeviceChanges switchDevice(Settings.CDevice devSettings, Device dev, ERole role) { var defaultDevice = EndPoints.GetDefaultMMDevice(dev.DataFlow, role); if (defaultDevice?.ID == null || !defaultDevice.ID.Equals(devSettings.DeviceID)) { EndPoints.SetDefaultDevice(devSettings.DeviceID, role); 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 static void Main(string[] args) { try { AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; settings = Settings.Load(); if (args.Length > 0) { if (!AttachConsole(-1)) { AllocConsole(); } isConsole = true; Console.WriteLine(); var hotkeyFunction = HotkeyFunction.SwitchPlaybackDevice; var modifiers = HotModifierKeys.LWin; var hotKey = Keys.LWin; var rType = settings.DefaultDataFlow; var keysOK = 0; for (var index = 0; index < args.Length; index++) { var arg = args[index]; if (string.IsNullOrWhiteSpace(arg)) { continue; } switch (arg.Substring(1, arg.Length - 1)) { case "help": case "h": case "?": Console.WriteLine(); Console.WriteLine("AudioSwitch v2.0 command-line help"); Console.WriteLine("----------------------------------"); Console.WriteLine("Available commands:"); Console.WriteLine(" /i - switch to input devices for the command. Default taken from settings."); Console.WriteLine(" /o - switch to output devices for the command. Default taken from settings."); Console.WriteLine(); Console.WriteLine(" /l - list all available devices of the selected type, < > is current default."); Console.WriteLine(" /s - set a device as default: number for index, string for name."); Console.WriteLine(); Console.WriteLine(" /m - set modifier keys for a new hotkey, separated by commas. Case sensitive."); 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(" /m, /k, /f (blank) - shows all possible values for the parameter."); break; case "i": rType = EDataFlow.eCapture; Console.WriteLine("Device group changed to input devices."); break; case "o": rType = EDataFlow.eRender; Console.WriteLine("Device group changed to output devices."); break; case "l": Console.WriteLine(); Console.WriteLine(" Devices available:"); EndPoints.RefreshDeviceList(rType); var i = 0; foreach (var device in EndPoints.DeviceNames.Values) { Console.WriteLine(device == EndPoints.DefaultDeviceName ? " <{0}> {1}" : " {0} {1}", i++, device); } Console.WriteLine(); break; case "s": index++; int devID; if (int.TryParse(args[index], out devID)) { EndPoints.RefreshDeviceList(rType); if (devID <= EndPoints.DeviceNames.Count - 1) { EndPoints.SetDefaultDeviceByID(devID); } else { Console.WriteLine("Error changing device!"); return; } } else { EndPoints.RefreshDeviceList(rType); if (!EndPoints.SetDefaultDeviceByName(args[index])) { Console.WriteLine("Error changing device!"); return; } } Console.WriteLine("Device changed to \"" + EndPoints.DefaultDeviceName + "\""); break; case "m": if (index == args.Length - 1) { Console.WriteLine("Modifier keys:"); Console.WriteLine(string.Join(", ", Enum.GetNames(typeof(HotModifierKeys)))); return; } index++; if (!Enum.TryParse(args[index], true, out modifiers)) { Console.WriteLine("Error reading modifier key(s)!"); return; } Console.WriteLine("Modifier key(s) set to " + modifiers); keysOK++; break; case "k": if (index == args.Length - 1) { Console.WriteLine("Keys:"); Console.WriteLine(string.Join(", ", Enum.GetNames(typeof(Keys)))); return; } index++; if (!Enum.TryParse(args[index], true, out hotKey)) { Console.WriteLine("Error reading hot key!"); return; } Console.WriteLine("Hot key set to " + hotKey); keysOK++; break; case "f": if (index == args.Length - 1) { Console.WriteLine("Function names:"); Console.WriteLine(string.Join(", ", Enum.GetNames(typeof(HotkeyFunction)))); return; } index++; if (!Enum.TryParse(args[index], true, out hotkeyFunction)) { Console.WriteLine("Error reading function name!"); return; } Console.WriteLine("Hot key function set to " + hotkeyFunction); keysOK++; break; } } if (keysOK == 3) { var hkey = GlobalHotkeys.AddOrFind(hotkeyFunction); hkey.ModifierKeys = modifiers; hkey.HotKey = hotKey; Console.WriteLine("Hot key saved: {0} => {1} + {2}", hotkeyFunction, modifiers, hotKey); settings.Save(); } return; } if (mutex.WaitOne(0, false)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frmOSD = new FormOSD(); formSwitcher = new FormSwitcher(); Application.Run(); mutex.Close(); } } finally { if (isConsole) { SendKeys.SendWait("{ENTER}"); FreeConsole(); } } }