v86/src/browser/screen.js

562 lines
15 KiB
JavaScript
Raw Normal View History

2013-11-06 01:12:55 +01:00
"use strict";
/**
2020-10-04 19:46:41 +02:00
* Adapter to use visual screen in browsers (in contrast to node)
2013-11-06 01:12:55 +01:00
* @constructor
2015-04-12 16:07:52 +02:00
*
* @param {BusConnector} bus
2013-11-06 01:12:55 +01:00
*/
2014-12-29 16:42:59 +01:00
function ScreenAdapter(screen_container, bus)
2013-11-06 01:12:55 +01:00
{
console.assert(screen_container, "1st argument must be a DOM container");
2015-09-16 03:25:09 +02:00
var
graphic_screen = screen_container.getElementsByTagName("canvas")[0],
2021-01-01 02:14:31 +01:00
graphic_context = graphic_screen.getContext("2d", { alpha: false }),
2013-11-06 01:12:55 +01:00
2017-11-09 18:11:16 +01:00
text_screen = screen_container.getElementsByTagName("div")[0],
cursor_element = document.createElement("div");
2013-11-06 01:12:55 +01:00
2015-09-16 03:25:09 +02:00
var
2013-11-06 01:12:55 +01:00
/** @type {number} */
cursor_row,
/** @type {number} */
cursor_col,
/** @type {number} */
scale_x = 1,
/** @type {number} */
scale_y = 1,
base_scale = 1,
2013-11-06 01:12:55 +01:00
changed_rows,
2014-07-15 00:07:17 +02:00
// are we in graphical mode now?
is_graphical = false,
2013-11-06 01:12:55 +01:00
// Index 0: ASCII code
// Index 1: Background color
// Index 2: Foreground color
text_mode_data,
// number of columns
text_mode_width,
// number of rows
text_mode_height;
2019-07-11 22:12:40 +02:00
var stopped = false;
var screen = this;
2013-11-06 01:12:55 +01:00
2014-05-14 00:19:17 +02:00
// 0x12345 -> "#012345"
function number_as_color(n)
{
n = n.toString(16);
2022-02-09 16:35:20 +01:00
return "#" + "0".repeat(6 - n.length) + n;
2014-05-14 00:19:17 +02:00
}
2013-11-06 01:12:55 +01:00
/**
2020-10-04 19:46:41 +02:00
* Charmaps that constraint unicode sequences for the default dospage
2013-11-06 01:12:55 +01:00
* @const
*/
var charmap_high = new Uint16Array([
0x2302,
2013-11-06 01:12:55 +01:00
0xC7, 0xFC, 0xE9, 0xE2, 0xE4, 0xE0, 0xE5, 0xE7,
2015-09-16 03:25:09 +02:00
0xEA, 0xEB, 0xE8, 0xEF, 0xEE, 0xEC, 0xC4, 0xC5,
0xC9, 0xE6, 0xC6, 0xF4, 0xF6, 0xF2, 0xFB, 0xF9,
2013-11-06 01:12:55 +01:00
0xFF, 0xD6, 0xDC, 0xA2, 0xA3, 0xA5, 0x20A7, 0x192,
0xE1, 0xED, 0xF3, 0xFA, 0xF1, 0xD1, 0xAA, 0xBA,
0xBF, 0x2310, 0xAC, 0xBD, 0xBC, 0xA1, 0xAB, 0xBB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
2015-09-16 03:25:09 +02:00
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
2013-11-06 01:12:55 +01:00
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x3B1, 0xDF, 0x393, 0x3C0, 0x3A3, 0x3C3, 0xB5, 0x3C4,
2015-09-16 03:25:09 +02:00
0x3A6, 0x398, 0x3A9, 0x3B4, 0x221E, 0x3C6, 0x3B5, 0x2229,
2013-11-06 01:12:55 +01:00
0x2261, 0xB1, 0x2265, 0x2264, 0x2320, 0x2321, 0xF7,
0x2248, 0xB0, 0x2219, 0xB7, 0x221A, 0x207F, 0xB2, 0x25A0, 0xA0
]);
/** @const */
var charmap_low = new Uint16Array([
0x20, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0xB6, 0xA7, 0x25AC, 0x21A8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
]);
2014-06-22 19:05:57 +02:00
var charmap = [],
chr;
for(var i = 0; i < 256; i++)
{
if(i > 126)
2014-06-22 19:05:57 +02:00
{
chr = charmap_high[i - 0x7F];
2014-06-22 19:05:57 +02:00
}
else if(i < 32)
{
chr = charmap_low[i];
}
else
{
chr = i;
}
charmap[i] = String.fromCharCode(chr);
}
2022-06-03 13:40:59 +02:00
graphic_context.imageSmoothingEnabled = false;
2013-11-06 01:12:55 +01:00
2014-07-22 20:21:48 +02:00
cursor_element.style.position = "absolute";
cursor_element.style.backgroundColor = "#ccc";
cursor_element.style.width = "7px";
cursor_element.style.display = "inline-block";
2013-11-06 01:12:55 +01:00
2014-07-22 20:21:48 +02:00
text_screen.style.display = "block";
2013-11-06 01:12:55 +01:00
graphic_screen.style.display = "none";
2015-01-11 23:43:31 +01:00
this.bus = bus;
2015-01-11 23:43:31 +01:00
bus.register("screen-set-mode", function(data)
2014-07-19 21:40:44 +02:00
{
2015-01-11 23:43:31 +01:00
this.set_mode(data);
}, this);
2015-04-12 21:40:25 +02:00
bus.register("screen-fill-buffer-end", function(data)
2015-01-11 23:43:31 +01:00
{
this.update_buffer(data);
2015-01-11 23:43:31 +01:00
}, this);
2015-04-12 21:40:25 +02:00
2015-01-11 23:43:31 +01:00
bus.register("screen-put-char", function(data)
{
//console.log(data);
this.put_char(data[0], data[1], data[2], data[3], data[4]);
}, this);
2015-01-11 23:43:31 +01:00
bus.register("screen-update-cursor", function(data)
{
this.update_cursor(data[0], data[1]);
}, this);
bus.register("screen-update-cursor-scanline", function(data)
{
this.update_cursor_scanline(data[0], data[1]);
}, this);
2018-01-20 22:56:44 +01:00
bus.register("screen-clear", function()
{
this.clear_screen();
}, this);
2015-01-11 23:43:31 +01:00
bus.register("screen-set-size-text", function(data)
{
this.set_size_text(data[0], data[1]);
}, this);
bus.register("screen-set-size-graphical", function(data)
{
this.set_size_graphical(data[0], data[1], data[2], data[3]);
2015-01-11 23:43:31 +01:00
}, this);
2015-01-11 23:43:31 +01:00
this.init = function()
{
// not necessary, because this gets initialized by the bios early,
// but nicer to look at
this.set_size_text(80, 25);
this.timer();
2014-07-19 21:40:44 +02:00
};
2013-12-22 22:21:37 +01:00
this.make_screenshot = function()
{
2022-06-03 13:40:59 +02:00
const image = new Image();
if(is_graphical)
{
2021-01-01 02:14:33 +01:00
image.src = graphic_screen.toDataURL("image/png");
2022-06-03 13:40:59 +02:00
}
else
{
// Default 720x400, but can be [8, 16] at 640x400
const char_size = [9, 16];
const canvas = document.createElement("canvas");
canvas.width = text_mode_width * char_size[0];
canvas.height = text_mode_height * char_size[1];
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
context.font = window.getComputedStyle(text_screen).font;
context.textBaseline = "top";
for(let x = 0; x < text_mode_width; x++)
{
for(let y = 0; y < text_mode_height; y++)
{
const index = (y * text_mode_width + x) * 3;
context.fillStyle = number_as_color(text_mode_data[index + 1]);
context.fillRect(x * char_size[0], y * char_size[1], char_size[0], char_size[1]);
context.fillStyle = number_as_color(text_mode_data[index + 2]);
context.fillText(charmap[text_mode_data[index]], x * char_size[0], y * char_size[1]);
}
}
if(cursor_element.style.display !== "none")
{
context.fillStyle = cursor_element.style.backgroundColor;
context.fillRect(
cursor_col * char_size[0],
cursor_row * char_size[1] + parseInt(cursor_element.style.marginTop, 10) - 1,
parseInt(cursor_element.style.width, 10),
parseInt(cursor_element.style.height, 10)
);
}
image.src = canvas.toDataURL("image/png");
}
return image;
2013-12-22 22:21:37 +01:00
};
2013-11-06 01:12:55 +01:00
this.put_char = function(row, col, chr, bg_color, fg_color)
{
if(row < text_mode_height && col < text_mode_width)
{
var p = 3 * (row * text_mode_width + col);
2021-10-17 21:54:20 +02:00
dbg_assert(chr >= 0 && chr < 0x100);
text_mode_data[p] = chr;
text_mode_data[p + 1] = bg_color;
text_mode_data[p + 2] = fg_color;
2013-11-06 01:12:55 +01:00
changed_rows[row] = 1;
}
2013-11-06 01:12:55 +01:00
};
2014-07-15 00:07:17 +02:00
this.timer = function()
2013-11-06 01:12:55 +01:00
{
2019-07-11 22:12:40 +02:00
if(!stopped)
{
requestAnimationFrame(is_graphical ? update_graphical : update_text);
}
2013-11-06 01:12:55 +01:00
};
2017-06-19 21:12:26 +02:00
var update_text = function()
2013-11-06 01:12:55 +01:00
{
for(var i = 0; i < text_mode_height; i++)
{
if(changed_rows[i])
{
screen.text_update_row(i);
changed_rows[i] = 0;
}
}
this.timer();
2017-06-19 21:05:53 +02:00
}.bind(this);
2013-11-06 01:12:55 +01:00
2017-06-19 21:05:53 +02:00
var update_graphical = function()
2013-11-06 01:12:55 +01:00
{
2015-04-12 21:40:25 +02:00
this.bus.send("screen-fill-buffer");
this.timer();
2017-06-19 21:05:53 +02:00
}.bind(this);
2013-11-06 01:12:55 +01:00
this.destroy = function()
{
2019-07-11 22:12:40 +02:00
stopped = true;
2013-11-06 01:12:55 +01:00
};
this.set_mode = function(graphical)
{
2014-07-15 00:07:17 +02:00
is_graphical = graphical;
2013-11-06 01:12:55 +01:00
if(graphical)
{
text_screen.style.display = "none";
graphic_screen.style.display = "block";
}
else
{
text_screen.style.display = "block";
graphic_screen.style.display = "none";
}
};
this.clear_screen = function()
{
graphic_context.fillStyle = "#000";
graphic_context.fillRect(0, 0, graphic_screen.width, graphic_screen.height);
};
/**
2015-09-16 03:25:09 +02:00
* @param {number} cols
2013-11-06 01:12:55 +01:00
* @param {number} rows
*/
this.set_size_text = function(cols, rows)
{
2015-04-12 17:30:22 +02:00
if(cols === text_mode_width && rows === text_mode_height)
{
return;
}
2013-11-06 01:12:55 +01:00
changed_rows = new Int8Array(rows);
text_mode_data = new Int32Array(cols * rows * 3);
text_mode_width = cols;
text_mode_height = rows;
2014-07-19 21:40:44 +02:00
while(text_screen.childNodes.length > rows)
2013-11-06 01:12:55 +01:00
{
text_screen.removeChild(text_screen.firstChild);
}
2014-07-19 21:40:44 +02:00
while(text_screen.childNodes.length < rows)
2013-11-06 01:12:55 +01:00
{
text_screen.appendChild(document.createElement("div"));
}
2014-02-12 21:08:15 +01:00
for(var i = 0; i < rows; i++)
{
this.text_update_row(i);
}
update_scale_text();
2013-11-06 01:12:55 +01:00
};
this.set_size_graphical = function(width, height, buffer_width, buffer_height)
2013-11-06 01:12:55 +01:00
{
if(DEBUG_SCREEN_LAYERS)
{
// Draw the entire buffer. Useful for debugging
// panning / page flipping / screen splitting code for both
// v86 developers and os developers
width = buffer_width;
height = buffer_height;
}
2013-11-06 01:12:55 +01:00
graphic_screen.style.display = "block";
graphic_screen.width = width;
graphic_screen.height = height;
2015-09-16 03:25:09 +02:00
// add some scaling to tiny resolutions
if(width <= 640 && width * 2 < window.innerWidth && width * 2 < window.innerHeight)
{
base_scale = 2;
}
else
{
base_scale = 1;
}
update_scale_graphic();
2013-11-06 01:12:55 +01:00
};
this.set_scale = function(s_x, s_y)
{
scale_x = s_x;
scale_y = s_y;
update_scale_text();
update_scale_graphic();
2013-11-06 01:12:55 +01:00
};
this.set_scale(scale_x, scale_y);
function update_scale_text()
{
elem_set_scale(text_screen, scale_x, scale_y, true);
}
function update_scale_graphic()
2013-11-06 01:12:55 +01:00
{
elem_set_scale(graphic_screen, scale_x * base_scale, scale_y * base_scale, false);
}
function elem_set_scale(elem, scale_x, scale_y, use_scale)
{
elem.style.width = "";
elem.style.height = "";
2013-11-06 01:12:55 +01:00
if(use_scale)
{
2021-01-01 02:14:33 +01:00
elem.style.transform = "";
}
var rectangle = elem.getBoundingClientRect();
if(use_scale)
{
var scale_str = "";
2013-11-06 01:12:55 +01:00
scale_str += scale_x === 1 ? "" : " scaleX(" + scale_x + ")";
scale_str += scale_y === 1 ? "" : " scaleY(" + scale_y + ")";
2021-01-01 02:14:33 +01:00
elem.style.transform = scale_str;
}
else
{
// unblur non-fractional scales
if(scale_x % 1 === 0 && scale_y % 1 === 0)
{
2021-01-01 02:14:33 +01:00
graphic_screen.style["imageRendering"] = "crisp-edges"; // firefox
graphic_screen.style["imageRendering"] = "pixelated";
graphic_screen.style["-ms-interpolation-mode"] = "nearest-neighbor";
}
else
{
graphic_screen.style.imageRendering = "";
graphic_screen.style["-ms-interpolation-mode"] = "";
}
// undo fractional css-to-device pixel ratios
var device_pixel_ratio = window.devicePixelRatio || 1;
if(device_pixel_ratio % 1 !== 0)
{
scale_x /= device_pixel_ratio;
scale_y /= device_pixel_ratio;
}
}
if(scale_x !== 1)
{
elem.style.width = rectangle.width * scale_x + "px";
}
if(scale_y !== 1)
{
elem.style.height = rectangle.height * scale_y + "px";
}
2013-11-06 01:12:55 +01:00
}
this.update_cursor_scanline = function(start, end)
{
if(start & 0x20)
{
cursor_element.style.display = "none";
}
else
{
cursor_element.style.display = "inline";
2014-02-23 23:05:25 +01:00
cursor_element.style.height = Math.min(15, end - start) + "px";
cursor_element.style.marginTop = Math.min(15, start) + "px";
2013-11-06 01:12:55 +01:00
}
};
this.update_cursor = function(row, col)
{
if(row !== cursor_row || col !== cursor_col)
{
changed_rows[row] = 1;
changed_rows[cursor_row] = 1;
cursor_row = row;
cursor_col = col;
}
};
this.text_update_row = function(row)
{
var offset = 3 * row * text_mode_width,
2015-09-16 03:25:09 +02:00
row_element,
color_element,
2014-06-22 19:05:57 +02:00
fragment;
2016-09-27 22:08:38 +02:00
var bg_color,
2013-11-06 01:12:55 +01:00
fg_color,
text;
2014-06-22 19:05:57 +02:00
row_element = text_screen.childNodes[row];
2016-09-27 22:08:38 +02:00
fragment = document.createElement("div");
2013-11-06 01:12:55 +01:00
for(var i = 0; i < text_mode_width; )
{
color_element = document.createElement("span");
bg_color = text_mode_data[offset + 1];
fg_color = text_mode_data[offset + 2];
2014-05-14 00:19:17 +02:00
color_element.style.backgroundColor = number_as_color(bg_color);
color_element.style.color = number_as_color(fg_color);
2015-09-16 03:25:09 +02:00
2013-11-06 01:12:55 +01:00
text = "";
// put characters of the same color in one element
2017-06-14 19:55:15 +02:00
while(i < text_mode_width &&
text_mode_data[offset + 1] === bg_color &&
text_mode_data[offset + 2] === fg_color)
2013-11-06 01:12:55 +01:00
{
2014-06-22 19:05:57 +02:00
var ascii = text_mode_data[offset];
2013-11-06 01:12:55 +01:00
2014-06-22 19:05:57 +02:00
text += charmap[ascii];
2021-10-17 21:54:20 +02:00
dbg_assert(charmap[ascii]);
2013-11-06 01:12:55 +01:00
i++;
offset += 3;
if(row === cursor_row)
{
if(i === cursor_col)
{
// next row will be cursor
// create new element
break;
}
else if(i === cursor_col + 1)
{
// found the cursor
2014-06-22 19:05:57 +02:00
fragment.appendChild(cursor_element);
2013-11-06 01:12:55 +01:00
break;
}
}
}
color_element.textContent = text;
2014-06-22 19:05:57 +02:00
fragment.appendChild(color_element);
2013-11-06 01:12:55 +01:00
}
2016-09-27 22:08:38 +02:00
row_element.parentNode.replaceChild(fragment, row_element);
2013-11-06 01:12:55 +01:00
};
2014-12-29 16:42:59 +01:00
this.update_buffer = function(layers)
2015-04-12 21:40:25 +02:00
{
if(DEBUG_SCREEN_LAYERS)
{
// For each visible layer that would've been drawn, draw a
// rectangle to visualise the layer instead.
graphic_context.strokeStyle = "#0F0";
graphic_context.lineWidth = 4;
layers.forEach(layer =>
{
graphic_context.strokeRect(
layer.buffer_x,
layer.buffer_y,
layer.buffer_width,
layer.buffer_height
);
});
graphic_context.lineWidth = 1;
return;
}
2018-01-21 02:15:58 +01:00
layers.forEach(layer =>
{
graphic_context.putImageData(
layer.image_data,
layer.screen_x - layer.buffer_x,
layer.screen_y - layer.buffer_y,
layer.buffer_x,
layer.buffer_y,
layer.buffer_width,
layer.buffer_height
);
});
2015-04-12 21:40:25 +02:00
};
2015-01-11 23:43:31 +01:00
this.init();
2013-11-06 01:12:55 +01:00
}