Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #65 from DarthAffe/SdkUpdate
Browse files Browse the repository at this point in the history
Sdk update
  • Loading branch information
DarthAffe authored Nov 18, 2017
2 parents 220953b + 5091b33 commit 8bcf64a
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CUE.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Generic\Enums\CorsairKeyId.cs" />
<Compile Include="Devices\Generic\Enums\CorsairLedId.cs" />
<Compile Include="Devices\Generic\EventArgs\ExceptionEventArgs.cs" />
<Compile Include="Brushes\AbstractBrush.cs" />
Expand All @@ -67,7 +68,12 @@
<Compile Include="Devices\Generic\EventArgs\UpdatedEventArgs.cs" />
<Compile Include="Devices\Generic\EventArgs\UpdatingEventArgs.cs" />
<Compile Include="Devices\Generic\LedUpateRequest.cs" />
<Compile Include="Devices\HeadsetStand\CorsairHeadsetStand.cs" />
<Compile Include="Devices\HeadsetStand\CorsairHeadsetStandDeviceInfo.cs" />
<Compile Include="Devices\HeadsetStand\Enums\CorsairHeadsetStandLedId.cs" />
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairMouseKeyId.cs" />
<Compile Include="Effects\AbstractLedGroupEffect.cs" />
<Compile Include="Effects\AbstractBrushEffect.cs" />
<Compile Include="Effects\AbstractEffectTarget.cs" />
Expand All @@ -76,6 +82,7 @@
<Compile Include="Devices\Mousemat\CorsairMousematDeviceInfo.cs" />
<Compile Include="Devices\Mousemat\Enums\CorsairMousematLedId.cs" />
<Compile Include="Effects\MoveGradientEffect.cs" />
<Compile Include="EventArgs\KeyPressedEventArgs.cs" />
<Compile Include="Gradients\AbstractGradient.cs" />
<Compile Include="Gradients\GradientStop.cs" />
<Compile Include="Gradients\IGradient.cs" />
Expand Down
58 changes: 57 additions & 1 deletion CueSDK.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset;
using CUE.NET.Devices.HeadsetStand;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Mouse;
using CUE.NET.Devices.Mousemat;
using CUE.NET.EventArgs;
using CUE.NET.Exceptions;
using CUE.NET.Native;

Expand Down Expand Up @@ -86,13 +89,33 @@ public static partial class CueSDK
public static CorsairHeadset HeadsetSDK { get; private set; }

/// <summary>
/// Gets the managed representation of a moustmat managed by the CUE-SDK.
/// Gets the managed representation of a mousemat managed by the CUE-SDK.
/// Note that currently only one connected mousemat is supported.
/// </summary>
public static CorsairMousemat MousematSDK { get; private set; }

/// <summary>
/// Gets the managed representation of a headset stand managed by the CUE-SDK.
/// Note that currently only one connected headset stand is supported.
/// </summary>
public static CorsairHeadsetStand HeadsetStandSDK { get; private set; }

// ReSharper restore UnusedAutoPropertyAccessor.Global

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void OnKeyPressedDelegate(IntPtr context, CorsairKeyId keyId, [MarshalAs(UnmanagedType.I1)] bool pressed);
private static readonly OnKeyPressedDelegate _onKeyPressedDelegate = OnKeyPressed;

#endregion

#region Events

/// <summary>
/// Occurs when the SDK reports that a key is pressed.
/// Notice that right now only G- (keyboard) and M- (mouse) keys are supported.
/// </summary>
public static event EventHandler<KeyPressedEventArgs> KeyPressed;

#endregion

#region Methods
Expand Down Expand Up @@ -120,6 +143,8 @@ public static bool IsSDKAvailable(CorsairDeviceType? sdkType = null)
return HeadsetSDK != null;
case CorsairDeviceType.Mousemat:
return MousematSDK != null;
case CorsairDeviceType.HeadsetStand:
return HeadsetStandSDK != null;
default:
return true;
}
Expand Down Expand Up @@ -205,6 +230,9 @@ public static void Initialize(bool exclusiveAccess = false)
case CorsairDeviceType.Mousemat:
device = MousematSDK = new CorsairMousemat(new CorsairMousematDeviceInfo(nativeDeviceInfo));
break;
case CorsairDeviceType.HeadsetStand:
device = HeadsetStandSDK = new CorsairHeadsetStand(new CorsairHeadsetStandDeviceInfo(nativeDeviceInfo));
break;
// ReSharper disable once RedundantCaseLabel
case CorsairDeviceType.Unknown:
default:
Expand All @@ -219,11 +247,25 @@ public static void Initialize(bool exclusiveAccess = false)
Throw(error, true);
}

