Gamma Init

This commit is contained in:
Serge
2024-02-16 15:55:37 +01:00
parent 42a598f177
commit cf84fa0616
103 changed files with 7907 additions and 41 deletions

View File

@@ -0,0 +1,48 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
/// <summary>
/// Contains possible values for the result of mode change request
/// </summary>
public enum ChangeDisplaySettingsExResults
{
/// <summary>
/// Completed successfully
/// </summary>
Successful = 0,
/// <summary>
/// Changes needs restart
/// </summary>
Restart = 1,
/// <summary>
/// Failed to change and save setings
/// </summary>
Failed = -1,
/// <summary>
/// Invalid data provide
/// </summary>
BadMode = -2,
/// <summary>
/// Changes not updated
/// </summary>
NotUpdated = -3,
/// <summary>
/// Invalid flags provided
/// </summary>
BadFlags = -4,
/// <summary>
/// Bad parameters provided
/// </summary>
BadParam = -5,
/// <summary>
/// Bad Dual View mode used with mode
/// </summary>
BadDualView = -6
}
}

View File

@@ -0,0 +1,18 @@
using System;
namespace WindowsDisplayAPI.Native.DeviceContext
{
[Flags]
internal enum ChangeDisplaySettingsFlags : uint
{
UpdateRegistry = 0x00000001,
Global = 0x00000008,
SetPrimary = 0x00000010,
Reset = 0x40000000,
NoReset = 0x10000000
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Runtime.InteropServices;
namespace WindowsDisplayAPI.Native.DeviceContext
{
internal class DCHandle : SafeHandle
{
private readonly bool _created;
private DCHandle(IntPtr handle, bool created) : base(handle, true)
{
_created = created;
}
public override bool IsInvalid
{
get => handle == IntPtr.Zero;
}
public static DCHandle CreateFromDevice(string screenName, string devicePath)
{
return new DCHandle(
DeviceContextApi.CreateDC(screenName, devicePath, null, IntPtr.Zero),
true
);
}
public static DCHandle CreateFromScreen(string screenName)
{
return CreateFromDevice(screenName, screenName);
}
public static DCHandle CreateFromWindow(IntPtr windowHandle)
{
return new DCHandle(
DeviceContextApi.GetDC(windowHandle),
true
);
}
public static DCHandle CreateGlobal()
{
return new DCHandle(
DeviceContextApi.CreateDC("DISPLAY", null, null, IntPtr.Zero),
true
);
}
protected override bool ReleaseHandle()
{
return _created
? DeviceContextApi.DeleteDC(this.handle)
: DeviceContextApi.ReleaseDC(IntPtr.Zero, this.handle);
}
}
}

View File

@@ -0,0 +1,51 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
internal enum DeviceCapability
{
DriverVersion = 0,
Technology = 2,
HorizontalSizeInMM = 4,
VerticalSizeInMM = 6,
HorizontalResolution = 8,
VerticalResolution = 10,
BitsPerPixel = 12,
Planes = 14,
NumberOfBrushes = 16,
NumberOfPens = 18,
NumberOfMarkers = 20,
NumberOfFonts = 22,
NumberOfColors = 24,
DeviceDescriptorSize = 26,
CurveCapabilities = 28,
LineCapabilities = 30,
PolygonalCapabilities = 32,
TextCapabilities = 34,
ClipCapabilities = 36,
RasterCapabilities = 38,
HorizontalAspect = 40,
VerticalAspect = 42,
HypotenuseAspect = 44,
//ShadeBlendingCapabilities = 45,
HorizontalLogicalPixels = 88,
VerticalLogicalPixels = 90,
PaletteSize = 104,
ReservedPaletteSize = 106,
ColorResolution = 108,
// Printer Only
PhysicalWidth = 110,
PhysicalHeight = 111,
PhysicalHorizontalMargin = 112,
PhysicalVerticalMargin = 113,
HorizontalScalingFactor = 114,
VerticalScalingFactor = 115,
// Display Only
VerticalRefreshRateInHz = 116,
DesktopVerticalResolution = 117,
DesktopHorizontalResolution = 118,
PreferredBLTAlignment = 119,
ShadeBlendingCapabilities = 120,
ColorManagementCapabilities = 121,
}
}

View File

@@ -0,0 +1,50 @@
using System;
namespace WindowsDisplayAPI.Native.DeviceContext
{
[Flags]
internal enum DeviceModeFields : uint
{
None = 0,
Position = 0x20,
DisplayOrientation = 0x80,
Color = 0x800,
Duplex = 0x1000,
YResolution = 0x2000,
TtOption = 0x4000,
Collate = 0x8000,
FormName = 0x10000,
LogPixels = 0x20000,
BitsPerPixel = 0x40000,
PelsWidth = 0x80000,
PelsHeight = 0x100000,
DisplayFlags = 0x200000,
DisplayFrequency = 0x400000,
DisplayFixedOutput = 0x20000000,
AllDisplay = Position |
DisplayOrientation |
YResolution |
BitsPerPixel |
PelsWidth |
PelsHeight |
DisplayFlags |
DisplayFrequency |
DisplayFixedOutput,
}
}

View File

@@ -0,0 +1,41 @@
using System;
namespace WindowsDisplayAPI.Native.DeviceContext
{
[Flags]
internal enum DisplayDeviceStateFlags : uint
{
/// <summary>
/// The device is part of the desktop.
/// </summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>
/// The device is part of the desktop.
/// </summary>
PrimaryDevice = 0x4,
/// <summary>
/// Represents a pseudo device used to mirror application drawing for remoting or other purposes.
/// </summary>
MirroringDriver = 0x8,
/// <summary>
/// The device is VGA compatible.
/// </summary>
VGACompatible = 0x10,
/// <summary>
/// The device is removable; it cannot be the primary display.
/// </summary>
Removable = 0x20,
/// <summary>
/// The device has more display modes than its output devices support.
/// </summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
}

View File

@@ -0,0 +1,23 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
/// <summary>
/// Contains possible values for the display fixed output
/// </summary>
public enum DisplayFixedOutput : uint
{
/// <summary>
/// Default behavior
/// </summary>
Default = 0,
/// <summary>
/// Stretches the output to fit to the display
/// </summary>
Stretch = 1,
/// <summary>
/// Centers the output in the middle of the display
/// </summary>
Center = 2
}
}

View File

@@ -0,0 +1,12 @@
using System;
namespace WindowsDisplayAPI.Native.DeviceContext
{
[Flags]
internal enum DisplayFlags : uint
{
None = 0,
Grayscale = 1,
Interlaced = 2
}
}

View File

@@ -0,0 +1,28 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
/// <summary>
/// Contains possible values for the display orientation
/// </summary>
public enum DisplayOrientation : uint
{
/// <summary>
/// No rotation
/// </summary>
Identity = 0,
/// <summary>
/// 90 degree rotation
/// </summary>
Rotate90Degree = 1,
/// <summary>
/// 180 degree rotation
/// </summary>
Rotate180Degree = 2,
/// <summary>
/// 270 degree rotation
/// </summary>
Rotate270Degree = 3
}
}

