mirror of
https://github.com/danbulant/node-x11
synced 2026-07-07 12:10:47 +00:00
Merge pull request #20 from santigimeno/new_create_client
Change createClient signature
This commit is contained in:
commit
568465da52
13 changed files with 177 additions and 117 deletions
50
README.md
50
README.md
|
|
@ -14,35 +14,39 @@ Windows users:
|
|||
|
||||
# example
|
||||
|
||||
Core requsests usage:
|
||||
Core requests usage:
|
||||
|
||||
var x11 = require('x11');
|
||||
|
||||
var Exposure = x11.eventMask.Exposure;
|
||||
var PointerMotion = x11.eventMask.PointerMotion;
|
||||
|
||||
x11.createClient(function(display) {
|
||||
var X = display.client;
|
||||
var root = display.screen[0].root;
|
||||
var wid = X.AllocID();
|
||||
X.CreateWindow(
|
||||
wid, root, // new window id, parent
|
||||
0, 0, 100, 100, // x, y, w, h
|
||||
0, 0, 0, 0, // border, depth, class, visual
|
||||
{ eventMask: Exposure|PointerMotion } // other parameters
|
||||
);
|
||||
X.MapWindow(wid);
|
||||
var gc = X.AllocID();
|
||||
X.CreateGC(gc, wid);
|
||||
X.on('event', function(ev) {
|
||||
if (ev.type == 12)
|
||||
{
|
||||
X.PolyText8(wid, gc, 50, 50, ['Hello, Node.JS!']);
|
||||
}
|
||||
});
|
||||
X.on('error', function(e) {
|
||||
console.log(e);
|
||||
});
|
||||
x11.createClient(function(err, display) {
|
||||
if (!err) {
|
||||
var X = display.client;
|
||||
var root = display.screen[0].root;
|
||||
var wid = X.AllocID();
|
||||
X.CreateWindow(
|
||||
wid, root, // new window id, parent
|
||||
0, 0, 100, 100, // x, y, w, h
|
||||
0, 0, 0, 0, // border, depth, class, visual
|
||||
{ eventMask: Exposure|PointerMotion } // other parameters
|
||||
);
|
||||
X.MapWindow(wid);
|
||||
var gc = X.AllocID();
|
||||
X.CreateGC(gc, wid);
|
||||
X.on('event', function(ev) {
|
||||
if (ev.type == 12)
|
||||
{
|
||||
X.PolyText8(wid, gc, 50, 50, ['Hello, Node.JS!']);
|
||||
}
|
||||
});
|
||||
X.on('error', function(e) {
|
||||
console.log(e);
|
||||
});
|
||||
} else {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
|
||||
# Screenshots
|
||||
|
|
|
|||
|
|
@ -430,15 +430,18 @@ XClient.prototype.require = function(extName, callback)
|
|||
ext.requireExt(this.display, callback);
|
||||
}
|
||||
|
||||
module.exports.createClient = function(initCb, display, options)
|
||||
module.exports.createClient = function(options, initCb)
|
||||
{
|
||||
if (!options)
|
||||
options = false;
|
||||
if (typeof options === 'function') {
|
||||
initCb = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (!options) options = {};
|
||||
|
||||
var display = options.display;
|
||||
if (!display)
|
||||
display = process.env.DISPLAY;
|
||||
if (!display)
|
||||
display = ':0';
|
||||
display = (process.env.DISPLAY) ? process.env.DISPLAY : ':0';
|
||||
|
||||
var displayMatch = display.match(/^(?:[^:]*?\/)?(.*):(\d+)(?:.(\d+))?$/);
|
||||
if (!displayMatch)
|
||||
|
|
@ -485,18 +488,25 @@ module.exports.createClient = function(initCb, display, options)
|
|||
if (initCb)
|
||||
{
|
||||
client.on('connect', function(display) {
|
||||
// Once connected don't call initCb on error, just emit
|
||||
stream.removeListener('error', initCb);
|
||||
stream.on('error', function(err) {
|
||||
client.emit('error', err);
|
||||
});
|
||||
// opt-in BigReq
|
||||
if (!options.disableBigRequests) {
|
||||
client.require('big-requests', function(BigReq) {
|
||||
BigReq.Enable(function(err, maxLen) {
|
||||
display.max_request_length = maxLen;
|
||||
initCb(display);
|
||||
initCb(undefined, display);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
initCb(display);
|
||||
initCb(undefined, display);
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('error', initCb);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ var mocha = new Mocha({
|
|||
// 2 - dpms version is 1.1.
|
||||
// 3 - to be dpms capable.
|
||||
var run_dpms_test = function(cb) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (err) return cb(false);
|
||||
var display = dpy;
|
||||
var X = display.client;
|
||||
X.require('dpms', function(ext) {
|
||||
|
|
@ -42,7 +43,8 @@ var run_dpms_test = function(cb) {
|
|||
};
|
||||
|
||||
var run_xtest_test = function(cb) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (err) return cb(false);
|
||||
var display = dpy;
|
||||
var X = display.client;
|
||||
X.require('dpms', function(ext) {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,22 @@
|
|||
var x11 = require('../lib/x11');
|
||||
var should = require('should');
|
||||
var assert = require('assert');
|
||||
var util = require('util');
|
||||
|
||||
describe('Client', function() {
|
||||
|
||||
var display;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
client.on('error', done);
|
||||
});
|
||||
|
||||
|
|
@ -26,9 +32,7 @@ describe('Client', function() {
|
|||
it('uses display variable from parameter if present ignoring anvironment $DISPLAY', function(done) {
|
||||
var disp = process.env.DISPLAY;
|
||||
process.env.DISPLAY = 'BOGUS DISPLAY';
|
||||
var client = x11.createClient(function(display) {
|
||||
done();
|
||||
}, disp);
|
||||
var client = x11.createClient({ display : disp }, done);
|
||||
client.on('error', done);
|
||||
process.env.DISPLAY=disp;
|
||||
});
|
||||
|
|
@ -36,9 +40,9 @@ describe('Client', function() {
|
|||
it('throws error if $DISPLAY is bogus', function(done) {
|
||||
try {
|
||||
assert.throws(function() {
|
||||
var client = x11.createClient(function(display) {
|
||||
var client = x11.createClient({ display : 'BOGUS DISPLAY' }, function(err, display) {
|
||||
done('Should not reach here');
|
||||
}, 'BOGUS DISPLAY');
|
||||
});
|
||||
client.on('error', function(err) { done(); });
|
||||
}, /Cannot parse display/);
|
||||
done();
|
||||
|
|
@ -46,4 +50,11 @@ describe('Client', function() {
|
|||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when connecting to non existent display', function(done) {
|
||||
var client = x11.createClient({ display : ':44' }, function(err, display) {
|
||||
assert(util.isError(err));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,12 +6,16 @@ describe('Client', function() {
|
|||
|
||||
var display;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
});
|
||||
client.on('error', done);
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
client.on('error', done);
|
||||
});
|
||||
|
||||
it('should respond to ping()', function(done) {
|
||||
|
|
|
|||
|
|
@ -12,10 +12,13 @@ describe('CreateWindow request', function() {
|
|||
var display;
|
||||
var X;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
X = display.client;
|
||||
done();
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
}
|
||||
|
||||
done(err);
|
||||
});
|
||||
client.on('error', done);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,10 +7,13 @@ describe('ForceScreenSaver request', function() {
|
|||
var display;
|
||||
var X;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
X = display.client;
|
||||
done();
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
}
|
||||
|
||||
done(err);
|
||||
});
|
||||
client.on('error', done);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,13 +7,15 @@ describe('KillKlient request', function() {
|
|||
var display;
|
||||
var X;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
X = display.client;
|
||||
done();
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
}
|
||||
|
||||
done(err);
|
||||
});
|
||||
client.on('error', function(err) {
|
||||
console.log(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
|
@ -32,12 +34,16 @@ describe('KillKlient request', function() {
|
|||
});
|
||||
|
||||
it('should terminate other client connection', function(done) {
|
||||
x11.createClient(function(dpy) {
|
||||
var otherclient = dpy.client;
|
||||
var wnd = otherclient.AllocID();
|
||||
otherclient.CreateWindow(wnd, dpy.screen[0].root, 0, 0, 1, 1);
|
||||
otherclient.on('end', done);
|
||||
X.KillKlient(wnd);
|
||||
x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
var otherclient = dpy.client;
|
||||
var wnd = otherclient.AllocID();
|
||||
otherclient.CreateWindow(wnd, dpy.screen[0].root, 0, 0, 1, 1);
|
||||
otherclient.on('end', done);
|
||||
X.KillKlient(wnd);
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -13,13 +13,17 @@ describe('Window property', function() {
|
|||
var X;
|
||||
var wid;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
X = display.client;
|
||||
wid = X.AllocID();
|
||||
X.CreateWindow(wid, display.screen[0].root, 0, 0, 100, 100, 0, 0, 0, 0, { eventMask: x11.eventMask.PropertyChange});
|
||||
done();
|
||||
client.removeListener('error', done); // all future errors should be attached to corresponding test 'done'
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
wid = X.AllocID();
|
||||
X.CreateWindow(wid, display.screen[0].root, 0, 0, 100, 100, 0, 0, 0, 0, { eventMask: x11.eventMask.PropertyChange});
|
||||
done();
|
||||
client.removeListener('error', done); // all future errors should be attached to corresponding test 'done'
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
client.on('error', done);
|
||||
});
|
||||
|
|
|
|||
26
test/dpms.js
26
test/dpms.js
|
|
@ -8,17 +8,21 @@ describe('DPMS extension', function() {
|
|||
var X;
|
||||
var dpms;
|
||||
before(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
X.require('dpms', function(ext) {
|
||||
if (util.isError(ext)) {
|
||||
done(ext);
|
||||
} else {
|
||||
dpms = ext;
|
||||
done();
|
||||
}
|
||||
});
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
X.require('dpms', function(ext) {
|
||||
if (util.isError(ext)) {
|
||||
done(ext);
|
||||
} else {
|
||||
dpms = ext;
|
||||
done();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
client.on('error', done);
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@ describe('Client', function() {
|
|||
|
||||
var display;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
done();
|
||||
});
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
console.log(err)
|
||||
display = dpy;
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit error which is instance of Error with seqence number corresponding to source request', function(done) {
|
||||
it('should emit error which is instance of Error with sequence number corresponding to source request', function(done) {
|
||||
display.client.options.debug = true;
|
||||
display.client.CreateWindow(); // should emit error
|
||||
var seq = display.client.seq_num;
|
||||
|
|
|
|||
|
|
@ -6,10 +6,14 @@ describe('Client', function() {
|
|||
|
||||
var display;
|
||||
beforeEach(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display=dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
done();
|
||||
client.removeListener('error', done);
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
client.on('error', done);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -8,17 +8,21 @@ describe('XTEST extension', function() {
|
|||
var X;
|
||||
var xtest;
|
||||
before(function(done) {
|
||||
var client = x11.createClient(function(dpy) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
X.require('xtest', function(ext) {
|
||||
if (util.isError(ext)) {
|
||||
done(ext);
|
||||
} else {
|
||||
xtest = ext;
|
||||
done();
|
||||
}
|
||||
});
|
||||
var client = x11.createClient(function(err, dpy) {
|
||||
if (!err) {
|
||||
display = dpy;
|
||||
X = display.client;
|
||||
X.require('xtest', function(ext) {
|
||||
if (util.isError(ext)) {
|
||||
done(ext);
|
||||
} else {
|
||||
xtest = ext;
|
||||
done();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
client.on('error', done);
|
||||
|
|
|
|||
Loading…
Reference in a new issue