1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-13 00:58:31 +00:00

Merge pull request #34 from DarthAffe/Development

This commit is contained in:
DarthAffe 2016-02-07 10:29:38 +01:00
commit 4788204297
24 changed files with 543 additions and 9 deletions

View File

@ -1,4 +1,6 @@
using System.Drawing;
// ReSharper disable VirtualMemberNeverOverriden.Global
using System.Drawing;
using CUE.NET.Helper;
namespace CUE.NET.Brushes

View File

@ -1,3 +1,5 @@
// ReSharper disable UnusedMemberInSuper.Global
using System.Drawing;
namespace CUE.NET.Brushes

View File

@ -2,6 +2,7 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System.Drawing;
using CUE.NET.Gradients;

54
Brushes/ProfileBrush.cs Normal file
View File

@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Keys;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a brush drawing the lighting of a CUE profile.
/// </summary>
public class ProfileBrush : AbstractBrush
{
#region Properties & Fields
private Dictionary<CorsairKeyboardKeyId, Color> _keyLights;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ProfileBrush"/> class.
/// </summary>
/// <param name="keyLights">The light settings of the CUE profile.</param>
internal ProfileBrush(Dictionary<CorsairKeyboardKeyId, Color> keyLights)
{
this._keyLights = keyLights;
}
#endregion
#region Methods
/// <summary>
/// Gets the color at an specific point getting the color of the key at the given point.
/// </summary>
/// <param name="rectangle">The rectangle in which the brush should be drawn.</param>
/// <param name="point">The point from which the color should be taken.</param>
/// <returns>The color of the key at the specified point.</returns>
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
CorsairKey key = CueSDK.KeyboardSDK[point];
if (key == null) return Color.Transparent;
Color color;
if (!_keyLights.TryGetValue(key.KeyId, out color))
return Color.Transparent;
return FinalizeColor(color);
}
#endregion
}
}

View File

@ -1,5 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Drawing;

View File

@ -1,4 +1,6 @@
using System;
// ReSharper disable UnusedMember.Global
using System;
using System.Drawing;
using CUE.NET.Helper;

View File

@ -45,6 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Brushes\ProfileBrush.cs" />
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
@ -97,6 +98,10 @@
<Compile Include="Native\_CorsairLedPositions.cs" />
<Compile Include="Native\_CorsairProtocolDetails.cs" />
<Compile Include="Native\_CUESDK.cs" />
<Compile Include="Profiles\CueProfile.cs" />
<Compile Include="Profiles\CueProfileDevice.cs" />
<Compile Include="Profiles\CueProfileMode.cs" />
<Compile Include="Profiles\CueProfiles.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Devices\Generic\Enums\CorsairError.cs" />
<Compile Include="Devices\Keyboard\CorsairKeyboardDeviceInfo.cs" />

View File

@ -1,4 +1,7 @@
using System;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMethodReturnValue.Global
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

View File

@ -1,5 +1,6 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections;
using System.Collections.Generic;

View File

@ -1,4 +1,7 @@
using CUE.NET.Devices.Generic;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices

View File

