69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
namespace Barcode.Host.Shared.KeyboardMouse;
|
|
|
|
public static class DeviceReader
|
|
{
|
|
|
|
public static IEnumerable<LinuxDevice> Get(string path)
|
|
{
|
|
LinuxDevice linuxDevice = new();
|
|
List<LinuxDevice> linuxDevices = new();
|
|
using FileStream filestream = new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
using StreamReader streamReader = new(filestream);
|
|
while (!streamReader.EndOfStream)
|
|
{
|
|
string? line = streamReader.ReadLine();
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(linuxDevice.Name))
|
|
{
|
|
linuxDevices.Add(linuxDevice);
|
|
linuxDevice = new LinuxDevice();
|
|
}
|
|
continue;
|
|
}
|
|
if (line.StartsWith("I"))
|
|
ApplyIdentifier(line, linuxDevice);
|
|
else if (line.StartsWith("N"))
|
|
linuxDevice.Name = line.Substring(9, line.Length - 9 - 1);
|
|
else if (line.StartsWith("P"))
|
|
linuxDevice.PhysicalPath = line[8..];
|
|
else if (line.StartsWith("S"))
|
|
linuxDevice.SysFsPath = line[9..];
|
|
else if (line.StartsWith("U"))
|
|
linuxDevice.UniqueIdentificationCode = line[8..];
|
|
else if (line.StartsWith("H"))
|
|
linuxDevice.Handlers = line[12..]
|
|
.Split(" ")
|
|
.Where(h => !string.IsNullOrWhiteSpace(h))
|
|
.ToList();
|
|
else if (line.StartsWith("B"))
|
|
linuxDevice.Bitmaps.Add(line[3..]);
|
|
}
|
|
return linuxDevices;
|
|
}
|
|
|
|
private static void ApplyIdentifier(string line, LinuxDevice linuxDevice)
|
|
{
|
|
string[] values = line[3..].Split(" ");
|
|
foreach (string v in values)
|
|
{
|
|
string[] kvp = v.Split("=");
|
|
switch (kvp[0])
|
|
{
|
|
case "Bus":
|
|
linuxDevice.Identifier.Bus = kvp[1];
|
|
break;
|
|
case "Vendor":
|
|
linuxDevice.Identifier.Vendor = kvp[1];
|
|
break;
|
|
case "Product":
|
|
linuxDevice.Identifier.Product = kvp[1];
|
|
break;
|
|
case "Version":
|
|
linuxDevice.Identifier.Version = kvp[1];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |