Skip to content

A TTY support by using xterm.js #16352

Open

Description

@mame

(This is not a bug report, but a feature proposal. I apologize if this is not the right place.)

I have created a prototype that runs several Emscripten'ed CUI programs with xterm.js.

https://xterm-pty.netlify.app/

xterm-pty is an addon for xterm.js to work as a PTY layer. By connecting its "slave" interface of the PTY with the TTY interface of the Emscripten runtime, some real-world CUI programs like VIM works on recent browsers such as Google Chrome and Firefox.

To create this prototype, I had to hook some system calls and events in the Emscripten runtime: ioctl, select, and TTY read/write. Currently, the prototype uses very dirty hack to achieve these hooks.

As the hack highly depends on the internal of Emscripten runtime, it is a bit tough to maintain outside of Emscripten project. Is there any chance that Emscripten could import this kind of functionality?

Alternatively, I would appreciate it if Emscripten could provide some necessary Module APIs for the hooks, but the API design might be a bit tricky.

  • ioctl syscall accepts a pointer to the heap, and read/write the heap depending on its request type. It's not enough to have a hook that just notifies when ioctl is called.
  • select syscall needs to support timeout for real-world CUI programs. We may need to create an API for a polling mechanism.
  • I know that there are Module["stdin"] and Module["stdout"] to hook the stdio, but if we use them, isatty(0) returns 0, which is inconvenient. Instead, my prototype overwrites TTY.default_tty_ops.get_char to hook TTY read/write.

If you're not interested in this feature, feel free to close this issue.