_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
error = LastError;
if (error != CorsairError.Success)
Throw(error, false);

InitializedDevices = new ReadOnlyCollection<ICueDevice>(devices);

IsInitialized = true;
}

/// <summary>
/// Resets the colors of all devices back to the last saved color-data. (If there wasn't a manual save, that's the data from the time the SDK was initialized.)
/// </summary>
public static void Reset()
{
foreach (ICueDevice device in InitializedDevices)
device.RestoreColors();
}

/// <summary>
/// Reinitialize the CUE-SDK and temporarily hand back full control to CUE.
/// </summary>
Expand All @@ -245,6 +287,7 @@ public static void Reinitialize(bool exclusiveAccess)
MouseSDK?.ResetLeds();
HeadsetSDK?.ResetLeds();
MousematSDK?.ResetLeds();
HeadsetStandSDK?.ResetLeds();

_CUESDK.Reload();

Expand Down Expand Up @@ -295,6 +338,15 @@ public static void Reinitialize(bool exclusiveAccess)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mousemat)
|| MousematSDK.MousematDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mousemat].Model)
throw new WrapperException("The previously loaded Mousemat got disconnected.");
if (HeadsetStandSDK != null)
if (!reloadedDevices.ContainsKey(CorsairDeviceType.HeadsetStand)
|| HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
throw new WrapperException("The previously loaded Headset Stand got disconnected.");

_CUESDK.CorsairRegisterKeypressCallback(Marshal.GetFunctionPointerForDelegate(_onKeyPressedDelegate), IntPtr.Zero);
error = LastError;
if (error != CorsairError.Success)
Throw(error, false);

IsInitialized = true;
}
Expand All @@ -309,12 +361,16 @@ private static void Throw(CorsairError error, bool reset)
MouseSDK = null;
HeadsetSDK = null;
MousematSDK = null;
HeadsetStandSDK = null;
IsInitialized = false;
}

throw new CUEException(error);
}

private static void OnKeyPressed(IntPtr context, CorsairKeyId keyId, bool pressed)
=> KeyPressed?.Invoke(null, new KeyPressedEventArgs(keyId, pressed));

#endregion
}
}
88 changes: 78 additions & 10 deletions Devices/Generic/AbstractCueDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public abstract class AbstractCueDevice : ICueDevice

private static DateTime _lastUpdate = DateTime.Now;

private Dictionary<CorsairLedId, CorsairColor> _colorDataSave;

/// <summary>
/// Gets generic information provided by CUE for the device.
/// </summary>
Expand Down Expand Up @@ -56,6 +58,7 @@ public abstract class AbstractCueDevice : ICueDevice

/// <summary>
/// Gets or sets the background brush of the keyboard.
/// If this is null the last saved color-data is used as background.
/// </summary>
public IBrush Brush { get; set; }

Expand Down Expand Up @@ -153,6 +156,7 @@ protected AbstractCueDevice(IDeviceInfo info)
public virtual void Initialize()
{
DeviceRectangle = RectangleHelper.CreateRectangleFromRectangles((this).Select(x => x.LedRectangle));
SaveColors();
}

/// <summary>
Expand Down Expand Up @@ -187,19 +191,26 @@ internal void ResetLeds()
/// Performs an update for all dirty keys, or all keys if flushLeds is set to true.
/// </summary>
/// <param name="flushLeds">Specifies whether all keys (including clean ones) should be updated.</param>
public void Update(bool flushLeds = false)
/// <param name="noRender">Only updates the hardware-leds skippin effects and the render-pass. Only use this if you know what that means!</param>
public void Update(bool flushLeds = false, bool noRender = false)
{
OnUpdating();

// Update effects
foreach (ILedGroup ledGroup in LedGroups)
ledGroup.UpdateEffects();

// Render brushes
Render(this);
foreach (ILedGroup ledGroup in LedGroups.OrderBy(x => x.ZIndex))
Render(ledGroup);

if (!noRender)
{
// Update effects
foreach (ILedGroup ledGroup in LedGroups)
ledGroup.UpdateEffects();

// Render brushes
if (Brush != null)
Render(this);
else
ApplyColorData(_colorDataSave);

foreach (ILedGroup ledGroup in LedGroups.OrderBy(x => x.ZIndex))
Render(ledGroup);
}
// Device-specific updates
DeviceUpdate();

Expand Down Expand Up @@ -293,6 +304,63 @@ private void UpdateLeds(ICollection<LedUpateRequest> updateRequests)
OnLedsUpdated(updateRequests);
}

