Skip to content
Merged
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
11 changes: 8 additions & 3 deletions src/backend/engine/worker/rdp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Status } from "../../protocol.js";
import type { RdpClient as WasmRdpClient } from "../../wasm/engine.js";
import type { PixelSource } from "../display.js";
import type { RemoteClient, RemoteHost, Settings } from "../remote.js";
import { Keyboard } from "./keyboard.js";
import { Keyboard, type Locks } from "./keyboard.js";
import { DEFAULT_LAYOUT } from "./layouts.js";
import { DriveCallbacks } from "./drive.js";
import { kerberosRequest } from "./kdc.js";
Expand Down Expand Up @@ -540,7 +540,7 @@ export class RdpClient implements RemoteClient, PixelSource {
this.height = event.h;
display.setSource(this);
display.resize(event.w, event.h);
this.rdp?.sync_locks(false, false, false);
this.syncLocks(this.keyboard.locks);
this.host.ready();
break;
case "resize":
Expand Down Expand Up @@ -631,12 +631,17 @@ export class RdpClient implements RemoteClient, PixelSource {

private sendKey(keysym: number, pressed: boolean): void {
for (const out of this.keyboard.key(keysym, pressed)) {
if ("scancode" in out) this.rdp?.key(out.scancode, out.down);
if ("locks" in out) this.syncLocks(out.locks);
else if ("scancode" in out) this.rdp?.key(out.scancode, out.down);
else this.rdp?.unicode(out.unicode, out.down);
}
this.pump();
}

private syncLocks(locks: Locks): void {
this.rdp?.sync_locks(false, locks.num, locks.caps);
}

resize(width: number, height: number, askedDpi: number): void {
const next = nextSize(this.size, width, height, askedDpi);
if (!next) return;
Expand Down
42 changes: 40 additions & 2 deletions src/backend/engine/worker/rdp/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ const ALT_L = 0x38;
const WIN_L = EXT | 0x5b;
const WIN_R = EXT | 0x5c;
const CAPS = 0x3a;
const NUM_LOCK = 0x45;

/**
* The Num Lock state a keypad key needs on the server: digits and the
* decimal point only type with it on, the navigation keys only with it off.
*/
function keypadNumLock(keysym: number): boolean | undefined {
const digit = keysym >= 0xffb0 && keysym <= 0xffb9;
if (digit || keysym === 0xffae || keysym === 0xffac) return true;
if (keysym >= 0xff95 && keysym <= 0xff9f) return false;
return undefined;
}

type Layer = "base" | "shift" | "altgr" | "altgrShift";

Expand Down Expand Up @@ -138,7 +150,15 @@ function indexLayout(spec: LayoutSpec): Map<string, Mapping> {
}

export type KeyOutput =
{ scancode: number; down: boolean } | { unicode: number; down: boolean };
| { scancode: number; down: boolean }
| { unicode: number; down: boolean }
/** Sets the server's lock keys, sent before a key that needs them. */
| { locks: Locks };

export interface Locks {
num: boolean;
caps: boolean;
}

/**
* Turns the keysyms a Guacamole client sends into RDP scancodes for the
Expand All @@ -154,6 +174,7 @@ export class Keyboard {
/** What each held keysym pressed, so its release lifts the same thing. */
private readonly pressedBy = new Map<number, KeyOutput[]>();
private capsLock = false;
private numLock = false;

constructor(layout: string) {
this.spec = LAYOUTS[layout] ?? LAYOUTS[DEFAULT_LAYOUT];
Expand All @@ -164,6 +185,11 @@ export class Keyboard {
return this.spec.id;
}

/** The lock state the server should have, to sync on (re)connect. */
get locks(): Locks {
return { num: this.numLock, caps: this.capsLock };
}

key(keysym: number, pressed: boolean): KeyOutput[] {
if (!pressed) return this.release(keysym);
// Key repeat: a second press without a release.
Expand Down Expand Up @@ -195,8 +221,9 @@ export class Keyboard {
const fixed = FIXED[keysym];
if (fixed !== undefined) {
if (fixed === CAPS) this.capsLock = !this.capsLock;
if (fixed === NUM_LOCK) this.numLock = !this.numLock;
this.pressedBy.set(keysym, [{ scancode: fixed, down: false }]);
return [this.downKey(fixed)];
return [...this.syncNumLock(keysym), this.downKey(fixed)];
}
const char = keysymToChar(keysym);
if (char === null) return [];
Expand All @@ -216,6 +243,17 @@ export class Keyboard {
return this.unicode(char);
}

/**
* The server reads a keypad scancode by its own Num Lock, not the user's,
* so set it to what the keysym means before sending the key.
*/
private syncNumLock(keysym: number): KeyOutput[] {
const want = keypadNumLock(keysym);
if (want === undefined || want === this.numLock) return [];
this.numLock = want;
return [{ locks: this.locks }];
}

private release(keysym: number): KeyOutput[] {
const held = this.pressedBy.get(keysym);
if (!held) return [];
Expand Down
36 changes: 36 additions & 0 deletions tests/backend/engine/worker/rdp/keyboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,39 @@ describe("Korean input keys", () => {
expect(tap(kb, 0xff34)).toEqual([down(0x71), up(0x71)]);
});
});

describe("Keyboard keypad Num Lock", () => {
const KP_1 = 0xffb1;
const KP_END = 0xff9c;
const NUM_LOCK = 0xff7f;

it("turns the server's Num Lock on before a keypad digit", () => {
const kb = new Keyboard("en-us-qwerty");
expect(tap(kb, KP_1)).toEqual([
{ locks: { num: true, caps: false } },
down(0x4f),
up(0x4f),
]);
// Already on: the next digit goes straight through.
expect(tap(kb, KP_1)).toEqual([down(0x4f), up(0x4f)]);
expect(kb.locks).toEqual({ num: true, caps: false });
});

it("turns it off for keypad navigation, keeping Caps Lock", () => {
const kb = new Keyboard("en-us-qwerty");
tap(kb, 0xffe5);
tap(kb, KP_1);
expect(tap(kb, KP_END)).toEqual([
{ locks: { num: false, caps: true } },
down(0x4f),
up(0x4f),
]);
});

it("follows Num Lock pressed in the session", () => {
const kb = new Keyboard("en-us-qwerty");
expect(tap(kb, NUM_LOCK)).toEqual([down(0x45), up(0x45)]);
expect(kb.locks.num).toBe(true);
expect(tap(kb, KP_1)).toEqual([down(0x4f), up(0x4f)]);
});
});