GraphicsContext wrapper

This commit is contained in:
sidorares 2011-07-19 11:04:16 +10:00
parent bd7897d6b5
commit a911771bfc
4 changed files with 66 additions and 37 deletions

View file

@ -206,11 +206,12 @@ XClient.prototype.expectReplyHeader = function()
// if (error_code == 14) // if (error_code == 14)
{ {
var res = buf.unpack('LSC'); var res = buf.unpack('LSC');
error.badPparam = res[0]; // id: GC, WinID, Font, Atom etc; Value error.badParam = res[0]; // id: GC, WinID, Font, Atom etc; Value
error.minorOpcode = res[1]; error.minorOpcode = res[1];
error.majorOpcode = res[2]; error.majorOpcode = res[2];
} }
client.emit('error', error); console.log(error);
//client.emit('error', error);
client.expectReplyHeader(); client.expectReplyHeader();
} ); } );
return; return;
@ -220,7 +221,6 @@ XClient.prototype.expectReplyHeader = function()
var extra = res[3]; var extra = res[3];
var ev = client.unpackEvent(type, seq_num, extra, buf); var ev = client.unpackEvent(type, seq_num, extra, buf);
client.emit('event', ev); client.emit('event', ev);
console.log(ev);
var ee = client.event_consumers[ev.wid]; var ee = client.event_consumers[ev.wid];
if (ee) { if (ee) {
ee.emit('event', ev); ee.emit('event', ev);

View file

@ -5,7 +5,6 @@ var Exposure = x11.eventMask.Exposure;
var PointerMotion = x11.eventMask.PointerMotion; var PointerMotion = x11.eventMask.PointerMotion;
var pts = []; var pts = [];
xclient.on('connect', function(display) { xclient.on('connect', function(display) {
var X = this; var X = this;
var root = display.screen[0].root; var root = display.screen[0].root;

View file

@ -5,33 +5,21 @@ var width = 700;
var height = 500; var height = 500;
var xclient = x11.createClient(); var xclient = x11.createClient();
var pts = [];
xclient.on('connect', function(display) { xclient.on('connect', function(display) {
var white = xclient.display.screen[0].white_pixel;
var black = xclient.display.screen[0].black_pixel;
var mainwnd = new Window(xclient, 0, 0, width, height, white); var mainwnd = new Window(xclient, 0, 0, width, height);
mainwnd.on('mousemove', function(ev) { mainwnd.on('mousemove', function(ev)
console.log(ev.x, ev.y); {
pts.push(ev.x);
pts.push(ev.y);
this.gc.drawText(ev.x, ev.y, 'Hello, NodeJS!');
});
mainwnd.on('expose', function(ev) {
for (var i=0; i < pts.length/2 ; ++i)
ev.gc.drawText(pts[i], pts[i+1], 'Hello, NodeJS!');
}); });
var ch = new Window(mainwnd, 10, 10, 50, 70, black);
ch.on('mousemove', function(ev) {
console.log(ev);
//ch.unmap();
//setTimeout( function() { ch.map() }, 500);
});
mainwnd.map(); mainwnd.map();
/*
for (var x = 0; x < width; x += 20) {
for (var y = 0; y < width; y += 20) {
// TODO: wnd.createChild() ?
var ch = new Window(mainwnd, x + 1, y + 1, 18, 18, 0, black);
//ch.map();
ch.on('mousemove', function(ev) {
ch.unmap();
setTimeout( function() { ch.map() }, 500);
});
}
}
*/
}); });

View file

@ -1,7 +1,35 @@
var x11 = require('../lib/x11');
var Exposure = x11.eventMask.Exposure;
var PointerMotion = x11.eventMask.PointerMotion;
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var util = require('util'); // util.inherits var util = require('util'); // util.inherits
function Window(parent, x, y, w, h, bg) function GraphicContext(win)
{
this.win = win;
this.xclient = win.xclient;
this.id = this.xclient.AllocID();
win.xclient.CreateGC(this.id, win.id);
}
GraphicContext.prototype.polyLine = function(points)
{
this.xclient.PolyLine(0, this.win.id, this.id, points);
}
GraphicContext.prototype.noop = function()
{
//testing triggering gc creation
}
GraphicContext.prototype.drawText = function(x, y, text)
{
//console.log([0, this.win.id, this.id, x, y, [text]]);
this.xclient.PolyText8(this.win.id, this.id, x, y, [text]);
}
function Window(parent, x, y, w, h)
{ {
if (parent.constructor && parent.constructor.name == 'XClient') if (parent.constructor && parent.constructor.name == 'XClient')
{ {
@ -27,7 +55,8 @@ function Window(parent, x, y, w, h, bg)
this.y = y; this.y = y;
this.w = w; this.w = w;
this.h = h; this.h = h;
this.bg = bg; this.black = this.xclient.display.screen[0].black_pixel;
this.white = this.xclient.display.screen[0].white_pixel;
this.id = this.xclient.AllocID(); this.id = this.xclient.AllocID();
var borderWidth = 1; var borderWidth = 1;
@ -37,25 +66,38 @@ function Window(parent, x, y, w, h, bg)
this.id, this.parent.id, this.x, this.y, this.w, this.h, this.id, this.parent.id, this.x, this.y, this.w, this.h,
borderWidth, _class, visual, borderWidth, _class, visual,
{ {
backgroundPixel: this.bg, backgroundPixel: this.white,
eventMask: 0x00000040 eventMask: Exposure|PointerMotion
} }
); );
//this.map();
//this.map();
var wnd = this; var wnd = this;
eventType2eventName = { eventType2eventName = {
6: 'mousemove' 6: 'mousemove',
12: 'expose'
}; };
var ee = new EventEmitter(); var ee = new EventEmitter();
this.xclient.event_consumers[wnd.id] = ee; this.xclient.event_consumers[wnd.id] = ee;
// TODO: do we need to have wnd as EventEmitter AND EventEmitter stored in event_consumers ? // TODO: do we need to have wnd as EventEmitter AND EventEmitter stored in event_consumers ?
ee.on('event', function( ev ) ee.on('event', function( ev )
{ {
if (ev.type == 12) //Expose
ev.gc = wnd.gc;
wnd.emit(eventType2eventName[ev.type], ev); // convert to mousemove? (ev is already event-spacific) wnd.emit(eventType2eventName[ev.type], ev); // convert to mousemove? (ev is already event-spacific)
}); });
// TODO: track delete events and remove wmd from consumers list // TODO: track delete events and remove wmd from consumers list
this.__defineGetter__('gc', function()
{
if (!this._gc)
{
this._gc = new GraphicContext(this);
}
return this._gc;
});
} }
util.inherits(Window, EventEmitter); util.inherits(Window, EventEmitter);
@ -67,4 +109,4 @@ Window.prototype.unmap = function() {
this.xclient.UnmapWindow(this.id); this.xclient.UnmapWindow(this.id);
} }
module.exports = Window; module.exports = Window;