View File

@@ -0,0 +1,9 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
internal enum DisplaySettingsMode
{
CurrentSettings = -1,
RegistrySettings = -2
}
}

View File

@@ -0,0 +1,13 @@
namespace WindowsDisplayAPI.Native.DeviceContext
{
internal enum DisplayTechnology : int
{
Plotter = 0,
RasterDisplay = 1,
RasterPrinter = 2,
RasterCamera = 3,
CharacterStream = 4,
MetaFile = 5,
DisplayFile = 6,
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsDisplayAPI.Native.DeviceContext
{
internal enum MonitorFromFlag : uint
{
DefaultToNull = 0,
DefaultToPrimary = 1,
DefaultToNearest = 2,
}
}

View File

@@ -0,0 +1,11 @@
using System;
namespace WindowsDisplayAPI.Native.DeviceContext
{
[Flags]
internal enum MonitorInfoFlags : uint
{
None = 0,
Primary = 1
}
}

View File

@@ -0,0 +1,133 @@
using System.Runtime.InteropServices;
using WindowsDisplayAPI.Native.Structures;
namespace WindowsDisplayAPI.Native.DeviceContext.Structures
{
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd183565(v=vs.85).aspx
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
internal struct DeviceMode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] [FieldOffset(0)]
public readonly string DeviceName;
[MarshalAs(UnmanagedType.U2)] [FieldOffset(32)]
public readonly ushort SpecificationVersion;
[MarshalAs(UnmanagedType.U2)] [FieldOffset(34)]
public readonly ushort DriverVersion;
[MarshalAs(UnmanagedType.U2)] [FieldOffset(36)]
public readonly ushort Size;
[MarshalAs(UnmanagedType.U2)] [FieldOffset(38)]
public readonly ushort DriverExtra;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(40)]
public readonly DeviceModeFields Fields;
[MarshalAs(UnmanagedType.Struct)] [FieldOffset(44)]
public readonly PointL Position;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(52)]
public readonly DisplayOrientation DisplayOrientation;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(56)]
public readonly DisplayFixedOutput DisplayFixedOutput;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(60)]
public readonly short Color;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(62)]
public readonly short Duplex;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(64)]
public readonly short YResolution;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(66)]
public readonly short TrueTypeOption;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(68)]
public readonly short Collate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] [FieldOffset(72)]
private readonly string FormName;
[MarshalAs(UnmanagedType.U2)] [FieldOffset(102)]
public readonly ushort LogicalInchPixels;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(104)]
public readonly uint BitsPerPixel;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(108)]
public readonly uint PixelsWidth;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(112)]
public readonly uint PixelsHeight;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(116)]
public readonly DisplayFlags DisplayFlags;
[MarshalAs(UnmanagedType.U4)] [FieldOffset(120)]
public readonly uint DisplayFrequency;
public DeviceMode(DeviceModeFields fields) : this()
{
SpecificationVersion = 0x0320;
Size = (ushort) Marshal.SizeOf(GetType());
Fields = fields;
}
public DeviceMode(string deviceName, DeviceModeFields fields) : this(fields)
{
DeviceName = deviceName;
}
public DeviceMode(
string deviceName,
PointL position,
DisplayOrientation orientation,
DisplayFixedOutput fixedOutput,
uint bpp,
uint width,
uint height,
DisplayFlags displayFlags,
uint displayFrequency) : this(
deviceName,
DeviceModeFields.Position |
DeviceModeFields.DisplayOrientation |
DeviceModeFields.DisplayFixedOutput |
DeviceModeFields.BitsPerPixel |
DeviceModeFields.PelsWidth |
DeviceModeFields.PelsHeight |
DeviceModeFields.DisplayFlags |
DeviceModeFields.DisplayFrequency
)
{
Position = position;
DisplayOrientation = orientation;
DisplayFixedOutput = fixedOutput;
BitsPerPixel = bpp;
PixelsWidth = width;
PixelsHeight = height;
DisplayFlags = displayFlags;
DisplayFrequency = displayFrequency;
}
public DeviceMode(string deviceName, PointL position, uint bpp, uint width, uint height, uint displayFrequency)
: this(
deviceName,
DeviceModeFields.Position |
DeviceModeFields.BitsPerPixel |
DeviceModeFields.PelsWidth |
DeviceModeFields.PelsHeight |
DeviceModeFields.DisplayFrequency
)
{
Position = position;
BitsPerPixel = bpp;
PixelsWidth = width;
PixelsHeight = height;
DisplayFrequency = displayFrequency;
}
}
}

