open correct socket on local OSX display

This commit is contained in:
Andrey Sidorov 2011-12-15 18:05:11 +11:00
parent 37a2ce9e3a
commit f5a65e5a42

View file

@ -1,3 +1,12 @@
if (process.platform == 'darwin') {
// some strage dns related errors in core node libs on OSX (node v0.6.5)
// skip them at the moment
process.on('uncaughtException', function (err) {
console.log(err);
console.log('Caught exception: ' + err);
});
}
var util = require('util'); // util.inherits var util = require('util'); // util.inherits
var net = require('net'); var net = require('net');
@ -62,30 +71,12 @@ function XClient(stream, displayNum, screenNum)
this.seq2stack = {}; // debug: map seq_num to stack at the moment request was issued this.seq2stack = {}; // debug: map seq_num to stack at the moment request was issued
// in/out packets indexed by sequence ID // in/out packets indexed by sequence ID
//this.requests = {};
this.replies = {}; this.replies = {};
//this.events = {};
this.atoms = stdatoms; this.atoms = stdatoms;
this.event_consumers = {}; // maps window id to eventemitter TODO: bad name this.event_consumers = {}; // maps window id to eventemitter TODO: bad name
this.importRequestsFromTemplates(this, coreRequests); this.importRequestsFromTemplates(this, coreRequests);
// TODO: this is potentially async and probably has to be synchronised with 'connect' event
/*
// import available extentions
// TODO: lazy import on first call?
this.ext = {};
this.ListExtensions( function(err, extentionsList ) {
for (ext in extentionsList) {
try {
X.QueryExtension(ext, function(e) {
var extRequests = require('./ext/' + extentionsList[ext]);
importRequestsFromTemplates(this, extRequests);
});
} catch (e) {
// do not import if module not defined
}
}
});
*/
this.startHandshake(); this.startHandshake();
} }
util.inherits(XClient, EventEmitter); util.inherits(XClient, EventEmitter);
@ -102,12 +93,13 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs)
for (r in reqs) for (r in reqs)
{ {
// r is request name // r is request name
target[r] = (function(reqName) { target[r] = (function(reqName) {
var reqFunc = function req_proxy() { var reqFunc = function req_proxy() {
client.seq_num++; // TODO: handle overflow (seq should be last 15 (?) bits of the number client.seq_num++; // TODO: handle overflow (seq should be last 15 (?) bits of the number
var err = new Error; var err = new Error;
err.name = r; err.name = reqName;
Error.captureStackTrace(err, arguments.callee); Error.captureStackTrace(err, arguments.callee);
client.seq2stack[client.seq_num] = err.stack; client.seq2stack[client.seq_num] = err.stack;
@ -324,11 +316,14 @@ XClient.prototype.require = function(extName, callback)
ext.requireExt(this.display, callback); ext.requireExt(this.display, callback);
} }
var platformDefaultTransport = { var platformLocalTransport = {
win32: 'tcp', win32: 'tcp',
win64: 'tcp', win64: 'tcp',
cygwin: 'tcp', cygwin: 'tcp',
linux: 'unix' linux: 'unix',
// OSX
darwin: 'unix'
// TODO: check process.platform on SmartMachine solaris box // TODO: check process.platform on SmartMachine solaris box
} }
@ -343,6 +338,7 @@ module.exports.createClient = function(initCb, display)
var host = displayMatch[1]; var host = displayMatch[1];
if (!host) if (!host)
host = '127.0.0.1'; host = '127.0.0.1';
var displayNum = displayMatch[2]; var displayNum = displayMatch[2];
if (!displayNum) if (!displayNum)
displayNum = 0; displayNum = 0;
@ -352,13 +348,22 @@ module.exports.createClient = function(initCb, display)
// open stream // open stream
var stream; var stream;
var defaultTransportName = platformDefaultTransport[process.platform]; var socketPath;
// use tcp if stated explicitly or if not defined at all if ( process.platform.match(/win/).length > 0 )
if (!defaultTransportName || defaultTransportName == 'tcp' || host != '127.0.0.1') {
stream = net.createConnection(6000 + parseInt(displayNum), host); if (process.platform == 'darwin')
if (defaultTransportName == 'unix' && host == '127.0.0.1') {
stream = net.createConnection('/tmp/.X11-unix/X' + displayNum); if (display[0] == '/') // local socket
{
socketPath = display;
}
} else if(host == '127.0.0.1')
socketPath = '/tmp/.X11-unix/X' + displayNum;
}
if(socketPath)
stream = net.createConnection(socketPath);
else
stream = net.createConnection(6000 + parseInt(displayNum), host);
var client = new XClient(stream, displayNum, screenNum); var client = new XClient(stream, displayNum, screenNum);
if (initCb) if (initCb)
{ {