using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.CorsairLegacy.Native;
namespace RGB.NET.Devices.CorsairLegacy;
///
///
/// Represents a generic CUE-device. (keyboard, mouse, headset, mousepad).
///
public abstract class CorsairRGBDevice : AbstractRGBDevice, ICorsairRGBDevice
where TDeviceInfo : CorsairRGBDeviceInfo
{
#region Properties & Fields
///
/// Gets the mapping of to used to update the LEDs of this device.
///
protected LedMapping Mapping { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The generic information provided by CUE for the device.
/// The mapping to used to update the LEDs of this device.
/// The queue used to update this device.
protected CorsairRGBDevice(TDeviceInfo info, LedMapping mapping, CorsairDeviceUpdateQueue updateQueue)
: base(info, updateQueue)
{
this.Mapping = mapping;
}
#endregion
#region Methods
void ICorsairRGBDevice.Initialize() => InitializeLayout();
///
/// Initializes the LEDs of the device based on the data provided by the SDK.
///
protected virtual void InitializeLayout()
{
_CorsairLedPositions? nativeLedPositions = (_CorsairLedPositions?)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositionsByDeviceIndex(DeviceInfo.CorsairDeviceIndex), typeof(_CorsairLedPositions));
if (nativeLedPositions == null) return;
int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition));
nint ptr = nativeLedPositions.pLedPosition;
for (int i = 0; i < nativeLedPositions.numberOfLed; i++)
{
_CorsairLedPosition? ledPosition = (_CorsairLedPosition?)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
if (ledPosition == null)
{
ptr += structSize;
continue;
}
LedId ledId = Mapping.TryGetValue(ledPosition.LedId, out LedId id) ? id : LedId.Invalid;
Rectangle rectangle = ledPosition.ToRectangle();
AddLed(ledId, rectangle.Location, rectangle.Size);
ptr += structSize;
}
}
///
protected override object GetLedCustomData(LedId ledId) => Mapping.TryGetValue(ledId, out CorsairLedId corsairLedId) ? corsairLedId : CorsairLedId.Invalid;
#endregion
}