-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
504 lines (433 loc) · 19 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
'use strict'
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true; // Disable security warning on the console
var { SerialPort } = require ( 'serialport' );
const fs = require ('fs');
const FREQ_VENDOR_BANDS = require ( 'require-all' )(__dirname +'/frequency_data/vendor_bands' );
const FREQ_VENDOR_PRESETS = require ( 'require-all' )(__dirname +'/frequency_data/presets');
const COUNTRIES = require ( './country_codes');
const {ipcMain} = require ( 'electron' );
const {app, BrowserWindow, Menu} = require ( 'electron' );
const electronLocalshortcut = require ( 'electron-localshortcut' );
const { productName, name, author, version } = require ( './package.json' );
const { dialog } = require ( 'electron' );
app.commandLine.appendSwitch('disable-gpu');
require('@electron/remote/main').initialize()
require ( './logger.js' );
// See the following discussions for next setting
// https://stackoverflow.com/questions/60106922/electron-non-context-aware-native-module-in-renderer
//https://github.com/electron/electron/issues/18397#issuecomment-583221969
app.allowRendererProcessReuse = false
const ConfigStore = require ( 'configstore' );
const configStore = new ConfigStore ( name );
let MENU_BAND = 0;
let MENU_CHANNELS = 1;
let MENU_COUNTRY = 2;
let MENU_PORT = 3;
let MENU_SCAN_DEVICE = 4;
let MENU_TOOLS = 5;
let MENU_HELP = 6;
if ( process.platform === 'darwin') {
MENU_BAND = 1;
MENU_CHANNELS = 2;
MENU_COUNTRY = 3;
MENU_PORT = 4;
MENU_SCAN_DEVICE = 5;
MENU_TOOLS = 6;
MENU_HELP = 7;
}
let mainWindow;
let helpWindow;
let aboutWindow;
let globalPorts = []
let country_code = configStore.get('country_code');
if ( !country_code ) {
log.info ( "No country setting saved! Using default: 'DE'");
country_code = "DE";
}
const gotLock = app.requestSingleInstanceLock()
if ( !gotLock ) {
app.quit()
} else { // This applies to the first instance of the applicatoin which has got the lock
app.on ( 'second-instance', (event, commandLine, workingDirectory ) => {
// Someone tried to run a second instance, we should focus our window.
if ( mainWindow ) {
if ( mainWindow.isMinimized() ) {
mainWindow.restore()
}
mainWindow.focus()
}
})
}
function createWindow () {
mainWindow = new BrowserWindow ({
width: 1200,
height: 700,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html');
let wc = mainWindow.webContents;
require("@electron/remote/main").enable(wc)
//wc.openDevTools();
electronLocalshortcut.register ( mainWindow, 'F12', () => {
mainWindow.toggleDevTools();
});
electronLocalshortcut.register ( mainWindow, 'CommandOrControl+R', () => {
app.relaunch ()
app.exit (0)
});
mainWindow.setTitle ( productName + " V" + version );
var menuJSON = [];
function addMenuEntryOrSubmenu ( menu_label, menu_data, menu_location ) {
if ( !Array.isArray (menu_data) ) {
menu_location.push ({
"label": menu_label,
click () { wc.send ( "CHANGE_BAND", {
start_freq : menu_data.start_freq,
stop_freq : menu_data.stop_freq,
details : menu_data.details,
band : menu_data.band
}); }
});
return;
}
let len = menu_location.push ({ "label": menu_label, "submenu": [] });
menu_data.forEach ( (submenu_entry) => {
if ( submenu_entry.hasOwnProperty ('submenu') ) {
addMenuEntryOrSubmenu ( submenu_entry.label, submenu_entry.submenu, menu_location[len-1].submenu );
} else if ( submenu_entry.hasOwnProperty ('type') && submenu_entry.type === 'separator' ) {
menu_location[len-1].submenu.push ({type: "separator"});
} else {
menu_location[len-1].submenu.push ({
"label": submenu_entry.label,
click () { wc.send ( "CHANGE_BAND", {
"start_freq" : submenu_entry.start_freq,
"stop_freq" : submenu_entry.stop_freq,
"details" : submenu_entry.details,
"band" : submenu_entry.band
}); }
});
}
});
}
if ( process.platform === 'darwin' )
menuJSON.push ({ label: 'App Menu', submenu: [{ role: 'quit'}] })
// Add bands
menuJSON.push ({ label: 'Band', submenu: [] });
menuJSON[MENU_BAND].submenu.push ({ label : 'Manual input F', click () { wc.send ( 'SHOW_MANUAL_BAND_SETTINGS') }});
menuJSON[MENU_BAND].submenu.push ({ label: 'Save to hotkey', submenu: [
{ label: "1", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '1'}) }},
{ label: "2", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '2'}) }},
{ label: "3", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '3'}) }},
{ label: "4", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '4'}) }},
{ label: "5", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '5'}) }},
{ label: "6", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '6'}) }},
{ label: "7", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '7'}) }},
{ label: "8", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '8'}) }},
{ label: "9", click () { wc.send ( 'SAVE_TO_HOTKEY', { hotkey : '9'}) }}
]})
menuJSON[MENU_BAND].submenu.push ({ type:'separator' });
Object.entries ( FREQ_VENDOR_BANDS ).forEach ( vendorBandData => {
let key = vendorBandData[0];
let value = vendorBandData[1];
if ( Array.isArray ( value ) ) {
menuJSON[MENU_BAND].submenu.push ({ type:'separator' });
value.forEach ( (val) => {
addMenuEntryOrSubmenu ( val.label, val, menuJSON[MENU_BAND].submenu );
});
menuJSON[MENU_BAND].submenu.push ({ type:'separator' });
} else if ( value.hasOwnProperty ('submenu') )
addMenuEntryOrSubmenu ( value.label, value.submenu, menuJSON[MENU_BAND].submenu );
else
addMenuEntryOrSubmenu ( value.label, value, menuJSON[MENU_BAND].submenu );
});
if ( country_code && fs.existsSync ( __dirname + '/frequency_data/country_bands/' + country_code ) ) {
const COUNTRY_BANDS = require ( 'require-all' )(__dirname +'/frequency_data/country_bands/' + country_code );
menuJSON[MENU_BAND].submenu.push ({ type:'separator' });
Object.entries ( COUNTRY_BANDS ).forEach ( countryBandData => {
let key = countryBandData[0];
let value = countryBandData[1];
addMenuEntryOrSubmenu ( value.label, value.hasOwnProperty('submenu')?value.submenu:value, menuJSON[MENU_BAND].submenu );
});
}
// Add channel presets
menuJSON.push ({ label: 'Chan. Presets', submenu: [] });
Object.entries ( FREQ_VENDOR_PRESETS ).forEach ( vendorPreset => {
let key = vendorPreset[0].split("_");
let preset = vendorPreset[1];
let vendor_idx = undefined;
let band_idx = undefined;
let series_idx = undefined;
vendor_idx = menuJSON[MENU_CHANNELS].submenu.findIndex ( ( elem ) => { return elem.label === key[0]; });
if ( vendor_idx === -1 ) // Does this vendor already exists as a submenu?
vendor_idx = menuJSON[MENU_CHANNELS].submenu.push ({ label: key[0], submenu: [] }) - 1;
band_idx = menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu.findIndex ( ( elem ) => { return elem.label === key[1] + " - Band"; });
if ( band_idx === -1 ) // Does this band already exist in this vendor submenu?
band_idx = menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu.push ({ label: key[1] + " - Band", submenu: [] }) - 1;
if ( key[2] !== "NONE" ) {
series_idx = menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu[band_idx].submenu.findIndex ( ( elem ) => { return elem.label === key[2]; });
if ( series_idx ) // Does this series already exist in this band submenu?
series_idx = menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu[band_idx].submenu.push ({ label: key[2], submenu: [] }) - 1;
}
preset.forEach ( (bank, i) => {
if ( key[2] !== "NONE" ) {
menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu[band_idx].submenu[series_idx].submenu.push ({
label: "Bank " + (i+1),
click () { wc.send ( "SET_CHAN_PRESET", { preset: vendorPreset[0] + "_" + (i+1) }); }
});
} else {
menuJSON[MENU_CHANNELS].submenu[vendor_idx].submenu[band_idx].submenu.push ({
label: "Bank " + (i+1),
click () { wc.send ( "SET_CHAN_PRESET", { preset: vendorPreset[0] + "_" + (i+1) }); }
});
}
});
});
// Add countries
menuJSON.push ({ label: 'Country', submenu: [] });
COUNTRIES.forEach ( c => {
if ( fs.existsSync ( __dirname + '/frequency_data/forbidden/FORBIDDEN_' + c.code + '.json' ) ) {
menuJSON[MENU_COUNTRY].submenu.push (
{
'label' : c.label,
'code' : c.code,
'type' : 'radio' ,
'checked': country_code===c.code?true:false,
click () { wc.send ( 'SET_COUNTRY', { country_code : c.code, country_label : c.label } ); }
}
);
}
});
// Add ports (will be filled later via renderer event)
var portMenuJSON = { label: 'Port', submenu: [] };
menuJSON.push ( portMenuJSON );
// Add scan devices
var scanDeviceMenuJSON = { label: 'Device', submenu: [{
label : 'RF Explorer',
code : 'RF_EXPLORER',
type : 'radio',
click () {
menuJSON[MENU_SCAN_DEVICE].submenu[0].checked = true
menuJSON[MENU_SCAN_DEVICE].submenu[1].checked = false
wc.send ( 'SET_SCAN_DEVICE', { scanDevice : 'RF_EXPLORER' } )
}
}, {
label : 'Tiny SA',
code : 'TINY_SA',
type : 'radio',
click () {
menuJSON[MENU_SCAN_DEVICE].submenu[0].checked = false
menuJSON[MENU_SCAN_DEVICE].submenu[1].checked = true
wc.send ( 'SET_SCAN_DEVICE', { scanDevice : 'TINY_SA' } )
}
}, {
label : 'Settings',
code : 'DEVICE_SETTINGS',
click () { wc.send ( 'DEVICE_SETTINGS', {} ); }
}]};
menuJSON.push ( scanDeviceMenuJSON );
// Add tools menu
var toolsMenuJSON = { label: 'Tools', submenu: [
{ label: 'Export', submenu: [
{
label: "Shure WW6 and IAS (CSV Format)", click () {
dialog.showSaveDialog ({
title: "Export for Shure WW6 and IAS (CSV Format)",
filters: [ {name: "CSV", extensions: ["csv"]} ]
}).then ( (res) => {
wc.send ( 'EXPORT_WW6_IAS_CSV', { filename : res.filePath })
})
}
}, {
label: "Sennheiser Wireless System Manager (CSV Format)", click () {
dialog.showSaveDialog ({
title: "Export for Sennheiser Wireless System Manager (CSV Format)",
filters: [ {name: "CSV", extensions: ["csv"]} ]
}).then ( (res) => {
wc.send ( 'EXPORT_WSM_CSV', { filename : res.filePath })
})
}
}
]},
{ label : 'MX Linux Workaround',
type : 'checkbox',
click (ev) {
let elem = menuJSON[MENU_TOOLS].submenu.find((elem)=> elem.label === 'MX Linux Workaround')
elem.checked = ev.checked;
wc.send ( 'MX_LINUX_WORKAROUND', { enabled : ev.checked ? true : false })
}
},
{ label: 'Reset Peak R', click () {
wc.send ('RESET_PEAK', {});
}},
{ label: 'Reset Settings', click () {
wc.send ('RESET_SETTINGS', {});
}},
{ label: 'Dark mode',
type: 'checkbox',
click (ev) {
let elem = menuJSON[MENU_TOOLS].submenu.find((elem)=> elem.label === 'Dark mode')
elem.checked = ev.checked;
wc.send ( 'DARK_MODE', { enabled : ev.checked ? true : false })
}
}
]};
menuJSON.push ( toolsMenuJSON );
// Add help menu
var helpMenuJSON = { label: 'Help', submenu: [
{ label: "Documentation", click () { openHelpWindow() ; } },
{ label: "Download logs", click () {
dialog.showSaveDialog ({
title: "Save logs",
defaultPath: 'logs.zip'
}).then ( (res) => {
zipLogs(res.filePath)
})
} },
{ label: "Developer tools", click () { wc.openDevTools(); } },
{ label: "About" , click () { openAboutWindow(); } }
]};
menuJSON.push ( helpMenuJSON );
/**************************/
/* Country */
/**************************/
ipcMain.on ( "SET_COUNTRY", (event, data) => {
menuJSON[MENU_COUNTRY].submenu.forEach ( function ( elem ) {
if ( elem.code === data.country_code ) {
elem.checked = true;
// Need to rebuild menu to reflect attribute (in this case the 'checked' attribute)
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) );
}
})
})
/**************************/
/* Port */
/**************************/
ipcMain.on ( "SET_PORT", (event, data) => {
SerialPort.list().then ( (ports, err) => {
if ( err ) {
log.info ( err );
return;
}
globalPorts = ports
let portPathArr = [];
menuJSON[MENU_PORT].submenu = []
globalPorts.forEach ( ( port ) => {
portPathArr.push ( port.path );
});
menuJSON[MENU_PORT].submenu[0] = {
label: 'Auto',
type: 'radio',
checked: (data.portPath === undefined || data.portPath === 'AUTO') ? true : false,
click () { wc.send ( 'SET_PORT', portPathArr ); }
}
portPathArr.forEach ( port => {
menuJSON[MENU_PORT].submenu.push (
{
'label' : port,
'type' : 'radio',
'checked': data.portPath === port ? true : false,
click () { wc.send ( 'SET_PORT', { port : port } ); }
}
);
});
// Need to rebuild menu to reflect attribute (in this case the 'checked' attribute)
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) );
})
})
/**************************/
/* Device */
/**************************/
ipcMain.on ( "SET_SCAN_DEVICE", (event, data) => {
switch ( data.scanDevice ) {
case 'RF_EXPLORER':
menuJSON[MENU_SCAN_DEVICE].submenu[0].checked = true
menuJSON[MENU_SCAN_DEVICE].submenu[1].checked = false
break
case 'TINY_SA':
menuJSON[MENU_SCAN_DEVICE].submenu[0].checked = false
menuJSON[MENU_SCAN_DEVICE].submenu[1].checked = true
break
}
// Need to rebuild menu to reflect attribute (in this case the 'checked' attribute)
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) )
})
/**************************/
/* Tools */
/**************************/
ipcMain.on ( "MX_LINUX_WORKAROUND", (event, data) => {
let elem = menuJSON[MENU_TOOLS].submenu.find((elem)=> elem.label === 'MX Linux Workaround')
elem.checked = data.checked;
// Need to rebuild menu to reflect attribute (in this case the 'checked' attribute)
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) )
});
ipcMain.on ( "DARK_MODE", (event, data) => {
let elem = menuJSON[MENU_TOOLS].submenu.find((elem)=> elem.label === 'Dark mode')
elem.checked = data.checked;
// Need to rebuild menu to reflect attribute (in this case the 'checked' attribute)
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) )
})
ipcMain.on ( "SET_MAIN_WINDOW_TITLE", (event, data) => {
mainWindow.setTitle ( productName + " V" + version + " ( " + data + " )")
})
/************/
/* Init */
/************/
// Add serial ports to the menu
SerialPort.list().then ( (ports, err) => {
if ( err ) {
log.info ( err );
return;
}
globalPorts = ports
})
function openHelpWindow () {
helpWindow = new BrowserWindow({width: 800, height: 600});
helpWindow.setMenu ( null );
helpWindow.loadFile('help.html');
electronLocalshortcut.register ( helpWindow, 'CommandOrControl+R', () => {
helpWindow.reload();
});
electronLocalshortcut.register ( helpWindow, 'Alt+CommandOrControl+Shift+I', () => {
helpWindow.toggleDevTools();
});
electronLocalshortcut.register ( helpWindow, 'Esc', () => {
helpWindow.close();
});
helpWindow.setTitle ( "Documentation" );
}
function openAboutWindow () {
aboutWindow = new BrowserWindow({width: 500, height: 170, resizable: false});
aboutWindow.setMenu ( null );
aboutWindow.loadFile('about.html');
electronLocalshortcut.register ( aboutWindow, 'CommandOrControl+R', () => {
aboutWindow.reload();
});
electronLocalshortcut.register ( aboutWindow, 'Alt+CommandOrControl+Shift+I', () => {
aboutWindow.toggleDevTools();
});
electronLocalshortcut.register ( aboutWindow, 'Esc', () => {
aboutWindow.close();
});
aboutWindow.setTitle ( "About" );
}
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.on ( 'ready', createWindow );
app.on ( 'window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if ( process.platform !== 'darwin' )
app.quit();
});
app.on ( 'activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if ( mainWindow === null )
createWindow();
});