Permalink
Browse files

initial import

  • Loading branch information...
1 parent f6f6065 commit 59c48b76edc4022324425dcead66b4b398adf794 @sidorares committed Dec 19, 2012
Showing with 330 additions and 0 deletions.
  1. +14 −0 examples/example.js
  2. +101 −0 examples/glclock.js
  3. +56 −0 examples/gradient.js
  4. +130 −0 lib/index.js
  5. +29 −0 package.json
View
@@ -0,0 +1,14 @@
+var ntk = require('../lib');
+
+ntk.createClient(main);
+
+function main(err, app) {
+ var mainwnd = app.createWindow({title: "Close me!"})
+ .on('mouseout', function(ev) { console.log('Out'); })
+ .on('mouseover', function(ev) { console.log('In'); })
+ .on('mousedown', function(ev) {
+ ev.window.unmap();
+ setTimeout(function() { ev.window.map(); }, 1000);
+ })
+ .map();
+}
View
@@ -0,0 +1,101 @@
+var ntk = require('../lib');
+
+var width = 300;
+var height = 300;
+var angle = 0;
+
+function draw(gl) {
+ gl.Enable(gl.BLEND);
+ gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
+ gl.Viewport(0, 0, width, height);
+ gl.MatrixMode(gl.PROJECTION);
+ gl.LoadIdentity();
+ if (width < height)
+ gl.Frustum(-1.0, 1.0, -height/width, height/width, -35.0, 20);
+ else
+ gl.Frustum(-width/height, width/height, -1, 1, -35.0, 20);
+ gl.MatrixMode(gl.MODELVIEW);
+ gl.LoadIdentity();
+
+ gl.ClearColor(0.3,0.3,0.3,0.0);
+ gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+
+ gl.Color3f(1, 0, 0);
+ for (var i=0; i < 12; ++i) {
+ gl.Rotatef(30, 0, 0, 1);
+ gl.Begin(gl.QUADS);
+ gl.Vertex3f(-0.01, 0.8, 0);
+ gl.Vertex3f(-0.01, 1.0, 0);
+ gl.Vertex3f(0.01, 1.0, 0);
+ gl.Vertex3f(0.01, 0.8, 0);
+ gl.End();
+ }
+ gl.LoadIdentity();
+ gl.Color4f(0, 0, 1, 0.3);
+ for (var i=0; i < 60; ++i) {
+ gl.Rotatef(6, 0, 0, 1);
+ gl.PointSize(8);
+ gl.Begin(gl.POINTS);
+ gl.Vertex3f(0, 0.99, 0);
+ gl.End();
+ gl.Begin(gl.QUADS);
+ gl.Vertex3f(-0.003, 0.85, 0);
+ gl.Vertex3f(-0.003, 1.0, 0);
+ gl.Vertex3f(0.003, 1.0, 0);
+ gl.Vertex3f(0.003, 0.85, 0);
+ gl.End();
+ }
+
+ var t = new Date();
+ gl.LoadIdentity();
+ gl.Color4f(1, 1, 1, 0.5);
+ gl.Rotatef(-(t.getSeconds()*6000+6*t.getMilliseconds())/1000, 0, 0, 1);
+ gl.Begin(gl.TRIANGLES);
+ gl.Vertex3f(-0.02, -0.1, 0);
+ gl.Vertex3f(0, 0.9, 0);
+ gl.Vertex3f(0.02, -0.1, 0);
+ gl.End();
+
+ gl.LoadIdentity();
+ gl.Color4f(0.5, 0.5, 0.1, 0.5);
+ gl.Rotatef(-t.getMinutes()*6 -t.getSeconds()/10 - t.getMilliseconds()/10000, 0, 0, 1);
+ gl.Begin(gl.TRIANGLES);
+ gl.Vertex3f(-0.05, -0.1, 0);
+ gl.Vertex3f(0, 0.8, 0);
+ gl.Vertex3f(0.05, -0.1, 0);
+ gl.End();
+
+ gl.LoadIdentity();
+ gl.Color4f(0, 0, 0, 0.5);
+ gl.Rotatef(-(t.getHours()*30 + t.getMinutes()/2), 0, 0, 1);
+ gl.Begin(gl.TRIANGLES);
+ gl.Vertex3f(-0.08, -0.1, 0);
+ gl.Vertex3f(0, 0.6, 0);
+ gl.Vertex3f(0.08, -0.1, 0);
+ gl.End();
+
+}
+
+ntk.createClient(main);
+function main(err, app) {
+ var wnd = app.createWindow({title: "Close me!", x: 100, y: 100, width: 300, height: 300});
+ wnd.map();
+ var gl = wnd.getContext('opengl');
+ gl.Enable(gl.POINT_SMOOTH);
+
+ function drawAndFinish() {
+ draw(gl);
+ gl.SwapBuffers();
+ gl.Finish(drawAndFinish);
+ }
+ drawAndFinish();
+
+ wnd.on('resize', function(ev) {
+ width = ev.width;
+ height = ev.height;
+ draw(gl);
+ gl.SwapBuffers();
+ }).on('mousemove', function(ev) {
+ console.log(ev.x, ev.y);
+ });
+}
View
@@ -0,0 +1,56 @@
+var ntk = require('../lib');
+
+var width = 300;
+var height = 300;
+var angle = 0;
+
+function draw(gl) {
+ //gl.Enable(gl.BLEND);
+ //gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
+ gl.Viewport(0, 0, width, height);
+ gl.MatrixMode(gl.PROJECTION);
+ gl.LoadIdentity();
+ gl.Frustum(-1.0, 1.0, -1, 1, -35.0, 20);
+ gl.MatrixMode(gl.MODELVIEW);
+ gl.LoadIdentity();
+
+ gl.ClearColor(0.3,0.3,0.3,0.0);
+ gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+ gl.Begin(gl.QUADS);
+ gl.Color3f(1, 0, 0);
+ gl.Vertex3f(-1, -1, 0);
+ gl.Vertex3f(1, -1, 0);
+ gl.Color3f(1, 1, 0);
+ gl.Vertex3f(1, 1, 0);
+ gl.Vertex3f(-1, 1, 0);
+ gl.End();
+
+}
+
+ntk.createClient(main);
+function main(err, app) {
+ var wnd = app.createWindow({title: "Close me!", x: 100, y: 100, width: 300, height: 300});
+ wnd.map();
+ var gl = wnd.getContext('opengl');
+ gl.Enable(gl.POINT_SMOOTH);
+
+ function drawAndFinish() {
+ draw(gl);
+ gl.SwapBuffers();
+ gl.Finish(drawAndFinish);
+ }
+ drawAndFinish();
+
+ wnd.setMouseHintOnly(true);
+ wnd.on('resize', function(ev) {
+ width = ev.width;
+ height = ev.height;
+ draw(gl);
+ gl.SwapBuffers();
+ }).on('mousemove', function(ev) {
+ console.log(ev.x, ev.y);
+ wnd.queryPointer(function(err, pt) {
+ console.log(pt);
+ });
+ });
+}
View
@@ -0,0 +1,130 @@
+var x11 = require('x11');
+var EventEmitter = require('events').EventEmitter;
+var util = require('util');
+
+module.exports.createClient = function(main) {
+ x11.createClient(function(error, display) {
+ if (error)
+ return main(error, null);
+ display.client.require('glx', function(glx) {
+ display.GLX = glx;
+ var app = new App(display);
+ main(null, app);
+ });
+ });
+};
+
+function App(display, glx) {
+ this.display = display;
+ this.display.X = display.client;
+}
+
+App.prototype.createWindow = function(args) {
+ return new Window(this.display, args);
+}
+
+function Window(display, args)
+{
+ var X = display.client;
+ this.display = display;
+ this.width = args.width ? args.width : 100;
+ this.height = args.height ? args.height : 100;
+ this.y = args.y ? args.y : 0;
+ this.y = args.y ? args.y : 0;
+ this.id = X.AllocID();
+ this.eventMask = 0; //x11.eventMask.PointerMotion|x11.eventMask.Exposure;
+ X.CreateWindow(this.id, this.display.screen[0].root, this.x, this.y, this.width, this.height,
+ 0, 0, 0, 0,
+ {eventMask: this.eventMask}
+ );
+ if (args.title) {
+ this.setTitle(args.title);
+ }
+
+ X.on('error', console.log);
+
+ X.event_consumers[this.id] = this;
+ this.on('event', function(ev) {
+ // translate to ntk event and re-emit
+ // console.log(ev);
+ var ntkev = ev;
+ ntkev.window = this;
+ switch(ev.type) {
+ case 6:
+ this.emit('mousemove', ntkev);
+ break;
+ case 4:
+ this.emit('mousedown', ntkev);
+ break;
+ case 22:
+ this.emit('resize', ntkev);
+ break;
+ default:
+ console.log(ev);
+ }
+ });
+
+ var name2mask = {
+ 'mousedown': x11.eventMask.PointerMotion,
+ 'mousemove': x11.eventMask.PointerMotion,
+ 'mouseover': x11.eventMask.EnterWindow,
+ 'mouseout': x11.eventMask.LeaveWindow,
+ 'mousedown': x11.eventMask.ButtonPress,
+ 'resize': x11.eventMask.StructureNotify
+ };
+
+ this.on('newListener', function(name) {
+ // check eventMask and add handler if missing
+ var eventMask = name2mask[name];
+ if ( (eventMask & this.eventMask) == 0) {
+ this.eventMask |= eventMask;
+ X.ChangeWindowAttributes(this.id, { eventMask: this.eventMask}, function() { console.trace(); console.log(arguments); });
+ console.log('New event mask: ', this.eventMask);
+ }
+ });
+}
+util.inherits(Window, EventEmitter);
+
+Window.prototype.map = function() {
+ this.display.client.MapWindow(this.id);
+ return this;
+}
+Window.prototype.unmap = function() {
+ this.display.client.UnmapWindow(this.id);
+ return this;
+}
+
+Window.prototype.setTitle = function(title) {
+ this.display.client.ChangeProperty(0, this.id, this.display.client.atoms.WM_NAME, this.display.client.atoms.STRING, 8, title);
+}
+
+Window.prototype.setMouseHintOnly = function(isOn) {
+ if (isOn && !(this.eventMask & x11.eventMask.PointerMotionHint )) {
+ this.eventMask |= x11.eventMask.PointerMotionHint;
+
+ } else if (!isOn && (this.eventMask & x11.eventMask.PointerMotionHint )) {
+ this.eventMask -= x11.eventMask.PointerMotionHint;
+ } else
+ return;
+ this.display.X.ChangeWindowAttributes(this.id, { eventMask: this.eventMask}, function() { console.trace(); console.log(arguments); });
+}
+
+Window.prototype.queryPointer = function(callback) {
+ this.display.X.QueryPointer(this.id, callback);
+};
+
+Window.prototype.getContext = function(name) {
+ switch(name) {
+ case 'opengl':
+ var visual = 161;
+ var ctx = this.display.X.AllocID();
+ this.display.GLX.CreateContext(ctx, visual, 0, 0, 0);
+ this.display.GLX.MakeCurrent(this.id, ctx, 0, function() {});
+ var gl = this.display.GLX.renderPipeline(ctx);
+ swapBuffers = gl.SwapBuffers;
+ var id = this.id;
+ gl.SwapBuffers = function() { swapBuffers.call(gl, id); };
+ return gl;
+ }
+ throw new Error('Context not supported: ' + name);
+}
View
@@ -0,0 +1,29 @@
+{ "name" : "ntk"
+, "author": "Andrey Sidorov <sidorares@yandex.ru>"
+, "description": "Desktop UI tollkit/widgets set"
+, "keywords": ["ui", "gui", "widgets", "desktop"]
+, "homepage": "https://github.com/sidorares/node-ntk"
+, "version" : "0.1.0"
+, "maintainers" :
+ [ { "name": "Andrey Sidorov"
+ , "email": "sidoares@yandex.ru"
+ }
+ ]
+, "bugs" : { "url" : "http://github.com/sidorares/node-ntk/issues" }
+, "licenses" : [ { "type" : "MIT" } ]
+, "repository" : { "type" : "git", "url" : "http://github.com/sidorares/node-ntk.git" }
+
+, "main" : "./lib/index.js"
+, "engines" : { "node" : "*" }
+, "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ }
+, "dependencies": {
+ "x11": "0.1.2"
+}
+, "scripts": {
+ "test": "mocha",
+ "prepublish" : "npm prune"
+ }
+}

0 comments on commit 59c48b7

Please sign in to comment.