Skip to content

Commit e12ddaf

Browse files
committed
Use a single exported HidKeyBufferSize from hid_keyboard
1 parent 8de61db commit e12ddaf

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

internal/hidrpc/message.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/google/uuid"
8+
"github.com/jetkvm/kvm/internal/usbgadget"
89
)
910

1011
// Message ..
@@ -135,18 +136,16 @@ func (m *Message) KeyboardReport() (KeyboardReport, error) {
135136
// Macro ..
136137
type KeyboardMacroStep struct {
137138
Modifier byte // 1 byte
138-
Keys []byte // 6 bytes: HidKeyBufferSize
139+
Keys []byte // 6 bytes: usbgadget.HidKeyBufferSize
139140
Delay uint16 // 2 bytes
140141
}
142+
141143
type KeyboardMacroReport struct {
142144
IsPaste bool
143145
StepCount uint32
144146
Steps []KeyboardMacroStep
145147
}
146148

147-
// HidKeyBufferSize is the size of the keys buffer in the keyboard report.
148-
const HidKeyBufferSize int = 6
149-
150149
// KeyboardMacroReport returns the keyboard macro report from the message.
151150
func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
152151
if m.t != TypeKeyboardMacroReport {
@@ -171,7 +170,7 @@ func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
171170
Delay: binary.BigEndian.Uint16(m.d[offset+7 : offset+9]),
172171
})
173172

174-
offset += 1 + HidKeyBufferSize + 2
173+
offset += 1 + usbgadget.HidKeyBufferSize + 2
175174
}
176175

177176
return KeyboardMacroReport{

internal/usbgadget/hid_keyboard.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var keyboardReportDesc = []byte{
7676

7777
const (
7878
hidReadBufferSize = 8
79-
hidKeyBufferSize = 6
79+
HidKeyBufferSize = 6
8080
hidErrorRollOver = 0x01
8181
// https://www.usb.org/sites/default/files/documents/hid1_11.pdf
8282
// https://www.usb.org/sites/default/files/hut1_2.pdf
@@ -342,7 +342,7 @@ func (u *UsbGadget) keyboardWriteHidFile(modifier byte, keys []byte) error {
342342
return err
343343
}
344344

345-
_, err := u.writeWithTimeout(u.keyboardHidFile, append([]byte{modifier, 0x00}, keys[:hidKeyBufferSize]...))
345+
_, err := u.writeWithTimeout(u.keyboardHidFile, append([]byte{modifier, 0x00}, keys[:HidKeyBufferSize]...))
346346
if err != nil {
347347
u.logWithSuppression("keyboardWriteHidFile", 100, u.log, err, "failed to write to hidg0")
348348
u.keyboardHidFile.Close()
@@ -386,11 +386,11 @@ func (u *UsbGadget) UpdateKeysDown(modifier byte, keys []byte) KeysDownState {
386386
func (u *UsbGadget) KeyboardReport(modifier byte, keys []byte) error {
387387
defer u.resetUserInputTime()
388388

389-
if len(keys) > hidKeyBufferSize {
390-
keys = keys[:hidKeyBufferSize]
389+
if len(keys) > HidKeyBufferSize {
390+
keys = keys[:HidKeyBufferSize]
391391
}
392-
if len(keys) < hidKeyBufferSize {
393-
keys = append(keys, make([]byte, hidKeyBufferSize-len(keys))...)
392+
if len(keys) < HidKeyBufferSize {
393+
keys = append(keys, make([]byte, HidKeyBufferSize-len(keys))...)
394394
}
395395

396396
err := u.keyboardWriteHidFile(modifier, keys)
@@ -473,7 +473,7 @@ func (u *UsbGadget) keypressReport(key byte, press bool) (KeysDownState, error)
473473
// handle other keys that are not modifier keys by placing or removing them
474474
// from the key buffer since the buffer tracks currently pressed keys
475475
overrun := true
476-
for i := range hidKeyBufferSize {
476+
for i := range HidKeyBufferSize {
477477
// If we find the key in the buffer the buffer, we either remove it (if press is false)
478478
// or do nothing (if down is true) because the buffer tracks currently pressed keys
479479
// 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)
484484
// we are releasing the key, remove it from the buffer
485485
if keys[i] != 0 {
486486
copy(keys[i:], keys[i+1:])
487-
keys[hidKeyBufferSize-1] = 0 // Clear the last byte
487+
keys[HidKeyBufferSize-1] = 0 // Clear the last byte
488488
}
489489
}
490490
overrun = false // We found a slot for the key

internal/usbgadget/usbgadget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func newUsbGadget(name string, configMap map[string]gadgetConfigItem, enabledDev
135135
keyboardStateCtx: keyboardCtx,
136136
keyboardStateCancel: keyboardCancel,
137137
keyboardState: 0,
138-
keysDownState: KeysDownState{Modifier: 0, Keys: []byte{0, 0, 0, 0, 0, 0}}, // must be initialized to hidKeyBufferSize (6) zero bytes
138+
keysDownState: KeysDownState{Modifier: 0, Keys: []byte{0, 0, 0, 0, 0, 0}}, // must be initialized to usbgadget.HidKeyBufferSize (6) zero bytes
139139
kbdAutoReleaseTimers: make(map[byte]*time.Timer),
140140
enabledDevices: *enabledDevices,
141141
lastUserInput: time.Now(),

jsonrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ func executeKeyboardMacro(ctx context.Context, isPaste bool, macro []hidrpc.Keyb
12101210
case <-ctx.Done():
12111211
// make sure keyboard state is reset and the client gets notified
12121212
gadget.ResumeSuspendKeyDownMessages()
1213-
err := rpcKeyboardReport(0, make([]byte, hidrpc.HidKeyBufferSize))
1213+
err := rpcKeyboardReport(0, make([]byte, usbgadget.HidKeyBufferSize))
12141214
if err != nil {
12151215
logger.Warn().Err(err).Msg("failed to reset keyboard state")
12161216
}

0 commit comments

Comments
 (0)