Added logitech support (unfinished: HID-Ids and layouts are missing)

This commit is contained in:
DarthAffe committed 2017-02-04 17:09:00 +01:00
1 parent 5bd1012fbe
commit 2af55146cc
22 files changed
+1310

No files matched your search

@@ -0,0 +1,107 @@
using System.Diagnostics;
using RGB.NET.Core;
namespace RGB.NET.Devices.Logitech
{
/// <summary>
/// Represents a Id of a <see cref="Led"/> on a <see cref="LogitechRGBDevice"/>.
/// </summary>
[DebuggerDisplay("{" + nameof(LedId) + "}")]
public class LogitechLedId : ILedId
{
#region Properties & Fields
internal readonly LogitechLedIds LedId;
/// <inheritdoc />
public IRGBDevice Device { get; }
/// <inheritdoc />
public bool IsValid => LedId != LogitechLedIds.Invalid;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LogitechLedId"/> class.
/// </summary>
/// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
/// <param name="ledId">The <see cref="LogitechLedId"/> of the represented <see cref="Led"/>.</param>
public LogitechLedId(IRGBDevice device, LogitechLedIds ledId)
{
this.Device = device;
this.LedId = ledId;
}
#endregion
#region Methods
/// <summary>
/// Converts the Id of this <see cref="LogitechLedId"/> to a human-readable string.
/// </summary>
/// <returns>A string that contains the Id of this <see cref="LogitechLedId"/>. For example "Enter".</returns>
public override string ToString()
{
return LedId.ToString();
}
/// <summary>
/// Tests whether the specified object is a <see cref="LogitechLedId" /> and is equivalent to this <see cref="LogitechLedId" />.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><c>true</c> if <paramref name="obj" /> is a <see cref="LogitechLedId" /> equivalent to this <see cref="LogitechLedId" />; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
LogitechLedId compareLedId = obj as LogitechLedId;
if (ReferenceEquals(compareLedId, null))
return false;
if (ReferenceEquals(this, compareLedId))
return true;
if (GetType() != compareLedId.GetType())
return false;
return compareLedId.LedId == LedId;
}
/// <summary>
/// Returns a hash code for this <see cref="LogitechLedId" />.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="LogitechLedId" />.</returns>
public override int GetHashCode()
{
return LedId.GetHashCode();
}
#endregion
#region Operators
/// <summary>
/// Returns a value that indicates whether two specified <see cref="LogitechLedId" /> are equal.
/// </summary>
/// <param name="ledId1">The first <see cref="LogitechLedId" /> to compare.</param>
/// <param name="ledId2">The second <see cref="LogitechLedId" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(LogitechLedId ledId1, LogitechLedId ledId2)
{
return ReferenceEquals(ledId1, null) ? ReferenceEquals(ledId2, null) : ledId1.Equals(ledId2);
}
/// <summary>
/// Returns a value that indicates whether two specified <see cref="LogitechLedId" /> are equal.
/// </summary>
/// <param name="ledId1">The first <see cref="LogitechLedId" /> to compare.</param>
/// <param name="ledId2">The second <see cref="LogitechLedId" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are not equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(LogitechLedId ledId1, LogitechLedId ledId2)
{
return !(ledId1 == ledId2);
}
#endregion
}
}
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Core.Layout;
using RGB.NET.Devices.Logitech.Native;
namespace RGB.NET.Devices.Logitech
{
/// <summary>
/// Represents a generic Logitech-device. (keyboard, mouse, headset, mousmat).
/// </summary>
public abstract class LogitechRGBDevice : AbstractRGBDevice
{
#region Properties & Fields
/// <summary>
/// Gets information about the <see cref="LogitechRGBDevice"/>.
/// </summary>
public override IRGBDeviceInfo DeviceInfo { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LogitechRGBDevice"/> class.
/// </summary>
/// <param name="info">The generic information provided by Logitech for the device.</param>
protected LogitechRGBDevice(IRGBDeviceInfo info)
{
this.DeviceInfo = info;
}
#endregion
#region Methods
/// <summary>
/// Initializes the device.
/// </summary>
internal void Initialize()
{
InitializeLayout();
if (InternalSize == null)
{
Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle));
InternalSize = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y);
}
}
/// <summary>
/// Initializes the <see cref="Led"/> and <see cref="Size"/> of the device.
/// </summary>
protected abstract void InitializeLayout();
/// <summary>
/// Applies the given layout.
/// </summary>
/// <param name="layoutPath">The file containing the layout.</param>
protected void ApplyLayoutFromFile(string layoutPath)
{
DeviceLayout layout = DeviceLayout.Load(layoutPath);
if (layout != null)
{
InternalSize = new Size(layout.Width, layout.Height);
if (layout.Leds != null)
foreach (LedLayout layoutLed in layout.Leds)
{
LogitechLedIds ledId;
if (Enum.TryParse(layoutLed.Id, true, out ledId))
{
LogitechLedId id = new LogitechLedId(this, ledId);
Led led;
if (!LedMapping.TryGetValue(id, out led))
led = InitializeLed(id, new Rectangle());
led.LedRectangle.Location.X = layoutLed.X;
led.LedRectangle.Location.Y = layoutLed.Y;
led.LedRectangle.Size.Width = layoutLed.Width;
led.LedRectangle.Size.Height = layoutLed.Height;
led.Shape = layoutLed.Shape;
}
}
}
}
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList();
foreach (Led led in leds)
_LogitechGSDK.LogiLedSetLightingForKeyWithKeyName((int)((LogitechLedId)led.Id).LedId,
(int)Math.Round(led.Color.RPercent * 100),
(int)Math.Round(led.Color.GPercent * 100),
(int)Math.Round(led.Color.BPercent * 100));
}
#endregion
}
}
@@ -0,0 +1,64 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Devices.Logitech
{
/// <summary>
/// Represents a generic information for a Logitech-<see cref="IRGBDevice"/>.
/// </summary>
public class LogitechRGBDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
/// <inheritdoc />
public RGBDeviceType DeviceType { get; }
/// <inheritdoc />
public string Manufacturer => "Logitech";
/// <inheritdoc />
public string Model { get; }
/// <inheritdoc />
public Uri Image { get; protected set; }
/// <inheritdoc />
public RGBDeviceLighting Lighting
{
get
{
if (DeviceCaps.HasFlag(LogitechDeviceCaps.PerKeyRGB))
return RGBDeviceLighting.Key;
if (DeviceCaps.HasFlag(LogitechDeviceCaps.DeviceRGB))
return RGBDeviceLighting.Keyboard;
return RGBDeviceLighting.None;
}
}
/// <summary>
/// Gets a flag that describes device capabilities. (<see cref="LogitechDeviceCaps" />)
/// </summary>
public LogitechDeviceCaps DeviceCaps { get; }
#endregion
#region Constructors
/// <summary>
/// Internal constructor of managed <see cref="LogitechRGBDeviceInfo"/>.
/// </summary>
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="model">The represented device model.</param>
/// <param name="deviceCaps">The lighting-capabilities of the device.</param>
internal LogitechRGBDeviceInfo(RGBDeviceType deviceType, string model, LogitechDeviceCaps deviceCaps)
{
this.DeviceType = deviceType;
this.Model = model;
this.DeviceCaps = deviceCaps;
}
#endregion
}
}