From a2afbe54bc9a2f3981d81e36d36903c3b089ca47 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 1 Feb 2015 09:21:07 -0800 Subject: [PATCH] detect bugged vte mouse better. see #89. --- lib/program.js | 24 ++++++++++++++++++++---- test/program-mouse.js | 9 +++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/program.js b/lib/program.js index 19c685f..8d10559 100644 --- a/lib/program.js +++ b/lib/program.js @@ -407,15 +407,31 @@ Program.prototype._bindMouse = function(s, buf) { // under 0x20, but since VTE can have the bytes overflow, we can consider // bytes below 0x20 to be up to 0xff + 0x20. This gives a limit of 287. Since // characters ranging from 223 to 248 confuse javascript's utf parser, we - // need to parse the raw binary. - if (this.isVTE && buf[0] === 0x1b && buf[1] === 0x5b && buf[2] === 0x4d) { + // need to parse the raw binary. We can detect whether the terminal is using + // a bugged VTE version by examining the coordinates and seeing whether they + // are a value they would never otherwise be with a properly implemented x10 + // protocol. This method of detecting VTE is only 99% reliable because we + // can't check if the coords are 0x00 (255) since that is a valid x10 coord + // technically. + var bx = s.charCodeAt(4); + var by = s.charCodeAt(5); + if (buf[0] === 0x1b && buf[1] === 0x5b && buf[2] === 0x4d + && (this.isVTE + || bx >= 65533 || by >= 65533 + || (bx > 0x00 && bx < 0x20) + || (by > 0x00 && by < 0x20) + || (buf[4] > 223 && buf[4] < 248 && buf.length === 6) + || (buf[5] > 223 && buf[5] < 248 && buf.length === 6))) { var b = buf[3] , x = buf[4] , y = buf[5]; - if (x < 0x20) x += 255; - if (y < 0x20) y += 255; + // unsigned char overflow. + if (x < 0x20) x += 0xff; + if (y < 0x20) y += 0xff; + // Convert the coordinates into a + // properly formatted x10 utf8 sequence. s = '\x1b[M' + String.fromCharCode(b) + String.fromCharCode(x) diff --git a/test/program-mouse.js b/test/program-mouse.js index 3280eec..067171b 100644 --- a/test/program-mouse.js +++ b/test/program-mouse.js @@ -1,4 +1,5 @@ var blessed = require('../') + , util = require('util') , program; program = blessed.program({ @@ -19,7 +20,7 @@ program.on('mouse', function(data) { program.cup(data.y, data.x); program.write(' ', 'blue bg'); program.cup(0, 0); - console.log(data); + program.write(util.inspect(data)); }); program.key(['q', 'escape', 'C-c'], function() { @@ -33,15 +34,15 @@ program.on('keypress', function(ch, data) { if (data.name === 'mouse') return; program.clear(); program.cup(0, 0); - console.log(data); + program.write(util.inspect(data)); }); program.on('mouse-debug', function(data) { program.cup(20, 0); data = Array.prototype.slice.call(data); - console.log(data); + program.write(util.inspect(data)); }); // program.getCursor(function(err, data) { -// console.log(data); +// program.write(util.inspect(data)); // });