refactor(hid): improve keyboard layout compatibility in HID handling functions

Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
luckfox-eng29
2026-04-29 20:03:13 +08:00
parent a1da483b27
commit 6292537c23
33 changed files with 2226 additions and 96 deletions

47
internal/hidrpc/hidrpc.go Normal file
View File

@@ -0,0 +1,47 @@
package hidrpc
import "fmt"
type Handler interface {
HandleKeyboardReport(modifier byte, keys []byte) error
HandleKeypressReport(key byte, press bool) error
HandleKeypressKeepAlive() error
HandleKeyboardMacroReport(data []byte) error
HandleCancelKeyboardMacro() error
}
type Server struct {
handler Handler
}
func NewServer(handler Handler) *Server {
return &Server{handler: handler}
}
func (s *Server) HandleMessage(data []byte) error {
msg, err := UnmarshalMessage(data)
if err != nil {
return err
}
switch msg.Type {
case MessageTypeKeyboardReport:
if len(msg.Data) < 7 {
return fmt.Errorf("invalid keyboard report length: %d", len(msg.Data))
}
return s.handler.HandleKeyboardReport(msg.Data[0], msg.Data[1:7])
case MessageTypeKeypressReport:
if len(msg.Data) < 2 {
return fmt.Errorf("invalid keypress report length: %d", len(msg.Data))
}
return s.handler.HandleKeypressReport(msg.Data[0], msg.Data[1] != 0)
case MessageTypeKeypressKeepAlive:
return s.handler.HandleKeypressKeepAlive()
case MessageTypeKeyboardMacroReport:
return s.handler.HandleKeyboardMacroReport(msg.Data)
case MessageTypeCancelKeyboardMacro:
return s.handler.HandleCancelKeyboardMacro()
default:
return fmt.Errorf("unknown message type: 0x%02x", msg.Type)
}
}