1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Added a device filter to LoadDevices and device Initialize

This commit is contained in:
Darth Affe 2017-12-07 12:24:14 +01:00
parent b700c84b8d
commit 11ca959328
9 changed files with 142 additions and 114 deletions

View File

@ -31,10 +31,11 @@ namespace RGB.NET.Core
/// <summary>
/// Initializes the <see cref="IRGBDeviceProvider"/> if not already happened or reloads it if it is already initialized.
/// </summary>
/// <param name="loadFilter">Specifies which types of devices to load.</param>
/// <param name="exclusiveAccessIfPossible">Specifies whether the application should request exclusive access of possible or not.</param>
/// <param name="throwExceptions">Specifies whether exception during the initialization sequence should be thrown or not.</param>
/// <returns></returns>
bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false);
bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false);
/// <summary>
/// Resets all handled <see cref="IRGBDevice"/> back top default.

View File

@ -8,10 +8,15 @@ namespace RGB.NET.Core
[Flags]
public enum RGBDeviceType
{
/// <summary>
/// Represents nothing.
/// </summary>
None = 0,
/// <summary>
/// Represents a device where the type is not known or not present in the list.
/// </summary>
Unknown = 0,
Unknown = -1,
/// <summary>
/// Represents a keyboard.
@ -62,5 +67,10 @@ namespace RGB.NET.Core
/// Represents a headset stand
/// </summary>
HeadsetStand = 1 << 9,
/// <summary>
/// Represents all devices
/// </summary>
All = 0xFFFFFFF
}
}

View File

@ -13,13 +13,15 @@ namespace RGB.NET.Core
/// Loads all devices the given <see cref="IRGBDeviceProvider"/> is able to provide.
/// </summary>
/// <param name="deviceProvider">The <see cref="IRGBDeviceProvider"/> to load the devices from.</param>
/// <param name="loadFilter">Specifies which types of devices to load.</param>
/// <param name="exclusiveAccessIfPossible">Specifies whether the application should request exclusive access of possible or not.</param>
/// <param name="throwExceptions">Specifies whether exception during the initialization sequence should be thrown or not.</param>
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<IRGBDevice> addedDevices = new List<IRGBDevice>();
if (deviceProvider.IsInitialized || deviceProvider.Initialize(throwExceptions))
if (deviceProvider.IsInitialized || deviceProvider.Initialize(loadFilter, exclusiveAccessIfPossible, throwExceptions))
{
_deviceProvider.Add(deviceProvider);

View File

@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Asus
#region Methods
/// <inheritdoc />
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

View File

@ -82,7 +82,7 @@ namespace RGB.NET.Devices.CoolerMaster
#region Methods
/// <inheritdoc />
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();

View File

@ -89,7 +89,7 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc />
/// <exception cref="RGBDeviceException">Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.</exception>
/// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
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))

View File

@ -75,7 +75,7 @@ namespace RGB.NET.Devices.Logitech
#region Methods
/// <inheritdoc />
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; }

View File

@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Msi
#region Methods
/// <inheritdoc />
public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
IsInitialized = false;

View File

@ -58,7 +58,7 @@ namespace RGB.NET.Devices.Novation
#region Methods
/// <inheritdoc />
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<IRGBDevice> devices = new List<IRGBDevice>();
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<Enum>()
.FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase));
NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices))
.Cast<Enum>()
.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<IRGBDevice>(devices);
IsInitialized = true;