diff --git a/lib/keys.js b/lib/keys.js index 8950648..1a0853b 100644 --- a/lib/keys.js +++ b/lib/keys.js @@ -117,6 +117,12 @@ function emitKey(stream, s) { } } + // libvte sometimes sends mouse events in chunks. + // deal with this in a better way later. + if (process.env.VTE_VERSION) { + if (~s.indexOf('\x1b[M')) return; + } + key.sequence = s; if (s === '\r') { diff --git a/lib/program.js b/lib/program.js index 7f9b11d..9ead86c 100644 --- a/lib/program.js +++ b/lib/program.js @@ -355,13 +355,19 @@ Program.prototype.bindMouse = function() { , self = this; this.on('data', function(data) { - data = decoder.write(data); - if (!data) return; - self._bindMouse(data); + var text = decoder.write(data); + if (!text) return; + self._bindMouse(text, data); }); }; -Program.prototype._bindMouse = function(s) { +Program.saneMouse = /^\x1b\[M([\x00\u0020-\uffff]{3})/; +Program.vteMouse = /^\x1b\[M([\u0000-\uffff]{3})/; +Program.x10Mouse = !process.env.VTE_VERSION + ? Program.saneMouse + : Program.vteMouse; + +Program.prototype._bindMouse = function(s, buf) { var self = this , key , parts; @@ -383,12 +389,24 @@ Program.prototype._bindMouse = function(s) { } // XTerm / X10 - if (parts = /^\x1b\[M([\x00\u0020-\uffff]{3})/.exec(s)) { + if (parts = Program.x10Mouse.exec(s)) { var b = parts[1].charCodeAt(0) , x = parts[1].charCodeAt(1) , y = parts[1].charCodeAt(2) , mod; + if (process.env.VTE_VERSION) { + if (x > 65000) { + x = buf[4]; + } + + // if libvte goes over 255, it sends this. + // it can't go beyond 262 with this: + if (x > 0x00 && x < 0x20) { + x += 223; + } + } + key.name = 'mouse'; key.type = 'X10'; diff --git a/test/program-mouse.js b/test/program-mouse.js index b5daa0f..90e9eda 100644 --- a/test/program-mouse.js +++ b/test/program-mouse.js @@ -22,6 +22,14 @@ program.on('mouse', function(data) { console.log(data); }); +program.input.on('data', function(data) { + program.cup(10, 0); + program.write(JSON.stringify(data + '')); + program.nel(); + program.cr(); + program.write(JSON.stringify(Array.prototype.slice.call(data))); +}); + program.key(['q', 'escape', 'C-c'], function() { program.showCursor(); program.disableMouse(); @@ -32,6 +40,6 @@ program.key(['q', 'escape', 'C-c'], function() { program.on('keypress', function(ch, data) { if (data.name === 'mouse') return; program.clear(); - program.cup(0, 0); + program.cup(20, 0); console.log(data); });