Compare commits
10 commits
918b2d74fa
...
ab70ed3414
Author | SHA1 | Date | |
---|---|---|---|
ab70ed3414 | |||
746b39e1e8 | |||
9d52b870d4 | |||
bb1cca0433 | |||
c13b123423 | |||
eccbc863b3 | |||
d21e9ef018 | |||
842c432aa3 | |||
e408c07387 | |||
d75a18c192 |
9 changed files with 181 additions and 105 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
config.json
|
||||
node_modules/
|
||||
yarn.lock
|
||||
npm-debug.log
|
||||
|
|
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
|
@ -10,7 +10,9 @@
|
|||
"<node_internals>/**"
|
||||
],
|
||||
"program": "${workspaceFolder}/ninwa.js",
|
||||
"args": []
|
||||
"args": [
|
||||
"example_config.json"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
20
LICENSE.md
Normal file
20
LICENSE.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# MIT License
|
||||
**Copyright (c) 2022 Daniel Sommer \<daniel.sommer@velvettear.de\>**
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**
|
12
README.md
12
README.md
|
@ -46,13 +46,17 @@ node input watcher
|
|||
- reload systemd-services
|
||||
`systemctl daemon-reload`
|
||||
|
||||
- enable and start the ninwa as a systemd-service
|
||||
- enable and start ninwa as a systemd-service
|
||||
`systemctl enable --now ninwa`
|
||||
|
||||
## configuration
|
||||
|
||||
configuration is done entirely within the file `config.json`.
|
||||
|
||||
### server: [*object*]
|
||||
- address: [*string*] server address to listen on (`0.0.0.0` to listen on all interfaces)
|
||||
- port: [*number*] port to listen on
|
||||
|
||||
### log: [*object*]
|
||||
- level: [*string*] verbosity of the log; either `debug`, `info`, `warning` or `error`
|
||||
- timestamp: [*string*] format string for the timestamp; review [moment.js](https://momentjs.com/docs/#/displaying/format/) for further information
|
||||
|
@ -78,3 +82,9 @@ configuration is done entirely within the file `config.json`.
|
|||
- cmd: [*string*] command / path to script to execute
|
||||
- args: [*string-array*] arguments to pass to the executed command
|
||||
- sudo: [*boolean*] use sudo to execute the command
|
||||
|
||||
**note:**
|
||||
if you intend to use ninwa as a systemd-service with `sudo` make sure the configured user is allowed to execute the command without entering a password. **otherwise your service will just stop at the password prompt!**
|
||||
|
||||
- `sudo visudo`
|
||||
- `username ALL=(ALL) NOPASSWD: command`
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
|
||||
},
|
||||
"combos": {
|
||||
"delay": 5000
|
||||
"timeout": 5000
|
||||
},
|
||||
"watchers": [
|
||||
{
|
||||
|
@ -15,25 +15,31 @@
|
|||
{
|
||||
"key": "key_f1",
|
||||
"combo": [
|
||||
"key_f2",
|
||||
"key_f4"
|
||||
"key_f5"
|
||||
],
|
||||
"event": "EV_KEY",
|
||||
"type": "keydown",
|
||||
"delay": 1000,
|
||||
"command": "combo"
|
||||
},
|
||||
{
|
||||
"key": "key_f1",
|
||||
"combo": [
|
||||
"key_f1",
|
||||
"key_f2"
|
||||
"key_f6"
|
||||
],
|
||||
"event": "EV_KEY",
|
||||
"type": "keydown",
|
||||
"delay": 1000,
|
||||
"command": "combo2"
|
||||
},
|
||||
{
|
||||
"key": "key_f2",
|
||||
"combo": [
|
||||
"key_f5",
|
||||
"key_f6"
|
||||
],
|
||||
"event": "EV_KEY",
|
||||
"type": "keydown",
|
||||
"command": "combo3"
|
||||
},
|
||||
{
|
||||
"key": "key_enter",
|
||||
"type": "keydown",
|
||||
|
@ -73,6 +79,14 @@
|
|||
],
|
||||
"sudo": false
|
||||
},
|
||||
"combo3": {
|
||||
"cmd": "notify-send",
|
||||
"args": [
|
||||
"combo #3",
|
||||
"third combo"
|
||||
],
|
||||
"sudo": false
|
||||
},
|
||||
"example": {
|
||||
"cmd": "notify-send",
|
||||
"args": [
|
||||
|
@ -94,7 +108,6 @@
|
|||
"args": [
|
||||
"https://velvettear.de"
|
||||
]
|
||||
},
|
||||
"sudo": false
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
const comboConfig = require('../config.json').combos;
|
||||
const commandConfig = require('../config.json').commands;
|
||||
const logger = require('./logger.js');
|
||||
|
||||
const LINE_START = 'Event: time';
|
||||
|
||||
|
@ -13,16 +12,21 @@ class Keyfilter {
|
|||
return;
|
||||
}
|
||||
this.actions = new Map();
|
||||
this.registered = {
|
||||
keys: [],
|
||||
events: [],
|
||||
types: []
|
||||
}
|
||||
for (let index = 0; index < config.length; index++) {
|
||||
this.setAction(config[index]);
|
||||
this.registerAction(config[index]);
|
||||
}
|
||||
this.currentCombo = undefined;
|
||||
}
|
||||
getCommand(command) {
|
||||
if (command === undefined || commandConfig === undefined) {
|
||||
if (command === undefined || global.config?.commands === undefined) {
|
||||
return;
|
||||
}
|
||||
const result = commandConfig[command];
|
||||
const result = global.config.commands[command];
|
||||
if (result === undefined) {
|
||||
return;
|
||||
}
|
||||
|
@ -52,13 +56,17 @@ class Keyfilter {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
setAction(config) {
|
||||
registerAction(config) {
|
||||
let key = config.key.toUpperCase();
|
||||
if (config.combo !== undefined && config.combo.length > 0) {
|
||||
const tmp = JSON.parse(JSON.stringify(config.combo));
|
||||
tmp.unshift(config.key);
|
||||
key = tmp.toString().toUpperCase();
|
||||
}
|
||||
if (Array.from(this.actions.keys()).includes(key)) {
|
||||
logger.warn('skipping already registered key(s) \'' + key + '\'...');
|
||||
return;
|
||||
}
|
||||
this.actions.set(key,
|
||||
{
|
||||
type: this.getActionType(config.type),
|
||||
|
@ -73,57 +81,70 @@ class Keyfilter {
|
|||
}(),
|
||||
}
|
||||
);
|
||||
const singleKeys = key.split(',');
|
||||
for (let index = 0; index < singleKeys.length; index++) {
|
||||
const singleKey = singleKeys[index];
|
||||
if (!this.registered.keys.includes(singleKey)) {
|
||||
this.registered.keys.push(singleKey);
|
||||
}
|
||||
}
|
||||
if (!this.registered.events.includes(this.actions.get(key).event)) {
|
||||
this.registered.events.push(this.actions.get(key).event);
|
||||
}
|
||||
if (!this.registered.types.includes(this.actions.get(key).type.id)) {
|
||||
this.registered.types.push(this.actions.get(key).type.id);
|
||||
}
|
||||
}
|
||||
filter(input) {
|
||||
if (input === undefined || input.length === 0) {
|
||||
return;
|
||||
}
|
||||
input = input.toString();
|
||||
let lines = input.split("\n");
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
let line = lines[index];
|
||||
if (line.length === 0) {
|
||||
continue;
|
||||
const parsedEvent = this.parseLine(input);
|
||||
if (this.parsedEventIsUnknown(parsedEvent)) {
|
||||
return;
|
||||
}
|
||||
if (!line.startsWith(LINE_START)) {
|
||||
continue;
|
||||
if (this.isPartOfCombo(parsedEvent)) {
|
||||
if (parsedEvent.ignore) {
|
||||
return;
|
||||
}
|
||||
const parsedEvent = this.parseLine(line);
|
||||
if (parsedEvent === undefined) {
|
||||
continue;
|
||||
}
|
||||
for (let [key, event] of this.actions) {
|
||||
if (this.currentCombo === undefined && !this.isParsedEventValid(key, event, parsedEvent)) {
|
||||
continue;
|
||||
return this.setComboResult(this.getFilterResult(parsedEvent.key, parsedEvent.type.action));
|
||||
}
|
||||
if (this.isStartOfCombo(parsedEvent)) {
|
||||
return this.setComboResult(this.getFilterResult(parsedEvent.key, parsedEvent.type.action));
|
||||
}
|
||||
if (this.isPartOfCombo(parsedEvent)) {
|
||||
if (parsedEvent.ignore) {
|
||||
for (let [key, event] of this.actions) {
|
||||
if (!this.parsedEventIsValid(key, event, parsedEvent)) {
|
||||
continue;
|
||||
}
|
||||
return this.setComboResult(this.getFilterResult(parsedEvent.key, parsedEvent.type.action));
|
||||
}
|
||||
if (!this.isParsedEventValid(key, event, parsedEvent)) {
|
||||
continue;
|
||||
}
|
||||
if (this.shouldBeDelayed(event)) {
|
||||
const result = this.getFilterResult(parsedEvent.key, event.type.action, event.command);
|
||||
if (this.shouldBeDelayed(event)) {
|
||||
result.delayed = true;
|
||||
return result;
|
||||
}
|
||||
event.captured = new Date().getTime();
|
||||
return this.getFilterResult(parsedEvent.key, event.type.action, event.command);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
parsedEventIsUnknown(parsedEvent) {
|
||||
return parsedEvent === undefined || !this.registered.keys.includes(parsedEvent.key) || !this.registered.events.includes(parsedEvent.event) || !this.registered.types.includes(parsedEvent.type.id);
|
||||
}
|
||||
isParsedEventValid(key, value, parsed) {
|
||||
let keyCheck = key === parsed.key;
|
||||
if (value.combo !== undefined && value.combo.length > 0) {
|
||||
keyCheck = key.includes(parsed.key);
|
||||
parsedEventIsValid(key, value, parsed) {
|
||||
if (value.event !== parsed.event || value.type.id !== parsed.type.id) {
|
||||
return false;
|
||||
}
|
||||
return keyCheck && (value.event === undefined || value.event === parsed.event) && value.type.id === parsed.type.id;
|
||||
if (value.combo === undefined || value.combo.length === 0) {
|
||||
return key === parsed.key;
|
||||
}
|
||||
if (this.currentCombo === undefined) {
|
||||
return key.startsWith(parsed.key);
|
||||
}
|
||||
for (let index = 0; index < this.currentCombo.possibilities.length; index++) {
|
||||
if (this.currentCombo.possibilities[index].combo[0].toUpperCase() === parsed.key) {
|
||||
this.resetCurrentCombo();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
setComboResult(result) {
|
||||
if (result === undefined || this.currentCombo === undefined) {
|
||||
|
@ -151,6 +172,9 @@ class Keyfilter {
|
|||
};
|
||||
}
|
||||
parseLine(line) {
|
||||
if (line === undefined || line.length === 0 || !line.startsWith(LINE_START)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const parts = line.split(',');
|
||||
const event = parts[1].substring(parts[1].indexOf('(') + 1, parts[1].lastIndexOf(')'));
|
||||
|
@ -168,15 +192,12 @@ class Keyfilter {
|
|||
return new Date().getTime() - event.captured < event.delay;
|
||||
}
|
||||
isStartOfCombo(parsedEvent) {
|
||||
if (this.currentCombo !== undefined) {
|
||||
return false;
|
||||
}
|
||||
let possibilities = [];
|
||||
for (let [actionKey, actionEvent] of this.actions) {
|
||||
if (actionEvent.combo === undefined || actionEvent.combo.length === 0) {
|
||||
continue;
|
||||
}
|
||||
if (!actionKey.toUpperCase().startsWith(parsedEvent.key)) {
|
||||
if (!actionKey.toUpperCase().startsWith(parsedEvent.key) || actionEvent.type.id !== parsedEvent.type.id || actionEvent.event !== parsedEvent.event) {
|
||||
continue;
|
||||
}
|
||||
possibilities.push(JSON.parse(JSON.stringify(actionEvent)));
|
||||
|
@ -192,24 +213,18 @@ class Keyfilter {
|
|||
done: [parsedEvent.key],
|
||||
possibilities: possibilities
|
||||
};
|
||||
this.setComboTimeout();
|
||||
return true;
|
||||
}
|
||||
isPartOfCombo(parsedEvent) {
|
||||
if (this.hasComboTimedOut()) {
|
||||
this.resetCurrentCombo();
|
||||
}
|
||||
if (this.currentCombo === undefined || this.currentCombo.type.id !== parsedEvent.type.id || this.currentCombo.event !== parsedEvent.event) {
|
||||
return false;
|
||||
}
|
||||
// if (this.currentCombo.done.includes(parsedEvent.key)) {
|
||||
// parsedEvent.ignore = true;
|
||||
// return true;
|
||||
// }
|
||||
let possibilities = [];
|
||||
for (let index = 0; index < this.currentCombo.possibilities.length; index++) {
|
||||
const possibility = this.currentCombo.possibilities[index];
|
||||
if (possibility.combo.length === 0) {
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
if (possibility.combo[0].toUpperCase() !== parsedEvent.key) {
|
||||
continue;
|
||||
|
@ -232,17 +247,29 @@ class Keyfilter {
|
|||
tmp.done = this.currentCombo.done;
|
||||
this.currentCombo = tmp;
|
||||
}
|
||||
this.setComboTimeout();
|
||||
return true;
|
||||
}
|
||||
hasComboTimedOut() {
|
||||
return false;
|
||||
return comboConfig !== undefined &&
|
||||
comboConfig.delay !== undefined &&
|
||||
this.currentCombo !== undefined &&
|
||||
this.currentCombo.timestamp !== undefined &&
|
||||
new Date().getTime() - this.currentCombo.timestamp > comboConfig.delay;
|
||||
setComboTimeout() {
|
||||
if (this.currentCombo === undefined || global.config?.combos?.timeout === undefined || isNaN(global.config?.combos?.timeout)) {
|
||||
return;
|
||||
}
|
||||
this.clearComboTimeout();
|
||||
logger.debug('setting timeout for current combo to ' + parseInt(global.config.combos.timeout) + 'ms');
|
||||
this.comboTimeout = setTimeout(() => {
|
||||
this.resetCurrentCombo()
|
||||
}, parseInt(global.config.combos.timeout));
|
||||
}
|
||||
clearComboTimeout() {
|
||||
if (this.comboTimeout === undefined) {
|
||||
return;
|
||||
}
|
||||
logger.debug('clearing timeout for current combo');
|
||||
clearTimeout(this.comboTimeout);
|
||||
}
|
||||
resetCurrentCombo() {
|
||||
this.clearComboTimeout();
|
||||
logger.debug('resetting current combo');
|
||||
this.currentCombo = undefined;
|
||||
}
|
||||
isValid() {
|
||||
|
|
|
@ -62,30 +62,13 @@ class Watcher {
|
|||
}
|
||||
logger.debug('adding stdout listener to watcher \'' + this.device + '\'...');
|
||||
this.process.stdout.on('data', (data) => {
|
||||
if (this.keyfilter == undefined) {
|
||||
if (this.keyfilter === undefined) {
|
||||
return;
|
||||
}
|
||||
let filtered = this.keyfilter.filter(data);
|
||||
if (filtered === undefined) {
|
||||
return;
|
||||
const lines = data.toString().split('\n');
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
this.handleEvent(this.keyfilter.filter(lines[index]));
|
||||
}
|
||||
logger.debug('handling captured \'' + filtered.type + '\' event for key \'' + filtered.key + '\' from watcher \'' + this.device + '\'...');
|
||||
if (filtered.delayed) {
|
||||
logger.debug('delaying captured event...');
|
||||
return;
|
||||
}
|
||||
if (filtered.combo) {
|
||||
if (!filtered.combo.finished) {
|
||||
logger.debug('captured event is part of ' + filtered.combo.possibilities + ' possible combo(s) and not yet finished')
|
||||
return;
|
||||
}
|
||||
logger.debug('captured event finished combo \'' + filtered.combo.done.toString().toUpperCase()+ '\'');
|
||||
}
|
||||
this.keyfilter.resetCurrentCombo();
|
||||
logger.info('executing command \'' + filtered.command.name + '\' registered for captured event...');
|
||||
cli.execute(filtered.command.cmd, filtered.command.args, filtered.command.sudo)
|
||||
.then(logger.info)
|
||||
.catch(logger.error);
|
||||
});
|
||||
}
|
||||
addStdErrListener() {
|
||||
|
@ -155,5 +138,27 @@ class Watcher {
|
|||
.catch(reject);
|
||||
});
|
||||
}
|
||||
handleEvent(event) {
|
||||
if (event === undefined) {
|
||||
return;
|
||||
}
|
||||
logger.debug('handling captured \'' + event.type + '\' event for key \'' + event.key + '\' from watcher \'' + this.device + '\'...');
|
||||
if (event.delayed) {
|
||||
logger.debug('delaying captured event...');
|
||||
return;
|
||||
}
|
||||
if (event.combo) {
|
||||
if (!event.combo.finished) {
|
||||
logger.debug('captured event is part of ' + event.combo.possibilities + ' possible combo(s) and not yet finished')
|
||||
return;
|
||||
}
|
||||
logger.debug('captured event finished combo \'' + event.combo.done.toString().toUpperCase() + '\'');
|
||||
}
|
||||
this.keyfilter.resetCurrentCombo();
|
||||
logger.info('executing command \'' + event.command.name + '\' registered for captured event...');
|
||||
cli.execute(event.command.cmd, event.command.args, event.command.sudo)
|
||||
.then(logger.info)
|
||||
.catch(logger.error);
|
||||
}
|
||||
}
|
||||
module.exports = Watcher;
|
10
ninwa.js
10
ninwa.js
|
@ -5,24 +5,22 @@ const packageJSON = require('./package.json');
|
|||
|
||||
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
|
||||
|
||||
let config;
|
||||
|
||||
main();
|
||||
|
||||
async function main() {
|
||||
handleInterrupts();
|
||||
try {
|
||||
let configFile = await util.getFileInfo(process.argv[2] || __dirname + '/config.json');
|
||||
config = require(configFile.path);
|
||||
config.path = config.path;
|
||||
global.config = require(configFile.path);
|
||||
global.config.path = configFile.path;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
logger.initialize(config.log.level, config.log.timestamp);
|
||||
logger.initialize(global.config.log.level, global.config.log.timestamp);
|
||||
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
|
||||
await watchers.initialize(config.watchers);
|
||||
await watchers.initialize(global.config.watchers);
|
||||
await watchers.start();
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
|
|
Loading…
Reference in a new issue