// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
///
///
/// Represents a device provider responsible for Novation devices.
///
public class NovationDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static NovationDeviceProvider? _instance;
///
/// Gets the singleton instance.
///
public static NovationDeviceProvider Instance => _instance ?? new NovationDeviceProvider();
///
///
/// Indicates if the SDK is initialized and ready to use.
///
public bool IsInitialized { get; private set; }
///
public IEnumerable Devices { get; private set; } = Enumerable.Empty();
///
/// The used to trigger the updates for novation devices.
///
public DeviceUpdateTrigger UpdateTrigger { get; }
#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()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(NovationDeviceProvider)}");
_instance = this;
UpdateTrigger = new DeviceUpdateTrigger();
}
#endregion
#region Methods
///
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
IsInitialized = false;
try
{
UpdateTrigger.Stop();
IList devices = new List();
if (loadFilter.HasFlag(RGBDeviceType.LedMatrix))
for (int index = 0; index < OutputDeviceBase.DeviceCount; index++)
{
try
{
MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index);
if (outCaps.name == null) continue;
NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices))
.Cast()
.FirstOrDefault(x => x.GetDeviceId()?.ToUpperInvariant().Contains(outCaps.name.ToUpperInvariant()) ?? false);
if (deviceId == null) continue;
NovationColorCapabilities colorCapability = deviceId.GetColorCapability();
if (colorCapability == NovationColorCapabilities.None) continue;
INovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, colorCapability, deviceId.GetLedIdMapping()));
device.Initialize(UpdateTrigger);
devices.Add(device);
}
catch { if (throwExceptions) throw; }
}
UpdateTrigger.Start();
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
}
catch
{
if (throwExceptions)
throw;
return false;
}
return true;
}
///
public void ResetDevices()
{
foreach (IRGBDevice device in Devices)
{
NovationLaunchpadRGBDevice? novationDevice = device as NovationLaunchpadRGBDevice;
novationDevice?.Reset();
}
}
///
public void Dispose()
{
try { UpdateTrigger.Dispose(); }
catch { /* at least we tried */ }
}
#endregion
}
}