Files
piik/src/client/remote-control/protocol.ts
T
AAA 808a49e624 refactor(core): centralize state changes and persistence outcomes
Keep channel time explicit, room binding transitions owned by their records, and shared limits checked against Core. Persist the actual changes before delivery, preserving expiry and the original requester on failure. Include the verified receiving-route, cache-clear, tooltip and native appearance corrections from this integration phase.
2026-11-29 02:50:22 -05:00

64 lines
3.0 KiB
TypeScript

import { z } from "zod";
import { isComposingOrAltGraph } from "../ui/shortcut-key";
import { INPUT_FRAME_BYTES, REMOTE_INPUT_COMMAND_BYTES, REMOTE_TEXT_BYTES } from "../../shared/core-limits";
const encoder = new TextEncoder();
export function inputFrameFits(frame: string): boolean {
return frame.length <= INPUT_FRAME_BYTES && encoder.encode(frame).byteLength <= INPUT_FRAME_BYTES;
}
// The editor can measure the command before sending. Every grant has the same hex length.
const exampleGrant = "0".repeat(64);
export function remoteTextFits(text: string, grant = exampleGrant): boolean {
return text.length <= REMOTE_TEXT_BYTES && encoder.encode(text).byteLength <= REMOTE_TEXT_BYTES
&& encoder.encode(JSON.stringify({ type: "text", text, grant })).byteLength <= REMOTE_INPUT_COMMAND_BYTES;
}
// F12 is reserved by Windows debuggers, including for global modified chords.
export const remoteShortcutKeySchema = z.number().int().min(1).max(11);
export const REMOTE_SHORTCUT_KEYS = Array.from({ length: 11 }, (_, index) => index + 1);
export function matchesRemoteShortcut(event: Pick<KeyboardEvent, "code" | "ctrlKey" | "altKey" | "shiftKey" | "metaKey"
| "isComposing" | "keyCode" | "getModifierState">, key: number): boolean {
return !isComposingOrAltGraph(event) && event.ctrlKey && event.altKey && !event.shiftKey && !event.metaKey && event.code === `F${key}`;
}
export const remoteControlModeSchema = z.enum(["off", "ask", "room", "password"]);
export type RemoteControlMode = z.infer<typeof remoteControlModeSchema>;
export const remoteInputPlatformSchema = z.enum(["windows", "darwin", "linux"]);
export type RemoteInputPlatform = z.infer<typeof remoteInputPlatformSchema>;
export const remoteInputStateSchema = z.object({
state: z.enum(["ready", "unavailable", "pending", "active", "suspended", "failed"]),
mode: remoteControlModeSchema,
platform: z.string().min(1),
grant: z.string().regex(/^[0-9a-f]{64}$/).optional(),
intent: z.string().regex(/^[0-9a-f]{32}$/).optional(),
// A future diagnostic has no local copy, but differs from a reply with no reason.
reason: z.enum(["password", "retry", "busy", "gamepad"]).or(z.string().min(1).transform(() => null)).optional(),
capabilities: z.array(z.string().min(1).max(32)).max(16).optional(),
coPlay: z.boolean().optional(),
inputFailed: z.boolean().optional(),
});
export type RemoteInputState = z.infer<typeof remoteInputStateSchema>;
export function remoteTextAvailable(state: RemoteInputState): boolean {
return !state.coPlay || state.capabilities?.includes("text") === true;
}
export function remoteGamepadAvailable(state: RemoteInputState): boolean {
return state.coPlay === true && state.capabilities?.includes("gamepad") === true;
}
// Button bits follow W3C standard indices; sticks are signed 16-bit, Y down,
// and triggers are unsigned 8-bit. Native SDK layouts stay in Host adapters.
export interface RemoteGamepadReport {
buttons: number;
leftTrigger: number;
rightTrigger: number;
leftX: number;
leftY: number;
rightX: number;
rightY: number;
}