forked from Blendman974/faceclaw
Configurable custom keybindings for bluetooth keyboards paired with the glasses
This commit is contained in:
1 parent
fd3c26333f
commit
344e140e92
17 files changed
+1203
-100
No files matched your search
@@ -23,7 +23,7 @@ import { getBooleanSetting, setBooleanSetting } from "../native/settings-store";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from "./firmware-compat";
|
||||
import { keyboardService } from "./keyboard/service";
|
||||
import { navigationGestureForKey, type KeyEvent } from "./keyboard/keys";
|
||||
import type { KeyEvent } from "./keyboard/keys";
|
||||
import { hasExtractedEvenHubFonts } from "./firmware-builder";
|
||||
import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy";
|
||||
import { WearRemote, type WearRemoteInputKind } from "./wear-remote";
|
||||
@@ -515,10 +515,23 @@ class DashboardController {
|
||||
assistantAvailable: () => shell.isAssistantAvailable(),
|
||||
assistant: text => shell.sendToAssistant(text),
|
||||
});
|
||||
// A keyboard paired through the glasses (CFW keyboard bridge). Its keys
|
||||
// go to the focused window when that takes keys, else act as navigation.
|
||||
// A keyboard paired through the glasses (CFW keyboard bridge). The
|
||||
// service decides what each key means (app/g2/keyboard/key-router.ts);
|
||||
// this is how it reaches the display and the input pipeline.
|
||||
keyboardService.setHost({
|
||||
onKey: (event) => this.handleKeyboardKey(event),
|
||||
ready: () => this.phase === "connected",
|
||||
locked: () => this.glassesLocked,
|
||||
wake: () => {
|
||||
void this.handleInputEvent({
|
||||
kind: "display-wake",
|
||||
containerName: "",
|
||||
eventType: OsEventTypeList.DOUBLE_CLICK_EVENT,
|
||||
eventSource: EventSourceType.TOUCH_EVENT_FORM_DUMMY_NULL,
|
||||
systemExitReasonCode: 0,
|
||||
frameId: 0,
|
||||
}).catch((error) => this.appendLog(`keyboard wake failed: ${this.formatError(error)}`));
|
||||
},
|
||||
gesture: (gesture, origin) => this.injectSyntheticRingInput(gesture, origin),
|
||||
log: (line) => this.appendLog(line),
|
||||
// A keyboard that is switched off would otherwise keep the glasses'
|
||||
// radio looking for it around the clock. One still connected when the
|
||||
@@ -2656,35 +2669,6 @@ class DashboardController {
|
||||
else keyboardService.linkLost(this.phase === "charging" || this.phase === "disconnecting");
|
||||
}
|
||||
|
||||
/**
|
||||
* A key from the keyboard paired through the glasses. A window that takes
|
||||
* keys (a terminal session) gets it as a key; everywhere else the few
|
||||
* navigation keys act as the ring gestures they stand for. A dark display
|
||||
* takes no typing: the first press only wakes it, as the ring's double tap
|
||||
* would, and a locked one stays locked.
|
||||
*/
|
||||
private handleKeyboardKey(event: KeyEvent): void {
|
||||
if (this.phase !== "connected") return;
|
||||
if (this.glassesLocked || !shell.isScreenOn()) {
|
||||
if (event.phase !== "down" || event.repeat || shell.isScreenOn()) return;
|
||||
void this.handleInputEvent({
|
||||
kind: "display-wake",
|
||||
containerName: "",
|
||||
eventType: OsEventTypeList.DOUBLE_CLICK_EVENT,
|
||||
eventSource: EventSourceType.TOUCH_EVENT_FORM_DUMMY_NULL,
|
||||
systemExitReasonCode: 0,
|
||||
frameId: 0,
|
||||
}).catch((error) => this.appendLog(`keyboard wake failed: ${this.formatError(error)}`));
|
||||
return;
|
||||
}
|
||||
if (shell.receiveKeyEvent(event)) return;
|
||||
const gesture = navigationGestureForKey(event);
|
||||
if (!gesture) return;
|
||||
void this.injectSyntheticRingInput(gesture).catch((error) =>
|
||||
this.appendLog(`keyboard navigation failed: ${this.formatError(error)}`),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Glanceboard event a sleep-time input means, or null when the shell
|
||||
* should see the input as usual. A display-wake carries the head-tilt
|
||||
@@ -2792,6 +2776,7 @@ class DashboardController {
|
||||
}
|
||||
},
|
||||
endTextSettingEdit: () => this.endTextSettingEdit(),
|
||||
unhandledKey: (event) => keyboardService.handleUnhandledKey(event as KeyEvent),
|
||||
});
|
||||
this.appHosts.set(appId, host);
|
||||
return host;
|
||||
|
||||
@@ -29,6 +29,8 @@ import { OsEventTypeList } from './events'
|
||||
import { loadDeviceAddresses } from './device-addresses'
|
||||
import { updateGlassesPresence } from './glasses-presence'
|
||||
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat'
|
||||
import { keyboardService } from './keyboard/service'
|
||||
import type { KeyEvent } from './keyboard/keys'
|
||||
import { isHaltedSessionPhase, resumeAutoReconnect, suppressAutoReconnect } from './reconnect-policy'
|
||||
import { WearRemote, type WearRemoteInputKind } from './wear-remote'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
@@ -232,6 +234,7 @@ export class IosPreviewController {
|
||||
if (!on) void this.display?.setScreenBlanked(true).catch(error => this.fail(error))
|
||||
this.requestShellRender()
|
||||
this.wearRemote?.schedulePublish()
|
||||
if (on) keyboardService.retryNow()
|
||||
},
|
||||
})
|
||||
const launcher = createLauncherWindow({
|
||||
@@ -270,6 +273,28 @@ export class IosPreviewController {
|
||||
assistantAvailable: () => shell.isAssistantAvailable(),
|
||||
assistant: text => shell.sendToAssistant(text),
|
||||
})
|
||||
// A keyboard paired through the glasses (the CFW keyboard bridge). The service decides what
|
||||
// each key means (app/g2/keyboard/key-router.ts); its gestures share the controller queue
|
||||
// with physical input, like the watch's.
|
||||
const queueKeyboardInput = (run: () => Promise<void>): Promise<void> => {
|
||||
const task = this.inputQueue.then(() => { this.onActivity(); return run() })
|
||||
this.inputQueue = task.catch(error => this.fail(error))
|
||||
return task
|
||||
}
|
||||
keyboardService.setHost({
|
||||
ready: () => this.state.phase === 'connected',
|
||||
locked: () => this.glassesLocked,
|
||||
wake: () => { void queueKeyboardInput(() => this.receiveInput(makeInputEvent({ type: 'display-wake' }))).catch(() => {}) },
|
||||
gesture: (gesture, origin) => queueKeyboardInput(async () => {
|
||||
await this.receiveInput(makeInputEvent({ type: gesture, source: origin } as InputEventPayload))
|
||||
// A discrete hold includes its release.
|
||||
if (gesture === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source: origin }))
|
||||
}),
|
||||
log: line => this.logBluetooth(line),
|
||||
// Only while the display is on: a keyboard that is switched off would otherwise keep the
|
||||
// glasses' radio looking for it around the clock.
|
||||
shouldReconnect: () => this.state.phase === 'connected' && shell.isScreenOn(),
|
||||
})
|
||||
// The header's EvenHub phone-UI button hides while that UI is showing.
|
||||
onPhoneShownChanged(() => this.syncEvenHubPhoneUi())
|
||||
registerSystemTools()
|
||||
@@ -809,6 +834,7 @@ export class IosPreviewController {
|
||||
},
|
||||
setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
|
||||
removeSurface: id => this.removeWindowSurface(id),
|
||||
unhandledKey: event => keyboardService.handleUnhandledKey(event as KeyEvent),
|
||||
submitPixels: (id, pixels, width, height, draws) => {
|
||||
void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error))
|
||||
},
|
||||
@@ -830,6 +856,18 @@ export class IosPreviewController {
|
||||
this.state = { ...this.state, phase, status }
|
||||
this.logBluetooth(status)
|
||||
this.emitState()
|
||||
this.syncKeyboardLink()
|
||||
}
|
||||
/**
|
||||
* Tell the keyboard bridge whether the session can host a keyboard: a G2 on firmware with
|
||||
* the bridge, connected. On a disconnect the user asked for, the link is still up when the
|
||||
* phase turns, so the firmware is told to let the keyboard go first.
|
||||
*/
|
||||
private syncKeyboardLink(): void {
|
||||
const up = !!this.communicator && this.state.phase === 'connected' && this.firmwareCompatible
|
||||
&& glassesTypeSetting.get() === 'g2'
|
||||
if (up) keyboardService.linkReady()
|
||||
else keyboardService.linkLost(this.state.phase === 'disconnecting')
|
||||
}
|
||||
/** Mirror the session into glasses-presence (read by EvenHub's device status and Timers). */
|
||||
private syncPresence(): void {
|
||||
@@ -949,6 +987,7 @@ export class IosPreviewController {
|
||||
const firmwareWarning = firmwareIncompatibilityMessage(info) ?? ''
|
||||
if (firmwareWarning && firmwareWarning !== this.state.firmwareWarning) this.logBluetooth(`Firmware compatibility warning: ${firmwareWarning}`)
|
||||
this.firmwareCompatible = !firmwareWarning && hasCompatibleFirmware(info)
|
||||
this.syncKeyboardLink()
|
||||
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
|
||||
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities,
|
||||
firmwareWarning }
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
/**
|
||||
* What keys from a hardware keyboard do, apart from typing: the built-in
|
||||
* defaults, the user's custom keybindings, and how the two combine.
|
||||
*
|
||||
* A key becomes one of the gestures the ring and the watch already have, so
|
||||
* nothing downstream knows about keyboards. Three layers, first match wins:
|
||||
*
|
||||
* 1. Custom keybindings (Settings > Keyboard > Keybindings). These apply
|
||||
* everywhere, including where the focused window takes keys for itself
|
||||
* (a terminal session), since that is the point of binding a remote's
|
||||
* few buttons.
|
||||
* 2. Built-in bindings that also apply there: Escape with Shift, Ctrl or
|
||||
* Alt goes back, which is the way out of a terminal session.
|
||||
* 3. Built-in navigation, only for keys no window took: the arrows are the
|
||||
* watch's four swipes, Enter selects, Escape goes back.
|
||||
*
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import type { KeyEvent } from "./keys";
|
||||
|
||||
/** A gesture a key can stand for, named as the input pipeline names it. */
|
||||
export type KeyGesture =
|
||||
| "swipe-up"
|
||||
| "swipe-down"
|
||||
| "swipe-left"
|
||||
| "swipe-right"
|
||||
| "scroll-up"
|
||||
| "scroll-down"
|
||||
| "click"
|
||||
| "double-click"
|
||||
| "long-press"
|
||||
| "short-then-long-press";
|
||||
|
||||
/**
|
||||
* What a custom keybinding does. "none" turns off the key's built-in
|
||||
* navigation meaning; it does not stop the key being typed into a window
|
||||
* that takes keys.
|
||||
*/
|
||||
export type KeyAction = KeyGesture | "none";
|
||||
|
||||
/** The actions, in the order the picker lists them. */
|
||||
export const KEY_ACTIONS: readonly { action: KeyAction; label: string }[] = [
|
||||
{ action: "click", label: "Select" },
|
||||
{ action: "double-click", label: "Back" },
|
||||
{ action: "swipe-up", label: "Swipe up" },
|
||||
{ action: "swipe-down", label: "Swipe down" },
|
||||
{ action: "swipe-left", label: "Swipe left" },
|
||||
{ action: "swipe-right", label: "Swipe right" },
|
||||
{ action: "scroll-up", label: "Scroll up" },
|
||||
{ action: "scroll-down", label: "Scroll down" },
|
||||
{ action: "long-press", label: "System menu" },
|
||||
{ action: "short-then-long-press", label: "App menu" },
|
||||
{ action: "none", label: "Nothing" },
|
||||
];
|
||||
|
||||
export function keyActionLabel(action: KeyAction): string {
|
||||
return KEY_ACTIONS.find((entry) => entry.action === action)?.label ?? action;
|
||||
}
|
||||
|
||||
function isKeyAction(value: unknown): value is KeyAction {
|
||||
return KEY_ACTIONS.some((entry) => entry.action === value);
|
||||
}
|
||||
|
||||
/** A physical key with the modifiers held: what a keybinding is keyed on. */
|
||||
export type KeyChord = { code: string; shift: boolean; ctrl: boolean; alt: boolean; meta: boolean };
|
||||
export type KeyBinding = { chord: KeyChord; action: KeyAction };
|
||||
|
||||
const MODIFIER_CODES = new Set([
|
||||
"ControlLeft", "ControlRight", "ShiftLeft", "ShiftRight", "AltLeft", "AltRight", "MetaLeft", "MetaRight",
|
||||
]);
|
||||
|
||||
/** Shift, Ctrl, Alt and Meta themselves: they qualify a chord rather than being one. */
|
||||
export function isModifierCode(code: string): boolean {
|
||||
return MODIFIER_CODES.has(code);
|
||||
}
|
||||
|
||||
export function chordOf(event: KeyEvent): KeyChord {
|
||||
const { shift, ctrl, alt, meta } = event.modifiers;
|
||||
return { code: event.code, shift, ctrl, alt, meta };
|
||||
}
|
||||
|
||||
/** Canonical text of a chord ("Ctrl+Shift+Escape"): the stored form, and the lookup key. */
|
||||
export function chordId(chord: KeyChord): string {
|
||||
return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", chord.code]
|
||||
.filter(Boolean).join("+");
|
||||
}
|
||||
|
||||
/** The inverse of chordId, or null for anything it could not have produced. */
|
||||
export function parseChordId(text: string): KeyChord | null {
|
||||
const parts = text.split("+");
|
||||
const code = parts.pop() ?? "";
|
||||
if (!/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(code) || isModifierCode(code)) return null;
|
||||
const chord: KeyChord = { code, shift: false, ctrl: false, alt: false, meta: false };
|
||||
for (const part of parts) {
|
||||
if (part === "Ctrl" && !chord.ctrl) chord.ctrl = true;
|
||||
else if (part === "Alt" && !chord.alt) chord.alt = true;
|
||||
else if (part === "Shift" && !chord.shift) chord.shift = true;
|
||||
else if (part === "Meta" && !chord.meta) chord.meta = true;
|
||||
else return null;
|
||||
}
|
||||
return chord;
|
||||
}
|
||||
|
||||
const CODE_LABELS: Record<string, string> = {
|
||||
ArrowUp: "Up", ArrowDown: "Down", ArrowLeft: "Left", ArrowRight: "Right",
|
||||
Escape: "Esc", PageUp: "Page Up", PageDown: "Page Down", CapsLock: "Caps Lock",
|
||||
PrintScreen: "Print Screen", ScrollLock: "Scroll Lock", NumLock: "Num Lock", ContextMenu: "Menu",
|
||||
Minus: "-", Equal: "=", BracketLeft: "[", BracketRight: "]", Backslash: "\\", IntlBackslash: "\\",
|
||||
IntlHash: "#", Semicolon: ";", Quote: "'", Backquote: "`", Comma: ",", Period: ".", Slash: "/",
|
||||
AudioVolumeUp: "Volume Up", AudioVolumeDown: "Volume Down", AudioVolumeMute: "Mute",
|
||||
MediaPlayPause: "Play/Pause", MediaPlay: "Play", MediaPause: "Pause", MediaStop: "Stop",
|
||||
MediaTrackNext: "Next Track", MediaTrackPrevious: "Previous Track",
|
||||
BrightnessUp: "Brightness Up", BrightnessDown: "Brightness Down",
|
||||
BrowserSearch: "Search", BrowserHome: "Browser Home", BrowserBack: "Browser Back", BrowserForward: "Browser Forward",
|
||||
LaunchMail: "Mail", LaunchApplication1: "App 1", LaunchApplication2: "App 2",
|
||||
};
|
||||
|
||||
/** A key as a person would name it. Unknown keys keep their HID usage number, which is all there is. */
|
||||
function codeLabel(code: string): string {
|
||||
const known = CODE_LABELS[code];
|
||||
if (known) return known;
|
||||
let match = /^Key([A-Z])$/.exec(code);
|
||||
if (match) return match[1]!;
|
||||
match = /^Digit([0-9])$/.exec(code);
|
||||
if (match) return match[1]!;
|
||||
match = /^Numpad(.+)$/.exec(code);
|
||||
if (match) return `Numpad ${match[1]}`;
|
||||
match = /^Usage([0-9a-f]+)$/.exec(code);
|
||||
if (match) return `Key 0x${match[1]}`;
|
||||
match = /^Consumer([0-9a-f]+)$/.exec(code);
|
||||
if (match) return `Media key 0x${match[1]}`;
|
||||
return code;
|
||||
}
|
||||
|
||||
/** A chord for display: "Shift+Esc", "Left", "Volume Up". */
|
||||
export function describeChord(chord: KeyChord): string {
|
||||
return [chord.ctrl && "Ctrl", chord.alt && "Alt", chord.shift && "Shift", chord.meta && "Meta", codeLabel(chord.code)]
|
||||
.filter(Boolean).join("+");
|
||||
}
|
||||
|
||||
/** The most custom keybindings kept; far more than a keyboard has uses for. */
|
||||
export const MAX_KEY_BINDINGS = 64;
|
||||
|
||||
/**
|
||||
* Custom keybindings from their stored JSON. Lenient: whatever doesn't make
|
||||
* sense is dropped, and a later entry for a chord replaces an earlier one.
|
||||
*/
|
||||
export function parseKeyBindings(raw: string): KeyBinding[] {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = raw ? JSON.parse(raw) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
const bindings = new Map<string, KeyBinding>();
|
||||
for (const entry of parsed) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const { key, action } = entry as { key?: unknown; action?: unknown };
|
||||
const chord = typeof key === "string" ? parseChordId(key) : null;
|
||||
if (!chord || !isKeyAction(action)) continue;
|
||||
bindings.set(chordId(chord), { chord, action });
|
||||
if (bindings.size >= MAX_KEY_BINDINGS) break;
|
||||
}
|
||||
return [...bindings.values()];
|
||||
}
|
||||
|
||||
export function serializeKeyBindings(bindings: readonly KeyBinding[]): string {
|
||||
return JSON.stringify(bindings.map((binding) => ({ key: chordId(binding.chord), action: binding.action })));
|
||||
}
|
||||
|
||||
/** Custom keybindings as the lookup resolveKey takes. */
|
||||
export function keyBindingMap(bindings: readonly KeyBinding[]): Map<string, KeyAction> {
|
||||
return new Map(bindings.map((binding) => [chordId(binding.chord), binding.action]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The built-in meaning of a key press, or null when it has none.
|
||||
* `overridesTyping`: it holds even where the focused window takes keys.
|
||||
* Built-ins go by the logical key, so both Enter keys select.
|
||||
*/
|
||||
export function defaultKeyAction(event: KeyEvent): { action: KeyGesture; overridesTyping: boolean } | null {
|
||||
const { shift, ctrl, alt, meta } = event.modifiers;
|
||||
if (meta) return null;
|
||||
if (event.key === "Escape") {
|
||||
// Plain Escape belongs to whatever takes keys (it is a keystroke in a
|
||||
// terminal); with a modifier it always means "back out of here".
|
||||
return { action: "double-click", overridesTyping: shift || ctrl || alt };
|
||||
}
|
||||
if (ctrl || alt) return null;
|
||||
switch (event.key) {
|
||||
case "ArrowUp":
|
||||
return { action: "swipe-up", overridesTyping: false };
|
||||
case "ArrowDown":
|
||||
return { action: "swipe-down", overridesTyping: false };
|
||||
case "ArrowLeft":
|
||||
return { action: "swipe-left", overridesTyping: false };
|
||||
case "ArrowRight":
|
||||
return { action: "swipe-right", overridesTyping: false };
|
||||
case "Enter":
|
||||
return { action: "click", overridesTyping: false };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The built-ins, for showing to the user. Keep in step with defaultKeyAction. */
|
||||
export const DEFAULT_KEYS_HELP =
|
||||
"Arrow keys: swipe up, down, left and right, as on the watch. Enter: select. Esc: back.\n\n" +
|
||||
"In a terminal session those keys are typed instead, and Shift+Esc (or Ctrl+Esc, Alt+Esc) goes back.\n\n" +
|
||||
"A custom keybinding replaces these for its key and works everywhere, terminals included. " +
|
||||
"\"Nothing\" turns a key's meaning here off without stopping it being typed.";
|
||||
|
||||
/**
|
||||
* Whether holding the key repeats the gesture. Moving up and down a list
|
||||
* does. Sideways swipes don't: depending on the layout they enter and leave
|
||||
* things, and a held key would flap between the two. Selecting, backing out
|
||||
* and menus act once per press.
|
||||
*/
|
||||
export function gestureRepeats(gesture: KeyGesture): boolean {
|
||||
return gesture === "scroll-up" || gesture === "scroll-down" || gesture === "swipe-up" || gesture === "swipe-down";
|
||||
}
|
||||
|
||||
export type KeyResolution = {
|
||||
/** The key is spoken for: don't offer it to the focused window. */
|
||||
consume: boolean;
|
||||
/**
|
||||
* The gesture to perform: at once when `consume`, otherwise only if no
|
||||
* window takes the key. Null for none (including a held key's repeats of
|
||||
* a gesture that doesn't repeat).
|
||||
*/
|
||||
gesture: KeyGesture | null;
|
||||
};
|
||||
|
||||
/** What one key press (or auto-repeat) means, given the custom keybindings. */
|
||||
export function resolveKey(event: KeyEvent, custom: ReadonlyMap<string, KeyAction>): KeyResolution {
|
||||
const repeatable = (gesture: KeyGesture): KeyGesture | null =>
|
||||
event.repeat && !gestureRepeats(gesture) ? null : gesture;
|
||||
const bound = custom.get(chordId(chordOf(event)));
|
||||
if (bound !== undefined && bound !== "none") return { consume: true, gesture: repeatable(bound) };
|
||||
// "none" takes the built-in meaning away and leaves the key to be typed.
|
||||
if (bound === "none") return { consume: false, gesture: null };
|
||||
const builtIn = defaultKeyAction(event);
|
||||
if (!builtIn) return { consume: false, gesture: null };
|
||||
return { consume: builtIn.overridesTyping, gesture: repeatable(builtIn.action) };
|
||||
}
|
||||
|
||||
export type GestureOrigin = "ring" | "watch";
|
||||
|
||||
/**
|
||||
* Which device a keyboard's gesture is presented as. Swipes are the watch's
|
||||
* and scrolls the ring's; the two schemes differ in places (an icon grid
|
||||
* moves by cell for the watch, by row and then cell for the ring). A tap, a
|
||||
* double tap or a hold exists in both, so it goes with whichever the
|
||||
* keyboard last moved with: arrow keys make the keyboard a watch, a remote
|
||||
* bound to scroll makes it a ring. Before any movement, the watch.
|
||||
*/
|
||||
export class GestureScheme {
|
||||
private scheme: GestureOrigin = "watch";
|
||||
|
||||
originFor(gesture: KeyGesture): GestureOrigin {
|
||||
if (gesture.startsWith("swipe-")) this.scheme = "watch";
|
||||
else if (gesture.startsWith("scroll-")) this.scheme = "ring";
|
||||
return this.scheme;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Where a key from the hardware keyboard goes: to a keybinding being set up,
|
||||
* to a gesture it is bound to, to the window with input focus, or to the
|
||||
* built-in navigation. The meanings are in bindings.ts; this is the order of
|
||||
* asking, and the bookkeeping around it. Shared by the Android and iOS
|
||||
* controllers through KeyRouterHost.
|
||||
* No platform imports: this runs under node in the tests.
|
||||
*/
|
||||
import {
|
||||
GestureScheme,
|
||||
chordOf,
|
||||
isModifierCode,
|
||||
resolveKey,
|
||||
type GestureOrigin,
|
||||
type KeyAction,
|
||||
type KeyChord,
|
||||
type KeyGesture,
|
||||
} from "./bindings";
|
||||
import type { KeyEvent } from "./keys";
|
||||
|
||||
export type KeyRouterHost = {
|
||||
/** Connected to glasses that are in use. */
|
||||
ready(): boolean;
|
||||
locked(): boolean;
|
||||
screenOn(): boolean;
|
||||
/** Turn a dark display on, and nothing else. */
|
||||
wake(): void;
|
||||
/** Offer a key to the window with input focus; true when that window takes keys. */
|
||||
offerToWindow(event: KeyEvent): boolean;
|
||||
/** Perform a gesture as if it came from the ring or the watch. */
|
||||
gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>;
|
||||
/** The custom keybindings, by chord id. */
|
||||
bindings(): ReadonlyMap<string, KeyAction>;
|
||||
/** A key was used for something other than a gesture or typing (count it against the screen timeout). */
|
||||
noteActivity(): void;
|
||||
log(line: string): void;
|
||||
now?(): number;
|
||||
};
|
||||
|
||||
/**
|
||||
* A held key repeats twenty times a second, faster than a list can repaint
|
||||
* over the glasses' link. Repeats are dropped while a gesture is still being
|
||||
* handled, and closer together than this.
|
||||
*/
|
||||
const REPEAT_MIN_INTERVAL_MS = 100;
|
||||
|
||||
export class KeyRouter {
|
||||
private capture: ((chord: KeyChord) => void) | null = null;
|
||||
/** Keys whose press was spoken for, so their release is too. */
|
||||
private readonly consumed = new Set<string>();
|
||||
private readonly scheme = new GestureScheme();
|
||||
private gesturesInFlight = 0;
|
||||
private lastGestureAtMs = 0;
|
||||
|
||||
constructor(private readonly host: KeyRouterHost) {}
|
||||
|
||||
/** One key transition from the keyboard. */
|
||||
handleKey(event: KeyEvent): void {
|
||||
const host = this.host;
|
||||
if (!host.ready()) return;
|
||||
if (host.locked() || !host.screenOn()) {
|
||||
// A dark display takes no typing: the first press only wakes it, as
|
||||
// the ring's double tap would, and a locked one stays locked.
|
||||
if (event.phase === "down" && !event.repeat && !host.screenOn()) host.wake();
|
||||
return;
|
||||
}
|
||||
if (this.capture) {
|
||||
this.captureKey(event);
|
||||
return;
|
||||
}
|
||||
if (event.phase === "up") {
|
||||
if (!this.consumed.delete(event.code)) host.offerToWindow(event);
|
||||
return;
|
||||
}
|
||||
const resolution = resolveKey(event, host.bindings());
|
||||
if (resolution.consume) {
|
||||
this.consumed.add(event.code);
|
||||
if (resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
return;
|
||||
}
|
||||
if (host.offerToWindow(event)) return;
|
||||
if (resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
}
|
||||
|
||||
/**
|
||||
* A key the focused window was offered and had no use for (a terminal
|
||||
* showing its session list rather than a session): it gets the built-in
|
||||
* navigation meaning after all.
|
||||
*/
|
||||
handleUnhandledKey(event: KeyEvent): void {
|
||||
const host = this.host;
|
||||
if (event.phase !== "down" || this.capture || !host.ready() || host.locked() || !host.screenOn()) return;
|
||||
const resolution = resolveKey(event, host.bindings());
|
||||
if (!resolution.consume && resolution.gesture) this.perform(resolution.gesture, event.repeat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the next key press instead of acting on it (setting up a
|
||||
* keybinding). Modifiers held with it are part of the chord; pressed alone
|
||||
* they are waited through. Returns a function that calls the capture off.
|
||||
*/
|
||||
captureNextKey(onChord: (chord: KeyChord) => void): () => void {
|
||||
this.capture = onChord;
|
||||
return () => {
|
||||
if (this.capture === onChord) this.capture = null;
|
||||
};
|
||||
}
|
||||
|
||||
/** The keyboard went away: nothing is held any more. */
|
||||
reset(): void {
|
||||
this.consumed.clear();
|
||||
}
|
||||
|
||||
private captureKey(event: KeyEvent): void {
|
||||
this.host.noteActivity();
|
||||
if (event.phase === "up") {
|
||||
this.consumed.delete(event.code);
|
||||
return;
|
||||
}
|
||||
if (event.repeat || isModifierCode(event.code)) return;
|
||||
const capture = this.capture!;
|
||||
this.capture = null;
|
||||
this.consumed.add(event.code);
|
||||
capture(chordOf(event));
|
||||
}
|
||||
|
||||
private perform(gesture: KeyGesture, repeat: boolean): void {
|
||||
const now = this.host.now ? this.host.now() : Date.now();
|
||||
if (repeat && (this.gesturesInFlight > 0 || now - this.lastGestureAtMs < REPEAT_MIN_INTERVAL_MS)) return;
|
||||
this.lastGestureAtMs = now;
|
||||
void this.run(gesture, this.scheme.originFor(gesture));
|
||||
}
|
||||
|
||||
private async run(gesture: KeyGesture, origin: GestureOrigin): Promise<void> {
|
||||
this.gesturesInFlight++;
|
||||
try {
|
||||
await this.host.gesture(gesture, origin);
|
||||
} catch (error) {
|
||||
this.host.log(`keyboard gesture ${gesture} failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
} finally {
|
||||
this.gesturesInFlight--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,33 +283,3 @@ export class KeyTranslator {
|
||||
this.repeating = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The ring gestures a keyboard can stand in for. */
|
||||
export type KeyNavigationGesture = "scroll-up" | "scroll-down" | "click" | "double-click";
|
||||
|
||||
/**
|
||||
* What a key does where nothing takes keys as keys: the ring's vocabulary,
|
||||
* with the same reading of left and right as a watch swipe (right selects,
|
||||
* left backs out). Held arrows keep scrolling; Enter and Escape act once per
|
||||
* press. Chords and every other key do nothing. This is a deliberately small
|
||||
* default, not a button-mapping scheme.
|
||||
*/
|
||||
export function navigationGestureForKey(event: KeyEvent): KeyNavigationGesture | null {
|
||||
if (event.phase !== "down") return null;
|
||||
const { ctrl, alt, meta } = event.modifiers;
|
||||
if (ctrl || alt || meta) return null;
|
||||
switch (event.key) {
|
||||
case "ArrowUp":
|
||||
return "scroll-up";
|
||||
case "ArrowDown":
|
||||
return "scroll-down";
|
||||
case "Enter":
|
||||
case "ArrowRight":
|
||||
return event.repeat ? null : "click";
|
||||
case "Escape":
|
||||
case "ArrowLeft":
|
||||
return event.repeat ? null : "double-click";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,29 @@
|
||||
/**
|
||||
* The keyboard bridge as the app uses it: one KeyboardBridge for the life of
|
||||
* the app, fed by whichever glasses session is live, its remembered keyboard
|
||||
* kept in the settings store, and its key transitions turned into KeyEvents.
|
||||
* The dashboard controller reports the link coming and going and takes the
|
||||
* keys; the Settings app drives pairing through `bridge`.
|
||||
* and the custom keybindings kept in the settings store, and its key
|
||||
* transitions turned into KeyEvents and routed. The platform's controller
|
||||
* reports the link coming and going and performs the gestures keys stand
|
||||
* for; the Settings app drives pairing through `bridge` and edits the
|
||||
* keybindings.
|
||||
*/
|
||||
import { addKeyboardEventListener, keyboardLinkSession, sendKeyboardControlRecord } from "../../native/keyboard-link";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import {
|
||||
MAX_KEY_BINDINGS,
|
||||
chordId,
|
||||
keyBindingMap,
|
||||
parseKeyBindings,
|
||||
serializeKeyBindings,
|
||||
type GestureOrigin,
|
||||
type KeyAction,
|
||||
type KeyBinding,
|
||||
type KeyChord,
|
||||
type KeyGesture,
|
||||
} from "./bindings";
|
||||
import { KeyboardBridge, type KeyboardBridgeSnapshot, type SavedKeyboard } from "./bridge";
|
||||
import { KeyRouter } from "./key-router";
|
||||
import { KeyTranslator, type KeyEvent } from "./keys";
|
||||
|
||||
declare const java: any;
|
||||
@@ -15,9 +31,18 @@ declare const arc4random_uniform: ((bound: number) => number) | undefined;
|
||||
|
||||
/** JSON of the remembered keyboard ({ address, addressType, name }), or "" for none. */
|
||||
const SAVED_KEYBOARD_KEY = "keyboard.saved";
|
||||
/** JSON list of the custom keybindings ({ key, action }); see bindings.ts. */
|
||||
const KEY_BINDINGS_KEY = "keyboard.bindings";
|
||||
|
||||
/** What the platform's controller does for the keyboard. */
|
||||
export type KeyboardServiceHost = {
|
||||
onKey(event: KeyEvent): void;
|
||||
/** Connected to glasses that are in use (not charging in their case). */
|
||||
ready(): boolean;
|
||||
locked(): boolean;
|
||||
/** Turn a dark display on, and nothing else (a locked display stays locked). */
|
||||
wake(): void;
|
||||
/** Perform a gesture as if it came from the ring or the watch. */
|
||||
gesture(gesture: KeyGesture, origin: GestureOrigin): Promise<void>;
|
||||
/** Routine lines only (failed background reconnects); the rest go to the device log directly. */
|
||||
log(line: string): void;
|
||||
/** Whether an absent keyboard is worth looking for now (see KeyboardBridgeHost). */
|
||||
@@ -52,7 +77,21 @@ class KeyboardService {
|
||||
private session: unknown = null;
|
||||
private offEvents: (() => void) | null = null;
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private readonly translator = new KeyTranslator({ emit: (event) => this.host?.onKey(event) });
|
||||
private bindings: KeyBinding[] = parseKeyBindings(getStringSetting(KEY_BINDINGS_KEY, ""));
|
||||
private bindingMap = keyBindingMap(this.bindings);
|
||||
|
||||
private readonly router = new KeyRouter({
|
||||
ready: () => this.host?.ready() ?? false,
|
||||
locked: () => this.host?.locked() ?? false,
|
||||
screenOn: () => shell.isScreenOn(),
|
||||
wake: () => this.host?.wake(),
|
||||
offerToWindow: (event) => shell.receiveKeyEvent(event),
|
||||
gesture: (gesture, origin) => this.host?.gesture(gesture, origin) ?? Promise.resolve(),
|
||||
bindings: () => this.bindingMap,
|
||||
noteActivity: () => shell.noteUserActivity(),
|
||||
log: (line) => console.log(`[keyboard] ${line}`),
|
||||
});
|
||||
private readonly translator = new KeyTranslator({ emit: (event) => this.router.handleKey(event) });
|
||||
|
||||
readonly bridge = new KeyboardBridge({
|
||||
send: (record) => {
|
||||
@@ -67,7 +106,10 @@ class KeyboardService {
|
||||
loadSaved: loadSavedKeyboard,
|
||||
storeSaved: (saved) => setStringSetting(SAVED_KEYBOARD_KEY, saved ? JSON.stringify(saved) : ""),
|
||||
onKeys: (events) => this.translator.handle(events),
|
||||
onKeyboardGone: () => this.translator.reset(),
|
||||
onKeyboardGone: () => {
|
||||
this.translator.reset();
|
||||
this.router.reset();
|
||||
},
|
||||
onChanged: () => {
|
||||
for (const listener of [...this.listeners]) listener();
|
||||
},
|
||||
@@ -126,6 +168,49 @@ class KeyboardService {
|
||||
this.offEvents = null;
|
||||
this.session = null;
|
||||
}
|
||||
|
||||
/** A key a worker's window was sent and had no use for (the "key-unhandled" reply). */
|
||||
handleUnhandledKey(event: KeyEvent): void {
|
||||
this.router.handleUnhandledKey(event);
|
||||
}
|
||||
|
||||
// ---------- Custom keybindings ----------
|
||||
|
||||
/** The custom keybindings, in the order they were added. */
|
||||
keyBindings(): readonly KeyBinding[] {
|
||||
return this.bindings;
|
||||
}
|
||||
|
||||
/** Bind a chord, replacing what it was bound to. False when the list is full. */
|
||||
setKeyBinding(chord: KeyChord, action: KeyAction): boolean {
|
||||
const id = chordId(chord);
|
||||
const index = this.bindings.findIndex((binding) => chordId(binding.chord) === id);
|
||||
if (index < 0 && this.bindings.length >= MAX_KEY_BINDINGS) return false;
|
||||
const next = [...this.bindings];
|
||||
if (index < 0) next.push({ chord, action });
|
||||
else next[index] = { chord, action };
|
||||
this.storeBindings(next);
|
||||
return true;
|
||||
}
|
||||
|
||||
removeKeyBinding(chord: KeyChord): void {
|
||||
const id = chordId(chord);
|
||||
this.storeBindings(this.bindings.filter((binding) => chordId(binding.chord) !== id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the next key pressed on the keyboard instead of acting on it. Returns
|
||||
* a function that calls the capture off.
|
||||
*/
|
||||
captureNextKey(onChord: (chord: KeyChord) => void): () => void {
|
||||
return this.router.captureNextKey(onChord);
|
||||
}
|
||||
|
||||
private storeBindings(bindings: KeyBinding[]): void {
|
||||
this.bindings = bindings;
|
||||
this.bindingMap = keyBindingMap(bindings);
|
||||
setStringSetting(KEY_BINDINGS_KEY, serializeKeyBindings(bindings));
|
||||
}
|
||||
}
|
||||
|
||||
export const keyboardService = new KeyboardService();
|
||||
Reference in new issue
Block a user