using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using RGB.NET.Core;
using RGB.NET.Devices.Logitech.HID;
using RGB.NET.Devices.Logitech.Native;
namespace RGB.NET.Devices.Logitech
{
///
/// Represents a device provider responsible for logitech devices.
///
public class LogitechDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
///
/// Gets the singleton instance.
///
public static LogitechDeviceProvider Instance { get; } = new LogitechDeviceProvider();
///
public bool IsInitialized { get; private set; }
///
/// Gets the loaded architecture (x64/x86).
///
public string LoadedArchitecture => _LogitechGSDK.LoadedArchitecture;
///
public IEnumerable Devices { get; private set; }
///
public bool HasExclusiveAccess => false; // Exclusive access isn't possible for logitech devices.
///
/// Gets or sets a function to get the culture for a specific device.
///
public Func GetCulture { get; set; } = () => CultureHelper.GetCurrentCulture();
#endregion
#region Constructors
private LogitechDeviceProvider()
{ }
#endregion
#region Methods
///
public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
try
{
if (IsInitialized)
_LogitechGSDK.LogiLedRestoreLighting();
}
catch
{ /* At least we tried ... */ }
IsInitialized = false;
try
{
_LogitechGSDK.Reload();
if (!_LogitechGSDK.LogiLedInit()) return false;
_LogitechGSDK.LogiLedSaveCurrentLighting();
IList devices = new List();
DeviceChecker.LoadDeviceList();
if (DeviceChecker.IsDeviceConnected)
{
LogitechRGBDevice device = new LogitechKeyboardRGBDevice(new LogitechKeyboardRGBDeviceInfo(
RGBDeviceType.Keyboard, DeviceChecker.ConnectedDeviceModel, LogitechDeviceCaps.PerKeyRGB, GetCulture()));
devices.Add(device);
try
{
device.Initialize();
}
catch
{
if (throwExceptions)
throw;
return false;
}
}
Devices = new ReadOnlyCollection(devices);
}
catch
{
if (throwExceptions)
throw;
else
return false;
}
IsInitialized = true;
return true;
}
///
public void ResetDevices()
{
_LogitechGSDK.LogiLedRestoreLighting();
}
#endregion
}
}