@ -1,4 +1,7 @@
using CUE.NET.Devices.Generic.Enums;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices
{

View File

@ -1,4 +1,5 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Linq;
using CUE.NET.Devices.Keyboard.Enums;

View File

@ -1,4 +1,7 @@
using System.Collections.Generic;
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;

View File

@ -1,4 +1,5 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using CUE.NET.Devices.Keyboard.Enums;

View File

@ -1,5 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Drawing;

View File

@ -2,7 +2,6 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

View File

@ -1,5 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Drawing;

View File

@ -1,4 +1,7 @@
using System.Collections.Generic;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMemberInSuper.Global
using System.Collections.Generic;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;

View File

@ -11,6 +11,7 @@ using CUE.NET.Devices.Keyboard.Extensions;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Exceptions;
using CUE.NET.Gradients;
using CUE.NET.Profiles;
namespace SimpleDevTest
{
@ -38,6 +39,40 @@ namespace SimpleDevTest
if (keyboard == null)
throw new WrapperException("No keyboard found");
keyboard.Brush = new SolidColorBrush(Color.Black);
keyboard.Update();
Wait(3);
keyboard.Brush = CueProfiles.LoadProfileByID()[null];
keyboard.Update();
Wait(3);
// My Profile 'K95 RGB Default 2' is all black - this could lead to different behavior than cue has since transparent isn't black in CUE.NET
// To swap a profile like CUE does we would need to black out the keyboard before
// 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.Update();
keyboard.Brush = CueProfiles.LoadProfileByID()["K95 RGB Default 2"];
keyboard.Update();
Wait(3);
return;
ListKeyGroup keyGroup = new ListKeyGroup(keyboard, keyboard['R'].KeyId);
keyGroup.Brush = new SolidColorBrush(Color.White);
keyboard.Update();
Wait(2);
keyGroup.RemoveKey(keyboard['R'].KeyId);
keyboard['R'].Led.Color = Color.Black;
keyGroup.AddKey(keyboard['T'].KeyId);
keyboard.Update();
Wait(10);
return;
// ---------------------------------------------------------------------------
// First we'll look at some basic coloring

View File

@ -1,4 +1,6 @@
using System.Drawing;
// ReSharper disable UnusedMember.Global
using System.Drawing;
using System.Linq;
namespace CUE.NET.Gradients

105
Profiles/CueProfile.cs Normal file
View File

@ -0,0 +1,105 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using CUE.NET.Brushes;
namespace CUE.NET.Profiles
{
/// <summary>
/// Represents a CUE profile.
/// </summary>
public class CueProfile
{
#region Properties & Fields
private Dictionary<string, CueProfileDevice> _devices;
/// <summary>
/// Gets the Id of the profile.
/// </summary>
public string Id { get; }
/// <summary>
/// Gets the Name of the profile.
/// </summary>
public string Name { get; }
/// <summary>
/// Returns a list of strings containing the name of all modes available.
/// </summary>
public IEnumerable<string> Modes
{
get
{
string device = _devices.Keys.FirstOrDefault();
CueProfileDevice cpd;
return (device != null && _devices.TryGetValue(device, out cpd)) ? cpd.Modes : new string[0];
}
}
/// <summary>
/// Returns the <see cref="ProfileBrush"/> for the given mode.
/// </summary>
/// <param name="mode">The mode to select.</param>
/// <returns>The <see cref="ProfileBrush"/> of the given mode.</returns>
public ProfileBrush this[string mode]
{
get
{
string device = CueSDK.KeyboardSDK?.KeyboardDeviceInfo?.Model;
CueProfileDevice cpd;
return (device != null && _devices.TryGetValue(device, out cpd)) ? cpd[mode] : null;
}
}
#endregion
#region Constructors
private CueProfile(string id, string name)
{
this.Id = id;
this.Name = name;
}
#endregion
#region Methods
/// <summary>
/// Loads a CUE profile from the given file.
/// </summary>
/// <param name="file">The profile-file.</param>
/// <returns>The loaded <see cref="CueProfile" /> or null.</returns>
internal static CueProfile Load(string file)
{
// ReSharper disable PossibleNullReferenceException - Just let it fail - no need to check anything here ...
try
{
if (!File.Exists(file)) return null;
XElement profileRoot = XDocument.Load(file).Root;
return new CueProfile(profileRoot.Element("id").Value, profileRoot.Element("name").Value)
{
_devices = profileRoot.Element("devices").Elements("device")
.Select(CueProfileDevice.Load)
.Where(x => x != null)
.ToDictionary(x => x.Name)
};
}
// ReSharper disable once CatchAllClause - I have no idea how the factory pattern should handle such a case - time to read :p
catch
{
return null;
}
// ReSharper restore PossibleNullReferenceException
}
#endregion
}
}

View File

@ -0,0 +1,91 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using CUE.NET.Brushes;
namespace CUE.NET.Profiles
{
/// <summary>
/// Represents a device of a CUE profile.
/// </summary>
internal class CueProfileDevice
{
#region Properties & Fields
/// <summary>
/// The name of the device.
/// </summary>
internal string Name { get; }
/// <summary>
/// Returns a list of strings containing the name of all modes available for this device.
/// </summary>
internal IEnumerable<string> Modes => _modes.Keys.ToList();
private Dictionary<string, CueProfileMode> _modes;
#endregion
#region Brush Conversion
/// <summary>
/// Returns the <see cref="ProfileBrush"/> for the given mode.
/// </summary>
/// <param name="mode">The mode to select.</param>
/// <returns>The <see cref="ProfileBrush"/> of the given mode.</returns>
internal ProfileBrush this[string mode]
{
get
{
if (mode == null)
mode = _modes.Keys.FirstOrDefault();
CueProfileMode cpm;
return (mode != null && _modes.TryGetValue(mode, out cpm)) ? cpm : null;
}
}
#endregion
#region Constructors
private CueProfileDevice(string name)
{
this.Name = name;
}
#endregion
#region Methods
/// <summary>
/// Loads a device of a CUE profile from the given XML-node.
/// </summary>
/// <param name="deviceRoot">The node containing the device.</param>
/// <returns>The loaded <see cref="CueProfileDevice" /> or null.</returns>
internal static CueProfileDevice Load(XElement deviceRoot)
{
// ReSharper disable PossibleNullReferenceException - Just let it fail - no need to check anything here ...
try
{
if (deviceRoot == null) return null;
return new CueProfileDevice(deviceRoot.Element("modelName").Value)
{
_modes = deviceRoot.Element("modes").Elements("mode")
.Select(CueProfileMode.Load)
.Where(x => x != null)
.ToDictionary(x => x.Name)
};
}
// ReSharper disable once CatchAllClause - I have no idea how the factory pattern should handle such a case - time to read :p
catch
{
return null;
}
// ReSharper restore PossibleNullReferenceException
}
#endregion
}
}

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using CUE.NET.Brushes;
using CUE.NET.Devices.Keyboard.Enums;
namespace CUE.NET.Profiles
{
/// <summary>
/// Represents a mode of a CUE profile.
/// </summary>
internal class CueProfileMode
{
#region Properties & Fields
/// <summary>
/// Gets the name of the mode.
/// </summary>
internal string Name { get; }
private Dictionary<CorsairKeyboardKeyId, Color> _keyLights;
#endregion
#region Brush Conversion
/// <summary>
/// Converts a <see cref="CueProfileMode" /> to a <see cref="ProfileBrush" />.
/// </summary>
/// <param name="profile">The profile mode to convert.</param>
public static implicit operator ProfileBrush(CueProfileMode profile)
{
return profile != null ? new ProfileBrush(profile._keyLights) : null;
}
#endregion
#region Constructors
private CueProfileMode(string name)
{
this.Name = name;
}
#endregion
#region Methods
/// <summary>
/// Loads a mode of a CUE profile from the given XML-node.
/// </summary>
/// <param name="modeRoot">The node containing the mode.</param>
/// <returns>The loaded <see cref="CueProfileMode" /> or null.</returns>
internal static CueProfileMode Load(XElement modeRoot)
{
// ReSharper disable PossibleNullReferenceException - Just let it fail - no need to check anything here ...
try
{
if (modeRoot == null) return null;
return new CueProfileMode(modeRoot.Element("name").Value)
{
_keyLights = modeRoot.Element("lightBackgrounds").Element("keyBgLights").Elements("lightBackground")
.Select(x =>
{
string name = x.Attribute("key").Value;
if (name.Length == 1 && char.IsDigit(name[0])) // Our enum names can't be digit only so we need to map them
name = 'D' + name;
return new
{
key = (CorsairKeyboardKeyId)Enum.Parse(typeof(CorsairKeyboardKeyId), name),
color = ColorTranslator.FromHtml(x.Attribute("color").Value)
};
})
.ToDictionary(x => x.key, x => x.color)
};
}
// ReSharper disable once CatchAllClause - I have no idea how the factory pattern should handle such a case - time to read :p
catch
{
return null;
}
// ReSharper restore PossibleNullReferenceException
}
#endregion
}
}

124
Profiles/CueProfiles.cs Normal file
View File

@ -0,0 +1,124 @@
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace CUE.NET.Profiles
{
/// <summary>
/// Represents the SDK for CUE profiles.
/// </summary>
public static class CueProfiles
{
#region Constants
private const string PROFILE_EXTENSION = ".prf";
private static readonly string PROFILE_FOLDER = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Corsair", "HID", "Profiles");
private static readonly string CONFIG_FILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Corsair", "HID", "config.cfg");
#endregion
#region Properties & Fields
private static Dictionary<string, string> _profileNameMapping = new Dictionary<string, string>();
/// <summary>
/// Gets a list containing the names of all existing profiles.
/// </summary>
public static List<string> ProfileNames
{
get
{
LoadProfileNames();
return _profileNameMapping.Keys.ToList();
}
}
/// <summary>
/// Gets a list containing the ids of all existing profiles.
/// </summary>
public static List<string> ProfileIds
{
get
{
LoadProfileNames();
return _profileNameMapping.Values.ToList();
}
}
#endregion
#region Methods
/// <summary>
/// Loads the profile with the given name.
/// </summary>
/// <param name="name">The name (the one given in CUE, not the filename) of the profile.</param>
/// <returns>The loaded <see cref="CueProfile" /> or null if it couldn't be loaded.</returns>
public static CueProfile LoadProfileByName(string name = null)
{
string id = null;
if (name != null && !_profileNameMapping.TryGetValue(name, out id))
{
LoadProfileNames(); // Reload and try again
if (!_profileNameMapping.TryGetValue(name, out id))
return null;
}
return LoadProfileByID(id);
}
/// <summary>
/// Loads the profile with the given id.
/// </summary>
/// <param name="id">The id of the profile.</param>
/// <returns>The loaded <see cref="CueProfile" /> or null if it couldn't be loaded.</returns>
public static CueProfile LoadProfileByID(string id = null)
{
if (id == null) id = GetDefaultProfileId();
return CueProfile.Load(Path.Combine(PROFILE_FOLDER, id + PROFILE_EXTENSION));
}
private static string GetDefaultProfileId()
{
try
{
return XDocument.Load(CONFIG_FILE).Root?.Elements("value").FirstOrDefault(x => string.Equals(x.Attribute("name")?.Value, "InitialProfile", StringComparison.OrdinalIgnoreCase))?.Value;
}
// ReSharper disable once CatchAllClause - This shouldn't happen but you never know ...
catch
{
return null;
}
}
private static void LoadProfileNames()
{
try
{
IEnumerable<string> profileFiles = Directory.GetFiles(PROFILE_FOLDER).Where(x => x.EndsWith(PROFILE_EXTENSION));
foreach (string profileFile in profileFiles)
{
XElement profileNode = XDocument.Load(profileFile).Root;
if (profileNode == null) continue;
string name = profileNode.Element("name")?.Value;
string id = profileNode.Element("id")?.Value;
if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(id) && !_profileNameMapping.ContainsKey(name)) // I think duplicates are an error case
_profileNameMapping.Add(name, id);
}
}
// ReSharper disable once CatchAllClause - This shouldn't happen but you never know ...
catch
{
_profileNameMapping.Clear();
}
}
#endregion
}
}