summaryrefslogtreecommitdiff
path: root/utils.js
blob: 3a0ee289c82f83fb6da8406cf6f95a066c8359d8 (plain)
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
"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);
    }
}