using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using NvAPIWrapper.Native.DRS.Structures; using NvAPIWrapper.Native.Exceptions; using NvAPIWrapper.Native.General; using NvAPIWrapper.Native.General.Structures; using NvAPIWrapper.Native.Helpers; using NvAPIWrapper.Native.Helpers.Structures; using NvAPIWrapper.Native.Interfaces.DRS; namespace NvAPIWrapper.Native { /// /// Contains driver settings static functions /// // ReSharper disable once ClassTooBig public static class DRSApi { /// /// This API adds an executable name to a profile. /// /// Input to the session handle. /// Input profile handle. /// Input instance containing the executable name. /// The newly created instance of . public static IDRSApplication CreateApplication( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, IDRSApplication application) { using (var applicationReference = ValueTypeReference.FromValueType(application, application.GetType())) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, applicationReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status != Status.Ok) { throw new NVIDIAApiException(status); } return applicationReference.ToValueType(application.GetType()); } } /// /// This API creates an empty profile. /// /// Input to the session handle. /// Input to the instance. /// The newly created profile handle. public static DRSProfileHandle CreateProfile(DRSSessionHandle sessionHandle, DRSProfileV1 profile) { using (var profileReference = ValueTypeReference.FromValueType(profile)) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileReference, out var profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return profileHandle; } } /// /// This API allocates memory and initializes the session. /// /// The newly created session handle. public static DRSSessionHandle CreateSession() { var status = DelegateFactory.GetDelegate()(out var sessionHandle); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return sessionHandle; } /// /// This API removes an executable from a profile. /// /// Input to the session handle. /// Input profile handle. /// Input all the information about the application to be removed. public static void DeleteApplication( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, IDRSApplication application) { using (var applicationReference = ValueTypeReference.FromValueType(application, application.GetType())) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, applicationReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status != Status.Ok) { throw new NVIDIAApiException(status); } } } /// /// This API removes an executable name from a profile. /// /// Input to the session handle. /// Input profile handle. /// Input the executable name to be removed. public static void DeleteApplication( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, string applicationName) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, new UnicodeString(applicationName) ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API deletes a profile or sets it back to a predefined value. /// /// Input to the session handle. /// Input profile handle. public static void DeleteProfile(DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API deletes a setting or sets it back to predefined value. /// /// Input to the session handle. /// Input profile handle. /// Input settingId to be deleted. public static void DeleteProfileSetting( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, uint settingId) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, settingId ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API frees the allocated resources for the session handle. /// /// Input to the session handle. public static void DestroySession(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()(sessionHandle); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API enumerates all the applications in a given profile. /// /// Input to the session handle. /// Input profile handle. /// Instances of with all the attributes filled. [SuppressMessage("ReSharper", "EventExceptionNotDocumented")] public static IEnumerable EnumApplications( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle) { var maxApplicationsPerRequest = 8; var enumApplications = DelegateFactory.GetDelegate(); foreach (var acceptType in enumApplications.Accepts()) { var i = 0u; while (true) { var instances = acceptType.Instantiate().Repeat(maxApplicationsPerRequest); using (var applicationsReference = ValueTypeArray.FromArray(instances, acceptType)) { var count = (uint) instances.Length; var status = enumApplications( sessionHandle, profileHandle, i, ref count, applicationsReference ); if (status == Status.IncompatibleStructureVersion) { break; } if (status == Status.EndEnumeration) { yield break; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } foreach (var application in applicationsReference.ToArray( (int) count, acceptType)) { yield return application; i++; } if (count < maxApplicationsPerRequest) { yield break; } } } } throw new NVIDIANotSupportedException("This operation is not supported."); } /// /// This API enumerates all the Ids of all the settings recognized by NVAPI. /// /// An array of s filled with the settings identification numbers of available settings. public static uint[] EnumAvailableSettingIds() { var settingIdsCount = (uint) ushort.MaxValue; var settingIds = 0u.Repeat((int) settingIdsCount); using (var settingIdsArray = ValueTypeArray.FromArray(settingIds)) { var status = DelegateFactory.GetDelegate()( settingIdsArray, ref settingIdsCount ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return settingIdsArray.ToArray((int) settingIdsCount); } } /// /// This API enumerates all available setting values for a given setting. /// /// Input settingId. /// All available setting values. public static DRSSettingValues EnumAvailableSettingValues(uint settingId) { var settingValuesCount = (uint) DRSSettingValues.MaximumNumberOfValues; var settingValues = typeof(DRSSettingValues).Instantiate(); using (var settingValuesReference = ValueTypeReference.FromValueType(settingValues)) { var status = DelegateFactory.GetDelegate()( settingId, ref settingValuesCount, settingValuesReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status != Status.Ok) { throw new NVIDIAApiException(status); } return settingValuesReference.ToValueType(typeof(DRSSettingValues)); } } /// /// This API enumerates through all the profiles in the session. /// /// Input to the session handle. /// Instances of each representing a profile. public static IEnumerable EnumProfiles(DRSSessionHandle sessionHandle) { var i = 0u; while (true) { var status = DelegateFactory.GetDelegate()( sessionHandle, i, out var profileHandle ); if (status == Status.EndEnumeration) { yield break; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } yield return profileHandle; i++; } } /// /// This API enumerates all the settings of a given profile. /// /// Input to the session handle. /// Input profile handle. /// Instances of . [SuppressMessage("ReSharper", "EventExceptionNotDocumented")] public static IEnumerable EnumSettings( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle) { var maxSettingsPerRequest = 32; var enumSettings = DelegateFactory.GetDelegate(); var i = 0u; while (true) { var instances = typeof(DRSSettingV1).Instantiate().Repeat(maxSettingsPerRequest); using (var applicationsReference = ValueTypeArray.FromArray(instances)) { var count = (uint) instances.Length; var status = enumSettings( sessionHandle, profileHandle, i, ref count, applicationsReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status == Status.EndEnumeration) { yield break; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } foreach (var application in applicationsReference.ToArray( (int) count, typeof(DRSSettingV1)) ) { yield return application; i++; } if (count < maxSettingsPerRequest) { yield break; } } } } /// /// This API searches the application and the associated profile for the given application name. /// If a fully qualified path is provided, this function will always return the profile /// the driver will apply upon running the application (on the path provided). /// /// Input to the hSession handle /// Input appName. For best results, provide a fully qualified path of the type /// The profile handle of the profile that the found application belongs to. /// An instance of . [SuppressMessage("ReSharper", "EventExceptionNotDocumented")] public static IDRSApplication FindApplicationByName( DRSSessionHandle sessionHandle, string applicationName, out DRSProfileHandle? profileHandle) { var findApplicationByName = DelegateFactory.GetDelegate(); foreach (var acceptType in findApplicationByName.Accepts()) { var instance = acceptType.Instantiate(); using (var applicationReference = ValueTypeReference.FromValueType(instance, acceptType)) { var status = findApplicationByName( sessionHandle, new UnicodeString(applicationName), out var applicationProfileHandle, applicationReference ); if (status == Status.IncompatibleStructureVersion) { continue; } if (status == Status.ExecutableNotFound) { profileHandle = null; return null; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } profileHandle = applicationProfileHandle; return applicationReference.ToValueType(acceptType); } } throw new NVIDIANotSupportedException("This operation is not supported."); } /// /// This API finds a profile in the current session. /// /// Input to the session handle. /// Input profileName. /// The profile handle. public static DRSProfileHandle FindProfileByName(DRSSessionHandle sessionHandle, string profileName) { var status = DelegateFactory.GetDelegate()( sessionHandle, new UnicodeString(profileName), out var profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return profileHandle; } /// /// This API gets information about the given application. The input application name /// must match exactly what the Profile has stored for the application. /// This function is better used to retrieve application information from a previous /// enumeration. /// /// Input to the session handle. /// Input profile handle. /// Input application name. /// /// An instance of with all attributes filled if found; otherwise /// . /// [SuppressMessage("ReSharper", "EventExceptionNotDocumented")] public static IDRSApplication GetApplicationInfo( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, string applicationName) { var getApplicationInfo = DelegateFactory.GetDelegate(); foreach (var acceptType in getApplicationInfo.Accepts()) { var instance = acceptType.Instantiate(); using (var applicationReference = ValueTypeReference.FromValueType(instance, acceptType)) { var status = getApplicationInfo( sessionHandle, profileHandle, new UnicodeString(applicationName), applicationReference ); if (status == Status.IncompatibleStructureVersion) { continue; } if (status == Status.ExecutableNotFound) { return null; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } return applicationReference.ToValueType(acceptType); } } throw new NVIDIANotSupportedException("This operation is not supported."); } /// /// Returns the handle to the current global profile. /// /// Input to the session handle. /// Base profile handle. public static DRSProfileHandle GetBaseProfile(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle, out var profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return profileHandle; } /// /// This API returns the handle to the current global profile. /// /// Input to the session handle. /// Current global profile handle. public static DRSProfileHandle GetCurrentGlobalProfile(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle, out var profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return profileHandle; } /// /// This API obtains the number of profiles in the current session object. /// /// Input to the session handle. /// Number of profiles in the current session. public static int GetNumberOfProfiles(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle, out var profileCount ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return (int) profileCount; } /// /// This API gets information about the given profile. /// /// Input to the session handle. /// Input profile handle. /// An instance of with all attributes filled. public static DRSProfileV1 GetProfileInfo(DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle) { var profile = typeof(DRSProfileV1).Instantiate(); using (var profileReference = ValueTypeReference.FromValueType(profile)) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, profileReference ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return profileReference.ToValueType().GetValueOrDefault(); } } /// /// This API gets information about the given setting. /// /// Input to the session handle. /// Input profile handle. /// Input settingId. /// An instance of describing the setting if found; otherwise . public static DRSSettingV1? GetSetting( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, uint settingId) { var instance = typeof(DRSSettingV1).Instantiate(); using (var settingReference = ValueTypeReference.FromValueType(instance, typeof(DRSSettingV1))) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, settingId, settingReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status == Status.SettingNotFound) { return null; } if (status != Status.Ok) { throw new NVIDIAApiException(status); } return settingReference.ToValueType(typeof(DRSSettingV1)); } } /// /// This API gets the binary identification number of a setting given the setting name. /// /// Input Unicode settingName. /// The corresponding settingId. public static uint GetSettingIdFromName(string settingName) { var status = DelegateFactory.GetDelegate()( new UnicodeString(settingName), out var settingId ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return settingId; } /// /// This API gets the setting name given the binary identification number. /// /// Input settingId. /// Corresponding settingName. public static string GetSettingNameFromId(uint settingId) { var status = DelegateFactory.GetDelegate()( settingId, out var settingName ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } return settingName.Value; } /// /// This API loads and parses the settings data. /// /// Input to the session handle. public static void LoadSettings(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()(sessionHandle); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API loads settings from the given file path. /// /// Input to the session handle /// Binary full file path. public static void LoadSettings(DRSSessionHandle sessionHandle, string fileName) { var unicodeFileName = new UnicodeString(fileName); var status = DelegateFactory.GetDelegate()( sessionHandle, unicodeFileName ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API restores the whole system to predefined(default) values. /// /// Input to the session handle. public static void RestoreDefaults(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API restores the given profile to predefined(default) values. /// Any and all user specified modifications will be removed. /// If the whole profile was set by the user, the profile will be removed. /// /// Input to the session handle. /// Input profile handle. public static void RestoreDefaults( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API restores the given profile setting to predefined(default) values. /// /// Input to the session handle. /// Input profile handle. /// Input settingId. public static void RestoreDefaults( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, uint settingId) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, settingId ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API saves the settings data to the system. /// /// Input to the session handle. public static void SaveSettings(DRSSessionHandle sessionHandle) { var status = DelegateFactory.GetDelegate()(sessionHandle); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API saves settings to the given file path. /// /// Input to the session handle. /// Binary full file path. public static void SaveSettings(DRSSessionHandle sessionHandle, string fileName) { var status = DelegateFactory.GetDelegate()( sessionHandle, new UnicodeString(fileName) ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// This API sets the current global profile in the driver. /// /// Input to the session handle. /// Input the new current global profile name. public static void SetCurrentGlobalProfile(DRSSessionHandle sessionHandle, string profileName) { var status = DelegateFactory.GetDelegate()( sessionHandle, new UnicodeString(profileName) ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } /// /// Specifies flags for a given profile. Currently only the GPUSupport is /// used to update the profile. Neither the name, number of settings or applications /// or other profile information can be changed with this function. /// /// Input to the session handle. /// Input profile handle. /// Input the new profile info. public static void SetProfileInfo( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, DRSProfileV1 profile) { using (var profileReference = ValueTypeReference.FromValueType(profile)) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, profileReference ); if (status != Status.Ok) { throw new NVIDIAApiException(status); } } } /// /// This API adds/modifies a setting to a profile. /// /// Input to the session handle. /// Input profile handle. /// /// An instance of containing the setting identification number and new /// value for the setting. /// public static void SetSetting( DRSSessionHandle sessionHandle, DRSProfileHandle profileHandle, DRSSettingV1 setting) { using (var settingReference = ValueTypeReference.FromValueType(setting, setting.GetType())) { var status = DelegateFactory.GetDelegate()( sessionHandle, profileHandle, settingReference ); if (status == Status.IncompatibleStructureVersion) { throw new NVIDIANotSupportedException("This operation is not supported."); } if (status != Status.Ok) { throw new NVIDIAApiException(status); } } } } }