This commit is contained in:
TinyHerm committed 2026-11-24 23:40:08 +03:00
commit 1c7a8cf25f
579 files changed
+83196

No files matched your search

@@ -0,0 +1,181 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace VibranceHud.Fullscreen;
public static class AnyFullScreenWindow
{
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(Left, Top, Right, Bottom);
}
}
private struct MONITORINFO
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}
private const int QUNS_BUSY = 2;
private const int QUNS_RUNNING_D3D_FULL_SCREEN = 3;
private const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
private const uint MONITOR_DEFAULTTONEAREST = 2u;
[DllImport("shell32.dll")]
private static extern int SHQueryUserNotificationState(out int state);
[DllImport("user32.dll")]
private static extern nint GetForegroundWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(nint hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(nint hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetClassNameW")]
private static extern int GetClassName(nint hWnd, StringBuilder className, int maxCount);
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(nint hWnd, out uint processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(nint hWnd, out RECT rect);
[DllImport("dwmapi.dll")]
private static extern int DwmGetWindowAttribute(nint hWnd, int attribute, out RECT value, int size);
[DllImport("user32.dll")]
private static extern nint MonitorFromWindow(nint hWnd, uint flags);
[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetMonitorInfoW")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetMonitorInfo(nint monitor, ref MONITORINFO info);
internal static bool ForShellState(int state)
{
if (state != 2)
{
return state == 3;
}
return true;
}
internal static bool FillsItsMonitor(Rectangle window, Rectangle monitor, bool visible, bool minimized, bool isShellOrDesktop, bool isOwnProcess)
{
if (!visible | minimized | isShellOrDesktop | isOwnProcess)
{
return false;
}
if (monitor.Width <= 0 || monitor.Height <= 0)
{
return false;
}
if (window.Left <= monitor.Left && window.Top <= monitor.Top && window.Right >= monitor.Right)
{
return window.Bottom >= monitor.Bottom;
}
return false;
}
internal static bool IsShellOrDesktopClass(string? className)
{
switch (className)
{
case "Progman":
case "WorkerW":
case "Shell_TrayWnd":
case "Shell_SecondaryTrayWnd":
return true;
default:
return false;
}
}
public static bool IsInFront()
{
try
{
if (SHQueryUserNotificationState(out var state) == 0 && ForShellState(state))
{
return true;
}
return ForegroundFillsItsMonitor();
}
catch
{
return false;
}
}
internal static string? ForegroundMonitorDeviceName()
{
try
{
nint foregroundWindow = GetForegroundWindow();
return (foregroundWindow == IntPtr.Zero) ? null : Screen.FromHandle(foregroundWindow).DeviceName;
}
catch
{
return null;
}
}
private static bool ForegroundFillsItsMonitor()
{
nint foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return false;
}
bool visible = IsWindowVisible(foregroundWindow);
bool minimized = IsIconic(foregroundWindow);
StringBuilder stringBuilder = new StringBuilder(256);
string className = ((GetClassName(foregroundWindow, stringBuilder, stringBuilder.Capacity) > 0) ? stringBuilder.ToString() : null);
GetWindowThreadProcessId(foregroundWindow, out var processId);
bool isOwnProcess = processId != 0 && processId == (uint)Environment.ProcessId;
if (DwmGetWindowAttribute(foregroundWindow, 9, out var value, Marshal.SizeOf<RECT>()) != 0 && !GetWindowRect(foregroundWindow, out value))
{
return false;
}
nint num = MonitorFromWindow(foregroundWindow, 2u);
if (num == IntPtr.Zero)
{
return false;
}
MONITORINFO info = new MONITORINFO
{
cbSize = Marshal.SizeOf<MONITORINFO>()
};
if (!GetMonitorInfo(num, ref info))
{
return false;
}
return FillsItsMonitor(value.ToRectangle(), info.rcMonitor.ToRectangle(), visible, minimized, IsShellOrDesktopClass(className), isOwnProcess);
}
}
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
using VibranceHud.Crosshair;
namespace VibranceHud.Fullscreen;
public static class ExclusiveFullscreenEvidence
{
public const string FullscreenOptimisationsOff = "DISABLEDXMAXIMIZEDWINDOWEDMODE";
private const string LayersKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers";
public static bool MatrixCannotArrive(bool shellSaysD3DFullscreen, string? foregroundProcessName, IEnumerable<KeyValuePair<string, string>> appCompatLayers)
{
if (shellSaysD3DFullscreen)
{
return FullscreenOptimisationsAreOffFor(foregroundProcessName, appCompatLayers);
}
return false;
}
internal static bool FullscreenOptimisationsAreOffFor(string? foregroundProcessName, IEnumerable<KeyValuePair<string, string>> appCompatLayers)
{
string text = WithExe(foregroundProcessName);
if (text == null || appCompatLayers == null)
{
return false;
}
foreach (KeyValuePair<string, string> appCompatLayer in appCompatLayers)
{
if (!string.IsNullOrEmpty(appCompatLayer.Key) && !string.IsNullOrEmpty(appCompatLayer.Value))
{
string fileName;
try
{
fileName = Path.GetFileName(appCompatLayer.Key);
}
catch
{
continue;
}
if (string.Equals(fileName, text, StringComparison.OrdinalIgnoreCase) && NamesTheToken(appCompatLayer.Value))
{
return true;
}
}
}
return false;
}
private static bool NamesTheToken(string layers)
{
string[] array = layers.Split(new char[2] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < array.Length; i++)
{
if (string.Equals(array[i], "DISABLEDXMAXIMIZEDWINDOWEDMODE", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private static string? WithExe(string? name)
{
if (string.IsNullOrWhiteSpace(name))
{
return null;
}
name = name.Trim();
if (!name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
return name + ".exe";
}
return name;
}
public static bool ForThisMachine()
{
try
{
if (!CrosshairService.IsExclusiveFullscreen())
{
return false;
}
string text = ForegroundWindowProcess.Name();
if (text == null)
{
return false;
}
return FullscreenOptimisationsAreOffFor(text, ReadLayers());
}
catch
{
return false;
}
}
private static IEnumerable<KeyValuePair<string, string>> ReadLayers()
{
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
Collect(Registry.CurrentUser, list);
Collect(Registry.LocalMachine, list);
return list;
}
private static void Collect(RegistryKey hive, List<KeyValuePair<string, string>> into)
{
try
{
using RegistryKey registryKey = hive.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", writable: false);
if (registryKey == null)
{
return;
}
string[] valueNames = registryKey.GetValueNames();
foreach (string text in valueNames)
{
if (!string.IsNullOrEmpty(text) && registryKey.GetValue(text) is string { Length: >0 } text2)
{
into.Add(new KeyValuePair<string, string>(text, text2));
}
}
}
catch
{
}
}
}
@@ -0,0 +1,37 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace VibranceHud.Fullscreen;
public static class ForegroundWindowProcess
{
[DllImport("user32.dll")]
private static extern nint GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(nint hWnd, out uint processId);
public static string? Name()
{
try
{
nint foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return null;
}
if (GetWindowThreadProcessId(foregroundWindow, out var processId) == 0 || processId == 0)
{
return null;
}
using Process process = Process.GetProcessById((int)processId);
string processName = process.ProcessName;
return string.IsNullOrEmpty(processName) ? null : processName;
}
catch
{
return null;
}
}
}
+215
View File
@@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VibranceHud.Fullscreen;
public sealed class FullscreenState
{
public static readonly TimeSpan EnterHold = TimeSpan.FromSeconds(2.0);
private readonly Func<bool> _shellSaysD3DFullscreen;
private readonly Func<string?> _foregroundProcessName;
private readonly HashSet<string> _tracked;
private readonly Func<long> _nowMs;
public static readonly TimeSpan LeaveHold = TimeSpan.FromMilliseconds(750.0);
private long? _candidateSinceMs;
private string? _candidate;
private long? _leaveSinceMs;
private readonly bool _anyWindow;
private const string UnnamedWindow = "(a full-screen window)";
private static readonly string CouldNotTell = new string("(could not tell)".ToCharArray());
public bool LeavePending => _leaveSinceMs.HasValue;
public bool IsExclusiveFullscreen { get; private set; }
public string? Game
{
get
{
if (!IsExclusiveFullscreen)
{
return null;
}
return _candidate;
}
}
public bool CandidatePending
{
get
{
if (_candidate != null)
{
return !IsExclusiveFullscreen;
}
return false;
}
}
public string? LatchedGame { get; private set; }
public FullscreenState(Func<bool> shellSaysD3DFullscreen, Func<string?> foregroundProcessName, IEnumerable<string> trackedProcessNames, Func<long> monotonicMs)
: this(shellSaysD3DFullscreen, foregroundProcessName, trackedProcessNames, monotonicMs, anyWindow: false)
{
}
private FullscreenState(Func<bool> shellSaysD3DFullscreen, Func<string?> foregroundProcessName, IEnumerable<string> trackedProcessNames, Func<long> monotonicMs, bool anyWindow)
{
_anyWindow = anyWindow;
_shellSaysD3DFullscreen = shellSaysD3DFullscreen ?? throw new ArgumentNullException("shellSaysD3DFullscreen");
_foregroundProcessName = foregroundProcessName ?? throw new ArgumentNullException("foregroundProcessName");
_tracked = new HashSet<string>(trackedProcessNames ?? Enumerable.Empty<string>(), StringComparer.OrdinalIgnoreCase);
_nowMs = monotonicMs ?? throw new ArgumentNullException("monotonicMs");
}
public static FullscreenState ForAnyFullScreenWindow(Func<bool> foregroundFillsTheScreen, Func<string?> foregroundProcessName, Func<long> monotonicMs)
{
return new FullscreenState(foregroundFillsTheScreen, foregroundProcessName, Array.Empty<string>(), monotonicMs, anyWindow: true);
}
public bool GameClosed(string? processName)
{
string text = WithoutExe(processName);
if (LatchedGame == null || text == null || !string.Equals(text, LatchedGame, StringComparison.OrdinalIgnoreCase))
{
return false;
}
LatchedGame = null;
return true;
}
public bool Poll()
{
string text = CandidateNow();
if ((object)text == CouldNotTell)
{
return IsExclusiveFullscreen;
}
if (text == null)
{
if (_anyWindow && IsExclusiveFullscreen)
{
long valueOrDefault = _leaveSinceMs.GetValueOrDefault();
if (!_leaveSinceMs.HasValue)
{
valueOrDefault = _nowMs();
_leaveSinceMs = valueOrDefault;
}
if (_nowMs() - _leaveSinceMs.Value < (long)LeaveHold.TotalMilliseconds)
{
return true;
}
}
_leaveSinceMs = null;
_candidate = null;
_candidateSinceMs = null;
IsExclusiveFullscreen = false;
return false;
}
_leaveSinceMs = null;
if (IsExclusiveFullscreen && string.Equals(text, _candidate, StringComparison.OrdinalIgnoreCase))
{
if (!_anyWindow)
{
LatchedGame = _candidate;
}
return true;
}
if (_anyWindow && IsExclusiveFullscreen)
{
_candidate = text;
return true;
}
if (!string.Equals(text, _candidate, StringComparison.OrdinalIgnoreCase))
{
_candidate = text;
_candidateSinceMs = _nowMs();
return IsExclusiveFullscreen = false;
}
if (!_candidateSinceMs.HasValue || _nowMs() - _candidateSinceMs.Value < (long)EnterHold.TotalMilliseconds)
{
return IsExclusiveFullscreen = false;
}
if (!_anyWindow)
{
LatchedGame = _candidate;
}
return IsExclusiveFullscreen = true;
}
private string? CandidateNow()
{
try
{
if (!_shellSaysD3DFullscreen())
{
if (LatchedGame != null && string.Equals(TrackedForeground(), LatchedGame, StringComparison.OrdinalIgnoreCase))
{
LatchedGame = null;
}
return null;
}
return TrackedForeground();
}
catch
{
return null;
}
}
private string? TrackedForeground()
{
string name;
try
{
name = _foregroundProcessName();
}
catch when (!_anyWindow)
{
return CouldNotTell;
}
string text = WithoutExe(name);
if (_anyWindow)
{
return text ?? "(a full-screen window)";
}
if (text == null)
{
return CouldNotTell;
}
if (!_tracked.Contains(text))
{
return null;
}
return text;
}
private static string? WithoutExe(string? name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}
if (name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
name = name.Substring(0, name.Length - 4);
}
if (name.Length != 0)
{
return name;
}
return null;
}
}
+266
View File
@@ -0,0 +1,266 @@
using System;
using System.Windows.Forms;
using VibranceHud.Crosshair;
using VibranceHud.Games;
namespace VibranceHud.Fullscreen;
public sealed class FullscreenWatch : IDisposable
{
public const int BackstopTickMs = 60000;
public static readonly TimeSpan RepollDelay = FullscreenState.EnterHold + TimeSpan.FromMilliseconds(100.0);
public static readonly TimeSpan LeaveRepollDelay = FullscreenState.LeaveHold + TimeSpan.FromMilliseconds(100.0);
private readonly FullscreenState _state;
private readonly Func<Action<nint>, WinEventListener> _listenerFactory;
private readonly bool _realTimers;
private WinEventListener? _listener;
private Timer? _repollTimer;
private Timer? _backstopTimer;
private bool _last;
private bool _started;
private bool _disposed;
internal Func<bool>? FullscreenAllowed { get; set; }
public string? LatchedGame => _state.LatchedGame;
internal bool RepollArmed { get; private set; }
internal bool HookRefused
{
get
{
if (_listener != null)
{
return !_listener.IsHooked;
}
return false;
}
}
internal bool BackstopArmed { get; private set; }
public event Action<bool, string?>? Changed;
public event Action<string?>? LatchChanged;
public void GameClosed(string? processName)
{
if (!_disposed && _state.GameClosed(processName))
{
Raise(LatchChanged, _state.LatchedGame);
}
}
public FullscreenWatch(FullscreenState state)
: this(state, DefaultListener, realTimers: true)
{
}
internal FullscreenWatch(FullscreenState state, Func<Action<nint>, WinEventListener> listenerFactory, bool realTimers)
{
_state = state ?? throw new ArgumentNullException("state");
_listenerFactory = listenerFactory ?? throw new ArgumentNullException("listenerFactory");
_realTimers = realTimers;
}
private static WinEventListener DefaultListener(Action<nint> onForeground)
{
return new WinEventListener(3u, 3u, onForeground);
}
public static FullscreenWatch ForThisMachine()
{
return new FullscreenWatch(new FullscreenState(CrosshairService.IsExclusiveFullscreen, ForegroundWindowProcess.Name, SupportedGames.AllProcessNames, () => Environment.TickCount64));
}
public static FullscreenWatch ForAnyFullScreenWindow()
{
return new FullscreenWatch(FullscreenState.ForAnyFullScreenWindow(AnyFullScreenWindow.IsInFront, ForegroundWindowProcess.Name, () => Environment.TickCount64));
}
public void Start()
{
if (_disposed || _started)
{
return;
}
_started = true;
_listener = _listenerFactory((nint _) =>
{
Poll();
});
Poll();
BackstopArmed = true;
if (_realTimers)
{
_backstopTimer = new Timer
{
Interval = 60000
};
_backstopTimer.Tick += (object? _, EventArgs _) =>
{
Poll();
};
_backstopTimer.Start();
}
}
public void PollNow()
{
Poll();
}
public void Stop()
{
_started = false;
BackstopArmed = false;
_listener?.Dispose();
_listener = null;
DisarmRepoll();
if (_repollTimer != null)
{
_repollTimer.Dispose();
_repollTimer = null;
}
if (_backstopTimer != null)
{
_backstopTimer.Stop();
_backstopTimer.Dispose();
_backstopTimer = null;
}
}
internal void TickForTest()
{
Poll();
}
internal void FireRepoll()
{
DisarmRepoll();
Poll();
}
private void Poll()
{
if (_disposed)
{
return;
}
string latchedGame = _state.LatchedGame;
bool flag = _state.Poll();
if (flag && FullscreenAllowed != null)
{
try
{
flag = FullscreenAllowed();
}
catch
{
flag = false;
}
}
if (_state.CandidatePending || _state.LeavePending)
{
ArmRepoll(_state.LeavePending);
}
else
{
DisarmRepoll();
}
if (flag != _last)
{
_last = flag;
Raise(Changed, flag, _state.Game);
}
if (!string.Equals(latchedGame, _state.LatchedGame, StringComparison.OrdinalIgnoreCase))
{
Raise(LatchChanged, _state.LatchedGame);
}
}
private static void Raise(Action<bool, string?>? handlers, bool now, string? game)
{
if (handlers == null)
{
return;
}
Delegate[] invocationList = handlers.GetInvocationList();
foreach (Delegate obj in invocationList)
{
try
{
((Action<bool, string>)obj)(now, game);
}
catch
{
}
}
}
private static void Raise(Action<string?>? handlers, string? game)
{
if (handlers == null)
{
return;
}
Delegate[] invocationList = handlers.GetInvocationList();
foreach (Delegate obj in invocationList)
{
try
{
((Action<string>)obj)(game);
}
catch
{
}
}
}
private void ArmRepoll(bool leaving = false)
{
RepollArmed = true;
if (!_realTimers)
{
return;
}
if (_repollTimer == null)
{
_repollTimer = new Timer();
_repollTimer.Tick += (object? _, EventArgs _) =>
{
FireRepoll();
};
}
_repollTimer.Interval = (int)(leaving ? LeaveRepollDelay : RepollDelay).TotalMilliseconds;
_repollTimer.Stop();
_repollTimer.Start();
}
private void DisarmRepoll()
{
RepollArmed = false;
_repollTimer?.Stop();
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
Stop();
}
}
}