diff --git a/lib/gpmclient.js b/lib/gpmclient.js index 3c2a191..2dc688c 100644 --- a/lib/gpmclient.js +++ b/lib/gpmclient.js @@ -102,63 +102,67 @@ function GpmClient(options) { vc=/[0-9]+$/.exec(tty)[0]; } + var self=this; + if (tty) { + fs.stat(GPM_SOCKET, function(err, stat) { + if (err || !stat.isSocket()) + return; + + var conf = { + eventMask: 0xffff, + defaultMask: GPM_MOVE|GPM_HARD, + minMod: 0, + maxMod: 0xffff, + pid: pid, + vc: vc + }; - var conf = { - eventMask: 0xffff, - defaultMask: GPM_MOVE|GPM_HARD, - minMod: 0, - maxMod: 0xffff, - pid: pid, - vc: vc - }; - - var gpm=net.createConnection(GPM_SOCKET); - this.gpm=gpm; - - gpm.on('connect', function() { - send_config(gpm, conf, function() { - conf.pid=0; - conf.vc=GPM_REQ_NOPASTE; - //send_config(gpm, conf); + var gpm=net.createConnection(GPM_SOCKET); + this.gpm=gpm; + + gpm.on('connect', function() { + send_config(gpm, conf, function() { + conf.pid=0; + conf.vc=GPM_REQ_NOPASTE; + //send_config(gpm, conf); + }); }); + + gpm.on('data', function(packet) { + var evnt=parseEvent(packet); + switch(evnt.type & 15) { + case GPM_MOVE: + if (evnt.dx || evnt.dy) 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; + case GPM_DRAG: + if (evnt.dx || evnt.dy) 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; + case GPM_DOWN: + self.emit('btndown', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) + if (evnt.type & GPM_DOUBLE) { + self.emit('dblclick', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) + } + break; + case GPM_UP: + self.emit('btnup', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) + if (!(evnt.type & GPM_MFLAG)) { + self.emit('click', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) + } + break; + } + }) + gpm.on('error', function(err) { + console.log('GPM ERROR', err); + self.stop(); + }); }); - - var self=this; - gpm.on('data', function(packet) { - var evnt=parseEvent(packet); - switch(evnt.type & 15) { - case GPM_MOVE: - if (evnt.dx || evnt.dy) 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; - case GPM_DRAG: - if (evnt.dx || evnt.dy) 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; - case GPM_DOWN: - self.emit('btndown', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) - if (evnt.type & GPM_DOUBLE) { - self.emit('dblclick', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) - } - break; - case GPM_UP: - self.emit('btnup', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) - if (!(evnt.type & GPM_MFLAG)) { - self.emit('click', evnt.buttons, evnt.modifiers, evnt.x, evnt.y) - } - break; - } - }) - gpm.on('error', function(err) { - console.log('GPM ERROR', err); - self.stop(); - }); } } - GpmClient.prototype=new EventEmitter(); GpmClient.prototype.stop=function() {