This commit is contained in:
Aveline 2025-09-09 22:13:25 +00:00 committed by GitHub
commit b9988220bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 345 additions and 179 deletions

View File

@ -475,6 +475,10 @@ func handleSessionRequest(
cloudLogger.Info().Interface("session", session).Msg("new session accepted") cloudLogger.Info().Interface("session", session).Msg("new session accepted")
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
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})
return nil return nil

View File

@ -1,7 +1,9 @@
package kvm package kvm
import ( import (
"errors"
"fmt" "fmt"
"io"
"time" "time"
"github.com/jetkvm/kvm/internal/hidrpc" "github.com/jetkvm/kvm/internal/hidrpc"
@ -143,6 +145,10 @@ func reportHidRPC(params any, session *Session) {
} }
if err := session.HidChannel.Send(message); err != nil { if err := session.HidChannel.Send(message); err != nil {
if errors.Is(err, io.ErrClosedPipe) {
logger.Debug().Err(err).Msg("HID RPC channel closed, skipping reportHidRPC")
return
}
logger.Warn().Err(err).Msg("failed to send HID RPC message") logger.Warn().Err(err).Msg("failed to send HID RPC message")
} }
} }

View File

@ -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"
@ -1049,88 +1050,194 @@ func rpcSetLocalLoopbackOnly(enabled bool) error {
return nil return nil
} }
var rpcHandlers = map[string]RPCHandler{ // cancelKeyboardReportMulti cancels any ongoing keyboard report multi execution
"ping": {Func: rpcPing}, func cancelKeyboardReportMulti() {
"reboot": {Func: rpcReboot, Params: []string{"force"}}, keyboardReportMultiLock.Lock()
"getDeviceID": {Func: rpcGetDeviceID}, defer keyboardReportMultiLock.Unlock()
"deregisterDevice": {Func: rpcDeregisterDevice},
"getCloudState": {Func: rpcGetCloudState}, if keyboardReportMultiCancel != nil {
"getNetworkState": {Func: rpcGetNetworkState}, keyboardReportMultiCancel()
"getNetworkSettings": {Func: rpcGetNetworkSettings}, logger.Info().Msg("canceled keyboard report multi")
"setNetworkSettings": {Func: rpcSetNetworkSettings, Params: []string{"settings"}}, keyboardReportMultiCancel = nil
"renewDHCPLease": {Func: rpcRenewDHCPLease}, }
"keyboardReport": {Func: rpcKeyboardReport, Params: []string{"modifier", "keys"}}, }
"getKeyboardLedState": {Func: rpcGetKeyboardLedState},
"keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}}, func setKeyboardReportMultiCancel(cancel context.CancelFunc) {
"getKeyDownState": {Func: rpcGetKeysDownState}, keyboardReportMultiLock.Lock()
"absMouseReport": {Func: rpcAbsMouseReport, Params: []string{"x", "y", "buttons"}}, defer keyboardReportMultiLock.Unlock()
"relMouseReport": {Func: rpcRelMouseReport, Params: []string{"dx", "dy", "buttons"}},
"wheelReport": {Func: rpcWheelReport, Params: []string{"wheelY"}}, keyboardReportMultiCancel = cancel
"getVideoState": {Func: rpcGetVideoState}, }
"getUSBState": {Func: rpcGetUSBState},
"unmountImage": {Func: rpcUnmountImage}, func rpcKeyboardReportMultiWrapper(macro []map[string]any) (usbgadget.KeysDownState, error) {
"rpcMountBuiltInImage": {Func: rpcMountBuiltInImage, Params: []string{"filename"}}, cancelKeyboardReportMulti()
"setJigglerState": {Func: rpcSetJigglerState, Params: []string{"enabled"}},
"getJigglerState": {Func: rpcGetJigglerState}, ctx, cancel := context.WithCancel(context.Background())
"setJigglerConfig": {Func: rpcSetJigglerConfig, Params: []string{"jigglerConfig"}}, setKeyboardReportMultiCancel(cancel)
"getJigglerConfig": {Func: rpcGetJigglerConfig},
"getTimezones": {Func: rpcGetTimezones}, writeJSONRPCEvent("keyboardReportMultiState", true, currentSession)
"sendWOLMagicPacket": {Func: rpcSendWOLMagicPacket, Params: []string{"macAddress"}},
"getStreamQualityFactor": {Func: rpcGetStreamQualityFactor}, result, err := rpcKeyboardReportMulti(ctx, macro)
"setStreamQualityFactor": {Func: rpcSetStreamQualityFactor, Params: []string{"factor"}},
"getAutoUpdateState": {Func: rpcGetAutoUpdateState}, setKeyboardReportMultiCancel(nil)
"setAutoUpdateState": {Func: rpcSetAutoUpdateState, Params: []string{"enabled"}},
"getEDID": {Func: rpcGetEDID}, writeJSONRPCEvent("keyboardReportMultiState", false, currentSession)
"setEDID": {Func: rpcSetEDID, Params: []string{"edid"}},
"getDevChannelState": {Func: rpcGetDevChannelState}, return result, err
"setDevChannelState": {Func: rpcSetDevChannelState, Params: []string{"enabled"}}, }
"getUpdateStatus": {Func: rpcGetUpdateStatus},
"tryUpdate": {Func: rpcTryUpdate}, var (
"getDevModeState": {Func: rpcGetDevModeState}, keyboardReportMultiCancel context.CancelFunc
"setDevModeState": {Func: rpcSetDevModeState, Params: []string{"enabled"}}, keyboardReportMultiLock sync.Mutex
"getSSHKeyState": {Func: rpcGetSSHKeyState}, )
"setSSHKeyState": {Func: rpcSetSSHKeyState, Params: []string{"sshKey"}},
"getTLSState": {Func: rpcGetTLSState}, func rpcCancelKeyboardReportMulti() {
"setTLSState": {Func: rpcSetTLSState, Params: []string{"state"}}, cancelKeyboardReportMulti()
"setMassStorageMode": {Func: rpcSetMassStorageMode, Params: []string{"mode"}}, }
"getMassStorageMode": {Func: rpcGetMassStorageMode},
"isUpdatePending": {Func: rpcIsUpdatePending}, func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgadget.KeysDownState, error) {
"getUsbEmulationState": {Func: rpcGetUsbEmulationState}, var last usbgadget.KeysDownState
"setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}}, var err error
"getUsbConfig": {Func: rpcGetUsbConfig},
"setUsbConfig": {Func: rpcSetUsbConfig, Params: []string{"usbConfig"}}, logger.Debug().Interface("macro", macro).Msg("Executing keyboard report multi")
"checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}},
"getVirtualMediaState": {Func: rpcGetVirtualMediaState}, for i, step := range macro {
"getStorageSpace": {Func: rpcGetStorageSpace}, // Check for cancellation before each step
"mountWithHTTP": {Func: rpcMountWithHTTP, Params: []string{"url", "mode"}}, select {
"mountWithStorage": {Func: rpcMountWithStorage, Params: []string{"filename", "mode"}}, case <-ctx.Done():
"listStorageFiles": {Func: rpcListStorageFiles}, logger.Debug().Msg("Keyboard report multi context cancelled")
"deleteStorageFile": {Func: rpcDeleteStorageFile, Params: []string{"filename"}}, return last, ctx.Err()
"startStorageFileUpload": {Func: rpcStartStorageFileUpload, Params: []string{"filename", "size"}}, default:
"getWakeOnLanDevices": {Func: rpcGetWakeOnLanDevices}, }
"setWakeOnLanDevices": {Func: rpcSetWakeOnLanDevices, Params: []string{"params"}},
"resetConfig": {Func: rpcResetConfig}, var modifier byte
"setDisplayRotation": {Func: rpcSetDisplayRotation, Params: []string{"params"}}, if m, ok := step["modifier"].(float64); ok {
"getDisplayRotation": {Func: rpcGetDisplayRotation}, modifier = byte(int(m))
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}}, } else if mi, ok := step["modifier"].(int); ok {
"getBacklightSettings": {Func: rpcGetBacklightSettings}, modifier = byte(mi)
"getDCPowerState": {Func: rpcGetDCPowerState}, } else if mb, ok := step["modifier"].(uint8); ok {
"setDCPowerState": {Func: rpcSetDCPowerState, Params: []string{"enabled"}}, modifier = mb
"setDCRestoreState": {Func: rpcSetDCRestoreState, Params: []string{"state"}}, }
"getActiveExtension": {Func: rpcGetActiveExtension},
"setActiveExtension": {Func: rpcSetActiveExtension, Params: []string{"extensionId"}}, var keys []byte
"getATXState": {Func: rpcGetATXState}, if arr, ok := step["keys"].([]any); ok {
"setATXPowerAction": {Func: rpcSetATXPowerAction, Params: []string{"action"}}, keys = make([]byte, 0, len(arr))
"getSerialSettings": {Func: rpcGetSerialSettings}, for _, v := range arr {
"setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}}, if f, ok := v.(float64); ok {
"getUsbDevices": {Func: rpcGetUsbDevices}, keys = append(keys, byte(int(f)))
"setUsbDevices": {Func: rpcSetUsbDevices, Params: []string{"devices"}}, } else if i, ok := v.(int); ok {
"setUsbDeviceState": {Func: rpcSetUsbDeviceState, Params: []string{"device", "enabled"}}, keys = append(keys, byte(i))
"setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"apiUrl", "appUrl"}}, } else if b, ok := v.(uint8); ok {
"getKeyboardLayout": {Func: rpcGetKeyboardLayout}, keys = append(keys, b)
"setKeyboardLayout": {Func: rpcSetKeyboardLayout, Params: []string{"layout"}}, }
"getKeyboardMacros": {Func: getKeyboardMacros}, }
"setKeyboardMacros": {Func: setKeyboardMacros, Params: []string{"params"}}, } else if bs, ok := step["keys"].([]byte); ok {
"getLocalLoopbackOnly": {Func: rpcGetLocalLoopbackOnly}, keys = bs
"setLocalLoopbackOnly": {Func: rpcSetLocalLoopbackOnly, Params: []string{"enabled"}}, }
// Use context-aware sleep that can be cancelled
select {
case <-time.After(100 * time.Millisecond):
// Sleep completed normally
case <-ctx.Done():
logger.Debug().Int("step", i).Msg("Keyboard report multi cancelled during sleep")
return last, ctx.Err()
}
last, err = rpcKeyboardReport(modifier, keys)
if err != nil {
logger.Warn().Err(err).Msg("failed to execute keyboard report multi")
return last, err
}
}
return last, nil
}
var rpcHandlers = map[string]RPCHandler{
"ping": {Func: rpcPing},
"reboot": {Func: rpcReboot, Params: []string{"force"}},
"getDeviceID": {Func: rpcGetDeviceID},
"deregisterDevice": {Func: rpcDeregisterDevice},
"getCloudState": {Func: rpcGetCloudState},
"getNetworkState": {Func: rpcGetNetworkState},
"getNetworkSettings": {Func: rpcGetNetworkSettings},
"setNetworkSettings": {Func: rpcSetNetworkSettings, Params: []string{"settings"}},
"renewDHCPLease": {Func: rpcRenewDHCPLease},
"keyboardReport": {Func: rpcKeyboardReport, Params: []string{"modifier", "keys"}},
"keyboardReportMulti": {Func: rpcKeyboardReportMultiWrapper, Params: []string{"macro"}},
"cancelKeyboardReportMulti": {Func: rpcCancelKeyboardReportMulti},
"getKeyboardLedState": {Func: rpcGetKeyboardLedState},
"keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}},
"getKeyDownState": {Func: rpcGetKeysDownState},
"absMouseReport": {Func: rpcAbsMouseReport, Params: []string{"x", "y", "buttons"}},
"relMouseReport": {Func: rpcRelMouseReport, Params: []string{"dx", "dy", "buttons"}},
"wheelReport": {Func: rpcWheelReport, Params: []string{"wheelY"}},
"getVideoState": {Func: rpcGetVideoState},
"getUSBState": {Func: rpcGetUSBState},
"unmountImage": {Func: rpcUnmountImage},
"rpcMountBuiltInImage": {Func: rpcMountBuiltInImage, Params: []string{"filename"}},
"setJigglerState": {Func: rpcSetJigglerState, Params: []string{"enabled"}},
"getJigglerState": {Func: rpcGetJigglerState},
"setJigglerConfig": {Func: rpcSetJigglerConfig, Params: []string{"jigglerConfig"}},
"getJigglerConfig": {Func: rpcGetJigglerConfig},
"getTimezones": {Func: rpcGetTimezones},
"sendWOLMagicPacket": {Func: rpcSendWOLMagicPacket, Params: []string{"macAddress"}},
"getStreamQualityFactor": {Func: rpcGetStreamQualityFactor},
"setStreamQualityFactor": {Func: rpcSetStreamQualityFactor, Params: []string{"factor"}},
"getAutoUpdateState": {Func: rpcGetAutoUpdateState},
"setAutoUpdateState": {Func: rpcSetAutoUpdateState, Params: []string{"enabled"}},
"getEDID": {Func: rpcGetEDID},
"setEDID": {Func: rpcSetEDID, Params: []string{"edid"}},
"getDevChannelState": {Func: rpcGetDevChannelState},
"setDevChannelState": {Func: rpcSetDevChannelState, Params: []string{"enabled"}},
"getUpdateStatus": {Func: rpcGetUpdateStatus},
"tryUpdate": {Func: rpcTryUpdate},
"getDevModeState": {Func: rpcGetDevModeState},
"setDevModeState": {Func: rpcSetDevModeState, Params: []string{"enabled"}},
"getSSHKeyState": {Func: rpcGetSSHKeyState},
"setSSHKeyState": {Func: rpcSetSSHKeyState, Params: []string{"sshKey"}},
"getTLSState": {Func: rpcGetTLSState},
"setTLSState": {Func: rpcSetTLSState, Params: []string{"state"}},
"setMassStorageMode": {Func: rpcSetMassStorageMode, Params: []string{"mode"}},
"getMassStorageMode": {Func: rpcGetMassStorageMode},
"isUpdatePending": {Func: rpcIsUpdatePending},
"getUsbEmulationState": {Func: rpcGetUsbEmulationState},
"setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}},
"getUsbConfig": {Func: rpcGetUsbConfig},
"setUsbConfig": {Func: rpcSetUsbConfig, Params: []string{"usbConfig"}},
"checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}},
"getVirtualMediaState": {Func: rpcGetVirtualMediaState},
"getStorageSpace": {Func: rpcGetStorageSpace},
"mountWithHTTP": {Func: rpcMountWithHTTP, Params: []string{"url", "mode"}},
"mountWithStorage": {Func: rpcMountWithStorage, Params: []string{"filename", "mode"}},
"listStorageFiles": {Func: rpcListStorageFiles},
"deleteStorageFile": {Func: rpcDeleteStorageFile, Params: []string{"filename"}},
"startStorageFileUpload": {Func: rpcStartStorageFileUpload, Params: []string{"filename", "size"}},
"getWakeOnLanDevices": {Func: rpcGetWakeOnLanDevices},
"setWakeOnLanDevices": {Func: rpcSetWakeOnLanDevices, Params: []string{"params"}},
"resetConfig": {Func: rpcResetConfig},
"setDisplayRotation": {Func: rpcSetDisplayRotation, Params: []string{"params"}},
"getDisplayRotation": {Func: rpcGetDisplayRotation},
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}},
"getBacklightSettings": {Func: rpcGetBacklightSettings},
"getDCPowerState": {Func: rpcGetDCPowerState},
"setDCPowerState": {Func: rpcSetDCPowerState, Params: []string{"enabled"}},
"setDCRestoreState": {Func: rpcSetDCRestoreState, Params: []string{"state"}},
"getActiveExtension": {Func: rpcGetActiveExtension},
"setActiveExtension": {Func: rpcSetActiveExtension, Params: []string{"extensionId"}},
"getATXState": {Func: rpcGetATXState},
"setATXPowerAction": {Func: rpcSetATXPowerAction, Params: []string{"action"}},
"getSerialSettings": {Func: rpcGetSerialSettings},
"setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}},
"getUsbDevices": {Func: rpcGetUsbDevices},
"setUsbDevices": {Func: rpcSetUsbDevices, Params: []string{"devices"}},
"setUsbDeviceState": {Func: rpcSetUsbDeviceState, Params: []string{"device", "enabled"}},
"setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"apiUrl", "appUrl"}},
"getKeyboardLayout": {Func: rpcGetKeyboardLayout},
"setKeyboardLayout": {Func: rpcSetKeyboardLayout, Params: []string{"layout"}},
"getKeyboardMacros": {Func: getKeyboardMacros},
"setKeyboardMacros": {Func: setKeyboardMacros, Params: []string{"params"}},
"getLocalLoopbackOnly": {Func: rpcGetLocalLoopbackOnly},
"setLocalLoopbackOnly": {Func: rpcSetLocalLoopbackOnly, Params: []string{"enabled"}},
} }

