diff --git a/CommonHelpers/OSDHelpers.cs b/CommonHelpers/OSDHelpers.cs index d907ab9..4cd70ab 100644 --- a/CommonHelpers/OSDHelpers.cs +++ b/CommonHelpers/OSDHelpers.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -9,6 +10,126 @@ namespace CommonHelpers { public static class OSDHelpers { + public static bool IsOSDForeground() + { + return IsOSDForeground(out _, out _); + } + + public static bool IsOSDForeground(out int processId) + { + return IsOSDForeground(out processId, out _); + } + + public static bool IsOSDForeground(out int processId, out string processName) + { + Applications.Instance.Refresh(); + + return Applications.Instance.FindForeground(out processId, out processName); + } + + public class Applications + { + 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 IsOSDForeground { 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; + + var topLevelProcessId = GetTopLevelProcessId(); + + foreach (var app in appEntries) + { + if (!oldIDs.TryGetValue(app.ProcessId, out var entry)) + { + entry.ProcessName = Path.GetFileNameWithoutExtension(app.Name); + } + + entry.IsOSDForeground = (topLevelProcessId == app.ProcessId); + + if (entry.LastFrame != app.OSDFrameId || entry.IsOSDForeground) + { + entry.LastFrame = app.OSDFrameId; + entry.LastFrameTime = now; + } + + newIDs.TryAdd(app.ProcessId, entry); + } + + IDs = newIDs; + } + + public bool FindForeground(out int processId, out string processName) + { + processId = 0; + processName = ""; + + var id = GetTopLevelProcessId(); + if (id is null) + return false; + + if (!IDs.TryGetValue(id.Value, out var entry)) + return false; + if (!entry.IsRecent) + return false; + + processId = id.Value; + processName = entry.ProcessName; + return true; + } + + public bool IsRunning(int processId) + { + if (!IDs.TryGetValue(processId, out var entry)) + return false; + return entry.IsRecent; + } + } + + private static int? GetTopLevelProcessId() + { + var hWnd = GetForegroundWindow(); + var result = GetWindowThreadProcessId(hWnd, out uint processId); + if (result != 0) + return (int)processId; + return null; + } + + [DllImport("user32.dll", SetLastError = true)] + private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + public static uint OSDIndex(this OSD? osd) { if (osd is null) diff --git a/CommonHelpers/RTSS.cs b/CommonHelpers/RTSS.cs index cd2fbac..276bfed 100644 --- a/CommonHelpers/RTSS.cs +++ b/CommonHelpers/RTSS.cs @@ -1,116 +1,9 @@ -using RTSSSharedMemoryNET; -using System.Diagnostics; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; namespace CommonHelpers { public static class RTSS { - public static bool IsOSDForeground() - { - return IsOSDForeground(out _, out _); - } - - public static bool IsOSDForeground(out int processId) - { - return IsOSDForeground(out processId, out _); - } - - public static bool IsOSDForeground(out int processId, out string processName) - { - Applications.Instance.Refresh(); - - return Applications.Instance.FindForeground(out processId, out processName); - } - - public class Applications - { - 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 IsOSDForeground { 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; - - var topLevelProcessId = GetTopLevelProcessId(); - - foreach (var app in appEntries) - { - if (!oldIDs.TryGetValue(app.ProcessId, out var entry)) - { - entry.ProcessName = Path.GetFileNameWithoutExtension(app.Name); - } - - entry.IsOSDForeground = (topLevelProcessId == app.ProcessId); - - if (entry.LastFrame != app.OSDFrameId || entry.IsOSDForeground) - { - entry.LastFrame = app.OSDFrameId; - entry.LastFrameTime = now; - } - - newIDs.TryAdd(app.ProcessId, entry); - } - - IDs = newIDs; - } - - public bool FindForeground(out int processId, out string processName) - { - processId = 0; - processName = ""; - - var id = GetTopLevelProcessId(); - if (id is null) - return false; - - if (!IDs.TryGetValue(id.Value, out var entry)) - return false; - if (!entry.IsRecent) - return false; - - processId = id.Value; - processName = entry.ProcessName; - return true; - } - - public bool IsRunning(int processId) - { - if (!IDs.TryGetValue(processId, out var entry)) - return false; - return entry.IsRecent; - } - } - public static bool GetProfileProperty(string propertyName, out T value) { var bytes = new byte[Marshal.SizeOf()]; @@ -196,20 +89,6 @@ namespace CommonHelpers [DllImport("C:\\Program Files (x86)\\RivaTuner Statistics Server\\RTSSHooks64.dll", CharSet = CharSet.Ansi)] public static extern void UpdateProfiles(); - [DllImport("user32.dll", SetLastError = true)] - static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); - - [DllImport("user32.dll")] - public static extern IntPtr GetForegroundWindow(); - - private static int? GetTopLevelProcessId() - { - var hWnd = GetForegroundWindow(); - var result = GetWindowThreadProcessId(hWnd, out uint processId); - if (result != 0) - return (int)processId; - return null; - } private static void PostMessage(uint Msg, IntPtr wParam, IntPtr lParam) { diff --git a/PowerControl/Controller.cs b/PowerControl/Controller.cs index ca8cc3f..be46e00 100644 --- a/PowerControl/Controller.cs +++ b/PowerControl/Controller.cs @@ -116,7 +116,7 @@ namespace PowerControl GlobalHotKey.RegisterHotKey(Settings.Default.MenuUpKey, () => { - if (!RTSS.IsOSDForeground()) + if (!OSDHelpers.IsOSDForeground()) return; rootMenu.Next(-1); setDismissTimer(); @@ -125,7 +125,7 @@ namespace PowerControl GlobalHotKey.RegisterHotKey(Settings.Default.MenuDownKey, () => { - if (!RTSS.IsOSDForeground()) + if (!OSDHelpers.IsOSDForeground()) return; rootMenu.Next(1); setDismissTimer(); @@ -134,7 +134,7 @@ namespace PowerControl GlobalHotKey.RegisterHotKey(Settings.Default.MenuLeftKey, () => { - if (!RTSS.IsOSDForeground()) + if (!OSDHelpers.IsOSDForeground()) return; rootMenu.SelectNext(-1); setDismissTimer(); @@ -143,7 +143,7 @@ namespace PowerControl GlobalHotKey.RegisterHotKey(Settings.Default.MenuRightKey, () => { - if (!RTSS.IsOSDForeground()) + if (!OSDHelpers.IsOSDForeground()) return; rootMenu.SelectNext(1); setDismissTimer(); @@ -154,7 +154,7 @@ namespace PowerControl { isOSDToggled = !rootMenu.Visible; - if (!RTSS.IsOSDForeground()) + if (!OSDHelpers.IsOSDForeground()) return; if (isOSDToggled) @@ -285,7 +285,7 @@ namespace PowerControl return; } - if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS) || !RTSS.IsOSDForeground()) + if (!neptuneDeviceState.buttons5.HasFlag(SDCButton5.BTN_QUICK_ACCESS) || !OSDHelpers.IsOSDForeground()) { // schedule next repeat far in the future dismissNeptuneInput(); diff --git a/PowerControl/Options/SMT.cs b/PowerControl/Options/SMT.cs index 3237999..1803c32 100644 --- a/PowerControl/Options/SMT.cs +++ b/PowerControl/Options/SMT.cs @@ -14,7 +14,7 @@ namespace PowerControl.Options ResetValue = () => { return "Yes"; }, CurrentValue = delegate () { - if (!RTSS.IsOSDForeground(out var processId)) + if (!OSDHelpers.IsOSDForeground(out var processId)) return null; if (!ProcessorCores.HasSMTThreads()) return null; @@ -23,7 +23,7 @@ namespace PowerControl.Options }, ApplyValue = (selected) => { - if (!RTSS.IsOSDForeground(out var processId)) + if (!OSDHelpers.IsOSDForeground(out var processId)) return null; if (!ProcessorCores.HasSMTThreads()) return null; diff --git a/PowerControl/ProfilesController.cs b/PowerControl/ProfilesController.cs index eefb751..4d5a63a 100644 --- a/PowerControl/ProfilesController.cs +++ b/PowerControl/ProfilesController.cs @@ -71,9 +71,9 @@ namespace PowerControl return; } - RTSS.Applications.Instance.Refresh(); + OSDHelpers.Applications.Instance.Refresh(); - if (RTSS.Applications.Instance.FindForeground(out var processId, out var processName)) + if (OSDHelpers.Applications.Instance.FindForeground(out var processId, out var processName)) { if (!BringUpProcess(processId)) AddProcess(processId, processName); @@ -81,7 +81,7 @@ namespace PowerControl foreach (var process in watchedProcesses) { - if (RTSS.Applications.Instance.IsRunning(process.Key)) + if (OSDHelpers.Applications.Instance.IsRunning(process.Key)) continue; RemoveProcess(process.Key); } diff --git a/SteamController/Managers/RTSSManager.cs b/SteamController/Managers/RTSSManager.cs index 9a2eaa2..f838f3e 100644 --- a/SteamController/Managers/RTSSManager.cs +++ b/SteamController/Managers/RTSSManager.cs @@ -6,7 +6,7 @@ namespace SteamController.Managers { public override void Tick(Context context) { - context.State.RTSSInForeground = Settings.Default.DetectRTSSForeground && RTSS.IsOSDForeground(); + context.State.RTSSInForeground = Settings.Default.DetectRTSSForeground && OSDHelpers.IsOSDForeground(); } } }