// Initialization, version and error reference // https://www.glfw.org/docs/latest/group__init.html // Hand-written members of the Initialization library: the static constructor (native library load, // DllImportResolver, callback closure wiring), the Initialized state, the error callback wiring, // Init/Terminate (guarded lifecycle), InitAllocator (delegate rooting), and the version accessors // (VersionInfo struct packing + property forms). The mechanical SetInitHint and GetError wrappers // are generated into Generated/Libraries/Glfw.Initialization.gen.cs and merge into this same // partial class. using System.Diagnostics; namespace Brutal.GlfwApi; /// The function pointer type for error callbacks. public delegate void GlfwErrorCallback(GlfwErrorCode inError, string inDescription); public static unsafe partial class Glfw { internal static bool Initialized { get; private set; } // GLFW holds the function pointers marshalled from these delegates for the lifetime of the // process, so the delegates must stay rooted or the pointers dangle. A static strong // reference is sufficient for that: the three permanent callbacks live in static readonly // fields, and allocator callbacks are appended to AllocatorCallbacks. Entries are never // removed from the list: an allocator installed while the library is initialized keeps // servicing that init cycle until Terminate, so dropping the previous set on replacement // would reintroduce the dangling-pointer bug the list exists to prevent. Installing an // allocator is a once-per-process event in practice, so the retained set stays bounded. private static readonly GlfwMonitorUnmanagedCallback MonitorUnmanagedCallback; private static readonly GlfwJoystickUnmanagedCallback JoystickUnmanagedCallback; private static readonly GlfwErrorUnmanagedCallback ErrorUnmanagedCallback; private static readonly List AllocatorCallbacks = []; static Glfw() { // Load the GLFW native library var glfwNative = NativeLibrary.Load( OperatingSystem.IsWindows() ? "glfw3" : "glfw", typeof(Glfw).Assembly, DllImportSearchPath.AssemblyDirectory ); // Assign the native library to the assembly resolver NativeLibrary.SetDllImportResolver( typeof(Glfw).Assembly, (libraryName, _, _) => { // We should only be resolving the GLFW library, any other name is unexpected Debug.Assert(libraryName == "glfw", "Unexpected library name requested for resolution."); return glfwNative; } ); MonitorUnmanagedCallback = (GlfwMonitorUnmanagedCallback)( (monitor, eventType) => { try { MonitorCallback?.Invoke(new GlfwMonitor(monitor), (GlfwMonitorEvent)eventType); } catch (Exception ex) { ExceptionAggregator.Push("An exception occurred in the monitor callback.", ex); } } ); JoystickUnmanagedCallback = (GlfwJoystickUnmanagedCallback)( (joystick, eventType) => { try { JoystickCallback?.Invoke(joystick, (GlfwJoystickEvent)eventType); } catch (Exception ex) { ExceptionAggregator.Push("An exception occurred in the joystick callback.", ex); } } ); ErrorUnmanagedCallback = (GlfwErrorUnmanagedCallback)( (error, description) => { try { if (ErrorCallback != null) { var desc = Marshal.PtrToStringUTF8((nint)description) ?? string.Empty; ErrorCallback.Invoke((GlfwErrorCode)error, desc); } } catch (Exception ex) { ExceptionAggregator.Push("An exception occurred in the error callback.", ex); } } ); PInvoke.SetErrorCallback(ErrorUnmanagedCallback); } /// Sets the error callback. /// /// This function sets the error callback, which is called with an error code and a human-readable description /// each time a GLFW error occurs. /// /// The error code is set before the callback is called. Calling from the error /// callback will return the same value as the error code argument. /// /// The error callback is called on the thread where the error occurred. If you are using GLFW from multiple /// threads, your error callback needs to be written accordingly. /// /// Once set, the error callback remains set even after the library has been terminated. /// /// This function may be called before . /// /// This function must only be called from the main thread. /// Error handling public static event GlfwErrorCallback? ErrorCallback; /// Sets the init allocator to the desired value. /// /// This function sets the memory allocator that GLFW uses for all internal allocations. The allocator is picked /// up at the beginning of initialization, i.e. by the next call to , and stays in use until /// that init cycle is ended by . /// /// GLFW copies the callback pointers before this function returns; the wrapper keeps the managed delegates /// rooted for the remaining lifetime of the process, so the caller does not need to hold references to them. /// /// This function may be called before . /// /// The memory allocation callback. Must return a valid pointer or null to signal failure. /// The memory reallocation callback. /// The memory deallocation callback. /// An opaque pointer passed back to every callback invocation. /// Any of the three callbacks is null. /// This function must only be called from the main thread. /// Custom heap memory allocator public static void InitAllocator( GlfwAllocateUnmanagedCallback allocate, GlfwReallocateUnmanagedCallback reallocate, GlfwDeallocateUnmanagedCallback deallocate, void* user = null ) { ArgumentNullException.ThrowIfNull(allocate); ArgumentNullException.ThrowIfNull(reallocate); ArgumentNullException.ThrowIfNull(deallocate); AllocatorCallbacks.Add(allocate); AllocatorCallbacks.Add(reallocate); AllocatorCallbacks.Add(deallocate); var allocator = new GlfwAllocator { AllocateCallback = Marshal.GetFunctionPointerForDelegate(allocate), ReallocateCallback = Marshal.GetFunctionPointerForDelegate(reallocate), DeallocateCallback = Marshal.GetFunctionPointerForDelegate(deallocate), User = user, }; PInvoke.InitAllocator(&allocator); } /// Initializes the GLFW library. /// /// This function initializes the GLFW library. Before most GLFW functions can be used, GLFW must be initialized, /// and before an application terminates GLFW should be terminated in order to free any resources allocated during /// or after initialization. /// /// If this function fails, it calls before returning. If it succeeds, you should call /// before the application exits. /// /// Additional calls to this function after successful initialization but before termination will return /// true immediately. /// /// true if successful, or false if an error occurred. /// /// /// /// This function must only be called from the main thread. /// Initialization and termination public static bool Init() { if (Initialized) return true; Initialized = PInvoke.Init() == Constants.GLFW_TRUE; if (Initialized) { PInvoke.SetMonitorCallback(MonitorUnmanagedCallback); PInvoke.SetJoystickCallback(JoystickUnmanagedCallback); } return Initialized; } /// Terminates the GLFW library. /// /// This function destroys all remaining windows and cursors, restores any modified gamma ramps and frees any /// other allocated resources. Once this function is called, you must again call successfully /// before you will be able to use most GLFW functions. /// /// If GLFW has been successfully initialized, this function should be called before the application exits. /// If initialization fails, there is no need to call this function, as it is called by before /// it returns failure /// /// This function has no effect if GLFW is not initialized. /// /// /// /// /// This function must only be called from the main thread. /// Initialization and termination public static void Terminate() { if (Initialized) { PInvoke.Terminate(); Initialized = false; } } public struct VersionInfo { public int Major; public int Minor; public int Revision; } /// Retrieves the version of the GLFW library. /// /// This function retrieves the major, minor and revision numbers of the GLFW library. It is intended for when you /// are using GLFW as a shared library and want to ensure that you are using the minimum required version. /// /// This function may be called before /// /// Major, Minor, Revision. /// Version management /// This function may be called from any thread. public static VersionInfo Version { get { PInvoke.GetVersion(out var major, out var minor, out var rev); return new VersionInfo { Major = major, Minor = minor, Revision = rev, }; } } /// Returns a string describing the compile-time configuration. /// This function may be called from any thread. /// Version management // glfwGetVersionString always returns a valid static string, so the null-preserving marshaller's // nullable return can be safely forgiven here. public static string VersionString => PInvoke.GetVersionString()!; }