Compare commits

..

No commits in common. "1e2cee7060dcca6a72335490233bc17185304f63" and "024cbb8fb19a46d8539f05dc463372402fa3fae5" have entirely different histories.

12 changed files with 104 additions and 247 deletions

View File

@ -477,7 +477,7 @@ func handleSessionRequest(
cloudLogger.Trace().Interface("session", session).Msg("new session accepted") cloudLogger.Trace().Interface("session", session).Msg("new session accepted")
// Cancel any ongoing keyboard report multi when session changes // Cancel any ongoing keyboard report multi when session changes
cancelKeyboardMacro() cancelKeyboardReportMulti()
currentSession = session currentSession = session
_ = wsjson.Write(context.Background(), c, gin.H{"type": "answer", "data": sd}) _ = wsjson.Write(context.Background(), c, gin.H{"type": "answer", "data": sd})

View File

@ -1,6 +1,7 @@
package kvm package kvm
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -37,10 +38,7 @@ func handleHidRPCMessage(message hidrpc.Message, session *Session) {
logger.Warn().Err(err).Msg("failed to get keyboard macro report") logger.Warn().Err(err).Msg("failed to get keyboard macro report")
return return
} }
_, rpcErr = rpcExecuteKeyboardMacro(keyboardMacroReport.Macro) _, rpcErr = rpcKeyboardReportMulti(context.Background(), keyboardMacroReport.Macro)
case hidrpc.TypeCancelKeyboardMacroReport:
rpcCancelKeyboardMacro()
return
case hidrpc.TypePointerReport: case hidrpc.TypePointerReport:
pointerReport, err := message.PointerReport() pointerReport, err := message.PointerReport()
if err != nil { if err != nil {
@ -140,8 +138,6 @@ func reportHidRPC(params any, session *Session) {
message, err = hidrpc.NewKeyboardLedMessage(params).Marshal() message, err = hidrpc.NewKeyboardLedMessage(params).Marshal()
case usbgadget.KeysDownState: case usbgadget.KeysDownState:
message, err = hidrpc.NewKeydownStateMessage(params).Marshal() message, err = hidrpc.NewKeydownStateMessage(params).Marshal()
case hidrpc.KeyboardMacroStateReport:
message, err = hidrpc.NewKeyboardMacroStateMessage(params.State, params.IsPaste).Marshal()
default: default:
err = fmt.Errorf("unknown HID RPC message type: %T", params) err = fmt.Errorf("unknown HID RPC message type: %T", params)
} }
@ -178,10 +174,3 @@ func (s *Session) reportHidRPCKeysDownState(state usbgadget.KeysDownState) {
} }
reportHidRPC(state, s) reportHidRPC(state, s)
} }
func (s *Session) reportHidRPCKeyboardMacroState(state hidrpc.KeyboardMacroStateReport) {
if !s.hidRPCAvailable {
writeJSONRPCEvent("keyboardMacroState", state, s)
}
reportHidRPC(state, s)
}

View File

@ -32,13 +32,10 @@ func GetQueueIndex(messageType MessageType) int {
switch messageType { switch messageType {
case TypeHandshake: case TypeHandshake:
return 0 return 0
case TypeKeyboardReport, TypeKeypressReport, TypeKeyboardMacroReport, TypeKeyboardLedState, TypeKeydownState, TypeKeyboardMacroStateReport: case TypeKeyboardReport, TypeKeypressReport, TypeKeyboardLedState, TypeKeydownState, TypeKeyboardMacroStateReport:
return 1 return 1
case TypePointerReport, TypeMouseReport, TypeWheelReport: case TypePointerReport, TypeMouseReport, TypeWheelReport:
return 2 return 2
// we don't want to block the queue for this message
case TypeCancelKeyboardMacroReport:
return 3
default: default:
return 3 return 3
} }
@ -104,19 +101,3 @@ func NewKeydownStateMessage(state usbgadget.KeysDownState) *Message {
d: data, d: data,
} }
} }
// NewKeyboardMacroStateMessage creates a new keyboard macro state message.
func NewKeyboardMacroStateMessage(state bool, isPaste bool) *Message {
data := make([]byte, 2)
if state {
data[0] = 1
}
if isPaste {
data[1] = 1
}
return &Message{
t: TypeKeyboardMacroStateReport,
d: data,
}
}

