Added gRPC sdk client

This commit is contained in:
Diogo Trindade committed 2025-06-18 19:01:20 +01:00
1 parent c0828c1d01
commit f26b3c193a
14 files changed
+426 -130

No files matched your search

@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using RGB.NET.Core;
using RGB.NET.Devices.Wooting.Enum;
using RGB.NET.Devices.Wooting.Generic;
using RGB.NET.Devices.Wooting.Keyboard;
using RGB.NET.Devices.Wooting.Keypad;
using RGB.NET.Devices.Wooting.Native;
namespace RGB.NET.Devices.Wooting;
/// <inheritdoc />
/// <summary>
/// Represents a device provider responsible for Wooting devices.
/// </summary>
public sealed class WootingDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly Lock _lock = new();
private static WootingDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="WootingDeviceProvider"/> instance.
/// </summary>
public static WootingDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new WootingDeviceProvider();
}
}
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 windows applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX86NativePathsWindows { get; } = ["x86/wooting-rgb-sdk.dll"];
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 windows applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX64NativePathsWindows { get; } = ["x64/wooting-rgb-sdk64.dll"];
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 linux applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleNativePathsLinux { get; } = ["x64/libwooting-rgb-sdk.so"];
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 MacOS applications.
/// The first match will be used.
/// </summary>
// ReSharper disable once InconsistentNaming
public static List<string> PossibleNativePathsMacOS { get; } = ["x64/libwooting-rgb-sdk.dylib"];
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WootingDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public WootingDeviceProvider()
{
lock (_lock)
{
if (_instance != null)
throw new InvalidOperationException($"There can be only one instance of type {nameof(WootingDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
/// <inheritdoc />
protected override void InitializeSDK()
{
lock (_WootingSDK.SdkLock)
{
_WootingSDK.Reload();
}
}
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
lock (_WootingSDK.SdkLock)
{
if (_WootingSDK.KeyboardConnected())
{
for (byte i = 0; i < _WootingSDK.GetDeviceCount(); i++)
{
WootingNativeUpdateQueue updateQueue = new(GetUpdateTrigger(), i);
_WootingSDK.SelectDevice(i);
_WootingDeviceInfo nativeDeviceInfo =
(_WootingDeviceInfo)Marshal.PtrToStructure(_WootingSDK.GetDeviceInfo(), typeof(_WootingDeviceInfo))!;
//Uwu non-rgb returns zero here.
if (nativeDeviceInfo.MaxLedIndex == 0)
continue;
KeyboardLayoutType layoutType = nativeDeviceInfo.LayoutType switch
{
WootingLayoutType.Unknown => KeyboardLayoutType.Unknown,
WootingLayoutType.ANSI => KeyboardLayoutType.ANSI,
WootingLayoutType.ISO => KeyboardLayoutType.ISO,
WootingLayoutType.JIS => KeyboardLayoutType.JIS,
WootingLayoutType.ANSI_SPLIT_SPACEBAR => KeyboardLayoutType.ANSI,
WootingLayoutType.ISO_SPLIT_SPACEBAR => KeyboardLayoutType.ISO,
_ => throw new ArgumentOutOfRangeException()
};
//Note: we cannot change *any* of these here or the local database Artemis has will no longer match up and everything breaks.
string model = nativeDeviceInfo.Model;
string name = DeviceHelper.CreateDeviceName("Wooting", model);
yield return nativeDeviceInfo.DeviceType switch
{
WootingDeviceType.Keypad3Keys => new WootingKeypadRGBDevice(nativeDeviceInfo.DeviceType, new(model, name),
updateQueue),
_ => new WootingKeyboardRGBDevice(nativeDeviceInfo.DeviceType, new(layoutType, model, name), updateQueue)
};
}
}
}
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
lock (_WootingSDK.SdkLock)
{
try { _WootingSDK.UnloadWootingSDK(); }
catch
{ /* at least we tried */
}
}
_instance = null;
}
}
#endregion
}
@@ -0,0 +1,72 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Devices.Wooting.Native;
/// <inheritdoc />
/// <summary>
/// Represents the update-queue performing updates for cooler master devices.
/// </summary>
public sealed class WootingNativeUpdateQueue : UpdateQueue
{
#region Properties & Fields
private readonly byte _deviceid;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="WootingUpdateQueue"/> class.
/// </summary>
/// <param name="updateTrigger">The update trigger used by this queue.</param>
public WootingNativeUpdateQueue(IDeviceUpdateTrigger updateTrigger, byte deviceId)
: base(updateTrigger)
{
this._deviceid = deviceId;
}
#endregion
#region Methods
/// <inheritdoc />
protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
lock (_WootingSDK.SdkLock)
{
_WootingSDK.SelectDevice(_deviceid);
foreach ((object key, Color color) in dataSet)
{
(int row, int column) = ((int, int))key;
_WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB());
}
_WootingSDK.ArrayUpdateKeyboard();
}
return true;
}
catch (Exception ex)
{
WootingDeviceProvider.Instance.Throw(ex);
}
return false;
}
/// <inheritdoc />
public override void Dispose()
{
_WootingSDK.SelectDevice(_deviceid);
_WootingSDK.Reset();
base.Dispose();
}
#endregion
}