diff --git a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs index 00123fb..475ab92 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs @@ -31,10 +31,11 @@ namespace RGB.NET.Core /// /// Initializes the if not already happened or reloads it if it is already initialized. /// + /// Specifies which types of devices to load. /// Specifies whether the application should request exclusive access of possible or not. /// Specifies whether exception during the initialization sequence should be thrown or not. /// - bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false); + bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false); /// /// Resets all handled back top default. diff --git a/RGB.NET.Core/Devices/RGBDeviceType.cs b/RGB.NET.Core/Devices/RGBDeviceType.cs index f2bd880..8316899 100644 --- a/RGB.NET.Core/Devices/RGBDeviceType.cs +++ b/RGB.NET.Core/Devices/RGBDeviceType.cs @@ -8,10 +8,15 @@ namespace RGB.NET.Core [Flags] public enum RGBDeviceType { + /// + /// Represents nothing. + /// + None = 0, + /// /// Represents a device where the type is not known or not present in the list. /// - Unknown = 0, + Unknown = -1, /// /// Represents a keyboard. @@ -62,5 +67,10 @@ namespace RGB.NET.Core /// Represents a headset stand /// HeadsetStand = 1 << 9, + + /// + /// Represents all devices + /// + All = 0xFFFFFFF } } diff --git a/RGB.NET.Core/RGBSurfaceDeviceLoader.cs b/RGB.NET.Core/RGBSurfaceDeviceLoader.cs index a072ec9..e868175 100644 --- a/RGB.NET.Core/RGBSurfaceDeviceLoader.cs +++ b/RGB.NET.Core/RGBSurfaceDeviceLoader.cs @@ -13,13 +13,15 @@ namespace RGB.NET.Core /// Loads all devices the given is able to provide. /// /// The to load the devices from. + /// Specifies which types of devices to load. + /// Specifies whether the application should request exclusive access of possible or not. /// Specifies whether exception during the initialization sequence should be thrown or not. - public void LoadDevices(IRGBDeviceProvider deviceProvider, bool throwExceptions = false) + public void LoadDevices(IRGBDeviceProvider deviceProvider, RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { if (_deviceProvider.Contains(deviceProvider) || _deviceProvider.Any(x => x.GetType() == deviceProvider.GetType())) return; List addedDevices = new List(); - if (deviceProvider.IsInitialized || deviceProvider.Initialize(throwExceptions)) + if (deviceProvider.IsInitialized || deviceProvider.Initialize(loadFilter, exclusiveAccessIfPossible, throwExceptions)) { _deviceProvider.Add(deviceProvider); diff --git a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs index 550e803..379d0ec 100644 --- a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs +++ b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs @@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Asus #region Methods /// - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; @@ -93,31 +93,32 @@ namespace RGB.NET.Devices.Asus #region Mainboard - try - { - //TODO DarthAffe 26.11.2017: This is not a fix! There might really be a second controller on the mainboard, but for now this should prevent the random crash for some guys. - // DarthAffe 26.11.2017: https://rog.asus.com/forum/showthread.php?97754-Access-Violation-Wrong-EnumerateMB-Result&p=688901#post688901 - int mainboardCount = Math.Max(1, _AsusSDK.EnumerateMbController(IntPtr.Zero, 0)); - if (mainboardCount > 0) + if (loadFilter.HasFlag(RGBDeviceType.Mainboard)) + try { - IntPtr mainboardHandles = Marshal.AllocHGlobal(mainboardCount * IntPtr.Size); - _AsusSDK.EnumerateMbController(mainboardHandles, mainboardCount); - - for (int i = 0; i < mainboardCount; i++) + //TODO DarthAffe 26.11.2017: This is not a fix! There might really be a second controller on the mainboard, but for now this should prevent the random crash for some guys. + // DarthAffe 26.11.2017: https://rog.asus.com/forum/showthread.php?97754-Access-Violation-Wrong-EnumerateMB-Result&p=688901#post688901 + int mainboardCount = Math.Max(1, _AsusSDK.EnumerateMbController(IntPtr.Zero, 0)); + if (mainboardCount > 0) { - try + IntPtr mainboardHandles = Marshal.AllocHGlobal(mainboardCount * IntPtr.Size); + _AsusSDK.EnumerateMbController(mainboardHandles, mainboardCount); + + for (int i = 0; i < mainboardCount; i++) { - IntPtr handle = Marshal.ReadIntPtr(mainboardHandles, i); - _AsusSDK.SetMbMode(handle, 1); - AsusMainboardRGBDevice device = new AsusMainboardRGBDevice(new AsusMainboardRGBDeviceInfo(RGBDeviceType.Mainboard, handle)); - device.Initialize(); - devices.Add(device); + try + { + IntPtr handle = Marshal.ReadIntPtr(mainboardHandles, i); + _AsusSDK.SetMbMode(handle, 1); + AsusMainboardRGBDevice device = new AsusMainboardRGBDevice(new AsusMainboardRGBDeviceInfo(RGBDeviceType.Mainboard, handle)); + device.Initialize(); + devices.Add(device); + } + catch { if (throwExceptions) throw; } } - catch { if (throwExceptions) throw; } } } - } - catch { if (throwExceptions) throw; } + catch { if (throwExceptions) throw; } #endregion @@ -125,29 +126,30 @@ namespace RGB.NET.Devices.Asus //TODO DarthAffe 21.10.2017: This somehow returns non-existant gpus (at least for me) which cause huge lags (if a real asus-ready gpu is connected this doesn't happen) - try - { - int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0); - if (graphicCardCount > 0) + if (loadFilter.HasFlag(RGBDeviceType.GraphicsCard)) + try { - IntPtr grapicsCardHandles = Marshal.AllocHGlobal(graphicCardCount * IntPtr.Size); - _AsusSDK.EnumerateGPU(grapicsCardHandles, graphicCardCount); - - for (int i = 0; i < graphicCardCount; i++) + int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0); + if (graphicCardCount > 0) { - try + IntPtr grapicsCardHandles = Marshal.AllocHGlobal(graphicCardCount * IntPtr.Size); + _AsusSDK.EnumerateGPU(grapicsCardHandles, graphicCardCount); + + for (int i = 0; i < graphicCardCount; i++) { - IntPtr handle = Marshal.ReadIntPtr(grapicsCardHandles, i); - _AsusSDK.SetGPUMode(handle, 1); - AsusGraphicsCardRGBDevice device = new AsusGraphicsCardRGBDevice(new AsusGraphicsCardRGBDeviceInfo(RGBDeviceType.GraphicsCard, handle)); - device.Initialize(); - devices.Add(device); + try + { + IntPtr handle = Marshal.ReadIntPtr(grapicsCardHandles, i); + _AsusSDK.SetGPUMode(handle, 1); + AsusGraphicsCardRGBDevice device = new AsusGraphicsCardRGBDevice(new AsusGraphicsCardRGBDeviceInfo(RGBDeviceType.GraphicsCard, handle)); + device.Initialize(); + devices.Add(device); + } + catch { if (throwExceptions) throw; } } - catch { if (throwExceptions) throw; } } } - } - catch { if (throwExceptions) throw; } + catch { if (throwExceptions) throw; } #endregion @@ -182,35 +184,37 @@ namespace RGB.NET.Devices.Asus #region Keyboard - try - { - IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size); - if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle)) + if (loadFilter.HasFlag(RGBDeviceType.Keyboard)) + try { - _AsusSDK.SetClaymoreKeyboardMode(keyboardHandle, 1); - AsusKeyboardRGBDevice device = new AsusKeyboardRGBDevice(new AsusKeyboardRGBDeviceInfo(RGBDeviceType.Keyboard, keyboardHandle, GetCulture())); - device.Initialize(); - devices.Add(device); + IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size); + if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle)) + { + _AsusSDK.SetClaymoreKeyboardMode(keyboardHandle, 1); + AsusKeyboardRGBDevice device = new AsusKeyboardRGBDevice(new AsusKeyboardRGBDeviceInfo(RGBDeviceType.Keyboard, keyboardHandle, GetCulture())); + device.Initialize(); + devices.Add(device); + } } - } - catch { if (throwExceptions) throw; } + catch { if (throwExceptions) throw; } #endregion #region Mouse - try - { - IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size); - if (_AsusSDK.CreateRogMouse(mouseHandle)) + if (loadFilter.HasFlag(RGBDeviceType.Mouse)) + try { - _AsusSDK.SetRogMouseMode(mouseHandle, 1); - AsusMouseRGBDevice device = new AsusMouseRGBDevice(new AsusMouseRGBDeviceInfo(RGBDeviceType.Mouse, mouseHandle)); - device.Initialize(); - devices.Add(device); + IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size); + if (_AsusSDK.CreateRogMouse(mouseHandle)) + { + _AsusSDK.SetRogMouseMode(mouseHandle, 1); + AsusMouseRGBDevice device = new AsusMouseRGBDevice(new AsusMouseRGBDeviceInfo(RGBDeviceType.Mouse, mouseHandle)); + device.Initialize(); + devices.Add(device); + } } - } - catch { if (throwExceptions) throw; } + catch { if (throwExceptions) throw; } #endregion diff --git a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs index b6151c1..b6dfdfd 100644 --- a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs +++ b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs @@ -82,7 +82,7 @@ namespace RGB.NET.Devices.CoolerMaster #region Methods /// - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; @@ -101,8 +101,11 @@ namespace RGB.NET.Devices.CoolerMaster _CoolerMasterSDK.SetControlDevice(index); if (_CoolerMasterSDK.IsDevicePlugged()) { + RGBDeviceType deviceType = index.GetDeviceType(); + if (!loadFilter.HasFlag(deviceType)) continue; + ICoolerMasterRGBDevice device; - switch (index.GetDeviceType()) + switch (deviceType) { case RGBDeviceType.Keyboard: CoolerMasterPhysicalKeyboardLayout physicalLayout = _CoolerMasterSDK.GetDeviceLayout(); diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index 74998c0..ab2ad7b 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -89,7 +89,7 @@ namespace RGB.NET.Devices.Corsair /// /// Thrown if the SDK is already initialized or if the SDK is not compatible to CUE. /// Thrown if the CUE-SDK provides an error. - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; @@ -129,34 +129,9 @@ namespace RGB.NET.Devices.Corsair if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) continue; // Everything that doesn't support lighting control is useless - ICorsairRGBDevice device; - switch (info.CorsairDeviceType) - { - case CorsairDeviceType.Keyboard: - device = new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo)); - break; + ICorsairRGBDevice device = GetRGBDevice(info, i, nativeDeviceInfo); + if ((device == null) || !loadFilter.HasFlag(device.DeviceInfo.DeviceType)) continue; - case CorsairDeviceType.Mouse: - device = new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo)); - break; - - case CorsairDeviceType.Headset: - device = new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo)); - break; - - case CorsairDeviceType.Mousepad: - device = new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo)); - break; - - case CorsairDeviceType.HeadsetStand: - device = new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo)); - break; - - // ReSharper disable once RedundantCaseLabel - case CorsairDeviceType.Unknown: - default: - throw new RGBDeviceException("Unknown Device-Type"); - } device.Initialize(); AddSpecialParts(device); @@ -182,6 +157,32 @@ namespace RGB.NET.Devices.Corsair return true; } + private static ICorsairRGBDevice GetRGBDevice(CorsairRGBDeviceInfo info, int i, _CorsairDeviceInfo nativeDeviceInfo) + { + switch (info.CorsairDeviceType) + { + case CorsairDeviceType.Keyboard: + return new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo)); + + case CorsairDeviceType.Mouse: + return new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo)); + + case CorsairDeviceType.Headset: + return new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo)); + + case CorsairDeviceType.Mousepad: + return new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo)); + + case CorsairDeviceType.HeadsetStand: + return new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo)); + + // ReSharper disable once RedundantCaseLabel + case CorsairDeviceType.Unknown: + default: + throw new RGBDeviceException("Unknown Device-Type"); + } + } + private void AddSpecialParts(ICorsairRGBDevice device) { if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase)) diff --git a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs index 295f2c9..3d85ff8 100644 --- a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs +++ b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs @@ -75,7 +75,7 @@ namespace RGB.NET.Devices.Logitech #region Methods /// - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { try { @@ -101,9 +101,12 @@ namespace RGB.NET.Devices.Logitech if (DeviceChecker.IsPerKeyDeviceConnected) { (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerKeyDeviceData; - ILogitechRGBDevice device = new LogitechPerKeyRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.PerKeyRGB, imageBasePath, imageLayout, layoutPath)); - device.Initialize(); - devices.Add(device); + 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, imageBasePath, imageLayout, layoutPath)); + device.Initialize(); + devices.Add(device); + } } } catch { if (throwExceptions) throw; } @@ -113,9 +116,12 @@ namespace RGB.NET.Devices.Logitech if (DeviceChecker.IsPerDeviceDeviceConnected) { (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerDeviceDeviceData; - ILogitechRGBDevice device = new LogitechPerDeviceRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, imageBasePath, imageLayout, layoutPath)); - device.Initialize(); - devices.Add(device); + 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, imageBasePath, imageLayout, layoutPath)); + device.Initialize(); + devices.Add(device); + } } } catch { if (throwExceptions) throw; } diff --git a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs index 36e2eff..0523b8d 100644 --- a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs +++ b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs @@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Msi #region Methods /// - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; diff --git a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs index b560056..d7fe3e0 100644 --- a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs +++ b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs @@ -58,7 +58,7 @@ namespace RGB.NET.Devices.Novation #region Methods /// - public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) + public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; @@ -66,25 +66,26 @@ namespace RGB.NET.Devices.Novation { IList devices = new List(); - for (int index = 0; index < OutputDeviceBase.DeviceCount; index++) - { - try + if (loadFilter.HasFlag(RGBDeviceType.LedMatrix)) + for (int index = 0; index < OutputDeviceBase.DeviceCount; index++) { - MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index); - if (outCaps.name == null) continue; + 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)); + NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices)) + .Cast() + .FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase)); - if (deviceId == null) continue; + if (deviceId == null) continue; - INovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, deviceId.GetColorCapability())); - device.Initialize(); - devices.Add(device); + INovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, deviceId.GetColorCapability())); + device.Initialize(); + devices.Add(device); + } + catch { if (throwExceptions) throw; } } - catch { if (throwExceptions) throw; } - } Devices = new ReadOnlyCollection(devices); IsInitialized = true;