// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) 2023 Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 // Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein. // %COPYRIGHT_END% // --------------------------------------------------------------------- // %BANNER_END% namespace UnityEngine.XR.MagicLeap { using System; using System.Runtime.InteropServices; using UnityEngine.XR.MagicLeap.Native; public partial class MLPowerManager { /// /// See ml_power_manager.h for additional comments. /// internal class NativeBindings : Native.MagicLeapNativeBindings { #region Delegates /// /// This callback will be invoked when #MLPowerManagerPowerState changes. /// /// #MLPowerManagerPowerState representing the new power state. /// User data as passed to the #MLPowerManagerSetCallbacks. private delegate void OnPowerStateChangedInternalDelegate(PowerState state, in IntPtr user_data); /// /// This callback will be invoked when #MLPowerManagerPropertyData of a component changes. /// Only the properties that have changed will be returned, the component may support additional /// properties which values were not returned. /// /// #MLPowerManagerPropertyData struct encapsulating the properties changed. /// User data as passed to the #MLPowerManagerSetCallbacks. private delegate void OnPropertiesChangedInternalDelegate(in MLPowerManagerPropertyData properties, in IntPtr user_data); /// /// This callback will be invoked when an #MLPowerManagerError occurs on one of the components. /// /// The error which has occurred. /// User data as passed to the #MLPowerManagerSetCallbacks. private delegate void OnErrorOccurredInternalDelegate(Error error, in IntPtr user_data); #endregion #region CallbackMethods [AOT.MonoPInvokeCallback(typeof(OnPowerStateChangedInternalDelegate))] public static void HandleOnPowerChanged(PowerState state, in IntPtr user_data) { PowerState newState = state; MLThreadDispatch.ScheduleMain(() => { OnPowerStateChangedEvent?.Invoke(newState); }); } [AOT.MonoPInvokeCallback(typeof(OnPropertiesChangedInternalDelegate))] public static void HandleOnPropertiesChanged(in MLPowerManagerPropertyData properties, in IntPtr user_data) { PropertyData newProperties; newProperties.Properties = MLConvert.MarshalUnmanagedArray(properties.Properties, (int)properties.Size); MLThreadDispatch.ScheduleMain(() => { OnPropertiesChangedEvent?.Invoke(newProperties); }); } [AOT.MonoPInvokeCallback(typeof(OnErrorOccurredInternalDelegate))] public static void HandleOnErrorOccurred(Error error, in IntPtr user_data) { var newError = error; MLThreadDispatch.ScheduleMain(() => { OnErrorOccurredEvent?.Invoke(newError); }); } #endregion #region Structs [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerComponentProperty { /// /// The type of each property. /// public PropertyType Type; /// /// Union of ComponentPropertyData. This struct and union is used as a flexible way /// for each component to output an array containing distinct types of data. /// public ComponentPropertyData Data; } /// /// A structure to encapsulate output data when getting the /// current properties. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPropertyData { /// /// Size of Properties array. /// public byte Size; /// /// Array of #MLPowerManagerComponentProperty elements. /// public IntPtr Properties; } [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPropertyTypeData { /// /// Size of PropertyTypes array. /// public byte Size; /// /// Array of PropertyType elements. /// public IntPtr PropertyTypes; } /// /// A structure to encapsulate information used by the Power Manager /// when getting the available property types. /// /// Version of this structure. /// public uint Version; public static MLPowerManagerPropertyTypeInfo Init(uint version = 1) { return new MLPowerManagerPropertyTypeInfo { Version = version, }; } } /// /// A structure to encapsulate info data used by the Power Manager /// when getting the current properties. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPropertyInfo { /// /// Version of this structure. /// public uint Version; public static MLPowerManagerPropertyInfo Init(uint version = 1) { return new MLPowerManagerPropertyInfo { Version = version, }; } } /// /// A structure to encapsulate settings used by the Power Manager /// when requesting the power state to be changed. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPowerStateSettings { /// /// Version of this structure. /// public uint Version; /// /// New power state to request. /// public PowerState State; public static MLPowerManagerPowerStateSettings Init(uint version = 1) { return new MLPowerManagerPowerStateSettings { Version = version, State = PowerState.None, }; } } /// /// A structure to encapsulate info data used by the Power Manager /// when getting the current power state. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPowerStateInfo { /// /// Version of this structure. /// public uint Version; public static MLPowerManagerPowerStateInfo Init(uint version = 1) { return new MLPowerManagerPowerStateInfo { Version = version, }; } } /// /// A structure to encapsulate output data when either getting /// available power states, or the current power state. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerPowerStateData { /// /// Size of #power_states array. /// public byte Size; /// /// Array of #MLPowerManagerPowerState elements. /// public IntPtr PowerState; } /// /// A structure containing Power Manager callback events. /// Individual callbacks which are not required by the Power Manager can be NULL. /// [StructLayout(LayoutKind.Sequential)] public struct MLPowerManagerCallbacks { /// /// Version of this structure. /// public uint Version; private OnPowerStateChangedInternalDelegate OnPowerChanged; private OnPropertiesChangedInternalDelegate OnPropertiesChanged; private OnErrorOccurredInternalDelegate OnErrorOccurred; public static MLPowerManagerCallbacks Init(uint version = 1) { return new MLPowerManagerCallbacks { Version = version, OnPowerChanged = NativeBindings.HandleOnPowerChanged, OnPropertiesChanged = NativeBindings.HandleOnPropertiesChanged, OnErrorOccurred = NativeBindings.HandleOnErrorOccurred }; } } #endregion #region DLL /// /// Creates a Power Manager handle for a specified component. /// /// The component specific to the handle to be created. /// The handle to be created. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerCreate(Component component, out ulong handle); /// /// Destroys a Power Manager handle. /// /// The Power Manager handle for a specific component to be destroyed. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerDestroy(ulong handle); /// /// Register Power Manager callbacks for a specific handle. /// /// Power Manager handle for component to set #MLPowerManagerCallbacks for. /// Callbacks to receive Power Manager events. Set this to NULL to unregister callbacks. /// The caller can pass in user context data that will be returned in the callback(can be NULL). [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerSetCallbacks(ulong handle, in MLPowerManagerCallbacks callbacks , in IntPtr user_data); /// /// Sets the power state of a component. /// /// Power Manager handle for component to set power state for. /// Settings used by the Power Manager updating a component's power state. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerSetPowerState(ulong handle, in MLPowerManagerPowerStateSettings settings); /// /// Gets the power manager properties of a component. /// /// Power Manager handle for component to get properties of. /// /// #MLPowerManagerPropertyInfo struct filled with information about /// the power manager properties of a component to request. /// /// /// Information about the properties of a component. Must be /// released using #MLPowerManagerReleasePropertyData after each successful call. /// [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerGetComponentProperties(ulong handle, in MLPowerManagerPropertyInfo in_info, out MLPowerManagerPropertyData out_properties); /// /// Releases specified #MLPowerManagerPropertyData. /// /// Power Manager handle for component relating to #MLPowerManagerPropertyData. /// /// Pointer to a #MLPowerManagerPropertyData returned /// from #MLPowerManagerGetComponentProperties. /// [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerReleasePropertyData(ulong handle, in MLPowerManagerPropertyData properties); /// /// Query available power states for a component. /// /// Power Manager handle for component to get list of all available power states for. /// /// #MLPowerManagerPowerStateInfo struct filled with data /// to be used by the Power Manager when requesting/receiving all available power states. /// /// #MLPowerManagerPowerStateData holding list of available power states. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerGetAvailablePowerStates(ulong handle, in MLPowerManagerPowerStateInfo in_info, out MLPowerManagerPowerStateData out_states); /// /// Gets the power state of a component. /// /// Power Manager handle for component to get power state from. /// /// #MLPowerManagerPowerStateInfo struct filled with data /// to be used by the Power Manager when requesting/receiving power state. /// /// /// #MLPowerManagerPowerStateData with the current power /// state.Must be released using #MLPowerManagerReleasePowerStateData /// after each successful call. /// [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerGetPowerState(ulong handle, in MLPowerManagerPowerStateInfo in_info, out MLPowerManagerPowerStateData out_state); /// /// Releases specified #MLPowerManagerPowerStateData. /// /// Power Manager handle for component relating to #MLPowerManagerPowerStateData. /// Pointer to a #MLPowerManagerPowerStateData. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerReleasePowerStateData(ulong handle, in MLPowerManagerPowerStateData power_states); /// /// Request a list of the available #MLPowerManagerPropertyType. /// /// Power Manager handle for component to get properties from. /// /// #MLPowerManagerPropertyTypeInfo struct filled with data /// to be used by the Power Manager when requesting/receiving available property types. /// /// /// Information about the properties of a component. Must be /// released using #MLPowerManagerReleasePropertyTypeData after each successful call. /// [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerGetAvailableProperties(ulong handle, in MLPowerManagerPropertyTypeInfo in_info, out MLPowerManagerPropertyTypeData out_properties); /// /// Releases specified #MLPowerManagerPropertyTypeData. /// /// Power Manager handle for component relating to #MLPowerManagerPropertyTypeData. /// Pointer to a #MLPowerManagerPropertyTypeData. [DllImport(MLPowerManagerDll, CallingConvention = CallingConvention.Cdecl)] public extern static MLResult.Code MLPowerManagerReleasePropertyTypeData(ulong handle, in MLPowerManagerPropertyTypeData properties); #endregion } } }