forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
473 lines (445 loc) · 22.1 KB
/
monitor.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
/**
* @fileoverview Implements the Monitor device
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
* @license MIT
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*/
"use strict";
/**
* @typedef {Config} MonitorConfig
* @property {number} monitorWidth
* @property {number} monitorHeight
*/
/**
* @class {Monitor}
* @unrestricted
* @property {MonitorConfig} config
*/
class Monitor extends Device {
/**
* Monitor(idMachine, idDevice, config)
*
* The Monitor component manages the container representing the machine's display device. The most
* important config properties include:
*
* monitorWidth: width of the monitor canvas, in pixels
* monitorHeight: height of the monitor canvas, in pixels
* monitorColor: background color of the monitor canvas (default is black)
* monitorRotate: the amount of counter-clockwise monitor rotation required (eg, -90 or 270)
* aspectRatio (eg, 1.33)
*
* NOTE: I originally wanted to call this the Screen device, but alas, the browser world has co-opted that
* name, so I had to settle for Monitor instead (I had also considered Display, but that seemed too generic).
*
* Monitor is probably a better choice anyway, because that allows us to clearly differentiate between the
* "host display" (which involves the browser's page, document, window, or screen, depending on the context)
* and the "guest display", which I now try to consistently refer to as the Monitor.
*
* There are still terms of art that can muddy the waters; for example, many video devices support the concept
* of "off-screen memory", and sure, I could call that "off-monitor memory", but let's not get carried away.
*
* @this {Monitor}
* @param {string} idMachine
* @param {string} idDevice
* @param {ROMConfig} [config]
*/
constructor(idMachine, idDevice, config)
{
super(idMachine, idDevice, config);
let sProp, sEvent;
let monitor = this;
this.touchType = this.config['touchType'];
this.diagnostics = this.config['diagnostics'];
this.cxMonitor = this.config['monitorWidth'] || 640;
this.cyMonitor = this.config['monitorHeight'] || 480;
this.monitor = this.bindings[Monitor.BINDING.MONITOR];
if (this.monitor) {
/*
* Making sure the monitor had a "tabindex" attribute seemed like a nice way of ensuring we
* had a single focusable surface that we could pass to our Input device, but that would be too
* simple. Safari once again bites us in the butt, just like it did when we tried to add the
* "contenteditable" attribute to the canvas: painting slows to a crawl.
*
* this.monitor.setAttribute("tabindex", "0");
*/
} else {
throw new Error("unable to find binding: " + Monitor.BINDING.MONITOR);
}
this.container = this.findBinding(Monitor.BINDING.CONTAINER) || this.monitor;
/*
* Create the Monitor canvas if we weren't given a predefined canvas; we'll assume that an existing
* canvas is already contained within the monitor.
*/
let canvas = this.bindings[Monitor.BINDING.SURFACE];
if (!canvas) {
canvas = document.createElement("canvas");
let id = this.getBindingID(Monitor.BINDING.SURFACE);
if (id) {
this.bindings[id] = canvas;
canvas.setAttribute("id", id);
}
canvas.setAttribute("class", "pcjsSurface");
canvas.setAttribute("width", this.config['monitorWidth']);
canvas.setAttribute("height", this.config['monitorHeight']);
canvas.style.backgroundColor = this.config['monitorColor'] || "black";
this.monitor.appendChild(canvas);
}
this.canvasMonitor = canvas;
/*
* The "contenteditable" attribute on a canvas is a simple way of creating a display surface that can
* also receive focus and generate input events. Unfortunately, in Safari, that attribute NOTICEABLY
* slows down canvas operations whenever it has focus. All you have to do is click away from the canvas,
* and drawing speeds up, then click back on the canvas, and drawing slows down. So we now rely on a
* "transparent textarea" solution (see below).
*
* canvas.setAttribute("contenteditable", "true");
*/
let context = canvas.getContext("2d");
this.contextMonitor = context;
/*
* HACK: A canvas style of "auto" provides for excellent responsive canvas scaling in EVERY browser
* except IE9/IE10, so I recalculate the appropriate CSS height every time the parent div is resized;
* IE11 works without this hack, so we take advantage of the fact that IE11 doesn't identify as "MSIE".
*
* The other reason it's good to keep this particular hack limited to IE9/IE10 is that most other
* browsers don't actually support an 'onresize' handler on anything but the window object.
*/
if (this.isUserAgent("MSIE")) {
this.monitor.onresize = function(parentElement, childElement, cx, cy) {
return function onResizeScreen() {
childElement.style.height = (((parentElement.clientWidth * cy) / cx) | 0) + "px";
};
}(this.monitor, canvas, this.config['monitorWidth'], this.config['monitorHeight']);
this.monitor.onresize();
}
/*
* The following is a related hack that allows the user to force the monitor to use a particular aspect
* ratio if an 'aspect' attribute or URL parameter is set. Initially, it's just for testing purposes
* until we figure out a better UI. And note that we use our onPageEvent() helper function to make sure
* we don't trample any other 'onresize' handler(s) attached to the window object.
*/
let aspect = +(this.config['aspect'] || this.getURLParms()['aspect']);
/*
* No 'aspect' parameter yields NaN, which is falsey, and anything else must satisfy my arbitrary
* constraints of 0.3 <= aspect <= 3.33, to prevent any useless (or worse, browser-blowing) results.
*/
if (aspect && aspect >= 0.3 && aspect <= 3.33) {
this.onPageEvent('onresize', function(parentElement, childElement, aspectRatio) {
return function onResizeWindow() {
/*
* Since aspectRatio is the target width/height, we have:
*
* parentElement.clientWidth / childElement.style.height = aspectRatio
*
* which means that:
*
* childElement.style.height = parentElement.clientWidth / aspectRatio
*
* so for example, if aspectRatio is 16:9, or 1.78, and clientWidth = 640,
* then the calculated height should approximately 360.
*/
childElement.style.height = ((parentElement.clientWidth / aspectRatio)|0) + "px";
};
}(this.monitor, canvas, aspect));
window['onresize']();
}
/*
* Here's the gross code to handle full-screen support across all supported browsers. Most of the crud is
* now buried inside findProperty(), which checks for all the browser prefix variations (eg, "moz", "webkit")
* and deals with certain property name variations, like 'Fullscreen' (new) vs 'FullScreen' (old).
*/
this.machine.isFullScreen = false;
this.fullScreen = this.fullScreenStyle = false;
let button = this.bindings[Monitor.BINDING.FULLSCREEN];
if (button) {
sProp = this.findProperty(this.container, 'requestFullscreen');
if (sProp) {
this.container.doFullScreen = this.container[sProp];
this.fullScreen = true;
this.fullScreenStyle = document.fullscreenEnabled || this.isUserAgent("Edge/");
sEvent = this.findProperty(document, 'on', 'fullscreenchange');
if (sEvent) {
let sFullScreen = this.findProperty(document, 'fullscreenElement');
document.addEventListener(sEvent, function onFullScreenChange() {
monitor.onFullScreen(document[sFullScreen] != null);
}, false);
}
sEvent = this.findProperty(document, 'on', 'fullscreenerror');
if (sEvent) {
document.addEventListener(sEvent, function onFullScreenError() {
monitor.onFullScreen();
}, false);
}
} else {
this.printf("Full-screen API not available\n");
button.parentNode.removeChild(/** @type {Node} */ (button));
}
}
/*
* The 'touchType' config property can be set to true for machines that require a full keyboard. If set,
* we create a transparent textarea "overlay" on top of the canvas and provide it to the Input device
* via addSurface(), making it easy for the user to activate the on-screen keyboard for touch-type devices.
*
* The parent div must have a style of "position:relative", so that we can position the textarea using
* "position:absolute" with "top" and "left" coordinates of zero. And we don't want the textarea to be
* visible, but we must use "opacity:0" instead of "visibility:hidden", because the latter seems to
* prevent the element from receiving events.
*
* All these styling requirements are resolved by using CSS class "pcjsSurface" for the parent div and
* CSS class "pcjsOverlay" for the textarea.
*
* Having the textarea can serve other useful purposes as well, such as providing a place for us to echo
* diagnostic messages, and it solves the Safari performance problem I observed (see above). Unfortunately,
* it creates new challenges, too. For example, textareas can cause certain key combinations, like "Alt-E",
* to be withheld as part of the browser's support for multi-key character composition. So I may have to
* alter which element on the page gets focus depending on the platform or other factors.
*
* Why do we ALSO create an overlay if fullScreen support is requested ONLY on non-iOS devices? Because
* we generally always need a surface for capturing keyboard events on desktop devices, whereas you're
* supposed to use 'touchType' if you really need keyboard events on iOS devices (ie, we don't want the
* iPhone or iPad soft keyboard popping up unnecessarily).
*/
let textarea;
if (this.touchType || this.diagnostics || this.fullScreen && !this.isUserAgent("iOS")) {
textarea = document.createElement("textarea");
let id = this.getBindingID(Monitor.BINDING.OVERLAY);
if (id) {
this.bindings[id] = textarea;
textarea.setAttribute("id", id);
}
textarea.setAttribute("class", "pcjsOverlay");
/*
* The soft keyboard on an iOS device tends to pop up with the SHIFT key depressed, which is not the
* initial keyboard state we prefer, so hopefully turning off these "auto" attributes will help.
*/
if (this.isUserAgent("iOS")) {
this.disableAuto(textarea);
/*
* One of the problems on iOS devices is that after a soft-key control is clicked, we need to give
* focus back to the above textarea, usually by calling cmp.updateFocus(), but in doing so, iOS may
* also "zoom" the page rather jarringly. While it's a simple matter to completely disable zooming,
* by fiddling with the page's viewport, that prevents the user from intentionally zooming. A bit of
* Googling reveals that another way to prevent those jarring unintentional zooms is to simply set the
* font-size of the text control to 16px. So that's what we do.
*/
textarea.style.fontSize = "16px";
}
this.monitor.appendChild(textarea);
}
/*
* If there's an Input device, make sure it is associated with our default input surface.
*/
this.input = /** @type {Input} */ (this.findDeviceByClass("Input", false));
if (this.input) {
this.input.addSurface(textarea || this.monitor, this.findBinding(this.config['focusBinding'], true));
}
/*
* These variables are here in case we want/need to add support for borders later...
*/
this.xMonitorOffset = this.yMonitorOffset = 0;
this.cxMonitorOffset = this.cxMonitor;
this.cyMonitorOffset = this.cyMonitor;
/*
* Support for disabling (or, less commonly, enabling) image smoothing, which all browsers
* seem to support now (well, OK, I still have to test the latest MS Edge browser), despite
* it still being labelled "experimental technology". Let's hope the browsers standardize
* on this. I see other options emerging, like the CSS property "image-rendering: pixelated"
* that's apparently been added to Chrome. Sigh.
*/
let fSmoothing = this.config['smoothing'];
let sSmoothing = this.getURLParms()['smoothing'];
if (sSmoothing) fSmoothing = (sSmoothing == "true");
this.fSmoothing = fSmoothing;
this.sSmoothing = this.findProperty(context, 'imageSmoothingEnabled');
this.rotateMonitor = this.config['monitorRotate'];
if (this.rotateMonitor) {
this.rotateMonitor = this.rotateMonitor % 360;
if (this.rotateMonitor > 0) this.rotateMonitor -= 360;
/*
* TODO: Consider also disallowing any rotateMonitor value if bufferRotate was already set; setting
* both is most likely a mistake, but who knows, maybe someone wants to use both for 180-degree rotation?
*/
if (this.rotateMonitor != -90) {
this.printf("unsupported monitor rotation: %d\n", this.rotateMonitor);
this.rotateMonitor = 0;
} else {
context.translate(0, this.cyMonitor);
context.rotate((this.rotateMonitor * Math.PI)/180);
context.scale(this.cyMonitor/this.cxMonitor, this.cxMonitor/this.cyMonitor);
}
}
}
/**
* addBinding(binding, element)
*
* @this {Monitor}
* @param {string} binding
* @param {Element} element
*/
addBinding(binding, element)
{
let monitor = this;
switch(binding) {
case Monitor.BINDING.FULLSCREEN:
element.onclick = function onClickFullScreen() {
/*
* I've encountered situations in Safari on iPadOS where full-screen mode was cancelled without
* notification via onFullScreen() (eg, diagnostic printf() calls that used to inadvertently change
* focus), so we'd mistakenly think we were still full-screen.
*
* print() attempts to avoid focus changes on "iOS" devices now, but the following sanity check
* still seems worthwhile.
*/
monitor.machine.isFullScreen = (window.outerHeight - window.innerHeight <= 1);
if (!monitor.machine.isFullScreen) {
monitor.doFullScreen();
} else {
if (DEBUG) monitor.printf(MESSAGE.MONITOR, "onClickFullScreen(): already full-screen?\n");
}
};
break;
}
super.addBinding(binding, element);
}
/**
* blankMonitor()
*
* @this {Monitor}
*/
blankMonitor()
{
if (this.contextMonitor) {
this.contextMonitor.fillStyle = "black";
this.contextMonitor.fillRect(0, 0, this.canvasMonitor.width, this.canvasMonitor.height);
}
}
/**
* doFullScreen()
*
* @this {Monitor}
* @returns {boolean} true if request successful, false if not (eg, failed OR not supported)
*/
doFullScreen()
{
let fSuccess = false;
if (DEBUG) this.printf(MESSAGE.MONITOR, "doFullScreen()\n");
if (this.container && this.container.doFullScreen) {
/*
* Styling the container with a width of "100%" and a height of "auto" works great when the aspect ratio
* of our virtual monitor is at least roughly equivalent to the physical screen's aspect ratio, but now that
* we support virtual VGA monitors with an aspect ratio of 1.33, that's very much out of step with modern
* wide-screen monitors, which usually have an aspect ratio of 1.6 or greater.
*
* And unfortunately, none of the browsers I've tested appear to make any attempt to scale our container to
* the physical screen's dimensions, so the bottom of our monitor gets clipped. To prevent that, I reduce
* the width from 100% to whatever percentage will accommodate the entire height of the virtual monitor.
*
* NOTE: Mozilla recommends both a width and a height of "100%", but all my tests suggest that using "auto"
* for height works equally well, so I'm sticking with it, because "auto" is also consistent with how I've
* implemented a responsive canvas when the browser window is being resized.
*/
let sWidth = "100%";
let sHeight = "auto";
if (screen && screen.width && screen.height) {
let aspectPhys = screen.width / screen.height;
let aspectVirt = this.cxMonitor / this.cyMonitor;
if (aspectPhys > aspectVirt) {
sWidth = Math.round(aspectVirt / aspectPhys * 100) + '%';
}
// TODO: We may need to someday consider the case of a physical screen with an aspect ratio < 1.0....
}
if (!this.fullScreenStyle) {
this.container.style.width = sWidth;
this.container.style.height = sHeight;
} else {
/*
* Sadly, the above code doesn't work for Firefox (nor for Chrome, as of Chrome 75 or so), because as
* http://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode explains:
*
* 'It's worth noting a key difference here between the Gecko and WebKit implementations at this time:
* Gecko automatically adds CSS rules to the element to stretch it to fill the screen: "width: 100%; height: 100%".
*
* Which would be OK if Gecko did that BEFORE we're called, but apparently it does that AFTER, effectively
* overwriting our careful calculations. So we style the inner element (canvasMonitor) instead, which
* requires even more work to ensure that the canvas is properly centered. FYI, this solution is consistent
* with Mozilla's recommendation for working around their automatic CSS rules:
*
* '[I]f you're trying to emulate WebKit's behavior on Gecko, you need to place the element you want
* to present inside another element, which you'll make fullscreen instead, and use CSS rules to adjust
* the inner element to match the appearance you want.'
*/
this.canvasMonitor.style.width = sWidth;
this.canvasMonitor.style.height = sHeight;
this.canvasMonitor.style.display = "block";
this.canvasMonitor.style.margin = "auto";
}
this.prevBackgroundColor = this.container.style.backgroundColor;
this.container.style.backgroundColor = "black";
this.container.doFullScreen();
if (this.input) this.input.setAltFocus(true);
fSuccess = true;
}
return fSuccess;
}
/**
* onFullScreen(fFullScreen)
*
* @this {Monitor}
* @param {boolean} [fFullScreen] (undefined if there was a full-screen error)
*/
onFullScreen(fFullScreen)
{
this.machine.isFullScreen = true;
if (!fFullScreen) {
if (this.container) {
if (!this.fullScreenStyle) {
this.container.style.width = this.container.style.height = "";
} else {
this.canvasMonitor.style.width = this.canvasMonitor.style.height = "";
}
if (this.prevBackgroundColor) this.container.style.backgroundColor = this.prevBackgroundColor;
}
this.machine.isFullScreen = false;
}
if (this.input && !fFullScreen) this.input.setAltFocus(false);
if (DEBUG) this.printf(MESSAGE.MONITOR, "onFullScreen(%b)\n", fFullScreen);
}
/**
* onPower(on)
*
* Called by the Machine device to provide notification of a power event.
*
* @this {Monitor}
* @param {boolean} on (true to power on, false to power off)
*/
onPower(on)
{
if (on) {
this.initCache();
this.updateScreen();
} else {
this.blankMonitor();
}
}
/**
* onReset()
*
* Called by the Machine device to provide notification of a reset event.
*
* @this {Monitor}
*/
onReset()
{
this.blankMonitor();
}
}
Monitor.BINDING = {
CONTAINER: "container",
SURFACE: "surface",
MONITOR: "monitor",
OVERLAY: "overlay",
FULLSCREEN: "fullScreen",
};
Defs.CLASSES["Monitor"] = Monitor;