gpm: refactor to fit style.

This commit is contained in:
Christopher Jeffrey 2015-02-01 07:07:55 -08:00
parent fae4acceef
commit 87a3d2f288
2 changed files with 327 additions and 281 deletions

View File

@ -5,24 +5,29 @@ var util = require('util')
var GPM_USE_MAGIC = false; var GPM_USE_MAGIC = false;
var GPM_MOVE=1, GPM_DRAG=2, GPM_DOWN=4, GPM_UP=8; var GPM_MOVE = 1
var GPM_DOUBLE=32,GPM_MFLAG=128; , GPM_DRAG = 2
, GPM_DOWN = 4
, GPM_UP = 8;
var GPM_REQ_NOPASTE=3, GPM_HARD=256; var GPM_DOUBLE = 32
, GPM_MFLAG = 128;
var GPM_REQ_NOPASTE = 3
, GPM_HARD = 256;
var GPM_MAGIC = 0x47706D4C; var GPM_MAGIC = 0x47706D4C;
var GPM_SOCKET="/dev/gpmctl"; var GPM_SOCKET = '/dev/gpmctl';
/* // typedef struct Gpm_Connect {
typedef struct Gpm_Connect { // unsigned short eventMask, defaultMask;
unsigned short eventMask, defaultMask; // unsigned short minMod, maxMod;
unsigned short minMod, maxMod; // int pid;
int pid; // int vc;
int vc; // } Gpm_Connect;
} Gpm_Connect;
*/ function send_config(socket, Gpm_Connect, callback) {
function send_config(sock, Gpm_Connect, cb) {
if (GPM_USE_MAGIC) { if (GPM_USE_MAGIC) {
var buffer = new Buffer(20); var buffer = new Buffer(20);
buffer.writeUInt32LE(GPM_MAGIC, 0); buffer.writeUInt32LE(GPM_MAGIC, 0);
@ -41,29 +46,28 @@ function send_config(sock, Gpm_Connect, cb) {
buffer.writeInt16LE(Gpm_Connect.pid, 8); buffer.writeInt16LE(Gpm_Connect.pid, 8);
buffer.writeInt16LE(Gpm_Connect.vc, 12); buffer.writeInt16LE(Gpm_Connect.vc, 12);
} }
sock.write(buffer, function () { socket.write(buffer, function() {
if (cb) cb(); if (callback) callback();
}); });
} }
/* // typedef struct Gpm_Event {
typedef struct Gpm_Event { // unsigned char buttons, modifiers; // try to be a multiple of 4
unsigned char buttons, modifiers; // try to be a multiple of 4 // unsigned short vc;
unsigned short vc; // short dx, dy, x, y; // displacement x,y for this event, and absolute x,y
short dx, dy, x, y; // displacement x,y for this event, and absolute x,y // enum Gpm_Etype type;
enum Gpm_Etype type; // // clicks e.g. double click are determined by time-based processing
// clicks e.g. double click are determined by time-based processing // int clicks;
int clicks; // enum Gpm_Margin margin;
enum Gpm_Margin margin; // // wdx/y: displacement of wheels in this event. Absolute values are not
// wdx/y: displacement of wheels in this event. Absolute values are not // // required, because wheel movement is typically used for scrolling
// required, because wheel movement is typically used for scrolling // // or selecting fields, not for cursor positioning. The application
// or selecting fields, not for cursor positioning. The application // // can determine when the end of file or form is reached, and not
// can determine when the end of file or form is reached, and not // // go any further.
// go any further. // // A single mouse will use wdy, "vertical scroll" wheel.
// A single mouse will use wdy, "vertical scroll" wheel. // short wdx, wdy;
short wdx, wdy; // } Gpm_Event;
} Gpm_Event;
*/
function parseEvent(raw) { function parseEvent(raw) {
var evnt = {}; var evnt = {};
evnt.buttons = raw[0]; evnt.buttons = raw[0];
@ -106,8 +110,9 @@ function GpmClient(options) {
if (tty) { if (tty) {
fs.stat(GPM_SOCKET, function(err, stat) { fs.stat(GPM_SOCKET, function(err, stat) {
if (err || !stat.isSocket()) if (err || !stat.isSocket()) {
return; return;
}
var conf = { var conf = {
eventMask: 0xffff, eventMask: 0xffff,
@ -133,61 +138,73 @@ function GpmClient(options) {
var evnt = parseEvent(packet); var evnt = parseEvent(packet);
switch (evnt.type & 15) { switch (evnt.type & 15) {
case GPM_MOVE: case GPM_MOVE:
if (evnt.dx || evnt.dy) self.emit('move', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) if (evnt.dx || evnt.dy) {
if (evnt.wdx || evnt.wdy) self.emit('mousewheel', evnt.buttons, evnt.modifiers, evnt.x, evnt.y, evnt.wdx, evnt.wdy) self.emit('move', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
}
if (evnt.wdx || evnt.wdy) {
self.emit('mousewheel',
evnt.buttons, evnt.modifiers,
evnt.x, evnt.y, evnt.wdx, evnt.wdy);
}
break; break;
case GPM_DRAG: case GPM_DRAG:
if (evnt.dx || evnt.dy) self.emit('drag', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) if (evnt.dx || evnt.dy) {
if (evnt.wdx || evnt.wdy) self.emit('mousewheel', evnt.buttons, evnt.modifiers, evnt.x, evnt.y, evnt.wdx, evnt.wdy) self.emit('drag', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
}
if (evnt.wdx || evnt.wdy) {
self.emit('mousewheel',
evnt.buttons, evnt.modifiers,
evnt.x, evnt.y, evnt.wdx, evnt.wdy);
}
break; break;
case GPM_DOWN: case GPM_DOWN:
self.emit('btndown', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) self.emit('btndown', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
if (evnt.type & GPM_DOUBLE) { if (evnt.type & GPM_DOUBLE) {
self.emit('dblclick', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) self.emit('dblclick', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
} }
break; break;
case GPM_UP: case GPM_UP:
self.emit('btnup', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) self.emit('btnup', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
if (!(evnt.type & GPM_MFLAG)) { if (!(evnt.type & GPM_MFLAG)) {
self.emit('click', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) self.emit('click', evnt.buttons, evnt.modifiers, evnt.x, evnt.y);
} }
break; break;
} }
}) });
gpm.on('error', function(err) { gpm.on('error', function(err) {
console.log('GPM ERROR', err); // console.log('GPM ERROR', err);
self.stop(); self.stop();
}); });
}); });
} }
} }
GpmClient.prototype=new EventEmitter(); GpmClient.prototype.__proto__ = EventEmitter.prototype;
GpmClient.prototype.stop = function() { GpmClient.prototype.stop = function() {
if (this.gpm) this.gpm.end(); if (this.gpm) {
delete this.gpm; this.gpm.end();
} }
delete this.gpm;
};
GpmClient.prototype.ButtonName = function(btn) { GpmClient.prototype.ButtonName = function(btn) {
if (btn & 4) return 'left'; if (btn & 4) return 'left';
if (btn & 2) return 'middle'; if (btn & 2) return 'middle';
if (btn & 1) return 'right'; if (btn & 1) return 'right';
return '' return '';
} };
GpmClient.prototype.hasShiftKey = function(mod) { GpmClient.prototype.hasShiftKey = function(mod) {
return (mod & 1) ? true : false; return (mod & 1) ? true : false;
} };
GpmClient.prototype.hasCtrlKey = function(mod) { GpmClient.prototype.hasCtrlKey = function(mod) {
return (mod & 4) ? true : false; return (mod & 4) ? true : false;
} };
GpmClient.prototype.hasMetaKey = function(mod) { GpmClient.prototype.hasMetaKey = function(mod) {
return (mod & 8) ? true : false; return (mod & 8) ? true : false;
} };
module.exports = GpmClient; module.exports = GpmClient;

View File

@ -635,76 +635,100 @@ Program.prototype._bindMouse = function(s, buf) {
/* gpm support for linux vc */ /* gpm support for linux vc */
Program.prototype.enableGpm = function() { Program.prototype.enableGpm = function() {
var gpmclient=require('./gpmclient')
this.gpm=gpmclient();
var self = this; var self = this;
var gpmclient = require('./gpmclient')
this.gpm = gpmclient();
this.gpm.on('btndown', function(btn, modifier, x, y) { this.gpm.on('btndown', function(btn, modifier, x, y) {
x--, y--; x--, y--;
var key = { var key = {
name: 'mouse', type: 'GPM', name: 'mouse',
type: 'GPM',
action: 'mousedown', action: 'mousedown',
button: self.gpm.ButtonName(btn), button: self.gpm.ButtonName(btn),
raw: [btn,modifier, x, y], raw: [btn,modifier, x, y],
x: x, y: y, x: x,
y: y,
shift: self.gpm.hasShiftKey(modifier), shift: self.gpm.hasShiftKey(modifier),
meta: self.gpm.hasMetaKey(modifier), meta: self.gpm.hasMetaKey(modifier),
ctrl: self.gpm.hasCtrlKey(modifier) ctrl: self.gpm.hasCtrlKey(modifier)
}; };
self.emit('keypress', null, key); self.emit('keypress', null, key);
self.emit('mouse', key); self.emit('mouse', key);
}); });
this.gpm.on('btnup', function(btn, modifier, x, y) { this.gpm.on('btnup', function(btn, modifier, x, y) {
x--, y--; x--, y--;
var key = { var key = {
name: 'mouse', type: 'GPM', name: 'mouse',
type: 'GPM',
action: 'mouseup', action: 'mouseup',
button: self.gpm.ButtonName(btn), button: self.gpm.ButtonName(btn),
raw: [btn,modifier, x, y], raw: [btn,modifier, x, y],
x: x, y: y, x: x,
y: y,
shift: self.gpm.hasShiftKey(modifier), shift: self.gpm.hasShiftKey(modifier),
meta: self.gpm.hasMetaKey(modifier), meta: self.gpm.hasMetaKey(modifier),
ctrl: self.gpm.hasCtrlKey(modifier) ctrl: self.gpm.hasCtrlKey(modifier)
}; };
self.emit('keypress', null, key); self.emit('keypress', null, key);
self.emit('mouse', key); self.emit('mouse', key);
}); });
this.gpm.on('move', function(btn, modifier, x, y) { this.gpm.on('move', function(btn, modifier, x, y) {
x--, y--; x--, y--;
var key = { var key = {
name: 'mouse', type: 'GPM', name: 'mouse',
type: 'GPM',
action: 'mousemove', action: 'mousemove',
button: self.gpm.ButtonName(btn), button: self.gpm.ButtonName(btn),
raw: [btn,modifier, x, y], raw: [btn,modifier, x, y],
x: x, y: y, x: x,
y: y,
shift: self.gpm.hasShiftKey(modifier), shift: self.gpm.hasShiftKey(modifier),
meta: self.gpm.hasMetaKey(modifier), meta: self.gpm.hasMetaKey(modifier),
ctrl: self.gpm.hasCtrlKey(modifier) ctrl: self.gpm.hasCtrlKey(modifier)
}; };
self.emit('keypress', null, key); self.emit('keypress', null, key);
self.emit('mouse', key); self.emit('mouse', key);
}); });
this.gpm.on('drag', function(btn, modifier, x, y) { this.gpm.on('drag', function(btn, modifier, x, y) {
x--, y--; x--, y--;
var key = { var key = {
name: 'mouse', type: 'GPM', name: 'mouse',
type: 'GPM',
action: 'mousemove', action: 'mousemove',
button: self.gpm.ButtonName(btn), button: self.gpm.ButtonName(btn),
raw: [btn,modifier, x, y], raw: [btn,modifier, x, y],
x: x, y: y, x: x,
y: y,
shift: self.gpm.hasShiftKey(modifier), shift: self.gpm.hasShiftKey(modifier),
meta: self.gpm.hasMetaKey(modifier), meta: self.gpm.hasMetaKey(modifier),
ctrl: self.gpm.hasCtrlKey(modifier) ctrl: self.gpm.hasCtrlKey(modifier)
}; };
self.emit('keypress', null, key); self.emit('keypress', null, key);
self.emit('mouse', key); self.emit('mouse', key);
}); });
this.gpm.on('mousewheel', function(btn, modifier, x, y, dx, dy) { this.gpm.on('mousewheel', function(btn, modifier, x, y, dx, dy) {
var key = { var key = {
name: 'mouse', type: 'GPM', name: 'mouse',
type: 'GPM',
action: dy > 0 ? 'wheelup':'wheeldown', action: dy > 0 ? 'wheelup':'wheeldown',
button: self.gpm.ButtonName(btn), button: self.gpm.ButtonName(btn),
raw: [btn,modifier, x, y, dx, dy], raw: [btn,modifier, x, y, dx, dy],
x: x, y: y, x: x,
y: y,
shift: self.gpm.hasShiftKey(modifier), shift: self.gpm.hasShiftKey(modifier),
meta: self.gpm.hasMetaKey(modifier), meta: self.gpm.hasMetaKey(modifier),
ctrl: self.gpm.hasCtrlKey(modifier) ctrl: self.gpm.hasCtrlKey(modifier)
@ -722,7 +746,6 @@ Program.prototype.disableGpm = function() {
} }
}; };
// All possible responses from the terminal // All possible responses from the terminal
Program.prototype.bindResponse = function() { Program.prototype.bindResponse = function() {
if (this._boundResponse) return; if (this._boundResponse) return;
@ -2751,36 +2774,42 @@ Program.prototype.normalBuffer = function() {
}; };
Program.prototype.enableMouse = function() { Program.prototype.enableMouse = function() {
console.log(process.env.BLESSED_FORCE_MODES);
if (process.env.BLESSED_FORCE_MODES) { if (process.env.BLESSED_FORCE_MODES) {
var modes = process.env.BLESSED_FORCE_MODES.split(','); var modes = process.env.BLESSED_FORCE_MODES.split(',');
var options = {}; var options = {};
for (var n = 0; n < modes.length; ++n) { for (var n = 0; n < modes.length; ++n) {
var mds=modes[n].split('='); var pair = modes[n].split('=');
var v=mds[1]=='0' ? false : true; var v = pair[1] !== '0';
switch(mds[0].toUpperCase()) { switch (pair[0].toUpperCase()) {
case 'SGRMOUSE': case 'SGRMOUSE':
options.sgrMouse=v; break; options.sgrMouse = v;
break;
case 'UTFMOUSE': case 'UTFMOUSE':
options.utfMouse=v; break; options.utfMouse = v;
break;
case 'VT200MOUSE': case 'VT200MOUSE':
options.vt200Mouse=v; break; options.vt200Mouse = v;
break;
case 'URXVTMOUSE': case 'URXVTMOUSE':
options.urxvtMouse=true; break; options.urxvtMouse = v;
break;
case 'X10MOUSE': case 'X10MOUSE':
options.x10Mouse=true; break; options.x10Mouse = v;
break;
case 'GPMMOUSE': case 'GPMMOUSE':
this.enableGpm(); this.enableGpm();
break; break;
case 'CELLMOTION': case 'CELLMOTION':
options.cellMotion=v; break; options.cellMotion = v;
break;
case 'ALLMOTION': case 'ALLMOTION':
options.allMotion=v; break; options.allMotion = v;
break;
case 'SENDFOCUS': case 'SENDFOCUS':
options.sendFocus=v; break; options.sendFocus = v;
break;
} }
} }
console.log(options);
return this.setMouse(options, true); return this.setMouse(options, true);
} }