mirror of https://github.com/jetkvm/kvm.git
feat: allow paste progress to be cancelled
This commit is contained in:
parent
d7c8abbb11
commit
7014560b41
2
cloud.go
2
cloud.go
|
@ -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
|
||||||
cancelKeyboardReportMulti()
|
cancelKeyboardMacro()
|
||||||
|
|
||||||
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})
|
||||||
|
|
15
hidrpc.go
15
hidrpc.go
|
@ -1,7 +1,6 @@
|
||||||
package kvm
|
package kvm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -38,7 +37,10 @@ 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 = rpcKeyboardReportMulti(context.Background(), keyboardMacroReport.Macro)
|
_, rpcErr = rpcExecuteKeyboardMacro(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 {
|
||||||
|
@ -138,6 +140,8 @@ 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)
|
||||||
}
|
}
|
||||||
|
@ -174,3 +178,10 @@ 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)
|
||||||
|
}
|
||||||
|
|
|
@ -32,10 +32,13 @@ func GetQueueIndex(messageType MessageType) int {
|
||||||
switch messageType {
|
switch messageType {
|
||||||
case TypeHandshake:
|
case TypeHandshake:
|
||||||
return 0
|
return 0
|
||||||
case TypeKeyboardReport, TypeKeypressReport, TypeKeyboardLedState, TypeKeydownState, TypeKeyboardMacroStateReport:
|
case TypeKeyboardReport, TypeKeypressReport, TypeKeyboardMacroReport, 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
|
||||||
}
|
}
|
||||||
|
@ -101,3 +104,19 @@ 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -182,3 +182,20 @@ 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
|
||||||
|
}
|
||||||
|
|
93
jsonrpc.go
93
jsonrpc.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v4"
|
"github.com/pion/webrtc/v4"
|
||||||
|
@ -1050,75 +1051,69 @@ func rpcSetLocalLoopbackOnly(enabled bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cancelKeyboardReportMulti() {
|
var (
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // cancelKeyboardReportMulti cancels any ongoing keyboard report multi execution
|
func setKeyboardMacroCancel(cancel context.CancelFunc) {
|
||||||
// func cancelKeyboardReportMulti() {
|
keyboardMacroLock.Lock()
|
||||||
// keyboardReportMultiLock.Lock()
|
defer keyboardMacroLock.Unlock()
|
||||||
// defer keyboardReportMultiLock.Unlock()
|
|
||||||
|
|
||||||
// if keyboardReportMultiCancel != nil {
|
keyboardMacroCancel = cancel
|
||||||
// keyboardReportMultiCancel()
|
}
|
||||||
// logger.Info().Msg("canceled keyboard report multi")
|
|
||||||
// keyboardReportMultiCancel = nil
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func setKeyboardReportMultiCancel(cancel context.CancelFunc) {
|
func rpcExecuteKeyboardMacro(macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) {
|
||||||
// keyboardReportMultiLock.Lock()
|
cancelKeyboardMacro()
|
||||||
// defer keyboardReportMultiLock.Unlock()
|
|
||||||
|
|
||||||
// keyboardReportMultiCancel = cancel
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
// }
|
setKeyboardMacroCancel(cancel)
|
||||||
|
|
||||||
// func rpcKeyboardReportMultiWrapper(macro []map[string]any) (usbgadget.KeysDownState, error) {
|
s := hidrpc.KeyboardMacroStateReport{
|
||||||
// // cancelKeyboardReportMulti()
|
State: true,
|
||||||
|
IsPaste: true,
|
||||||
|
}
|
||||||
|
|
||||||
// // ctx, cancel := context.WithCancel(context.Background())
|
reportHidRPC(s, currentSession)
|
||||||
// // setKeyboardReportMultiCancel(cancel)
|
|
||||||
|
|
||||||
// // writeJSONRPCEvent("keyboardReportMultiState", true, currentSession)
|
result, err := rpcDoExecuteKeyboardMacro(ctx, macro)
|
||||||
|
|
||||||
// // result, err := rpcKeyboardReportMulti(ctx, macro)
|
setKeyboardMacroCancel(nil)
|
||||||
|
|
||||||
// // setKeyboardReportMultiCancel(nil)
|
s.State = false
|
||||||
|
reportHidRPC(s, currentSession)
|
||||||
|
|
||||||
// // writeJSONRPCEvent("keyboardReportMultiState", false, currentSession)
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
// // return result, err
|
func rpcCancelKeyboardMacro() {
|
||||||
// }
|
cancelKeyboardMacro()
|
||||||
|
}
|
||||||
|
|
||||||
// var (
|
func rpcDoExecuteKeyboardMacro(ctx context.Context, macro []hidrpc.KeyboardMacro) (usbgadget.KeysDownState, error) {
|
||||||
// keyboardReportMultiCancel context.CancelFunc
|
|
||||||
// keyboardReportMultiLock sync.Mutex
|
|
||||||
// )
|
|
||||||
|
|
||||||
// func rpcCancelKeyboardReportMulti() {
|
|
||||||
// cancelKeyboardReportMulti()
|
|
||||||
// }
|
|
||||||
|
|
||||||
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 report multi")
|
logger.Debug().Interface("macro", macro).Msg("Executing keyboard macro")
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
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 report multi")
|
logger.Warn().Err(err).Msg("failed to execute keyboard macro")
|
||||||
return last, err
|
return last, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1127,7 +1122,9 @@ func rpcKeyboardReportMulti(ctx context.Context, macro []hidrpc.KeyboardMacro) (
|
||||||
case <-time.After(delay):
|
case <-time.After(delay):
|
||||||
// Sleep completed normally
|
// Sleep completed normally
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logger.Debug().Int("step", i).Msg("Keyboard report multi cancelled during sleep")
|
// make sure keyboard state is reset
|
||||||
|
rpcKeyboardReport(0, make([]byte, 6))
|
||||||
|
logger.Debug().Int("step", i).Msg("Keyboard macro cancelled during sleep")
|
||||||
return last, ctx.Err()
|
return last, ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1146,8 +1143,6 @@ var rpcHandlers = map[string]RPCHandler{
|
||||||
"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},
|
||||||
|
|
|
@ -41,9 +41,6 @@ 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;
|
||||||
|
|
|
@ -8,8 +8,10 @@ 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];
|
||||||
|
@ -211,22 +213,22 @@ export class KeyboardReportMessage extends RpcMessage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface KeyboardMacro extends KeysDownState {
|
export interface KeyboardMacroStep extends KeysDownState {
|
||||||
delay: number;
|
delay: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class KeyboardMacroReportMessage extends RpcMessage {
|
export class KeyboardMacroReportMessage extends RpcMessage {
|
||||||
isPaste: boolean;
|
isPaste: boolean;
|
||||||
length: number;
|
length: number;
|
||||||
macro: KeyboardMacro[];
|
steps: KeyboardMacroStep[];
|
||||||
|
|
||||||
KEYS_LENGTH = 6;
|
KEYS_LENGTH = 6;
|
||||||
|
|
||||||
constructor(isPaste: boolean, length: number, macro: KeyboardMacro[]) {
|
constructor(isPaste: boolean, length: number, steps: KeyboardMacroStep[]) {
|
||||||
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.macro = macro;
|
this.steps = steps;
|
||||||
}
|
}
|
||||||
|
|
||||||
marshal(): Uint8Array {
|
marshal(): Uint8Array {
|
||||||
|
@ -238,7 +240,7 @@ export class KeyboardMacroReportMessage extends RpcMessage {
|
||||||
|
|
||||||
let dataBody = new Uint8Array();
|
let dataBody = new Uint8Array();
|
||||||
|
|
||||||
for (const step of this.macro) {
|
for (const step of this.steps) {
|
||||||
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,6 +271,33 @@ export class KeyboardMacroReportMessage extends RpcMessage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class KeyboardLedStateMessage extends RpcMessage {
|
export class KeyboardLedStateMessage extends RpcMessage {
|
||||||
keyboardLedState: KeyboardLedState;
|
keyboardLedState: KeyboardLedState;
|
||||||
|
|
||||||
|
@ -339,6 +368,17 @@ 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;
|
||||||
|
@ -367,6 +407,9 @@ 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 => {
|
||||||
|
|
|
@ -3,9 +3,10 @@ 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,
|
||||||
KeyboardMacro,
|
KeyboardMacroStep,
|
||||||
KeyboardMacroReportMessage,
|
KeyboardMacroReportMessage,
|
||||||
KeyboardReportMessage,
|
KeyboardReportMessage,
|
||||||
KeypressReportMessage,
|
KeypressReportMessage,
|
||||||
|
@ -71,7 +72,7 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const reportKeyboardMacroEvent = useCallback(
|
const reportKeyboardMacroEvent = useCallback(
|
||||||
(macro: KeyboardMacro[]) => {
|
(macro: KeyboardMacroStep[]) => {
|
||||||
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());
|
console.log("Sent keyboard macro report", d, d.marshal());
|
||||||
|
@ -79,6 +80,13 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
|
||||||
[sendMessage],
|
[sendMessage],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const cancelOngoingKeyboardMacro = useCallback(
|
||||||
|
() => {
|
||||||
|
sendMessage(new CancelKeyboardMacroReportMessage());
|
||||||
|
},
|
||||||
|
[sendMessage],
|
||||||
|
);
|
||||||
|
|
||||||
const sendHandshake = useCallback(() => {
|
const sendHandshake = useCallback(() => {
|
||||||
if (rpcHidProtocolVersion) return;
|
if (rpcHidProtocolVersion) return;
|
||||||
if (!rpcHidChannel) return;
|
if (!rpcHidChannel) return;
|
||||||
|
@ -155,6 +163,7 @@ export function useHidRpc(onHidRpcMessage?: (payload: RpcMessage) => void) {
|
||||||
reportAbsMouseEvent,
|
reportAbsMouseEvent,
|
||||||
reportRelMouseEvent,
|
reportRelMouseEvent,
|
||||||
reportKeyboardMacroEvent,
|
reportKeyboardMacroEvent,
|
||||||
|
cancelOngoingKeyboardMacro,
|
||||||
rpcHidProtocolVersion,
|
rpcHidProtocolVersion,
|
||||||
rpcHidReady,
|
rpcHidReady,
|
||||||
rpcHidStatus,
|
rpcHidStatus,
|
||||||
|
|
|
@ -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, KeyboardMacro, KeysDownStateMessage } from "@/hooks/hidRpc";
|
import { KeyboardLedStateMessage, KeyboardMacroStateReportMessage, KeyboardMacroStep, 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 } = useHidStore();
|
const { keysDownState, setKeysDownState, setKeyboardLedState, setPasteModeEnabled } = 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,6 +33,7 @@ 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) {
|
||||||
|
@ -42,6 +43,10 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +106,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: KeyboardMacro[] = [];
|
const macro: KeyboardMacroStep[] = [];
|
||||||
|
|
||||||
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);
|
||||||
|
@ -120,12 +125,9 @@ export default function useKeyboard() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const cancelExecuteMacro = useCallback(async () => {
|
const cancelExecuteMacro = useCallback(async () => {
|
||||||
send("cancelKeyboardReportMulti", {}, (resp: JsonRpcResponse) => {
|
if (!rpcHidReady) return;
|
||||||
if ("error" in resp) {
|
cancelOngoingKeyboardMacroHidRpc();
|
||||||
console.error(`Failed to cancel keyboard report multi`, resp.error);
|
}, [rpcHidReady, cancelOngoingKeyboardMacroHidRpc]);
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [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.
|
||||||
|
|
|
@ -580,7 +580,7 @@ export default function KvmIdRoute() {
|
||||||
const { setNetworkState} = useNetworkStateStore();
|
const { setNetworkState} = useNetworkStateStore();
|
||||||
const { setHdmiState } = useVideoStore();
|
const { setHdmiState } = useVideoStore();
|
||||||
const {
|
const {
|
||||||
keyboardLedState, setKeyboardLedState, setPasteModeEnabled,
|
keyboardLedState, setKeyboardLedState,
|
||||||
keysDownState, setKeysDownState, setUsbState,
|
keysDownState, setKeysDownState, setUsbState,
|
||||||
} = useHidStore();
|
} = useHidStore();
|
||||||
|
|
||||||
|
@ -598,12 +598,6 @@ 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
2
web.go
|
@ -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
|
||||||
cancelKeyboardReportMulti()
|
cancelKeyboardMacro()
|
||||||
|
|
||||||
currentSession = session
|
currentSession = session
|
||||||
c.JSON(http.StatusOK, gin.H{"sd": sd})
|
c.JSON(http.StatusOK, gin.H{"sd": sd})
|
||||||
|
|
|
@ -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
|
||||||
cancelKeyboardReportMulti()
|
cancelKeyboardMacro()
|
||||||
currentSession = nil
|
currentSession = nil
|
||||||
}
|
}
|
||||||
// Stop RPC processor
|
// Stop RPC processor
|
||||||
|
|
Loading…
Reference in New Issue