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

Merge pull request #114 from DarthAffe/Fix/Dispose

Fix/dispose
This commit is contained in:
DarthAffe 2020-03-03 15:24:08 +01:00 committed by GitHub
commit d187ecb997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 246 additions and 65 deletions

View File

@ -157,8 +157,12 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() public virtual void Dispose()
{ {
SpecialDeviceParts.Clear(); try
LedMapping.Clear(); {
SpecialDeviceParts.Clear();
LedMapping.Clear();
}
catch { /* this really shouldn't happen */ }
} }
/// <summary> /// <summary>

View File

@ -139,6 +139,10 @@ namespace RGB.NET.Core
try { deviceProvider.Dispose(); } try { deviceProvider.Dispose(); }
catch { /* We do what we can */ } catch { /* We do what we can */ }
foreach (IUpdateTrigger updateTrigger in _updateTriggers)
try { updateTrigger.Dispose(); }
catch { /* We do what we can */ }
_ledGroups.Clear(); _ledGroups.Clear();
_devices = null; _devices = null;
_deviceProvider = null; _deviceProvider = null;

View File

@ -5,7 +5,7 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Represents a generic update trigger. /// Represents a generic update trigger.
/// </summary> /// </summary>
public class AbstractUpdateTrigger : AbstractBindable, IUpdateTrigger public abstract class AbstractUpdateTrigger : AbstractBindable, IUpdateTrigger
{ {
#region Events #region Events
@ -31,8 +31,7 @@ namespace RGB.NET.Core
protected virtual void OnUpdate(CustomUpdateData updateData = null) => Update?.Invoke(this, updateData); protected virtual void OnUpdate(CustomUpdateData updateData = null) => Update?.Invoke(this, updateData);
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() public abstract void Dispose();
{ }
#endregion #endregion
} }

View File

@ -144,6 +144,9 @@ namespace RGB.NET.Core
UpdateFrequency = UpdateRateHardLimit; UpdateFrequency = UpdateRateHardLimit;
} }
/// <inheritdoc />
public override void Dispose() => Stop();
#endregion #endregion
} }
} }

View File

@ -106,6 +106,9 @@ namespace RGB.NET.Core
} }
} }
/// <inheritdoc />
public override void Dispose() => Stop();
#endregion #endregion
} }
} }

View File

@ -172,7 +172,12 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
_sdk?.ReleaseControl(0); try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _sdk?.ReleaseControl(0); }
catch { /* at least we tried */ }
_sdk = null; _sdk = null;
} }

View File

@ -80,6 +80,15 @@ namespace RGB.NET.Devices.Asus
//} //}
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -228,7 +228,7 @@ namespace RGB.NET.Devices.Asus
catch { if (throwExceptions) throw; } catch { if (throwExceptions) throw; }
#endregion #endregion
UpdateTrigger?.Start(); UpdateTrigger?.Start();
Devices = new ReadOnlyCollection<IRGBDevice>(devices); Devices = new ReadOnlyCollection<IRGBDevice>(devices);
@ -267,7 +267,13 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _AsusSDK.UnloadAsusSDK(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -80,6 +80,9 @@ namespace RGB.NET.Devices.Asus
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
if ((DeviceInfo is AsusRGBDeviceInfo deviceInfo) && (deviceInfo.Handle != IntPtr.Zero)) if ((DeviceInfo is AsusRGBDeviceInfo deviceInfo) && (deviceInfo.Handle != IntPtr.Zero))
Marshal.FreeHGlobal(deviceInfo.Handle); Marshal.FreeHGlobal(deviceInfo.Handle);

View File

@ -75,7 +75,7 @@ namespace RGB.NET.Devices.Asus.Native
//_getDramColorPointer = (GetDramColorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "GetDramColor"), typeof(GetDramColorPointer)); //_getDramColorPointer = (GetDramColorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "GetDramColor"), typeof(GetDramColorPointer));
} }
private static void UnloadAsusSDK() internal static void UnloadAsusSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -108,7 +108,7 @@ namespace RGB.NET.Devices.CoolerMaster
{ {
RGBDeviceType deviceType = index.GetDeviceType(); RGBDeviceType deviceType = index.GetDeviceType();
if (deviceType == RGBDeviceType.None) continue; if (deviceType == RGBDeviceType.None) continue;
if (_CoolerMasterSDK.IsDevicePlugged(index)) if (_CoolerMasterSDK.IsDevicePlugged(index))
{ {
if (!loadFilter.HasFlag(deviceType)) continue; if (!loadFilter.HasFlag(deviceType)) continue;
@ -175,6 +175,9 @@ namespace RGB.NET.Devices.CoolerMaster
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
if (IsInitialized) if (IsInitialized)
foreach (IRGBDevice device in Devices) foreach (IRGBDevice device in Devices)
{ {
@ -185,6 +188,9 @@ namespace RGB.NET.Devices.CoolerMaster
} }
catch {/* shit happens */} catch {/* shit happens */}
} }
try { _CoolerMasterSDK.UnloadCMSDK(); }
catch { /* at least we tried */ }
} }
#endregion #endregion