Activity

  1. sbc100 commented on Feb 22, 2022

    @sbc100
    Collaborator

    This feature sounds great!

    I would hope you can do a lot of what you needs using a library JS file. e.g. library_xterm.js .. then anyone who wants to could grab you code and use it via --js-library library_xterm.js

  2. kjperkin commented on Jul 5, 2022

    @kjperkin

    I took a stab at a proof-of-concept (#17371) in my free time. I think there's still much to do. Unfortunately, I don't have tons of experience writing good JavaScript, and practically zero experience with implementing tests for JavaScript.

    Some goals I have in mind are:

    • Must have: Developers should be able to provide alternative implementations of basic TTY operations before stdin, stdout, and stderr are initialized, so that those streams use the supplied operations instead.
    • Should have: Existing code should be unaffected by the changes to stream setup.
    • Could have: Registering a TTY could, by default, create a device node for it under /dev, so developers don't have to bother
    • Should have: Developers should be able to register their own device implementations and create /dev/tty{,1} with them, in case they want to do something very unusual.
    • Developers should be able to supply their own devices for /dev/stdin, /dev/stdout, and /dev/stderr
  3. iliakan commented on Feb 15, 2023

    @iliakan
    Contributor

    @mame Is everything good on xterm side?

    In other words, if Emscripten is adapted, will the integration be all-good and fluent?

    I saw your demos, they look awesome, did you manage to fully integrate it, without hanging bugs/side-effects?

  4. mame commented on Jun 1, 2023

    @mame
    ContributorAuthor

    As a first step, I sent a PR #19499 to expose the syscall hooks that we would like Emscripten runtime to provide at a minimum.

    We are still a long way from getting it to work with --js-library library_xterm.js, but in the meantime, it will save xterm-pty from using the awful dirty hack.

  5. kjvalencik commented on Jun 30, 2023

    @kjvalencik

    This is really awesome and the demos look cool.

    I tried out xterm-pty with tintin but, it doesn't seem to be quite working. Sequences like up/down arrows are being echoed instead of handled.

    Since that's not the case with the vim demo, I was wondering what might be causing that?

  6. mame commented on Jul 1, 2023

    @mame
    ContributorAuthor

    @kjvalencik That should be discussed in https://github.com/mame/xterm-pty, not here. Could you file an issue with how to reproduce your problem, the expected result, and the actual result at https://github.com/mame/xterm-pty/issues/new?

  7. sbc100 commented on Jul 1, 2023

    @sbc100
    Collaborator

    @kjvalencik xterm-pty looks really great. I hope we can improve the integration over time to make it even smoother.

    Perhaps we could even add some kind of integrations directly into emscripten as an alternative/option for the default out.html for emscripten programs?

    A couple of notes from looking through the README. -sNO_EXIT_RUNTIME=1 (AKA -sEXIT_RUNTIME=0) is the default in emscripten so I don't think you need to ask folks to specify that settings. Also, I think the acronym normally used to refer to command line apps is CLI rather then CUI (I could wrong but that certainly seems to show a lot more on the web).

  8. kjvalencik commented on Jul 3, 2023

    @kjvalencik

    I spent some time reading over xterm-pty and the generated emscripten code and have a better understanding.

    For my use case, I need a TTY with lower level control and ran into the same issue as @mame. In the generated code, there's no way to override stdin and still having it as a TTY.

    I don't actually need super deep control and would like to defer to most of emscripten's implementation (e.g., UTF8 handling). I only need:

    • Override getChar (instead of using window.prompt) to read from my app's input buffer
    • Override poll so that select doesn't always return ready (causes an infinite loop)

    Editing the generated code to look for overrides on Module, this was sufficient for my use case:

    const bufs: string[] = [];
    
    await init({
        stdin: {
            getChar() {
                return bufs.shift();
            },
            poll() {
                // NOTE: This implementation is incomplete, but sufficient for my app
                return (bufs.length || stream.tty.input.length) ? 5 : 0;
            }
        },
    });
  9. mame commented on Jul 6, 2023

    @mame
    ContributorAuthor

    For my use case, I need a TTY with lower level control and ran into the same issue as @mame. In the generated code, there's no way to override stdin and still having it as a TTY.

    @kjvalencik If you need to replace only getchar and poll, there is a way, though it is not so good:

    TTY.default_tty_ops.get_char = yourGetcharFunction;
    TTY.stream_ops.poll = yourPollFunction;

    In fact, xterm-pty overrides those functions in such a way.

    It would be nice if there was a less-hacky way to override them, and I think it would be possible for just getchar and poll. But a more realistic program would need to intercept more complicated syscalls like ioctl. I think it would be difficult for emscripten to expose the API to allow to hook all IO-related syscalls. Rather, it would be more realistic to integrate xterm-pty itself into emscripten, if emscripten authors accept its integration.

    (I'm not quite sure what specific problem you are facing, so sorry if my comment is misguided.)

  10. versaloon commented on Aug 6, 2023

    @versaloon

    The demo is really cool, but failed to launch even if follow the instructions at "Emscripten integration" section, with errors below:
    emscriptenHack.ts:201 failed to overwrite __syscall_ioctl of emscripten
    e.emscriptenHack @ emscriptenHack.ts:201
    emscriptenHack.ts:205 failed to overwrite __syscall_newselect of emscripten
    e.emscriptenHack @ emscriptenHack.ts:205
    emscriptenHack.ts:208 Uncaught TypeError: Cannot read properties of undefined (reading 'dummy')
    at e.emscriptenHack (emscriptenHack.ts:208:30)
    at onmessage (worker.js:6:3)

    With some simple debug, I found that asmLibraryArg in invalid in emscriptenHack.ts.

    Is xterm-pty still compatible with the latest emscripten?

  11. leahanderson1 commented on Nov 17, 2023

    @leahanderson1

    The demo is really cool, but failed to launch even if follow the instructions at "Emscripten integration" section, with errors below: emscriptenHack.ts:201 failed to overwrite __syscall_ioctl of emscripten e.emscriptenHack @ emscriptenHack.ts:201 emscriptenHack.ts:205 failed to overwrite __syscall_newselect of emscripten e.emscriptenHack @ emscriptenHack.ts:205 emscriptenHack.ts:208 Uncaught TypeError: Cannot read properties of undefined (reading 'dummy') at e.emscriptenHack (emscriptenHack.ts:208:30) at onmessage (worker.js:6:3)

    With some simple debug, I found that asmLibraryArg in invalid in emscriptenHack.ts.

    Is xterm-pty still compatible with the latest emscripten?

    because of #19499 getting merged, I don't think you need xterm-pty anymore.

  12. leahanderson1 commented on Nov 17, 2023

    @leahanderson1

    nevermind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions