diff --git a/CUE.NET.csproj b/CUE.NET.csproj index d4cde53..aaa9020 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -50,8 +50,12 @@ - + + + + + diff --git a/Devices/Generic/AbstractCueDevice.cs b/Devices/Generic/AbstractCueDevice.cs index 790d550..59ed5a7 100644 --- a/Devices/Generic/AbstractCueDevice.cs +++ b/Devices/Generic/AbstractCueDevice.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using CUE.NET.Devices.Generic.Enums; +using CUE.NET.Devices.Generic.EventArgs; using CUE.NET.Effects; using CUE.NET.Native; @@ -73,6 +74,26 @@ namespace CUE.NET.Devices.Generic /// public event ExceptionEventHandler Exception; + /// + /// Occurs when the device starts updating. + /// + public event UpdatingEventHandler Updating; + + /// + /// Occurs when the device update is done. + /// + public event UpdatedEventHandler Updated; + + /// + /// Occurs when the device starts to update the leds. + /// + public event LedsUpdatingEventHandler LedsUpdating; + + /// + /// Occurs when the device updated the leds. + /// + public event LedsUpdatedEventHandler LedsUpdated; + #endregion #region Constructors @@ -158,8 +179,11 @@ namespace CUE.NET.Devices.Generic /// Performs an update for all dirty keys, or all keys if flushLeds is set to true. /// /// Specifies whether all keys (including clean ones) should be updated. - public virtual void Update(bool flushLeds = false) + public void Update(bool flushLeds = false) { + OnUpdating(); + + DeviceUpdate(); UpdateEffects(); IList> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList(); @@ -168,8 +192,16 @@ namespace CUE.NET.Devices.Generic led.Update(); UpdateLeds(ledsToUpdate); + + OnUpdated(); } + /// + /// Empty method which can be overwritten to perform device specific updates. + /// + protected virtual void DeviceUpdate() + { } + private void UpdateEffects() { List effectsToRemove = new List(); @@ -198,7 +230,7 @@ namespace CUE.NET.Devices.Generic effectsToRemove.Add(effect.Effect); } // ReSharper disable once CatchAllClause - catch (Exception ex) { ManageException(ex); } + catch (Exception ex) { OnException(ex); } } } @@ -212,31 +244,35 @@ namespace CUE.NET.Devices.Generic /// The effect to apply. protected abstract void ApplyEffect(IEffect effect); - private static void UpdateLeds(ICollection> ledsToUpdate) + private void UpdateLeds(ICollection> ledsToUpdate) { ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList(); - if (!ledsToUpdate.Any()) - return; // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array + OnLedsUpdating(ledsToUpdate); - int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); - IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count); - IntPtr addPtr = new IntPtr(ptr.ToInt64()); - foreach (KeyValuePair led in ledsToUpdate) + if (ledsToUpdate.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array { - _CorsairLedColor color = new _CorsairLedColor + int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); + IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count); + IntPtr addPtr = new IntPtr(ptr.ToInt64()); + foreach (KeyValuePair led in ledsToUpdate) { - ledId = led.Key, - r = led.Value.Color.R, - g = led.Value.Color.G, - b = led.Value.Color.B - }; + _CorsairLedColor color = new _CorsairLedColor + { + ledId = led.Key, + r = led.Value.Color.R, + g = led.Value.Color.G, + b = led.Value.Color.B + }; - Marshal.StructureToPtr(color, addPtr, false); - addPtr = new IntPtr(addPtr.ToInt64() + structSize); + Marshal.StructureToPtr(color, addPtr, false); + addPtr = new IntPtr(addPtr.ToInt64() + structSize); + } + _CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr); + Marshal.FreeHGlobal(ptr); } - _CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr); - Marshal.FreeHGlobal(ptr); + + OnLedsUpdated(ledsToUpdate); } /// @@ -286,16 +322,6 @@ namespace CUE.NET.Devices.Generic return retVal; } - /// - /// Handles the needed event-calls for an exception. - /// - /// - /// A delegate callback throws an exception. - protected void ManageException(Exception ex) - { - Exception?.Invoke(this, new ExceptionEventArgs(ex)); - } - /// /// Resets all loaded LEDs back to default. /// @@ -305,6 +331,85 @@ namespace CUE.NET.Devices.Generic led.Reset(); } + #region EventCaller + + /// + /// Handles the needed event-calls for an exception. + /// + /// The exception previously thrown. + protected virtual void OnException(Exception ex) + { + try + { + Exception?.Invoke(this, new ExceptionEventArgs(ex)); + } + catch + { + // Well ... that's not my fault + } + } + + /// + /// Handles the needed event-calls before updating. + /// + protected virtual void OnUpdating() + { + try + { + Updating?.Invoke(this, new UpdatingEventArgs()); + } + catch + { + // Well ... that's not my fault + } + } + + /// + /// Handles the needed event-calls after an update. + /// + protected virtual void OnUpdated() + { + try + { + Updated?.Invoke(this, new UpdatedEventArgs()); + } + catch + { + // Well ... that's not my fault + } + } + + /// + /// Handles the needed event-calls before the leds are updated. + /// + protected virtual void OnLedsUpdating(ICollection> updatingLeds) + { + try + { + LedsUpdating?.Invoke(this, new LedsUpdatingEventArgs(updatingLeds)); + } + catch + { + // Well ... that's not my fault + } + } + + /// + /// Handles the needed event-calls after the leds are updated. + /// + protected virtual void OnLedsUpdated(IEnumerable> updatedLeds) + { + try + { + LedsUpdated?.Invoke(this, new LedsUpdatedEventArgs(updatedLeds)); + } + catch + { + // Well ... that's not my fault + } + } + #endregion + #endregion } } diff --git a/Devices/Generic/ExceptionEventArgs.cs b/Devices/Generic/EventArgs/ExceptionEventArgs.cs similarity index 89% rename from Devices/Generic/ExceptionEventArgs.cs rename to Devices/Generic/EventArgs/ExceptionEventArgs.cs index 7d443a1..50f15e9 100644 --- a/Devices/Generic/ExceptionEventArgs.cs +++ b/Devices/Generic/EventArgs/ExceptionEventArgs.cs @@ -3,12 +3,12 @@ using System; -namespace CUE.NET.Devices.Generic +namespace CUE.NET.Devices.Generic.EventArgs { /// /// Represents the information supplied with an Exception-event. /// - public class ExceptionEventArgs : EventArgs + public class ExceptionEventArgs : System.EventArgs { #region Properties & Fields diff --git a/Devices/ICueDevice.cs b/Devices/ICueDevice.cs index ed962e6..9914e3c 100644 --- a/Devices/ICueDevice.cs +++ b/Devices/ICueDevice.cs @@ -1,8 +1,8 @@ // ReSharper disable UnusedMemberInSuper.Global // ReSharper disable UnusedMember.Global -using CUE.NET.Devices.Generic; using CUE.NET.Devices.Generic.Enums; +using CUE.NET.Devices.Generic.EventArgs; namespace CUE.NET.Devices { @@ -13,6 +13,34 @@ namespace CUE.NET.Devices /// The arguments provided by the event. public delegate void ExceptionEventHandler(object sender, ExceptionEventArgs args); + /// + /// Represents the event-handler of the Updating-event. + /// + /// The sender of the event. + /// The arguments provided by the event. + public delegate void UpdatingEventHandler(object sender, UpdatingEventArgs args); + + /// + /// Represents the event-handler of the Updated-event. + /// + /// The sender of the event. + /// The arguments provided by the event. + public delegate void UpdatedEventHandler(object sender, UpdatedEventArgs args); + + /// + /// Represents the event-handler of the LedsUpdating-event. + /// + /// The sender of the event. + /// The arguments provided by the event. + public delegate void LedsUpdatingEventHandler(object sender, LedsUpdatingEventArgs args); + + /// + /// Represents the event-handler of the LedsUpdated-event. + /// + /// The sender of the event. + /// The arguments provided by the event. + public delegate void LedsUpdatedEventHandler(object sender, LedsUpdatedEventArgs args); + /// /// Represents a generic cue device. /// @@ -33,12 +61,35 @@ namespace CUE.NET.Devices /// float UpdateFrequency { get; set; } - // ReSharper disable once EventNeverSubscribedTo.Global + // ReSharper disable EventNeverSubscribedTo.Global + /// /// Occurs when a catched exception is thrown inside the device. /// event ExceptionEventHandler Exception; + /// + /// Occurs when the device starts updating. + /// + event UpdatingEventHandler Updating; + + /// + /// Occurs when the device update is done. + /// + event UpdatedEventHandler Updated; + + /// + /// Occurs when the device starts to update the leds. + /// + event LedsUpdatingEventHandler LedsUpdating; + + /// + /// Occurs when the device updated the leds. + /// + event LedsUpdatedEventHandler LedsUpdated; + + // ReSharper restore EventNeverSubscribedTo.Global + /// /// Perform an update for all dirty keys, or all keys if flushLeds is set to true. ///