View File

@ -182,20 +182,3 @@ func (m *Message) MouseReport() (MouseReport, error) {
Button: uint8(m.d[2]), Button: uint8(m.d[2]),
}, nil }, nil
} }
type KeyboardMacroStateReport struct {
State bool
IsPaste bool
}
// KeyboardMacroStateReport returns the keyboard macro state report from the message.
func (m *Message) KeyboardMacroStateReport() (KeyboardMacroStateReport, error) {
if m.t != TypeKeyboardMacroStateReport {
return KeyboardMacroStateReport{}, fmt.Errorf("invalid message type: %d", m.t)
}
return KeyboardMacroStateReport{
State: m.d[0] == uint8(1),
IsPaste: m.d[1] == uint8(1),
}, nil
}

View File

@ -1,7 +1,6 @@
package kvm package kvm
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
@ -11,7 +10,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/pion/webrtc/v4" "github.com/pion/webrtc/v4"
@ -1052,100 +1050,85 @@ func rpcSetLocalLoopbackOnly(enabled bool) error {
return nil return nil
} }
var ( func cancelKeyboardReportMulti() {
keyboardMacroCancel context.CancelFunc
keyboardMacroLock sync.Mutex
)
// cancelKeyboardMacro cancels any ongoing keyboard macro execution
func cancelKeyboardMacro() {
keyboardMacroLock.Lock()
defer keyboardMacroLock.Unlock()
if keyboardMacroCancel != nil {
keyboardMacroCancel()
logger.Info().Msg("canceled keyboard macro")
keyboardMacroCancel = nil
}
} }
func setKeyboardMacroCancel(cancel context.CancelFunc) { // // cancelKeyboardReportMulti cancels any ongoing keyboard report multi execution
keyboardMacroLock.Lock() // func cancelKeyboardReportMulti() {
defer keyboardMacroLock.Unlock() // keyboardReportMultiLock.Lock()
// defer keyboardReportMultiLock.Unlock()
keyboardMacroCancel = cancel // if keyboardReportMultiCancel != nil {
} // keyboardReportMultiCancel()
// logger.Info().Msg("canceled keyboard report multi")
// keyboardReportMultiCancel = nil
// }
// }
func rpcExecuteKeyboardMacro(macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) { // func setKeyboardReportMultiCancel(cancel context.CancelFunc) {
cancelKeyboardMacro() // keyboardReportMultiLock.Lock()
// defer keyboardReportMultiLock.Unlock()
ctx, cancel := context.WithCancel(context.Background()) // keyboardReportMultiCancel = cancel
setKeyboardMacroCancel(cancel) // }
s := hidrpc.KeyboardMacroStateReport{ // func rpcKeyboardReportMultiWrapper(macro []map[string]any) (usbgadget.KeysDownState, error) {
State: true, // // cancelKeyboardReportMulti()
IsPaste: true,
}
if currentSession != nil { // // ctx, cancel := context.WithCancel(context.Background())
currentSession.reportHidRPCKeyboardMacroState(s) // // setKeyboardReportMultiCancel(cancel)
}
result, err := rpcDoExecuteKeyboardMacro(ctx, macro) // // writeJSONRPCEvent("keyboardReportMultiState", true, currentSession)
setKeyboardMacroCancel(nil) // // result, err := rpcKeyboardReportMulti(ctx, macro)
s.State = false // // setKeyboardReportMultiCancel(nil)
if currentSession != nil {
currentSession.reportHidRPCKeyboardMacroState(s)
}
return result, err // // writeJSONRPCEvent("keyboardReportMultiState", false, currentSession)
}
func rpcCancelKeyboardMacro() { // // return result, err
cancelKeyboardMacro() // }
}
var keyboardClearStateKeys = make([]byte, 6) // var (
// keyboardReportMultiCancel context.CancelFunc
// keyboardReportMultiLock sync.Mutex
// )
func isClearKeyStep(step hidrpc.KeyboardMacro) bool { // func rpcCancelKeyboardReportMulti() {
return step.Modifier == 0 && bytes.Equal(step.Keys, keyboardClearStateKeys) // cancelKeyboardReportMulti()
} // }
func rpcDoExecuteKeyboardMacro(ctx context.Context, macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) { func rpcKeyboardReportMulti(ctx context.Context, macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) {
var last usbgadget.KeysDownState var last usbgadget.KeysDownState
var err error var err error
logger.Debug().Interface("macro", macro).Msg("Executing keyboard macro") logger.Debug().Interface("macro", macro).Msg("Executing keyboard report multi")
for i, step := range macro { for i, step := range macro {
// Check for cancellation before each step
select {
case <-ctx.Done():
logger.Debug().Msg("Keyboard report multi context cancelled")
return last, ctx.Err()
default:
}
delay := time.Duration(step.Delay) * time.Millisecond delay := time.Duration(step.Delay) * time.Millisecond
logger.Info().Int("step", i).Uint16("delay", step.Delay).Msg("Keyboard report multi delay")
last, err = rpcKeyboardReport(step.Modifier, step.Keys) last, err = rpcKeyboardReport(step.Modifier, step.Keys)
if err != nil { if err != nil {
logger.Warn().Err(err).Msg("failed to execute keyboard macro") logger.Warn().Err(err).Msg("failed to execute keyboard report multi")
return last, err return last, err
} }
// notify the device that the keyboard state is being cleared
if isClearKeyStep(step) {
gadget.UpdateKeysDown(0, keyboardClearStateKeys)
}
// Use context-aware sleep that can be cancelled // Use context-aware sleep that can be cancelled
select { select {
case <-time.After(delay): case <-time.After(delay):
// Sleep completed normally // Sleep completed normally
case <-ctx.Done(): case <-ctx.Done():
// make sure keyboard state is reset logger.Debug().Int("step", i).Msg("Keyboard report multi cancelled during sleep")
_, err := rpcKeyboardReport(0, keyboardClearStateKeys)
if err != nil {
logger.Warn().Err(err).Msg("failed to reset keyboard state")
}
gadget.UpdateKeysDown(0, keyboardClearStateKeys)
logger.Debug().Int("step", i).Msg("Keyboard macro cancelled during sleep")
return last, ctx.Err() return last, ctx.Err()
} }
} }
@ -1154,16 +1137,18 @@ func rpcDoExecuteKeyboardMacro(ctx context.Context, macro []hidrpc.KeyboardMacro
} }
var rpcHandlers = map[string]RPCHandler{ var rpcHandlers = map[string]RPCHandler{
"ping": {Func: rpcPing}, "ping": {Func: rpcPing},
"reboot": {Func: rpcReboot, Params: []string{"force"}}, "reboot": {Func: rpcReboot, Params: []string{"force"}},
"getDeviceID": {Func: rpcGetDeviceID}, "getDeviceID": {Func: rpcGetDeviceID},
"deregisterDevice": {Func: rpcDeregisterDevice}, "deregisterDevice": {Func: rpcDeregisterDevice},
"getCloudState": {Func: rpcGetCloudState}, "getCloudState": {Func: rpcGetCloudState},
"getNetworkState": {Func: rpcGetNetworkState}, "getNetworkState": {Func: rpcGetNetworkState},
"getNetworkSettings": {Func: rpcGetNetworkSettings}, "getNetworkSettings": {Func: rpcGetNetworkSettings},
"setNetworkSettings": {Func: rpcSetNetworkSettings, Params: []string{"settings"}}, "setNetworkSettings": {Func: rpcSetNetworkSettings, Params: []string{"settings"}},
"renewDHCPLease": {Func: rpcRenewDHCPLease}, "renewDHCPLease": {Func: rpcRenewDHCPLease},
"keyboardReport": {Func: rpcKeyboardReport, Params: []string{"modifier", "keys"}}, "keyboardReport": {Func: rpcKeyboardReport, Params: []string{"modifier", "keys"}},
// "keyboardReportMulti": {Func: rpcKeyboardReportMultiWrapper, Params: []string{"macro"}},
// "cancelKeyboardReportMulti": {Func: rpcCancelKeyboardReportMulti},
"getKeyboardLedState": {Func: rpcGetKeyboardLedState}, "getKeyboardLedState": {Func: rpcGetKeyboardLedState},
"keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}}, "keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}},
"getKeyDownState": {Func: rpcGetKeysDownState}, "getKeyDownState": {Func: rpcGetKeysDownState},

View File

@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import { LuCornerDownLeft } from "react-icons/lu"; import { LuCornerDownLeft } from "react-icons/lu";
import { ExclamationCircleIcon } from "@heroicons/react/16/solid"; import { ExclamationCircleIcon } from "@heroicons/react/16/solid";
import { useClose } from "@headlessui/react"; import { useClose } from "@headlessui/react";
@ -12,7 +12,6 @@ import { useHidStore, useSettingsStore, useUiStore } from "@/hooks/stores";
import useKeyboard from "@/hooks/useKeyboard"; import useKeyboard from "@/hooks/useKeyboard";
import useKeyboardLayout from "@/hooks/useKeyboardLayout"; import useKeyboardLayout from "@/hooks/useKeyboardLayout";
import notifications from "@/notifications"; import notifications from "@/notifications";
import { InputFieldWithLabel } from "@components/InputField";
export default function PasteModal() { export default function PasteModal() {
const TextAreaRef = useRef<HTMLTextAreaElement>(null); const TextAreaRef = useRef<HTMLTextAreaElement>(null);
@ -23,13 +22,6 @@ export default function PasteModal() {
const { executeMacro, cancelExecuteMacro } = useKeyboard(); const { executeMacro, cancelExecuteMacro } = useKeyboard();
const [invalidChars, setInvalidChars] = useState<string[]>([]); const [invalidChars, setInvalidChars] = useState<string[]>([]);
const [delayValue, setDelayValue] = useState(100);
const delay = useMemo(() => {
if (delayValue < 50 || delayValue > 65534) {
return 100;
}
return delayValue;
}, [delayValue]);
const close = useClose(); const close = useClose();
const { setKeyboardLayout } = useSettingsStore(); const { setKeyboardLayout } = useSettingsStore();
@ -49,6 +41,9 @@ export default function PasteModal() {
}, [setDisableVideoFocusTrap, cancelExecuteMacro]); }, [setDisableVideoFocusTrap, cancelExecuteMacro]);
const onConfirmPaste = useCallback(async () => { const onConfirmPaste = useCallback(async () => {
// setPasteModeEnabled(false);
// setDisableVideoFocusTrap(false);
if (!TextAreaRef.current || !selectedKeyboard) return; if (!TextAreaRef.current || !selectedKeyboard) return;
const text = TextAreaRef.current.value; const text = TextAreaRef.current.value;
@ -76,7 +71,7 @@ export default function PasteModal() {
macroSteps.push({ macroSteps.push({
keys: [String(accentKey.key)], keys: [String(accentKey.key)],
modifiers: accentModifiers.length > 0 ? accentModifiers : null, modifiers: accentModifiers.length > 0 ? accentModifiers : null,
delay, delay: 100,
}); });
} }
@ -88,12 +83,12 @@ export default function PasteModal() {
macroSteps.push({ macroSteps.push({
keys: [String(key)], keys: [String(key)],
modifiers: modifiers.length > 0 ? modifiers : null, modifiers: modifiers.length > 0 ? modifiers : null,
delay delay: 100,
}); });
// if what was requested was a dead key, we need to send an unmodified space to emit // if what was requested was a dead key, we need to send an unmodified space to emit
// just the accent character // just the accent character
if (deadKey) macroSteps.push({ keys: ["Space"], modifiers: null, delay }); if (deadKey) macroSteps.push({ keys: ["Space"], modifiers: null, delay: 100 });
} }
if (macroSteps.length > 0) { if (macroSteps.length > 0) {
@ -103,7 +98,7 @@ export default function PasteModal() {
console.error("Failed to paste text:", error); console.error("Failed to paste text:", error);
notifications.error("Failed to paste text"); notifications.error("Failed to paste text");
} }
}, [selectedKeyboard, executeMacro, delay]); }, [selectedKeyboard, executeMacro]);
useEffect(() => { useEffect(() => {
if (TextAreaRef.current) { if (TextAreaRef.current) {
@ -176,27 +171,6 @@ export default function PasteModal() {
)} )}
</div> </div>
</div> </div>
<div className="text-xs text-slate-600 dark:text-slate-400">
<InputFieldWithLabel
type="number"
label="Delay between keys"
placeholder="Delay between keys"
min={50}
max={65534}
value={delayValue}
onChange={e => {
setDelayValue(parseInt(e.target.value, 10));
}}
/>
{delayValue < 50 || delayValue > 65534 && (
<div className="mt-2 flex items-center gap-x-2">
<ExclamationCircleIcon className="h-4 w-4 text-red-500 dark:text-red-400" />
<span className="text-xs text-red-500 dark:text-red-400">
Delay must be between 50 and 65534
</span>
</div>
)}
</div>
<div className="space-y-4"> <div className="space-y-4">
<p className="text-xs text-slate-600 dark:text-slate-400"> <p className="text-xs text-slate-600 dark:text-slate-400">
Sending text using keyboard layout: {selectedKeyboard.isoCode}- Sending text using keyboard layout: {selectedKeyboard.isoCode}-

View File

@ -8,10 +8,8 @@ export const HID_RPC_MESSAGE_TYPES = {
KeypressReport: 0x05, KeypressReport: 0x05,
MouseReport: 0x06, MouseReport: 0x06,
KeyboardMacroReport: 0x07, KeyboardMacroReport: 0x07,
CancelKeyboardMacroReport: 0x08,
KeyboardLedState: 0x32, KeyboardLedState: 0x32,
KeysDownState: 0x33, KeysDownState: 0x33,
KeyboardMacroStateReport: 0x34,
} }
export type HidRpcMessageType = typeof HID_RPC_MESSAGE_TYPES[keyof typeof HID_RPC_MESSAGE_TYPES]; export type HidRpcMessageType = typeof HID_RPC_MESSAGE_TYPES[keyof typeof HID_RPC_MESSAGE_TYPES];
@ -213,39 +211,34 @@ export class KeyboardReportMessage extends RpcMessage {
} }
} }
export interface KeyboardMacroStep extends KeysDownState { export interface KeyboardMacro extends KeysDownState {
delay: number; delay: number;
} }
export class KeyboardMacroReportMessage extends RpcMessage { export class KeyboardMacroReportMessage extends RpcMessage {
isPaste: boolean; isPaste: boolean;
length: number; length: number;
steps: KeyboardMacroStep[]; macro: KeyboardMacro[];
KEYS_LENGTH = 6; KEYS_LENGTH = 6;
constructor(isPaste: boolean, length: number, steps: KeyboardMacroStep[]) { constructor(isPaste: boolean, length: number, macro: KeyboardMacro[]) {
super(HID_RPC_MESSAGE_TYPES.KeyboardMacroReport); super(HID_RPC_MESSAGE_TYPES.KeyboardMacroReport);
this.isPaste = isPaste; this.isPaste = isPaste;
this.length = length; this.length = length;
this.steps = steps; this.macro = macro;
} }
marshal(): Uint8Array { marshal(): Uint8Array {
// validate if length is correct const dataHeader = new Uint8Array([
if (this.length !== this.steps.length) {
throw new Error(`Length ${this.length} is not equal to the number of steps ${this.steps.length}`);
}
const data = new Uint8Array(this.length * 9 + 6);
data.set(new Uint8Array([
this.messageType, this.messageType,
this.isPaste ? 1 : 0, this.isPaste ? 1 : 0,
...fromUint32toUint8(this.length), ...fromUint32toUint8(this.length),
]), 0); ]);
for (let i = 0; i < this.length; i++) { let dataBody = new Uint8Array();
const step = this.steps[i];
for (const step of this.macro) {
if (!withinUint8Range(step.modifier)) { if (!withinUint8Range(step.modifier)) {
throw new Error(`Modifier ${step.modifier} is not within the uint8 range`); throw new Error(`Modifier ${step.modifier} is not within the uint8 range`);
} }
@ -269,40 +262,10 @@ export class KeyboardMacroReportMessage extends RpcMessage {
...keys, ...keys,
...fromUint16toUint8(step.delay), ...fromUint16toUint8(step.delay),
]); ]);
const offset = 6 + i * 9;
dataBody = new Uint8Array([...dataBody, ...macroBinary]);
data.set(macroBinary, offset);
} }
return new Uint8Array([...dataHeader, ...dataBody]);
return data;
}
}
export class KeyboardMacroStateReportMessage extends RpcMessage {
state: boolean;
isPaste: boolean;
constructor(state: boolean, isPaste: boolean) {
super(HID_RPC_MESSAGE_TYPES.KeyboardMacroStateReport);
this.state = state;
this.isPaste = isPaste;
}
marshal(): Uint8Array {
return new Uint8Array([
this.messageType,
this.state ? 1 : 0,
this.isPaste ? 1 : 0,
]);
}
public static unmarshal(data: Uint8Array): KeyboardMacroStateReportMessage | 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);
} }
} }
@ -376,17 +339,6 @@ export class PointerReportMessage extends RpcMessage {
} }
} }
export class CancelKeyboardMacroReportMessage extends RpcMessage {
constructor() {
super(HID_RPC_MESSAGE_TYPES.CancelKeyboardMacroReport);
}
marshal(): Uint8Array {
return new Uint8Array([this.messageType]);
}
}
export class MouseReportMessage extends RpcMessage { export class MouseReportMessage extends RpcMessage {
dx: number; dx: number;
dy: number; dy: number;
@ -415,9 +367,6 @@ export const messageRegistry = {
[HID_RPC_MESSAGE_TYPES.KeyboardLedState]: KeyboardLedStateMessage, [HID_RPC_MESSAGE_TYPES.KeyboardLedState]: KeyboardLedStateMessage,
[HID_RPC_MESSAGE_TYPES.KeyboardReport]: KeyboardReportMessage, [HID_RPC_MESSAGE_TYPES.KeyboardReport]: KeyboardReportMessage,
[HID_RPC_MESSAGE_TYPES.KeypressReport]: KeypressReportMessage, [HID_RPC_MESSAGE_TYPES.KeypressReport]: KeypressReportMessage,
[HID_RPC_MESSAGE_TYPES.KeyboardMacroReport]: KeyboardMacroReportMessage,
[HID_RPC_MESSAGE_TYPES.CancelKeyboardMacroReport]: CancelKeyboardMacroReportMessage,
[HID_RPC_MESSAGE_TYPES.KeyboardMacroStateReport]: KeyboardMacroStateReportMessage,
} }
export const unmarshalHidRpcMessage = (data: Uint8Array): RpcMessage | undefined => { export const unmarshalHidRpcMessage = (data: Uint8Array): RpcMessage | undefined => {

View File

@ -3,10 +3,9 @@ import { useCallback, useEffect, useMemo } from "react";
import { useRTCStore } from "@/hooks/stores"; import { useRTCStore } from "@/hooks/stores";
import { import {
CancelKeyboardMacroReportMessage,
HID_RPC_VERSION, HID_RPC_VERSION,
HandshakeMessage, HandshakeMessage,
KeyboardMacroStep, KeyboardMacro,
KeyboardMacroReportMessage, KeyboardMacroReportMessage,
KeyboardReportMessage, KeyboardReportMessage,
KeypressReportMessage, KeypressReportMessage,
@ -72,16 +71,10 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
); );
const reportKeyboardMacroEvent = useCallback( const reportKeyboardMacroEvent = useCallback(
(macro: KeyboardMacroStep[]) => { (macro: KeyboardMacro[]) => {
const d = new KeyboardMacroReportMessage(false, macro.length, macro); const d = new KeyboardMacroReportMessage(false, macro.length, macro);
sendMessage(d); sendMessage(d);
}, console.log("Sent keyboard macro report", d, d.marshal());
[sendMessage],
);
const cancelOngoingKeyboardMacro = useCallback(
() => {
sendMessage(new CancelKeyboardMacroReportMessage());
}, },
[sendMessage], [sendMessage],
); );
@ -162,7 +155,6 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
reportAbsMouseEvent, reportAbsMouseEvent,
reportRelMouseEvent, reportRelMouseEvent,
reportKeyboardMacroEvent, reportKeyboardMacroEvent,
cancelOngoingKeyboardMacro,
rpcHidProtocolVersion, rpcHidProtocolVersion,
rpcHidReady, rpcHidReady,
rpcHidStatus, rpcHidStatus,

View File

@ -9,13 +9,13 @@ import {
} from "@/hooks/stores"; } from "@/hooks/stores";
import { JsonRpcResponse, useJsonRpc } from "@/hooks/useJsonRpc"; import { JsonRpcResponse, useJsonRpc } from "@/hooks/useJsonRpc";
import { useHidRpc } from "@/hooks/useHidRpc"; import { useHidRpc } from "@/hooks/useHidRpc";
import { KeyboardLedStateMessage, KeyboardMacroStateReportMessage, KeyboardMacroStep, KeysDownStateMessage } from "@/hooks/hidRpc"; import { KeyboardLedStateMessage, KeyboardMacro, KeysDownStateMessage } from "@/hooks/hidRpc";
import { hidKeyToModifierMask, keys, modifiers } from "@/keyboardMappings"; import { hidKeyToModifierMask, keys, modifiers } from "@/keyboardMappings";
export default function useKeyboard() { export default function useKeyboard() {
const { send } = useJsonRpc(); const { send } = useJsonRpc();
const { rpcDataChannel } = useRTCStore(); const { rpcDataChannel } = useRTCStore();
const { keysDownState, setKeysDownState, setKeyboardLedState, setPasteModeEnabled } = useHidStore(); const { keysDownState, setKeysDownState, setKeyboardLedState } = useHidStore();
// INTRODUCTION: The earlier version of the JetKVM device shipped with all keyboard state // INTRODUCTION: The earlier version of the JetKVM device shipped with all keyboard state
// being tracked on the browser/client-side. When adding the keyPressReport API to the // being tracked on the browser/client-side. When adding the keyPressReport API to the
@ -33,7 +33,6 @@ export default function useKeyboard() {
reportKeyboardEvent: sendKeyboardEventHidRpc, reportKeyboardEvent: sendKeyboardEventHidRpc,
reportKeypressEvent: sendKeypressEventHidRpc, reportKeypressEvent: sendKeypressEventHidRpc,
reportKeyboardMacroEvent: sendKeyboardMacroEventHidRpc, reportKeyboardMacroEvent: sendKeyboardMacroEventHidRpc,
cancelOngoingKeyboardMacro: cancelOngoingKeyboardMacroHidRpc,
rpcHidReady, rpcHidReady,
} = useHidRpc(message => { } = useHidRpc(message => {
switch (message.constructor) { switch (message.constructor) {
@ -43,10 +42,6 @@ export default function useKeyboard() {
case KeyboardLedStateMessage: case KeyboardLedStateMessage:
setKeyboardLedState((message as KeyboardLedStateMessage).keyboardLedState); setKeyboardLedState((message as KeyboardLedStateMessage).keyboardLedState);
break; break;
case KeyboardMacroStateReportMessage:
if (!(message as KeyboardMacroStateReportMessage).isPaste) break;
setPasteModeEnabled((message as KeyboardMacroStateReportMessage).state);
break;
default: default:
break; break;
} }
@ -106,7 +101,7 @@ export default function useKeyboard() {
const executeMacro = async ( const executeMacro = async (
steps: { keys: string[] | null; modifiers: string[] | null; delay: number }[], steps: { keys: string[] | null; modifiers: string[] | null; delay: number }[],
) => { ) => {
const macro: KeyboardMacroStep[] = []; const macro: KeyboardMacro[] = [];
for (const [_, step] of steps.entries()) { for (const [_, step] of steps.entries()) {
const keyValues = (step.keys || []).map(key => keys[key]).filter(Boolean); const keyValues = (step.keys || []).map(key => keys[key]).filter(Boolean);
@ -116,8 +111,8 @@ export default function useKeyboard() {
// If the step has keys and/or modifiers, press them and hold for the delay // If the step has keys and/or modifiers, press them and hold for the delay
if (keyValues.length > 0 || modifierMask > 0) { if (keyValues.length > 0 || modifierMask > 0) {
macro.push({ keys: keyValues, modifier: modifierMask, delay: 20 }); macro.push({ keys: keyValues, modifier: modifierMask, delay: 50 });
macro.push({ ...MACRO_RESET_KEYBOARD_STATE, delay: step.delay || 100 }); macro.push({ ...MACRO_RESET_KEYBOARD_STATE, delay: 200 });
} }
} }
@ -125,9 +120,12 @@ export default function useKeyboard() {
}; };
const cancelExecuteMacro = useCallback(async () => { const cancelExecuteMacro = useCallback(async () => {
if (!rpcHidReady) return; send("cancelKeyboardReportMulti", {}, (resp: JsonRpcResponse) => {
cancelOngoingKeyboardMacroHidRpc(); if ("error" in resp) {
}, [rpcHidReady, cancelOngoingKeyboardMacroHidRpc]); console.error(`Failed to cancel keyboard report multi`, resp.error);
}
});
}, [send]);
// handleKeyPress is used to handle a key press or release event. // handleKeyPress is used to handle a key press or release event.
// This function handle both key press and key release events. // This function handle both key press and key release events.

View File

@ -580,7 +580,7 @@ export default function KvmIdRoute() {
const { setNetworkState} = useNetworkStateStore(); const { setNetworkState} = useNetworkStateStore();
const { setHdmiState } = useVideoStore(); const { setHdmiState } = useVideoStore();
const { const {
keyboardLedState, setKeyboardLedState, keyboardLedState, setKeyboardLedState, setPasteModeEnabled,
keysDownState, setKeysDownState, setUsbState, keysDownState, setKeysDownState, setUsbState,
} = useHidStore(); } = useHidStore();
@ -598,6 +598,12 @@ export default function KvmIdRoute() {
setUsbState(usbState); setUsbState(usbState);
} }
if (resp.method === "keyboardReportMultiState") {
const reportMultiState = resp.params as unknown as boolean;
console.debug("Setting keyboard report multi state", reportMultiState);
setPasteModeEnabled(reportMultiState);
}
if (resp.method === "videoInputState") { if (resp.method === "videoInputState") {
const hdmiState = resp.params as Parameters<VideoState["setHdmiState"]>[0]; const hdmiState = resp.params as Parameters<VideoState["setHdmiState"]>[0];
console.debug("Setting HDMI state", hdmiState); console.debug("Setting HDMI state", hdmiState);

2
web.go
View File

@ -200,7 +200,7 @@ func handleWebRTCSession(c *gin.Context) {
} }
// Cancel any ongoing keyboard report multi when session changes // Cancel any ongoing keyboard report multi when session changes
cancelKeyboardMacro() cancelKeyboardReportMulti()
currentSession = session currentSession = session
c.JSON(http.StatusOK, gin.H{"sd": sd}) c.JSON(http.StatusOK, gin.H{"sd": sd})

View File

@ -267,7 +267,7 @@ func newSession(config SessionConfig) (*Session, error) {
scopedLogger.Debug().Msg("ICE Connection State is closed, unmounting virtual media") scopedLogger.Debug().Msg("ICE Connection State is closed, unmounting virtual media")
if session == currentSession { if session == currentSession {
// Cancel any ongoing keyboard report multi when session closes // Cancel any ongoing keyboard report multi when session closes
cancelKeyboardMacro() cancelKeyboardReportMulti()
currentSession = nil currentSession = nil
} }
// Stop RPC processor // Stop RPC processor