forked from santigimeno/node-uinput
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
175 lines (149 loc) · 4.51 KB
/
index.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
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
const fs = require('fs');
const bindings = require('bindings')('uinput');
const ioctl = require('ioctl');
const InputEvent = bindings.input_event;
const Events = bindings.events;
// Throughout this file there's an assumption that the host machine is little endian.
// struct input_id {
// __u16 bustype;
// __u16 vendor;
// __u16 product;
// __u16 version;
// };
function InputId(options) {
const buffer = Buffer.alloc(4 * 2);
buffer.writeUInt16LE(options.busType, 0);
buffer.writeUInt16LE(options.vendor, 2);
buffer.writeUInt16LE(options.product, 4);
buffer.writeUInt16LE(options.version, 6);
return buffer;
}
function DeviceName(name) {
const buf = Buffer.alloc(Events.UINPUT_MAX_NAME_SIZE).fill(0);
if (name) {
buf.write(name, 0);
}
return buf;
}
function AbsArray(abs) {
const buf = Buffer.alloc(Events.ABS_CNT * 4).fill(0);
if (abs) {
for (let i = 0; i < abs.length; i++) {
buf.writeUInt32LE(abs[i].value, abs[i].offset * 4);
}
}
return buf;
}
const Abs = (offset, value) => {
return {
offset: offset,
value: value
};
};
function UInputUserDev(options) {
const name = DeviceName(options.name);
const id = InputId(options.id);
const ffEffectsMax = Buffer.alloc(4);
ffEffectsMax.writeUInt32LE(options.ffEffectsMax || 0, 0);
const absMax = AbsArray(options.absMax);
const absMin = AbsArray(options.absMin);
const absFuzz = AbsArray(options.absFuzz);
const absFlat = AbsArray(options.absFlat);
return Buffer.concat([
name,
id,
ffEffectsMax,
absMax,
absMin,
absFuzz,
absFlat
]);
}
class UInput {
constructor(stream) {
this.stream = stream;
}
async write(data) {
return new Promise((resolve, reject) => {
this.stream.once('error', reject);
this.stream.write(data, (err) => {
this.stream.removeAllListeners('error');
if (err) {
return reject(err);
}
resolve();
});
});
}
async create(options) {
if (!options.id) {
throw new Error('Device id params is mandatory');
}
const userDev = UInputUserDev(options);
return new Promise((resolve, reject) => {
this.stream.once('error', reject);
this.stream.write(userDev, (err) => {
if (ioctl(this.stream.fd, Events.UI_DEV_CREATE)) {
throw new Error('Could not create uinput device');
}
this.stream.removeAllListeners('error');
resolve();
});
});
}
/**
* @param type
* @param code
* @param value
* @param syn
* @returns {Promise<void>}
*/
async sendEvent(type, code, value, syn = true) {
await this.write(InputEvent(type, code, value));
if (syn) {
await this.write(InputEvent(Events.EV_SYN, Events.SYN_REPORT, 0));
}
}
async keyEvent(code, press = true) {
/* press / click */
if (press) {
await this.sendEvent(Events.EV_KEY, code, 1);
} else {
await this.sendEvent(Events.EV_KEY, code, 0);
}
}
async emitCombo(code) {
// Press each of the keys in series
for (let i = 0; i < code.length; i++) {
await this.sendEvent(Events.EV_KEY, code[i], 1);
}
// Release them in reverse
for (let i = code.length; i-- > 0;) {
await this.sendEvent(Events.EV_KEY, code[i], 0);
}
}
}
async function setup(options) {
return new Promise((resolve, reject) => {
const stream = fs.createWriteStream('/dev/uinput');
const uinput = new UInput(stream);
stream.once('error', reject);
stream.on('open', (fd) => {
const events = Object.keys(options);
events.forEach((event) => {
const indexEvent = Events[event];
const values = options[event];
(values || []).forEach(value => {
if (ioctl(fd, indexEvent, value) || undefined === indexEvent) {
throw new Error("Could not setup: " + event + ': ' + value);
}
});
});
stream.removeAllListeners('error');
resolve(uinput);
});
});
}
module.exports = Events;
module.exports.Abs = Abs;
module.exports.setup = setup;