mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2026-11-11 04:57:15 +00:00
Streamlined device loading/handling in device providers
This commit is contained in:
1 parent
f6f3e9185c
commit
2a9a43683c
81 files changed
+867
-1864
No files matched your search
@@ -3,8 +3,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Devices.CoolerMaster.Helper;
|
||||
using RGB.NET.Devices.CoolerMaster.Native;
|
||||
@@ -15,7 +13,7 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
/// <summary>
|
||||
/// Represents a device provider responsible for Cooler Master devices.
|
||||
/// </summary>
|
||||
public class CoolerMasterDeviceProvider : IRGBDeviceProvider
|
||||
public class CoolerMasterDeviceProvider : AbstractRGBDeviceProvider
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
@@ -37,20 +35,6 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
/// </summary>
|
||||
public static List<string> PossibleX64NativePaths { get; } = new() { "x64/CMSDK.dll" };
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Indicates if the SDK is initialized and ready to use.
|
||||
/// </summary>
|
||||
public bool IsInitialized { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<IRGBDevice> Devices { get; private set; } = Enumerable.Empty<IRGBDevice>();
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DeviceUpdateTrigger"/> used to trigger the updates for cooler master devices.
|
||||
/// </summary>
|
||||
public DeviceUpdateTrigger UpdateTrigger { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
@@ -63,97 +47,48 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
{
|
||||
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CoolerMasterDeviceProvider)}");
|
||||
_instance = this;
|
||||
|
||||
UpdateTrigger = new DeviceUpdateTrigger();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false)
|
||||
protected override void InitializeSDK()
|
||||
{
|
||||
IsInitialized = false;
|
||||
|
||||
try
|
||||
{
|
||||
UpdateTrigger.Stop();
|
||||
|
||||
_CoolerMasterSDK.Reload();
|
||||
if (_CoolerMasterSDK.GetSDKVersion() <= 0) return false;
|
||||
|
||||
IList<IRGBDevice> devices = new List<IRGBDevice>();
|
||||
|
||||
foreach (CoolerMasterDevicesIndexes index in Enum.GetValues(typeof(CoolerMasterDevicesIndexes)))
|
||||
{
|
||||
try
|
||||
{
|
||||
RGBDeviceType deviceType = index.GetDeviceType();
|
||||
if (deviceType == RGBDeviceType.None) continue;
|
||||
|
||||
if (_CoolerMasterSDK.IsDevicePlugged(index))
|
||||
{
|
||||
if (!loadFilter.HasFlag(deviceType)) continue;
|
||||
|
||||
ICoolerMasterRGBDevice device;
|
||||
switch (deviceType)
|
||||
{
|
||||
case RGBDeviceType.Keyboard:
|
||||
CoolerMasterPhysicalKeyboardLayout physicalLayout = _CoolerMasterSDK.GetDeviceLayout(index);
|
||||
device = new CoolerMasterKeyboardRGBDevice(new CoolerMasterKeyboardRGBDeviceInfo(index, physicalLayout));
|
||||
break;
|
||||
|
||||
case RGBDeviceType.Mouse:
|
||||
device = new CoolerMasterMouseRGBDevice(new CoolerMasterMouseRGBDeviceInfo(index));
|
||||
break;
|
||||
|
||||
default:
|
||||
if (throwExceptions)
|
||||
throw new RGBDeviceException("Unknown Device-Type");
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_CoolerMasterSDK.EnableLedControl(true, index))
|
||||
throw new RGBDeviceException("Failed to enable LED control for device " + index);
|
||||
|
||||
device.Initialize(UpdateTrigger);
|
||||
devices.Add(device);
|
||||
}
|
||||
}
|
||||
catch { if (throwExceptions) throw; }
|
||||
}
|
||||
|
||||
UpdateTrigger.Start();
|
||||
|
||||
Devices = new ReadOnlyCollection<IRGBDevice>(devices);
|
||||
IsInitialized = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwExceptions)
|
||||
throw;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
_CoolerMasterSDK.Reload();
|
||||
if (_CoolerMasterSDK.GetSDKVersion() <= 0) Throw(new RGBDeviceException("Failed to initialize CoolerMaster-SDK"));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
protected override IEnumerable<IRGBDevice> LoadDevices()
|
||||
{
|
||||
try { UpdateTrigger.Dispose(); }
|
||||
catch { /* at least we tried */ }
|
||||
foreach (CoolerMasterDevicesIndexes index in Enum.GetValues(typeof(CoolerMasterDevicesIndexes)))
|
||||
{
|
||||
RGBDeviceType deviceType = index.GetDeviceType();
|
||||
if (deviceType == RGBDeviceType.None) continue;
|
||||
|
||||
foreach (IRGBDevice device in Devices)
|
||||
try { device.Dispose(); }
|
||||
catch { /* at least we tried */ }
|
||||
Devices = Enumerable.Empty<IRGBDevice>();
|
||||
if (_CoolerMasterSDK.IsDevicePlugged(index))
|
||||
{
|
||||
if (!_CoolerMasterSDK.EnableLedControl(true, index))
|
||||
Throw(new RGBDeviceException("Failed to enable LED control for device " + index));
|
||||
else
|
||||
{
|
||||
switch (deviceType)
|
||||
{
|
||||
case RGBDeviceType.Keyboard:
|
||||
yield return new CoolerMasterKeyboardRGBDevice(new CoolerMasterKeyboardRGBDeviceInfo(index, _CoolerMasterSDK.GetDeviceLayout(index)), GetUpdateTrigger());
|
||||
break;
|
||||
|
||||
// DarthAffe 03.03.2020: Should be done but isn't possible due to an weird winodws-hook inside the sdk which corrupts the stack when unloading the dll
|
||||
//try { _CoolerMasterSDK.UnloadCMSDK(); }
|
||||
//catch { /* at least we tried */ }
|
||||
case RGBDeviceType.Mouse:
|
||||
yield return new CoolerMasterMouseRGBDevice(new CoolerMasterMouseRGBDeviceInfo(index), GetUpdateTrigger());
|
||||
break;
|
||||
|
||||
default:
|
||||
Throw(new RGBDeviceException("Unknown Device-Type"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,76 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Devices.CoolerMaster.Native;
|
||||
|
||||
namespace RGB.NET.Devices.CoolerMaster
|
||||
{
|
||||
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}" />
|
||||
/// <inheritdoc cref="ICoolerMasterRGBDevice" />
|
||||
/// <summary>
|
||||
/// Represents a generic CoolerMaster-device. (keyboard, mouse, headset, mousepad).
|
||||
/// </summary>
|
||||
public abstract class CoolerMasterRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDeviceInfo>, ICoolerMasterRGBDevice
|
||||
where TDeviceInfo : CoolerMasterRGBDeviceInfo
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Gets information about the <see cref="T:RGB.NET.Devices.CoolerMaster.CoolerMasterRGBDevice" />.
|
||||
/// </summary>
|
||||
public override TDeviceInfo DeviceInfo { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the update queue performing updates for this device.
|
||||
/// </summary>
|
||||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
protected CoolerMasterUpdateQueue? UpdateQueue { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CoolerMasterRGBDevice{TDeviceInfo}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The generic information provided by CoolerMaster for the device.</param>
|
||||
protected CoolerMasterRGBDevice(TDeviceInfo info)
|
||||
{
|
||||
this.DeviceInfo = info;
|
||||
}
|
||||
protected CoolerMasterRGBDevice(TDeviceInfo info, IDeviceUpdateTrigger updateTrigger)
|
||||
: base(info, new CoolerMasterUpdateQueue(updateTrigger, info.DeviceIndex))
|
||||
{ }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the device.
|
||||
/// </summary>
|
||||
public void Initialize(IDeviceUpdateTrigger updateTrigger)
|
||||
{
|
||||
InitializeLayout();
|
||||
|
||||
UpdateQueue = new CoolerMasterUpdateQueue(updateTrigger, DeviceInfo.DeviceIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="Led"/> and <see cref="Size"/> of the device.
|
||||
/// </summary>
|
||||
protected abstract void InitializeLayout();
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue?.SetData(GetUpdateData(ledsToUpdate));
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose" />
|
||||
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
|
||||
public override void Dispose()
|
||||
{
|
||||
try { UpdateQueue?.Dispose(); }
|
||||
catch { /* at least we tried */ }
|
||||
|
||||
_CoolerMasterSDK.EnableLedControl(false, DeviceInfo.DeviceIndex);
|
||||
|
||||
base.Dispose();
|
||||
|
||||
@@ -5,8 +5,6 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
/// <summary>
|
||||
/// Represents a CoolerMaster RGB-device.
|
||||
/// </summary>
|
||||
internal interface ICoolerMasterRGBDevice : IRGBDevice
|
||||
{
|
||||
void Initialize(IDeviceUpdateTrigger updateTrigger);
|
||||
}
|
||||
public interface ICoolerMasterRGBDevice : IRGBDevice
|
||||
{ }
|
||||
}
|
||||
@@ -22,16 +22,17 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
/// Initializes a new instance of the <see cref="T:RGB.NET.Devices.CoolerMaster.CoolerMasterKeyboardRGBDevice" /> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The specific information provided by CoolerMaster for the keyboard</param>
|
||||
internal CoolerMasterKeyboardRGBDevice(CoolerMasterKeyboardRGBDeviceInfo info)
|
||||
: base(info)
|
||||
{ }
|
||||
internal CoolerMasterKeyboardRGBDevice(CoolerMasterKeyboardRGBDeviceInfo info, IDeviceUpdateTrigger updateTrigger)
|
||||
: base(info, updateTrigger)
|
||||
{
|
||||
InitializeLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void InitializeLayout()
|
||||
private void InitializeLayout()
|
||||
{
|
||||
if (!CoolerMasterKeyboardLedMappings.Mapping.TryGetValue(DeviceInfo.DeviceIndex, out Dictionary<CoolerMasterPhysicalKeyboardLayout, Dictionary<LedId, (int row, int column)>>? deviceMappings))
|
||||
throw new RGBDeviceException($"Failed to find a CoolerMasterKeyboardLedMapping for device index {DeviceInfo.DeviceIndex}");
|
||||
@@ -44,7 +45,7 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override object GetLedCustomData(LedId ledId) => CoolerMasterKeyboardLedMappings.Mapping[DeviceInfo.DeviceIndex][DeviceInfo.PhysicalLayout][ledId];
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -16,16 +16,15 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
/// Initializes a new instance of the <see cref="T:RGB.NET.Devices.CoolerMaster.CoolerMasterMouseRGBDevice" /> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The specific information provided by CoolerMaster for the mouse</param>
|
||||
internal CoolerMasterMouseRGBDevice(CoolerMasterMouseRGBDeviceInfo info)
|
||||
: base(info)
|
||||
internal CoolerMasterMouseRGBDevice(CoolerMasterMouseRGBDeviceInfo info, IDeviceUpdateTrigger updateTrigger)
|
||||
: base(info, updateTrigger)
|
||||
{ }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void InitializeLayout()
|
||||
private void InitializeLayout()
|
||||
{
|
||||
Dictionary<LedId, (int row, int column)> mapping = CoolerMasterMouseLedMappings.Mapping[DeviceInfo.DeviceIndex];
|
||||
|
||||
@@ -35,7 +34,7 @@ namespace RGB.NET.Devices.CoolerMaster
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override object GetLedCustomData(LedId ledId) => CoolerMasterMouseLedMappings.Mapping[DeviceInfo.DeviceIndex][ledId];
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user