mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2025-12-13 09:08:34 +00:00
Merge pull request #35 from DarthAffe/Development
Implemented a reinitialize-functionality
This commit is contained in:
commit
4fd4cb0a31
69
CueSDK.cs
69
CueSDK.cs
@ -1,5 +1,6 @@
|
|||||||
// ReSharper disable MemberCanBePrivate.Global
|
// ReSharper disable MemberCanBePrivate.Global
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using CUE.NET.Devices.Generic;
|
using CUE.NET.Devices.Generic;
|
||||||
using CUE.NET.Devices.Generic.Enums;
|
using CUE.NET.Devices.Generic.Enums;
|
||||||
@ -124,6 +125,74 @@ namespace CUE.NET
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reinitialize the CUE-SDK and temporarily hand back full control to CUE.
|
||||||
|
/// </summary>
|
||||||
|
public static void Reinitialize()
|
||||||
|
{
|
||||||
|
Reinitialize(HasExclusiveAccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reinitialize the CUE-SDK and temporarily hand back full control to CUE.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exclusiveAccess">Specifies whether the application should request exclusive access or not.</param>
|
||||||
|
public static void Reinitialize(bool exclusiveAccess)
|
||||||
|
{
|
||||||
|
if (ProtocolDetails == null)
|
||||||
|
throw new WrapperException("CueSDK isn't initialized.");
|
||||||
|
|
||||||
|
KeyboardSDK?.ResetLeds();
|
||||||
|
MouseSDK?.ResetLeds();
|
||||||
|
HeadsetSDK?.ResetLeds();
|
||||||
|
|
||||||
|
_CUESDK.Reload();
|
||||||
|
|
||||||
|
_CUESDK.CorsairPerformProtocolHandshake();
|
||||||
|
|
||||||
|
CorsairError error = LastError;
|
||||||
|
if (error != CorsairError.Success)
|
||||||
|
Throw(error);
|
||||||
|
|
||||||
|
if (ProtocolDetails.BreakingChanges)
|
||||||
|
throw new WrapperException("The SDK currently used isn't compatible with the installed version of CUE.\r\n" +
|
||||||
|
$"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n" +
|
||||||
|
$"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})");
|
||||||
|
|
||||||
|
if (exclusiveAccess)
|
||||||
|
if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl))
|
||||||
|
Throw(LastError);
|
||||||
|
HasExclusiveAccess = exclusiveAccess;
|
||||||
|
|
||||||
|
int deviceCount = _CUESDK.CorsairGetDeviceCount();
|
||||||
|
Dictionary<CorsairDeviceType, GenericDeviceInfo> reloadedDevices = new Dictionary<CorsairDeviceType, GenericDeviceInfo>();
|
||||||
|
for (int i = 0; i < deviceCount; i++)
|
||||||
|
{
|
||||||
|
GenericDeviceInfo info = new GenericDeviceInfo((_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)));
|
||||||
|
if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
|
||||||
|
continue; // Everything that doesn't support lighting control is useless
|
||||||
|
|
||||||
|
reloadedDevices.Add(info.Type, info);
|
||||||
|
|
||||||
|
error = LastError;
|
||||||
|
if (error != CorsairError.Success)
|
||||||
|
Throw(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (KeyboardSDK != null)
|
||||||
|
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Keyboard)
|
||||||
|
|| KeyboardSDK.KeyboardDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Keyboard].Model)
|
||||||
|
throw new WrapperException("The previously loaded Keyboard got disconnected.");
|
||||||
|
if (MouseSDK != null)
|
||||||
|
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mouse)
|
||||||
|
|| MouseSDK.MouseDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mouse].Model)
|
||||||
|
throw new WrapperException("The previously loaded Mouse got disconnected.");
|
||||||
|
if (HeadsetSDK != null)
|
||||||
|
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Headset)
|
||||||
|
|| HeadsetSDK.HeadsetDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Headset].Model)
|
||||||
|
throw new WrapperException("The previously loaded Headset got disconnected.");
|
||||||
|
}
|
||||||
|
|
||||||
private static void Throw(CorsairError error)
|
private static void Throw(CorsairError error)
|
||||||
{
|
{
|
||||||
ProtocolDetails = null;
|
ProtocolDetails = null;
|
||||||
|
|||||||
@ -296,6 +296,15 @@ namespace CUE.NET.Devices.Generic
|
|||||||
OnException?.Invoke(this, new OnExceptionEventArgs(ex));
|
OnException?.Invoke(this, new OnExceptionEventArgs(ex));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resets all loaded LEDs back to default.
|
||||||
|
/// </summary>
|
||||||
|
internal void ResetLeds()
|
||||||
|
{
|
||||||
|
foreach (CorsairLed led in Leds.Values)
|
||||||
|
led.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,12 +61,26 @@ namespace CUE.NET.Devices.Generic
|
|||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the LED to the requested color.
|
||||||
|
/// </summary>
|
||||||
internal void Update()
|
internal void Update()
|
||||||
{
|
{
|
||||||
_color = RequestedColor;
|
_color = RequestedColor;
|
||||||
IsUpdated = false;
|
IsUpdated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resets the LED back to default
|
||||||
|
/// </summary>
|
||||||
|
internal void Reset()
|
||||||
|
{
|
||||||
|
_color = Color.Transparent;
|
||||||
|
RequestedColor = Color.Transparent;
|
||||||
|
IsUpdated = false;
|
||||||
|
IsLocked = false;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,8 +44,9 @@ namespace SimpleDevTest
|
|||||||
|
|
||||||
Wait(3);
|
Wait(3);
|
||||||
|
|
||||||
keyboard.Brush = CueProfiles.LoadProfileByID()[null];
|
CueSDK.Reinitialize();
|
||||||
keyboard.Update();
|
//keyboard.Brush = CueProfiles.LoadProfileByID()[null];
|
||||||
|
//keyboard.Update();
|
||||||
|
|
||||||
Wait(3);
|
Wait(3);
|
||||||
|
|
||||||
@ -54,8 +55,8 @@ namespace SimpleDevTest
|
|||||||
// OR work with a key group containing all keys and leave the background black - this should be always the prefered solution
|
// OR work with a key group containing all keys and leave the background black - this should be always the prefered solution
|
||||||
keyboard.Brush = new SolidColorBrush(Color.Black);
|
keyboard.Brush = new SolidColorBrush(Color.Black);
|
||||||
keyboard.Update();
|
keyboard.Update();
|
||||||
keyboard.Brush = CueProfiles.LoadProfileByID()["K95 RGB Default 2"];
|
//keyboard.Brush = CueProfiles.LoadProfileByID()["K95 RGB Default 2"];
|
||||||
keyboard.Update();
|
//keyboard.Update();
|
||||||
|
|
||||||
Wait(3);
|
Wait(3);
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,8 @@ namespace CUE.NET.Native
|
|||||||
{
|
{
|
||||||
#region Libary Management
|
#region Libary Management
|
||||||
|
|
||||||
|
private static IntPtr _dllHandle = IntPtr.Zero;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the loaded architecture (x64/x86).
|
/// Gets the loaded architecture (x64/x86).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -17,72 +19,166 @@ namespace CUE.NET.Native
|
|||||||
|
|
||||||
static _CUESDK()
|
static _CUESDK()
|
||||||
{
|
{
|
||||||
|
LoadCUESDK();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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
|
// HACK: Load library at runtime to support both, x86 and x64 with one managed dll
|
||||||
LoadLibrary((LoadedArchitecture = Environment.Is64BitProcess ? "x64" : "x86") + "/CUESDK_2013.dll");
|
_dllHandle = LoadLibrary((LoadedArchitecture = Environment.Is64BitProcess ? "x64" : "x86") + "/CUESDK_2013.dll");
|
||||||
|
|
||||||
|
_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));
|
||||||
|
_corsairGetLedIdForKeyNamePointer = (CorsairGetLedIdForKeyNamePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedIdForKeyName"), typeof(CorsairGetLedIdForKeyNamePointer));
|
||||||
|
_corsairRequestControlPointer = (CorsairRequestControlPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairRequestControl"), typeof(CorsairRequestControlPointer));
|
||||||
|
_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")]
|
[DllImport("kernel32.dll")]
|
||||||
private static extern IntPtr LoadLibrary(string dllToLoad);
|
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
|
#endregion
|
||||||
|
|
||||||
#region SDK-IMPORTS
|
#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 CorsairRequestControlPointer _corsairRequestControlPointer;
|
||||||
|
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 CorsairKeyboardKeyId CorsairGetLedIdForKeyNamePointer(char keyName);
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
private delegate bool CorsairRequestControlPointer(CorsairAccessMode accessMode);
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
private delegate _CorsairProtocolDetails CorsairPerformProtocolHandshakePointer();
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
private delegate CorsairError CorsairGetLastErrorPointer();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
// ReSharper disable EventExceptionNotDocumented
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
/// 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>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static bool CorsairSetLedsColors(int size, IntPtr ledsColors)
|
||||||
internal static extern bool CorsairSetLedsColors(int size, IntPtr ledsColors);
|
{
|
||||||
|
return _corsairSetLedsColorsPointer(size, ledsColors);
|
||||||
//#if WIN64
|
}
|
||||||
// [DllImport("CUESDK.x64_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
//#else
|
|
||||||
// [DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
//#endif
|
|
||||||
//internal static extern bool CorsairSetLedsColorsAsync(int size, CorsairLedColor* ledsColors, void(*CallbackType)(void*, bool, CorsairError), void* context);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: returns number of connected Corsair devices that support lighting control.
|
/// CUE-SDK: returns number of connected Corsair devices that support lighting control.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static int CorsairGetDeviceCount()
|
||||||
internal static extern int CorsairGetDeviceCount();
|
{
|
||||||
|
return _corsairGetDeviceCountPointer();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: returns information about device at provided index
|
/// CUE-SDK: returns information about device at provided index
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static IntPtr CorsairGetDeviceInfo(int deviceIndex)
|
||||||
internal static extern IntPtr CorsairGetDeviceInfo(int deviceIndex);
|
{
|
||||||
|
return _corsairGetDeviceInfoPointer(deviceIndex);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: provides list of keyboard LEDs with their physical positions.
|
/// CUE-SDK: provides list of keyboard LEDs with their physical positions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static IntPtr CorsairGetLedPositions()
|
||||||
internal static extern IntPtr CorsairGetLedPositions();
|
{
|
||||||
|
return _corsairGetLedPositionsPointer();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: retrieves led id for key name taking logical layout into account.
|
/// CUE-SDK: retrieves led id for key name taking logical layout into account.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static CorsairKeyboardKeyId CorsairGetLedIdForKeyName(char keyName)
|
||||||
internal static extern CorsairKeyboardKeyId CorsairGetLedIdForKeyName(char keyName);
|
{
|
||||||
|
return _corsairGetLedIdForKeyNamePointer(keyName);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: requestes control using specified access mode.
|
/// 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
|
/// By default client has shared control over lighting so there is no need to call CorsairRequestControl unless client requires exclusive control
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static bool CorsairRequestControl(CorsairAccessMode accessMode)
|
||||||
internal static extern bool CorsairRequestControl(CorsairAccessMode accessMode);
|
{
|
||||||
|
return _corsairRequestControlPointer(accessMode);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: checks file and protocol version of CUE to understand which of SDK functions can be used with this version of CUE
|
/// CUE-SDK: checks file and protocol version of CUE to understand which of SDK functions can be used with this version of CUE
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static _CorsairProtocolDetails CorsairPerformProtocolHandshake()
|
||||||
internal static extern _CorsairProtocolDetails CorsairPerformProtocolHandshake();
|
{
|
||||||
|
return _corsairPerformProtocolHandshakePointer();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CUE-SDK: returns last error that occured while using any of Corsair* functions
|
/// CUE-SDK: returns last error that occured while using any of Corsair* functions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DllImport("CUESDK_2013.dll", CallingConvention = CallingConvention.Cdecl)]
|
internal static CorsairError CorsairGetLastError()
|
||||||
internal static extern CorsairError CorsairGetLastError();
|
{
|
||||||
|
return _corsairGetLastErrorPointer();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReSharper restore EventExceptionNotDocumented
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user