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
149const fs = require('fs')
const path = require('path')
const program = require('commander')
const {app, BrowserWindow, ipcMain} = require('electron')
const context_menu = require('electron-context-menu')
const { version } = require('./package.json')
function fail(message) {
console.error(message)
process.exit(1)
}
function read_file(path) {
let file
try {
// stdin is file 0
if (path === '-') {
path = 0
}
file = fs.readFileSync(path).toString()
} catch (error) {
fail(`Unable to read file\n${error.message}`)
}
return file
}
function parse_freq(value) {
let freq = parseInt(value)
if (freq < 100) {
fail("Freq must be >=100Hz")
}
return freq
}
program
.version(`v${version}`)
.arguments('<input_file>')
.option('--freq <number>', 'emulator frequency, integer kHz', parse_freq)
.option('--no-autostart', 'don\'t automatically start emulator')
.option('--fullscreen', 'make emulator display fullscreen')
.option('--dev-tools', 'open Chrome DevTools')
.parse(process.argv)
let queued_message
let input_path = program.args[0]
if (input_path !== undefined) {
queued_message = {
file: read_file(input_path),
freq: program.freq,
autostart: program.autostart
}
}
let mainWindow
context_menu({
showInspectElement: true,
showSearchWithGoogle: false,
showCopyImage: false,
showLookUpSelection: false
})
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 1550,
height: 656,
minWidth: 350,
minHeight: 200,
frame: false,
show: false,
icon: path.join(__dirname, 'assets/icon.png'),
webPreferences: {
nativeWindowOpen: true,
nodeIntegration: true,
nodeIntegrationInWorker: false
}
})
mainWindow
.loadFile('index.html')
.then(() => {
if (queued_message) {
mainWindow.webContents.send('run-file', queued_message)
}
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.on('closed', () => {
mainWindow = null
})
// centre any popup windows
mainWindow.webContents.on('new-window', centre_window)
if (program.devTools) {
mainWindow.webContents.openDevTools()
}
})
function centre_window(event, url, frameName, disposition, options) {
event.preventDefault()
options.x = undefined
options.y = undefined
options.useContentSize = true
event.newGuest = new BrowserWindow(options)
event.newGuest.webContents.on('new-window', centre_window)
}
ipcMain.on('window-management', (event, arg) => {
switch (arg) {
case 'maximise':
if (mainWindow.isMaximized()) {
mainWindow.restore()
} else {
mainWindow.maximize()
}
break
case 'minimise':
mainWindow.minimize()
break
case 'emulator_ready':
if (program.fullscreen) {
// must be called from main thread (in renderer process, only a user initiated button press can go fullscreen)
mainWindow.webContents.executeJavaScript("window.frames[0].go_fullscreen()", true)
}
break
default:
console.console.warn(`Unknown command ${arg}`);
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})