linux-events 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/get-codes.js ADDED
@@ -0,0 +1,25 @@
1
+ import fs from 'fs';
2
+ import util from 'util';
3
+
4
+ let { positionals } = util.parseArgs({
5
+ args: process.argv,
6
+ allowPositionals: true,
7
+ strict: true
8
+ });
9
+
10
+ let path = positionals[2] || '/usr/include/linux/input-event-codes.h';
11
+ console.log(`Parsing ${path}`);
12
+ let codes = {};
13
+ let lines = [];
14
+ (await fs.promises.readFile(path)).toString().split('\n').forEach(line => {
15
+ line = line.trim();
16
+ if (!line.startsWith('#define')) return;
17
+ let args = line.slice(8).trim().split('\t');
18
+ if (args.length == 1) return;
19
+ let code = args[0].trim();
20
+ let value = args.slice(1).join(' ').trim();
21
+ if (value.includes('/*')) value = value.slice(0, value.indexOf('/*'));
22
+ lines.push(`let ${code} = ${value};`, `if (codes[${code}] == null) codes[${code}] = [];`, `codes[${code}].push('${code}');`);
23
+ })
24
+ eval(lines.join('\n'));
25
+ await fs.promises.writeFile('codes.json', JSON.stringify(codes, 0, 2));
package/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import os from 'os';
2
+ import fs from 'fs';
3
+ import events from 'node:events';
4
+ import codes from './codes.json' with { type: 'json' };
5
+
6
+ let endianness = os.endianness();
7
+
8
+ export async function listDevices(options = {}) {
9
+ let files = [];
10
+ for (let file of await fs.promises.readdir('/dev/input')) {
11
+ file = { device: file };
12
+ if (!(await fs.promises.stat(`/dev/input/${file.device}`)).isCharacterDevice()) continue;
13
+ files.push(file);
14
+ if (options.info) {
15
+ try {
16
+ await fs.promises.access(`/sys/class/input/${file.device}/device`);
17
+ } catch (err) {
18
+ continue;
19
+ }
20
+ file.name = (await fs.promises.readFile(`/sys/class/input/${file.device}/device/name`)).toString();
21
+ }
22
+ }
23
+ return files;
24
+ }
25
+
26
+ export class DeviceListener extends events.EventEmitter {
27
+ constructor(device, ...args) {
28
+ if (typeof device !== 'string') throw new Error('device must be a string');
29
+ super(...args);
30
+ this.device = device;
31
+ if (!device.startsWith('event')) throw new Error(`Input type of "${device}" is not supported.`);
32
+ fs.access(`/sys/class/input/${device}/device`, (err) => {
33
+ if (err) throw err;
34
+ this.stream = fs.createReadStream(`/dev/input/${device}`);
35
+ this.stream.on('data', data => {
36
+ for (let i = 0; i < data.length;) {
37
+ let tv_sec = data.slice(i, i += 8)[`readBigInt64${endianness}`]();
38
+ let tv_usec = data.slice(i, i += 8)[`readBigInt64${endianness}`]();
39
+ let type = data.slice(i, i += 2)[`readInt16${endianness}`]();
40
+ type = codes[type].find(a => a.startsWith('EV_'));
41
+ type = type.slice(type.indexOf('_') + 1);
42
+ let codeTypes = type == 'KEY' ? ['KEY', 'BTN'] : [type];
43
+ if (codeTypes[0] == 'KEY') codeTypes.push('BTN');
44
+ let code = data.slice(i, i += 2)[`readInt16${endianness}`]();
45
+ code = codes[code].find(a => codeTypes.includes(a.split('_')[0])) || code;
46
+ let value = data.slice(i, i += 4)[`readInt32${endianness}`]();
47
+ this.emit('data', { tv_sec, tv_usec, type, code, value });
48
+ }
49
+ });
50
+ this.stream.on('error', err => this.emit('error', err));
51
+ this.stream.on('close', () => this.emit('end'));
52
+ this.stream.on('end', () => this.emit('end'));
53
+ });
54
+ }
55
+
56
+ stop() {
57
+ this.stream.destroy();
58
+ this.emit('end');
59
+ }
60
+ }
61
+
62
+ export default { listDevices, DeviceListener };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "linux-events",
3
+ "version": "1.0.0",
4
+ "description": "A small package for listening to evdev (/dev/input/eventX) device streams on Linux",
5
+ "keywords": [
6
+ "evdev",
7
+ "linux",
8
+ "input",
9
+ "events",
10
+ "devices"
11
+ ],
12
+ "homepage": "https://github.com/kgurchiek/Linux-Events#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/kgurchiek/Linux-Events/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/kgurchiek/Linux-Events.git"
19
+ },
20
+ "license": "ISC",
21
+ "author": "kgurchiek",
22
+ "type": "module",
23
+ "main": "index.js"
24
+ }