// 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; } /// /// /// Gets whether the application has exclusive access to the SDK or not. /// public bool HasExclusiveAccess => false; /// public IEnumerable Devices { get; private set; } public UpdateTrigger 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. private NovationDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(NovationDeviceProvider)}"); _instance = this; UpdateTrigger = new UpdateTrigger(); } #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 => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase)); if (deviceId == null) continue; INovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, deviceId.GetColorCapability())); 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() { } #endregion } }