Use a single exported HidKeyBufferSize from hid_keyboard

This commit is contained in:
Marc Brooks 2025-11-05 14:56:04 -06:00
parent 8de61db3d8
commit e12ddaff79
No known key found for this signature in database
GPG Key ID: 583A6AF2D6AE1DC6
4 changed files with 14 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jetkvm/kvm/internal/usbgadget"
) )
// Message .. // Message ..
@ -135,18 +136,16 @@ func (m *Message) KeyboardReport() (KeyboardReport, error) {
// Macro .. // Macro ..
type KeyboardMacroStep struct { type KeyboardMacroStep struct {
Modifier byte // 1 byte Modifier byte // 1 byte
Keys []byte // 6 bytes: HidKeyBufferSize Keys []byte // 6 bytes: usbgadget.HidKeyBufferSize
Delay uint16 // 2 bytes Delay uint16 // 2 bytes
} }
type KeyboardMacroReport struct { type KeyboardMacroReport struct {
IsPaste bool IsPaste bool
StepCount uint32 StepCount uint32
Steps []KeyboardMacroStep Steps []KeyboardMacroStep
} }
// HidKeyBufferSize is the size of the keys buffer in the keyboard report.
const HidKeyBufferSize int = 6
// KeyboardMacroReport returns the keyboard macro report from the message. // KeyboardMacroReport returns the keyboard macro report from the message.
func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) { func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
if m.t != TypeKeyboardMacroReport { if m.t != TypeKeyboardMacroReport {
@ -171,7 +170,7 @@ func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
Delay: binary.BigEndian.Uint16(m.d[offset+7 : offset+9]), Delay: binary.BigEndian.Uint16(m.d[offset+7 : offset+9]),
}) })
offset += 1 + HidKeyBufferSize + 2 offset += 1 + usbgadget.HidKeyBufferSize + 2
} }
return KeyboardMacroReport{ return KeyboardMacroReport{

View File

@ -76,7 +76,7 @@ var keyboardReportDesc = []byte{
const ( const (
hidReadBufferSize = 8 hidReadBufferSize = 8
hidKeyBufferSize = 6 HidKeyBufferSize = 6
hidErrorRollOver = 0x01 hidErrorRollOver = 0x01
// https://www.usb.org/sites/default/files/documents/hid1_11.pdf // https://www.usb.org/sites/default/files/documents/hid1_11.pdf
// https://www.usb.org/sites/default/files/hut1_2.pdf // https://www.usb.org/sites/default/files/hut1_2.pdf
@ -342,7 +342,7 @@ func (u *UsbGadget) keyboardWriteHidFile(modifier byte, keys []byte) error {
return err return err
} }
_, err := u.writeWithTimeout(u.keyboardHidFile, append([]byte{modifier, 0x00}, keys[:hidKeyBufferSize]...)) _, err := u.writeWithTimeout(u.keyboardHidFile, append([]byte{modifier, 0x00}, keys[:HidKeyBufferSize]...))
if err != nil { if err != nil {
u.logWithSuppression("keyboardWriteHidFile", 100, u.log, err, "failed to write to hidg0") u.logWithSuppression("keyboardWriteHidFile", 100, u.log, err, "failed to write to hidg0")
u.keyboardHidFile.Close() u.keyboardHidFile.Close()
@ -386,11 +386,11 @@ func (u *UsbGadget) UpdateKeysDown(modifier byte, keys []byte) KeysDownState {
func (u *UsbGadget) KeyboardReport(modifier byte, keys []byte) error { func (u *UsbGadget) KeyboardReport(modifier byte, keys []byte) error {
defer u.resetUserInputTime() defer u.resetUserInputTime()
if len(keys) > hidKeyBufferSize { if len(keys) > HidKeyBufferSize {
keys = keys[:hidKeyBufferSize] keys = keys[:HidKeyBufferSize]
} }
if len(keys) < hidKeyBufferSize { if len(keys) < HidKeyBufferSize {
keys = append(keys, make([]byte, hidKeyBufferSize-len(keys))...) keys = append(keys, make([]byte, HidKeyBufferSize-len(keys))...)
} }
err := u.keyboardWriteHidFile(modifier, keys) err := u.keyboardWriteHidFile(modifier, keys)
@ -473,7 +473,7 @@ func (u *UsbGadget) keypressReport(key byte, press bool) (KeysDownState, error)
// handle other keys that are not modifier keys by placing or removing them // handle other keys that are not modifier keys by placing or removing them
// from the key buffer since the buffer tracks currently pressed keys // from the key buffer since the buffer tracks currently pressed keys
overrun := true overrun := true
for i := range hidKeyBufferSize { for i := range HidKeyBufferSize {
// If we find the key in the buffer the buffer, we either remove it (if press is false) // If we find the key in the buffer the buffer, we either remove it (if press is false)
// or do nothing (if down is true) because the buffer tracks currently pressed keys // or do nothing (if down is true) because the buffer tracks currently pressed keys
// and if we find a zero byte, we can place the key there (if press is true) // and if we find a zero byte, we can place the key there (if press is true)
@ -484,7 +484,7 @@ func (u *UsbGadget) keypressReport(key byte, press bool) (KeysDownState, error)
// we are releasing the key, remove it from the buffer // we are releasing the key, remove it from the buffer
if keys[i] != 0 { if keys[i] != 0 {
copy(keys[i:], keys[i+1:]) copy(keys[i:], keys[i+1:])
keys[hidKeyBufferSize-1] = 0 // Clear the last byte keys[HidKeyBufferSize-1] = 0 // Clear the last byte
} }
} }
overrun = false // We found a slot for the key overrun = false // We found a slot for the key

View File

@ -135,7 +135,7 @@ func newUsbGadget(name string, configMap map[string]gadgetConfigItem, enabledDev
keyboardStateCtx: keyboardCtx, keyboardStateCtx: keyboardCtx,
keyboardStateCancel: keyboardCancel, keyboardStateCancel: keyboardCancel,
keyboardState: 0, keyboardState: 0,
keysDownState: KeysDownState{Modifier: 0, Keys: []byte{0, 0, 0, 0, 0, 0}}, // must be initialized to hidKeyBufferSize (6) zero bytes keysDownState: KeysDownState{Modifier: 0, Keys: []byte{0, 0, 0, 0, 0, 0}}, // must be initialized to usbgadget.HidKeyBufferSize (6) zero bytes
kbdAutoReleaseTimers: make(map[byte]*time.Timer), kbdAutoReleaseTimers: make(map[byte]*time.Timer),
enabledDevices: *enabledDevices, enabledDevices: *enabledDevices,
lastUserInput: time.Now(), lastUserInput: time.Now(),

View File

@ -1210,7 +1210,7 @@ func executeKeyboardMacro(ctx context.Context, isPaste bool, macro []hidrpc.Keyb
case <-ctx.Done(): case <-ctx.Done():
// make sure keyboard state is reset and the client gets notified // make sure keyboard state is reset and the client gets notified
gadget.ResumeSuspendKeyDownMessages() gadget.ResumeSuspendKeyDownMessages()
err := rpcKeyboardReport(0, make([]byte, hidrpc.HidKeyBufferSize)) err := rpcKeyboardReport(0, make([]byte, usbgadget.HidKeyBufferSize))
if err != nil { if err != nil {
logger.Warn().Err(err).Msg("failed to reset keyboard state") logger.Warn().Err(err).Msg("failed to reset keyboard state")
} }