1 | var Frame = require('./frame')
|
2 | , Hand = require('./hand')
|
3 | , Pointable = require('./pointable')
|
4 | , Finger = require('./finger')
|
5 | , CircularBuffer = require("./circular_buffer")
|
6 | , Pipeline = require("./pipeline")
|
7 | , EventEmitter = require('events').EventEmitter
|
8 | , gestureListener = require('./gesture').gestureListener
|
9 | , Dialog = require('./dialog')
|
10 | , _ = require('underscore');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var Controller = module.exports = function(opts) {
|
44 | var inNode = (typeof(process) !== 'undefined' && process.versions && process.versions.node),
|
45 | controller = this;
|
46 |
|
47 | opts = _.defaults(opts || {}, {
|
48 | inNode: inNode
|
49 | });
|
50 |
|
51 | this.inNode = opts.inNode;
|
52 |
|
53 | opts = _.defaults(opts || {}, {
|
54 | frameEventName: this.useAnimationLoop() ? 'animationFrame' : 'deviceFrame',
|
55 | suppressAnimationLoop: !this.useAnimationLoop(),
|
56 | loopWhileDisconnected: false,
|
57 | useAllPlugins: false,
|
58 | checkVersion: true
|
59 | });
|
60 |
|
61 | this.animationFrameRequested = false;
|
62 | this.onAnimationFrame = function() {
|
63 | controller.emit('animationFrame', controller.lastConnectionFrame);
|
64 | if (controller.loopWhileDisconnected && (controller.connection.focusedState || controller.connection.opts.background) ){
|
65 | window.requestAnimationFrame(controller.onAnimationFrame);
|
66 | }else{
|
67 | controller.animationFrameRequested = false;
|
68 | }
|
69 | }
|
70 | this.suppressAnimationLoop = opts.suppressAnimationLoop;
|
71 | this.loopWhileDisconnected = opts.loopWhileDisconnected;
|
72 | this.frameEventName = opts.frameEventName;
|
73 | this.useAllPlugins = opts.useAllPlugins;
|
74 | this.history = new CircularBuffer(200);
|
75 | this.lastFrame = Frame.Invalid;
|
76 | this.lastValidFrame = Frame.Invalid;
|
77 | this.lastConnectionFrame = Frame.Invalid;
|
78 | this.accumulatedGestures = [];
|
79 | this.checkVersion = opts.checkVersion;
|
80 | if (opts.connectionType === undefined) {
|
81 | this.connectionType = (this.inBrowser() ? require('./connection/browser') : require('./connection/node'));
|
82 | } else {
|
83 | this.connectionType = opts.connectionType;
|
84 | }
|
85 | this.connection = new this.connectionType(opts);
|
86 | this.streamingCount = 0;
|
87 | this.devices = {};
|
88 | this.plugins = {};
|
89 | this._pluginPipelineSteps = {};
|
90 | this._pluginExtendedMethods = {};
|
91 | if (opts.useAllPlugins) this.useRegisteredPlugins();
|
92 | this.setupFrameEvents(opts);
|
93 | this.setupConnectionEvents();
|
94 | }
|
95 |
|
96 | Controller.prototype.gesture = function(type, cb) {
|
97 | var creator = gestureListener(this, type);
|
98 | if (cb !== undefined) {
|
99 | creator.stop(cb);
|
100 | }
|
101 | return creator;
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | Controller.prototype.setBackground = function(state) {
|
108 | this.connection.setBackground(state);
|
109 | return this;
|
110 | }
|
111 |
|
112 | Controller.prototype.inBrowser = function() {
|
113 | return !this.inNode;
|
114 | }
|
115 |
|
116 | Controller.prototype.useAnimationLoop = function() {
|
117 | return this.inBrowser() && !this.inBackgroundPage();
|
118 | }
|
119 |
|
120 | Controller.prototype.inBackgroundPage = function(){
|
121 |
|
122 | return (typeof(chrome) !== "undefined") &&
|
123 | chrome.extension &&
|
124 | chrome.extension.getBackgroundPage &&
|
125 | (chrome.extension.getBackgroundPage() === window)
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | Controller.prototype.connect = function() {
|
132 | this.connection.connect();
|
133 | return this;
|
134 | }
|
135 |
|
136 | Controller.prototype.streaming = function() {
|
137 | return this.streamingCount > 0;
|
138 | }
|
139 |
|
140 | Controller.prototype.connected = function() {
|
141 | return !!this.connection.connected;
|
142 | }
|
143 |
|
144 | Controller.prototype.runAnimationLoop = function(){
|
145 | if (!this.suppressAnimationLoop && !this.animationFrameRequested) {
|
146 | this.animationFrameRequested = true;
|
147 | window.requestAnimationFrame(this.onAnimationFrame);
|
148 | }
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | Controller.prototype.disconnect = function() {
|
155 | this.connection.disconnect();
|
156 | return this;
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | Controller.prototype.frame = function(num) {
|
176 | return this.history.get(num) || Frame.Invalid;
|
177 | }
|
178 |
|
179 | Controller.prototype.loop = function(callback) {
|
180 | if (callback) {
|
181 | if (typeof callback === 'function'){
|
182 | this.on(this.frameEventName, callback);
|
183 | }else{
|
184 |
|
185 | this.setupFrameEvents(callback);
|
186 | }
|
187 | }
|
188 |
|
189 | return this.connect();
|
190 | }
|
191 |
|
192 | Controller.prototype.addStep = function(step) {
|
193 | if (!this.pipeline) this.pipeline = new Pipeline(this);
|
194 | this.pipeline.addStep(step);
|
195 | }
|
196 |
|
197 |
|
198 | Controller.prototype.processFrame = function(frame) {
|
199 | if (frame.gestures) {
|
200 | this.accumulatedGestures = this.accumulatedGestures.concat(frame.gestures);
|
201 | }
|
202 |
|
203 | this.lastConnectionFrame = frame;
|
204 | this.runAnimationLoop();
|
205 | this.emit('deviceFrame', frame);
|
206 | }
|
207 |
|
208 |
|
209 | Controller.prototype.processFinishedFrame = function(frame) {
|
210 | this.lastFrame = frame;
|
211 | if (frame.valid) {
|
212 | this.lastValidFrame = frame;
|
213 | }
|
214 | frame.controller = this;
|
215 | frame.historyIdx = this.history.push(frame);
|
216 | if (frame.gestures) {
|
217 | frame.gestures = this.accumulatedGestures;
|
218 | this.accumulatedGestures = [];
|
219 | for (var gestureIdx = 0; gestureIdx != frame.gestures.length; gestureIdx++) {
|
220 | this.emit("gesture", frame.gestures[gestureIdx], frame);
|
221 | }
|
222 | }
|
223 | if (this.pipeline) {
|
224 | frame = this.pipeline.run(frame);
|
225 | if (!frame) frame = Frame.Invalid;
|
226 | }
|
227 | this.emit('frame', frame);
|
228 | this.emitHandEvents(frame);
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | Controller.prototype.emitHandEvents = function(frame){
|
238 | for (var i = 0; i < frame.hands.length; i++){
|
239 | this.emit('hand', frame.hands[i]);
|
240 | }
|
241 | }
|
242 |
|
243 | Controller.prototype.setupFrameEvents = function(opts){
|
244 | if (opts.frame){
|
245 | this.on('frame', opts.frame);
|
246 | }
|
247 | if (opts.hand){
|
248 | this.on('hand', opts.hand);
|
249 | }
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | Controller.prototype.setupConnectionEvents = function() {
|
275 | var controller = this;
|
276 | this.connection.on('frame', function(frame) {
|
277 | controller.processFrame(frame);
|
278 | });
|
279 |
|
280 | this.on(this.frameEventName, function(frame) {
|
281 | controller.processFinishedFrame(frame);
|
282 | });
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | var backfillStreamingStartedEventsHandler = function(){
|
288 | if (controller.connection.opts.requestProtocolVersion < 5 && controller.streamingCount == 0){
|
289 | controller.streamingCount = 1;
|
290 | var info = {
|
291 | attached: true,
|
292 | streaming: true,
|
293 | type: 'unknown',
|
294 | id: "Lx00000000000"
|
295 | };
|
296 | controller.devices[info.id] = info;
|
297 |
|
298 | controller.emit('deviceAttached', info);
|
299 | controller.emit('deviceStreaming', info);
|
300 | controller.emit('streamingStarted', info);
|
301 | controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler)
|
302 | }
|
303 | }
|
304 |
|
305 | var backfillStreamingStoppedEvents = function(){
|
306 | if (controller.streamingCount > 0) {
|
307 | for (var deviceId in controller.devices){
|
308 | controller.emit('deviceStopped', controller.devices[deviceId]);
|
309 | controller.emit('deviceRemoved', controller.devices[deviceId]);
|
310 | }
|
311 |
|
312 | controller.emit('streamingStopped', controller.devices[deviceId]);
|
313 |
|
314 | controller.streamingCount = 0;
|
315 |
|
316 | for (var deviceId in controller.devices){
|
317 | delete controller.devices[deviceId];
|
318 | }
|
319 | }
|
320 | }
|
321 |
|
322 | this.connection.on('focus', function() { controller.emit('focus'); });
|
323 | this.connection.on('blur', function() { controller.emit('blur') });
|
324 | this.connection.on('protocol', function(protocol) { controller.emit('protocol', protocol); });
|
325 | this.connection.on('ready', function() {
|
326 |
|
327 | if (controller.checkVersion && !controller.inNode){
|
328 |
|
329 | controller.checkOutOfDate();
|
330 | }
|
331 |
|
332 | controller.emit('ready');
|
333 | });
|
334 |
|
335 | this.connection.on('connect', function() {
|
336 | controller.emit('connect');
|
337 | controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler)
|
338 | controller.connection.on('frame', backfillStreamingStartedEventsHandler);
|
339 | });
|
340 |
|
341 | this.connection.on('disconnect', function() {
|
342 | controller.emit('disconnect');
|
343 | backfillStreamingStoppedEvents();
|
344 | });
|
345 |
|
346 |
|
347 |
|
348 | this.connection.on('deviceConnect', function(evt) {
|
349 | if (evt.state){
|
350 | controller.emit('deviceConnected');
|
351 | controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler)
|
352 | controller.connection.on('frame', backfillStreamingStartedEventsHandler);
|
353 | }else{
|
354 | controller.emit('deviceDisconnected');
|
355 | backfillStreamingStoppedEvents();
|
356 | }
|
357 | });
|
358 |
|
359 |
|
360 | this.connection.on('deviceEvent', function(evt) {
|
361 | var info = evt.state,
|
362 | oldInfo = controller.devices[info.id];
|
363 |
|
364 |
|
365 | var changed = {};
|
366 | for(var property in info) {
|
367 |
|
368 | if( !oldInfo || !oldInfo.hasOwnProperty(property) || oldInfo[property] != info[property] ) {
|
369 | changed[property] = true;
|
370 | }
|
371 | }
|
372 |
|
373 |
|
374 | controller.devices[info.id] = info;
|
375 |
|
376 |
|
377 | if(changed.attached) {
|
378 | controller.emit(info.attached ? 'deviceAttached' : 'deviceRemoved', info);
|
379 | }
|
380 |
|
381 | if(!changed.streaming) return;
|
382 |
|
383 | if(info.streaming) {
|
384 | controller.streamingCount++;
|
385 | controller.emit('deviceStreaming', info);
|
386 | if( controller.streamingCount == 1 ) {
|
387 | controller.emit('streamingStarted', info);
|
388 | }
|
389 |
|
390 |
|
391 | if(!changed.attached) {
|
392 | controller.emit('deviceConnected');
|
393 | }
|
394 | }
|
395 |
|
396 | else if(!(changed.attached && info.attached)) {
|
397 | controller.streamingCount--;
|
398 | controller.emit('deviceStopped', info);
|
399 | if(controller.streamingCount == 0){
|
400 | controller.emit('streamingStopped', info);
|
401 | }
|
402 | controller.emit('deviceDisconnected');
|
403 | }
|
404 |
|
405 | });
|
406 |
|
407 |
|
408 | this.on('newListener', function(event, listener) {
|
409 | if( event == 'deviceConnected' || event == 'deviceDisconnected' ) {
|
410 | console.warn(event + " events are depricated. Consider using 'streamingStarted/streamingStopped' or 'deviceStreaming/deviceStopped' instead");
|
411 | }
|
412 | });
|
413 |
|
414 | };
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | Controller.prototype.checkOutOfDate = function(){
|
421 | console.assert(this.connection && this.connection.protocol);
|
422 |
|
423 | var serviceVersion = this.connection.protocol.serviceVersion;
|
424 | var protocolVersion = this.connection.protocol.version;
|
425 | var defaultProtocolVersion = this.connectionType.defaultProtocolVersion;
|
426 |
|
427 | if (defaultProtocolVersion > protocolVersion){
|
428 |
|
429 | console.warn("Your Protocol Version is v" + protocolVersion +
|
430 | ", this app was designed for v" + defaultProtocolVersion);
|
431 |
|
432 | Dialog.warnOutOfDate({
|
433 | sV: serviceVersion,
|
434 | pV: protocolVersion
|
435 | });
|
436 | return true
|
437 | }else{
|
438 | return false
|
439 | }
|
440 |
|
441 | };
|
442 |
|
443 |
|
444 |
|
445 | Controller._pluginFactories = {};
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 | Controller.plugin = function(pluginName, factory) {
|
528 | if (this._pluginFactories[pluginName]) {
|
529 | console.warn("Plugin \"" + pluginName + "\" already registered");
|
530 | }
|
531 | return this._pluginFactories[pluginName] = factory;
|
532 | };
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 | Controller.plugins = function() {
|
539 | return _.keys(this._pluginFactories);
|
540 | };
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 | Controller.prototype.use = function(pluginName, options) {
|
557 | var functionOrHash, pluginFactory, key, pluginInstance, klass;
|
558 |
|
559 | pluginFactory = (typeof pluginName == 'function') ? pluginName : Controller._pluginFactories[pluginName];
|
560 |
|
561 | if (!pluginFactory) {
|
562 | throw 'Leap Plugin ' + pluginName + ' not found.';
|
563 | }
|
564 |
|
565 | options || (options = {});
|
566 |
|
567 | if (this.plugins[pluginName]){
|
568 | _.extend(this.plugins[pluginName], options)
|
569 | return this;
|
570 | }
|
571 |
|
572 | this.plugins[pluginName] = options;
|
573 |
|
574 | pluginInstance = pluginFactory.call(this, options);
|
575 |
|
576 | for (key in pluginInstance) {
|
577 | functionOrHash = pluginInstance[key];
|
578 |
|
579 | if (typeof functionOrHash === 'function') {
|
580 | if (!this.pipeline) this.pipeline = new Pipeline(this);
|
581 | if (!this._pluginPipelineSteps[pluginName]) this._pluginPipelineSteps[pluginName] = [];
|
582 |
|
583 | this._pluginPipelineSteps[pluginName].push( this.pipeline.addWrappedStep(key, functionOrHash) );
|
584 | } else {
|
585 | if (!this._pluginExtendedMethods[pluginName]) this._pluginExtendedMethods[pluginName] = [];
|
586 |
|
587 | switch (key) {
|
588 | case 'frame':
|
589 | klass = Frame
|
590 | break;
|
591 | case 'hand':
|
592 | klass = Hand
|
593 | break;
|
594 | case 'pointable':
|
595 | klass = Pointable;
|
596 | _.extend(Finger.prototype, functionOrHash);
|
597 | _.extend(Finger.Invalid, functionOrHash);
|
598 | break;
|
599 | case 'finger':
|
600 | klass = Finger;
|
601 | break;
|
602 | default:
|
603 | throw pluginName + ' specifies invalid object type "' + key + '" for prototypical extension'
|
604 | }
|
605 |
|
606 | _.extend(klass.prototype, functionOrHash);
|
607 | _.extend(klass.Invalid, functionOrHash);
|
608 | this._pluginExtendedMethods[pluginName].push([klass, functionOrHash])
|
609 | }
|
610 | }
|
611 | return this;
|
612 | };
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 | Controller.prototype.stopUsing = function (pluginName) {
|
624 | var steps = this._pluginPipelineSteps[pluginName],
|
625 | extMethodHashes = this._pluginExtendedMethods[pluginName],
|
626 | i = 0, klass, extMethodHash;
|
627 |
|
628 | if (!this.plugins[pluginName]) return;
|
629 |
|
630 | if (steps) {
|
631 | for (i = 0; i < steps.length; i++) {
|
632 | this.pipeline.removeStep(steps[i]);
|
633 | }
|
634 | }
|
635 |
|
636 | if (extMethodHashes){
|
637 | for (i = 0; i < extMethodHashes.length; i++){
|
638 | klass = extMethodHashes[i][0]
|
639 | extMethodHash = extMethodHashes[i][1]
|
640 | for (var methodName in extMethodHash) {
|
641 | delete klass.prototype[methodName]
|
642 | delete klass.Invalid[methodName]
|
643 | }
|
644 | }
|
645 | }
|
646 |
|
647 | delete this.plugins[pluginName]
|
648 |
|
649 | return this;
|
650 | }
|
651 |
|
652 | Controller.prototype.useRegisteredPlugins = function(){
|
653 | for (var plugin in Controller._pluginFactories){
|
654 | this.use(plugin);
|
655 | }
|
656 | }
|
657 |
|
658 |
|
659 | _.extend(Controller.prototype, EventEmitter.prototype);
|