forked from Blendman974/faceclaw
Add bluetooth keyboard support
G2 only: You can pair the glasses with a bluetooth keyboard. It will relay key events, and you can use it to type in some apps and to navigate the UI.
This commit is contained in:
1 parent
9372b2742c
commit
fd3c26333f
46 files changed
+3867
-41
No files matched your search
@@ -187,6 +187,8 @@ export async function launchWorkerAppWindow(
|
||||
matchExistingBy?: "appId" | "windowId";
|
||||
/** Deliver watch swipes raw instead of the fallback (see WorkerWindowSpec). */
|
||||
acceptsDirectional?: boolean;
|
||||
/** Deliver hardware-keyboard keys as keys (see WorkerWindowSpec). */
|
||||
acceptsKeys?: boolean;
|
||||
},
|
||||
): Promise<void> {
|
||||
const existing = shell
|
||||
@@ -207,6 +209,7 @@ export async function launchWorkerAppWindow(
|
||||
icon: options.icon,
|
||||
focus: true,
|
||||
acceptsDirectional: options.acceptsDirectional,
|
||||
acceptsKeys: options.acceptsKeys,
|
||||
});
|
||||
ctx.appendLog(`launched ${options.windowId}`);
|
||||
}
|
||||
@@ -312,6 +312,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "text-input":
|
||||
case "key":
|
||||
case "tool-call":
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -305,6 +305,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
break;
|
||||
case "navigation-sensors":
|
||||
case "text-input":
|
||||
case "key":
|
||||
case "tool-call":
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -236,6 +236,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
case "navigation-sensors":
|
||||
case "screen":
|
||||
case "text-input":
|
||||
case "key":
|
||||
case "tool-call":
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -272,6 +272,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "text-input":
|
||||
case "key":
|
||||
case "tool-call":
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -370,6 +370,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
case "key":
|
||||
// Only windows opened with acceptsKeys are sent keys.
|
||||
break;
|
||||
case "text-input":
|
||||
if (editing) {
|
||||
// Voice input as an alternative to the phone keyboard.
|
||||
|
||||
@@ -433,6 +433,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
}
|
||||
case "navigation-sensors":
|
||||
case "text-input":
|
||||
case "key":
|
||||
case "tool-call":
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -227,6 +227,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
case "key":
|
||||
// Only windows opened with acceptsKeys are sent keys.
|
||||
break;
|
||||
case "text-input":
|
||||
if (editingSetting) {
|
||||
// Voice input as an alternative to the phone keyboard.
|
||||
|
||||
@@ -424,6 +424,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
||||
handleInput(window, message.event as InputEvent, message.frameId);
|
||||
break;
|
||||
case "key":
|
||||
// Only windows opened with acceptsKeys are sent keys.
|
||||
break;
|
||||
case "text-input":
|
||||
if (window && window.windowId === message.windowId) handleTextInput(message.text.trim());
|
||||
break;
|
||||
|
||||
@@ -14,6 +14,8 @@ const terminalApp: AppDefinition = {
|
||||
// Session windows stay independent: launching reopens the hub even
|
||||
// while terminal sessions are open.
|
||||
matchExistingBy: "windowId",
|
||||
// The hub window can show a session too, so it takes keys like one.
|
||||
acceptsKeys: true,
|
||||
}),
|
||||
};
|
||||
|
||||
|
||||
@@ -55,6 +55,8 @@ import {
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { navigationGestureForKey, type KeyEvent } from "../../g2/keyboard/keys";
|
||||
import { encodeTerminalKey, terminalScrollbackKey } from "./terminal-keys";
|
||||
import { G2MirrorClient, type G2MirrorClientOptions, type G2MirrorSession, type G2MirrorState } from "../../native/g2mirror-client";
|
||||
import { onSettingsStoreChanged } from "../../native/settings-store";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
@@ -435,6 +437,13 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "key": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.focused = message.focused;
|
||||
handleKey(window, message.event as KeyEvent);
|
||||
break;
|
||||
}
|
||||
case "render": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
@@ -1260,6 +1269,35 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number)
|
||||
frameTimings.finishFrame(frameId, "discarded: terminal view ignored input");
|
||||
}
|
||||
|
||||
/**
|
||||
* A key from a hardware keyboard (the glasses' keyboard bridge). A session
|
||||
* with nothing over it gets the key as its terminal would, every key
|
||||
* included: Escape and the arrows belong to the program, so leaving is still
|
||||
* the ring's double tap. Anywhere else (the hub's lists, an open menu) the
|
||||
* navigation keys act as the ring gestures they stand for.
|
||||
*/
|
||||
function handleKey(window: TerminalWindow, event: KeyEvent): void {
|
||||
if (window.kind === "view" && !window.menu?.isOpen()) {
|
||||
activeViewId = window.windowId;
|
||||
const page = terminalScrollbackKey(event);
|
||||
if (page !== null) {
|
||||
handleViewScroll(window, page, 0);
|
||||
return;
|
||||
}
|
||||
const bytes = encodeTerminalKey(event, window.emulator.applicationCursorKeys());
|
||||
if (bytes === null) return;
|
||||
// Typing follows the live screen, as in any terminal emulator.
|
||||
if (window.scrollTop !== null) {
|
||||
window.scrollTop = null;
|
||||
scheduleRender(window);
|
||||
}
|
||||
window.client.sendInput(bytes);
|
||||
return;
|
||||
}
|
||||
const gesture = navigationGestureForKey(event);
|
||||
if (gesture) handleInput(window, { type: gesture, source: "ring", timestampMs: event.timestampMs }, 0);
|
||||
}
|
||||
|
||||
/** Top visible absolute line index when following the live bottom. */
|
||||
function followTop(window: ViewWindow): number {
|
||||
return window.historyNext + window.emulator.bufferLength() - window.gridRows;
|
||||
@@ -1800,6 +1838,7 @@ function openViewWindow(control: ControlConnection, socket: string, label: strin
|
||||
focus: true,
|
||||
// Default to tall sessions; the app display setting can override this.
|
||||
heightMode: "max",
|
||||
acceptsKeys: true,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -82,4 +82,9 @@ export class TerminalEmulator {
|
||||
cursorCol(): number {
|
||||
return this.term.buffer.active.cursorX;
|
||||
}
|
||||
|
||||
/** DECCKM: the session asked for application cursor keys (ESC O A rather than ESC [ A). */
|
||||
applicationCursorKeys(): boolean {
|
||||
return this.term.modes.applicationCursorKeysMode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* What a terminal emulator sends for a key from a hardware keyboard, by the
|
||||
* classic xterm conventions: Ctrl chords become control characters, Alt
|
||||
* prefixes ESC, and modified cursor and function keys carry the xterm
|
||||
* modifier parameter (1 + shift + 2*alt + 4*ctrl). Follows the Glassly
|
||||
* Terminal miniapp's encoder (terminal/keys.ts, MIT).
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import type { KeyEvent } from "../../g2/keyboard/keys";
|
||||
|
||||
const ESC = "\x1b";
|
||||
|
||||
/** Keys xterm encodes as `ESC [ <n> ~`, with a modifier as `;m`. */
|
||||
const TILDE_KEYS: Record<string, number> = {
|
||||
Insert: 2,
|
||||
Delete: 3,
|
||||
PageUp: 5,
|
||||
PageDown: 6,
|
||||
F5: 15,
|
||||
F6: 17,
|
||||
F7: 18,
|
||||
F8: 19,
|
||||
F9: 20,
|
||||
F10: 21,
|
||||
F11: 23,
|
||||
F12: 24,
|
||||
};
|
||||
|
||||
/** Keys with a single-letter final byte: SS3 in application cursor mode, CSI otherwise. */
|
||||
const LETTER_KEYS: Record<string, string> = {
|
||||
ArrowUp: "A",
|
||||
ArrowDown: "B",
|
||||
ArrowRight: "C",
|
||||
ArrowLeft: "D",
|
||||
Home: "H",
|
||||
End: "F",
|
||||
};
|
||||
|
||||
/** F1-F4 are always SS3 (`ESC O P`), or CSI with a modifier. */
|
||||
const SS3_KEYS: Record<string, string> = { F1: "P", F2: "Q", F3: "R", F4: "S" };
|
||||
|
||||
/** Ctrl with a punctuation or digit key, per xterm (^[ ^\ ^] ^^ ^_ ^? ^@). */
|
||||
const CONTROL_PUNCTUATION: Record<string, string> = {
|
||||
"[": "\x1b",
|
||||
"\\": "\x1c",
|
||||
"]": "\x1d",
|
||||
"^": "\x1e",
|
||||
"6": "\x1e",
|
||||
"_": "\x1f",
|
||||
"-": "\x1f",
|
||||
"?": "\x7f",
|
||||
"/": "\x7f",
|
||||
"8": "\x7f",
|
||||
"@": "\x00",
|
||||
"2": "\x00",
|
||||
};
|
||||
|
||||
/**
|
||||
* The text to write to the terminal for one key event, or null when the key
|
||||
* sends nothing: releases, bare modifiers, Meta chords, and keys a terminal
|
||||
* has no bytes for (volume, media). `appCursor` is DECCKM, which the emulator
|
||||
* tracks from the session's output.
|
||||
*/
|
||||
export function encodeTerminalKey(event: KeyEvent, appCursor: boolean): string | null {
|
||||
if (event.phase !== "down") return null;
|
||||
const { key, modifiers } = event;
|
||||
if (modifiers.meta) return null;
|
||||
const modifierParam = 1 + (modifiers.shift ? 1 : 0) + (modifiers.alt ? 2 : 0) + (modifiers.ctrl ? 4 : 0);
|
||||
const modified = (prefix: string, final: string): string | null =>
|
||||
modifierParam > 1 ? `${ESC}[${prefix};${modifierParam}${final}` : null;
|
||||
|
||||
switch (key) {
|
||||
case "Enter":
|
||||
return modifiers.alt ? `${ESC}\r` : "\r";
|
||||
case "Escape":
|
||||
return ESC;
|
||||
case "Tab":
|
||||
return modifiers.shift ? `${ESC}[Z` : "\t";
|
||||
case "Backspace":
|
||||
if (modifiers.alt) return `${ESC}\x7f`;
|
||||
return modifiers.ctrl ? "\x08" : "\x7f";
|
||||
case " ":
|
||||
return modifiers.ctrl ? "\x00" : modifiers.alt ? `${ESC} ` : " ";
|
||||
}
|
||||
|
||||
const letter = LETTER_KEYS[key];
|
||||
if (letter) return modified("1", letter) ?? (appCursor ? `${ESC}O${letter}` : `${ESC}[${letter}`);
|
||||
const tilde = TILDE_KEYS[key];
|
||||
if (tilde) return modified(String(tilde), "~") ?? `${ESC}[${tilde}~`;
|
||||
const ss3 = SS3_KEYS[key];
|
||||
if (ss3) return modified("1", ss3) ?? `${ESC}O${ss3}`;
|
||||
|
||||
if (modifiers.ctrl) {
|
||||
// From the unshifted key: ^A..^Z, then the punctuation set.
|
||||
const base = key.length === 1 ? key.toLowerCase() : "";
|
||||
let char: string | undefined;
|
||||
if (base >= "a" && base <= "z") char = String.fromCharCode(base.charCodeAt(0) - 96);
|
||||
else char = CONTROL_PUNCTUATION[key];
|
||||
if (char === undefined) return null;
|
||||
return modifiers.alt ? `${ESC}${char}` : char;
|
||||
}
|
||||
|
||||
// With Alt held the translator leaves `text` out (it is a chord, not
|
||||
// typing); a terminal still wants ESC and the character.
|
||||
const text = event.text ?? (modifiers.alt && key.length === 1 ? key : undefined);
|
||||
if (text === undefined) return null;
|
||||
return modifiers.alt ? `${ESC}${text}` : text;
|
||||
}
|
||||
|
||||
/** Shift+PageUp / Shift+PageDown page the emulator's own scrollback, as in xterm. */
|
||||
export function terminalScrollbackKey(event: KeyEvent): -1 | 1 | null {
|
||||
if (event.phase !== "down") return null;
|
||||
const { shift, ctrl, alt, meta } = event.modifiers;
|
||||
if (!shift || ctrl || alt || meta) return null;
|
||||
if (event.key === "PageUp") return -1;
|
||||
if (event.key === "PageDown") return 1;
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user