View File

@@ -0,0 +1,32 @@
using System.Runtime.InteropServices;
namespace WindowsDisplayAPI.Native.DeviceContext.Structures
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct DisplayDevice
{
[MarshalAs(UnmanagedType.U4)] internal uint Size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public readonly string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public readonly string DeviceString;
[MarshalAs(UnmanagedType.U4)] public readonly DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public readonly string DeviceId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public readonly string DeviceKey;
public static DisplayDevice Initialize()
{
return new DisplayDevice
{
Size = (uint) Marshal.SizeOf(typeof(DisplayDevice))
};
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsDisplayAPI.Native.DeviceContext.Structures
{
[StructLayout(LayoutKind.Sequential)]
internal struct GammaRamp
{
public const int DataPoints = 256;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
public readonly ushort[] Red;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
public readonly ushort[] Green;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DataPoints)]
public readonly ushort[] Blue;
public GammaRamp(ushort[] red, ushort[] green, ushort[] blue)
{
if (red == null)
{
throw new ArgumentNullException(nameof(red));
}
if (green == null)
{
throw new ArgumentNullException(nameof(green));
}
if (blue == null)
{
throw new ArgumentNullException(nameof(blue));
}
if (red.Length != DataPoints)
{
throw new ArgumentOutOfRangeException(nameof(red));
}
if (green.Length != DataPoints)
{
throw new ArgumentOutOfRangeException(nameof(green));
}
if (blue.Length != DataPoints)
{
throw new ArgumentOutOfRangeException(nameof(blue));
}
Red = red;
Green = green;
Blue = blue;
}
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using WindowsDisplayAPI.Native.Structures;
namespace WindowsDisplayAPI.Native.DeviceContext.Structures
{
[StructLayout(LayoutKind.Sequential)]
internal struct MonitorInfo
{
internal uint Size;
public readonly RectangleL Bounds;
public readonly RectangleL WorkingArea;
public readonly MonitorInfoFlags Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public readonly string DisplayName;
public static MonitorInfo Initialize()
{
return new MonitorInfo
{
Size = (uint)Marshal.SizeOf(typeof(MonitorInfo))
};
}
}
}