-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.js
80 lines (68 loc) · 2.07 KB
/
device.js
1
2
3
4
5
6
7
8
9
10
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var ll = require("./lowlevel");
var headers = require("./headers");
var EE = require("events").EventEmitter;
var fs = require("fs");
var util = require("util");
function ignore() {};
function Device(options) {
if (! this instanceof Device) return new Device(options);
EE.call(this);
this.name = options.name || "Node Device";
this.vendor = options.vendor || 1;
this.product = options.product || 1;
this.version = options.version || 1;
this.path = options.path || "/dev/uinput";
this.timeout = options.timeout || 10;
this.unmasking = {};
this.setup = false;
this.out = fs.createWriteStream(this.path);
var unmask = function (type, name) {
this.unmasking[type+"_"+name] = "unmasking";
var cb = this.unmasked.bind(this, type, name);
ll.unmask(this.out, type, name, cb);
}.bind(this);
this.once('setup', this.create.bind(this));
for (TYPE in headers.INPUT_H.EV) (function (TYPE) {
var type = TYPE.toLowerCase();
if (TYPE === "VERSION") return;
if (options[type] !== undefined) {
this.out.on('open', function() {
unmask("EV", TYPE);
if (options[type] instanceof String) {
unmask(TYPE, options[type]);
} else if (options[type] instanceof Array) {
options[type].forEach(unmask.bind({}, TYPE));
}
});
}
}).bind(this)(TYPE);
}
util.inherits(Device, EE);
Device.prototype.unmasked = function (TYPE, NAME, err) {
if (err) {
console.error("Error unmasking: %s", err);
return;
}
delete this.unmasking[TYPE+"_"+NAME];
if (TYPE === "EV") {
var type = NAME.toLowerCase();
this[type] = ll.inject.bind(this, this.out, NAME); // (name, value, cb)
}
var done = true;
for (x in this.unmasking) { done = false; break; }
if (done) {
this.syn = ll.inject.bind(this, this.out, "SYN", "REPORT", 0); // (, cb)
this.setup = true;
this.emit("setup");
}
}
Device.prototype.create = function() {
ll.create(this.out, this.name, this.vendor, this.product, this.version, function(err) {
if (err) {
console.error(err);
return;
}
setTimeout(function(){this.emit("ready");}.bind(this), this.timeout);
}.bind(this));
}
module.exports = Device;