From e1ec84877ac8dc6b5d63e9cf5464beae94f7cbdf Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sun, 8 Sep 2024 23:52:45 -0400 Subject: [PATCH] dev: resize handling --- src/emulator/src/main.js | 24 +++++++++++++++++++ src/phoenix/src/ansi-shell/ANSIShell.js | 1 + .../providers/PuterAppCommandProvider.js | 15 +++++++++++- src/puter-wisp/src/exports.js | 14 +++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/emulator/src/main.js b/src/emulator/src/main.js index 6ef3b3dc..633d0a0d 100644 --- a/src/emulator/src/main.js +++ b/src/emulator/src/main.js @@ -108,9 +108,20 @@ window.onload = async function() on: function () { const pty = this.getPTY(); console.log('PTY created', pty); + + // resize + ptt.on('ioctl.set', evt => { + console.log('event?', evt); + pty.resize(evt.windowSize); + }); + ptt.TIOCGWINSZ(); + + // data from PTY pty.on_payload = data => { ptt.out.write(data); } + + // data to PTY (async () => { // for (;;) { // const buff = await ptt.in.read(); @@ -205,6 +216,19 @@ window.onload = async function() { data, direction: WispPacket.SEND }); this.client.send(packet); } + + resize ({ rows, cols }) { + console.log('resize called!'); + const data = new DataBuilder({ leb: true }) + .uint8(0xf0) + .uint32(this.streamId) + .uint16(rows) + .uint16(cols) + .build(); + const packet = new WispPacket( + { data, direction: WispPacket.SEND }); + this.client.send(packet); + } } const ptyMgr = new PTYManager({ diff --git a/src/phoenix/src/ansi-shell/ANSIShell.js b/src/phoenix/src/ansi-shell/ANSIShell.js index 580ba2b7..4d9cd47f 100644 --- a/src/phoenix/src/ansi-shell/ANSIShell.js +++ b/src/phoenix/src/ansi-shell/ANSIShell.js @@ -219,6 +219,7 @@ export class ANSIShell extends EventTarget { } const executionCtx = this.ctx.sub({ + shell: this, vars: this.variables, env: this.env, locals: { diff --git a/src/phoenix/src/puter-shell/providers/PuterAppCommandProvider.js b/src/phoenix/src/puter-shell/providers/PuterAppCommandProvider.js index 20149893..ff888975 100644 --- a/src/phoenix/src/puter-shell/providers/PuterAppCommandProvider.js +++ b/src/phoenix/src/puter-shell/providers/PuterAppCommandProvider.js @@ -60,6 +60,17 @@ export class PuterAppCommandProvider { }; const child = await puter.ui.launchApp(id, args); + const resize_listener = evt => { + child.postMessage({ + $: 'ioctl.set', + windowSize: { + rows: evt.detail.rows, + cols: evt.detail.cols, + } + }); + }; + ctx.shell.addEventListener('signal.window-resize', resize_listener); + // Wait for app to close. const app_close_promise = new Promise((resolve, reject) => { child.on('close', (data) => { @@ -118,7 +129,9 @@ export class PuterAppCommandProvider { } // TODO: propagate sigint to the app - return Promise.race([ app_close_promise, sigint_promise ]); + const exit = await Promise.race([ app_close_promise, sigint_promise ]); + ctx.shell.removeEventListener('signal.window-resize', resize_listener); + return exit; } }; } diff --git a/src/puter-wisp/src/exports.js b/src/puter-wisp/src/exports.js index 39f06e0d..88b50283 100644 --- a/src/puter-wisp/src/exports.js +++ b/src/puter-wisp/src/exports.js @@ -223,6 +223,20 @@ const wisp_types = [ } } }, + { + // TODO: extension types should not be hardcoded here + id: 0xf0, + label: 'RESIZE', + describe: ({ attributes }) => { + return `${attributes.cols}x${attributes.rows}`; + }, + getAttributes ({ payload }) { + return { + rows: lib.get_int(2, payload), + cols: lib.get_int(2, payload.slice(2)), + } + } + }, ]; class WispPacket {