Use two package.json structure
This commit is contained in:
parent
4dd837b565
commit
8da021b672
108 changed files with 98 additions and 89 deletions
140
app/main/callbacks.js
Normal file
140
app/main/callbacks.js
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
var vm = require('vm')
|
||||
|
||||
module.exports = function(settings,fs,ipc,osc,dialog) {
|
||||
|
||||
return {
|
||||
|
||||
ready: function(data,clientId) {
|
||||
if (settings.read('theme')) ipc.send('applyStyle',settings.read('theme'),clientId)
|
||||
if (settings.read('sessionFile')) this.sessionOpen({path:settings.read('sessionFile')},clientId)
|
||||
var recentSessions = settings.read('recentSessions')
|
||||
|
||||
if (settings.read('examples')) {
|
||||
recentSessions = fs.readdirSync('examples')
|
||||
recentSessions = recentSessions.map(function(file){return 'examples/' + file})
|
||||
}
|
||||
|
||||
ipc.send('sessionList',recentSessions,clientId)
|
||||
},
|
||||
|
||||
sessionBrowse: function(data) {
|
||||
var sessionlist = settings.read('recentSessions'),
|
||||
self = this
|
||||
dialog.showOpenDialog(window,{title:'Load session file',defaultPath:settings.read('sessionPath'),filters: [ { name: 'OSC Session file', extensions: ['js'] }]},function(file){
|
||||
if (file==undefined) {return}
|
||||
settings.write('sessionPath',file[0].replace(file[0].split('/').pop(),''))
|
||||
self.sessionOpen({path:file[0]})
|
||||
})
|
||||
},
|
||||
|
||||
sessionSave: function(data) {
|
||||
var self = this
|
||||
dialog.showSaveDialog(window,{title:'Save current session',defaultPath:settings.read('sessionPath'),filters: [ { name: 'OSC Session file', extensions: ['js'] }]},function(file){
|
||||
|
||||
if (file==undefined) {return}
|
||||
settings.write('sessionPath',file.replace(file.split('/').pop(),''))
|
||||
|
||||
if (file.indexOf('.js')==-1){file+='.js'}
|
||||
fs.writeFile(file,data, function (err, data) {
|
||||
if (err) throw err
|
||||
self.sessionAddToHistory(file)
|
||||
console.log('The session was saved in '+file)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
sessionAddToHistory: function(data) {
|
||||
var sessionlist = settings.read('recentSessions')
|
||||
// add session to history
|
||||
sessionlist.unshift(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')
|
||||
sessionlist.splice(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 = vm.runInNewContext(file)
|
||||
} catch(err) {
|
||||
error = err
|
||||
}
|
||||
|
||||
if (!session) error= 'No session object returned'
|
||||
|
||||
if (!error) {
|
||||
if (data.path) this.sessionAddToHistory(data.path)
|
||||
ipc.send('sessionOpen',JSON.stringify(session),clientId)
|
||||
} else {
|
||||
ipc.send('error',{title:'Error: invalid session file',text:'<p>'+error+'</p>'})
|
||||
}
|
||||
},
|
||||
|
||||
sendOsc: function(data) {
|
||||
|
||||
var targets = []
|
||||
|
||||
if (settings.read('syncTargets') && data.sync!==false) Array.prototype.push.apply(targets, settings.read('syncTargets'))
|
||||
if (data.target) Array.prototype.push.apply(targets, data.target)
|
||||
|
||||
|
||||
for (i in targets) {
|
||||
|
||||
var host = targets[i].split(':')[0],
|
||||
port = targets[i].split(':')[1]
|
||||
|
||||
if (port) osc.send(host,port,data.path,data.args,data.precision)
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
stateLoad: function(data,clientId) {
|
||||
dialog.showOpenDialog(window,{title:'Load 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[0])
|
||||
|
||||
fs.readFile(file[0],'utf-8', function read(err, data) {
|
||||
if (err) throw err
|
||||
ipc.send('stateLoad',data,clientId)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
fullscreen: function(data) {
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
},
|
||||
|
||||
log: function(data) {
|
||||
console.log(data)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
41
app/main/main.js
Normal file
41
app/main/main.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
var fs = require('fs'),
|
||||
settings = require('./settings')(fs)
|
||||
|
||||
if (!settings.read('noGui')) {
|
||||
|
||||
var electron = require('electron'),
|
||||
app = electron.app,
|
||||
browserWindow = electron.BrowserWindow,
|
||||
dialog = electron.dialog,
|
||||
ipc = electron.ipcMain
|
||||
|
||||
dialog.showErrorBox = function(title,err) {
|
||||
console.log(title + ': ' + err)
|
||||
}
|
||||
|
||||
app.commandLine.appendSwitch('--enable-touch-events')
|
||||
app.on('window-all-closed', function() {
|
||||
if (process.platform != 'darwin') {
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
|
||||
app.on('ready',function(){
|
||||
var server = require('./server-electron')(settings,app,ipc,browserWindow),
|
||||
osc = require('./osc')(ipc,settings),
|
||||
callbacks = require('./callbacks')(settings,fs,ipc,osc,dialog)
|
||||
|
||||
|
||||
server.bindCallbacks(callbacks)
|
||||
})
|
||||
|
||||
} else {
|
||||
|
||||
var server = require('./server-express')(settings),
|
||||
ipc = server.ipc,
|
||||
osc = require('./osc')(ipc,settings),
|
||||
callbacks = require('./callbacks')(settings,fs,ipc,osc,dialog)
|
||||
|
||||
server.bindCallbacks(callbacks)
|
||||
|
||||
}
|
||||
64
app/main/osc.js
Normal file
64
app/main/osc.js
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
var osc = require('node-osc')
|
||||
|
||||
module.exports = function(ipc, settings) {
|
||||
|
||||
var inPort = settings.read('oscInPort'),
|
||||
debug = settings.read('debug')
|
||||
|
||||
if (inPort) {
|
||||
var oscServer = new osc.Server(inPort, '127.0.0.1')
|
||||
|
||||
oscServer.on('message', function (msg, rinfo) {
|
||||
var data = {path:msg.shift(),args:msg}
|
||||
if (data.args.length==1) data.args = data.args[0]
|
||||
ipc.send('receiveOsc',data)
|
||||
if (debug) console.log('OSC received: ',data, 'From : ' + rinfo.address + ':' + rinfo.port)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
send: function(host,port,path,args,precision) {
|
||||
|
||||
var client = new osc.Client(host, port)
|
||||
|
||||
client._sock.bind({exclusive:false,reuseAddr:true,port:inPort},function(){
|
||||
|
||||
|
||||
var message = new osc.Message(path)
|
||||
|
||||
var numberType = precision==0?'integer':'float'
|
||||
|
||||
if (typeof args=='object' && args!==null) {
|
||||
for (i in args) {
|
||||
var arg = args[i]
|
||||
|
||||
if (typeof arg == 'number' && arg!==null && typeof arg != 'object') {
|
||||
message.append({type:numberType,value:arg})
|
||||
} else if (arg!==null && typeof arg != 'object'){
|
||||
message.append({type:'string',value:String(arg)})
|
||||
} else if (arg!==null && typeof arg == 'object' && arg.type && arg.value!=undefined) {
|
||||
message.append({type:arg.type,value:arg.value})
|
||||
}
|
||||
}
|
||||
} else if (args!==null){
|
||||
if (typeof args == 'number') {
|
||||
message.append({type:numberType,value:args})
|
||||
} else if (typeof args == 'object' && arg.type && arg.value!=undefined) {
|
||||
message.append({type:args.type,value:args.value})
|
||||
} else {
|
||||
message.append({type:'string',value:String(args)})
|
||||
}
|
||||
}
|
||||
|
||||
client.send(message, function () {
|
||||
client.kill()
|
||||
})
|
||||
|
||||
if (debug) console.log('OSC sent: ',{path:path,args:message.args}, 'To : ' + host + ':' + port)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
83
app/main/server-electron.js
Normal file
83
app/main/server-electron.js
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
var path = require('path')
|
||||
module.exports = function(settings,app,ipc,browserWindow) {
|
||||
|
||||
window = new browserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
icon: path.resolve(__dirname + '/../resources/images/logo.png'),
|
||||
title:settings.read('appName'),
|
||||
backgroundColor:'#1a1d22',
|
||||
autoHideMenuBar:true,
|
||||
// show:false
|
||||
})
|
||||
|
||||
window.loadURL('file://' + __dirname + '/../browser/index.html')
|
||||
|
||||
window.on('closed', function() {
|
||||
window = null
|
||||
})
|
||||
|
||||
window.setMenuBarVisibility(false)
|
||||
|
||||
var Menu = require('electron').Menu
|
||||
|
||||
Menu.setApplicationMenu(Menu.buildFromTemplate(
|
||||
[
|
||||
{
|
||||
label: 'View',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Restart',
|
||||
accelerator: 'CmdOrCtrl+R',
|
||||
click: function(){
|
||||
window.reload()
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Toggle DevTools',
|
||||
accelerator: 'F12',
|
||||
click: function() { window.toggleDevTools(); }
|
||||
},
|
||||
{
|
||||
label: 'Toggle Fullscreen',
|
||||
accelerator: 'F11',
|
||||
click: function() { window.setFullScreen(!window.isFullScreen()); }
|
||||
}
|
||||
]
|
||||
},
|
||||
]
|
||||
))
|
||||
|
||||
// mainProcess > renderProcess ipc shorthand
|
||||
|
||||
ipc.send = function(name,data) {
|
||||
window.webContents.send(name,data)
|
||||
}
|
||||
|
||||
|
||||
// ipc bindings
|
||||
|
||||
var bindCallbacks = function(callbacks) {
|
||||
|
||||
var bindCallback = function(i) {
|
||||
ipc.on(i,function(event,data){
|
||||
callbacks[i](data)
|
||||
})
|
||||
}
|
||||
|
||||
for (i in callbacks) {
|
||||
bindCallback(i)
|
||||
}
|
||||
|
||||
ipc.on('ready',function(){
|
||||
window.show()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
ipc:ipc,
|
||||
bindCallbacks:bindCallbacks
|
||||
}
|
||||
|
||||
}
|
||||
58
app/main/server-express.js
Normal file
58
app/main/server-express.js
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
module.exports = function(settings) {
|
||||
|
||||
// Headless (noGui) mode
|
||||
|
||||
var express = require('express')(),
|
||||
path = require('path'),
|
||||
http = require('http'),
|
||||
server = http.createServer(express),
|
||||
io = require('socket.io')(),
|
||||
ioWildcard = require('socketio-wildcard')(),
|
||||
ipc = {}
|
||||
|
||||
express.get('/', function(req, res){
|
||||
res.sendFile(path.resolve(__dirname + '/../browser/index-headless.html'))
|
||||
})
|
||||
express.get('*', function(req, res){
|
||||
res.sendFile(path.resolve(__dirname + '/../browser' + req.path))
|
||||
})
|
||||
|
||||
|
||||
io.use(ioWildcard)
|
||||
|
||||
|
||||
io.listen(server);
|
||||
|
||||
server.listen(settings.read('noGui'))
|
||||
|
||||
ipc.send = function(name,data,clientId) {
|
||||
if (clientId) {
|
||||
io.to(clientId).emit(name,data)
|
||||
} else {
|
||||
io.emit(name,data)
|
||||
}
|
||||
}
|
||||
|
||||
var bindCallbacks = function(callbacks) {
|
||||
io.on('connection', function(socket) {
|
||||
socket.on('*', function(e){
|
||||
var name = e.data[0],
|
||||
data = e.data[1]
|
||||
if (callbacks[name]) callbacks[name](data,socket.id)
|
||||
if (name=='sendOsc' && data.sync!==false) {
|
||||
// synchronize all other connected clients
|
||||
socket.broadcast.emit('receiveOsc',data)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
console.log('Headless mode: app available at http://127.0.0.1:'+settings.read('noGui'))
|
||||
|
||||
return {
|
||||
ipc:ipc,
|
||||
bindCallbacks:bindCallbacks
|
||||
}
|
||||
|
||||
}
|
||||
66
app/main/settings.js
Normal file
66
app/main/settings.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// This prevents argv parsing to be breaked when the app is packaged (executed without 'electron' prefix)
|
||||
if (process.argv[1]&&process.argv[1].indexOf('-')==0) process.argv.unshift('')
|
||||
|
||||
var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
|
||||
configFile = require('path').join(baseDir, '.open-stage-control')
|
||||
|
||||
var argv = require('yargs')
|
||||
.help('help').usage(`\nUsage:\n $0 [options]`).alias('h', 'help')
|
||||
.options({
|
||||
's':{alias:'sync',type:'array',describe:'synchronized hosts (ip:port pairs)'},
|
||||
'l':{alias:'load',type:'string',describe:'session file to load'},
|
||||
'p':{alias:'port',describe:'osc input port (for synchronization)'},
|
||||
'd':{alias:'debug',describe:'log received osc messages in the console'},
|
||||
'n':{alias:'nogui',describe:'disable default gui and makes the app availabe through http on specified port'},
|
||||
't':{alias:'theme',type:'array',describe:'theme name or path (mutliple values allowed)'},
|
||||
'examples':{describe:'list examples instead of recent sessions'}
|
||||
})
|
||||
.check(function(a,x){if(a.p==undefined || !isNaN(a.p)&&a.p>1023&&parseInt(a.p)===a.p){return true}else{throw 'Error: Port must be an integer >= 1024'}})
|
||||
.check(function(a,x){if(a.n==undefined || !isNaN(a.n)&&a.n>1023&&parseInt(a.n)===a.n){return true}else{throw 'Error: Port must be an integer >= 1024'}})
|
||||
.check(function(a,x){if(a.s==undefined || a.s.join(' ').match('^([^:\s]*:[0-9]{4,5}[\s]*)*$')!=null){return true}else{throw 'Error: Sync hosts must be ip:port pairs & port must be >= 1024'}})
|
||||
.strict()
|
||||
.argv
|
||||
|
||||
module.exports = function(fs) {
|
||||
var config = function(){try {return JSON.parse(fs.readFileSync(configFile,'utf-8'))} catch(err) {return {}}}(),
|
||||
defaultConfig = {
|
||||
presetPath : process.cwd(),
|
||||
sessionPath: process.cwd(),
|
||||
recentSessions: [],
|
||||
|
||||
appName: 'Open Stage Control',
|
||||
syncTargets: argv.s || false,
|
||||
oscInPort: argv.p || 0,
|
||||
debug: argv.d || false,
|
||||
sessionFile: argv.l || false,
|
||||
noGui: argv.n || false,
|
||||
examples: argv.examples,
|
||||
theme: function(){
|
||||
if (!argv.t) return
|
||||
var style = []
|
||||
for (i in argv.t) {
|
||||
try {style.push(fs.readFileSync(__dirname + '/themes/' + argv.t[i] + '.css','utf-8'))}
|
||||
catch(err) {
|
||||
try {style.push(fs.readFileSync(argv.t[i],'utf-8'))}
|
||||
catch(err) {
|
||||
console.log('Theme "' + argv.t[i] + '" not found.')
|
||||
}
|
||||
}
|
||||
}
|
||||
return style
|
||||
}()
|
||||
}
|
||||
return {
|
||||
|
||||
read:function(key){
|
||||
var x = config[key] || defaultConfig[key]
|
||||
return x
|
||||
},
|
||||
write:function(key,value) {
|
||||
config[key] = value
|
||||
fs.writeFile(configFile,JSON.stringify(config,null,4), function (err, data) {
|
||||
if (err) throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
10
app/main/themes/light.css
Normal file
10
app/main/themes/light.css
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
:root {
|
||||
--color-text: rgba(0,0,0,0.7);
|
||||
--color-text-fade: rgba(0,0,0,0.5);
|
||||
--color-bg: #bbb;
|
||||
--color-fg: #ddd;
|
||||
--color-raised: #eee;
|
||||
--color-faded: #ccc;
|
||||
--color-track: #aaa;
|
||||
--color-accent: #307aaa;
|
||||
}
|
||||
3
app/main/themes/noinput.css
Normal file
3
app/main/themes/noinput.css
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
.widget .input {
|
||||
display: none;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue