// Monitor reference
// https://www.glfw.org/docs/latest/group__monitor.html
// Hand-written members of the Monitor library: the GlfwMonitor handle struct scaffolding + its
// marshaller, the monitor-configuration callback delegate/event, GetMonitors (span - deferred),
// and the instance members not yet covered by a generator transform: Position/PhysicalSize/
// ContentScale (out-params -> vector), GetMonitorWorkArea (4 outs -> 2 vectors), Size (computed),
// VideoModes (span), VideoMode (pointer deref), and GammaRamp (GlfwGammaRampView get/set). The
// basic members - PrimaryMonitor, Name, UserPointer, SetGamma - are generated into
// Generated/Libraries/Glfw.Monitor.gen.cs and merge into these same partial declarations.
using System.Runtime.InteropServices.Marshalling;
namespace Brutal.GlfwApi;
/// The function pointer type for monitor configuration callbacks.
/// The monitor that was connected or disconnected.
///
/// One of or
/// .
///
///
/// Monitor configuration changes
public delegate void GlfwMonitorCallback(GlfwMonitor monitor, GlfwMonitorEvent eventType);
public static unsafe partial class Glfw
{
/// Sets the monitor configuration function.
///
/// This function sets the monitor configuration callback, or removes the currently set callback. This is called when a
/// monitor is connected to or disconnected from the system.
///
///
///
///
/// This function must only be called from the main thread.
/// Monitor configuration changes
public static event GlfwMonitorCallback? MonitorCallback;
}
///
/// Wrapper around a *
///
[NativeMarshalling(typeof(GlfwMonitorMarshaller))]
public readonly unsafe partial struct GlfwMonitor : INullable
{
public static readonly GlfwMonitor Null = new(null);
internal GlfwMonitor(GlfwMonitorHandle* inPointer)
{
Pointer = inPointer;
}
internal GlfwMonitorHandle* Pointer { get; }
///
/// Gets the native Glfw handle for this monitor
///
public nint NativeHandle => (nint)Pointer;
public bool IsNull()
{
return Pointer == null;
}
/// Retrieves the work area of the monitor.
///
/// This function returns the position, in screen coordinates, of the upper-left corner of the work area of the
/// monitor along with the work area size in screen coordinates. The work area is defined as the area
/// of the monitor not occluded by the operating system task bar where present. If no task bar exists then the
/// work area is the monitor resolution in screen coordinates.
///
/// If an error occurs, all values are set to zero.
///
///
/// ,
///
///
/// This function must only be called from the main thread.
/// Work area
public void GetMonitorWorkArea(out int2 pos, out int2 size)
{
Glfw.PInvoke.GetMonitorWorkArea(this, out var x, out var y, out var w, out var h);
pos = new int2(x, y);
size = new int2(w, h);
}
public int2 Size
{
get
{
var videoMode = VideoMode;
return new int2(videoMode.Width, videoMode.Height);
}
}
/// Returns the current mode of the monitor.
///
/// This function returns the current video mode of the monitor. If you have created a full screen window for
/// that monitor, the return value will depend on whether that window is iconified.
///
///
///
///
///
/// This function must only be called from the main thread.
/// VideoModes
/// Video modes
public GlfwVidMode VideoMode
{
get
{
var videoMode = Glfw.PInvoke.GetVideoMode(this);
return videoMode != null ? *videoMode : default;
}
}
/// Gets/Sets the current gamma ramp for the monitor.
///
/// The getter returns the current gamma ramp of the monitor, or an empty view if an error occurred. The view
/// is valid until the monitor is disconnected, the gamma ramp is read again for that monitor or the library
/// is terminated.
///
/// The setter sets the current gamma ramp for the monitor. The original gamma ramp for that monitor is
/// saved by GLFW the first time the ramp is set and is restored by glfwTerminate. The specified gamma ramp
/// is copied before the setter returns.
///
/// The software controlled gamma ramp is applied in addition to the hardware gamma correction, which today is usually
/// an approximation of sRGB gamma. This means that setting a perfectly linear ramp, or gamma 1.0, will produce the
/// default (usually sRGB-like) behavior.
///
/// For gamma correct rendering with OpenGL or OpenGL ES, see the GLFW_SRGB_CAPABLE hint.
///
/// The setter was given an empty ramp.
///
///
///
///
/// This function must only be called from the main thread.
/// Gamma ramp
public GlfwGammaRampView GammaRamp
{
get
{
var ramp = Glfw.PInvoke.GetGammaRamp(this);
if (ramp == null)
return default;
var size = (int)ramp->Size;
return new GlfwGammaRampView(
new ReadOnlySpan(ramp->Red, size),
new ReadOnlySpan(ramp->Green, size),
new ReadOnlySpan(ramp->Blue, size)
);
}
set
{
if (value.IsEmpty)
throw new ArgumentException("Cannot set an empty gamma ramp.", nameof(value));
fixed (ushort* red = value.Red)
fixed (ushort* green = value.Green)
fixed (ushort* blue = value.Blue)
{
var ramp = new GlfwGammaRamp
{
Red = red,
Green = green,
Blue = blue,
Size = (uint)value.Size,
};
Glfw.PInvoke.SetGammaRamp(this, &ramp);
}
}
}
}
/// Describes the gamma ramp for a monitor.
///
/// Views returned by borrow memory owned by GLFW; see that
/// property for the lifetime rules.
///
///
/// Gamma ramp
public readonly ref struct GlfwGammaRampView
{
/// Creates a gamma ramp view over three equal-length channel buffers.
/// The response of the red channel.
/// The response of the green channel.
/// The response of the blue channel.
/// The channels are not all the same length.
public GlfwGammaRampView(ReadOnlySpan red, ReadOnlySpan green, ReadOnlySpan blue)
{
if (red.Length != green.Length || red.Length != blue.Length)
throw new ArgumentException("Gamma ramp channels must all be the same length.");
Red = red;
Green = green;
Blue = blue;
}
/// The response of the red channel.
public ReadOnlySpan Red { get; }
/// The response of the green channel.
public ReadOnlySpan Green { get; }
/// The response of the blue channel.
public ReadOnlySpan Blue { get; }
/// The number of elements in each channel.
public int Size => Red.Length;
/// Whether the view holds no data.
public bool IsEmpty => Red.IsEmpty;
}
[CustomMarshaller(
managedType: typeof(GlfwMonitor),
marshalMode: MarshalMode.Default,
marshallerType: typeof(GlfwMonitorMarshaller)
)]
internal static unsafe class GlfwMonitorMarshaller
{
internal static GlfwMonitorHandle* ConvertToUnmanaged(GlfwMonitor managed) => managed.Pointer;
internal static GlfwMonitor ConvertToManaged(GlfwMonitorHandle* unmanaged) => new(unmanaged);
}