Skip to content

Commit

Permalink
Merge pull request #121 from CoraleStudios/feature/temp-init-endpoints
Browse files Browse the repository at this point in the history
Add endpoints for init and uninit
  • Loading branch information
brandonscott committed Nov 24, 2015
2 parents bc29639 + a1679ad commit 27b8c27
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 39 deletions.
85 changes: 58 additions & 27 deletions Corale.Colore/Core/Chroma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public sealed class Chroma : IChroma
/// </summary>
private static readonly object InitLock = new object();

/// <summary>
/// Keeps track of whether the SDK has been initialized.
/// </summary>
private static bool _initialized;

/// <summary>
/// Holds the application-wide instance of the <see cref="IChroma" /> interface.
/// </summary>
Expand All @@ -82,12 +77,7 @@ public sealed class Chroma : IChroma
/// </summary>
private Chroma()
{
Log.Info("Chroma is initializing.");
Log.Debug("Calling SDK Init function");
NativeWrapper.Init();
_initialized = true;
Log.Debug("Resetting _registeredHandle");
_registeredHandle = IntPtr.Zero;
Initialize();
}

/// <summary>
Expand All @@ -98,11 +88,7 @@ private Chroma()
/// </remarks>
~Chroma()
{
if (!_initialized)
return;

Unregister();
NativeWrapper.UnInit();
Uninitialize();
}

/// <summary>
Expand Down Expand Up @@ -211,15 +197,10 @@ public IKeypad Keypad
}

/// <summary>
/// Gets a value indicating whether the Chroma main class has been initialized or not.
/// Gets a value indicating whether the Chroma
/// SDK has been initialized or not.
/// </summary>
internal static bool Initialized
{
get
{
return _instance != null;
}
}
public bool Initialized { get; private set; }

/// <summary>
/// Checks if the Chroma SDK is available on this system.
Expand Down Expand Up @@ -288,6 +269,57 @@ public static bool IsSdkAvailable()
return dllValid && regEnabled;
}

/// <summary>
/// Initializes the SDK if it hasn't already.
/// </summary>
/// <remarks>
/// <span style="color: red;">Manual manipulation of the SDK state is
/// <strong>not supported by the CoraleStudios team</strong> and may
/// result in <emph>undefined behaviour</emph>. Usage of this method is
/// <strong>at your own risk</strong>.</span>
/// </remarks>
public void Initialize()
{
if (Initialized)
return;

Log.Info("Chroma is initializing.");
Log.Debug("Calling SDK Init function");
NativeWrapper.Init();
Initialized = true;
Log.Debug("Resetting _registeredHandle");
_registeredHandle = IntPtr.Zero;
}

/// <summary>
/// Uninitializes the SDK if it has been initialized.
/// </summary>
/// <remarks>
/// <span style="color: red;">Manual manipulation of the SDK state is
/// <strong>not supported by the CoraleStudios team</strong> and may
/// result in <emph>undefined behaviour</emph>. Usage of this method is
/// <strong>at your own risk</strong>. Usage of SDK functions while
/// the SDK is in an <emph>uninitialized</emph> state is <strong>highly
/// advised against</strong> and <emph>WILL</emph> result in catastrophic
/// failure. <strong>YOU HAVE BEEN WARNED</strong>.</span>
/// </remarks>
public void Uninitialize()
{
if (!Initialized)
return;

((Device)Keyboard).DeleteCurrentEffect();
((Device)Mouse).DeleteCurrentEffect();
((Device)Keypad).DeleteCurrentEffect();
((Device)Mousepad).DeleteCurrentEffect();
((Device)Headset).DeleteCurrentEffect();

Unregister();
NativeWrapper.UnInit();

Initialized = false;
}

/// <summary>
/// Queries the SDK for information regarding a specific device.
/// </summary>
Expand Down Expand Up @@ -428,10 +460,9 @@ public void SetAll(Color color)
/// <remarks>
/// For internal use by singleton accessors in device interface implementations.
/// </remarks>
internal static void Initialize()
internal static void InitInstance()
{
if (!Initialized)
_instance = new Chroma();
Instance.Initialize();
}