View File

@ -74,6 +74,9 @@ namespace RGB.NET.Devices.CoolerMaster
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
_CoolerMasterSDK.EnableLedControl(false, DeviceInfo.DeviceIndex); _CoolerMasterSDK.EnableLedControl(false, DeviceInfo.DeviceIndex);
base.Dispose(); base.Dispose();

View File

@ -52,7 +52,7 @@ namespace RGB.NET.Devices.CoolerMaster.Native
_setAllLedColorPointer = (SetAllLedColorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "SetAllLedColor"), typeof(SetAllLedColorPointer)); _setAllLedColorPointer = (SetAllLedColorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "SetAllLedColor"), typeof(SetAllLedColorPointer));
} }
private static void UnloadCMSDK() internal static void UnloadCMSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -292,7 +292,13 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _CUESDK.UnloadCUESDK(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -122,6 +122,15 @@ namespace RGB.NET.Devices.Corsair
Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ptr);
} }
/// <inheritdoc />
public override void Dispose()
{
try { DeviceUpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -56,7 +56,7 @@ namespace RGB.NET.Devices.Corsair.Native
_corsairGetLastErrorPointer = (CorsairGetLastErrorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer)); _corsairGetLastErrorPointer = (CorsairGetLastErrorPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLastError"), typeof(CorsairGetLastErrorPointer));
} }
private static void UnloadCUESDK() internal static void UnloadCUESDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;
@ -136,7 +136,7 @@ namespace RGB.NET.Devices.Corsair.Native
#endregion #endregion
// ReSharper disable EventExceptionNotDocumented // ReSharper disable EventExceptionNotDocumented
/// <summary> /// <summary>
/// CUE-SDK: set specified LEDs to some colors. /// CUE-SDK: set specified LEDs to some colors.
/// This function set LEDs colors in the buffer which is written to the devices via CorsairSetLedsColorsFlushBuffer or CorsairSetLedsColorsFlushBufferAsync. /// This function set LEDs colors in the buffer which is written to the devices via CorsairSetLedsColorsFlushBuffer or CorsairSetLedsColorsFlushBufferAsync.
@ -151,7 +151,7 @@ namespace RGB.NET.Devices.Corsair.Native
/// This function executes synchronously, if you are concerned about delays consider using CorsairSetLedsColorsFlushBufferAsync /// This function executes synchronously, if you are concerned about delays consider using CorsairSetLedsColorsFlushBufferAsync
/// </summary> /// </summary>
internal static bool CorsairSetLedsColorsFlushBuffer() => _corsairSetLedsColorsFlushBufferPointer(); internal static bool CorsairSetLedsColorsFlushBuffer() => _corsairSetLedsColorsFlushBufferPointer();
/// <summary> /// <summary>
/// CUE-SDK: get current color for the list of requested LEDs. /// CUE-SDK: get current color for the list of requested LEDs.
/// The color should represent the actual state of the hardware LED, which could be a combination of SDK and/or CUE input. /// The color should represent the actual state of the hardware LED, which could be a combination of SDK and/or CUE input.
@ -175,7 +175,7 @@ namespace RGB.NET.Devices.Corsair.Native
/// CUE-SDK: returns information about device at provided index. /// CUE-SDK: returns information about device at provided index.
/// </summary> /// </summary>
internal static IntPtr CorsairGetDeviceInfo(int deviceIndex) => _corsairGetDeviceInfoPointer(deviceIndex); internal static IntPtr CorsairGetDeviceInfo(int deviceIndex) => _corsairGetDeviceInfoPointer(deviceIndex);
/// <summary> /// <summary>
/// CUE-SDK: provides list of keyboard or mousepad LEDs with their physical positions. /// CUE-SDK: provides list of keyboard or mousepad LEDs with their physical positions.
/// </summary> /// </summary>

