Skip to content

Commit

Permalink
PowerControl: Update and expose UserProfiles that can persist per-g…
Browse files Browse the repository at this point in the history
…ame settings
  • Loading branch information
ayufan committed Jan 9, 2023
1 parent 3252e79 commit 2d5f8c4
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 198 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build-Debug/
.vscode/
scripts/Redist/
SteamDeckTools_Setup*.exe
UserProfiles/
26 changes: 26 additions & 0 deletions CommonHelpers/BaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ protected BaseSettings(string settingsKey)
this.SettingChanged += delegate { };
}

public bool Exists
{
get { return File.Exists(this.ConfigFile); }
}

public override string ToString()
{
return "";
Expand Down Expand Up @@ -138,6 +143,27 @@ public bool DeleteAll()
}
}

public void TouchFile()
{
lock (this)
{
if (Exists)
return;

using (File.Create(ConfigFile)) { }
}
}

public void DeleteFile()
{
lock (this)
{
cachedValues.Clear();
try { File.Delete(ConfigFile); }
catch (DirectoryNotFoundException) { }
}
}

[DllImport("kernel32.dll")]
static extern bool WritePrivateProfileString(string lpAppName, string? lpKeyName, string? lpString, string lpFileName);

Expand Down
70 changes: 33 additions & 37 deletions CommonHelpers/RTSS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RTSSSharedMemoryNET;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CommonHelpers
Expand All @@ -15,32 +16,46 @@ public static bool IsOSDForeground(out int processId)
return IsOSDForeground(out processId, out _);
}

public static bool IsOSDForeground(out int processId, out string? applicationName)
public static bool IsOSDForeground(out int processId, out string processName)
{
applicationName = null;
return new Applications().FindForeground(out processId, out processName);
}

try
public struct Applications
{
public IDictionary<int, String> IDs { get; } = new Dictionary<int, String>();

public Applications()
{
RTSSSharedMemoryNET.AppEntry[] appEntries;

try { appEntries = OSD.GetAppEntries(AppFlags.MASK); }
catch { return; }

foreach (var app in appEntries)
IDs.TryAdd(app.ProcessId, Path.GetFileNameWithoutExtension(app.Name));
}

public bool FindForeground(out int processId, out string processName)
{
processId = 0;
processName = "";

var id = GetTopLevelProcessId();
processId = (int)id.GetValueOrDefault(0);
if (id is null)
return false;

foreach (var app in OSD.GetAppEntries(AppFlags.MASK))
{
if (app.ProcessId == processId)
{
applicationName = ExtractAppName(app.Name);
return true;
}
}
if (!IDs.TryGetValue(id.Value, out var name))
return false;

return false;
processId = id.Value;
processName = name;
return true;
}
catch

public bool IsRunning(int processId)
{
processId = 0;
return false;
return IDs.ContainsKey(processId);
}
}

Expand Down Expand Up @@ -86,13 +101,6 @@ public static bool SetProfileProperty<T>(string propertyName, T value)
}
}

public static List<string> GetCurrentApps()
{
var apps = OSD.GetAppEntries(AppFlags.MASK).Select(e => ExtractAppName(e.Name)).ToList();

return apps;
}

public static uint EnableFlag(uint flag, bool status)
{
var current = SetFlags(~flag, status ? flag : 0);
Expand Down Expand Up @@ -142,24 +150,12 @@ public static void UpdateSettings()
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

private static string ExtractAppName(string fullName)
{
string res = fullName.Split('\\').Last();

if (res.ToLower().Contains(".exe"))
{
return res[..^4];
}

return res;
}

private static uint? GetTopLevelProcessId()
private static int? GetTopLevelProcessId()
{
var hWnd = GetForegroundWindow();
var result = GetWindowThreadProcessId(hWnd, out uint processId);
if (result != 0)
return processId;
return (int)processId;
return null;
}

Expand Down
8 changes: 6 additions & 2 deletions PowerControl/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class Controller : IDisposable
DateTime? neptuneDeviceNextKey;
System.Windows.Forms.Timer neptuneTimer;

ProfilesController profilesController;
ProfilesController? profilesController;

SharedData<PowerControlSetting> sharedData = SharedData<PowerControlSetting>.CreateNew();

Expand Down Expand Up @@ -113,7 +113,6 @@ public Controller()
osdTimer.Enabled = true;

profilesController = new ProfilesController();
profilesController.Initialize();

GlobalHotKey.RegisterHotKey(Settings.Default.MenuUpKey, () =>
{
Expand Down Expand Up @@ -222,6 +221,10 @@ private void OsdTimer_Tick(object? sender, EventArgs e)
notifyIcon.Icon = Resources.traffic_light_outline_red;
}

var watchedProfiles = profilesController?.WatchedProfiles ?? new string[0];
if (watchedProfiles.Any())
notifyIcon.Text += ". Profile: " + string.Join(", ", watchedProfiles);

updateOSD();
}

Expand Down Expand Up @@ -381,6 +384,7 @@ private void ExitItem_Click(object? sender, EventArgs e)

public void Dispose()
{
using (profilesController) { }
components.Dispose();
osdClose();
}
Expand Down
45 changes: 24 additions & 21 deletions PowerControl/Helpers/ProfileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,46 @@ namespace PowerControl.Helper
{
public class ProfileSettings : BaseSettings
{
private static string profilesPath = Path.Combine(Directory.GetCurrentDirectory(), "Profiles");

static ProfileSettings()
public static String UserProfilesPath
{
Directory.CreateDirectory(profilesPath);
get
{
var exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
var exeFolder = Path.GetDirectoryName(exePath) ?? Directory.GetCurrentDirectory();
var exeUserProfiles = Path.Combine(exeFolder, "UserProfiles");
if (!Directory.Exists(exeUserProfiles))
Directory.CreateDirectory(exeUserProfiles);
return exeUserProfiles;
}
}

public ProfileSettings(string profileName) : base("Profile")
public String ProfileName { get; }

public ProfileSettings(string profileName) : base("PersistentSettings")
{
this.TouchSettings = true;
this.ConfigFile = Path.Combine(profilesPath, profileName + ".ini");
this.ProfileName = profileName;
this.ConfigFile = Path.Combine(UserProfilesPath, String.Format("PowerControl.Process.{0}.ini", profileName));

this.SettingChanging += delegate { };
this.SettingChanged += delegate { };
}

public T Get<T>(string key, T defaultValue)
public String? GetValue(string key)
{
return base.Get(key, defaultValue);
var result = base.Get(key, String.Empty);
if (result == String.Empty)
return null;
return result;
}

public new bool Set<T>(string key, T value)
public int GetInt(string key, int defaultValue)
{
return base.Set(key, value);
return base.Get(key, defaultValue);
}

public static bool CheckIfExists(string profileName)
public void SetValue(string key, string value)
{
foreach (FileInfo fi in Directory.CreateDirectory(profilesPath).GetFiles())
{
if (fi.Name[^4..].Equals(".ini") && fi.Name[..^4].Equals(profileName))
{
return true;
}
}

return false;
base.Set(key, value);
}
}
}
137 changes: 0 additions & 137 deletions PowerControl/Helpers/ProfilesController.cs

This file was deleted.

Loading

0 comments on commit 2d5f8c4

Please sign in to comment.