1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08: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> /// <summary>
/// Initializes the <see cref="IRGBDeviceProvider"/> if not already happened or reloads it if it is already initialized. /// Initializes the <see cref="IRGBDeviceProvider"/> if not already happened or reloads it if it is already initialized.
/// </summary> /// </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="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> /// <param name="throwExceptions">Specifies whether exception during the initialization sequence should be thrown or not.</param>
/// <returns></returns> /// <returns></returns>
bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false); bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false);
/// <summary> /// <summary>
/// Resets all handled <see cref="IRGBDevice"/> back top default. /// Resets all handled <see cref="IRGBDevice"/> back top default.

View File

@ -8,10 +8,15 @@ namespace RGB.NET.Core
[Flags] [Flags]
public enum RGBDeviceType public enum RGBDeviceType
{ {
/// <summary>
/// Represents nothing.
/// </summary>
None = 0,
/// <summary> /// <summary>
/// Represents a device where the type is not known or not present in the list. /// Represents a device where the type is not known or not present in the list.
/// </summary> /// </summary>
Unknown = 0, Unknown = -1,
/// <summary> /// <summary>
/// Represents a keyboard. /// Represents a keyboard.
@ -62,5 +67,10 @@ namespace RGB.NET.Core
/// Represents a headset stand /// Represents a headset stand
/// </summary> /// </summary>
HeadsetStand = 1 << 9, 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. /// Loads all devices the given <see cref="IRGBDeviceProvider"/> is able to provide.
/// </summary> /// </summary>
/// <param name="deviceProvider">The <see cref="IRGBDeviceProvider"/> to load the devices from.</param> /// <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> /// <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; if (_deviceProvider.Contains(deviceProvider) || _deviceProvider.Any(x => x.GetType() == deviceProvider.GetType())) return;
List<IRGBDevice> addedDevices = new List<IRGBDevice>(); List<IRGBDevice> addedDevices = new List<IRGBDevice>();
if (deviceProvider.IsInitialized || deviceProvider.Initialize(throwExceptions)) if (deviceProvider.IsInitialized || deviceProvider.Initialize(loadFilter, exclusiveAccessIfPossible, throwExceptions))
{ {
_deviceProvider.Add(deviceProvider); _deviceProvider.Add(deviceProvider);

View File

@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Asus
#region Methods #region Methods
/// <inheritdoc /> /// <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; IsInitialized = false;
@ -93,31 +93,32 @@ namespace RGB.NET.Devices.Asus
#region Mainboard #region Mainboard
try if (loadFilter.HasFlag(RGBDeviceType.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)
{ {
IntPtr mainboardHandles = Marshal.AllocHGlobal(mainboardCount * IntPtr.Size); //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.
_AsusSDK.EnumerateMbController(mainboardHandles, mainboardCount); // 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));
for (int i = 0; i < mainboardCount; i++) 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); try
_AsusSDK.SetMbMode(handle, 1); {
AsusMainboardRGBDevice device = new AsusMainboardRGBDevice(new AsusMainboardRGBDeviceInfo(RGBDeviceType.Mainboard, handle)); IntPtr handle = Marshal.ReadIntPtr(mainboardHandles, i);
device.Initialize(); _AsusSDK.SetMbMode(handle, 1);
devices.Add(device); 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 #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) //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 if (loadFilter.HasFlag(RGBDeviceType.GraphicsCard))
{ try
int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0);
if (graphicCardCount > 0)
{ {
IntPtr grapicsCardHandles = Marshal.AllocHGlobal(graphicCardCount * IntPtr.Size); int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0);
_AsusSDK.EnumerateGPU(grapicsCardHandles, graphicCardCount); if (graphicCardCount > 0)
for (int i = 0; i < graphicCardCount; i++)
{ {
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); try
_AsusSDK.SetGPUMode(handle, 1); {
AsusGraphicsCardRGBDevice device = new AsusGraphicsCardRGBDevice(new AsusGraphicsCardRGBDeviceInfo(RGBDeviceType.GraphicsCard, handle)); IntPtr handle = Marshal.ReadIntPtr(grapicsCardHandles, i);
device.Initialize(); _AsusSDK.SetGPUMode(handle, 1);
devices.Add(device); 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 #endregion
@ -182,35 +184,37 @@ namespace RGB.NET.Devices.Asus
#region Keyboard #region Keyboard
try if (loadFilter.HasFlag(RGBDeviceType.Keyboard))
{ try
IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size);
if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle))
{ {
_AsusSDK.SetClaymoreKeyboardMode(keyboardHandle, 1); IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size);
AsusKeyboardRGBDevice device = new AsusKeyboardRGBDevice(new AsusKeyboardRGBDeviceInfo(RGBDeviceType.Keyboard, keyboardHandle, GetCulture())); if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle))
device.Initialize(); {
devices.Add(device); _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 #endregion
#region Mouse #region Mouse
try if (loadFilter.HasFlag(RGBDeviceType.Mouse))
{ try
IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size);
if (_AsusSDK.CreateRogMouse(mouseHandle))
{ {
_AsusSDK.SetRogMouseMode(mouseHandle, 1); IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size);
AsusMouseRGBDevice device = new AsusMouseRGBDevice(new AsusMouseRGBDeviceInfo(RGBDeviceType.Mouse, mouseHandle)); if (_AsusSDK.CreateRogMouse(mouseHandle))
device.Initialize(); {
devices.Add(device); _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 #endregion

View File

@ -82,7 +82,7 @@ namespace RGB.NET.Devices.CoolerMaster
#region Methods #region Methods
/// <inheritdoc /> /// <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; IsInitialized = false;
@ -101,8 +101,11 @@ namespace RGB.NET.Devices.CoolerMaster
_CoolerMasterSDK.SetControlDevice(index); _CoolerMasterSDK.SetControlDevice(index);
if (_CoolerMasterSDK.IsDevicePlugged()) if (_CoolerMasterSDK.IsDevicePlugged())
{ {
RGBDeviceType deviceType = index.GetDeviceType();
if (!loadFilter.HasFlag(deviceType)) continue;
ICoolerMasterRGBDevice device; ICoolerMasterRGBDevice device;
switch (index.GetDeviceType()) switch (deviceType)
{ {
case RGBDeviceType.Keyboard: case RGBDeviceType.Keyboard:
CoolerMasterPhysicalKeyboardLayout physicalLayout = _CoolerMasterSDK.GetDeviceLayout(); CoolerMasterPhysicalKeyboardLayout physicalLayout = _CoolerMasterSDK.GetDeviceLayout();

View File

@ -89,7 +89,7 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc /> /// <inheritdoc />
/// <exception cref="RGBDeviceException">Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.</exception> /// <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> /// <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; IsInitialized = false;
@ -129,34 +129,9 @@ namespace RGB.NET.Devices.Corsair
if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
continue; // Everything that doesn't support lighting control is useless continue; // Everything that doesn't support lighting control is useless
ICorsairRGBDevice device; ICorsairRGBDevice device = GetRGBDevice(info, i, nativeDeviceInfo);
switch (info.CorsairDeviceType) if ((device == null) || !loadFilter.HasFlag(device.DeviceInfo.DeviceType)) continue;
{
case CorsairDeviceType.Keyboard:
device = new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo));
break;
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(); device.Initialize();
AddSpecialParts(device); AddSpecialParts(device);
@ -182,6 +157,32 @@ namespace RGB.NET.Devices.Corsair
return true; 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) private void AddSpecialParts(ICorsairRGBDevice device)
{ {
if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase)) if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase))

View File

@ -75,7 +75,7 @@ namespace RGB.NET.Devices.Logitech
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false) public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{ {
try try
{ {
@ -101,9 +101,12 @@ namespace RGB.NET.Devices.Logitech
if (DeviceChecker.IsPerKeyDeviceConnected) if (DeviceChecker.IsPerKeyDeviceConnected)
{ {
(string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerKeyDeviceData; (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)); 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
device.Initialize(); {
devices.Add(device); ILogitechRGBDevice device = new LogitechPerKeyRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.PerKeyRGB, imageBasePath, imageLayout, layoutPath));
device.Initialize();
devices.Add(device);
}
} }
} }
catch { if (throwExceptions) throw; } catch { if (throwExceptions) throw; }
@ -113,9 +116,12 @@ namespace RGB.NET.Devices.Logitech
if (DeviceChecker.IsPerDeviceDeviceConnected) if (DeviceChecker.IsPerDeviceDeviceConnected)
{ {
(string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerDeviceDeviceData; (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)); 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
device.Initialize(); {
devices.Add(device); ILogitechRGBDevice device = new LogitechPerDeviceRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, imageBasePath, imageLayout, layoutPath));
device.Initialize();
devices.Add(device);
}
} }
} }
catch { if (throwExceptions) throw; } catch { if (throwExceptions) throw; }

View File

@ -81,7 +81,7 @@ namespace RGB.NET.Devices.Msi
#region Methods #region Methods
/// <inheritdoc /> /// <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; IsInitialized = false;

View File

@ -58,7 +58,7 @@ namespace RGB.NET.Devices.Novation
#region Methods #region Methods
/// <inheritdoc /> /// <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; IsInitialized = false;
@ -66,25 +66,26 @@ namespace RGB.NET.Devices.Novation
{ {
IList<IRGBDevice> devices = new List<IRGBDevice>(); IList<IRGBDevice> devices = new List<IRGBDevice>();
for (int index = 0; index < OutputDeviceBase.DeviceCount; index++) if (loadFilter.HasFlag(RGBDeviceType.LedMatrix))
{ for (int index = 0; index < OutputDeviceBase.DeviceCount; index++)
try
{ {
MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index); try
if (outCaps.name == null) continue; {
MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index);
if (outCaps.name == null) continue;
NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices)) NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices))
.Cast<Enum>() .Cast<Enum>()
.FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase)); .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())); INovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index, deviceId.GetColorCapability()));
device.Initialize(); device.Initialize();
devices.Add(device); devices.Add(device);
}
catch { if (throwExceptions) throw; }
} }
catch { if (throwExceptions) throw; }
}
Devices = new ReadOnlyCollection<IRGBDevice>(devices); Devices = new ReadOnlyCollection<IRGBDevice>(devices);
IsInitialized = true; IsInitialized = true;