Changed all existing device-providers to use update-queues to decouple the physical update from the internal update-loop (first draft, code-documention is missing)

This commit is contained in:
DarthAffe committed 2018-03-25 15:53:13 +02:00
1 parent 3965bfb9a0
commit 66d03cdf4f
61 files changed
+1125 -440

No files matched your search

@@ -66,6 +66,9 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc />
public IEnumerable<IRGBDevice> Devices { get; private set; }
public UpdateTrigger UpdateTrigger { get; private set; }
private CorsairUpdateQueue _updateQueue;
#endregion
#region Constructors
@@ -78,6 +81,9 @@ namespace RGB.NET.Devices.Corsair
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(CorsairDeviceProvider)}");
_instance = this;
UpdateTrigger = new UpdateTrigger();
_updateQueue = new CorsairUpdateQueue(UpdateTrigger);
}
#endregion
@@ -93,6 +99,8 @@ namespace RGB.NET.Devices.Corsair
try
{
UpdateTrigger?.Stop();
_CUESDK.Reload();
ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake());
@@ -130,7 +138,7 @@ namespace RGB.NET.Devices.Corsair
ICorsairRGBDevice device = GetRGBDevice(info, i, nativeDeviceInfo);
if ((device == null) || !loadFilter.HasFlag(device.DeviceInfo.DeviceType)) continue;
device.Initialize();
device.Initialize(_updateQueue);
AddSpecialParts(device);
error = LastError;
@@ -142,6 +150,8 @@ namespace RGB.NET.Devices.Corsair
catch { if (throwExceptions) throw; }
}
UpdateTrigger?.Start();
Devices = new ReadOnlyCollection<IRGBDevice>(devices);
IsInitialized = true;
}
@@ -29,6 +29,8 @@ namespace RGB.NET.Devices.Corsair
// ReSharper disable once MemberCanBePrivate.Global
protected Dictionary<CorsairLedId, Led> InternalLedMapping { get; } = new Dictionary<CorsairLedId, Led>();
protected CorsairUpdateQueue UpdateQueue { get; set; }
#endregion
#region Indexer
@@ -61,8 +63,10 @@ namespace RGB.NET.Devices.Corsair
/// <summary>
/// Initializes the device.
/// </summary>
public void Initialize()
public void Initialize(CorsairUpdateQueue updateQueue)
{
UpdateQueue = updateQueue;
InitializeLayout();
foreach (Led led in LedMapping.Values)
@@ -86,31 +90,7 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))).ToList();
if (leds.Count > 0) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * leds.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (Led led in leds)
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = (int)led.CustomData,
r = led.Color.R,
g = led.Color.G,
b = led.Color.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(leds.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
}
=> UpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))));
/// <inheritdoc cref="IRGBDevice.SyncBack" />
public override void SyncBack()
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
{
public class CorsairUpdateQueue : UpdateQueue
{
#region Constructors
public CorsairUpdateQueue(IUpdateTrigger updateTrigger)
: base(updateTrigger)
{ }
#endregion
#region Methods
/// <inheritdoc />
protected override void Update(Dictionary<object, Color> dataSet)
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<object, Color> data in dataSet)
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = (int)data.Key,
r = data.Value.R,
g = data.Value.G,
b = data.Value.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(dataSet.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
#endregion
}
}
@@ -7,6 +7,6 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
internal interface ICorsairRGBDevice : IRGBDevice
{
void Initialize();
void Initialize(CorsairUpdateQueue updateQueue);
}
}
@@ -59,6 +59,7 @@
<Compile Include="Generic\CorsairProtocolDetails.cs" />
<Compile Include="Generic\CorsairRGBDeviceInfo.cs" />
<Compile Include="Generic\CorsairRGBDevice.cs" />
<Compile Include="Generic\CorsairUpdateQueue.cs" />
<Compile Include="Generic\ICorsairRGBDevice.cs" />
<Compile Include="HeadsetStand\CorsairHeadsetStandRGBDevice.cs" />
<Compile Include="HeadsetStand\CorsairHeadsetStandRGBDeviceInfo.cs" />
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=generic_005Cupdate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=headsetstand/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=helper/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=libs/@EntryIndexedValue">True</s:Boolean>