diff --git a/CommonHelpers/BaseSettings.cs b/CommonHelpers/BaseSettings.cs index 1ec9454..6fd0cbe 100644 --- a/CommonHelpers/BaseSettings.cs +++ b/CommonHelpers/BaseSettings.cs @@ -16,6 +16,12 @@ namespace CommonHelpers [Browsable(false)] public bool TouchSettings { get; set; } + [Browsable(false)] + public bool UseMemoryCache { get; set; } = true; + + [Browsable(false)] + public bool UseConfigFile { get; set; } = true; + [Browsable(false)] public String SettingsKey { get; protected set; } @@ -34,7 +40,7 @@ namespace CommonHelpers [Browsable(false)] public bool Exists { - get { return File.Exists(this.ConfigFile); } + get { return !this.UseConfigFile || File.Exists(this.ConfigFile); } } public override string ToString() @@ -61,7 +67,7 @@ namespace CommonHelpers return false; SettingChanging(key); - if (!SetProfileString(key, valueString)) + if (this.UseConfigFile && !SetProfileString(key, valueString)) return false; cachedValues[key] = value; @@ -71,8 +77,11 @@ namespace CommonHelpers protected T Get(string key, T defaultValue, bool touchSettings = false) { - if (cachedValues.TryGetValue(key, out var cachedValue)) - return ((T?)cachedValue) ?? defaultValue; + if (this.UseMemoryCache) + { + if (cachedValues.TryGetValue(key, out var cachedValue)) + return ((T?)cachedValue) ?? defaultValue; + } var typeConverter = TypeDescriptor.GetConverter(typeof(T)); var defaultString = typeConverter.ConvertToString(defaultValue); @@ -82,6 +91,12 @@ namespace CommonHelpers return defaultValue; } + if (!this.UseConfigFile) + { + cachedValues[key] = defaultValue; + return defaultValue; + } + try { var valueString = GetProfileString(key, defaultString); @@ -131,7 +146,12 @@ namespace CommonHelpers lock (this) { cachedValues.Remove(key); - return WritePrivateProfileString(SettingsKey, key, null, ConfigFile); + + if (this.UseConfigFile) + { + return WritePrivateProfileString(SettingsKey, key, null, ConfigFile); + } + return true; } } @@ -140,7 +160,12 @@ namespace CommonHelpers lock (this) { cachedValues.Clear(); - return WritePrivateProfileString(SettingsKey, null, null, ConfigFile); + + if (this.UseConfigFile) + { + return WritePrivateProfileString(SettingsKey, null, null, ConfigFile); + } + return true; } } @@ -148,10 +173,10 @@ namespace CommonHelpers { lock (this) { - if (Exists) - return; - - using (File.Create(ConfigFile)) { } + if (this.UseConfigFile && Exists) + { + using (File.Create(ConfigFile)) { } + } } } @@ -160,8 +185,12 @@ namespace CommonHelpers lock (this) { cachedValues.Clear(); - try { File.Delete(ConfigFile); } - catch (DirectoryNotFoundException) { } + + if (this.UseConfigFile) + { + try { File.Delete(ConfigFile); } + catch (DirectoryNotFoundException) { } + } } } diff --git a/PowerControl/Helpers/ProfileSettings.cs b/PowerControl/Helpers/ProfileSettings.cs index 98b821e..c9ead61 100644 --- a/PowerControl/Helpers/ProfileSettings.cs +++ b/PowerControl/Helpers/ProfileSettings.cs @@ -26,10 +26,10 @@ namespace PowerControl.Helper public String ProfileName { get; } - public ProfileSettings(string profileName) : base("PersistentSettings") + public ProfileSettings(string prefix, string profileName) : base("PersistentSettings") { this.ProfileName = profileName; - this.ConfigFile = Path.Combine(UserProfilesPath, String.Format("PowerControl.Process.{0}.ini", profileName)); + this.ConfigFile = Path.Combine(UserProfilesPath, String.Format("{0}.{1}.ini", prefix, profileName)); this.SettingChanging += delegate { }; this.SettingChanged += delegate { }; diff --git a/PowerControl/Menu/MenuItemWithOptions.cs b/PowerControl/Menu/MenuItemWithOptions.cs index 5a8485a..73ef0f1 100644 --- a/PowerControl/Menu/MenuItemWithOptions.cs +++ b/PowerControl/Menu/MenuItemWithOptions.cs @@ -155,10 +155,50 @@ namespace PowerControl.Menu toolStripItem.DropDownItems.Add(item); } + AddMenuItemsToModifyProfile( + PowerControl.Options.Profiles.Controller?.AutostartProfileSettings, + toolStripItem.DropDownItems + ); + toolStripItem.Visible = Visible && Options.Count > 0; }; } + private void AddMenuItemsToModifyProfile(Helper.ProfileSettings? profileSettings, ToolStripItemCollection dropDownItems) + { + if (profileSettings is null || PersistentKey is null) + return; + + dropDownItems.Add(new ToolStripSeparator()); + + var headingItem = new ToolStripMenuItem(profileSettings.ProfileName + ": "); + dropDownItems.Add(headingItem); + + var persistedValue = profileSettings.GetValue(PersistentKey); + + foreach (var option in Options) + { + var item = new ToolStripMenuItem("Set: " + option); + item.Checked = option == persistedValue; + item.Click += delegate { profileSettings.SetValue(PersistentKey, option); }; + headingItem.DropDownItems.Add(item); + } + + if (persistedValue is not null) + { + headingItem.Text += persistedValue; + headingItem.Checked = true; + + headingItem.DropDownItems.Add(new ToolStripSeparator()); + var unsetItem = headingItem.DropDownItems.Add("Unset"); + unsetItem.Click += delegate { profileSettings.DeleteKey(PersistentKey); }; + } + else + { + headingItem.Text += "Not set"; + } + } + private void SelectIndex(int index) { if (Options.Count == 0) diff --git a/PowerControl/Options/Profiles.cs b/PowerControl/Options/Profiles.cs index 545dde0..dfc3d42 100644 --- a/PowerControl/Options/Profiles.cs +++ b/PowerControl/Options/Profiles.cs @@ -10,7 +10,7 @@ namespace PowerControl.Options ApplyDelay = 500, OptionsValues = delegate () { - var currentProfileSettings = Controller?.CurrentProfileSettings; + var currentProfileSettings = Controller?.GameProfileSettings; if (currentProfileSettings == null) return null; @@ -34,7 +34,7 @@ namespace PowerControl.Options CycleOptions = false, CurrentValue = delegate () { - var currentProfileSettings = Controller?.CurrentProfileSettings; + var currentProfileSettings = Controller?.GameProfileSettings; if (currentProfileSettings == null) return null; @@ -53,11 +53,11 @@ namespace PowerControl.Options case "Create New": Controller?.CreateProfile(false); - return Controller?.CurrentProfileSettings?.ProfileName; + return Controller?.GameProfileSettings?.ProfileName; case "Save All": Controller?.CreateProfile(true); - return Controller?.CurrentProfileSettings?.ProfileName; + return Controller?.GameProfileSettings?.ProfileName; default: return selected; diff --git a/PowerControl/ProfilesController.cs b/PowerControl/ProfilesController.cs index 4d5a63a..30e8b2e 100644 --- a/PowerControl/ProfilesController.cs +++ b/PowerControl/ProfilesController.cs @@ -10,10 +10,8 @@ namespace PowerControl { public const bool AutoCreateProfiles = true; public const int ApplyProfileDelayMs = 500; - public const int ResetProfileDelayMs = 500; private Dictionary watchedProcesses = new Dictionary(); - private Dictionary? changedSettings; private CancellationTokenSource? changeTask; private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() @@ -30,15 +28,23 @@ namespace PowerControl } } - public ProfileSettings? CurrentProfileSettings { get; private set; } + public ProfileSettings? GameProfileSettings { get; private set; } + + public ProfileSettings? SessionProfileSettings { get; private set; } + public ProfileSettings AutostartProfileSettings { get; private set; } public ProfilesController() { PowerControl.Options.Profiles.Controller = this; MenuStack.Root.ValueChanged += Root_OnOptionValueChange; + AutostartProfileSettings = new ProfileSettings("PowerControl", "Autostart"); + timer.Start(); timer.Tick += Timer_Tick; + + ProfileChanged(null); + ApplyProfile(AutostartProfileSettings); } ~ProfilesController() @@ -92,12 +98,12 @@ namespace PowerControl if (!watchedProcesses.TryGetValue(processId, out var profileSettings)) return false; - if (CurrentProfileSettings != profileSettings) + if (GameProfileSettings != profileSettings) { Log.TraceLine("ProfilesController: Foreground changed: {0} => {1}", - CurrentProfileSettings?.ProfileName, profileSettings.ProfileName); - CurrentProfileSettings = profileSettings; - ProfileChanged(); + GameProfileSettings?.ProfileName, profileSettings.ProfileName); + GameProfileSettings = profileSettings; + ProfileChanged(profileSettings); } return true; } @@ -106,12 +112,18 @@ namespace PowerControl { Log.TraceLine("ProfilesController: New Process: {0}/{1}", processId, processName); - if (changedSettings == null) - changedSettings = new Dictionary(); - - var profileSettings = new ProfileSettings(processName); + var profileSettings = new ProfileSettings("PowerControl.Process", processName); watchedProcesses.Add(processId, profileSettings); + // Create memory only SessionProfileSettings + if (SessionProfileSettings is null) + { + SessionProfileSettings = new ProfileSettings("PowerControl.Session", "Session." + processName) { UseConfigFile = false }; + SaveProfile(SessionProfileSettings); + } + + GameProfileSettings = profileSettings; + ProfileChanged(profileSettings); ApplyProfile(profileSettings); } @@ -120,8 +132,8 @@ namespace PowerControl if (!watchedProcesses.Remove(processId, out var profileSettings)) return; - if (CurrentProfileSettings == profileSettings) - CurrentProfileSettings = null; + if (GameProfileSettings == profileSettings) + GameProfileSettings = null; Log.TraceLine("ProfilesController: Removed Process: {0}", processId); @@ -136,79 +148,77 @@ namespace PowerControl if (options.PersistentKey is null) return; - if (oldValue is not null) - { - if (changedSettings?.TryAdd(options, oldValue) == true) - { - Log.TraceLine("ProfilesController: Saved change: {0} from {1}", options.PersistentKey, oldValue); - } - } + // No active profile, cannot persist + if (GameProfileSettings is null) + return; - // If profile exists persist value - if (CurrentProfileSettings != null && (CurrentProfileSettings.Exists || AutoCreateProfiles)) - { - CurrentProfileSettings.SetValue(options.PersistentKey, newValue); - options.ProfileOption = newValue; + // Do not auto-create profile unless requested + if (!GameProfileSettings.Exists && !AutoCreateProfiles) + return; - Log.TraceLine("ProfilesController: Stored: {0} {1} = {2}", - CurrentProfileSettings.ProfileName, options.PersistentKey, newValue); - } + GameProfileSettings.SetValue(options.PersistentKey, newValue); + options.ProfileOption = newValue; + + Log.TraceLine("ProfilesController: Stored: {0} {1} = {2}", + GameProfileSettings.ProfileName, options.PersistentKey, newValue); } - private void ProfileChanged() + private void ProfileChanged(ProfileSettings? profileSettings) { foreach (var menuItem in PersistableOptions()) { - menuItem.ProfileOption = CurrentProfileSettings?.GetValue(menuItem.PersistentKey ?? ""); + menuItem.ProfileOption = profileSettings?.GetValue(menuItem.PersistentKey ?? ""); } } public void CreateProfile(bool saveAll = true) { - var profileSettings = CurrentProfileSettings; - - profileSettings?.TouchFile(); - - Log.TraceLine("ProfilesController: Created Profile: {0}, SaveAll={1}", - profileSettings?.ProfileName, saveAll); - - if (!saveAll) + var profileSettings = GameProfileSettings; + if (profileSettings is null) return; - foreach (var menuItem in PersistableOptions()) - { - if (menuItem.ActiveOption is null || !menuItem.PersistOnCreate) - continue; - profileSettings?.SetValue(menuItem.PersistentKey ?? "", menuItem.ActiveOption); - } + profileSettings.TouchFile(); - ProfileChanged(); + Log.TraceLine("ProfilesController: Created Profile: {0}, SaveAll={1}", + profileSettings.ProfileName, saveAll); + + if (saveAll) + SaveProfile(profileSettings); + + ProfileChanged(profileSettings); } public void DeleteProfile() { - CurrentProfileSettings?.DeleteFile(); - ProfileChanged(); + GameProfileSettings?.DeleteFile(); + ProfileChanged(GameProfileSettings); - Log.TraceLine("ProfilesController: Deleted Profile: {0}", CurrentProfileSettings?.ProfileName); + Log.TraceLine("ProfilesController: Deleted Profile: {0}", GameProfileSettings?.ProfileName); + } + + private void SaveProfile(ProfileSettings profileSettings, bool force = false) + { + foreach (var menuItem in PersistableOptions()) + { + if (menuItem.ActiveOption is null || !menuItem.PersistOnCreate && !force) + continue; + profileSettings?.SetValue(menuItem.PersistentKey ?? "", menuItem.ActiveOption); + } } private void ApplyProfile(ProfileSettings profileSettings) { - CurrentProfileSettings = profileSettings; - ProfileChanged(); - - if (CurrentProfileSettings is null || CurrentProfileSettings?.Exists != true) + if (profileSettings is null || profileSettings?.Exists != true) return; - int delay = CurrentProfileSettings.GetInt("ApplyDelay", ApplyProfileDelayMs); + int delay = profileSettings.GetInt("ApplyDelay", ApplyProfileDelayMs); changeTask?.Cancel(); changeTask = Dispatcher.RunWithDelay(delay, () => { foreach (var menuItem in PersistableOptions()) { - var persistedValue = CurrentProfileSettings.GetValue(menuItem.PersistentKey ?? ""); + var persistedValue = profileSettings.GetValue(menuItem.PersistentKey ?? ""); if (persistedValue is null) continue; @@ -217,14 +227,14 @@ namespace PowerControl menuItem.Set(persistedValue, true, false); Log.TraceLine("ProfilesController: Applied from Profile: {0}: {1} = {2}", - CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue); + profileSettings.ProfileName, menuItem.PersistentKey, persistedValue); } catch (Exception e) { Log.TraceLine("ProfilesController: Exception Profile: {0}: {1} = {2} => {3}", - CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue, e); + profileSettings.ProfileName, menuItem.PersistentKey, persistedValue, e); - CurrentProfileSettings.DeleteKey(menuItem.PersistentKey ?? ""); + profileSettings.DeleteKey(menuItem.PersistentKey ?? ""); menuItem.ProfileOption = null; } } @@ -233,38 +243,14 @@ namespace PowerControl private void ResetProfile() { - CurrentProfileSettings = null; - ProfileChanged(); + GameProfileSettings = null; + ProfileChanged(null); - if (changedSettings is null) - return; - - // Revert all changes made to original value - var appliedSettings = changedSettings; - changedSettings = null; - - changeTask?.Cancel(); - changeTask = Dispatcher.RunWithDelay(ResetProfileDelayMs, () => + if (SessionProfileSettings is not null) { - foreach (var menuItem in PersistableOptions()) - { - if (!appliedSettings.TryGetValue(menuItem, out var setting)) - continue; - - try - { - menuItem.Set(setting, true, true); - - Log.TraceLine("ProfilesController: Reset: {0} = {1}", - menuItem.PersistentKey, setting); - } - catch (Exception e) - { - Log.TraceLine("ProfilesController: Reset Exception: {0} = {1} => {2}", - menuItem.PersistentKey, setting, e); - } - } - }); + ApplyProfile(SessionProfileSettings); + SessionProfileSettings = null; + } } private IEnumerable PersistableOptions() diff --git a/RELEASE.md b/RELEASE.md index 8fff653..4f1d0e3 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -31,6 +31,9 @@ ## 0.6.16 +- PowerControl: Allow to set Autostart Profile Settings +- PowerControl: Show Game Profiles menu item +- PowerControl: Improve handling of restoring DesktopProfile - All: Support [unofficial APU drivers](https://sourceforge.net/projects/amernimezone/files/Release%20Polaris-Vega-Navi/AMD%20SOC%20Driver%20Variant/) that present themselves as `AMD Radeon 670M` - PowerControl: Show Game Profiles menu item diff --git a/SteamController/Devices/SteamButtons.cs b/SteamController/Devices/SteamButtons.cs index 1a5704f..dfeef23 100644 --- a/SteamController/Devices/SteamButtons.cs +++ b/SteamController/Devices/SteamButtons.cs @@ -52,10 +52,10 @@ namespace SteamController.Devices public readonly SteamAxis GyroYaw = new SteamAxis(0x22); public readonly SteamAxis LeftTrigger = new SteamAxis(0x2C); public readonly SteamAxis RightTrigger = new SteamAxis(0x2E); - public readonly SteamAxis LeftThumbX = new SteamAxis(0x30) { Deadzone = 5000, MinChange = 10, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; - public readonly SteamAxis LeftThumbY = new SteamAxis(0x32) { Deadzone = 5000, MinChange = 10, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; - public readonly SteamAxis RightThumbX = new SteamAxis(0x34) { Deadzone = 5000, MinChange = 10, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; - public readonly SteamAxis RightThumbY = new SteamAxis(0x36) { Deadzone = 5000, MinChange = 10, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; + public readonly SteamAxis LeftThumbX = new SteamAxis(0x30) { Deadzone = 1400, MinChange = 0, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; + public readonly SteamAxis LeftThumbY = new SteamAxis(0x32) { Deadzone = 1400, MinChange = 0, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; + public readonly SteamAxis RightThumbX = new SteamAxis(0x34) { Deadzone = 1400, MinChange = 0, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; + public readonly SteamAxis RightThumbY = new SteamAxis(0x36) { Deadzone = 1400, MinChange = 0, DeltaValueMode = Devices.DeltaValueMode.AbsoluteTime }; public readonly SteamAxis LPadPressure = new SteamAxis(0x38); public readonly SteamAxis RPadPressure = new SteamAxis(0x38);