Skip to content

Commit 62501ad

Browse files
do minor cleanup with EvDevKeyInput, add in define for switching from X11KeyInput to EvDevKeyInput
1 parent d4602cf commit 62501ad

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

src/BizHawk.Bizware.Input/OSTailoredKeyInputAdapter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#nullable enable
22

3+
// #define USE_EVDEV
4+
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
@@ -27,7 +29,11 @@ public virtual void DeInitAll()
2729
switch (OSTailoredCode.CurrentOS)
2830
{
2931
case OSTailoredCode.DistinctOS.Linux:
32+
#if USE_EVDEV
33+
EvDevKeyInput.Deinitialize();
34+
#else
3035
X11KeyInput.Deinitialize();
36+
#endif
3137
break;
3238
case OSTailoredCode.DistinctOS.macOS:
3339
QuartzKeyInput.Deinitialize();
@@ -47,7 +53,11 @@ public virtual void FirstInitAll(IntPtr mainFormHandle)
4753
case OSTailoredCode.DistinctOS.Linux:
4854
// TODO: probably need a libinput option for Wayland
4955
// (unless we just want to ditch this and always use evdev here?)
56+
#if USE_EVDEV
57+
EvDevKeyInput.Deinitialize();
58+
#else
5059
X11KeyInput.Initialize();
60+
#endif
5161
break;
5262
case OSTailoredCode.DistinctOS.macOS:
5363
QuartzKeyInput.Initialize();
@@ -74,7 +84,11 @@ public virtual IEnumerable<KeyEvent> ProcessHostKeyboards()
7484
{
7585
var ret = OSTailoredCode.CurrentOS switch
7686
{
87+
#if USE_EVDEV
88+
OSTailoredCode.DistinctOS.Linux => EvDevKeyInput.Update(),
89+
#else
7790
OSTailoredCode.DistinctOS.Linux => X11KeyInput.Update(),
91+
#endif
7892
OSTailoredCode.DistinctOS.macOS => QuartzKeyInput.Update(),
7993
OSTailoredCode.DistinctOS.Windows => RAWKeyInput.Update(_config ?? throw new(nameof(ProcessHostKeyboards) + " called before the global config was passed")),
8094
_ => throw new InvalidOperationException()

src/BizHawk.Bizware.Input/evdev/EvDevKeyInput.cs

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ namespace BizHawk.Bizware.Input
1818
{
1919
internal static class EvDevKeyInput
2020
{
21-
private struct EvDevKeyboard
21+
private sealed record EvDevKeyboard(
22+
uint DriverVersion,
23+
ushort IdBus,
24+
ushort IdVendor,
25+
ushort IdProduct,
26+
ushort IdVersion,
27+
string Name,
28+
int Fd,
29+
string Path) : IDisposable
2230
{
23-
public uint DriverVersion;
24-
public ushort IdBus;
25-
public ushort IdVendor;
26-
public ushort IdProduct;
27-
public ushort IdVersion;
28-
public string Name;
29-
public int Fd;
30-
public string Path;
31+
public void Dispose()
32+
=> _ = close(Fd);
3133

3234
public override string ToString()
3335
{
@@ -44,7 +46,7 @@ public override string ToString()
4446

4547
private static readonly object _lockObj = new();
4648

47-
private static List<int> DecodeBits(Span<byte> bits)
49+
private static List<int> DecodeBits(ReadOnlySpan<byte> bits)
4850
{
4951
var result = new List<int>(bits.Length * 8);
5052
for (var i = 0; i < bits.Length; i++)
@@ -135,17 +137,16 @@ private static unsafe void MaybeAddKeyboard(string path)
135137
return;
136138
}
137139

138-
var keyboard = new EvDevKeyboard
139-
{
140-
DriverVersion = version,
141-
IdBus = id[0],
142-
IdProduct = id[1],
143-
IdVendor = id[2],
144-
IdVersion = id[3],
145-
Name = name,
146-
Fd = fd,
147-
Path = path,
148-
};
140+
var keyboard = new EvDevKeyboard(
141+
DriverVersion: version,
142+
IdBus: id[0],
143+
IdProduct: id[1],
144+
IdVendor: id[2],
145+
IdVersion: id[3],
146+
Name: name,
147+
Fd: fd,
148+
Path: path
149+
);
149150

150151
Console.WriteLine($"Added keyboard {keyboard}");
151152
_keyboards.Add(path, keyboard);
@@ -155,8 +156,8 @@ private static void MaybeRemoveKeyboard(string path)
155156
{
156157
if (_keyboards.TryGetValue(path, out var keyboard))
157158
{
158-
_ = close(keyboard.Fd);
159159
Console.WriteLine($"Removed keyboard {keyboard}");
160+
keyboard.Dispose();
160161
_keyboards.Remove(path);
161162
}
162163
}
@@ -181,7 +182,7 @@ private static void OnWatcherEvent(object _, FileSystemEventArgs e)
181182
MaybeRemoveKeyboard(e.FullPath);
182183
break;
183184
default:
184-
Console.WriteLine($"Unexpected watcher event {e.ChangeType}");
185+
Debug.WriteLine($"Unexpected watcher event {e.ChangeType}");
185186
break;
186187
}
187188
}
@@ -228,7 +229,7 @@ public static void Deinitialize()
228229

229230
foreach (var keyboard in _keyboards.Values)
230231
{
231-
_ = close(keyboard.Fd);
232+
keyboard.Dispose();
232233
}
233234

234235
_keyboards.Clear();
@@ -248,8 +249,7 @@ public static IEnumerable<KeyEvent> Update()
248249
var kbEvent = default(EvDevKeyboardEvent);
249250
var kbEventSize = (IntPtr)Marshal.SizeOf<EvDevKeyboardEvent>();
250251
var kbsToClose = new List<string>();
251-
Span<byte> keyChanges = stackalloc byte[(int)EvDevKeyCode.KEY_CNT];
252-
keyChanges.Clear();
252+
var keyEvents = new List<KeyEvent>();
253253

254254
foreach (var keyboard in _keyboards.Values)
255255
{
@@ -275,6 +275,7 @@ public static IEnumerable<KeyEvent> Update()
275275
// ENODEV means the device is gone
276276
if (errno == ENODEV)
277277
{
278+
// can't remove the kbs while iterating them!
278279
kbsToClose.Add(keyboard.Path);
279280
break;
280281
}
@@ -296,24 +297,24 @@ public static IEnumerable<KeyEvent> Update()
296297
continue;
297298
}
298299

299-
if (kbEvent.code > EvDevKeyCode.KEY_MAX)
300-
{
301-
Debug.WriteLine($"Unexpected event code {kbEvent.code}");
302-
continue;
303-
}
304-
305-
switch (kbEvent.value)
300+
if (KeyEnumMap.TryGetValue(kbEvent.code, out var key))
306301
{
307-
case EvDevKeyValue.KeyUp:
308-
keyChanges[(int)kbEvent.code] = 1;
309-
break;
310-
case EvDevKeyValue.KeyDown:
311-
case EvDevKeyValue.KeyRepeat:
312-
keyChanges[(int)kbEvent.code] = 2;
313-
break;
314-
default:
315-
Debug.WriteLine($"Unexpected event value {kbEvent.value}");
316-
break;
302+
switch (kbEvent.value)
303+
{
304+
case EvDevKeyValue.KeyUp:
305+
keyEvents.Add(new(key, pressed: false));
306+
break;
307+
case EvDevKeyValue.KeyDown:
308+
keyEvents.Add(new(key, pressed: true));
309+
break;
310+
case EvDevKeyValue.KeyRepeat:
311+
// should this be considered a press event?
312+
// probably not particularly useful to do so
313+
break;
314+
default:
315+
Debug.WriteLine($"Unexpected event value {kbEvent.value}");
316+
break;
317+
}
317318
}
318319
}
319320
}
@@ -323,16 +324,6 @@ public static IEnumerable<KeyEvent> Update()
323324
MaybeRemoveKeyboard(path);
324325
}
325326

326-
var keyEvents = new List<KeyEvent>();
327-
for (var i = 0; i < (int)EvDevKeyCode.KEY_CNT; i++)
328-
{
329-
if (keyChanges[i] != 0 &&
330-
KeyEnumMap.TryGetValue((EvDevKeyCode)i, out var key))
331-
{
332-
keyEvents.Add(new(key, keyChanges[i] == 1));
333-
}
334-
}
335-
336327
return keyEvents;
337328
}
338329
}

0 commit comments

Comments
 (0)