From 1cfab855005b0e48c8ba4da209d9a8ad468826e2 Mon Sep 17 00:00:00 2001 From: sidorares Date: Tue, 26 Jul 2011 11:22:00 +1000 Subject: [PATCH] pass correctly mouse button (and keyboard) code in unpackEvent --- lib/x11/xcore.js | 6 ++++-- test/sketch.js | 38 +++++++++++++++++++++++++++++--------- test/wndwrap.js | 10 ++++++++-- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/x11/xcore.js b/lib/x11/xcore.js index 04bd379..cb07096 100644 --- a/lib/x11/xcore.js +++ b/lib/x11/xcore.js @@ -156,7 +156,7 @@ XClient.prototype.AllocID = function() return (this.display.rsrc_id << this.display.rsrc_shift) + this.display.resource_base; } -XClient.prototype.unpackEvent = function(type, seq, extra, raw) +XClient.prototype.unpackEvent = function(type, seq, extra, code, raw) { var event = {}; // TODO: constructor & base functions event.type = type; @@ -167,6 +167,7 @@ XClient.prototype.unpackEvent = function(type, seq, extra, raw) //event.raw = values; // TODO: use unpackTo??? event.time = extra; + event.keycode = code; event.root = values[0]; event.wid = values[1]; event.child = values[2]; @@ -231,7 +232,8 @@ XClient.prototype.expectReplyHeader = function() { client.pack_stream.get(24, function(buf) { var extra = res[3]; - var ev = client.unpackEvent(type, seq_num, extra, buf); + var code = res[1]; + var ev = client.unpackEvent(type, seq_num, extra, code, buf); client.emit('event', ev); var ee = client.event_consumers[ev.wid]; if (ee) { diff --git a/test/sketch.js b/test/sketch.js index 9be20aa..9baa5c5 100644 --- a/test/sketch.js +++ b/test/sketch.js @@ -1,22 +1,42 @@ var x11 = require('../lib/x11'); var Window = require('./wndwrap'); -var width = 700; -var height = 500; +x11.createClient(function(display) { -var xclient = x11.createClient(function(display) { - - new Window(xclient, 0, 0, width, height) + var pts = []; + new Window(display.client, 0, 0, 700, 500) .handle({ + mousemove: function(ev) { - pts.push(ev.x); - pts.push(ev.y); + if (this.pressed) + { + var lastpoly = pts[pts.length - 1]; + lastpoly.push(ev.x); + lastpoly.push(ev.y); + if (lastpoly.length > 3) + this.gc.polyLine(lastpoly.slice(-4)); + } + }, + + mousedown: function(ev) { + if (ev.keycode == 1) // left button + { + this.pressed = true; + pts.push([]); + } + }, + + mouseup: function(ev) { + if (ev.keycode == 1) // left button + this.pressed = false; }, expose: function(ev) { - for (var i=0; i < pts.length/2 ; ++i) - ev.gc.drawText(pts[i], pts[i+1], 'Hello, NodeJS!'); + for (var i=0; i < pts.length ; ++i) { + this.gc.polyLine(pts[i]); + } } + }) .map() .title = 'Hello, world!'; diff --git a/test/wndwrap.js b/test/wndwrap.js index 3ab876b..14f661d 100644 --- a/test/wndwrap.js +++ b/test/wndwrap.js @@ -1,6 +1,8 @@ var x11 = require('../lib/x11'); var Exposure = x11.eventMask.Exposure; var PointerMotion = x11.eventMask.PointerMotion; +var ButtonPress = x11.eventMask.ButtonPress; +var ButtonRelease = x11.eventMask.ButtonRelease; var EventEmitter = require('events').EventEmitter; var util = require('util'); // util.inherits @@ -87,13 +89,15 @@ function Window(parent, x, y, w, h) borderWidth, _class, visual, { backgroundPixel: this.white, - eventMask: Exposure|PointerMotion + eventMask: Exposure|PointerMotion|ButtonPress|ButtonRelease } ); //this.map(); var wnd = this; eventType2eventName = { + 4: 'mousedown', + 5: 'mouseup', 6: 'mousemove', 12: 'expose' }; @@ -105,7 +109,6 @@ function Window(parent, x, y, w, h) { if (ev.type == 12) //Expose ev.gc = wnd.gc; - wnd.emit(eventType2eventName[ev.type], ev); // convert to mousemove? (ev is already event-spacific) }); // TODO: track delete events and remove wmd from consumers list @@ -142,9 +145,12 @@ Window.prototype.unmap = function() { } Window.prototype.handle = function(handlers) { + // TODO: compare event mask with events names and issue + // one ChangeWindowAttributes request adding missing events for (var eventName in handlers) { this.on(eventName, handlers[eventName]); } + return this; } Window.prototype.getProperty = function(name, cb) {