Skip to content

Commit 08779ef

Browse files
authored
feat: input parser recognizes probe responses (input-spec §6)
1 parent 00aee53 commit 08779ef

3 files changed

Lines changed: 492 additions & 13 deletions

File tree

‎input-native.ts‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@ export const EVENT_KEY = 1;
22
export const EVENT_MOUSE = 2;
33
export const EVENT_RESIZE = 3;
44
export const EVENT_CURSOR = 4;
5+
export const EVENT_CAPABILITY = 5;
56

67
export const MOD_ALT = 1;
78
export const MOD_CTRL = 2;
89
export const MOD_SHIFT = 4;
910
export const MOD_MOTION = 8;
1011
export const MOD_RELEASE = 16;
1112

13+
/* Capability key constants — must match CAP_* in src/input.h */
14+
export const CAP_FOREGROUND_COLOR = 1;
15+
export const CAP_BACKGROUND_COLOR = 2;
16+
export const CAP_CURSOR_COLOR = 3;
17+
export const CAP_COLORDEPTH = 4;
18+
export const CAP_SYNC_OUTPUT = 5;
19+
export const CAP_KITTY_KEYBOARD = 6;
20+
export const CAP_KITTY_GRAPHICS = 7;
21+
export const CAP_POINTER_SHAPE = 8;
22+
23+
/* CAP_COLORDEPTH ch values — must match COLORDEPTH_* in src/input.c */
24+
export const COLORDEPTH_16 = 0;
25+
export const COLORDEPTH_256 = 1;
26+
export const COLORDEPTH_TRUECOLOR = 2;
27+
1228
export const KEY_F1 = 0xFFFF;
1329
export const KEY_F2 = 0xFFFE;
1430
export const KEY_F3 = 0xFFFD;
@@ -173,6 +189,8 @@ import { compiled } from "./wasm.ts";
173189

174190
export async function createInputNative(
175191
escLatency: number,
192+
keys?: Uint8Array,
193+
initialColors?: number,
176194
): Promise<InputNative> {
177195
let memory = new WebAssembly.Memory({ initial: 4 });
178196

@@ -191,7 +209,13 @@ export async function createInputNative(
191209
let exports = instance.exports as unknown as {
192210
__heap_base: WebAssembly.Global;
193211
input_size(): number;
194-
input_init(mem: number, escLatency: number): number;
212+
input_init(
213+
mem: number,
214+
escLatency: number,
215+
terminfo: number,
216+
terminfoLen: number,
217+
initialColors: number,
218+
): number;
195219
input_scan(st: number, buf: number, len: number, now: number): number;
196220
input_count(st: number): number;
197221
input_event(st: number, index: number): number;
@@ -200,8 +224,29 @@ export async function createInputNative(
200224

201225
let heap = exports.__heap_base.value as number;
202226
let size = exports.input_size();
203-
let state = exports.input_init(heap, escLatency);
204-
let buffer = (heap + size + 7) & ~7;
227+
228+
let keysPtr = 0;
229+
let keysLen = 0;
230+
let top = (heap + 7) & ~7;
231+
if (keys && keys.byteLength > 0) {
232+
top = (top + 7) & ~7;
233+
keysPtr = top;
234+
keysLen = keys.byteLength;
235+
top += (keysLen + 7) & ~7;
236+
let pages = Math.ceil(top / 65536);
237+
let current = memory.buffer.byteLength / 65536;
238+
if (pages > current) memory.grow(pages - current);
239+
new Uint8Array(memory.buffer).set(keys, keysPtr);
240+
}
241+
242+
let state = exports.input_init(
243+
top,
244+
escLatency,
245+
keysPtr,
246+
keysLen,
247+
initialColors ?? 256,
248+
);
249+
let buffer = (top + size + 7) & ~7;
205250

206251
return {
207252
memory,
@@ -214,11 +259,6 @@ export async function createInputNative(
214259
};
215260
}
216261

217-
// Compiled terminfo entries are limited to 4096 bytes (legacy) or 32768
218-
// bytes (extended ncurses format). We use the extended limit as our upper
219-
// bound. See https://man7.org/linux/man-pages/man5/term.5.html
220-
export const MAX_TERMINFO = 32768;
221-
222262
// Must match SCAN_BUFFER_SIZE in input.c — the maximum bytes input_scan()
223263
// can accept in a single call.
224264
export const SCAN_BUFFER_SIZE = 4096;

0 commit comments

Comments
 (0)