View File

@ -27,6 +27,7 @@ export default function InfoBar() {
const { rpcDataChannel } = useRTCStore(); const { rpcDataChannel } = useRTCStore();
const { debugMode, mouseMode, showPressedKeys } = useSettingsStore(); const { debugMode, mouseMode, showPressedKeys } = useSettingsStore();
const { isPasteModeEnabled } = useHidStore();
useEffect(() => { useEffect(() => {
if (!rpcDataChannel) return; if (!rpcDataChannel) return;
@ -108,7 +109,12 @@ export default function InfoBar() {
<span className="text-xs">{rpcHidStatus}</span> <span className="text-xs">{rpcHidStatus}</span>
</div> </div>
)} )}
{isPasteModeEnabled && (
<div className="flex w-[156px] items-center gap-x-1">
<span className="text-xs font-semibold">Paste Mode:</span>
<span className="text-xs">Enabled</span>
</div>
)}
{showPressedKeys && ( {showPressedKeys && (
<div className="flex items-center gap-x-1"> <div className="flex items-center gap-x-1">
<span className="text-xs font-semibold">Keys:</span> <span className="text-xs font-semibold">Keys:</span>

View File

@ -8,35 +8,24 @@ import { GridCard } from "@components/Card";
import { TextAreaWithLabel } from "@components/TextArea"; import { TextAreaWithLabel } from "@components/TextArea";
import { SettingsPageHeader } from "@components/SettingsPageheader"; import { SettingsPageHeader } from "@components/SettingsPageheader";
import { JsonRpcResponse, useJsonRpc } from "@/hooks/useJsonRpc"; import { JsonRpcResponse, useJsonRpc } from "@/hooks/useJsonRpc";
import { useHidStore, useRTCStore, useUiStore, useSettingsStore } from "@/hooks/stores"; import { useHidStore, useSettingsStore, useUiStore } from "@/hooks/stores";
import { keys, modifiers } from "@/keyboardMappings"; import useKeyboard from "@/hooks/useKeyboard";
import { KeyStroke } from "@/keyboardLayouts";
import useKeyboardLayout from "@/hooks/useKeyboardLayout"; import useKeyboardLayout from "@/hooks/useKeyboardLayout";
import notifications from "@/notifications"; import notifications from "@/notifications";
const hidKeyboardPayload = (modifier: number, keys: number[]) => {
return { modifier, keys };
};
const modifierCode = (shift?: boolean, altRight?: boolean) => {
return (shift ? modifiers.ShiftLeft : 0)
| (altRight ? modifiers.AltRight : 0)
}
const noModifier = 0
export default function PasteModal() { export default function PasteModal() {
const TextAreaRef = useRef<HTMLTextAreaElement>(null); const TextAreaRef = useRef<HTMLTextAreaElement>(null);
const { setPasteModeEnabled } = useHidStore(); const { isPasteModeEnabled } = useHidStore();
const { setDisableVideoFocusTrap } = useUiStore(); const { setDisableVideoFocusTrap } = useUiStore();
const { send } = useJsonRpc(); const { send } = useJsonRpc();
const { rpcDataChannel } = useRTCStore(); const { executeMacro, cancelExecuteMacro } = useKeyboard();
const [invalidChars, setInvalidChars] = useState<string[]>([]); const [invalidChars, setInvalidChars] = useState<string[]>([]);
const close = useClose(); const close = useClose();
const { setKeyboardLayout } = useSettingsStore(); const { setKeyboardLayout } = useSettingsStore();
const { selectedKeyboard } = useKeyboardLayout(); const { selectedKeyboard } = useKeyboardLayout();
useEffect(() => { useEffect(() => {
send("getKeyboardLayout", {}, (resp: JsonRpcResponse) => { send("getKeyboardLayout", {}, (resp: JsonRpcResponse) => {
@ -46,21 +35,26 @@ export default function PasteModal() {
}, [send, setKeyboardLayout]); }, [send, setKeyboardLayout]);
const onCancelPasteMode = useCallback(() => { const onCancelPasteMode = useCallback(() => {
setPasteModeEnabled(false); cancelExecuteMacro();
setDisableVideoFocusTrap(false); setDisableVideoFocusTrap(false);
setInvalidChars([]); setInvalidChars([]);
}, [setDisableVideoFocusTrap, setPasteModeEnabled]); }, [setDisableVideoFocusTrap, cancelExecuteMacro]);
const onConfirmPaste = useCallback(async () => { const onConfirmPaste = useCallback(async () => {
setPasteModeEnabled(false); // setPasteModeEnabled(false);
setDisableVideoFocusTrap(false); // setDisableVideoFocusTrap(false);
if (rpcDataChannel?.readyState !== "open" || !TextAreaRef.current) return; if (!TextAreaRef.current || !selectedKeyboard) return;
if (!selectedKeyboard) return;
const text = TextAreaRef.current.value; const text = TextAreaRef.current.value;
try { try {
const macroSteps: {
keys: string[] | null;
modifiers: string[] | null;
delay: number;
}[] = [];
for (const char of text) { for (const char of text) {
const keyprops = selectedKeyboard.chars[char]; const keyprops = selectedKeyboard.chars[char];
if (!keyprops) continue; if (!keyprops) continue;
@ -70,39 +64,41 @@ export default function PasteModal() {
// if this is an accented character, we need to send that accent FIRST // if this is an accented character, we need to send that accent FIRST
if (accentKey) { if (accentKey) {
await sendKeystroke({modifier: modifierCode(accentKey.shift, accentKey.altRight), keys: [ keys[accentKey.key] ] }) const accentModifiers: string[] = [];
if (accentKey.shift) accentModifiers.push("ShiftLeft");
if (accentKey.altRight) accentModifiers.push("AltRight");
macroSteps.push({
keys: [String(accentKey.key)],
modifiers: accentModifiers.length > 0 ? accentModifiers : null,
delay: 100,
});
} }
// now send the actual key // now send the actual key
await sendKeystroke({ modifier: modifierCode(shift, altRight), keys: [ keys[key] ]}); const modifiers: string[] = [];
if (shift) modifiers.push("ShiftLeft");
if (altRight) modifiers.push("AltRight");
macroSteps.push({
keys: [String(key)],
modifiers: modifiers.length > 0 ? modifiers : null,
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) { if (deadKey) macroSteps.push({ keys: ["Space"], modifiers: null, delay: 100 });
await sendKeystroke({ modifier: noModifier, keys: [ keys["Space"] ] }); }
}
// now send a message with no keys down to "release" the keys if (macroSteps.length > 0) {
await sendKeystroke({ modifier: 0, keys: [] }); await executeMacro(macroSteps);
} }
} catch (error) { } catch (error) {
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]);
async function sendKeystroke(stroke: KeyStroke) {
await new Promise<void>((resolve, reject) => {
send(
"keyboardReport",
hidKeyboardPayload(stroke.modifier, stroke.keys),
params => {
if ("error" in params) return reject(params.error);
resolve();
}
);
});
}
}, [selectedKeyboard, rpcDataChannel?.readyState, send, setDisableVideoFocusTrap, setPasteModeEnabled]);
useEffect(() => { useEffect(() => {
if (TextAreaRef.current) { if (TextAreaRef.current) {
@ -122,14 +118,18 @@ export default function PasteModal() {
/> />
<div <div
className="animate-fadeIn opacity-0 space-y-2" className="animate-fadeIn space-y-2 opacity-0"
style={{ style={{
animationDuration: "0.7s", animationDuration: "0.7s",
animationDelay: "0.1s", animationDelay: "0.1s",
}} }}
> >
<div> <div>
<div className="w-full" onKeyUp={e => e.stopPropagation()} onKeyDown={e => e.stopPropagation()}> <div
className="w-full"
onKeyUp={e => e.stopPropagation()}
onKeyDown={e => e.stopPropagation()}
>
<TextAreaWithLabel <TextAreaWithLabel
ref={TextAreaRef} ref={TextAreaRef}
label="Paste from host" label="Paste from host"
@ -173,7 +173,8 @@ export default function PasteModal() {
</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}-{selectedKeyboard.name} Sending text using keyboard layout: {selectedKeyboard.isoCode}-
{selectedKeyboard.name}
</p> </p>
</div> </div>
</div> </div>
@ -181,7 +182,7 @@ export default function PasteModal() {
</div> </div>
</div> </div>
<div <div
className="flex animate-fadeIn opacity-0 items-center justify-end gap-x-2" className="flex animate-fadeIn items-center justify-end gap-x-2 opacity-0"
style={{ style={{
animationDuration: "0.7s", animationDuration: "0.7s",
animationDelay: "0.2s", animationDelay: "0.2s",
@ -200,6 +201,7 @@ export default function PasteModal() {
size="SM" size="SM"
theme="primary" theme="primary"
text="Confirm Paste" text="Confirm Paste"
disabled={isPasteModeEnabled}
onClick={onConfirmPaste} onClick={onConfirmPaste}
LeadingIcon={LuCornerDownLeft} LeadingIcon={LuCornerDownLeft}
/> />

View File

@ -1,6 +1,12 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { hidErrorRollOver, hidKeyBufferSize, KeysDownState, useHidStore, useRTCStore } from "@/hooks/stores"; import {
hidErrorRollOver,
hidKeyBufferSize,
KeysDownState,
useHidStore,
useRTCStore,
} 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, KeysDownStateMessage } from "@/hooks/hidRpc"; import { KeyboardLedStateMessage, KeysDownStateMessage } from "@/hooks/hidRpc";
@ -27,7 +33,7 @@ export default function useKeyboard() {
reportKeyboardEvent: sendKeyboardEventHidRpc, reportKeyboardEvent: sendKeyboardEventHidRpc,
reportKeypressEvent: sendKeypressEventHidRpc, reportKeypressEvent: sendKeypressEventHidRpc,
rpcHidReady, rpcHidReady,
} = useHidRpc((message) => { } = useHidRpc(message => {
switch (message.constructor) { switch (message.constructor) {
case KeysDownStateMessage: case KeysDownStateMessage:
setKeysDownState((message as KeysDownStateMessage).keysDownState); setKeysDownState((message as KeysDownStateMessage).keysDownState);
@ -48,7 +54,9 @@ export default function useKeyboard() {
async (state: KeysDownState) => { async (state: KeysDownState) => {
if (rpcDataChannel?.readyState !== "open" && !rpcHidReady) return; if (rpcDataChannel?.readyState !== "open" && !rpcHidReady) return;
console.debug(`Send keyboardReport keys: ${state.keys}, modifier: ${state.modifier}`); console.debug(
`Send keyboardReport keys: ${state.keys}, modifier: ${state.modifier}`,
);
if (rpcHidReady) { if (rpcHidReady) {
console.debug("Sending keyboard report via HidRPC"); console.debug("Sending keyboard report via HidRPC");
@ -56,31 +64,29 @@ export default function useKeyboard() {
return; return;
} }
send("keyboardReport", { keys: state.keys, modifier: state.modifier }, (resp: JsonRpcResponse) => { send(
if ("error" in resp) { "keyboardReport",
console.error(`Failed to send keyboard report ${state}`, resp.error); { keys: state.keys, modifier: state.modifier },
} (resp: JsonRpcResponse) => {
}); if ("error" in resp) {
console.error(`Failed to send keyboard report ${state}`, resp.error);
}
},
);
}, },
[ [rpcDataChannel?.readyState, rpcHidReady, send, sendKeyboardEventHidRpc],
rpcDataChannel?.readyState,
rpcHidReady,
send,
sendKeyboardEventHidRpc,
],
); );
// resetKeyboardState is used to reset the keyboard state to no keys pressed and no modifiers. // resetKeyboardState is used to reset the keyboard state to no keys pressed and no modifiers.
// This is useful for macros and when the browser loses focus to ensure that the keyboard state // This is useful for macros and when the browser loses focus to ensure that the keyboard state
// is clean. // is clean.
const resetKeyboardState = useCallback( const resetKeyboardState = useCallback(async () => {
async () => { // Reset the keys buffer to zeros and the modifier state to zero
// Reset the keys buffer to zeros and the modifier state to zero keysDownState.keys.length = hidKeyBufferSize;
keysDownState.keys.length = hidKeyBufferSize; keysDownState.keys.fill(0);
keysDownState.keys.fill(0); keysDownState.modifier = 0;
keysDownState.modifier = 0; sendKeyboardEvent(keysDownState);
sendKeyboardEvent(keysDownState); }, [keysDownState, sendKeyboardEvent]);
}, [keysDownState, sendKeyboardEvent]);
// executeMacro is used to execute a macro consisting of multiple steps. // executeMacro is used to execute a macro consisting of multiple steps.
// Each step can have multiple keys, multiple modifiers and a delay. // Each step can have multiple keys, multiple modifiers and a delay.
@ -88,29 +94,42 @@ export default function useKeyboard() {
// After the delay, the keys and modifiers are released and the next step is executed. // After the delay, the keys and modifiers are released and the next step is executed.
// If a step has no keys or modifiers, it is treated as a delay-only step. // If a step has no keys or modifiers, it is treated as a delay-only step.
// A small pause is added between steps to ensure that the device can process the events. // A small pause is added between steps to ensure that the device can process the events.
const executeMacro = async (steps: { keys: string[] | null; modifiers: string[] | null; delay: number }[]) => { const executeMacro = async (
for (const [index, step] of steps.entries()) { steps: { keys: string[] | null; modifiers: string[] | null; delay: number }[],
) => {
const macro: KeysDownState[] = [];
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);
const modifierMask: number = (step.modifiers || []).map(mod => modifiers[mod]).reduce((acc, val) => acc + val, 0); const modifierMask: number = (step.modifiers || [])
.map(mod => modifiers[mod])
.reduce((acc, val) => acc + val, 0);
// 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) {
sendKeyboardEvent({ keys: keyValues, modifier: modifierMask }); macro.push({ keys: keyValues, modifier: modifierMask });
await new Promise(resolve => setTimeout(resolve, step.delay || 50)); keysDownState.keys.length = hidKeyBufferSize;
keysDownState.keys.fill(0);
resetKeyboardState(); keysDownState.modifier = 0;
} else { macro.push(keysDownState);
// This is a delay-only step, just wait for the delay amount
await new Promise(resolve => setTimeout(resolve, step.delay || 50));
}
// Add a small pause between steps if not the last step
if (index < steps.length - 1) {
await new Promise(resolve => setTimeout(resolve, 10));
} }
} }
// KeyboardReportMessage
send("keyboardReportMulti", { macro }, (resp: JsonRpcResponse) => {
if ("error" in resp) {
console.error(`Failed to send keyboard report ${macro}`, resp.error);
}
});
}; };
const cancelExecuteMacro = useCallback(async () => {
send("cancelKeyboardReportMulti", {}, (resp: JsonRpcResponse) => {
if ("error" in resp) {
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.
// It checks if the keyPressReport API is available and sends the key press event. // It checks if the keyPressReport API is available and sends the key press event.
@ -132,7 +151,11 @@ export default function useKeyboard() {
sendKeypressEventHidRpc(key, press); sendKeypressEventHidRpc(key, press);
} else { } else {
// if the keyPress api is not available, we need to handle the key locally // if the keyPress api is not available, we need to handle the key locally
const downState = simulateDeviceSideKeyHandlingForLegacyDevices(keysDownState, key, press); const downState = simulateDeviceSideKeyHandlingForLegacyDevices(
keysDownState,
key,
press,
);
sendKeyboardEvent(downState); // then we send the full state sendKeyboardEvent(downState); // then we send the full state
// if we just sent ErrorRollOver, reset to empty state // if we just sent ErrorRollOver, reset to empty state
@ -152,7 +175,11 @@ export default function useKeyboard() {
); );
// IMPORTANT: See the keyPressReportApiAvailable comment above for the reason this exists // IMPORTANT: See the keyPressReportApiAvailable comment above for the reason this exists
function simulateDeviceSideKeyHandlingForLegacyDevices(state: KeysDownState, key: number, press: boolean): KeysDownState { function simulateDeviceSideKeyHandlingForLegacyDevices(
state: KeysDownState,
key: number,
press: boolean,
): KeysDownState {
// IMPORTANT: This code parallels the logic in the kernel's hid-gadget driver // IMPORTANT: This code parallels the logic in the kernel's hid-gadget driver
// for handling key presses and releases. It ensures that the USB gadget // for handling key presses and releases. It ensures that the USB gadget
// behaves similarly to a real USB HID keyboard. This logic is paralleled // behaves similarly to a real USB HID keyboard. This logic is paralleled
@ -181,7 +208,7 @@ export default function useKeyboard() {
// 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)
if (keys[i] === key || keys[i] === 0) { if (keys[i] === key || keys[i] === 0) {
if (press) { if (press) {
keys[i] = key // overwrites the zero byte or the same key if already pressed keys[i] = key; // overwrites the zero byte or the same key if already pressed
} else { } else {
// 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) {
@ -197,18 +224,20 @@ export default function useKeyboard() {
// If we reach here it means we didn't find an empty slot or the key in the buffer // If we reach here it means we didn't find an empty slot or the key in the buffer
if (overrun) { if (overrun) {
if (press) { if (press) {
console.warn(`keyboard buffer overflow current keys ${keys}, key: ${key} not added`); console.warn(
`keyboard buffer overflow current keys ${keys}, key: ${key} not added`,
);
// Fill all key slots with ErrorRollOver (0x01) to indicate overflow // Fill all key slots with ErrorRollOver (0x01) to indicate overflow
keys.length = hidKeyBufferSize; keys.length = hidKeyBufferSize;
keys.fill(hidErrorRollOver); keys.fill(hidErrorRollOver);
} else { } else {
// If we are releasing a key, and we didn't find it in a slot, who cares? // If we are releasing a key, and we didn't find it in a slot, who cares?
console.debug(`key ${key} not found in buffer, nothing to release`) console.debug(`key ${key} not found in buffer, nothing to release`);
} }
} }
} }
return { modifier: modifiers, keys }; return { modifier: modifiers, keys };
} }
return { handleKeyPress, resetKeyboardState, executeMacro }; return { handleKeyPress, resetKeyboardState, executeMacro, cancelExecuteMacro };
} }

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);

4
web.go
View File

@ -198,6 +198,10 @@ func handleWebRTCSession(c *gin.Context) {
_ = peerConn.Close() _ = peerConn.Close()
}() }()
} }
// Cancel any ongoing keyboard report multi when session changes
cancelKeyboardReportMulti()
currentSession = session currentSession = session
c.JSON(http.StatusOK, gin.H{"sd": sd}) c.JSON(http.StatusOK, gin.H{"sd": sd})
} }

View File

@ -266,6 +266,8 @@ func newSession(config SessionConfig) (*Session, error) {
if connectionState == webrtc.ICEConnectionStateClosed { if connectionState == webrtc.ICEConnectionStateClosed {
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
cancelKeyboardReportMulti()
currentSession = nil currentSession = nil
} }
// Stop RPC processor // Stop RPC processor