mirror of https://github.com/jetkvm/kvm.git
minor issues
This commit is contained in:
parent
ade9a4961a
commit
b5978016af
|
@ -37,7 +37,7 @@ func handleHidRPCMessage(message hidrpc.Message, session *Session) {
|
|||
logger.Warn().Err(err).Msg("failed to get keyboard macro report")
|
||||
return
|
||||
}
|
||||
_, rpcErr = rpcExecuteKeyboardMacro(keyboardMacroReport.Macro)
|
||||
_, rpcErr = rpcExecuteKeyboardMacro(keyboardMacroReport.Steps)
|
||||
case hidrpc.TypeCancelKeyboardMacroReport:
|
||||
rpcCancelKeyboardMacro()
|
||||
return
|
||||
|
|
|
@ -91,17 +91,19 @@ func (m *Message) KeyboardReport() (KeyboardReport, error) {
|
|||
}
|
||||
|
||||
// Macro ..
|
||||
type KeyboardMacro struct {
|
||||
type KeyboardMacroStep struct {
|
||||
Modifier byte // 1 byte
|
||||
Keys []byte // 6 bytes, to make things easier, the keys length is fixed to 6
|
||||
Keys []byte // 6 bytes: hidKeyBufferSize
|
||||
Delay uint16 // 2 bytes
|
||||
}
|
||||
type KeyboardMacroReport struct {
|
||||
IsPaste bool
|
||||
Length uint32
|
||||
Macro []KeyboardMacro
|
||||
IsPaste bool
|
||||
StepCount uint32
|
||||
Steps []KeyboardMacroStep
|
||||
}
|
||||
|
||||
const hidKeyBufferSize = 6
|
||||
|
||||
// KeyboardMacroReport returns the keyboard macro report from the message.
|
||||
func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
|
||||
if m.t != TypeKeyboardMacroReport {
|
||||
|
@ -109,29 +111,30 @@ func (m *Message) KeyboardMacroReport() (KeyboardMacroReport, error) {
|
|||
}
|
||||
|
||||
isPaste := m.d[0] == uint8(1)
|
||||
length := binary.BigEndian.Uint32(m.d[1:5])
|
||||
stepCount := binary.BigEndian.Uint32(m.d[1:5])
|
||||
|
||||
// check total length
|
||||
expectedLength := int(length)*9 + 5
|
||||
expectedLength := int(stepCount)*9 + 5
|
||||
if len(m.d) != expectedLength {
|
||||
return KeyboardMacroReport{}, fmt.Errorf("invalid length: %d, expected: %d", len(m.d), expectedLength)
|
||||
}
|
||||
|
||||
macro := make([]KeyboardMacro, 0, int(length))
|
||||
for i := 0; i < int(length); i++ {
|
||||
offset := 5 + i*9
|
||||
|
||||
macro = append(macro, KeyboardMacro{
|
||||
steps := make([]KeyboardMacroStep, 0, int(stepCount))
|
||||
offset := 5
|
||||
for i := 0; i < int(stepCount); i++ {
|
||||
steps = append(steps, KeyboardMacroStep{
|
||||
Modifier: m.d[offset],
|
||||
Keys: m.d[offset+1 : offset+7],
|
||||
Delay: binary.BigEndian.Uint16(m.d[offset+7 : offset+9]),
|
||||
})
|
||||
|
||||
offset += 1 + hidKeyBufferSize + 2
|
||||
}
|
||||
|
||||
return KeyboardMacroReport{
|
||||
IsPaste: isPaste,
|
||||
Macro: macro,
|
||||
Length: length,
|
||||
IsPaste: isPaste,
|
||||
Steps: steps,
|
||||
StepCount: stepCount,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1076,7 +1076,7 @@ func setKeyboardMacroCancel(cancel context.CancelFunc) {
|
|||
keyboardMacroCancel = cancel
|
||||
}
|
||||
|
||||
func rpcExecuteKeyboardMacro(macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) {
|
||||
func rpcExecuteKeyboardMacro(macro []hidrpc.KeyboardMacroStep) (usbgadget.KeysDownState, error) {
|
||||
cancelKeyboardMacro()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -1109,11 +1109,11 @@ func rpcCancelKeyboardMacro() {
|
|||
|
||||
var keyboardClearStateKeys = make([]byte, 6)
|
||||
|
||||
func isClearKeyStep(step hidrpc.KeyboardMacro) bool {
|
||||
func isClearKeyStep(step hidrpc.KeyboardMacroStep) bool {
|
||||
return step.Modifier == 0 && bytes.Equal(step.Keys, keyboardClearStateKeys)
|
||||
}
|
||||
|
||||
func rpcDoExecuteKeyboardMacro(ctx context.Context, macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) {
|
||||
func rpcDoExecuteKeyboardMacro(ctx context.Context, macro []hidrpc.KeyboardMacroStep) (usbgadget.KeysDownState, error) {
|
||||
var last usbgadget.KeysDownState
|
||||
var err error
|
||||
|
||||
|
|
|
@ -219,32 +219,32 @@ export interface KeyboardMacroStep extends KeysDownState {
|
|||
|
||||
export class KeyboardMacroReportMessage extends RpcMessage {
|
||||
isPaste: boolean;
|
||||
length: number;
|
||||
stepCount: number;
|
||||
steps: KeyboardMacroStep[];
|
||||
|
||||
KEYS_LENGTH = 6;
|
||||
|
||||
constructor(isPaste: boolean, length: number, steps: KeyboardMacroStep[]) {
|
||||
constructor(isPaste: boolean, stepCount: number, steps: KeyboardMacroStep[]) {
|
||||
super(HID_RPC_MESSAGE_TYPES.KeyboardMacroReport);
|
||||
this.isPaste = isPaste;
|
||||
this.length = length;
|
||||
this.stepCount = stepCount;
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
marshal(): Uint8Array {
|
||||
// validate if length is correct
|
||||
if (this.length !== this.steps.length) {
|
||||
throw new Error(`Length ${this.length} is not equal to the number of steps ${this.steps.length}`);
|
||||
if (this.stepCount !== this.steps.length) {
|
||||
throw new Error(`Length ${this.stepCount} is not equal to the number of steps ${this.steps.length}`);
|
||||
}
|
||||
|
||||
const data = new Uint8Array(this.length * 9 + 6);
|
||||
const data = new Uint8Array(this.stepCount * 9 + 6);
|
||||
data.set(new Uint8Array([
|
||||
this.messageType,
|
||||
this.isPaste ? 1 : 0,
|
||||
...fromUint32toUint8(this.length),
|
||||
...fromUint32toUint8(this.stepCount),
|
||||
]), 0);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
for (let i = 0; i < this.stepCount; i++) {
|
||||
const step = this.steps[i];
|
||||
if (!withinUint8Range(step.modifier)) {
|
||||
throw new Error(`Modifier ${step.modifier} is not within the uint8 range`);
|
||||
|
@ -279,7 +279,7 @@ export class KeyboardMacroReportMessage extends RpcMessage {
|
|||
}
|
||||
}
|
||||
|
||||
export class KeyboardMacroStateReportMessage extends RpcMessage {
|
||||
export class KeyboardMacroStateMessage extends RpcMessage {
|
||||
state: boolean;
|
||||
isPaste: boolean;
|
||||
|
||||
|
@ -297,12 +297,12 @@ export class KeyboardMacroStateReportMessage extends RpcMessage {
|
|||
]);
|
||||
}
|
||||
|
||||
public static unmarshal(data: Uint8Array): KeyboardMacroStateReportMessage | undefined {
|
||||
public static unmarshal(data: Uint8Array): KeyboardMacroStateMessage | undefined {
|
||||
if (data.length < 1) {
|
||||
throw new Error(`Invalid keyboard macro state report message length: ${data.length}`);
|
||||
}
|
||||
|
||||
return new KeyboardMacroStateReportMessage(data[0] === 1, data[1] === 1);
|
||||
return new KeyboardMacroStateMessage(data[0] === 1, data[1] === 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -417,7 +417,7 @@ export const messageRegistry = {
|
|||
[HID_RPC_MESSAGE_TYPES.KeypressReport]: KeypressReportMessage,
|
||||
[HID_RPC_MESSAGE_TYPES.KeyboardMacroReport]: KeyboardMacroReportMessage,
|
||||
[HID_RPC_MESSAGE_TYPES.CancelKeyboardMacroReport]: CancelKeyboardMacroReportMessage,
|
||||
[HID_RPC_MESSAGE_TYPES.KeyboardMacroStateReport]: KeyboardMacroStateReportMessage,
|
||||
[HID_RPC_MESSAGE_TYPES.KeyboardMacroStateReport]: KeyboardMacroStateMessage,
|
||||
}
|
||||
|
||||
export const unmarshalHidRpcMessage = (data: Uint8Array): RpcMessage | undefined => {
|
||||
|
|
|
@ -76,9 +76,8 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
|
|||
);
|
||||
|
||||
const reportKeyboardMacroEvent = useCallback(
|
||||
(macro: KeyboardMacroStep[]) => {
|
||||
const d = new KeyboardMacroReportMessage(false, macro.length, macro);
|
||||
sendMessage(d);
|
||||
(steps: KeyboardMacroStep[]) => {
|
||||
sendMessage(new KeyboardMacroReportMessage(false, steps.length, steps));
|
||||
},
|
||||
[sendMessage],
|
||||
);
|
||||
|
|
|
@ -11,7 +11,7 @@ import { JsonRpcResponse, useJsonRpc } from "@/hooks/useJsonRpc";
|
|||
import { useHidRpc } from "@/hooks/useHidRpc";
|
||||
import {
|
||||
KeyboardLedStateMessage,
|
||||
KeyboardMacroStateReportMessage,
|
||||
KeyboardMacroStateMessage,
|
||||
KeyboardMacroStep,
|
||||
KeysDownStateMessage,
|
||||
} from "@/hooks/hidRpc";
|
||||
|
@ -70,9 +70,9 @@ export default function useKeyboard() {
|
|||
case KeyboardLedStateMessage:
|
||||
setKeyboardLedState((message as KeyboardLedStateMessage).keyboardLedState);
|
||||
break;
|
||||
case KeyboardMacroStateReportMessage:
|
||||
if (!(message as KeyboardMacroStateReportMessage).isPaste) break;
|
||||
setPasteModeEnabled((message as KeyboardMacroStateReportMessage).state);
|
||||
case KeyboardMacroStateMessage:
|
||||
if (!(message as KeyboardMacroStateMessage).isPaste) break;
|
||||
setPasteModeEnabled((message as KeyboardMacroStateMessage).state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue