using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using RGB.NET.Core;
using RGB.NET.Devices.Roccat.Native;
namespace RGB.NET.Devices.Roccat
{
///
///
/// Represents a device provider responsible for roccat (TalkFX)devices.
///
public class RoccatDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static RoccatDeviceProvider _instance;
///
/// Gets the singleton instance.
///
public static RoccatDeviceProvider Instance => _instance ?? new RoccatDeviceProvider();
///
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
/// The first match will be used.
///
public static List PossibleX86NativePaths { get; } = new List { "x86/RoccatTalkSDKWrapper.dll" };
///
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications.
/// The first match will be used.
///
public static List PossibleX64NativePaths { get; } = new List { "x64/RoccatTalkSDKWrapper.dll" };
///
///
/// Indicates if the SDK is initialized and ready to use.
///
public bool IsInitialized { get; private set; }
///
/// Gets the loaded architecture (x64/x86).
///
public string LoadedArchitecture => _RoccatSDK.LoadedArchitecture;
///
///
/// Gets whether the application has exclusive access to the SDK or not.
///
public bool HasExclusiveAccess { get; private set; }
///
public IEnumerable Devices { get; private set; }
private IntPtr _sdkHandle;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// Thrown if this constructor is called even if there is already an instance of this class.
public RoccatDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(RoccatDeviceProvider)}");
_instance = this;
}
#endregion
#region Methods
///
/// Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
IsInitialized = false;
try
{
_sdkHandle = _RoccatSDK.InitSDK();
IList devices = new List();
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
}
catch
{
if (throwExceptions) throw;
return false;
}
return true;
}
///
public void ResetDevices()
{ }
///
public void Dispose()
{
if (_sdkHandle != IntPtr.Zero)
{
try { _RoccatSDK.RestoreLedRGB(_sdkHandle); }
catch { /* We tried our best */}
try { _RoccatSDK.SetRyosKbSDKMode(_sdkHandle, false); }
catch { /* We tried our best */}
try { _RoccatSDK.UnloadSDK(_sdkHandle); }
catch { /* We tried our best */}
}
try { _RoccatSDK.UnloadRoccatSDK(); }
catch { /* at least we tried */ }
}
#endregion
}
}