feat: cancel paste mode

This commit is contained in:
Siyuan Miao 2025-09-10 00:13:10 +02:00
parent 4b0818502c
commit f58e5476bf
5 changed files with 129 additions and 98 deletions

View File

@ -1062,21 +1062,26 @@ func cancelKeyboardReportMulti() {
} }
} }
func rpcKeyboardReportMultiWrapper(macro []map[string]any) (usbgadget.KeysDownState, error) { func setKeyboardReportMultiCancel(cancel context.CancelFunc) {
keyboardReportMultiLock.Lock() keyboardReportMultiLock.Lock()
defer keyboardReportMultiLock.Unlock() defer keyboardReportMultiLock.Unlock()
if keyboardReportMultiCancel != nil { keyboardReportMultiCancel = cancel
keyboardReportMultiCancel() }
logger.Info().Msg("canceled previous keyboard report multi")
} func rpcKeyboardReportMultiWrapper(macro []map[string]any) (usbgadget.KeysDownState, error) {
cancelKeyboardReportMulti()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
keyboardReportMultiCancel = cancel setKeyboardReportMultiCancel(cancel)
writeJSONRPCEvent("keyboardReportMultiState", true, currentSession)
result, err := rpcKeyboardReportMulti(ctx, macro) result, err := rpcKeyboardReportMulti(ctx, macro)
keyboardReportMultiCancel = nil setKeyboardReportMultiCancel(nil)
writeJSONRPCEvent("keyboardReportMultiState", false, currentSession)
return result, err return result, err
} }
@ -1086,6 +1091,10 @@ var (
keyboardReportMultiLock sync.Mutex keyboardReportMultiLock sync.Mutex
) )
func rpcCancelKeyboardReportMulti() {
cancelKeyboardReportMulti()
}
func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgadget.KeysDownState, error) { func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgadget.KeysDownState, error) {
var last usbgadget.KeysDownState var last usbgadget.KeysDownState
var err error var err error
@ -1146,88 +1155,89 @@ func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgad
} }
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"}}, "keyboardReportMulti": {Func: rpcKeyboardReportMultiWrapper, Params: []string{"macro"}},
"getKeyboardLedState": {Func: rpcGetKeyboardLedState}, "cancelKeyboardReportMulti": {Func: rpcCancelKeyboardReportMulti},
"keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}}, "getKeyboardLedState": {Func: rpcGetKeyboardLedState},
"getKeyDownState": {Func: rpcGetKeysDownState}, "keypressReport": {Func: rpcKeypressReport, Params: []string{"key", "press"}},
"absMouseReport": {Func: rpcAbsMouseReport, Params: []string{"x", "y", "buttons"}}, "getKeyDownState": {Func: rpcGetKeysDownState},
"relMouseReport": {Func: rpcRelMouseReport, Params: []string{"dx", "dy", "buttons"}}, "absMouseReport": {Func: rpcAbsMouseReport, Params: []string{"x", "y", "buttons"}},
"wheelReport": {Func: rpcWheelReport, Params: []string{"wheelY"}}, "relMouseReport": {Func: rpcRelMouseReport, Params: []string{"dx", "dy", "buttons"}},
"getVideoState": {Func: rpcGetVideoState}, "wheelReport": {Func: rpcWheelReport, Params: []string{"wheelY"}},
"getUSBState": {Func: rpcGetUSBState}, "getVideoState": {Func: rpcGetVideoState},
"unmountImage": {Func: rpcUnmountImage}, "getUSBState": {Func: rpcGetUSBState},
"rpcMountBuiltInImage": {Func: rpcMountBuiltInImage, Params: []string{"filename"}}, "unmountImage": {Func: rpcUnmountImage},
"setJigglerState": {Func: rpcSetJigglerState, Params: []string{"enabled"}}, "rpcMountBuiltInImage": {Func: rpcMountBuiltInImage, Params: []string{"filename"}},
"getJigglerState": {Func: rpcGetJigglerState}, "setJigglerState": {Func: rpcSetJigglerState, Params: []string{"enabled"}},
"setJigglerConfig": {Func: rpcSetJigglerConfig, Params: []string{"jigglerConfig"}}, "getJigglerState": {Func: rpcGetJigglerState},
"getJigglerConfig": {Func: rpcGetJigglerConfig}, "setJigglerConfig": {Func: rpcSetJigglerConfig, Params: []string{"jigglerConfig"}},
"getTimezones": {Func: rpcGetTimezones}, "getJigglerConfig": {Func: rpcGetJigglerConfig},
"sendWOLMagicPacket": {Func: rpcSendWOLMagicPacket, Params: []string{"macAddress"}}, "getTimezones": {Func: rpcGetTimezones},
"getStreamQualityFactor": {Func: rpcGetStreamQualityFactor}, "sendWOLMagicPacket": {Func: rpcSendWOLMagicPacket, Params: []string{"macAddress"}},
"setStreamQualityFactor": {Func: rpcSetStreamQualityFactor, Params: []string{"factor"}}, "getStreamQualityFactor": {Func: rpcGetStreamQualityFactor},
"getAutoUpdateState": {Func: rpcGetAutoUpdateState}, "setStreamQualityFactor": {Func: rpcSetStreamQualityFactor, Params: []string{"factor"}},
"setAutoUpdateState": {Func: rpcSetAutoUpdateState, Params: []string{"enabled"}}, "getAutoUpdateState": {Func: rpcGetAutoUpdateState},
"getEDID": {Func: rpcGetEDID}, "setAutoUpdateState": {Func: rpcSetAutoUpdateState, Params: []string{"enabled"}},
"setEDID": {Func: rpcSetEDID, Params: []string{"edid"}}, "getEDID": {Func: rpcGetEDID},
"getDevChannelState": {Func: rpcGetDevChannelState}, "setEDID": {Func: rpcSetEDID, Params: []string{"edid"}},
"setDevChannelState": {Func: rpcSetDevChannelState, Params: []string{"enabled"}}, "getDevChannelState": {Func: rpcGetDevChannelState},
"getUpdateStatus": {Func: rpcGetUpdateStatus}, "setDevChannelState": {Func: rpcSetDevChannelState, Params: []string{"enabled"}},
"tryUpdate": {Func: rpcTryUpdate}, "getUpdateStatus": {Func: rpcGetUpdateStatus},
"getDevModeState": {Func: rpcGetDevModeState}, "tryUpdate": {Func: rpcTryUpdate},
"setDevModeState": {Func: rpcSetDevModeState, Params: []string{"enabled"}}, "getDevModeState": {Func: rpcGetDevModeState},
"getSSHKeyState": {Func: rpcGetSSHKeyState}, "setDevModeState": {Func: rpcSetDevModeState, Params: []string{"enabled"}},
"setSSHKeyState": {Func: rpcSetSSHKeyState, Params: []string{"sshKey"}}, "getSSHKeyState": {Func: rpcGetSSHKeyState},
"getTLSState": {Func: rpcGetTLSState}, "setSSHKeyState": {Func: rpcSetSSHKeyState, Params: []string{"sshKey"}},
"setTLSState": {Func: rpcSetTLSState, Params: []string{"state"}}, "getTLSState": {Func: rpcGetTLSState},
"setMassStorageMode": {Func: rpcSetMassStorageMode, Params: []string{"mode"}}, "setTLSState": {Func: rpcSetTLSState, Params: []string{"state"}},
"getMassStorageMode": {Func: rpcGetMassStorageMode}, "setMassStorageMode": {Func: rpcSetMassStorageMode, Params: []string{"mode"}},
"isUpdatePending": {Func: rpcIsUpdatePending}, "getMassStorageMode": {Func: rpcGetMassStorageMode},
"getUsbEmulationState": {Func: rpcGetUsbEmulationState}, "isUpdatePending": {Func: rpcIsUpdatePending},
"setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}}, "getUsbEmulationState": {Func: rpcGetUsbEmulationState},
"getUsbConfig": {Func: rpcGetUsbConfig}, "setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}},
"setUsbConfig": {Func: rpcSetUsbConfig, Params: []string{"usbConfig"}}, "getUsbConfig": {Func: rpcGetUsbConfig},
"checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}}, "setUsbConfig": {Func: rpcSetUsbConfig, Params: []string{"usbConfig"}},
"getVirtualMediaState": {Func: rpcGetVirtualMediaState}, "checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}},
"getStorageSpace": {Func: rpcGetStorageSpace}, "getVirtualMediaState": {Func: rpcGetVirtualMediaState},
"mountWithHTTP": {Func: rpcMountWithHTTP, Params: []string{"url", "mode"}}, "getStorageSpace": {Func: rpcGetStorageSpace},
"mountWithStorage": {Func: rpcMountWithStorage, Params: []string{"filename", "mode"}}, "mountWithHTTP": {Func: rpcMountWithHTTP, Params: []string{"url", "mode"}},
"listStorageFiles": {Func: rpcListStorageFiles}, "mountWithStorage": {Func: rpcMountWithStorage, Params: []string{"filename", "mode"}},
"deleteStorageFile": {Func: rpcDeleteStorageFile, Params: []string{"filename"}}, "listStorageFiles": {Func: rpcListStorageFiles},
"startStorageFileUpload": {Func: rpcStartStorageFileUpload, Params: []string{"filename", "size"}}, "deleteStorageFile": {Func: rpcDeleteStorageFile, Params: []string{"filename"}},
"getWakeOnLanDevices": {Func: rpcGetWakeOnLanDevices}, "startStorageFileUpload": {Func: rpcStartStorageFileUpload, Params: []string{"filename", "size"}},
"setWakeOnLanDevices": {Func: rpcSetWakeOnLanDevices, Params: []string{"params"}}, "getWakeOnLanDevices": {Func: rpcGetWakeOnLanDevices},
"resetConfig": {Func: rpcResetConfig}, "setWakeOnLanDevices": {Func: rpcSetWakeOnLanDevices, Params: []string{"params"}},
"setDisplayRotation": {Func: rpcSetDisplayRotation, Params: []string{"params"}}, "resetConfig": {Func: rpcResetConfig},
"getDisplayRotation": {Func: rpcGetDisplayRotation}, "setDisplayRotation": {Func: rpcSetDisplayRotation, Params: []string{"params"}},
"setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}}, "getDisplayRotation": {Func: rpcGetDisplayRotation},
"getBacklightSettings": {Func: rpcGetBacklightSettings}, "setBacklightSettings": {Func: rpcSetBacklightSettings, Params: []string{"params"}},
"getDCPowerState": {Func: rpcGetDCPowerState}, "getBacklightSettings": {Func: rpcGetBacklightSettings},
"setDCPowerState": {Func: rpcSetDCPowerState, Params: []string{"enabled"}}, "getDCPowerState": {Func: rpcGetDCPowerState},
"setDCRestoreState": {Func: rpcSetDCRestoreState, Params: []string{"state"}}, "setDCPowerState": {Func: rpcSetDCPowerState, Params: []string{"enabled"}},
"getActiveExtension": {Func: rpcGetActiveExtension}, "setDCRestoreState": {Func: rpcSetDCRestoreState, Params: []string{"state"}},
"setActiveExtension": {Func: rpcSetActiveExtension, Params: []string{"extensionId"}}, "getActiveExtension": {Func: rpcGetActiveExtension},
"getATXState": {Func: rpcGetATXState}, "setActiveExtension": {Func: rpcSetActiveExtension, Params: []string{"extensionId"}},
"setATXPowerAction": {Func: rpcSetATXPowerAction, Params: []string{"action"}}, "getATXState": {Func: rpcGetATXState},
"getSerialSettings": {Func: rpcGetSerialSettings}, "setATXPowerAction": {Func: rpcSetATXPowerAction, Params: []string{"action"}},
"setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}}, "getSerialSettings": {Func: rpcGetSerialSettings},
"getUsbDevices": {Func: rpcGetUsbDevices}, "setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}},
"setUsbDevices": {Func: rpcSetUsbDevices, Params: []string{"devices"}}, "getUsbDevices": {Func: rpcGetUsbDevices},
"setUsbDeviceState": {Func: rpcSetUsbDeviceState, Params: []string{"device", "enabled"}}, "setUsbDevices": {Func: rpcSetUsbDevices, Params: []string{"devices"}},
"setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"apiUrl", "appUrl"}}, "setUsbDeviceState": {Func: rpcSetUsbDeviceState, Params: []string{"device", "enabled"}},
"getKeyboardLayout": {Func: rpcGetKeyboardLayout}, "setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"apiUrl", "appUrl"}},
"setKeyboardLayout": {Func: rpcSetKeyboardLayout, Params: []string{"layout"}}, "getKeyboardLayout": {Func: rpcGetKeyboardLayout},
"getKeyboardMacros": {Func: getKeyboardMacros}, "setKeyboardLayout": {Func: rpcSetKeyboardLayout, Params: []string{"layout"}},
"setKeyboardMacros": {Func: setKeyboardMacros, Params: []string{"params"}}, "getKeyboardMacros": {Func: getKeyboardMacros},
"getLocalLoopbackOnly": {Func: rpcGetLocalLoopbackOnly}, "setKeyboardMacros": {Func: setKeyboardMacros, Params: []string{"params"}},
"setLocalLoopbackOnly": {Func: rpcSetLocalLoopbackOnly, Params: []string{"enabled"}}, "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

@ -15,11 +15,11 @@ import notifications from "@/notifications";
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 { executeMacro } = useKeyboard(); const { executeMacro, cancelExecuteMacro } = useKeyboard();
const [invalidChars, setInvalidChars] = useState<string[]>([]); const [invalidChars, setInvalidChars] = useState<string[]>([]);
const close = useClose(); const close = useClose();
@ -35,10 +35,10 @@ 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);
@ -201,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

@ -122,6 +122,14 @@ export default function useKeyboard() {
}); });
}; };
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.
@ -231,5 +239,5 @@ export default function useKeyboard() {
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);