From ea44c0d203e8e93443781c5ccd79034d6806db1d Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 31 Mar 2022 21:31:13 +0200 Subject: [PATCH] Fixed custom device creation for corsair --- RGB.NET.Core/Devices/RGBDeviceType.cs | 5 ++ .../CorsairDeviceProvider.cs | 62 +++++++++---------- .../Custom/CorsairCustomRGBDeviceInfo.cs | 36 ++++++++--- 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/RGB.NET.Core/Devices/RGBDeviceType.cs b/RGB.NET.Core/Devices/RGBDeviceType.cs index 8f33f0d..88e4f82 100644 --- a/RGB.NET.Core/Devices/RGBDeviceType.cs +++ b/RGB.NET.Core/Devices/RGBDeviceType.cs @@ -88,6 +88,11 @@ public enum RGBDeviceType /// Monitor = 1 << 14, + /// + /// Represents a generic led-controller. + /// + LedController = 1 << 15, + /// /// Represents a device where the type is not known or not present in the list. /// diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index 91bba74..57c0440 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Corsair.Native; @@ -141,38 +142,30 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider case CorsairDeviceType.Cooler: case CorsairDeviceType.CommanderPro: case CorsairDeviceType.LightningNodePro: - _CorsairChannelsInfo? channelsInfo = nativeDeviceInfo.channels; - if (channelsInfo != null) + List<_CorsairChannelInfo> channels = GetChannels(nativeDeviceInfo).ToList(); + int channelsLedCount = channels.Sum(x => x.totalLedsCount); + int deviceLedCount = nativeDeviceInfo.ledsCount - channelsLedCount; + + if (deviceLedCount > 0) + yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(i, nativeDeviceInfo, deviceLedCount), updateQueue); + + int ledOffset = deviceLedCount; + foreach (_CorsairChannelInfo channelInfo in channels) { - IntPtr channelInfoPtr = channelsInfo.channels; - int ledOffset = 0; - - for (int channel = 0; channel < channelsInfo.channelsCount; channel++) + int channelDeviceInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelDeviceInfo)); + IntPtr channelDeviceInfoPtr = channelInfo.devices; + for (int device = 0; (device < channelInfo.devicesCount) && (ledOffset < nativeDeviceInfo.ledsCount); device++) { - CorsairLedId referenceLed = GetChannelReferenceId(nativeDeviceInfo.type, channel); - if (referenceLed == CorsairLedId.Invalid) continue; + _CorsairChannelDeviceInfo channelDeviceInfo = (_CorsairChannelDeviceInfo)Marshal.PtrToStructure(channelDeviceInfoPtr, typeof(_CorsairChannelDeviceInfo))!; - _CorsairChannelInfo channelInfo = (_CorsairChannelInfo)Marshal.PtrToStructure(channelInfoPtr, typeof(_CorsairChannelInfo))!; + yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(i, nativeDeviceInfo, channelDeviceInfo, ledOffset), updateQueue); - int channelDeviceInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelDeviceInfo)); - IntPtr channelDeviceInfoPtr = channelInfo.devices; - - for (int device = 0; (device < channelInfo.devicesCount) && (ledOffset < nativeDeviceInfo.ledsCount); device++) - { - _CorsairChannelDeviceInfo channelDeviceInfo = (_CorsairChannelDeviceInfo)Marshal.PtrToStructure(channelDeviceInfoPtr, typeof(_CorsairChannelDeviceInfo))!; - - yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(i, nativeDeviceInfo, channelDeviceInfo, referenceLed, ledOffset), updateQueue); - referenceLed += channelDeviceInfo.deviceLedCount; - - ledOffset += channelDeviceInfo.deviceLedCount; - channelDeviceInfoPtr = new IntPtr(channelDeviceInfoPtr.ToInt64() + channelDeviceInfoStructSize); - } - - int channelInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelInfo)); - channelInfoPtr = new IntPtr(channelInfoPtr.ToInt64() + channelInfoStructSize); + ledOffset += channelDeviceInfo.deviceLedCount; + channelDeviceInfoPtr = new IntPtr(channelDeviceInfoPtr.ToInt64() + channelDeviceInfoStructSize); } } break; + default: Throw(new RGBDeviceException("Unknown Device-Type")); break; @@ -180,18 +173,19 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider } } - private static CorsairLedId GetChannelReferenceId(CorsairDeviceType deviceType, int channel) + private static IEnumerable<_CorsairChannelInfo> GetChannels(_CorsairDeviceInfo deviceInfo) { - if (deviceType == CorsairDeviceType.Cooler) - return CorsairLedId.CustomLiquidCoolerChannel1Led1; + _CorsairChannelsInfo? channelsInfo = deviceInfo.channels; + if (channelsInfo == null) yield break; - return channel switch + IntPtr channelInfoPtr = channelsInfo.channels; + for (int channel = 0; channel < channelsInfo.channelsCount; channel++) { - 0 => CorsairLedId.CustomDeviceChannel1Led1, - 1 => CorsairLedId.CustomDeviceChannel2Led1, - 2 => CorsairLedId.CustomDeviceChannel3Led1, - _ => CorsairLedId.Invalid - }; + yield return (_CorsairChannelInfo)Marshal.PtrToStructure(channelInfoPtr, typeof(_CorsairChannelInfo))!; + + int channelInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelInfo)); + channelInfoPtr = new IntPtr(channelInfoPtr.ToInt64() + channelInfoStructSize); + } } /// diff --git a/RGB.NET.Devices.Corsair/Custom/CorsairCustomRGBDeviceInfo.cs b/RGB.NET.Devices.Corsair/Custom/CorsairCustomRGBDeviceInfo.cs index 57242cc..8c30c72 100644 --- a/RGB.NET.Devices.Corsair/Custom/CorsairCustomRGBDeviceInfo.cs +++ b/RGB.NET.Devices.Corsair/Custom/CorsairCustomRGBDeviceInfo.cs @@ -17,11 +17,6 @@ public class CorsairCustomRGBDeviceInfo : CorsairRGBDeviceInfo { #region Properties & Fields - /// - /// Gets the corsair-id of the first LED of this device. - /// - public CorsairLedId ReferenceCorsairLed { get; } - /// /// Gets the amount of LEDs this device contains. /// @@ -36,7 +31,6 @@ public class CorsairCustomRGBDeviceInfo : CorsairRGBDeviceInfo #region Constructors - //TODO DarthAffe 07.07.2018: DAP is a fan right now, that's most likely wrong /// /// /// Internal constructor of managed . @@ -44,18 +38,24 @@ public class CorsairCustomRGBDeviceInfo : CorsairRGBDeviceInfo /// The index of the . /// The native -struct /// The native representing this device. - /// The id of the first LED of this device. /// The offset used to find the LEDs of this device. - internal CorsairCustomRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, _CorsairChannelDeviceInfo channelDeviceInfo, CorsairLedId referenceCorsairLed, int ledOffset) + internal CorsairCustomRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, _CorsairChannelDeviceInfo channelDeviceInfo, int ledOffset) : base(deviceIndex, GetDeviceType(channelDeviceInfo.type), nativeInfo, GetModelName(nativeInfo.model == IntPtr.Zero ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase), channelDeviceInfo)) { - this.ReferenceCorsairLed = referenceCorsairLed; this.LedOffset = ledOffset; LedCount = channelDeviceInfo.deviceLedCount; } + internal CorsairCustomRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, int ledCount) + : base(deviceIndex, GetDeviceType(nativeInfo.type), nativeInfo) + { + this.LedCount = ledCount; + + LedOffset = 0; + } + #endregion #region Methods @@ -77,6 +77,24 @@ public class CorsairCustomRGBDeviceInfo : CorsairRGBDeviceInfo _ => throw new ArgumentOutOfRangeException(nameof(deviceType), deviceType, null) }; + private static RGBDeviceType GetDeviceType(CorsairDeviceType deviceType) + => deviceType switch + { + CorsairDeviceType.Unknown => RGBDeviceType.Unknown, + CorsairDeviceType.Mouse => RGBDeviceType.Mouse, + CorsairDeviceType.Keyboard => RGBDeviceType.Keyboard, + CorsairDeviceType.Headset => RGBDeviceType.Headset, + CorsairDeviceType.Mousepad => RGBDeviceType.Mousepad, + CorsairDeviceType.HeadsetStand => RGBDeviceType.HeadsetStand, + CorsairDeviceType.CommanderPro => RGBDeviceType.LedController, + CorsairDeviceType.LightningNodePro => RGBDeviceType.LedController, + CorsairDeviceType.MemoryModule => RGBDeviceType.DRAM, + CorsairDeviceType.Cooler => RGBDeviceType.Cooler, + CorsairDeviceType.Mainboard => RGBDeviceType.Mainboard, + CorsairDeviceType.GraphicsCard => RGBDeviceType.GraphicsCard, + _ => throw new ArgumentOutOfRangeException(nameof(deviceType), deviceType, null) + }; + private static string GetModelName(string model, _CorsairChannelDeviceInfo channelDeviceInfo) { switch (channelDeviceInfo.type)