View File

@ -116,7 +116,10 @@ namespace RGB.NET.Devices.DMX
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -59,6 +59,15 @@ namespace RGB.NET.Devices.DMX.E131
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => _updateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => _updateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { _updateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -70,6 +70,15 @@ namespace RGB.NET.Devices.Logitech
ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\Logitech", $"{layoutPath}.xml"), layout, true); ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\Logitech", $"{layoutPath}.xml"), layout, true);
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -182,7 +182,17 @@ namespace RGB.NET.Devices.Logitech
public void ResetDevices() => _LogitechGSDK.LogiLedRestoreLighting(); public void ResetDevices() => _LogitechGSDK.LogiLedRestoreLighting();
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() => _LogitechGSDK.LogiLedRestoreLighting(); public void Dispose()
{
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _LogitechGSDK.LogiLedRestoreLighting(); }
catch { /* at least we tried */ }
try { _LogitechGSDK.UnloadLogitechGSDK(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -55,7 +55,7 @@ namespace RGB.NET.Devices.Logitech.Native
_logiLedSetLightingForTargetZonePointer = (LogiLedSetLightingForTargetZonePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "LogiLedSetLightingForTargetZone"), typeof(LogiLedSetLightingForTargetZonePointer)); _logiLedSetLightingForTargetZonePointer = (LogiLedSetLightingForTargetZonePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "LogiLedSetLightingForTargetZone"), typeof(LogiLedSetLightingForTargetZonePointer));
} }
private static void UnloadLogitechGSDK() internal static void UnloadLogitechGSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -68,6 +68,15 @@ namespace RGB.NET.Devices.Msi
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
=> DeviceUpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is int))); => DeviceUpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is int)));
/// <inheritdoc />
public override void Dispose()
{
try { DeviceUpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -163,7 +163,13 @@ namespace RGB.NET.Devices.Msi
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _MsiSDK.UnloadMsiSDK(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -60,7 +60,7 @@ namespace RGB.NET.Devices.Msi.Native
_getErrorMessagePointer = (GetErrorMessagePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "MLAPI_GetErrorMessage"), typeof(GetErrorMessagePointer)); _getErrorMessagePointer = (GetErrorMessagePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "MLAPI_GetErrorMessage"), typeof(GetErrorMessagePointer));
} }
private static void UnloadMsiSDK() internal static void UnloadMsiSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -78,8 +78,11 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" /> /// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose() public override void Dispose()
{ {
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
Reset(); Reset();
UpdateQueue.Dispose();
base.Dispose(); base.Dispose();
} }

View File

@ -122,7 +122,10 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ } {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -81,6 +81,15 @@ namespace RGB.NET.Devices.Razer
/// </summary> /// </summary>
public void Reset() => UpdateQueue.Reset(); public void Reset() => UpdateQueue.Reset();
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -52,7 +52,7 @@ namespace RGB.NET.Devices.Razer.Native
_deleteEffectPointer = (DeleteEffectPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "DeleteEffect"), typeof(DeleteEffectPointer)); _deleteEffectPointer = (DeleteEffectPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "DeleteEffect"), typeof(DeleteEffectPointer));
} }
private static void UnloadRazerSDK() internal static void UnloadRazerSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -222,7 +222,16 @@ namespace RGB.NET.Devices.Razer
} }
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() => TryUnInit(); public void Dispose()
{
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
TryUnInit();
try { _RazerSDK.UnloadRazerSDK(); }
catch { /* at least we tried */ }
}
#endregion #endregion
} }

