From 658f0709ad73ec1d9c4f8c3b893baeaf9c326621 Mon Sep 17 00:00:00 2001 From: Adam Shiervani Date: Fri, 20 Nov 2026 11:35:34 +0100 Subject: [PATCH] Linux copy and paste use the Insert shortcuts A paste did nothing in a Linux terminal, and a copy stopped the program that ran in it. The Mini typed Ctrl+V and Ctrl+C after the OS Services clipboard request, and a terminal takes both as control characters. Paste: a Linux sender that also writes the primary selection says so with bit 2 of the flags in clipboard.capabilities. The Mini then types Shift+Insert: a terminal pastes the primary selection, and other applications paste the clipboard. The Mini sends "clipboard_primary": true in session.config, because a sender sets the bit only for a KVM that accepts it. Without the bit the Mini types Ctrl+V as before. Copy: for a Linux target the Mini types Ctrl+Insert, which copies in a terminal and in other applications. The sender reads the clipboard as before, so this needs no flag. macOS and Windows targets do not change. Co-Authored-By: Claude Fable 5.2 --- .../include/os_services_wire.h | 4 ++ .../jetkvm_os_services/jetkvm_os_services.c | 5 +++ .../jetkvm_os_services/os_services_session.c | 15 +++++-- .../tests/test_os_services_protocol.py | 3 +- .../tests/test_os_services_session.c | 28 +++++++++++++ frontend/jetkvm-ui/src/hooks/stores.ts | 3 ++ .../jetkvm-ui/src/hooks/useGuestClipboard.ts | 42 ++++++++++++------- 7 files changed, 81 insertions(+), 19 deletions(-) diff --git a/components/jetkvm_os_services/include/os_services_wire.h b/components/jetkvm_os_services/include/os_services_wire.h index 53a81546..898a2317 100644 --- a/components/jetkvm_os_services/include/os_services_wire.h +++ b/components/jetkvm_os_services/include/os_services_wire.h @@ -132,6 +132,10 @@ enum { #define OSVC_CLIPBOARD_FLAG_READ (1U << 0) #define OSVC_CLIPBOARD_FLAG_WRITE (1U << 1) +/* clipboard.set also writes the primary selection, which a Linux terminal + * pastes with Shift+Insert. A sender sets it only with the write flag, and + * only for a KVM whose session.config has "clipboard_primary". */ +#define OSVC_CLIPBOARD_FLAG_PRIMARY (1U << 2) #define OSVC_CLIPBOARD_MAX_TEXT 65528U typedef enum { diff --git a/components/jetkvm_os_services/jetkvm_os_services.c b/components/jetkvm_os_services/jetkvm_os_services.c index 7da4203b..7afc5d8b 100644 --- a/components/jetkvm_os_services/jetkvm_os_services.c +++ b/components/jetkvm_os_services/jetkvm_os_services.c @@ -510,6 +510,11 @@ bool jetkvm_os_services_add_video_state(cJSON *object, void *ctx) cJSON_AddBoolToObject(clipboard, "write", (view->clipboard_flags & OSVC_CLIPBOARD_FLAG_WRITE) != 0U) && + /* A write also sets the primary selection, so the paste + * shortcut for Linux is Shift+Insert. */ + cJSON_AddBoolToObject(clipboard, "primary", + (view->clipboard_flags & + OSVC_CLIPBOARD_FLAG_PRIMARY) != 0U) && cJSON_AddStringToObject(clipboard, "os", osvc_target_os_name(view->os)); } diff --git a/components/jetkvm_os_services/os_services_session.c b/components/jetkvm_os_services/os_services_session.c index 5f9e2c49..9d50ff2d 100644 --- a/components/jetkvm_os_services/os_services_session.c +++ b/components/jetkvm_os_services/os_services_session.c @@ -399,7 +399,10 @@ static bool send_session_config(osvc_session_t *s) /* This KVM presents the same monitor each time to a sender * that sets the display's mode (video_display_serial). */ cJSON_AddBoolToObject(config, "display_mode_by_sender", true) != - NULL; + NULL && + /* This KVM takes the primary-selection flag in + * clipboard.capabilities (OSVC_CLIPBOARD_FLAG_PRIMARY). */ + cJSON_AddBoolToObject(config, "clipboard_primary", true) != NULL; if (ok) { text = cJSON_PrintUnformatted(config); ok = text != NULL; @@ -774,8 +777,14 @@ static void handle_clipboard(osvc_session_t *s, const uint8_t *payload, return; } const uint32_t flags = osvc_get_u32(payload); - if ((flags & ~(OSVC_CLIPBOARD_FLAG_READ | OSVC_CLIPBOARD_FLAG_WRITE)) != - 0U || + /* clipboard.set writes the primary selection, so its flag comes + * only with the write flag. */ + const bool primary_without_write = + (flags & OSVC_CLIPBOARD_FLAG_PRIMARY) != 0U && + (flags & OSVC_CLIPBOARD_FLAG_WRITE) == 0U; + if ((flags & ~(OSVC_CLIPBOARD_FLAG_READ | OSVC_CLIPBOARD_FLAG_WRITE | + OSVC_CLIPBOARD_FLAG_PRIMARY)) != 0U || + primary_without_write || payload[4] > OSVC_OS_LINUX || payload[5] != 0U || payload[6] != 0U || payload[7] != 0U) { fault_service(s, OSVC_SERVICE_CLIPBOARD, diff --git a/components/jetkvm_os_services/tests/test_os_services_protocol.py b/components/jetkvm_os_services/tests/test_os_services_protocol.py index 5a1d2f16..219ffe3d 100644 --- a/components/jetkvm_os_services/tests/test_os_services_protocol.py +++ b/components/jetkvm_os_services/tests/test_os_services_protocol.py @@ -802,7 +802,8 @@ def test_settings_changes_reach_the_sender(self): config = json.loads([p for s, k, p in got if (s, k) == (SESSION, CONFIG)][0]) self.assertEqual(config, {"terminal_enabled": True, "files_enabled": False, "capture_display": "HDMI-A-1", - "display_mode_by_sender": True}) + "display_mode_by_sender": True, + "clipboard_primary": True}) self.assertIn((CLIPBOARD, 7, b"\x00"), got) def test_registrations_answered_as_specified(self): diff --git a/components/jetkvm_os_services/tests/test_os_services_session.c b/components/jetkvm_os_services/tests/test_os_services_session.c index 8cbcfaa0..9e928796 100644 --- a/components/jetkvm_os_services/tests/test_os_services_session.c +++ b/components/jetkvm_os_services/tests/test_os_services_session.c @@ -177,6 +177,10 @@ static void check_config(const fake_message_t *config, bool terminal, /* The KVM leaves the display's mode to a sender that says it sets it. */ assert(cJSON_IsTrue( cJSON_GetObjectItemCaseSensitive(json, "display_mode_by_sender"))); + /* A sender sets the primary-selection flag only for a KVM that says it + * takes the flag. */ + assert(cJSON_IsTrue( + cJSON_GetObjectItemCaseSensitive(json, "clipboard_primary"))); cJSON_Delete(json); } @@ -676,6 +680,29 @@ static void test_clipboard(void) assert(!osvc_session_clipboard_available(&s_session)); } +/* The primary-selection flag is valid only with the write flag, because + * clipboard.set does the write. */ +static void test_clipboard_primary_flag(void) +{ + setup(); + uint8_t token[OSVC_TOKEN_LEN]; + open_session(token); + const uint32_t all = OSVC_CLIPBOARD_FLAG_READ | OSVC_CLIPBOARD_FLAG_WRITE | + OSVC_CLIPBOARD_FLAG_PRIMARY; + register_clipboard(all, OSVC_OS_LINUX); + expect_clipboard_config(1U); + assert(osvc_session_clipboard_available(&s_session)); + assert(s_session.clipboard.flags == all); + register_clipboard(OSVC_CLIPBOARD_FLAG_READ | OSVC_CLIPBOARD_FLAG_PRIMARY, + OSVC_OS_LINUX); + assert(!osvc_session_clipboard_available(&s_session)); + assert(s_session.counters.service_faults == 1U); + /* A bit this KVM does not know stays an error. */ + register_clipboard(1U << 3, OSVC_OS_LINUX); + assert(s_session.counters.service_faults == 2U); + assert(osvc_session_open(&s_session)); +} + static void test_clipboard_faults_and_settings(void) { setup(); @@ -4155,6 +4182,7 @@ int main(void) test_session_info_reports_changes(); test_approval_pending(); test_clipboard(); + test_clipboard_primary_flag(); test_clipboard_faults_and_settings(); test_video_registration_is_answered(); test_other_registrations(); diff --git a/frontend/jetkvm-ui/src/hooks/stores.ts b/frontend/jetkvm-ui/src/hooks/stores.ts index 6bb693a6..18f9008a 100644 --- a/frontend/jetkvm-ui/src/hooks/stores.ts +++ b/frontend/jetkvm-ui/src/hooks/stores.ts @@ -319,6 +319,9 @@ export type VideoSource = "physical" | "guest" | "switching_to_physical" | "swit export interface GuestClipboardState { read: boolean; write: boolean; + // A write also sets the primary selection, which a Linux terminal pastes + // with Shift+Insert. Not present on a KVM that predates the flag. + primary?: boolean; os: "windows" | "macos" | "linux" | "unknown"; } diff --git a/frontend/jetkvm-ui/src/hooks/useGuestClipboard.ts b/frontend/jetkvm-ui/src/hooks/useGuestClipboard.ts index eca34654..bc326147 100644 --- a/frontend/jetkvm-ui/src/hooks/useGuestClipboard.ts +++ b/frontend/jetkvm-ui/src/hooks/useGuestClipboard.ts @@ -1,6 +1,12 @@ import { useCallback } from "react"; -import { useCapability, useRTCStore, useUiStore, useVideoStore } from "@hooks/stores"; +import { + type GuestClipboardState, + useCapability, + useRTCStore, + useUiStore, + useVideoStore, +} from "@hooks/stores"; import useKeyboard from "@hooks/useKeyboard"; import { useCopyToClipboard } from "@components/useCopyToClipBoard"; import notifications from "@/notifications"; @@ -52,6 +58,24 @@ export function clipboardShortcut(key: "V" | "C") { return isMac() ? `⌘⇧${key}` : `Ctrl+Shift+${key}`; } +// The chord that pastes on the guest. A Linux terminal takes Ctrl+V for a +// control character, so a sender that also wrote the primary selection gets +// Shift+Insert: a terminal pastes the primary selection, and every other +// application pastes the clipboard. +export function pasteShortcut(guest: Pick) { + if (guest.os === "macos") return { keys: ["KeyV"], modifiers: ["MetaLeft"] }; + if (guest.primary) return { keys: ["Insert"], modifiers: ["ShiftLeft"] }; + return { keys: ["KeyV"], modifiers: ["ControlLeft"] }; +} + +// The chord that copies on the guest. A Linux terminal takes Ctrl+C to stop +// the program, and Ctrl+Insert copies there and in other apps. +export function copyShortcut(guest: Pick) { + if (guest.os === "macos") return { keys: ["KeyC"], modifiers: ["MetaLeft"] }; + if (guest.os === "linux") return { keys: ["Insert"], modifiers: ["ControlLeft"] }; + return { keys: ["KeyC"], modifiers: ["ControlLeft"] }; +} + function statusMessage(status: GuestClipboardStatus) { switch (status) { case "empty": @@ -143,13 +167,7 @@ export default function useGuestClipboard() { } // The chord that triggered this left Ctrl/Shift held on the guest. await resetKeyboardState(); - await executeMacro([ - { - keys: ["KeyV"], - modifiers: [guestClipboard.os === "macos" ? "MetaLeft" : "ControlLeft"], - delay: shortcutDelay, - }, - ]); + await executeMacro([{ ...pasteShortcut(guestClipboard), delay: shortcutDelay }]); return "pasted"; } catch (error) { notifications.error(m.clipboard_guest_error({ error: String(error) })); @@ -180,13 +198,7 @@ export default function useGuestClipboard() { try { // The chord that triggered this left modifiers held on the guest. await resetKeyboardState(); - await executeMacro([ - { - keys: ["KeyC"], - modifiers: [guestClipboard.os === "macos" ? "MetaLeft" : "ControlLeft"], - delay: shortcutDelay, - }, - ]); + await executeMacro([{ ...copyShortcut(guestClipboard), delay: shortcutDelay }]); await sleep(guestCopySettleMs); const content = await guestRpc("guestClipboardGet", {}); if (content.status === "unsupported") {