using System; using System.Collections.Generic; using System.Collections.ObjectModel; using RGB.NET.Core; using RGB.NET.Devices.SteelSeries.API; using RGB.NET.Devices.SteelSeries.HID; namespace RGB.NET.Devices.SteelSeries { /// /// /// Represents a device provider responsible for SteelSeries- devices. /// public class SteelSeriesDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static SteelSeriesDeviceProvider _instance; /// /// Gets the singleton instance. /// public static SteelSeriesDeviceProvider Instance => _instance ?? new SteelSeriesDeviceProvider(); /// /// /// 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; } /// /// The used to trigger the updates for SteelSeries devices. /// public SteelSeriesDeviceUpdateTrigger 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. public SteelSeriesDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SteelSeriesDeviceProvider)}"); _instance = this; UpdateTrigger = new SteelSeriesDeviceUpdateTrigger(); } #endregion #region Methods /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { try { IsInitialized = false; UpdateTrigger?.Stop(); if (!SteelSeriesSDK.IsInitialized) SteelSeriesSDK.Initialize(); IList devices = new List(); DeviceChecker.LoadDeviceList(loadFilter); try { foreach ((string model, RGBDeviceType deviceType, int _, SteelSeriesDeviceType steelSeriesDeviceType, string imageLayout, string layoutPath, Dictionary ledMapping) in DeviceChecker.ConnectedDevices) { ISteelSeriesRGBDevice device = new SteelSeriesRGBDevice(new SteelSeriesRGBDeviceInfo(deviceType, model, steelSeriesDeviceType, imageLayout, layoutPath)); SteelSeriesDeviceUpdateQueue updateQueue = new SteelSeriesDeviceUpdateQueue(UpdateTrigger, steelSeriesDeviceType.GetAPIName()); device.Initialize(updateQueue, ledMapping); devices.Add(device); } } catch { if (throwExceptions) throw; } UpdateTrigger?.Start(); Devices = new ReadOnlyCollection(devices); IsInitialized = true; } catch { IsInitialized = false; if (throwExceptions) throw; return false; } return true; } /// public void ResetDevices() { if (IsInitialized) try { SteelSeriesSDK.ResetLeds(); } catch {/* shit happens */} } /// public void Dispose() { try { UpdateTrigger?.Dispose(); } catch { /* at least we tried */ } try { SteelSeriesSDK.Dispose(); } catch { /* shit happens */ } } #endregion } }