mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-05-28 17:11:20 +02:00
refactor(hid): improve keyboard layout compatibility in HID handling functions
Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
47
internal/hidrpc/hidrpc.go
Normal file
47
internal/hidrpc/hidrpc.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user