// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation;
///
///
/// Represents a device provider responsible for Novation devices.
///
public sealed class NovationDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly Lock _lock = new();
private static NovationDeviceProvider? _instance;
///
/// Gets the singleton instance.
///
public static NovationDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new NovationDeviceProvider();
}
}
#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.
private NovationDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(NovationDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
///
protected override void InitializeSDK() { }
///
protected override IEnumerable LoadDevices()
{
for (int index = 0; index < OutputDeviceBase.DeviceCount; index++)
{
MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index);
if (outCaps.name == null) continue;
NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices))
.Cast()
.Where(x => x.GetDeviceId() != null)
.FirstOrDefault(x => outCaps.name.Contains(x.GetDeviceId()!, StringComparison.InvariantCultureIgnoreCase));
if (deviceId == null) continue;
NovationColorCapabilities colorCapability = deviceId.GetColorCapability();
if (colorCapability == NovationColorCapabilities.None) continue;
yield return new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, colorCapability, deviceId.GetLedIdMapping()), GetUpdateTrigger());
}
}
///
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
_instance = null;
}
}
#endregion
}