package: package refactoring
This commit is contained in:
parent
1b2da1750a
commit
d0cc7f51aa
145 changed files with 175 additions and 847 deletions
343
src/main/callbacks.js
Normal file
343
src/main/callbacks.js
Normal file
|
|
@ -0,0 +1,343 @@
|
|||
var path = require('path'),
|
||||
fs = require('fs'),
|
||||
settings = require('./settings'),
|
||||
osc = require('./osc'),
|
||||
{ipc} = require('./server'),
|
||||
{deepCopy} = require('./utils'),
|
||||
theme = require('./theme'),
|
||||
chokidar = require('chokidar')
|
||||
|
||||
var openedSessions = {},
|
||||
widgetHashTable = {},
|
||||
lastSavingClient,
|
||||
sessionBackups = {}
|
||||
|
||||
module.exports = {
|
||||
|
||||
ready: function(data,clientId) {
|
||||
ipc.send('connected')
|
||||
|
||||
if (settings.read('readOnly')) {
|
||||
ipc.send('readOnly')
|
||||
}
|
||||
|
||||
if (data.backupId && sessionBackups[data.backupId]) {
|
||||
ipc.send('loadBackup', sessionBackups[data.backupId])
|
||||
return
|
||||
}
|
||||
|
||||
if (settings.read('newSession')) {
|
||||
ipc.send('sessionNew')
|
||||
return
|
||||
}
|
||||
|
||||
if (settings.read('sessionFile')) return this.sessionOpen({path:settings.read('sessionFile')},clientId)
|
||||
|
||||
var recentSessions = settings.read('recentSessions')
|
||||
|
||||
if (settings.read('examples')) {
|
||||
var dir = path.resolve(__dirname + '/../examples')
|
||||
recentSessions = fs.readdirSync(dir)
|
||||
recentSessions = recentSessions.map(function(file){return dir + '/' + file})
|
||||
}
|
||||
|
||||
ipc.send('sessionList',recentSessions,clientId)
|
||||
},
|
||||
|
||||
sessionAddToHistory: function(data) {
|
||||
var sessionlist = settings.read('recentSessions')
|
||||
|
||||
fs.lstat(data,(err, stats)=>{
|
||||
|
||||
if (err || !stats.isFile()) return
|
||||
|
||||
// add session to history
|
||||
sessionlist.unshift(path.resolve(data))
|
||||
// remove doubles from history
|
||||
sessionlist = sessionlist.filter(function(elem, index, self) {
|
||||
return index == self.indexOf(elem)
|
||||
})
|
||||
// save history
|
||||
settings.write('recentSessions',sessionlist)
|
||||
|
||||
})
|
||||
},
|
||||
|
||||
sessionRemoveFromHistory: function(data) {
|
||||
var sessionlist = settings.read('recentSessions')
|
||||
if (sessionlist.indexOf(data) > -1) {
|
||||
sessionlist.splice(sessionlist.indexOf(data),1)
|
||||
settings.write('recentSessions',sessionlist)
|
||||
}
|
||||
},
|
||||
|
||||
sessionOpen: function(data,clientId) {
|
||||
|
||||
var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(),
|
||||
error = file === false && data.path ? 'Session file "' + data.path + '" not found.' : false,
|
||||
session
|
||||
|
||||
try {
|
||||
session = JSON.parse(file)
|
||||
} catch(err) {
|
||||
error = err
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
|
||||
ipc.send('sessionOpen', session,clientId)
|
||||
|
||||
|
||||
for (var i in openedSessions) {
|
||||
if (openedSessions[i].indexOf(clientId) != -1) {
|
||||
openedSessions[i].splice(openedSessions[i].indexOf(clientId), 1)
|
||||
}
|
||||
}
|
||||
|
||||
if (data.path) {
|
||||
|
||||
if (!settings.read('readOnly')) this.sessionAddToHistory(data.path)
|
||||
|
||||
fs.lstat(data.path, (err, stats)=>{
|
||||
|
||||
if (err || !stats.isFile()) return
|
||||
|
||||
|
||||
if (!openedSessions[data.path]) {
|
||||
|
||||
openedSessions[data.path] = []
|
||||
|
||||
var watchFile = ()=>{
|
||||
var watcher = chokidar.watch(data.path)
|
||||
watcher.on('change',()=>{
|
||||
var openedSessionsClone = deepCopy(openedSessions[data.path])
|
||||
for (var k in openedSessionsClone) {
|
||||
if (openedSessionsClone[k] != lastSavingClient) {
|
||||
module.exports.sessionOpen({path:data.path}, openedSessionsClone[k])
|
||||
}
|
||||
}
|
||||
watcher.close()
|
||||
watchFile()
|
||||
})
|
||||
}
|
||||
|
||||
watchFile()
|
||||
|
||||
|
||||
}
|
||||
|
||||
openedSessions[data.path].push(clientId)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
ipc.send('error', `Invalid session file (${error})`)
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
sessionOpened: function(data, clientId) {
|
||||
|
||||
if (settings.read('stateFile')) {
|
||||
var send = true
|
||||
for (var id in ipc.clients) {
|
||||
// only make the client send its osc state if there are no other active clients
|
||||
if (id !== clientId && ipc.clients[id].connected()) {
|
||||
send = false
|
||||
}
|
||||
}
|
||||
var state = {
|
||||
state: settings.read('stateFile'),
|
||||
send: send
|
||||
}
|
||||
ipc.send('stateLoad', state, clientId)
|
||||
}
|
||||
|
||||
ipc.send('stateSend', null, null, clientId)
|
||||
|
||||
},
|
||||
|
||||
savingSession: function(data, clientId) {
|
||||
|
||||
lastSavingClient = clientId
|
||||
|
||||
},
|
||||
|
||||
syncOsc: function(shortdata, clientId) {
|
||||
|
||||
if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return
|
||||
|
||||
var value = shortdata.v,
|
||||
data = widgetHashTable[clientId][shortdata.h]
|
||||
|
||||
data = deepCopy(data)
|
||||
for (var k in shortdata) {
|
||||
data[k] = shortdata[k]
|
||||
}
|
||||
|
||||
data.args = data.preArgs ? data.preArgs.concat(value) : [value]
|
||||
|
||||
if (!data.noSync) ipc.send('receiveOsc', data, null, clientId)
|
||||
|
||||
|
||||
},
|
||||
|
||||
sendOsc: function(shortdata, clientId) {
|
||||
|
||||
if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return
|
||||
|
||||
var value = shortdata.v,
|
||||
data = widgetHashTable[clientId][shortdata.h]
|
||||
|
||||
data = deepCopy(data)
|
||||
for (var k in shortdata) {
|
||||
data[k] = shortdata[k]
|
||||
}
|
||||
|
||||
data.args = data.preArgs ? data.preArgs.concat(value) : [value]
|
||||
|
||||
if (data.target) {
|
||||
|
||||
var targets = []
|
||||
|
||||
if (data.target.indexOf(null) === -1 && settings.read('targets') && !shortdata.target) Array.prototype.push.apply(targets, settings.read('targets'))
|
||||
if (data.target) Array.prototype.push.apply(targets, data.target)
|
||||
|
||||
for (var i in targets) {
|
||||
|
||||
if (targets[i] === 'self') {
|
||||
ipc.send('receiveOsc',data,clientId)
|
||||
continue
|
||||
} else if (targets[i] === null) {
|
||||
continue
|
||||
}
|
||||
|
||||
var host = targets[i].split(':')[0],
|
||||
port = targets[i].split(':')[1]
|
||||
|
||||
if (port) {
|
||||
|
||||
if (data.split) {
|
||||
|
||||
for (var j in data.split) {
|
||||
osc.send(host,port,data.split[j],data.args[j],data.precision)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
osc.send(host,port,data.address,data.args,data.precision)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!data.noSync) ipc.send('receiveOsc', data, null, clientId)
|
||||
|
||||
},
|
||||
|
||||
addWidget(data, clientId) {
|
||||
|
||||
if (!widgetHashTable[clientId]) {
|
||||
widgetHashTable[clientId] = {}
|
||||
}
|
||||
|
||||
if (!widgetHashTable[clientId][data.hash]) {
|
||||
widgetHashTable[clientId][data.hash] = {}
|
||||
}
|
||||
|
||||
var cache = widgetHashTable[clientId][data.hash],
|
||||
widgetData = data.data
|
||||
|
||||
for (var k in widgetData) {
|
||||
if ((k === 'target' || k === 'preArgs')) {
|
||||
if (widgetData[k] !== '') {
|
||||
cache[k] = Array.isArray(widgetData[k]) ? widgetData[k] : [widgetData[k]]
|
||||
} else {
|
||||
cache[k] = []
|
||||
}
|
||||
} else {
|
||||
cache[k] = widgetData[k]
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
removeWidget(data, clientId) {
|
||||
|
||||
delete widgetHashTable[clientId][data.hash]
|
||||
|
||||
},
|
||||
|
||||
removeClientWidgets(clientId) {
|
||||
|
||||
if (widgetHashTable[clientId]) {
|
||||
delete widgetHashTable[clientId]
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
stateSave: function(data) {
|
||||
dialog.showSaveDialog(window,{title:'Save current state to preset file',defaultPath:settings.read('presetPath').replace(settings.read('presetPath').split('/').pop(),''),filters: [ { name: 'OSC Preset', extensions: ['preset'] }]},function(file){
|
||||
|
||||
if (file==undefined) {return}
|
||||
settings.write('presetPath',file)
|
||||
|
||||
if (file.indexOf('.preset')==-1){file+='.preset'}
|
||||
fs.writeFile(file,data, function(err, data) {
|
||||
if (err) throw err
|
||||
console.log('The current state was saved in '+file)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
fullscreen: function(data) {
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
},
|
||||
|
||||
reload: function(data) {
|
||||
|
||||
ipc.send('reload')
|
||||
|
||||
},
|
||||
|
||||
storeBackup: function(data) {
|
||||
|
||||
sessionBackups[data.backupId] = data
|
||||
|
||||
},
|
||||
|
||||
deleteBackup: function(data) {
|
||||
|
||||
delete sessionBackups[data]
|
||||
|
||||
},
|
||||
|
||||
reloadCss:function(){
|
||||
theme.load()
|
||||
ipc.send('reloadCss')
|
||||
},
|
||||
|
||||
log: function(data) {
|
||||
console.log(data)
|
||||
},
|
||||
|
||||
error: function(data) {
|
||||
console.error(data)
|
||||
},
|
||||
|
||||
errorLog: function(data) {
|
||||
console.error(data)
|
||||
},
|
||||
|
||||
errorPopup: function(data) {
|
||||
ipc.send('error', data)
|
||||
}
|
||||
|
||||
}
|
||||
110
src/main/electron-app.js
Normal file
110
src/main/electron-app.js
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
var {app, Menu, shell} = require('electron'),
|
||||
settings = require('./settings'),
|
||||
infos = require('../package.json')
|
||||
|
||||
app.commandLine.appendSwitch('--touch-events')
|
||||
|
||||
if (settings.read('noVsync') || (!settings.cli && settings.read('argv')['disable-vsync'])) {
|
||||
app.commandLine.appendSwitch('--disable-gpu-vsync')
|
||||
}
|
||||
|
||||
if (settings.read('noGpu') || (!settings.cli && settings.read('argv')['disable-gpu'])) {
|
||||
app.disableHardwareAcceleration()
|
||||
}
|
||||
|
||||
var template = [{
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
{role: 'undo', accelerator: 'CmdOrCtrl+Z'},
|
||||
{role: 'redo', accelerator: 'Shift+CmdOrCtrl+Z'},
|
||||
{type: 'separator'},
|
||||
{role: 'cut', accelerator: 'CmdOrCtrl+X'},
|
||||
{role: 'copy', accelerator: 'CmdOrCtrl+C'},
|
||||
{role: 'paste', accelerator: 'CmdOrCtrl+V'}
|
||||
]
|
||||
}]
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
// Add app menu (OS X)
|
||||
template.unshift({
|
||||
label: settings.read('appName'),
|
||||
submenu: [
|
||||
{
|
||||
label: 'Hide ' + infos.productName,
|
||||
accelerator: 'Command+H',
|
||||
role: 'hide'
|
||||
},
|
||||
{
|
||||
label: 'Hide Others',
|
||||
accelerator: 'Command+Alt+H',
|
||||
role: 'hideothers'
|
||||
},
|
||||
{
|
||||
label: 'Show All',
|
||||
role: 'unhide'
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Quit',
|
||||
accelerator: 'Command+Q',
|
||||
click: ()=>app.quit()
|
||||
}
|
||||
]
|
||||
})
|
||||
template.push(
|
||||
{
|
||||
label: 'Window',
|
||||
role: 'window',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Minimize',
|
||||
accelerator: 'CmdOrCtrl+M',
|
||||
role: 'minimize'
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Bring All to Front',
|
||||
role: 'front'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Help',
|
||||
role: 'help',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Documentation',
|
||||
click: ()=>shell.openExternal(infos.homepage)
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
label: 'Report an Issue',
|
||||
click: ()=>shell.openExternal(infos.repository.url + '/issues')
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
app.on('before-quit',()=>{
|
||||
process.exit()
|
||||
})
|
||||
|
||||
app.on('ready',()=>{
|
||||
var menu = Menu.buildFromTemplate(template)
|
||||
Menu.setApplicationMenu(menu)
|
||||
})
|
||||
|
||||
app.on('window-all-closed', function() {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
87
src/main/electron-window.js
Normal file
87
src/main/electron-window.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
var path = require('path'),
|
||||
{BrowserWindow, dialog} = require('electron'),
|
||||
shortcut = require('electron-localshortcut'),
|
||||
settings = require('./settings'),
|
||||
theme = require('./theme')
|
||||
|
||||
|
||||
module.exports = function(options={}) {
|
||||
|
||||
var window
|
||||
|
||||
window = new BrowserWindow({
|
||||
width: options.width || 800,
|
||||
height: options.height || 600,
|
||||
title: options.title || settings.read('appName'),
|
||||
icon: path.resolve(__dirname + '/../browser/logo.png'),
|
||||
backgroundColor: options.color || theme.backgroundColor,
|
||||
type: options.type,
|
||||
fullscreen: options.fullscreen,
|
||||
useContentSize: true,
|
||||
autoHideMenuBar: true,
|
||||
webPreferences: {
|
||||
nodeIntegration: !!options.node
|
||||
}
|
||||
})
|
||||
|
||||
window.webContents.on('will-prevent-unload', (event)=>{
|
||||
var choice = dialog.showMessageBox(window, {
|
||||
type: 'question',
|
||||
buttons: ['Yes', 'No'],
|
||||
title: 'Are you sure ?',
|
||||
message: 'Unsaved data will be lost. Are you sure you want to quit?',
|
||||
defaultId: 0,
|
||||
cancelId: 1
|
||||
})
|
||||
if (choice === 0) {
|
||||
event.preventDefault()
|
||||
}
|
||||
})
|
||||
|
||||
// window.webContents.on('will-navigate', (event)=>event.preventDefault())
|
||||
window.webContents.on('new-window', (event)=>event.preventDefault())
|
||||
|
||||
if (options.fullscreen) {
|
||||
window.webContents.on('dom-ready', ()=>{
|
||||
window.webContents.executeJavaScript(`
|
||||
window.ELECTRON_FULLSCREEN = true
|
||||
`)
|
||||
})
|
||||
}
|
||||
|
||||
if (options.zoom === false) {
|
||||
window.webContents.setVisualZoomLevelLimits(1, 1)
|
||||
}
|
||||
|
||||
|
||||
window.on('closed', function() {
|
||||
window = null
|
||||
})
|
||||
|
||||
window.setMenu(null)
|
||||
|
||||
window.loadURL(options.address)
|
||||
|
||||
if (options.shortcuts) {
|
||||
|
||||
shortcut.register(window,'CmdOrCtrl+R',function(){
|
||||
window.reload()
|
||||
})
|
||||
|
||||
shortcut.register(window,'F11',function(){
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
})
|
||||
|
||||
shortcut.register(window,'F12',function(){
|
||||
window.toggleDevTools()
|
||||
})
|
||||
|
||||
} else {
|
||||
shortcut.register(window,'CmdOrCtrl+R',function(e){
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
return window
|
||||
|
||||
}
|
||||
145
src/main/index.js
Normal file
145
src/main/index.js
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
var dev = process.argv[0].includes('node_modules'),
|
||||
settings = require('./settings')
|
||||
|
||||
var serverStarted
|
||||
|
||||
function nodeMode() {
|
||||
|
||||
console.warn('Running with node')
|
||||
|
||||
require('source-map-support').install({handleUncaughtExceptions: false})
|
||||
|
||||
if (!settings.read('noGui')) {
|
||||
settings.cli = true
|
||||
settings.write('noGui', true, true)
|
||||
console.warn('Headless mode (--no-gui) enabled automatically')
|
||||
}
|
||||
|
||||
process.on('uncaughtException', (err)=>{
|
||||
console.error('A JavaScript error occurred in the main process:')
|
||||
console.error(err.stack)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if (process.title === 'node' || process.title === 'node.exe') {
|
||||
|
||||
nodeMode()
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
|
||||
require('electron').dialog.showErrorBox = (title, err)=>{
|
||||
console.error(title + ': ' + err)
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
|
||||
nodeMode()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var start = function(readyApp) {
|
||||
|
||||
if (!settings.read('guiOnly') && !serverStarted) {
|
||||
|
||||
var server = require('./server'),
|
||||
osc = require('./osc'),
|
||||
callbacks = require('./callbacks'),
|
||||
zeroconf = require('./zeroconf')
|
||||
|
||||
server.bindCallbacks(callbacks)
|
||||
|
||||
serverStarted = true
|
||||
process.on('exit',()=>{
|
||||
if (osc.midi) osc.midi.stop()
|
||||
zeroconf.unpublishAll()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if (!settings.read('noGui')) {
|
||||
|
||||
var app = require('./electron-app')
|
||||
var address = typeof settings.read('guiOnly')=='string'? 'http://' + settings.read('guiOnly') : settings.read('appAddresses')[0]
|
||||
address += settings.read('urlOptions')
|
||||
|
||||
var launch = ()=>{
|
||||
var win = require('./electron-window')({address:address, shortcuts:true, zoom:false, fullscreen: settings.read('fullScreen')})
|
||||
return win
|
||||
}
|
||||
if (app.isReady()) {
|
||||
return launch()
|
||||
} else {
|
||||
app.on('ready',function(){
|
||||
launch()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (settings.cli) {
|
||||
|
||||
start()
|
||||
|
||||
} else {
|
||||
|
||||
var app = require('./electron-app'),
|
||||
path = require('path'),
|
||||
address = 'file://' + path.resolve(__dirname + '/../launcher/' + 'index.html'),
|
||||
{ipcMain} = require('electron'),
|
||||
launcher
|
||||
|
||||
app.on('ready',function(){
|
||||
global.settings = settings
|
||||
launcher = require('./electron-window')({address:address, shortcuts:dev, width:680, height:(100 + 8*3 + 29 * Object.keys(settings.options).filter(x=>settings.options[x].launcher !== false).length), node:true, color:'#253040'})
|
||||
})
|
||||
|
||||
if (process.log) {
|
||||
process.log = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process, arguments)
|
||||
launcher.webContents.send('stdout', string)
|
||||
}
|
||||
})(process.log)
|
||||
}
|
||||
|
||||
process.stdout.write = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process.stdout, arguments)
|
||||
launcher.webContents.send('stdout', string)
|
||||
}
|
||||
})(process.stdout.write)
|
||||
|
||||
process.stderr.write = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
write.apply(process.stdout, arguments)
|
||||
launcher.webContents.send('stderr', string)
|
||||
}
|
||||
})(process.stderr.write)
|
||||
|
||||
|
||||
ipcMain.on('start',function(e, options){
|
||||
|
||||
var gui = start()
|
||||
|
||||
if (settings.read('guiOnly')) {
|
||||
launcher.hide()
|
||||
gui.on('close',()=>{
|
||||
launcher.close()
|
||||
})
|
||||
} else {
|
||||
launcher.webContents.send('started')
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
119
src/main/ipc/client.js
Normal file
119
src/main/ipc/client.js
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
var EventEmitter = require('events').EventEmitter
|
||||
|
||||
var hearbeatInterval = 25000,
|
||||
hearbeatTimeout = 5000,
|
||||
dieTimeout = 60000
|
||||
|
||||
class Socket extends EventEmitter {
|
||||
|
||||
constructor(ws, id) {
|
||||
|
||||
super()
|
||||
|
||||
this.id = id
|
||||
this.socket = null
|
||||
|
||||
this.queue = []
|
||||
|
||||
this.hearbeat = undefined
|
||||
this.hearbeatTimeout = undefined
|
||||
this.on('pong', ()=>{
|
||||
this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout)
|
||||
})
|
||||
this.on('ping', ()=>{
|
||||
this.socket.send('["pong"]')
|
||||
})
|
||||
|
||||
this.dieTimeout = undefined
|
||||
|
||||
this.open(ws)
|
||||
|
||||
}
|
||||
|
||||
open(ws) {
|
||||
|
||||
this.socket = ws
|
||||
|
||||
this.socket.onmessage = (e)=>{
|
||||
this.receive(e.data)
|
||||
}
|
||||
|
||||
this.socket.onclose = this.socket.onerror = ()=>{
|
||||
this.close()
|
||||
}
|
||||
|
||||
this.dieTimeout = clearTimeout(this.dieTimeout)
|
||||
|
||||
this.hearbeat = setInterval(()=>{
|
||||
if (!this.connected()) return
|
||||
this.socket.send('["ping"]')
|
||||
this.hearbeatTimeout = setTimeout(()=>{
|
||||
if (this.connected()) this.socket.close()
|
||||
}, hearbeatTimeout)
|
||||
}, hearbeatInterval)
|
||||
|
||||
}
|
||||
|
||||
close() {
|
||||
|
||||
this.hearbeat = clearInterval(this.hearbeat)
|
||||
this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout)
|
||||
this.dieTimeout = clearTimeout(this.dieTimeout)
|
||||
|
||||
this.dieTimeout = setTimeout(()=>{
|
||||
|
||||
this.emit('close')
|
||||
|
||||
}, dieTimeout)
|
||||
|
||||
}
|
||||
|
||||
connected() {
|
||||
|
||||
return this.socket.readyState == this.socket.OPEN
|
||||
|
||||
}
|
||||
|
||||
receive(message) {
|
||||
|
||||
if (typeof message == 'string') {
|
||||
|
||||
var packet = JSON.parse(message)
|
||||
|
||||
if (Array.isArray(packet) && typeof packet[0] == 'string') {
|
||||
|
||||
this.emit(packet[0], packet[1])
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
send(event, data) {
|
||||
|
||||
var packet = JSON.stringify([event, data])
|
||||
|
||||
if (this.connected()) {
|
||||
this.socket.send(packet)
|
||||
} else {
|
||||
this.queue.push(packet)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
flush(){
|
||||
|
||||
for (var i in this.queue) {
|
||||
|
||||
if (this.connected()) this.socket.send(this.queue[i])
|
||||
|
||||
}
|
||||
|
||||
this.queue = []
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Socket
|
||||
59
src/main/ipc/server.js
Normal file
59
src/main/ipc/server.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
var EventEmitter = require('events').EventEmitter,
|
||||
WebSocketServer = require('ws').Server,
|
||||
Client = require('./client')
|
||||
|
||||
class Ipc extends EventEmitter {
|
||||
|
||||
constructor(server) {
|
||||
|
||||
super()
|
||||
|
||||
this.clients = {}
|
||||
this.server = null
|
||||
|
||||
this.server = new WebSocketServer({server: server})
|
||||
|
||||
this.server.on('connection', (socket, req)=>{
|
||||
|
||||
var id = req.url.split('/').pop()
|
||||
|
||||
if (!id) return
|
||||
|
||||
if (!this.clients[id]) {
|
||||
|
||||
var client = new Client(socket, id)
|
||||
this.clients[id] = client
|
||||
|
||||
this.emit('connection', client)
|
||||
|
||||
client.on('close', ()=>{
|
||||
delete this.clients[id]
|
||||
})
|
||||
|
||||
} else {
|
||||
|
||||
this.clients[id].open(socket)
|
||||
this.clients[id].flush()
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
send(event, data, id, except) {
|
||||
|
||||
var clients = id ? [this.clients[id]] : this.clients
|
||||
|
||||
for (var k in clients) {
|
||||
if (!except || this.clients[k] != this.clients[except]) {
|
||||
if (clients[k]) clients[k].send(event, data)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = Ipc
|
||||
64
src/main/midi.js
Normal file
64
src/main/midi.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
var {PythonShell} = require('python-shell'),
|
||||
settings = require('./settings')
|
||||
|
||||
class Midi {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.py = new PythonShell('midi.py', {
|
||||
scriptPath:__dirname,
|
||||
mode:'text',
|
||||
args: [
|
||||
settings.read('debug') ? 'debug' : '',
|
||||
...settings.read('midi')
|
||||
]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
send(data) {
|
||||
|
||||
var args = []
|
||||
for (i in data.args) {
|
||||
args.push(data.args[i].value)
|
||||
}
|
||||
|
||||
this.py.send(JSON.stringify([data.port, data.address, ...args]))
|
||||
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
||||
this.py.childProcess.kill()
|
||||
|
||||
}
|
||||
|
||||
init(receiveOsc) {
|
||||
this.py.on('message', function(message) {
|
||||
// console.log(message)
|
||||
var name, data
|
||||
try {
|
||||
[name, data] = JSON.parse(message)
|
||||
} catch (err) {
|
||||
// console.log(err)
|
||||
}
|
||||
if (name == 'log') {
|
||||
if (data.indexOf('ERROR') === 0) {
|
||||
console.error(data)
|
||||
} else {
|
||||
console.log(data)
|
||||
}
|
||||
} else if (name == 'osc') {
|
||||
receiveOsc(data)
|
||||
} else if (name == 'error') {
|
||||
console.error('ERROR: MIDI: ' + data)
|
||||
this.stop()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var midi = new Midi()
|
||||
|
||||
module.exports = midi
|
||||
234
src/main/osc/index.js
Normal file
234
src/main/osc/index.js
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// destroy serialport module
|
||||
// (electron-rebuild breaks it for node-only mode)
|
||||
require('vmodule')('serialport', {}, {global: true})
|
||||
|
||||
var ipc = require('../server').ipc,
|
||||
settings = require('../settings'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
debug = settings.read('debug'),
|
||||
vm = require('vm'),
|
||||
fs = require('fs'),
|
||||
midi = settings.read('midi') ? require('../midi') : false,
|
||||
oscUDPServer = require('./udp'),
|
||||
oscTCPServer = require('./tcp'),
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
|
||||
class OscServer {
|
||||
|
||||
constructor(){
|
||||
|
||||
this.customModuleEventEmitter = new EventEmitter()
|
||||
this.customModule = (()=>{
|
||||
|
||||
if (!settings.read('customModule')) return false
|
||||
|
||||
var file = (()=>{
|
||||
try {
|
||||
return fs.readFileSync(settings.read('customModule'),'utf8')
|
||||
} catch(err) {
|
||||
console.log('CustomModule Error: File not found: ' + settings.read('customModule'))
|
||||
return false
|
||||
}
|
||||
})(),
|
||||
mod,
|
||||
context = {
|
||||
console,
|
||||
sendOsc: this.sendOsc.bind(this),
|
||||
receiveOsc: this.receiveOsc.bind(this),
|
||||
app: this.customModuleEventEmitter,
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
settings
|
||||
}
|
||||
|
||||
try {
|
||||
mod = vm.runInContext(file, vm.createContext(context))
|
||||
} catch(err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
return mod
|
||||
|
||||
})()
|
||||
|
||||
|
||||
}
|
||||
|
||||
parseType(type){
|
||||
var t = type[0].match(/[bhtdscrmifTFNI]/)
|
||||
|
||||
if (t!==null) {
|
||||
return t[0]
|
||||
} else {
|
||||
return 's'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parseArg(arg, precision){
|
||||
if (arg==null) return null
|
||||
switch (typeof arg) {
|
||||
case 'number':
|
||||
var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null,
|
||||
typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1],
|
||||
precisionValue = parseFloat(precision) || 0
|
||||
|
||||
return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))}
|
||||
case 'boolean':
|
||||
return {type: arg ? 'T' : 'F', value: arg}
|
||||
case 'object':
|
||||
if (arg.type) {
|
||||
return {type: this.parseType(arg.type), value: arg.value}
|
||||
} else {
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
case 'string':
|
||||
return {type: 's', value:arg}
|
||||
default:
|
||||
return {type: 's', value:JSON.stringify(arg)}
|
||||
}
|
||||
}
|
||||
|
||||
sendOsc(data){
|
||||
|
||||
if (!data) return
|
||||
|
||||
if (data.host == 'midi') {
|
||||
|
||||
if (midi) midi.send(data)
|
||||
|
||||
} else {
|
||||
|
||||
if (typeof data.address !== 'string' || data.address[0] !== '/') {
|
||||
console.error('OSC error: malformed address: "' + data.address + '')
|
||||
return
|
||||
}
|
||||
|
||||
oscUDPServer.send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
}, data.host, data.port)
|
||||
|
||||
if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) {
|
||||
oscTCPServer.clients[data.host][data.port].send({
|
||||
address: data.address,
|
||||
args: data.args
|
||||
})
|
||||
}
|
||||
|
||||
if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
receiveOsc(data, info){
|
||||
|
||||
if (!data) return
|
||||
|
||||
for (i in data.args) {
|
||||
data.args[i] = data.args[i].value
|
||||
}
|
||||
|
||||
if (data.args.length==1) data.args = data.args[0]
|
||||
|
||||
ipc.send('receiveOsc',data)
|
||||
|
||||
if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port)
|
||||
|
||||
}
|
||||
|
||||
|
||||
oscInFilter(data){
|
||||
if (this.customModule.oscInFilter) {
|
||||
return this.customModule.oscInFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
oscOutFilter(data){
|
||||
if (this.customModule.oscOutFilter) {
|
||||
return this.customModule.oscOutFilter(data)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
oscInHandler(msg, timetag, info){
|
||||
var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port})
|
||||
if (!data) return
|
||||
this.receiveOsc(data)
|
||||
}
|
||||
|
||||
oscInHandlerWrapper(msg, timetag, info) {
|
||||
var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0
|
||||
if (delay) {
|
||||
setTimeout(()=>{
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}, delay)
|
||||
} else {
|
||||
this.oscInHandler(msg, timetag, info)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
init(){
|
||||
|
||||
if (midi) midi.init((data)=>{
|
||||
|
||||
data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port})
|
||||
|
||||
if (!data) return
|
||||
|
||||
this.receiveOsc(data)
|
||||
|
||||
})
|
||||
|
||||
oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscUDPServer.open()
|
||||
|
||||
if (settings.read('tcpInPort')) {
|
||||
oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this))
|
||||
oscTCPServer.open()
|
||||
}
|
||||
|
||||
if (this.customModule.init) {
|
||||
this.customModule.init()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
var oscServer = new OscServer()
|
||||
oscServer.init()
|
||||
|
||||
module.exports = {
|
||||
server: oscServer,
|
||||
send: function(host,port,address,args,precision) {
|
||||
|
||||
var message = []
|
||||
|
||||
if (!Array.isArray(args)) args = [args]
|
||||
if (typeof args=='object' && args!==null) {
|
||||
for (var i in args) {
|
||||
var arg = oscServer.parseArg(args[i],precision)
|
||||
if (arg!=null) message.push(arg)
|
||||
}
|
||||
}
|
||||
|
||||
var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port})
|
||||
|
||||
oscServer.sendOsc(data)
|
||||
|
||||
},
|
||||
midi:midi
|
||||
}
|
||||
146
src/main/osc/tcp.js
Normal file
146
src/main/osc/tcp.js
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
var osc = require('osc'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
settings = require('../settings'),
|
||||
zeroconf = require('../zeroconf'),
|
||||
tcpInPort = settings.read('tcpInPort'),
|
||||
tcpTargets = settings.read('tcpTargets'),
|
||||
net = require('net')
|
||||
|
||||
class OscTCPClient extends EventEmitter {
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super()
|
||||
|
||||
this.options = options
|
||||
this.remoteAddress = options.address
|
||||
this.remotePort = options.port
|
||||
|
||||
this.open()
|
||||
|
||||
}
|
||||
|
||||
open() {
|
||||
|
||||
this.port = new osc.TCPSocketPort(this.options)
|
||||
|
||||
this.port.on('error', (err)=>{
|
||||
console.error(`TCP: Error: ${err.message}`)
|
||||
})
|
||||
|
||||
this.port.on('close', (hadErr)=>{
|
||||
console.log(`TCP: connection closed (${this.remoteAddress}:${this.remotePort})`)
|
||||
if (!this.options.socket) {
|
||||
console.log(`TCP: attempting reconnection in 5s (${this.remoteAddress}:${this.remotePort})`)
|
||||
this.reconnect()
|
||||
} else {
|
||||
this.emit('die')
|
||||
}
|
||||
})
|
||||
|
||||
this.port.on('message', (msg, timetag)=>{
|
||||
this.emit('message', msg, timetag, {address: this.remoteAddress, port: this.remotePort})
|
||||
})
|
||||
|
||||
this.port.on('ready', ()=>{
|
||||
console.log(`TCP: connection established (${this.remoteAddress}:${this.remotePort})`)
|
||||
})
|
||||
|
||||
if (!this.options.socket) {
|
||||
|
||||
this.port.open()
|
||||
|
||||
} else {
|
||||
|
||||
this.port.emit('ready')
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
reconnect() {
|
||||
|
||||
setTimeout(()=>{
|
||||
this.open()
|
||||
}, 5000)
|
||||
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
|
||||
this.port.send(msg)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class OscTCPServer extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
this.clients = {}
|
||||
|
||||
}
|
||||
|
||||
bindSocket(socket) {
|
||||
|
||||
this.addClient(new OscTCPClient({
|
||||
metadata: true,
|
||||
socket: socket,
|
||||
address: socket.remoteAddress.indexOf('::ffff:') === 0 ? socket.remoteAddress.replace('::ffff:', '') : socket.remoteAddress,
|
||||
port: socket.remotePort
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
addClient(client) {
|
||||
|
||||
this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {}
|
||||
this.clients[client.remoteAddress][client.remotePort] = client
|
||||
|
||||
client.on('message', (msg, timetag, info)=>{
|
||||
this.emit('message', msg, timetag, info)
|
||||
})
|
||||
|
||||
client.on('die', (hadError)=>{
|
||||
if (this.clients[client.remoteAddress]) delete this.clients[client.remoteAddress][client.remotePort]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
open() {
|
||||
|
||||
this.server = net.createServer(this.bindSocket.bind(this))
|
||||
this.server.listen({port: tcpInPort})
|
||||
|
||||
for (var i in tcpTargets) {
|
||||
|
||||
this.addClient(new OscTCPClient({
|
||||
metadata: true,
|
||||
address: tcpTargets[i].split(':')[0],
|
||||
port: tcpTargets[i].split(':')[1]
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
zeroconf.publish({
|
||||
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
|
||||
protocol: 'tcp',
|
||||
type: 'osc',
|
||||
port: tcpInPort
|
||||
}).on('error', (e)=>{
|
||||
console.error(`Error: Zeroconf: ${e.message}`)
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var oscTCPServer = new OscTCPServer()
|
||||
|
||||
module.exports = oscTCPServer
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue