mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2026-11-10 20:46:24 +00:00
Refactorings to better fit general code-style; Fix for device-loading
This commit is contained in:
1 parent
a04018fef1
commit
afb68f1277
5 files changed
+116
-81
No files matched your search
@@ -3,27 +3,23 @@ using HidSharp.Reports.Encodings;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RGB.NET.Devices.Logitech.HID
|
||||
{
|
||||
public static class Extensions
|
||||
internal static class Extensions
|
||||
{
|
||||
public static Span<byte> AsSpan<T>(this ref T val) where T : unmanaged
|
||||
internal static Span<byte> AsSpan<T>(this ref T val)
|
||||
where T : unmanaged
|
||||
{
|
||||
Span<T> valSpan = MemoryMarshal.CreateSpan(ref val, 1);
|
||||
return MemoryMarshal.Cast<T, byte>(valSpan);
|
||||
}
|
||||
|
||||
public static uint GetUsagePage(this HidDevice device)
|
||||
internal static uint GetUsagePage(this HidDevice device)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[]? descriptor = device.GetRawReportDescriptor();
|
||||
IEnumerable<EncodedItem>? decodedItems = EncodedItem.DecodeItems(descriptor, 0, descriptor.Length);
|
||||
IEnumerable<EncodedItem>? usefulItems = decodedItems.Where(de => de.TagForLocal == LocalItemTag.Usage && de.TagForGlobal == GlobalItemTag.UsagePage);
|
||||
EncodedItem? usagePage = usefulItems.FirstOrDefault(de => de.ItemType == ItemType.Global);
|
||||
return usagePage.DataValue;
|
||||
return device.GetItemByType(ItemType.Global)?.DataValue ?? uint.MaxValue;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -31,20 +27,24 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
}
|
||||
}
|
||||
|
||||
public static uint GetUsage(this HidDevice device)
|
||||
internal static uint GetUsage(this HidDevice device)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[]? descriptor = device.GetRawReportDescriptor();
|
||||
IEnumerable<EncodedItem>? decodedItems = EncodedItem.DecodeItems(descriptor, 0, descriptor.Length);
|
||||
IEnumerable<EncodedItem>? usefulItems = decodedItems.Where(de => de.TagForLocal == LocalItemTag.Usage && de.TagForGlobal == GlobalItemTag.UsagePage);
|
||||
EncodedItem? usage = usefulItems.FirstOrDefault(de => de.ItemType == ItemType.Local);
|
||||
return usage.DataValue;
|
||||
return device.GetItemByType(ItemType.Local)?.DataValue ?? uint.MaxValue;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return uint.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
private static EncodedItem? GetItemByType(this HidDevice device, ItemType itemType)
|
||||
{
|
||||
byte[] descriptor = device.GetRawReportDescriptor();
|
||||
return EncodedItem.DecodeItems(descriptor, 0, descriptor.Length)
|
||||
.Where(de => (de.TagForLocal == LocalItemTag.Usage) && (de.TagForGlobal == GlobalItemTag.UsagePage))
|
||||
.FirstOrDefault(de => de.ItemType == itemType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace RGB.NET.Devices.Logitech.HID
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0, Size = 64)]
|
||||
public struct FapResponse
|
||||
internal struct FapResponse
|
||||
{
|
||||
public byte Command;
|
||||
public byte DeviceIndex;
|
||||
|
||||
@@ -3,9 +3,15 @@
|
||||
namespace RGB.NET.Devices.Logitech.HID
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0, Size = 7)]
|
||||
public struct FapShortRequest
|
||||
internal struct FapShortRequest
|
||||
{
|
||||
const byte LOGITECH_SHORT_MESSAGE = 0x10;
|
||||
#region Constants
|
||||
|
||||
private const byte LOGITECH_SHORT_MESSAGE = 0x10;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
public byte ReportId;
|
||||
public byte DeviceIndex;
|
||||
@@ -15,15 +21,22 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
public byte Data1;
|
||||
public byte Data2;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public void Init(byte deviceIndex, byte featureIndex)
|
||||
{
|
||||
this.DeviceIndex = deviceIndex;
|
||||
this.FeatureIndex = featureIndex;
|
||||
|
||||
ReportId = LOGITECH_SHORT_MESSAGE;
|
||||
DeviceIndex = deviceIndex;
|
||||
FeatureIndex = featureIndex;
|
||||
FeatureCommand = 0;
|
||||
Data0 = 0;
|
||||
Data1 = 0;
|
||||
Data2 = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,32 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RGB.NET.Devices.Logitech.HID
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a loaded for logitech HID-devices.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLed">The type of the identifier leds are mapped to.</typeparam>
|
||||
/// <typeparam name="TData">The type of the custom data added to the HID-device.</typeparam>
|
||||
public class LightspeedHIDLoader<TLed, TData> : IEnumerable<HIDDeviceDefinition<TLed, TData>>
|
||||
where TLed : notnull
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const int VENDOR_ID = 0x046D;
|
||||
|
||||
// ReSharper disable once StaticMemberInGenericType - This is used like a const
|
||||
private static readonly List<int> RECEIVER_PIDS = new()
|
||||
{
|
||||
0xC539,
|
||||
0xC53A,
|
||||
0xC541,
|
||||
0xC545
|
||||
};
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly Dictionary<int, HIDDeviceDefinition<TLed, TData>> _deviceDefinitions = new();
|
||||
@@ -20,21 +38,13 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
/// <summary>
|
||||
/// Gets the vendor id used for this loader.
|
||||
/// </summary>
|
||||
public int VendorId => 0x046d;
|
||||
public int VendorId => VENDOR_ID;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the filter used to determine which devices should be loaded.
|
||||
/// </summary>
|
||||
public RGBDeviceType LoadFilter { get; set; } = RGBDeviceType.All;
|
||||
|
||||
private static List<int> ReceiverPids { get; } = new()
|
||||
{
|
||||
0xC539,
|
||||
0xC53A,
|
||||
0xC541,
|
||||
0xC545
|
||||
};
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
@@ -72,7 +82,7 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
/// <param name="groupBy">The function grouping the devices.</param>
|
||||
/// <returns>The enumerable containing the selected devices.</returns>
|
||||
public IEnumerable<HIDDeviceDefinition<TLed, TData>> GetConnectedDevices<TKey>(Func<HIDDeviceDefinition<TLed, TData>, TKey> groupBy)
|
||||
=> GetConnectedDevices().GroupBy(x => groupBy(x))
|
||||
=> GetConnectedDevices().GroupBy(groupBy)
|
||||
.Select(group => group.First());
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
@@ -84,36 +94,20 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private IEnumerable<int> Detect()
|
||||
{
|
||||
foreach (int receiverPid in ReceiverPids)
|
||||
{
|
||||
foreach (int wirelessPid in Detect(receiverPid))
|
||||
{
|
||||
yield return wirelessPid;
|
||||
}
|
||||
}
|
||||
}
|
||||
private IEnumerable<int> Detect() => RECEIVER_PIDS.SelectMany(Detect);
|
||||
|
||||
private IEnumerable<int> Detect(int pid)
|
||||
{
|
||||
IEnumerable<HidDevice>? receiverDevices = DeviceList.Local.GetHidDevices(VendorId, pid);
|
||||
IEnumerable<HidDevice>? interfaceTwo = receiverDevices.Where(d => d.DevicePath.Contains("mi_02"));
|
||||
//this is terrible but i don't know how else to filter interfaces
|
||||
Dictionary<byte, HidDevice> deviceUsages = DeviceList.Local
|
||||
.GetHidDevices(VendorId, pid)
|
||||
.Where(d => d.DevicePath.Contains("mi_02"))
|
||||
.ToDictionary(x => (byte)x.GetUsage(), x => x);
|
||||
|
||||
Dictionary<byte, HidDevice> deviceUsages = new();
|
||||
foreach (HidDevice? item in interfaceTwo)
|
||||
{
|
||||
deviceUsages.Add((byte)item.GetUsage(), item);
|
||||
}
|
||||
|
||||
foreach ((int wirelessPid, byte deviceIndex) in GetWirelessDevices(deviceUsages))
|
||||
{
|
||||
foreach ((int wirelessPid, byte _) in GetWirelessDevices(deviceUsages))
|
||||
yield return wirelessPid;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<int, byte> GetWirelessDevices(Dictionary<byte, HidDevice> device_usages)
|
||||
private Dictionary<int, byte> GetWirelessDevices(IReadOnlyDictionary<byte, HidDevice> deviceUsages)
|
||||
{
|
||||
const byte LOGITECH_RECEIVER_ADDRESS = 0xFF;
|
||||
const byte LOGITECH_SET_REGISTER_REQUEST = 0x80;
|
||||
@@ -121,20 +115,20 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
|
||||
Dictionary<int, byte> map = new();
|
||||
|
||||
if (device_usages.TryGetValue(1, out HidDevice? device))
|
||||
if (deviceUsages.TryGetValue(1, out HidDevice? device))
|
||||
{
|
||||
HidStream? stream = device.Open();
|
||||
|
||||
FapResponse response = new FapResponse();
|
||||
FapResponse response = new();
|
||||
|
||||
FapShortRequest getConnectedDevices = new FapShortRequest();
|
||||
FapShortRequest getConnectedDevices = new();
|
||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
|
||||
|
||||
stream.Write(getConnectedDevices.AsSpan());
|
||||
stream.Read(response.AsSpan());
|
||||
|
||||
bool wireless_notifications = (response.Data01 & 1) == 1;
|
||||
if (!wireless_notifications)
|
||||
bool wirelessNotifications = (response.Data01 & 1) == 1;
|
||||
if (!wirelessNotifications)
|
||||
{
|
||||
response = new FapResponse();
|
||||
|
||||
@@ -163,8 +157,7 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
{
|
||||
//log "Faking a reconnect to get device list"
|
||||
deviceCount++;
|
||||
|
||||
response = new FapResponse();
|
||||
|
||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
|
||||
getConnectedDevices.FeatureCommand = 0x02;
|
||||
getConnectedDevices.Data0 = 0x02;
|
||||
@@ -172,13 +165,11 @@ namespace RGB.NET.Devices.Logitech.HID
|
||||
|
||||
for (int i = 0; i < deviceCount; i++)
|
||||
{
|
||||
FapResponse devices = new FapResponse();
|
||||
FapResponse devices = new();
|
||||
stream.Read(devices.AsSpan());
|
||||
int wirelessPid = (devices.Data02 << 8) | devices.Data01;
|
||||
if (devices.DeviceIndex != 0xff)
|
||||
{
|
||||
map.Add(wirelessPid, devices.DeviceIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user