Add config-driven action system (launch, url) with factory pattern

This commit is contained in:
2026-08-25 00:48:09 +02:00
parent 909d9aa13c
commit 56bf3ff5bf
8 changed files with 86 additions and 26 deletions
+23 -26
View File
@@ -1,46 +1,39 @@
using HidSharp;
using System.Text.Json;
using HidSharp;
var candidates = DeviceList.Local.GetHidDevices(vendorID: 0x2341, productID: 0x8036).ToList();
var configPath = Path.Combine(AppContext.BaseDirectory, "config.json");
var configJson = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<Config>(configJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
?? new Config();
Console.WriteLine($"{candidates.Count} interface(s) trouvée(s) pour ce VID/PID :\n");
foreach (var d in candidates)
var actions = new Dictionary<int, IAction>();
foreach (var (key, binding) in config.Bindings)
{
Console.WriteLine($" {d.DevicePath}");
var action = ActionFactory.Create(binding);
if (action is not null && int.TryParse(key, out var bit))
actions[bit] = action;
}
Console.WriteLine();
var device = candidates.FirstOrDefault(d => !d.DevicePath.Contains("kbd", StringComparison.OrdinalIgnoreCase));
var device = DeviceList.Local.GetHidDevices(vendorID: 0x2341, productID: 0x8036).FirstOrDefault();
if (device is null)
{
Console.WriteLine("Interface Raw HID non trouvée (seule l'interface clavier standard a été détectée).");
Console.WriteLine("Pad non trouvé. Vérifie qu'il est branché.");
return;
}
Console.WriteLine($"Ouverture de : {device.DevicePath}\n");
using var stream = device.Open();
stream.ReadTimeout = Timeout.Infinite;
Console.WriteLine($"MaxInputReportLength: {device.GetMaxInputReportLength()}");
Console.WriteLine($"MaxOutputReportLength: {device.GetMaxOutputReportLength()}");
var buffer = new byte[device.GetMaxInputReportLength()];
ushort lastMask = 0;
Console.WriteLine("En écoute... appuie sur chaque bouton un par un (Ctrl+C pour quitter)\n");
Console.WriteLine("En écoute...\n");
while (true)
{
int count;
try
{
count = stream.Read(buffer, 0, buffer.Length);
}
catch (TimeoutException)
{
continue;
}
try { count = stream.Read(buffer, 0, buffer.Length); }
catch (TimeoutException) { continue; }
int offset = count > 2 ? 1 : 0;
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
@@ -50,10 +43,14 @@ while (true)
ushort changed = (ushort)(mask ^ lastMask);
for (int bit = 0; bit < 12; bit++)
{
if ((changed & (1 << bit)) != 0)
bool bitChanged = (changed & (1 << bit)) != 0;
bool nowPressed = (mask & (1 << bit)) != 0;
// On déclenche seulement au moment où le bouton est pressé, pas relâché
if (bitChanged && nowPressed && actions.TryGetValue(bit, out var action))
{
bool pressed = (mask & (1 << bit)) != 0;
Console.WriteLine($"Bit {bit}: {(pressed ? "PRESSÉ" : "relâché")}");
Console.WriteLine($"Bouton {bit} → exécution de l'action");
action.Execute();
}
}
lastMask = mask;