View File

@ -27,11 +27,11 @@ namespace RGB.NET.Devices.Roccat.Native
/// </summary> /// </summary>
internal static void Reload() internal static void Reload()
{ {
UnloadCUESDK(); UnloadRoccatSDK();
LoadCUESDK(); LoadRoccatSDK();
} }
private static void LoadCUESDK() private static void LoadRoccatSDK()
{ {
if (_dllHandle != IntPtr.Zero) return; if (_dllHandle != IntPtr.Zero) return;
@ -57,7 +57,7 @@ namespace RGB.NET.Devices.Roccat.Native
_setAllLedSfxPointer = (SetAllLedSfxPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "Set_all_LEDSFX"), typeof(SetAllLedSfxPointer)); _setAllLedSfxPointer = (SetAllLedSfxPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "Set_all_LEDSFX"), typeof(SetAllLedSfxPointer));
} }
private static void UnloadCUESDK() internal static void UnloadRoccatSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;

View File

@ -114,6 +114,9 @@ namespace RGB.NET.Devices.Roccat
try { _RoccatSDK.UnloadSDK(_sdkHandle); } try { _RoccatSDK.UnloadSDK(_sdkHandle); }
catch { /* We tried our best */} catch { /* We tried our best */}
} }
try { _RoccatSDK.UnloadRoccatSDK(); }
catch { /* at least we tried */ }
} }
#endregion #endregion

View File

@ -66,6 +66,9 @@ namespace RGB.NET.Devices.SoIP.Server
/// <inheritdoc /> /// <inheritdoc />
public override void Dispose() public override void Dispose()
{ {
try { _updateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose(); base.Dispose();
_tcpServer.Stop(); _tcpServer.Stop();

View File

@ -56,7 +56,7 @@ namespace RGB.NET.Devices.SoIP
{ {
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SoIPDeviceProvider)}"); if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(SoIPDeviceProvider)}");
_instance = this; _instance = this;
UpdateTrigger = new DeviceUpdateTrigger(); UpdateTrigger = new DeviceUpdateTrigger();
} }
@ -130,8 +130,8 @@ namespace RGB.NET.Devices.SoIP
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
foreach (IRGBDevice device in Devices) try { UpdateTrigger?.Dispose(); }
device.Dispose(); catch { /* at least we tried */ }
} }
#endregion #endregion

View File

@ -85,7 +85,9 @@ namespace RGB.NET.Devices.SteelSeries.API
internal static void Dispose() internal static void Dispose()
{ {
ResetLeds(); if (IsInitialized)
ResetLeds();
_client.Dispose(); _client.Dispose();
} }

View File

@ -83,6 +83,15 @@ namespace RGB.NET.Devices.SteelSeries
ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\SteelSeries", $"{layoutPath}.xml"), layout, true); ApplyLayoutFromFile(PathHelper.GetAbsolutePath(this, @"Layouts\SteelSeries", $"{layoutPath}.xml"), layout, true);
} }
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -117,11 +117,11 @@ namespace RGB.NET.Devices.SteelSeries
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try try { UpdateTrigger?.Dispose(); }
{ catch { /* at least we tried */ }
SteelSeriesSDK.Dispose();
} try { SteelSeriesSDK.Dispose(); }
catch {/* shit happens */} catch { /* shit happens */ }
} }
#endregion #endregion

View File

@ -73,6 +73,15 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -39,7 +39,7 @@ namespace RGB.NET.Devices.WS281X.Arduino
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class. /// Initializes a new instance of the <see cref="ArduinoWS281XDeviceDefinition"/> class.
/// </summary> /// </summary>
/// <param name="portName">The name of the serial-port to connect to.</param> /// <param name="port">The name of the serial-port to connect to.</param>
public ArduinoWS281XDeviceDefinition(string port) public ArduinoWS281XDeviceDefinition(string port)
{ {
this.Port = port; this.Port = port;
@ -50,10 +50,8 @@ namespace RGB.NET.Devices.WS281X.Arduino
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<IRGBDevice> CreateDevices() public IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger)
{ {
DeviceUpdateTrigger updateTrigger = new DeviceUpdateTrigger();
ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, Port, BaudRate); ArduinoWS2812USBUpdateQueue queue = new ArduinoWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
IEnumerable<(int channel, int ledCount)> channels = queue.GetChannels(); IEnumerable<(int channel, int ledCount)> channels = queue.GetChannels();
int counter = 0; int counter = 0;
@ -64,8 +62,6 @@ namespace RGB.NET.Devices.WS281X.Arduino
device.Initialize(ledCount); device.Initialize(ledCount);
yield return device; yield return device;
} }
updateTrigger.Start();
} }
#endregion #endregion

View File

@ -63,6 +63,15 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <inheritdoc /> /// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0)); protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -43,7 +43,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class. /// Initializes a new instance of the <see cref="BitwizardWS281XDeviceDefinition"/> class.
/// </summary> /// </summary>
/// <param name="portName">The name of the serial-port to connect to.</param> /// <param name="port">The name of the serial-port to connect to.</param>
public BitwizardWS281XDeviceDefinition(string port) public BitwizardWS281XDeviceDefinition(string port)
{ {
this.Port = port; this.Port = port;
@ -54,17 +54,13 @@ namespace RGB.NET.Devices.WS281X.Bitwizard
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<IRGBDevice> CreateDevices() public IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger)
{ {
DeviceUpdateTrigger updateTrigger = new DeviceUpdateTrigger();
BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, Port, BaudRate); BitwizardWS2812USBUpdateQueue queue = new BitwizardWS2812USBUpdateQueue(updateTrigger, Port, BaudRate);
string name = Name ?? $"Bitwizard WS2812 USB ({Port})"; string name = Name ?? $"Bitwizard WS2812 USB ({Port})";
BitwizardWS2812USBDevice device = new BitwizardWS2812USBDevice(new BitwizardWS2812USBDeviceInfo(name), queue); BitwizardWS2812USBDevice device = new BitwizardWS2812USBDevice(new BitwizardWS2812USBDeviceInfo(name), queue);
device.Initialize(StripLength); device.Initialize(StripLength);
yield return device; yield return device;
updateTrigger.Start();
} }
#endregion #endregion

View File

@ -13,6 +13,6 @@ namespace RGB.NET.Devices.WS281X
/// Gets the devices defined by this definition. /// Gets the devices defined by this definition.
/// </summary> /// </summary>
/// <returns>The initialized devices defined by this definition.</returns> /// <returns>The initialized devices defined by this definition.</returns>
IEnumerable<IRGBDevice> CreateDevices(); IEnumerable<IRGBDevice> CreateDevices(IDeviceUpdateTrigger updateTrigger);
} }
} }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using RGB.NET.Core; using RGB.NET.Core;
namespace RGB.NET.Devices.WS281X namespace RGB.NET.Devices.WS281X
@ -37,6 +38,11 @@ namespace RGB.NET.Devices.WS281X
// ReSharper disable once ReturnTypeCanBeEnumerable.Global // ReSharper disable once ReturnTypeCanBeEnumerable.Global
public List<IWS281XDeviceDefinition> DeviceDefinitions { get; } = new List<IWS281XDeviceDefinition>(); public List<IWS281XDeviceDefinition> DeviceDefinitions { get; } = new List<IWS281XDeviceDefinition>();
/// <summary>
/// The <see cref="DeviceUpdateTrigger"/> used to trigger the updates for corsair devices.
/// </summary>
public DeviceUpdateTrigger UpdateTrigger { get; }
#endregion #endregion
#region Constructors #region Constructors
@ -49,6 +55,8 @@ namespace RGB.NET.Devices.WS281X
{ {
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}"); if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(WS281XDeviceProvider)}");
_instance = this; _instance = this;
UpdateTrigger = new DeviceUpdateTrigger();
} }
#endregion #endregion
@ -69,17 +77,20 @@ namespace RGB.NET.Devices.WS281X
try try
{ {
UpdateTrigger?.Stop();
List<IRGBDevice> devices = new List<IRGBDevice>(); List<IRGBDevice> devices = new List<IRGBDevice>();
foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions) foreach (IWS281XDeviceDefinition deviceDefinition in DeviceDefinitions)
{ {
try try
{ {
devices.AddRange(deviceDefinition.CreateDevices()); devices.AddRange(deviceDefinition.CreateDevices(UpdateTrigger));
} }
catch { if (throwExceptions) throw; } catch { if (throwExceptions) throw; }
} }
Devices = devices; UpdateTrigger?.Start();
Devices = new ReadOnlyCollection<IRGBDevice>(devices);
IsInitialized = true; IsInitialized = true;
} }
catch catch
@ -99,10 +110,10 @@ namespace RGB.NET.Devices.WS281X
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
if (IsInitialized) try { UpdateTrigger?.Dispose(); }
foreach (IRGBDevice device in Devices) catch { /* at least we tried */}
if (device is IDisposable disposable)
disposable.Dispose(); DeviceDefinitions.Clear();
} }
#endregion #endregion

