Skip to content

Commit 7c57548

Browse files
committed
basic nonblocking input
1 parent 079e8d3 commit 7c57548

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

library_emterm.js

+19
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ mergeInto(LibraryManager.library, {
9696
}
9797
return i;
9898
},
99+
poll: function(stream) {
100+
if (!stream.tty) {
101+
throw new FS.ErrnoError({{{ cDefine('EBADF') }}});
102+
}
103+
var ret = 0;
104+
if (Module.tty && Module.tty.poll_out) {
105+
if (Module.tty.poll_out(stream.tty)) {
106+
ret |= {{{ cDefine('POLLOUT') }}};
107+
}
108+
} else {
109+
ret |= {{{ cDefine('POLLOUT') }}};
110+
}
111+
if (stream.tty.input.length) {
112+
ret |= {{{ cDefine('POLLIN') }}};
113+
} else if (Module.tty && Module.tty.poll_in && Module.tty.poll_in(stream.tty)) {
114+
ret |= {{{ cDefine('POLLIN') }}};
115+
}
116+
return ret;
117+
},
99118
ioctl: function(stream, op, argp) {
100119
if (!stream.tty) {
101120
throw new FS.ErrnoError({{{ cDefine('ENOTTY') }}});

shell_emterm.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@
8888
<script type='text/javascript'>
8989
const term = new Terminal({convertEol: true, scrollback: 0});
9090
const fitAddon = new FitAddon.FitAddon();
91+
var inputBuf = [];
9192
term.loadAddon(fitAddon);
9293
term.open(document.getElementById('output'));
9394
fitAddon.fit();
9495
window.addEventListener('resize', ()=>fitAddon.fit());
96+
term.onData(data=> inputBuf = inputBuf.concat(intArrayFromString(data)));
9597
function write(text) {
9698
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
9799
// These replacements are necessary if you render to raw HTML
@@ -109,7 +111,11 @@
109111
term.write(val);
110112
},
111113
get_char: function(tty) {
112-
return undefined;
114+
if (!inputBuf.length) return undefined;
115+
return inputBuf.shift();
116+
},
117+
poll_in: function(tty) {
118+
return inputBuf.length > 0;
113119
},
114120
get_winsize: function(tty, winsize) {
115121
winsize.ws_row = term.rows;

0 commit comments

Comments
 (0)