deb-spice-html5/utils.js
Jeremy White f8f622157c initial
2012-06-04 17:22:01 +03:00

240 lines
7.9 KiB
JavaScript

"use strict";
/*
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
This file is part of spice-html5.
spice-html5 is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
spice-html5 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
*/
/*----------------------------------------------------------------------------
** Utility settings and functions for Spice
**--------------------------------------------------------------------------*/
var DEBUG = 0;
var DUMP_DRAWS = false;
var DUMP_CANVASES = false;
/*----------------------------------------------------------------------------
** combine_array_buffers
** Combine two array buffers.
** FIXME - this can't be optimal. See wire.js about eliminating the need.
**--------------------------------------------------------------------------*/
function combine_array_buffers(a1, a2)
{
var in1 = new Uint8Array(a1);
var in2 = new Uint8Array(a2);
var ret = new ArrayBuffer(a1.byteLength + a2.byteLength);
var out = new Uint8Array(ret);
var o = 0;
var i;
for (i = 0; i < in1.length; i++)
out[o++] = in1[i];
for (i = 0; i < in2.length; i++)
out[o++] = in2[i];
return ret;
}
/*----------------------------------------------------------------------------
** hexdump_buffer
**--------------------------------------------------------------------------*/
function hexdump_buffer(a)
{
var mg = new Uint8Array(a);
var hex = "";
var str = "";
var last_zeros = 0;
for (var i = 0; i < mg.length; i++)
{
var h = Number(mg[i]).toString(16);
if (h.length == 1)
hex += "0";
hex += h + " ";
str += String.fromCharCode(mg[i]);
if ((i % 16 == 15) || (i == (mg.length - 1)))
{
while (i % 16 != 15)
{
hex += " ";
i++;
}
if (last_zeros == 0)
console.log(hex + " | " + str);
if (hex == "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ")
{
if (last_zeros == 1)
{
console.log(".");
last_zeros++;
}
else if (last_zeros == 0)
last_zeros++;
}
else
last_zeros = 0;
hex = str = "";
}
}
}
/*----------------------------------------------------------------------------
** FIXME FIXME FIXME - web key code to scan code mapping
** This is really atrocious code. At the end of the day, we need to
** map keystrokes generated by the browser into the scan codes required
** by spice. This table was literally built by me pressing keys on my keyboard
** and typing the resulting value in by hand.
** I'm sure Europeans will hate me, and the Chinese will likely *never*
** talk to me.
**--------------------------------------------------------------------------*/
var scanmap = [];
var codei = 1;
scanmap[27] = codei++;
scanmap['1'.charCodeAt(0)] = codei++;
scanmap['2'.charCodeAt(0)] = codei++;
scanmap['3'.charCodeAt(0)] = codei++;
scanmap['4'.charCodeAt(0)] = codei++;
scanmap['5'.charCodeAt(0)] = codei++;
scanmap['6'.charCodeAt(0)] = codei++;
scanmap['7'.charCodeAt(0)] = codei++;
scanmap['8'.charCodeAt(0)] = codei++;
scanmap['9'.charCodeAt(0)] = codei++;
scanmap['0'.charCodeAt(0)] = codei++;
scanmap[109] = codei++;
scanmap['='.charCodeAt(0)] = codei++;
scanmap[8] = codei++;
scanmap[9] = codei++;
scanmap['Q'.charCodeAt(0)] = codei++;
scanmap['W'.charCodeAt(0)] = codei++;
scanmap['E'.charCodeAt(0)] = codei++;
scanmap['R'.charCodeAt(0)] = codei++;
scanmap['T'.charCodeAt(0)] = codei++;
scanmap['Y'.charCodeAt(0)] = codei++;
scanmap['U'.charCodeAt(0)] = codei++;
scanmap['I'.charCodeAt(0)] = codei++;
scanmap['O'.charCodeAt(0)] = codei++;
scanmap['P'.charCodeAt(0)] = codei++;
scanmap[219] = codei++;
scanmap[221] = codei++;
scanmap[13] = codei++;
scanmap[17] = codei++;
scanmap['A'.charCodeAt(0)] = codei++;
scanmap['S'.charCodeAt(0)] = codei++;
scanmap['D'.charCodeAt(0)] = codei++;
scanmap['F'.charCodeAt(0)] = codei++;
scanmap['G'.charCodeAt(0)] = codei++;
scanmap['H'.charCodeAt(0)] = codei++;
scanmap['J'.charCodeAt(0)] = codei++;
scanmap['K'.charCodeAt(0)] = codei++;
scanmap['L'.charCodeAt(0)] = codei++;
scanmap[';'.charCodeAt(0)] = codei++;
scanmap[222] = codei++;
scanmap[192] = codei++;
scanmap[16] = codei++;
scanmap[220] = codei++;
scanmap['Z'.charCodeAt(0)] = codei++;
scanmap['X'.charCodeAt(0)] = codei++;
scanmap['C'.charCodeAt(0)] = codei++;
scanmap['V'.charCodeAt(0)] = codei++;
scanmap['B'.charCodeAt(0)] = codei++;
scanmap['N'.charCodeAt(0)] = codei++;
scanmap['M'.charCodeAt(0)] = codei++;
scanmap[188] = codei++;
scanmap[190] = codei++;
scanmap[191] = codei++;
codei++; // right shift seems to be same
scanmap[106] = codei++;
scanmap[18] = codei++;
scanmap[' '.charCodeAt(0)] = codei++;
scanmap[20] = codei++;
scanmap[112] = codei++;
scanmap[113] = codei++;
scanmap[114] = codei++;
scanmap[115] = codei++;
scanmap[116] = codei++;
scanmap[117] = codei++;
scanmap[118] = codei++;
scanmap[119] = codei++;
scanmap[120] = codei++;
scanmap[121] = codei++;
scanmap[144] = codei++;
scanmap[145] = codei++; // Scroll lock
scanmap[103] = codei++;
scanmap[104] = codei++;
scanmap[105] = codei++;
codei++;// skip minus;
scanmap[100] = codei++;
scanmap[101] = codei++;
scanmap[102] = codei++;
scanmap[107] = codei++;
scanmap[97] = codei++;
scanmap[98] = codei++;
scanmap[99] = codei++;
scanmap[96] = codei++;
scanmap[110] = codei++;
// F11 + 12 go up at 87/88, after the less
codei = 87;
scanmap[122] = codei++;
scanmap[123] = codei++;
codei = 99;
scanmap[42] = codei++;
codei++; // skip alt
scanmap[19] = codei++; // break
scanmap[36] = codei++; // home
scanmap[38] = codei++; // up
scanmap[33] = codei++; // page up
scanmap[37] = codei++; // left
scanmap[39] = codei++; // right
scanmap[35] = codei++; // end
scanmap[40] = codei++; // down
scanmap[34] = codei++; // page down
scanmap[45] = codei++; // insert
scanmap[46] = codei++; // delete
function keycode_to_start_scan(code)
{
if (scanmap[code] === undefined)
{
alert('no map for ' + code);
return 0;
}
if (scanmap[code] < 0x100) {
return scanmap[code];
} else {
return 0xe0 | ((scanmap[code] - 0x100) << 8);
}
}
function keycode_to_end_scan(code)
{
if (scanmap[code] === undefined)
return 0;
if (scanmap[code] < 0x100) {
return scanmap[code] | 0x80;
} else {
return 0x80e0 | ((scanmap[code] - 0x100) << 8);
}
}