Files
Spectrum/Spectrum/Input/InputLibraries/InputLibraryManager.cs
T
whoswhip e08e971bf9 old changes
all of these changes were done MONTHS ago, but i'll try to list them out:
- simplified input library switching & implementation
- implemented makcu binary, which is their new communication, have NOT tested
- renamed existing makcu implementation to makcuLegacy since they've changed their communication format
- overall simplification of code
- formatted code via csharpier
- minimal optimizations
2026-08-30 10:32:56 -04:00

61 lines
1.8 KiB
C#

namespace Spectrum.Input.InputLibraries
{
internal sealed class InputLibraryManager
{
private IInputLibrary? _current;
private InputLibraryType _currentType = InputLibraryType.MouseEvent;
private readonly object _lock = new();
public InputLibraryType CurrentType => _currentType;
public IInputLibrary? Current => _current;
public bool Switch(InputLibraryType type)
{
lock (_lock)
{
if (_currentType == type)
return true;
_current?.Dispose();
_current = null;
_currentType = InputLibraryType.MouseEvent;
_current = type switch
{
InputLibraryType.HID => HID.HIDMain.Instance,
InputLibraryType.Arduino => Arduino.ArduinoMain.Instance,
InputLibraryType.MouseEvent => MouseEvent.MouseMain.Instance,
InputLibraryType.MakcuBinary => MakcuBinary.MakcuBinaryMain.Instance,
InputLibraryType.MakcuLegacy => MakcuLegacy.MakcuLegacyMain.Instance,
_ => MouseEvent.MouseMain.Instance,
};
if (_current == null)
return true;
if (_current.RequiresInitialization && !_current.Initialize())
{
_current.Dispose();
_current = null;
return false;
}
_currentType = type;
return true;
}
}
public void Dispose()
{
lock (_lock)
{
_current?.Dispose();
_current = null;
_currentType = InputLibraryType.MouseEvent;
}
}
}
}