using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.Wooting.Enum;
using RGB.NET.Devices.Wooting.Generic;
using RGB.NET.Devices.Wooting.Keyboard;
using RGB.NET.Devices.Wooting.Native;
namespace RGB.NET.Devices.Wooting
{
///
///
/// Represents a device provider responsible for Wooting devices.
///
public class WootingDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static WootingDeviceProvider _instance;
///
/// Gets the singleton instance.
///
public static WootingDeviceProvider Instance => _instance ?? new WootingDeviceProvider();
///
/// 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/wooting-rgb-sdk.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/wooting-rgb-sdk64.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 => _WootingSDK.LoadedArchitecture;
///
///
/// Gets whether the application has exclusive access to the SDK or not.
///
public bool HasExclusiveAccess => false;
///
public IEnumerable Devices { get; private set; }
///
/// The used to trigger the updates for cooler master devices.
///
public DeviceUpdateTrigger UpdateTrigger { get; private set; }
#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 WootingDeviceProvider()
{
if (_instance != null)
throw new InvalidOperationException($"There can be only one instance of type {nameof(WootingDeviceProvider)}");
_instance = this;
UpdateTrigger = new DeviceUpdateTrigger();
}
#endregion
#region Methods
///
/// Thrown if the SDK failed to initialize
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false,
bool throwExceptions = false)
{
IsInitialized = false;
try
{
UpdateTrigger?.Stop();
_WootingSDK.Reload();
IList devices = new List();
if (_WootingSDK.KeyboardConnected())
{
_WootingDeviceInfo nativeDeviceInfo = (_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo));
IWootingRGBDevice device;
// TODO: Find an accurate way to determine physical and logical layouts
if (nativeDeviceInfo.Model == "Wooting two")
{
device = new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingTwo,
WootingPhysicalKeyboardLayout.US,
CultureHelper.GetCurrentCulture()));
}
else if (nativeDeviceInfo.Model == "Wooting one")
{
device = new WootingKeyboardRGBDevice(new WootingKeyboardRGBDeviceInfo(WootingDevicesIndexes.WootingOne,
WootingPhysicalKeyboardLayout.US,
CultureHelper.GetCurrentCulture()));
}
else
{
throw new RGBDeviceException("No supported Wooting keyboard connected");
}
device.Initialize(UpdateTrigger);
devices.Add(device);
}
UpdateTrigger?.Start();
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
}
catch
{
if (throwExceptions) throw;
return false;
}
return true;
}
///
public void ResetDevices()
{ }
///
public void Dispose()
{
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _WootingSDK.Reset(); }
catch { /* Unlucky.. */ }
try { _WootingSDK.UnloadWootingSDK(); }
catch { /* at least we tried */ }
}
#endregion
}
}