XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
param()
$ErrorActionPreference = 'Stop'
if ($null -eq ('XfeHostDisplayProbe' -as [type])) {
    Add-Type -TypeDefinition @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public sealed class XfeHostDisplayInfo
{
    public string DeviceName;
    public string Description;
    public bool Primary;
    public int CurrentWidth;
    public int CurrentHeight;
    public int CurrentRefreshHz;
    public int MaximumWidth;
    public int MaximumHeight;
    public int MaximumRefreshHzAtMaximumResolution;
    public int MaximumRefreshHzAtCurrentResolution;
    public int ValidModeCount;
}

public static class XfeHostDisplayProbe
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct DisplayDevice
    {
        public int Size;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString;
        public uint StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceId;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct DevMode
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName;
        public ushort SpecVersion, DriverVersion, Size, DriverExtra;
        public uint Fields;
        public int PositionX, PositionY;
        public uint DisplayOrientation, DisplayFixedOutput;
        public short Color, Duplex, YResolution, TTOption, Collate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string FormName;
        public ushort LogPixels;
        public uint BitsPerPel, PelsWidth, PelsHeight, DisplayFlags, DisplayFrequency;
        public uint ICMMethod, ICMIntent, MediaType, DitherType, Reserved1, Reserved2, PanningWidth, PanningHeight;
    }

    [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "EnumDisplayDevicesW")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumDisplayDevices(string device, uint index, ref DisplayDevice info, uint flags);

    [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "EnumDisplaySettingsExW")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumDisplaySettingsEx(string device, int mode, ref DevMode info, uint flags);

    private static DevMode NewMode() { return new DevMode { Size = (ushort)Marshal.SizeOf(typeof(DevMode)) }; }
    private static bool Valid(DevMode mode)
    {
        return mode.PelsWidth >= 320 && mode.PelsWidth <= 16384 && mode.PelsHeight >= 200 && mode.PelsHeight <= 16384 &&
            mode.BitsPerPel >= 24 && mode.DisplayFrequency >= 20 && mode.DisplayFrequency <= 1000 && (mode.DisplayFlags & 2) == 0;
    }

    public static XfeHostDisplayInfo[] Read()
    {
        var result = new List<XfeHostDisplayInfo>();
        for (uint index = 0; index < 128; index++)
        {
            var device = new DisplayDevice { Size = Marshal.SizeOf(typeof(DisplayDevice)) };
            if (!EnumDisplayDevices(null, index, ref device, 0)) break;
            if ((device.StateFlags & 1) == 0 || (device.StateFlags & 8) != 0) continue;
            var current = NewMode();
            if (!EnumDisplaySettingsEx(device.DeviceName, -1, ref current, 0) || !Valid(current)) continue;
            var info = new XfeHostDisplayInfo {
                DeviceName = device.DeviceName, Description = device.DeviceString, Primary = (device.StateFlags & 4) != 0,
                CurrentWidth = (int)current.PelsWidth, CurrentHeight = (int)current.PelsHeight, CurrentRefreshHz = (int)current.DisplayFrequency,
                MaximumWidth = (int)current.PelsWidth, MaximumHeight = (int)current.PelsHeight,
                MaximumRefreshHzAtMaximumResolution = (int)current.DisplayFrequency, MaximumRefreshHzAtCurrentResolution = (int)current.DisplayFrequency
            };
            for (int modeIndex = 0; modeIndex < 10000; modeIndex++)
            {
                var mode = NewMode();
                if (!EnumDisplaySettingsEx(device.DeviceName, modeIndex, ref mode, 0)) break;
                if (!Valid(mode)) continue;
                info.ValidModeCount++;
                if (mode.PelsWidth == current.PelsWidth && mode.PelsHeight == current.PelsHeight)
                    info.MaximumRefreshHzAtCurrentResolution = Math.Max(info.MaximumRefreshHzAtCurrentResolution, (int)mode.DisplayFrequency);
                long area = (long)mode.PelsWidth * mode.PelsHeight;
                long largest = (long)info.MaximumWidth * info.MaximumHeight;
                if (area > largest)
                {
                    info.MaximumWidth = (int)mode.PelsWidth; info.MaximumHeight = (int)mode.PelsHeight;
                    info.MaximumRefreshHzAtMaximumResolution = (int)mode.DisplayFrequency;
                }
                else if (mode.PelsWidth == info.MaximumWidth && mode.PelsHeight == info.MaximumHeight)
                    info.MaximumRefreshHzAtMaximumResolution = Math.Max(info.MaximumRefreshHzAtMaximumResolution, (int)mode.DisplayFrequency);
            }
            result.Add(info);
        }
        return result.ToArray();
    }
}
'@
}
[XfeHostDisplayProbe]::Read()