/// <inheritdoc />
public void SyncColors()
{
Dictionary<CorsairLedId, CorsairColor> colorData = GetColors();
ApplyColorData(colorData);
Update(true, true);
}

/// <inheritdoc />
public void SaveColors()
{
_colorDataSave = GetColors();
}

/// <inheritdoc />
public void RestoreColors()
{
ApplyColorData(_colorDataSave);
Update(true, true);
}

private void ApplyColorData(Dictionary<CorsairLedId, CorsairColor> colorData)
{
if (colorData == null) return;

foreach (KeyValuePair<CorsairLedId, CorsairColor> corsairColor in _colorDataSave)
LedMapping[corsairColor.Key].Color = corsairColor.Value;
}

private Dictionary<CorsairLedId, CorsairColor> GetColors()
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * LedMapping.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<CorsairLedId, CorsairLed> led in LedMapping)
{
_CorsairLedColor color = new _CorsairLedColor { ledId = (int)led.Value.Id };
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairGetLedsColors(LedMapping.Count, ptr);

IntPtr readPtr = ptr;
Dictionary<CorsairLedId, CorsairColor> colorData = new Dictionary<CorsairLedId, CorsairColor>();
for (int i = 0; i < LedMapping.Count; i++)
{
_CorsairLedColor ledColor = (_CorsairLedColor)Marshal.PtrToStructure(readPtr, typeof(_CorsairLedColor));
colorData.Add((CorsairLedId)ledColor.ledId, new CorsairColor((byte)ledColor.r, (byte)ledColor.g, (byte)ledColor.b));

readPtr = new IntPtr(readPtr.ToInt64() + structSize);
}

Marshal.FreeHGlobal(ptr);

return colorData;
}

#endregion

#region LedGroup
Expand Down
3 changes: 2 additions & 1 deletion Devices/Generic/Enums/CorsairDeviceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum CorsairDeviceType
Mouse = 1,
Keyboard = 2,
Headset = 3,
Mousemat = 4
Mousemat = 4,
HeadsetStand = 5
};
}
46 changes: 46 additions & 0 deletions Devices/Generic/Enums/CorsairKeyId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ReSharper disable InconsistentNaming

#pragma warning disable 1591 // Missing XML comment for publicly visible type or member

namespace CUE.NET.Devices.Generic.Enums
{
/// <summary>
/// Contains list of all KeyIds available for all corsair devices.
/// </summary>
public enum CorsairKeyId
{
Invalid = 0,

G1 = 1,
G2 = 2,
G3 = 3,
G4 = 4,
G5 = 5,
G6 = 6,
G7 = 7,
G8 = 8,
G9 = 9,
G10 = 10,
G11 = 11,
G12 = 12,
G13 = 13,
G14 = 14,
G15 = 15,
G16 = 16,
G17 = 17,
G18 = 18,

M1 = 19,
M2 = 20,
M3 = 21,
M4 = 22,
M5 = 23,
M6 = 24,
M7 = 25,
M8 = 26,
M9 = 27,
M10 = 28,
M11 = 29,
M12 = 30,
}
}
10 changes: 10 additions & 0 deletions Devices/Generic/Enums/CorsairLedId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,15 @@ public enum CorsairLedId
Lightbar17 = 186,
Lightbar18 = 187,
Lightbar19 = 188,

HeadsetStandZone1 = 191,
HeadsetStandZone2 = 192,
HeadsetStandZone3 = 193,
HeadsetStandZone4 = 194,
HeadsetStandZone5 = 195,
HeadsetStandZone6 = 196,
HeadsetStandZone7 = 197,
HeadsetStandZone8 = 198,
HeadsetStandZone9 = 199,
}
}
Loading

0 comments on commit 8bcf64a

Please sign in to comment.