-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (169 loc) · 5.31 KB
/
index.js
File metadata and controls
192 lines (169 loc) · 5.31 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var fs = require('fs');
var bindings = require('bindings')('uinput');
var ioctl = require('ioctl');
var ref = require('ref');
var ArrayType = require('ref-array');
var StructType = require('ref-struct');
require('array.prototype.fill');
var async = require('async');
// struct input_id {
// __u16 bustype;
// __u16 vendor;
// __u16 product;
// __u16 version;
// };
var InputId = StructType({
bustype : ref.types.uint16,
vendor : ref.types.uint16,
product : ref.types.uint16,
version : ref.types.uint16,
});
// struct uinput_user_dev {
// char name[UINPUT_MAX_NAME_SIZE];
// struct input_id id;
// int ff_effects_max;
// int absmax[ABS_CNT];
// int absmin[ABS_CNT];
// int absfuzz[ABS_CNT];
// int absflat[ABS_CNT];
// };
var DeviceName = ArrayType(ref.types.char, bindings.UINPUT_MAX_NAME_SIZE);
var AbsArray = ArrayType(ref.types.int, bindings.ABS_CNT);
var UInputUserDev = StructType({
name : DeviceName,
id : InputId,
ff_effects_max : ref.types.int,
absmax : AbsArray,
absmin : AbsArray,
absfuzz : AbsArray,
absflat : AbsArray
});
var ioctls = [
undefined,
bindings.UI_SET_KEYBIT,
bindings.UI_SET_RELBIT,
bindings.UI_SET_ABSBIT,
bindings.UI_SET_MSCBIT,
bindings.UI_SET_LEDBIT,
bindings.UI_SET_SNDBIT,
bindings.UI_SET_FFBIT,
bindings.UI_SET_PHYS,
bindings.UI_SET_SWBIT,
bindings.UI_SET_PROPBIT
];
function setup(options, cb) {
var stream = fs.createWriteStream('/dev/uinput');
stream.once('error', cb);
stream.on('open', function(fd) {
var events = Object.keys(options);
for (var i = 0; i < events.length; ++ i) {
var ev = events[i];
if (ioctl(fd, bindings.UI_SET_EVBIT, bindings[ev])) {
return cb(new Error("Could not listen for event: " + ev));
}
for (var j = 0; j < (options[ev]).length; ++ j) {
var val = options[ev][j];
if (ioctl(fd, ioctls[bindings[ev]], val)) {
return cb(new Error("Could not setup: " + val));
}
}
}
stream.removeAllListeners('error');
cb(undefined, stream);
});
}
function create(stream, options, cb) {
if (!options.id) {
return cb(new Error('Device id params is mandatory'));
}
var zero_array = new Array(bindings.ABS_CNT).fill(0);
var user_dev = new UInputUserDev({
id : options.id,
ff_effects_max : options.ff_effects_max || 0,
absmax : options.absmax ? new AbsArray(options.absmax) : new AbsArray(zero_array),
absmin : options.absmin ? new AbsArray(options.absmin) : new AbsArray(zero_array),
absfuzz : options.absfuzz ? new AbsArray(options.absfuzz) : new AbsArray(zero_array),
absflat : options.absflat ? new AbsArray(options.absflat) : new AbsArray(zero_array)
});
var name = new Buffer(new Array(80));
name.write(options.name);
user_dev.name = new DeviceName(name);
stream.once('error', cb);
stream.write(user_dev.ref(), function(err) {
if (err) {
return cb(err);
}
stream.removeAllListeners('error');
if (ioctl(stream.fd, bindings.UI_DEV_CREATE)) {
cb(new Error('Could not create uinput device'));
} else {
cb();
}
});
}
var input_event = bindings.input_event;
function send_event(stream, type, code, value, cb) {
var ev = input_event(type, code, value);
stream.once('error', cb);
stream.write(ev, function(err) {
if (err) {
return cb(err);
}
ev = input_event(bindings.EV_SYN, bindings.SYN_REPORT, 0);
stream.write(ev, cb);
});
}
function key_event(stream, code, cb) {
/* press / click */
send_event(stream, bindings.EV_KEY, code, 1, function(err) {
if (err) {
return cb(err);
}
/* release / unclick */
send_event(stream, bindings.EV_KEY, code, 0, cb);
});
}
function emit_combo(stream, code, cb) {
/*
We save a copy array keys because function reverse()
change original array and we need this in the function
emit_combo.
*/
var copy = code.slice();
var reverse = code.reverse();
async.eachSeries(
copy,
function(item, callback) {
send_event(stream, bindings.EV_KEY, item, 1, callback);
/*
First, we send event with value = 1 to
press the keys in array copy and generate combo.
Example:
RightShift + key_a --- > A
*/
},
function(err) {
if (err) {
return cb(err);
}
async.eachSeries(
reverse,
function(item, callback) {
send_event(stream, bindings.EV_KEY, item, 0, callback);
/*
Last, we send event with value = 0 to release keys
and do not keep them pressed.
*/
},
cb
);
}
);
}
module.exports = bindings;
delete module.exports.input_event;
module.exports.setup = setup;
module.exports.create = create;
module.exports.send_event = send_event;
module.exports.key_event = key_event;
module.exports.emit_combo = emit_combo;