DJCtrlEmu/main.js

102 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-10-03 06:53:25 -04:00
const path = require("node:path");
const fs = require("node:fs");
const electron = require("electron");
let window;
let driver;
let mappings;
electron.app.on("ready", function () {
window = new electron.BrowserWindow({
"width": 1280,
"height": 480,
"backgroundColor": "#080C10FF",
"title": "DJ Controller Emulator",
"webPreferences": {
"preload": path.join(__dirname, "preload.js")
},
"show": false
});
window.loadFile(path.join(__dirname, "view", "index.html"));
window.once("ready-to-show", function () {
window.show();
});
});
electron.app.on("window-all-closed", function () {
electron.app.quit();
});
electron.ipcMain.handle("load-driver", function (event, driverName) {
if (driver) {
driver.close();
delete driver;
}
var driverPath = path.join(__dirname, "drivers", path.basename(driverName) + ".js");
driver = require(driverPath);
return true;
});
electron.ipcMain.handle("load-mappings", function (event, mappingsName) {
if (mappings) {
delete mappings;
}
var mappingsPath = path.join(__dirname, "mappings", path.basename(mappingsName) + ".json");
mappings = JSON.parse(fs.readFileSync(mappingsPath));
return true;
});
electron.ipcMain.handle("list-devices", function (event) {
return driver.list();
});
electron.ipcMain.handle("open-device", function (event, name) {
return driver.open(name, function (message) {
var byteArray = new Array(...message);
var hexStringifiedMessage = byteArray.map(b => b.toString(16)).map(c => ("00".substring(c.length) + c)).join(" ");
for (var mappingRegExpString in mappings.inbound) {
if (mappings.inbound.hasOwnProperty(mappingRegExpString)) {
var mappingRegExp = new RegExp(mappingRegExpString);
var mappingMatches = hexStringifiedMessage.match(mappingRegExp);
if (!mappingMatches) continue;
var mappingName = mappings.inbound[mappingRegExpString];
mappingMatches.shift();
var value = parseInt(mappingMatches[0], 16);
window.webContents.send("signal", mappingName, value);
}
}
});
});
electron.ipcMain.handle("control", function (event, name, value = 0x00) {
if (!driver) return false;
if (!mappings.outbound[name]) return false;
var message = mappings.outbound[name].map(function (byte) {
if (typeof byte != "number") {
return value;
} else {
return byte;
}
});
driver.send(message);
});
electron.ipcMain.handle("close-device", function (event) {
return driver.close();
});