From 12de2267bfce2b4d6efde5b3dfa6acfac0ecbbb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Tue, 10 Jan 2023 10:52:18 +0100 Subject: [PATCH] PowerControl: Detect RTSS applications inactivity --- CommonHelpers/RTSS.cs | 61 ++++++++++++++++++++++++++---- PowerControl/ProfilesController.cs | 6 +-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/CommonHelpers/RTSS.cs b/CommonHelpers/RTSS.cs index fa74ac1..12091e7 100644 --- a/CommonHelpers/RTSS.cs +++ b/CommonHelpers/RTSS.cs @@ -18,22 +18,65 @@ namespace CommonHelpers public static bool IsOSDForeground(out int processId, out string processName) { - return new Applications().FindForeground(out processId, out processName); + Applications.Instance.Refresh(); + + return Applications.Instance.FindForeground(out processId, out processName); } - public struct Applications + public class Applications { - public IDictionary IDs { get; } = new Dictionary(); + public readonly static Applications Instance = new Applications(); + + public struct Entry + { + public String ProcessName { get; set; } + public uint LastFrame { get; set; } + public DateTimeOffset LastFrameTime { get; set; } + + public bool IsRecent + { + get { return LastFrameTime.AddMilliseconds(FrameTimeoutMs) >= DateTimeOffset.UtcNow; } + } + } + + public IDictionary IDs { get; private set; } = new Dictionary(); + + private const int FrameTimeoutMs = 5000; public Applications() + { + Refresh(); + } + + public void Refresh() { RTSSSharedMemoryNET.AppEntry[] appEntries; + var oldIDs = IDs; + var newIDs = new Dictionary(); + try { appEntries = OSD.GetAppEntries(AppFlags.MASK); } catch { return; } + var now = DateTimeOffset.UtcNow; + foreach (var app in appEntries) - IDs.TryAdd(app.ProcessId, Path.GetFileNameWithoutExtension(app.Name)); + { + if (!oldIDs.TryGetValue(app.ProcessId, out var entry)) + { + entry.ProcessName = Path.GetFileNameWithoutExtension(app.Name); + } + + if (entry.LastFrame != app.OSDFrameId) + { + entry.LastFrame = app.OSDFrameId; + entry.LastFrameTime = now; + } + + newIDs.TryAdd(app.ProcessId, entry); + } + + IDs = newIDs; } public bool FindForeground(out int processId, out string processName) @@ -45,17 +88,21 @@ namespace CommonHelpers if (id is null) return false; - if (!IDs.TryGetValue(id.Value, out var name)) + if (!IDs.TryGetValue(id.Value, out var entry)) + return false; + if (!entry.IsRecent) return false; processId = id.Value; - processName = name; + processName = entry.ProcessName; return true; } public bool IsRunning(int processId) { - return IDs.ContainsKey(processId); + if (!IDs.TryGetValue(processId, out var entry)) + return false; + return entry.IsRecent; } } diff --git a/PowerControl/ProfilesController.cs b/PowerControl/ProfilesController.cs index 58ba262..224eb70 100644 --- a/PowerControl/ProfilesController.cs +++ b/PowerControl/ProfilesController.cs @@ -68,9 +68,9 @@ namespace PowerControl return; } - var applications = new RTSS.Applications(); + RTSS.Applications.Instance.Refresh(); - if (applications.FindForeground(out var processId, out var processName)) + if (RTSS.Applications.Instance.FindForeground(out var processId, out var processName)) { if (!BringUpProcess(processId)) AddProcess(processId, processName); @@ -78,7 +78,7 @@ namespace PowerControl foreach (var process in watchedProcesses) { - if (applications.IsRunning(process.Key)) + if (RTSS.Applications.Instance.IsRunning(process.Key)) continue; RemoveProcess(process.Key); }