windows95/src/renderer/renderer.js

153 lines
3 KiB
JavaScript
Raw Normal View History

2018-08-24 00:33:29 +02:00
const $ = document.querySelector.bind(document)
const $$ = document.querySelectorAll.bind(document)
const BUTTONS = $('#buttons')
2018-08-23 07:03:28 +02:00
2018-08-23 07:25:17 +02:00
let cursorCaptured = false
2018-08-24 00:33:29 +02:00
let floppyFile = null
let bootFresh = false
2018-08-23 04:51:31 +02:00
const OPTIONS = {
win95: {
hda: {
url: './images/windows95.img',
async: true,
2018-08-23 07:25:17 +02:00
size: 242049024
2018-08-23 04:51:31 +02:00
}
}
}
2018-08-23 07:25:17 +02:00
async function main (id) {
2018-08-23 04:51:31 +02:00
const opts = Object.assign({
memory_size: 64 * 1024 * 1024,
screen_container: document.getElementById('emulator'),
bios: {
2018-08-23 07:25:17 +02:00
url: './bios/seabios.bin'
2018-08-23 04:51:31 +02:00
},
vga_bios: {
2018-08-23 07:25:17 +02:00
url: './bios/vgabios.bin'
2018-08-24 00:33:29 +02:00
},
fda: {
buffer: floppyFile || undefined
},
boot_order: 0x132
2018-08-23 04:51:31 +02:00
}, OPTIONS[id])
2018-08-23 07:03:28 +02:00
// New v86 instance
2018-08-23 04:51:31 +02:00
window.emulator = new V86Starter(opts)
// High DPI support
if (navigator.userAgent.includes('Windows')) {
var scale = window.devicePixelRatio;
window.emulator.screen_adapter.set_scale(scale,scale);
}
2018-08-23 07:03:28 +02:00
// Restore state. We can't do this right away.
setTimeout(async () => {
2018-08-24 00:33:29 +02:00
if (!bootFresh) {
await windows95.restoreState()
}
2018-08-23 07:03:28 +02:00
cursorCaptured = true
window.emulator.lock_mouse()
window.emulator.run()
}, 500)
2018-08-23 04:51:31 +02:00
}
2018-08-24 00:33:29 +02:00
function start (id) {
BUTTONS.remove()
document.body.className = ''
setupClickListener()
2018-08-24 00:33:29 +02:00
main(id)
}
2018-08-23 07:25:17 +02:00
function setupButtons () {
2018-08-24 00:33:29 +02:00
// Start
$$('.btn-start').forEach((btn) => {
btn.addEventListener('click', () => start(btn.id))
2018-08-23 04:51:31 +02:00
})
2018-08-23 07:03:28 +02:00
2018-08-24 00:33:29 +02:00
// Reset
$('#reset').addEventListener('click', () => {
2018-08-23 07:14:50 +02:00
if (window.emulator.stop) {
2018-08-23 07:03:28 +02:00
window.emulator.stop()
}
windows95.resetState()
2018-08-23 07:14:50 +02:00
if (window.emulator.run) {
2018-08-23 07:03:28 +02:00
window.emulator.run()
}
2018-08-23 07:25:17 +02:00
2018-08-24 00:33:29 +02:00
$('#reset').disabled = true
})
$('#discard-state').addEventListener('click', () => {
bootFresh = true
start('win95')
})
// Floppy
$('#floppy').addEventListener('click', () => {
$('#file-input').click()
})
// Floppy (Hidden Input)
$('#file-input').addEventListener('change', (event) => {
floppyFile = event.target.files && event.target.files.length > 0
? event.target.files[0]
: null
if (floppyFile) {
$('#floppy-path').innerHTML = `Inserted Floppy Disk: ${floppyFile.path}`
}
2018-08-23 07:03:28 +02:00
})
}
2018-08-23 07:25:17 +02:00
function setupEscListener () {
document.onkeydown = function (evt) {
evt = evt || window.event
if (evt.keyCode === 27) {
2018-08-23 07:03:28 +02:00
if (cursorCaptured) {
cursorCaptured = false
document.exitPointerLock()
} else {
cursorCaptured = true
window.emulator.lock_mouse()
}
}
}
}
2018-08-23 07:25:17 +02:00
function setupCloseListener () {
2018-08-23 07:03:28 +02:00
let isQuitting = false
const handleClose = async () => {
await windows95.saveState()
isQuitting = true
windows95.quit()
}
window.onbeforeunload = (event) => {
if (isQuitting) return
handleClose()
event.preventDefault()
event.returnValue = false
}
2018-08-23 04:51:31 +02:00
}
function setupClickListener () {
document.addEventListener('click', () => {
if (!cursorCaptured) {
cursorCaptured = true
window.emulator.lock_mouse()
}
})
}
2018-08-23 07:03:28 +02:00
setupEscListener()
setupCloseListener()
2018-08-23 04:51:31 +02:00
setupButtons()