Releases: Linux amd64 + Windows amd64 Binaries mit Build-Tags für USB
- USB-Input via evdev wird nur auf Linux gebaut (build tag linux) - Windows bekommt USB-Stub (build tag !linux) - Binaries: sportscore-linux-amd64 (ELF 64-bit), sportscore-windows-amd64.exe (PE32+) - Alle Tests bestanden
This commit is contained in:
+3
-123
@@ -1,13 +1,7 @@
|
|||||||
// Package usb provides keyboard input handling via Linux evdev devices.
|
// Package usb provides keyboard input handling.
|
||||||
|
// Platform-independent types and interfaces.
|
||||||
package usb
|
package usb
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gvalkov/golang-evdev"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Key represents a keyboard key event.
|
// Key represents a keyboard key event.
|
||||||
type Key struct {
|
type Key struct {
|
||||||
Code uint16 // Linux input event code
|
Code uint16 // Linux input event code
|
||||||
@@ -15,118 +9,4 @@ type Key struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keyboard represents an input device (keyboard).
|
// Keyboard represents an input device (keyboard).
|
||||||
type Keyboard struct {
|
// The concrete type is defined per-platform via build tags.
|
||||||
device *evdev.InputDevice
|
|
||||||
}
|
|
||||||
|
|
||||||
// ScanForDevices finds all keyboard devices in /dev/input/.
|
|
||||||
func ScanForDevices() ([]*evdev.InputDevice, error) {
|
|
||||||
keyboards := make([]*evdev.InputDevice, 0)
|
|
||||||
devices, err := evdev.ListInputDevices("event*")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to list input devices: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dev := range devices {
|
|
||||||
// Use CapabilitiesFlat which maps int event type to []int codes
|
|
||||||
if dev.CapabilitiesFlat == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
keyCaps, ok := dev.CapabilitiesFlat[1] // EV_KEY = 1
|
|
||||||
if !ok || len(keyCaps) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter: only consider devices with standard keyboard keys
|
|
||||||
hasSlash := false
|
|
||||||
for _, code := range keyCaps {
|
|
||||||
if code == 1 { // KEY_SLASH
|
|
||||||
hasSlash = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if hasSlash {
|
|
||||||
keyboards = append(keyboards, dev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyboards, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// OpenKeyboard opens a keyboard device at the given path.
|
|
||||||
func OpenKeyboard(devicePath string) (*Keyboard, error) {
|
|
||||||
dev, err := evdev.Open(devicePath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to open %s: %w", devicePath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Keyboard{
|
|
||||||
device: dev,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// OpenFirstKeyboard opens the first available keyboard device.
|
|
||||||
func OpenFirstKeyboard() (*Keyboard, error) {
|
|
||||||
keyboards, err := ScanForDevices()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(keyboards) == 0 {
|
|
||||||
return nil, fmt.Errorf("no keyboard devices found in /dev/input/")
|
|
||||||
}
|
|
||||||
|
|
||||||
dev, err := evdev.Open(keyboards[0].Fn)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to open first keyboard %s: %w", keyboards[0].Fn, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "[usb] Using keyboard: %s (%s)\n", dev.Name, dev.Fn)
|
|
||||||
|
|
||||||
return &Keyboard{
|
|
||||||
device: dev,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close releases the keyboard device.
|
|
||||||
func (kb *Keyboard) Close() error {
|
|
||||||
if kb.device != nil {
|
|
||||||
return kb.device.File.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadKey reads the next key event from the keyboard.
|
|
||||||
// Returns a Key struct or an error.
|
|
||||||
func (kb *Keyboard) ReadKey() (*Key, error) {
|
|
||||||
event, err := kb.device.ReadOne()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read event: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.Type != 1 { // EV_KEY
|
|
||||||
// Not a key event, try to read again
|
|
||||||
return kb.ReadKey()
|
|
||||||
}
|
|
||||||
|
|
||||||
key := &Key{
|
|
||||||
Code: event.Code,
|
|
||||||
Type: "press",
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.Value == 0 {
|
|
||||||
key.Type = "release"
|
|
||||||
}
|
|
||||||
|
|
||||||
return key, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDeviceInfo returns information about the connected keyboard.
|
|
||||||
func (kb *Keyboard) GetDeviceInfo() string {
|
|
||||||
if kb.device == nil {
|
|
||||||
return "no device"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s (%s)", kb.device.Name, kb.device.Fn)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package usb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/gvalkov/golang-evdev"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Keyboard implements input device for Linux evdev.
|
||||||
|
type Keyboard struct {
|
||||||
|
device *evdev.InputDevice
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenFirstKeyboard opens the first available keyboard device.
|
||||||
|
func OpenFirstKeyboard() (*Keyboard, error) {
|
||||||
|
keyboards, err := ScanForDevices()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(keyboards) == 0 {
|
||||||
|
return nil, fmt.Errorf("no keyboard devices found")
|
||||||
|
}
|
||||||
|
|
||||||
|
dev, err := evdev.Open(keyboards[0].Fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open first keyboard %s: %w", keyboards[0].Fn, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "[usb] Using keyboard: %s (%s)\n", dev.Name, dev.Fn)
|
||||||
|
|
||||||
|
return &Keyboard{
|
||||||
|
device: dev,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close releases the keyboard device.
|
||||||
|
func (kb *Keyboard) Close() error {
|
||||||
|
if kb.device != nil {
|
||||||
|
return kb.device.File.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadKey reads the next key event from the keyboard.
|
||||||
|
func (kb *Keyboard) ReadKey() (*Key, error) {
|
||||||
|
event, err := kb.device.ReadOne()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read event: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Type != 1 { // EV_KEY
|
||||||
|
return kb.ReadKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
key := &Key{
|
||||||
|
Code: event.Code,
|
||||||
|
Type: "press",
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Value == 0 {
|
||||||
|
key.Type = "release"
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDeviceInfo returns information about the connected keyboard.
|
||||||
|
func (kb *Keyboard) GetDeviceInfo() string {
|
||||||
|
if kb.device == nil {
|
||||||
|
return "no device"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s (%s)", kb.device.Name, kb.device.Fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScanForDevices finds all keyboard devices in /dev/input/.
|
||||||
|
func ScanForDevices() ([]*evdev.InputDevice, error) {
|
||||||
|
keyboards := make([]*evdev.InputDevice, 0)
|
||||||
|
devices, err := evdev.ListInputDevices("event*")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list input devices: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dev := range devices {
|
||||||
|
if dev.CapabilitiesFlat == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
keyCaps, ok := dev.CapabilitiesFlat[1] // EV_KEY = 1
|
||||||
|
if !ok || len(keyCaps) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
hasSlash := false
|
||||||
|
for _, code := range keyCaps {
|
||||||
|
if code == 1 { // KEY_SLASH
|
||||||
|
hasSlash = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasSlash {
|
||||||
|
keyboards = append(keyboards, dev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyboards, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package usb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Keyboard represents an input device (keyboard).
|
||||||
|
// Non-Linux stub.
|
||||||
|
type Keyboard struct{}
|
||||||
|
|
||||||
|
// OpenFirstKeyboard opens the first available keyboard device.
|
||||||
|
// On non-Linux platforms, it always returns an error.
|
||||||
|
func OpenFirstKeyboard() (*Keyboard, error) {
|
||||||
|
return nil, fmt.Errorf("usb keyboard input is only supported on Linux")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close releases the keyboard device.
|
||||||
|
func (kb *Keyboard) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadKey reads the next key event from the keyboard.
|
||||||
|
func (kb *Keyboard) ReadKey() (*Key, error) {
|
||||||
|
return nil, fmt.Errorf("usb keyboard input is only supported on Linux")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDeviceInfo returns information about the connected keyboard.
|
||||||
|
func (kb *Keyboard) GetDeviceInfo() string {
|
||||||
|
return "linux-only"
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user