// 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.CoolerMaster.Helper; using RGB.NET.Devices.CoolerMaster.Native; namespace RGB.NET.Devices.CoolerMaster { /// /// /// Represents a device provider responsible for Cooler Master devices. /// public class CoolerMasterDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static CoolerMasterDeviceProvider _instance; /// /// Gets the singleton instance. /// public static CoolerMasterDeviceProvider Instance => _instance ?? new CoolerMasterDeviceProvider(); /// /// 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/CMSDK.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/CMSDK.dll" }; /// /// /// Indicates if the SDK is initialized and ready to use. /// public bool IsInitialized { get; private set; } /// /// Gets the loaded architecture (x64/x86). /// public string LoadedArchitecture => _CoolerMasterSDK.LoadedArchitecture; /// /// /// Gets whether the application has exclusive access to the SDK or not. /// public bool HasExclusiveAccess { get; private set; } /// public IEnumerable Devices { get; private set; } /// /// Gets or sets a function to get the culture for a specific device. /// // ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global public Func GetCulture { get; set; } = CultureHelper.GetCurrentCulture; 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. public CoolerMasterDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CoolerMasterDeviceProvider)}"); _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(); _CoolerMasterSDK.Reload(); if (_CoolerMasterSDK.GetSDKVersion() <= 0) return false; IList devices = new List(); foreach (CoolerMasterDevicesIndexes index in Enum.GetValues(typeof(CoolerMasterDevicesIndexes))) { try { _CoolerMasterSDK.SetControlDevice(index); if (_CoolerMasterSDK.IsDevicePlugged()) { RGBDeviceType deviceType = index.GetDeviceType(); if (!loadFilter.HasFlag(deviceType)) continue; ICoolerMasterRGBDevice device; switch (deviceType) { case RGBDeviceType.Keyboard: CoolerMasterPhysicalKeyboardLayout physicalLayout = _CoolerMasterSDK.GetDeviceLayout(); device = new CoolerMasterKeyboardRGBDevice(new CoolerMasterKeyboardRGBDeviceInfo(index, physicalLayout, GetCulture())); break; case RGBDeviceType.Mouse: device = new CoolerMasterMouseRGBDevice(new CoolerMasterMouseRGBDeviceInfo(index)); break; default: if (throwExceptions) throw new RGBDeviceException("Unknown Device-Type"); else continue; } _CoolerMasterSDK.EnableLedControl(true); 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() { if (IsInitialized) foreach (IRGBDevice device in Devices) { try { CoolerMasterRGBDeviceInfo deviceInfo = (CoolerMasterRGBDeviceInfo)device.DeviceInfo; _CoolerMasterSDK.SetControlDevice(deviceInfo.DeviceIndex); _CoolerMasterSDK.EnableLedControl(false); _CoolerMasterSDK.EnableLedControl(true); } catch {/* shit happens */} } } /// public void Dispose() { if (IsInitialized) foreach (IRGBDevice device in Devices) { try { CoolerMasterRGBDeviceInfo deviceInfo = (CoolerMasterRGBDeviceInfo)device.DeviceInfo; _CoolerMasterSDK.SetControlDevice(deviceInfo.DeviceIndex); _CoolerMasterSDK.EnableLedControl(false); } catch {/* shit happens */} } } #endregion } }