Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/beep.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ played.) If the attempt to call 'play' fails, we flash the screen. */

static void _raw_beep( void)
{
#ifdef __EMSCRIPTEN__
/* The browser has no processes, so forking 'play' cannot work (and the
failed fork would pull Emscripten's libc stub object into the link). */
flash( );
#else
if( !fork( )) /* we're the child; beep at 400 Hz for 0.2 second */
{
execlp( "play", "play", "-q", "-V0", "-n", "synth", "0.2", "sin", "400",
Expand All @@ -53,6 +58,7 @@ static void _raw_beep( void)
flash( ); /* so we'll just flash the screen instead */
exit( 0);
}
#endif
}
#endif

Expand Down
41 changes: 41 additions & 0 deletions vt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,47 @@ For DOS/Windows, makefiles for Borland, Digital Mars, MSVC, and
OpenWATCOM are provided, but are at the 'it works on my machine' stage,
and haven't really been thoroughly vetted by others yet.

Building for Emscripten / the browser
-------------------------------------

The VT port also builds under Emscripten and renders into an xterm.js
terminal, or any other terminal emulator driven through a PTY-style
JavaScript bridge such as xterm-pty. Build the library with the wide
character API and a 32-bit chtype:

```sh
mkdir build && cd build
emcc -O2 -std=gnu99 -D_GNU_SOURCE -DPDC_WIDE -DCHTYPE_32 \
-I.. -c ../pdcurses/*.c ../vt/*.c
emar rcs libpdcurses.a *.o
```

The 32-bit chtype matters for programs that keep color pairs in an `int`
(Angband's attribute tables, for example); the default 64-bit chtype does
not fit there. With `PDC_WIDE`, wide strings are written as UTF-8, which
is what the browser expects.

Link applications with Asyncify enabled, because `PDC_napms()` hands its
wait to the browser event loop instead of blocking it:

```sh
emcc app.c libpdcurses.a -sASYNCIFY=1 -o app.js
```

At run time, the terminal size is read from the PTY with `TIOCGWINSZ`, so
the program sees the size the xterm.js terminal had at startup. Setting
`TERM=xterm-256color` gives the full 256-color palette; `PDC_VT=RGB`
requests true color, as on a native terminal.

Some POSIX facilities have no browser equivalent:

- Forking a sound player for the bell: the port just flashes the screen.
- `SIGWINCH`: a resize of the xterm.js window is not delivered to the
program, so the geometry stays fixed for its lifetime.
- Clipboard functions, which are absent on this platform anyway.
- The terminfo and termcap functions remain the usual stubs; the VT port
uses fixed ANSI sequences.

Caveats
-------

Expand Down
11 changes: 11 additions & 0 deletions vt/pdcutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#elif !defined( DOS)
#include <unistd.h>
#include <stdlib.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#endif
#include "curspriv.h"

Expand Down Expand Up @@ -56,7 +59,15 @@ void PDC_napms(int ms)
#else
if( remains > 0)
{
#ifdef __EMSCRIPTEN__
/* usleep() blocks the browser's event loop, which freezes
keyboard input and timers for the duration of the nap.
emscripten_sleep() yields to it instead. This requires the
program to be linked with -sASYNCIFY. */
emscripten_sleep( remains);
#else
usleep( 1000 * remains);
#endif
t = PDC_millisecs( );
}
#endif
Expand Down
Loading