Ready to test in Fab
This commit is contained in:
@ -4,59 +4,42 @@ namespace Barcode.Host.Shared.KeyboardMouse;
|
||||
|
||||
public class InputReader : IDisposable
|
||||
{
|
||||
private readonly ILogger<InputReader> _Logger;
|
||||
|
||||
private long _Ticks;
|
||||
private bool _Disposing;
|
||||
private readonly int _PiOffset;
|
||||
private const int _BufferLength = 24;
|
||||
|
||||
private static readonly int _PiOffset;
|
||||
|
||||
private readonly FileStream? _FileStream;
|
||||
private readonly ILogger<IDisposable> _Logger;
|
||||
private readonly byte[] _Buffer = new byte[_BufferLength];
|
||||
|
||||
private FileStream? _Stream;
|
||||
private bool _Disposing;
|
||||
|
||||
public delegate void RaiseKeyPress(KeyPressEvent e);
|
||||
|
||||
public delegate void RaiseMouseMove(MouseMoveEvent e);
|
||||
|
||||
public event RaiseKeyPress? OnKeyPress;
|
||||
|
||||
public event RaiseMouseMove? OnMouseMove;
|
||||
|
||||
public string Path { get; }
|
||||
|
||||
public string Path { get; init; }
|
||||
public bool Faulted { get; private set; }
|
||||
|
||||
static InputReader()
|
||||
{
|
||||
if (RunningOnRaspberryPi())
|
||||
{
|
||||
_PiOffset = -8;
|
||||
}
|
||||
}
|
||||
public event RaiseKeyPress? OnKeyPress;
|
||||
public event RaiseMouseMove? OnMouseMove;
|
||||
|
||||
public InputReader(
|
||||
string path,
|
||||
ILogger<InputReader> logger)
|
||||
{
|
||||
_Logger = logger;
|
||||
public delegate void RaiseKeyPress(KeyPressEvent e);
|
||||
public delegate void RaiseMouseMove(MouseMoveEvent e);
|
||||
|
||||
public InputReader(string path, ILogger<IDisposable> logger)
|
||||
{
|
||||
Path = path;
|
||||
|
||||
_Logger = logger;
|
||||
if (RunningOnRaspberryPi())
|
||||
_PiOffset = -8;
|
||||
try
|
||||
{
|
||||
_Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
}
|
||||
{ _FileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); }
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_Logger.LogError(ex, "Current user doesn't have permissions to access input data. Add user to input group to correct this error");
|
||||
logger.LogError(ex, "Current user doesn't have permissions to access input data. Add user to input group to correct this error");
|
||||
Faulted = true;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
_Logger.LogWarning(ex, $"Error occurred while trying to build stream for {path}");
|
||||
logger.LogWarning(ex, $"Error occurred while trying to build stream for {path}");
|
||||
Faulted = true;
|
||||
}
|
||||
|
||||
_ = Task.Run(Run);
|
||||
}
|
||||
|
||||
@ -66,33 +49,27 @@ public class InputReader : IDisposable
|
||||
{
|
||||
if (_Disposing)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
if (!Faulted && _Stream is not null)
|
||||
{
|
||||
_ = _Stream.Read(_Buffer, 0, _BufferLength);
|
||||
}
|
||||
if (!Faulted && _FileStream is not null)
|
||||
_ = _FileStream.Read(_Buffer, 0, _BufferLength);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
_Logger.LogInformation(ex, $"Error occured while trying to read from the stream for {Path}");
|
||||
Faulted = true;
|
||||
}
|
||||
|
||||
EventType type = GetEventType();
|
||||
short code = GetCode();
|
||||
int value = GetValue();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case EventType.EV_SYN:
|
||||
_Ticks = DateTime.Now.Ticks;
|
||||
break;
|
||||
case EventType.EV_KEY:
|
||||
HandleKeyPressEvent(code, value);
|
||||
HandleKeyPressEvent();
|
||||
break;
|
||||
case EventType.EV_REL:
|
||||
MouseAxis axis = (MouseAxis)code;
|
||||
MouseMoveEvent e = new(axis, value);
|
||||
OnMouseMove?.Invoke(e);
|
||||
HandleMouseMoveEvent();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -107,9 +84,7 @@ public class InputReader : IDisposable
|
||||
_Buffer[22 + _PiOffset],
|
||||
_Buffer[23 + _PiOffset]
|
||||
};
|
||||
|
||||
int value = BitConverter.ToInt32(valueBits, 0);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -120,9 +95,7 @@ public class InputReader : IDisposable
|
||||
_Buffer[18 + _PiOffset],
|
||||
_Buffer[19 + _PiOffset]
|
||||
};
|
||||
|
||||
short code = BitConverter.ToInt16(codeBits, 0);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
@ -133,27 +106,35 @@ public class InputReader : IDisposable
|
||||
_Buffer[16 + _PiOffset],
|
||||
_Buffer[17 + _PiOffset]
|
||||
};
|
||||
|
||||
short type = BitConverter.ToInt16(typeBits, 0);
|
||||
|
||||
EventType eventType = (EventType)type;
|
||||
|
||||
return eventType;
|
||||
}
|
||||
|
||||
private void HandleKeyPressEvent(short code, int value)
|
||||
private void HandleKeyPressEvent()
|
||||
{
|
||||
EventCode c = (EventCode)code;
|
||||
KeyState s = (KeyState)value;
|
||||
KeyPressEvent e = new(c, s);
|
||||
OnKeyPress?.Invoke(e);
|
||||
short code = GetCode();
|
||||
int value = GetValue();
|
||||
KeyState keyState = (KeyState)value;
|
||||
EventCode eventCode = (EventCode)code;
|
||||
KeyPressEvent keyPressEvent = new(eventCode, keyState, new TimeSpan(DateTime.Now.Ticks - _Ticks));
|
||||
OnKeyPress?.Invoke(keyPressEvent);
|
||||
}
|
||||
|
||||
private void HandleMouseMoveEvent()
|
||||
{
|
||||
short code = GetCode();
|
||||
int value = GetValue();
|
||||
MouseAxis axis = (MouseAxis)code;
|
||||
MouseMoveEvent e = new(axis, value);
|
||||
OnMouseMove?.Invoke(e);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_Disposing = true;
|
||||
_Stream?.Dispose();
|
||||
_Stream = null;
|
||||
_FileStream?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private static bool RunningOnRaspberryPi()
|
||||
@ -163,4 +144,5 @@ public class InputReader : IDisposable
|
||||
bool runningOnPi = text.Any(l => l.Contains("Raspberry Pi"));
|
||||
return runningOnPi;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user