// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Devices.CoolerMaster.Helper;
using RGB.NET.Devices.CoolerMaster.Native;
namespace RGB.NET.Devices.CoolerMaster;
///
///
/// Represents a device provider responsible for Cooler Master devices.
///
public sealed class CoolerMasterDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly object _lock = new();
private static CoolerMasterDeviceProvider? _instance;
///
/// Gets the singleton instance.
///
public static CoolerMasterDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new CoolerMasterDeviceProvider();
}
}
///
/// 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; } = ["x86/CMSDK.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; } = ["x64/CMSDK.dll"];
#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 CoolerMasterDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CoolerMasterDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
///
protected override void InitializeSDK()
{
_CoolerMasterSDK.Reload();
if (_CoolerMasterSDK.GetSDKVersion() <= 0) Throw(new RGBDeviceException("Failed to initialize CoolerMaster-SDK"), true);
}
///
protected override IEnumerable LoadDevices()
{
foreach (CoolerMasterDevicesIndexes index in Enum.GetValues(typeof(CoolerMasterDevicesIndexes)))
{
RGBDeviceType deviceType = index.GetDeviceType();
if (deviceType == RGBDeviceType.None) continue;
if (_CoolerMasterSDK.IsDevicePlugged(index))
{
if (!_CoolerMasterSDK.EnableLedControl(true, index))
Throw(new RGBDeviceException("Failed to enable LED control for device " + index));
else
{
switch (deviceType)
{
case RGBDeviceType.Keyboard:
yield return new CoolerMasterKeyboardRGBDevice(new CoolerMasterKeyboardRGBDeviceInfo(index, _CoolerMasterSDK.GetDeviceLayout(index)), GetUpdateTrigger());
break;
case RGBDeviceType.Mouse:
yield return new CoolerMasterMouseRGBDevice(new CoolerMasterMouseRGBDeviceInfo(index), GetUpdateTrigger());
break;
default:
Throw(new RGBDeviceException("Unknown Device-Type"));
break;
}
}
}
}
}
///
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
try { _CoolerMasterSDK.Reload(); }
catch { /* Unlucky.. */ }
_instance = null;
}
}
#endregion
}