/// <summary>
Expand Down
19 changes: 13 additions & 6 deletions Corale.Colore/Core/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,21 @@ public void Clear()
/// <param name="guid">GUID to set.</param>
public void SetGuid(Guid guid)
{
if (CurrentEffectId != Guid.Empty)
{
NativeWrapper.DeleteEffect(CurrentEffectId);
CurrentEffectId = Guid.Empty;
}

DeleteCurrentEffect();
NativeWrapper.SetEffect(guid);
CurrentEffectId = guid;
}

/// <summary>
/// Deletes the currently set effect.
/// </summary>
internal void DeleteCurrentEffect()
{
if (CurrentEffectId == Guid.Empty)
return;

NativeWrapper.DeleteEffect(CurrentEffectId);
CurrentEffectId = Guid.Empty;
}
}
}
2 changes: 1 addition & 1 deletion Corale.Colore/Core/GenericDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Guid DeviceId
[PublicAPI]
public static IGenericDevice Get(Guid deviceId)
{
Chroma.Initialize();
Chroma.InitInstance();

if (!Instances.ContainsKey(deviceId))
Instances[deviceId] = new GenericDevice(deviceId);
Expand Down
2 changes: 1 addition & 1 deletion Corale.Colore/Core/Headset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public sealed class Headset : Device, IHeadset
private Headset()
{
Log.Info("Headset is initializing");
Chroma.Initialize();
Chroma.InitInstance();
}

/// <summary>
Expand Down
29 changes: 29 additions & 0 deletions Corale.Colore/Core/IChroma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ public interface IChroma
[PublicAPI]
IKeypad Keypad { get; }

/// <summary>
/// Gets a value indicating whether the Chroma
/// SDK has been initialized or not.
/// </summary>
[PublicAPI]
bool Initialized { get; }

/// <summary>
/// Initializes the SDK if it hasn't already.
/// </summary>
/// <remarks>
/// Manually modifying the SDK init state is <b>untested</b>
/// and may result in <emph>undefined behaviour</emph>, usage
/// is at <b>your own risk</b>.
/// </remarks>
[PublicAPI]
void Initialize();

/// <summary>
/// Uninitializes the SDK if it has been initialized.
/// </summary>
/// <remarks>
/// Manually modifying the SDK init state is <b>untested</b>
/// and may result in <emph>undefined behaviour</emph>, usage
/// is at <b>your own risk</b>.
/// </remarks>
[PublicAPI]
void Uninitialize();

/// <summary>
/// Queries the SDK for information regarding a specific device.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Corale.Colore/Core/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private Keyboard()
{
Log.Info("Keyboard initializing...");

Chroma.Initialize();
Chroma.InitInstance();

CurrentEffectId = Guid.Empty;

Expand Down
2 changes: 1 addition & 1 deletion Corale.Colore/Core/Keypad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public sealed class Keypad : Device, IKeypad
private Keypad()
{
Log.Debug("Keypad is initializing");
Chroma.Initialize();
Chroma.InitInstance();

_custom = Custom.Create();
}
Expand Down
2 changes: 1 addition & 1 deletion Corale.Colore/Core/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public sealed class Mouse : Device, IMouse
private Mouse()
{
Log.Info("Mouse is initializing");
Chroma.Initialize();
Chroma.InitInstance();
_custom = Custom.Create();
_customGrid = CustomGrid.Create();
}
Expand Down
2 changes: 1 addition & 1 deletion Corale.Colore/Core/Mousepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public sealed class Mousepad : Device, IMousepad
private Mousepad()
{
Log.Debug("Mousepad is initializing.");
Chroma.Initialize();
Chroma.InitInstance();
_custom = Custom.Create();
}

Expand Down

0 comments on commit 27b8c27

Please sign in to comment.