// ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using RGB.NET.Core; using RGB.NET.Devices.Logitech.HID; using RGB.NET.Devices.Logitech.Native; namespace RGB.NET.Devices.Logitech { /// /// /// Represents a device provider responsible for logitech devices. /// public class LogitechDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static LogitechDeviceProvider _instance; /// /// Gets the singleton instance. /// public static LogitechDeviceProvider Instance => _instance ?? new LogitechDeviceProvider(); /// /// 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; } = new List { "x86/LogitechLedEnginesWrapper.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; } = new List { "x64/LogitechLedEnginesWrapper.dll" }; /// public bool IsInitialized { get; private set; } /// /// Gets the loaded architecture (x64/x86). /// public string LoadedArchitecture => _LogitechGSDK.LoadedArchitecture; /// public IEnumerable Devices { get; private set; } /// public bool HasExclusiveAccess => false; // Exclusive access isn't possible for logitech devices. /// /// Gets or sets a function to get the culture for a specific device. /// public Func GetCulture { get; set; } = CultureHelper.GetCurrentCulture; /// /// The used to trigger the updates for logitech devices. /// public DeviceUpdateTrigger UpdateTrigger { get; private set; } // ReSharper disable once CollectionNeverQueried.Local - for now this is just to make sure they're never collected private readonly Dictionary _zoneUpdateQueues = new Dictionary(); private LogitechPerDeviceUpdateQueue _perDeviceUpdateQueue; private LogitechPerKeyUpdateQueue _perKeyUpdateQueue; #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 LogitechDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(LogitechDeviceProvider)}"); _instance = this; UpdateTrigger = new DeviceUpdateTrigger(); _perDeviceUpdateQueue = new LogitechPerDeviceUpdateQueue(UpdateTrigger); _perKeyUpdateQueue = new LogitechPerKeyUpdateQueue(UpdateTrigger); } #endregion #region Methods /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { try { if (IsInitialized) _LogitechGSDK.LogiLedRestoreLighting(); } catch { /* At least we tried ... */ } IsInitialized = false; try { UpdateTrigger?.Stop(); _LogitechGSDK.Reload(); if (!_LogitechGSDK.LogiLedInit()) return false; _LogitechGSDK.LogiLedSaveCurrentLighting(); IList devices = new List(); DeviceChecker.LoadDeviceList(); try { if (DeviceChecker.IsPerDeviceDeviceConnected) { (string model, RGBDeviceType deviceType, int _, int _, string imageLayout, string layoutPath) = DeviceChecker.PerKeyDeviceData; if (loadFilter.HasFlag(deviceType)) //TODO DarthAffe 07.12.2017: Check if it's worth to try another device if the one returned doesn't match the filter { ILogitechRGBDevice device = new LogitechPerKeyRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.PerKeyRGB, 0, imageLayout, layoutPath)); device.Initialize(_perKeyUpdateQueue); devices.Add(device); } } } catch { if (throwExceptions) throw; } try { if (DeviceChecker.IsPerDeviceDeviceConnected) { (string model, RGBDeviceType deviceType, int _, int _, string imageLayout, string layoutPath) = DeviceChecker.PerDeviceDeviceData; if (loadFilter.HasFlag(deviceType)) //TODO DarthAffe 07.12.2017: Check if it's worth to try another device if the one returned doesn't match the filter { ILogitechRGBDevice device = new LogitechPerDeviceRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, 0, imageLayout, layoutPath)); device.Initialize(_perDeviceUpdateQueue); devices.Add(device); } } } catch { if (throwExceptions) throw; } try { if (DeviceChecker.IsZoneDeviceConnected) { foreach ((string model, RGBDeviceType deviceType, int _, int zones, string imageLayout, string layoutPath) in DeviceChecker.ZoneDeviceData) try { if (loadFilter.HasFlag(deviceType)) { LogitechZoneUpdateQueue updateQueue = new LogitechZoneUpdateQueue(UpdateTrigger, deviceType); ILogitechRGBDevice device = new LogitechZoneRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, zones, imageLayout, layoutPath)); device.Initialize(updateQueue); devices.Add(device); _zoneUpdateQueues.Add(deviceType, updateQueue); } } catch { if (throwExceptions) throw; } } } catch { if (throwExceptions) throw; } UpdateTrigger?.Start(); Devices = new ReadOnlyCollection(devices); IsInitialized = true; } catch { if (throwExceptions) throw; return false; } return true; } /// public void ResetDevices() => _LogitechGSDK.LogiLedRestoreLighting(); /// public void Dispose() => _LogitechGSDK.LogiLedRestoreLighting(); #endregion } }