Fixed some stuff to get Corsair-Devices working

This commit is contained in:
DarthAffe committed 2017-01-26 20:14:38 +01:00
1 parent 7f6c8e6a70
commit 05c0633348
18 files changed
+308 -35

No files matched your search

@@ -6,15 +6,18 @@ namespace RGB.NET.Devices.Corsair
/// <summary>
/// Represents a Id of a <see cref="Led"/> on a <see cref="CorsairRGBDevice"/>.
/// </summary>
[DebuggerDisplay("{" + nameof(_ledId) + "}")]
[DebuggerDisplay("{" + nameof(LedId) + "}")]
public class CorsairLedId : ILedId
{
#region Properties & Fields
private readonly CorsairLedIds _ledId;
internal readonly CorsairLedIds LedId;
/// <inheritdoc />
public bool IsValid => _ledId != CorsairLedIds.Invalid;
public IRGBDevice Device { get; }
/// <inheritdoc />
public bool IsValid => LedId != CorsairLedIds.Invalid;
#endregion
@@ -23,10 +26,12 @@ namespace RGB.NET.Devices.Corsair
/// <summary>
/// Initializes a new instance of the <see cref="CorsairLedId"/> class.
/// </summary>
/// <param name="ledId">The corsair-id of the represented <see cref="Led"/>.</param>
public CorsairLedId(CorsairLedIds ledId)
/// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
/// <param name="ledId">The <see cref="CorsairLedId"/> of the represented <see cref="Led"/>.</param>
public CorsairLedId(IRGBDevice device, CorsairLedIds ledId)
{
this._ledId = ledId;
this.Device = device;
this.LedId = ledId;
}
#endregion
@@ -39,7 +44,7 @@ namespace RGB.NET.Devices.Corsair
/// <returns>A string that contains the Id of this <see cref="CorsairLedId"/>. For example "Enter".</returns>
public override string ToString()
{
return _ledId.ToString();
return LedId.ToString();
}
/// <summary>
@@ -59,7 +64,7 @@ namespace RGB.NET.Devices.Corsair
if (GetType() != compareLedId.GetType())
return false;
return compareLedId._ledId == _ledId;
return compareLedId.LedId == LedId;
}
/// <summary>
@@ -68,7 +73,7 @@ namespace RGB.NET.Devices.Corsair
/// <returns>An integer value that specifies the hash code for this <see cref="CorsairLedId" />.</returns>
public override int GetHashCode()
{
return _ledId.GetHashCode();
return LedId.GetHashCode();
}
#endregion
@@ -1,5 +1,9 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
{
@@ -51,6 +55,34 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
protected abstract void InitializeLeds();
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => x.Color.A > 0).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)((CorsairLedId)led.Id).LedId,
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);
}
}
#endregion
}
}
@@ -40,8 +40,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
protected override void InitializeLeds()
{
InitializeLed(new CorsairLedId(CorsairLedIds.LeftLogo), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.RightLogo), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.LeftLogo), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.RightLogo), new Rectangle(10, 0, 10, 10));
}
#endregion
@@ -52,7 +52,7 @@ namespace RGB.NET.Devices.Corsair
for (int i = 0; i < nativeLedPositions.numberOfLed; i++)
{
_CorsairLedPosition ledPosition = (_CorsairLedPosition)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
InitializeLed(new CorsairLedId(ledPosition.ledId),
InitializeLed(new CorsairLedId(this, ledPosition.ledId),
new Rectangle(ledPosition.left, ledPosition.top, ledPosition.width, ledPosition.height));
ptr = new IntPtr(ptr.ToInt64() + structSize);
@@ -44,22 +44,22 @@ namespace RGB.NET.Devices.Corsair
switch (MouseDeviceInfo.PhysicalLayout)
{
case CorsairPhysicalMouseLayout.Zones1:
InitializeLed(new CorsairLedId(CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
break;
case CorsairPhysicalMouseLayout.Zones2:
InitializeLed(new CorsairLedId(CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
break;
case CorsairPhysicalMouseLayout.Zones3:
InitializeLed(new CorsairLedId(CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B3), new Rectangle(20, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B3), new Rectangle(20, 0, 10, 10));
break;
case CorsairPhysicalMouseLayout.Zones4:
InitializeLed(new CorsairLedId(CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B3), new Rectangle(20, 0, 10, 10));
InitializeLed(new CorsairLedId(CorsairLedIds.B4), new Rectangle(30, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B1), new Rectangle(0, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B2), new Rectangle(10, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B3), new Rectangle(20, 0, 10, 10));
InitializeLed(new CorsairLedId(this, CorsairLedIds.B4), new Rectangle(30, 0, 10, 10));
break;
default:
throw new RGBDeviceException($"Can't initial mouse with layout '{MouseDeviceInfo.PhysicalLayout}'");
@@ -62,7 +62,7 @@ namespace RGB.NET.Devices.Corsair
}
foreach (_CorsairLedPosition ledPosition in positions.OrderBy(p => p.ledId))
InitializeLed(new CorsairLedId(ledPosition.ledId),
InitializeLed(new CorsairLedId(this, ledPosition.ledId),
new Rectangle(ledPosition.left, ledPosition.top, ledPosition.width, ledPosition.height));
}