Implemented corsair devices

This commit is contained in:
DarthAffe committed 2017-01-22 15:31:30 +01:00
1 parent 89194c4cb4
commit f3128a3dd0
31 files changed
+1731

No files matched your search

+242
View File
@@ -0,0 +1,242 @@
// ReSharper disable UnusedMethodReturnValue.Global
// ReSharper disable UnusedMember.Global
using System;
using System.IO;
using System.Runtime.InteropServices;
using RGB.NET.Core.Exceptions;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
internal static class _CUESDK
{
#region Libary Management
private static IntPtr _dllHandle = IntPtr.Zero;
/// <summary>
/// Gets the loaded architecture (x64/x86).
/// </summary>
internal static string LoadedArchitecture { get; private set; }
/// <summary>
/// Reloads the SDK.
/// </summary>
internal static void Reload()
{
UnloadCUESDK();
LoadCUESDK();
}
private static void LoadCUESDK()
{
if (_dllHandle != IntPtr.Zero) return;
// HACK: Load library at runtime to support both, x86 and x64 with one managed dll
string dllPath = (LoadedArchitecture = Environment.Is64BitProcess ? "x64" : "x86") + "/CUESDK_2015.dll";
if (!File.Exists(dllPath))
throw new RGBDeviceException($"Can't find the CUE-SDK at the expected location '{Path.GetFullPath(dllPath)}'");
_dllHandle = LoadLibrary(dllPath);
_corsairSetLedsColorsPointer =
(CorsairSetLedsColorsPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairSetLedsColors"),
typeof(CorsairSetLedsColorsPointer));
_corsairGetDeviceCountPointer =
(CorsairGetDeviceCountPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceCount"),
typeof(CorsairGetDeviceCountPointer));
_corsairGetDeviceInfoPointer =
(CorsairGetDeviceInfoPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceInfo"),
typeof(CorsairGetDeviceInfoPointer));
_corsairGetLedPositionsPointer =
(CorsairGetLedPositionsPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedPositions"),
typeof(CorsairGetLedPositionsPointer));
_corsairGetLedPositionsByDeviceIndexPointer =
(CorsairGetLedPositionsByDeviceIndexPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedPositionsByDeviceIndex"),
typeof(CorsairGetLedPositionsByDeviceIndexPointer));
_corsairGetLedIdForKeyNamePointer =
(CorsairGetLedIdForKeyNamePointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedIdForKeyName"),
typeof(CorsairGetLedIdForKeyNamePointer));
_corsairRequestControlPointer =
(CorsairRequestControlPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRequestControl"),
typeof(CorsairRequestControlPointer));
_corsairReleaseControlPointer =
(CorsairReleaseControlPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairReleaseControl"),
typeof(CorsairReleaseControlPointer));
_corsairPerformProtocolHandshakePointer =
(CorsairPerformProtocolHandshakePointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairPerformProtocolHandshake"),
typeof(CorsairPerformProtocolHandshakePointer));
_corsairGetLastErrorPointer =
(CorsairGetLastErrorPointer)
Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer));
}
private static void UnloadCUESDK()
{
if (_dllHandle == IntPtr.Zero) return;
// ReSharper disable once EmptyEmbeddedStatement - DarthAffe 20.02.2016: We might need to reduce the internal reference counter more than once to set the library free
while (FreeLibrary(_dllHandle)) ;
_dllHandle = IntPtr.Zero;
}
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr dllHandle);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr dllHandle, string name);
#endregion
#region SDK-METHODS
#region Pointers
private static CorsairSetLedsColorsPointer _corsairSetLedsColorsPointer;
private static CorsairGetDeviceCountPointer _corsairGetDeviceCountPointer;
private static CorsairGetDeviceInfoPointer _corsairGetDeviceInfoPointer;
private static CorsairGetLedPositionsPointer _corsairGetLedPositionsPointer;
private static CorsairGetLedIdForKeyNamePointer _corsairGetLedIdForKeyNamePointer;
private static CorsairGetLedPositionsByDeviceIndexPointer _corsairGetLedPositionsByDeviceIndexPointer;
private static CorsairRequestControlPointer _corsairRequestControlPointer;
private static CorsairReleaseControlPointer _corsairReleaseControlPointer;
private static CorsairPerformProtocolHandshakePointer _corsairPerformProtocolHandshakePointer;
private static CorsairGetLastErrorPointer _corsairGetLastErrorPointer;
#endregion
#region Delegates
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairSetLedsColorsPointer(int size, IntPtr ledsColors);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int CorsairGetDeviceCountPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetDeviceInfoPointer(int deviceIndex);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetLedPositionsPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr CorsairGetLedPositionsByDeviceIndexPointer(int deviceIndex);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate CorsairLedIds CorsairGetLedIdForKeyNamePointer(char keyName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairRequestControlPointer(CorsairAccessMode accessMode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CorsairReleaseControlPointer(CorsairAccessMode accessMode);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate _CorsairProtocolDetails CorsairPerformProtocolHandshakePointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate CorsairError CorsairGetLastErrorPointer();
#endregion
// ReSharper disable EventExceptionNotDocumented
/// <summary>
/// CUE-SDK: set specified leds to some colors. The color is retained until changed by successive calls. This function does not take logical layout into account.
/// </summary>
internal static bool CorsairSetLedsColors(int size, IntPtr ledsColors)
{
return _corsairSetLedsColorsPointer(size, ledsColors);
}
/// <summary>
/// CUE-SDK: returns number of connected Corsair devices that support lighting control.
/// </summary>
internal static int CorsairGetDeviceCount()
{
return _corsairGetDeviceCountPointer();
}
/// <summary>
/// CUE-SDK: returns information about device at provided index.
/// </summary>
internal static IntPtr CorsairGetDeviceInfo(int deviceIndex)
{
return _corsairGetDeviceInfoPointer(deviceIndex);
}
/// <summary>
/// CUE-SDK: provides list of keyboard LEDs with their physical positions.
/// </summary>
internal static IntPtr CorsairGetLedPositions()
{
return _corsairGetLedPositionsPointer();
}
/// <summary>
/// CUE-SDK: provides list of keyboard or mousemat LEDs with their physical positions.
/// </summary>
internal static IntPtr CorsairGetLedPositionsByDeviceIndex(int deviceIndex)
{
return _corsairGetLedPositionsByDeviceIndexPointer(deviceIndex);
}
/// <summary>
/// CUE-SDK: retrieves led id for key name taking logical layout into account.
/// </summary>
internal static CorsairLedIds CorsairGetLedIdForKeyName(char keyName)
{
return _corsairGetLedIdForKeyNamePointer(keyName);
}
/// <summary>
/// CUE-SDK: requestes control using specified access mode.
/// By default client has shared control over lighting so there is no need to call CorsairRequestControl unless client requires exclusive control.
/// </summary>
internal static bool CorsairRequestControl(CorsairAccessMode accessMode)
{
return _corsairRequestControlPointer(accessMode);
}
/// <summary>
/// CUE-SDK: releases previously requested control for specified access mode.
/// </summary>
internal static bool CorsairReleaseControl(CorsairAccessMode accessMode)
{
return _corsairReleaseControlPointer(accessMode);
}
/// <summary>
/// CUE-SDK: checks file and protocol version of CUE to understand which of SDK functions can be used with this version of CUE.
/// </summary>
internal static _CorsairProtocolDetails CorsairPerformProtocolHandshake()
{
return _corsairPerformProtocolHandshakePointer();
}
/// <summary>
/// CUE-SDK: returns last error that occured while using any of Corsair* functions.
/// </summary>
internal static CorsairError CorsairGetLastError()
{
return _corsairGetLastErrorPointer();
}
// ReSharper restore EventExceptionNotDocumented
#endregion
}
}
@@ -0,0 +1,47 @@
#pragma warning disable 169 // Field 'x' is never used
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// CUE-SDK: contains information about device
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal class _CorsairDeviceInfo
{
/// <summary>
/// CUE-SDK: enum describing device type
/// </summary>
internal CorsairDeviceType type;
/// <summary>
/// CUE-SDK: null - terminated device model(like “K95RGB”)
/// </summary>
internal IntPtr model;
/// <summary>
/// CUE-SDK: enum describing physical layout of the keyboard or mouse
/// </summary>
internal int physicalLayout;
/// <summary>
/// CUE-SDK: enum describing logical layout of the keyboard as set in CUE settings
/// </summary>
internal int logicalLayout;
/// <summary>
/// CUE-SDK: mask that describes device capabilities, formed as logical “or” of CorsairDeviceCaps enum values
/// </summary>
internal int capsMask;
/// <summary>
/// CUE-SDK: number of controllable LEDs on the device
/// </summary>
internal int ledsCount;
}
}
@@ -0,0 +1,36 @@
#pragma warning disable 169 // Field 'x' is never used
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// CUE-SDK: contains information about led and its color
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal class _CorsairLedColor
{
/// <summary>
/// CUE-SDK: identifier of LED to set
/// </summary>
internal int ledId;
/// <summary>
/// CUE-SDK: red brightness[0..255]
/// </summary>
internal int r;
/// <summary>
/// CUE-SDK: green brightness[0..255]
/// </summary>
internal int g;
/// <summary>
/// CUE-SDK: blue brightness[0..255]
/// </summary>
internal int b;
};
}
@@ -0,0 +1,42 @@
#pragma warning disable 169 // Field 'x' is never used
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// CUE-SDK: contains led id and position of led rectangle.Most of the keys are rectangular.
/// In case if key is not rectangular(like Enter in ISO / UK layout) it returns the smallest rectangle that fully contains the key
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal class _CorsairLedPosition
{
/// <summary>
/// CUE-SDK: identifier of led
/// </summary>
internal CorsairLedIds ledId;
/// <summary>
/// CUE-SDK: values in mm
/// </summary>
internal double top;
/// <summary>
/// CUE-SDK: values in mm
/// </summary>
internal double left;
/// <summary>
/// CUE-SDK: values in mm
/// </summary>
internal double height;
/// <summary>
/// CUE-SDK: values in mm
/// </summary>
internal double width;
}
}
@@ -0,0 +1,27 @@
#pragma warning disable 169 // Field 'x' is never used
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// CUE-SDK: contains number of leds and arrays with their positions
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal class _CorsairLedPositions
{
/// <summary>
/// CUE-SDK: integer value.Number of elements in following array
/// </summary>
internal int numberOfLed;
/// <summary>
/// CUE-SDK: array of led positions
/// </summary>
internal IntPtr pLedPosition;
}
}
@@ -0,0 +1,44 @@
#pragma warning disable 169 // Field 'x' is never used
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.Corsair.Native
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// CUE-SDK: contains information about SDK and CUE versions
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct _CorsairProtocolDetails
{
/// <summary>
/// CUE-SDK: null - terminated string containing version of SDK(like “1.0.0.1”). Always contains valid value even if there was no CUE found
/// </summary>
internal IntPtr sdkVersion;
/// <summary>
/// CUE-SDK: null - terminated string containing version of CUE(like “1.0.0.1”) or NULL if CUE was not found.
/// </summary>
internal IntPtr serverVersion;
/// <summary>
/// CUE-SDK: integer number that specifies version of protocol that is implemented by current SDK.
/// Numbering starts from 1. Always contains valid value even if there was no CUE found
/// </summary>
internal int sdkProtocolVersion;
/// <summary>
/// CUE-SDK: integer number that specifies version of protocol that is implemented by CUE.
/// Numbering starts from 1. If CUE was not found then this value will be 0
/// </summary>
internal int serverProtocolVersion;
/// <summary>
/// CUE-SDK: boolean value that specifies if there were breaking changes between version of protocol implemented by server and client
/// </summary>
internal byte breakingChanges;
};
}