View File

@ -63,6 +63,15 @@ namespace RGB.NET.Devices.Wooting.Generic
/// </summary> /// </summary>
protected abstract void InitializeLayout(); protected abstract void InitializeLayout();
/// <inheritdoc />
public override void Dispose()
{
try { UpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion #endregion
} }
} }

View File

@ -52,7 +52,7 @@ namespace RGB.NET.Devices.Wooting.Native
_arraySetSinglePointer = (ArraySetSinglePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "wooting_rgb_array_set_single"), typeof(ArraySetSinglePointer)); _arraySetSinglePointer = (ArraySetSinglePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "wooting_rgb_array_set_single"), typeof(ArraySetSinglePointer));
} }
private static void UnloadWootingSDK() internal static void UnloadWootingSDK()
{ {
if (_dllHandle == IntPtr.Zero) return; if (_dllHandle == IntPtr.Zero) return;
@ -91,13 +91,13 @@ namespace RGB.NET.Devices.Wooting.Native
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr GetDeviceInfoPointer(); private delegate IntPtr GetDeviceInfoPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool KeyboardConnectedPointer(); private delegate bool KeyboardConnectedPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool ResetPointer(); private delegate bool ResetPointer();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool ArrayUpdateKeyboardPointer(); private delegate bool ArrayUpdateKeyboardPointer();

View File

@ -28,13 +28,13 @@ namespace RGB.NET.Devices.Wooting
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications. /// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
/// The first match will be used. /// The first match will be used.
/// </summary> /// </summary>
public static List<string> PossibleX86NativePaths { get; } = new List<string> {"x86/wooting-rgb-sdk.dll"}; public static List<string> PossibleX86NativePaths { get; } = new List<string> { "x86/wooting-rgb-sdk.dll" };
/// <summary> /// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications. /// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications.
/// The first match will be used. /// The first match will be used.
/// </summary> /// </summary>
public static List<string> PossibleX64NativePaths { get; } = new List<string> {"x64/wooting-rgb-sdk64.dll"}; public static List<string> PossibleX64NativePaths { get; } = new List<string> { "x64/wooting-rgb-sdk64.dll" };
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
@ -143,10 +143,14 @@ namespace RGB.NET.Devices.Wooting
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
try { UpdateTrigger?.Dispose(); }
catch { /* at least we tried */ }
try { _WootingSDK.Reset(); } try { _WootingSDK.Reset(); }
catch catch { /* Unlucky.. */ }
{ /* Unlucky.. */
} try { _WootingSDK.UnloadWootingSDK(); }
catch { /* at least we tried */ }
} }
#endregion #endregion