Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/jetkvm_os_services/include/os_services_wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions components/jetkvm_os_services/jetkvm_os_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
15 changes: 12 additions & 3 deletions components/jetkvm_os_services/os_services_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 28 additions & 0 deletions components/jetkvm_os_services/tests/test_os_services_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions frontend/jetkvm-ui/src/hooks/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
42 changes: 27 additions & 15 deletions frontend/jetkvm-ui/src/hooks/useGuestClipboard.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<GuestClipboardState, "os" | "primary">) {
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<GuestClipboardState, "os">) {
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":
Expand Down Expand Up @@ -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) }));
Expand Down Expand Up @@ -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<GuestClipboardContent>("guestClipboardGet", {});
if (content.status === "unsupported") {
Expand Down
Loading