Skip to content

Commit

Permalink
Add XON/XOFF and eparate write from processing
Browse files Browse the repository at this point in the history
Part of xtermjs#425
  • Loading branch information
Tyriar committed Jan 3, 2017
1 parent 1fbd3c8 commit 500fb38
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ app.ws('/terminals/:pid', function (ws, req) {

term.on('data', function(data) {
try {
// XOFF - stop pty pipe
// XON will be triggered by emulator before processing data chunk
term.write('\x13');
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
Expand Down
37 changes: 37 additions & 0 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ function Terminal(options) {
this.prefix = '';
this.postfix = '';

// user input states
this.writeBuffer = [];
this.writeInProgress = false;
this.user_xoff = false; // user pressed XOFF

// leftover surrogate high from previous write invocation
this.surrogate_high = '';

Expand Down Expand Up @@ -1341,8 +1346,27 @@ Terminal.prototype.scrollToBottom = function() {
* @param {string} text The text to write to the terminal.
*/
Terminal.prototype.write = function(data) {
this.writeBuffer.push(data);
if (!this.writeInProgress) {
// Kick off a write which will write all data in sequence recursively
this.writeInProgress = true;
// Kick off an async innerWrite so more writes can come in while processing data
setTimeout(() => this.innerWrite(this.writeBuffer.shift()));
}
}

Terminal.prototype.innerWrite = function(data) {
var l = data.length, i = 0, j, cs, ch, code, low, ch_width, row;

// TODO: Need to have another buffer where data is held where write can grab lines from
// When this hits a certain threshold it should send this.write('\x13')

// XON - about to process data, thus we can get more
// dont lift XOFF if user pressed it
if (!this.user_xoff) {
this.send('\x11');
}

this.refreshStart = this.y;
this.refreshEnd = this.y;

Expand Down Expand Up @@ -2380,6 +2404,12 @@ Terminal.prototype.write = function(data) {

this.updateRange(this.y);
this.refresh(this.refreshStart, this.refreshEnd);

if (this.writeBuffer.length > 0) {
this.innerWrite(this.writeBuffer.shift());
} else {
this.writeInProgress = false;
}
};

/**
Expand Down Expand Up @@ -2423,6 +2453,12 @@ Terminal.prototype.keyDown = function(ev) {
var self = this;
var result = this.evaluateKeyEscapeSequence(ev);

if (result.key === '\x13') { // XOFF
this.user_xoff = true;
} else if (result.key === '\x11') { // XON
this.user_xoff = false;
}

if (result.scrollDisp) {
this.scrollDisp(result.scrollDisp);
return this.cancel(ev, true);
Expand Down Expand Up @@ -2728,6 +2764,7 @@ Terminal.prototype.evaluateKeyEscapeSequence = function(ev) {
}
break;
}

return result;
};

Expand Down

0 comments on commit 500fb38

Please sign in to comment.