/// <summary> /// Verifies that the input in the textbox is a unique name and is non-empty, calls in to /// OutputSwitcher.Core to create and save the new preset. /// </summary> /// <returns>True if preset created and saved. False if input is invalid or failed to save new preset.</returns> private bool TryCreateNewPresetWithEnteredName() { if (TextboxEnterNewPresetName.Text.Length < 1) { MessageBox.Show("Please enter at least one character.", "OutputSwitcher"); return(false); } string newPresetName = TextboxEnterNewPresetName.Text; DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); DisplayPreset existingPreset = displayPresetCollection.GetPreset(newPresetName); if (existingPreset != null) { MessageBox.Show("Name already exists, please enter a unique name.", "OutputSwitcher"); return(false); } DisplayPreset newPreset = DisplayPresetRecorderAndApplier.RecordCurrentConfiguration(newPresetName); if (!displayPresetCollection.TryAddDisplayPreset(newPreset)) { MessageBox.Show("Failed to save new display preset configuration.", "OutputSwitcher"); return(false); } else { return(true); } }
static public void CaptureCurrentConfigAndSaveAsPreset() { bool uniqueNameEntered = false; string presetName; DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); do { Console.Write("Enter name for new preset: "); presetName = Console.ReadLine(); DisplayPreset existingDisplayPreset = displayPresetCollection.GetPreset(presetName); uniqueNameEntered = existingDisplayPreset == null; if (!uniqueNameEntered) { Console.WriteLine("Preset with name '" + presetName + "' already exists. Please choose a different name."); } } while (!uniqueNameEntered); DisplayPreset newPreset = DisplayPresetRecorderAndApplier.RecordCurrentConfiguration(presetName); if (!displayPresetCollection.TryAddDisplayPreset(newPreset)) { throw new Exception("Failed to add new preset to saved presets collection."); // This is really unexpected because we've checked for existence already. } Console.WriteLine("Added new preset!"); ConsoleOutputUtilities.WriteDisplayPresetToConsole(newPreset); }
/// <summary> /// Event handler when a global hotkey event for this app has been raised. Finds the /// preset that corresponds to the hotkey pressed and applies it. /// </summary> /// <param name="modifierKeyFlags">Modifier keys for the hotkey.</param> /// <param name="keycode">Key code for the hotkey.</param> private void OnPresetSwitchHotkeyPressed(ushort modifierKeyFlags, ushort keycode) { // TODO: This kinda sucks because it sorta relies on abusing the interface of PresetToHotkeyMap. foreach (KeyValuePair <string, VirtualHotkey> presetHotkeyPair in mPresetToHotkeyMap.GetHotkeyMappings().ToArray()) { // Chop off the MOD_NOREPEAT that is added by HotkeyRegistrar. ushort presetModifierKeyFlags = unchecked ((ushort)(presetHotkeyPair.Value.ModifierKeyCode & (VirtualKeyCodes.MOD_ALT | VirtualKeyCodes.MOD_CONTROL | VirtualKeyCodes.MOD_SHIFT | VirtualKeyCodes.MOD_WIN))); ushort presetKeyCode = (unchecked ((ushort)(presetHotkeyPair.Value.KeyCode))); if (presetModifierKeyFlags == modifierKeyFlags && presetKeyCode == keycode) { DisplayPreset invokedPreset = DisplayPresetCollection.GetDisplayPresetCollection().GetPreset(presetHotkeyPair.Key); if (invokedPreset != null) { SafePresetApplier.ApplyPresetWithRevertCountdown(invokedPreset); } else { throw new Exception("Hotkey pressed for unknown preset."); } break; } } }
/// <summary> /// Applies the supplied DisplayPreset and displays a 15 second countdown to /// revert to the previous display configuration unless a user intervenes. This is /// to mimic the behavior of Windows when display resolutions are changed, and is /// a safety pattern in case the new display configuration is not visible. /// </summary> /// <param name="displayPreset">The display preset to apply.</param> public static void ApplyPresetWithRevertCountdown(DisplayPreset displayPreset) { DisplayPreset lastConfig = DisplayPresetRecorderAndApplier.ReturnLastConfigAndApplyPreset(displayPreset); // Pop up a dialog to give user the option to keep the configuration or else // automatically revert to the last configuration. // TODO: This seems weird to pass control away like this. UseAppliedPresetCountdownForm revertPresetCountdownForm = new UseAppliedPresetCountdownForm(lastConfig); revertPresetCountdownForm.Show(); }
public UseAppliedPresetCountdownForm(DisplayPreset revertPreset) { InitializeComponent(); mRevertPreset = revertPreset; mCountdown = COUNTDOWN_TIME_MAX; UpdateLabelCountdownText(); mTimer = new Timer(); mTimer.Interval = 1000; // Tick every 1 second. mTimer.Tick += Timer_Tick; mTimer.Start(); }
public static void ListPresetDetail() { DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); List <DisplayPreset> displayPresets = displayPresetCollection.GetPresets(); Console.WriteLine("Available presets: "); IEnumerator <DisplayPreset> displayPresetsEnumerator = displayPresets.GetEnumerator(); for (int i = 0; i < displayPresets.Count; i++) { displayPresetsEnumerator.MoveNext(); Console.WriteLine(String.Format("[{0}] {1}", i, displayPresetsEnumerator.Current.Name)); } Console.Write("Select preset: "); string selection = Console.ReadLine(); int selectedPresetIndex = -1; string selectedPresetName = String.Empty; if (!Int32.TryParse(selection, out selectedPresetIndex)) { // If it's not a number, assume the user typed in a name selectedPresetName = selection; } else if (selectedPresetIndex >= displayPresets.Count) { Console.WriteLine("Invalid selection!"); return; } else { selectedPresetName = displayPresets[selectedPresetIndex].Name; } DisplayPreset selectedPreset = displayPresetCollection.GetPreset(selectedPresetName); if (selectedPreset == null) { Console.WriteLine("Invalid preset name: " + selectedPresetName); return; } ConsoleOutputUtilities.WriteDisplayPresetToConsole(selectedPreset); }
static public void ApplyPreset() { Console.Write("Enter name of preset to apply: "); string presetName = Console.ReadLine(); DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); DisplayPreset targetPreset = displayPresetCollection.GetPreset(presetName); if (targetPreset == null) { Console.WriteLine("Preset with name '" + presetName + "' does not exist."); return; } Console.WriteLine("Applying preset '" + presetName + "'..."); DisplayPresetRecorderAndApplier.ApplyPreset(targetPreset); }
static public void WriteDisplayPresetToConsole(DisplayPreset displayPreset) { Console.WriteLine("Preset Name: " + displayPreset.Name); foreach (CCD.DisplayConfigPathInfo dcPathInfo in displayPreset.PathInfoArray) { WriteDisplayConfigPathInfoToConsole(dcPathInfo); Console.WriteLine(); } foreach (CCD.DisplayConfigModeInfo dcModeInfo in displayPreset.ModeInfoArray) { WriteDisplayConfigModeInfoToConsole(dcModeInfo); Console.WriteLine(); } foreach (CCD.DisplayConfigTargetDeviceName dcTargetDeviceName in displayPreset.TargetDeviceNames) { WriteDisplayConfigTargetDeviceNameToConsole(dcTargetDeviceName); Console.WriteLine(); } }
public static void ApplyPreset(string[] args) { if (args.Length < 2) { Console.WriteLine("Insufficient arguments."); return; } // Trim quotes in case the preset name has spaces string presetName = args[1].Trim(new char[] { '"', '\'' }); DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); DisplayPreset displayPreset = displayPresetCollection.GetPreset(presetName); if (displayPreset == null) { Console.WriteLine(String.Format("Preset '{0}' does not exist.", presetName)); return; } DisplayPresetRecorderAndApplier.ApplyPreset(displayPreset); }
public static void CaptureCurrentConfigAndSaveAsPreset(string[] args) { if (args.Length < 2) { Console.WriteLine("Insufficient arguments."); return; } // Trim quotes in case the preset name has spaces string presetName = args[1].Trim(new char[] { '"', '\'' }); DisplayPreset displayPreset = DisplayPresetRecorderAndApplier.RecordCurrentConfiguration(presetName); DisplayPresetCollection displayPresetCollection = DisplayPresetCollection.GetDisplayPresetCollection(); if (displayPresetCollection.TryAddDisplayPreset(displayPreset)) { Console.WriteLine(String.Format("New display preset '{0}' saved!", presetName)); } else { Console.WriteLine("Failed to save preset."); } }