js/yubi.js
changeset 0 52d30e6014a0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/yubi.js	Mon Feb 09 09:20:01 2015 -0800
@@ -0,0 +1,77 @@
+
+/*######################################################################
+### I N I T I A L I Z A T I O N
+######################################################################*/
+var Y = global.Y;
+
+// Global jQuery callback queues.  Used across the application to trigger actions
+// on various events.
+//
+Y.callback = function( name, flags ) {
+        if ( ! Y.callbacks ) Y.callbacks = {};
+
+        // Create a new callback queue if one doesn't already exist
+        // for the provided +name+.
+        //
+        if ( ! Y.callbacks[name] ) {
+                if ( ! flags ) flags = 'unique';
+                Y.callbacks[name] = $.Callbacks( flags );
+        }
+
+        return Y.callbacks[name];
+};
+
+// Node's gui interface.
+Y.gui = require( 'nw.gui' );
+
+
+// Execute yubi external processes.
+//
+Y.yubiexec = function( cmd, args ) {
+
+	// Add the dynamic lib path for libyubi.
+	//
+	var lib = './yubi/' + process.platform + '/lib';
+	var env = process.env;
+	env[ 'DYLD_LIBRARY_PATH' ] = env[ 'DYLD_LIBRARY_PATH' ] + ':' + lib;
+	// FIXME:  need to add linux and windows
+
+	if ( ! args ) args = [];
+	var binary = './yubi/' + process.platform + '/bin/' + cmd;
+	Y.fs.chmodSync( binary, 0755 );
+	var child = Y.spawn( binary, args, { env: env });
+
+	if ( ! child ) {
+		Y.callback( cmd + '-fatal' ).fire();
+		return;
+	}
+
+	// Send callbacks when data is received.
+	//
+	child.stdout.on( 'data', function(data) {
+		Y.callback( cmd ).fire( data );
+	});
+	child.stderr.on( 'data', function(data) {
+		Y.callback( cmd + '-error' ).fire( data );
+	});
+
+	// Log exit code.
+	//
+	child.on( 'close', function(code) {
+		console.log( cmd + ' process exited with code: ' + code);
+	});
+};
+
+
+// Callback behaviors for ykinfo execution.
+//
+Y.callback( 'ykinfo' ).add( function(data) {
+	Y.keyinfo = {};
+	$.each( data.toString().split("\n"), function(i, info) {
+		var match = info.match( /^(\w+):\s+(.+)$/ );
+		if ( match ) Y.keyinfo[ match[1] ] = match[2];
+	});
+});
+Y.callback( 'ykinfo-fatal' ).add( function() { Y.keyinfo = {} } );
+Y.callback( 'ykinfo-error' ).add( function() { Y.keyinfo = {} } );
+