]> git.proxmox.com Git - mirror_novnc.git/blame - vnc.js
Support 3.7.
[mirror_novnc.git] / vnc.js
CommitLineData
97bfe5ba 1/*
71d2426a 2 * noVNC: HTML5 VNC client
ded9dfae
JM
3 *
4 * Licensed under AGPL-3 (see LICENSE.AGPL-3)
5 *
6 * See README.md for usage and integration instructions.
97bfe5ba 7 */
c4164bda
JM
8"use strict";
9
10/*global window, WebSocket, $, Browser, Canvas, Base64, DES */
11
12// Globals defined here
56ec48be 13var VNC_native_ws, RFB;
97bfe5ba
JM
14
15
16/*
17 * Load supporting scripts
18 */
c4164bda
JM
19(function () {
20 var extra, start, end;
21 extra = "";
22 start = "<script src='";
23 end = "'><\/script>";
24
25 // Uncomment to activate firebug lite
26 //extra += start + "http://getfirebug.com/releases/lite/1.2/" +
27 // firebug-lite-compressed.js" + end;
28
29 extra += start + "include/mootools.js" + end;
30 extra += start + "include/base64.js" + end;
31 extra += start + "include/des.js" + end;
32 extra += start + "include/util.js" + end;
33 extra += start + "canvas.js" + end;
34
35 /* If no builtin websockets then load web_socket.js */
36 if (window.WebSocket) {
37 VNC_native_ws = true;
38 } else {
39 VNC_native_ws = false;
40 extra += start + "include/web-socket-js/swfobject.js" + end;
41 extra += start + "include/web-socket-js/FABridge.js" + end;
42 extra += start + "include/web-socket-js/web_socket.js" + end;
43 }
44 document.write(extra);
45}());
97bfe5ba 46
65e27ddd 47/*
cc0410a3 48 * RFB namespace
65e27ddd
JM
49 */
50
64ab5c4d
JM
51RFB = {
52
56ec48be
JM
53ws : null, // Web Socket object
54sendID : null,
55use_seq : false,
56
57// Receive and send queues
58RQ : [], // Receive Queue
59RQ_reorder : [], // Receive Queue re-order list
60RQ_seq_num : 0, // Expected sequence number
61SQ : "", // Send Queue
62
63// Frame buffer update state
64FBU : {
65 rects : 0,
66 subrects : 0, // RRE and HEXTILE
67 lines : 0, // RAW
68 tiles : 0, // HEXTILE
69 bytes : 0,
70 x : 0,
71 y : 0,
72 width : 0,
73 height : 0,
74 encoding : 0,
75 subencoding : -1,
76 background : null
77},
cc0410a3 78
db504ade 79// DOM objects
56ec48be
JM
80statusLine : null,
81connectBtn : null,
82clipboard : null,
83
84max_version : 3.8,
85version : 0,
86auth_scheme : '',
87state : 'disconnected',
88cuttext : 'none', // ServerCutText wait state
89ct_length : 0,
97bfe5ba 90clipboardFocus : false,
30059bdf 91
56ec48be
JM
92shared : 1,
93check_rate : 217,
94req_rate : 1413,
95last_req : 0,
64ab5c4d 96
56ec48be
JM
97host : '',
98port : 5900,
99password : '',
cc0410a3 100
56ec48be
JM
101fb_width : 0,
102fb_height : 0,
103fb_name : "",
104fb_Bpp : 4,
105rre_chunk : 100,
cc0410a3 106
56ec48be
JM
107timing : {
108 last_fbu : 0,
109 fbu_total : 0,
753bde8f
JM
110 fbu_total_cnt : 0,
111 full_fbu_total : 0,
56ec48be 112 full_fbu_cnt : 0
753bde8f
JM
113},
114
97bfe5ba
JM
115/* Mouse state */
116mouse_buttonmask : 0,
117mouse_arr : [],
118
cc0410a3
JM
119
120/*
121 * Server message handlers
122 */
123
65e27ddd 124/* RFB/VNC initialisation */
484a4696 125init_msg: function () {
753bde8f 126 console.log(">> init_msg [RFB.state '" + RFB.state + "']");
ef764d3b 127
67b24a90
JM
128 var RQ = RFB.RQ, strlen, reason, reason_len,
129 sversion, cversion, types, num_types, challenge, response,
c4164bda
JM
130 bpp, depth, big_endian, true_color, name_length;
131
56ec48be 132 //console.log("RQ (" + RQ.length + ") " + RQ);
64ab5c4d 133 switch (RFB.state) {
65e27ddd
JM
134
135 case 'ProtocolVersion' :
ef764d3b 136 if (RQ.length < 12) {
c4164bda
JM
137 RFB.updateState('failed',
138 "Disconnected: incomplete protocol version");
ef764d3b
JM
139 return;
140 }
67b24a90
JM
141 sversion = RQ.shiftStr(12).substr(4,7);
142 console.log("Server ProtocolVersion: " + sversion);
143 switch (sversion) {
144 case "003.003": RFB.version = 3.3; break;
145 case "003.007": RFB.version = 3.7; break;
146 case "003.008": RFB.version = 3.8; break;
147 default:
148 RFB.updateState('failed',
149 "Invalid server version " + sversion);
150 return;
151 }
152 if (RFB.version > RFB.max_version) {
153 RFB.version = RFB.max_version;
65e27ddd 154 }
67b24a90
JM
155
156 cversion = "00" + parseInt(RFB.version,10) +
157 ".00" + ((RFB.version * 10) % 10);
158 RFB.send_string("RFB " + cversion + "\n");
159 RFB.updateState('Security', "Sent ProtocolVersion: " + sversion);
65e27ddd
JM
160 break;
161
ef764d3b 162 case 'Security' :
67b24a90 163 if (RFB.version >= 3.7) {
c4164bda
JM
164 num_types = RQ.shift8();
165 if (num_types === 0) {
166 strlen = RQ.shift32();
167 reason = RQ.shiftStr(strlen);
168 RFB.updateState('failed',
169 "Disconnected: security failure: " + reason);
ef764d3b
JM
170 return;
171 }
c4164bda 172 types = RQ.shiftBytes(num_types);
2cec49d4 173
c4164bda
JM
174 if ((types[0] !== 1) && (types[0] !== 2)) {
175 RFB.updateState('failed',
176 "Disconnected: invalid security types list: " + types);
ef764d3b
JM
177 return;
178 }
2cec49d4 179
c4164bda 180 if (RFB.password.length === 0) {
4ce2696d 181 RFB.auth_scheme = 1;
2cec49d4 182 } else {
4ce2696d 183 RFB.auth_scheme = types[0];
2cec49d4 184 }
ef764d3b 185 RFB.send_array([RFB.auth_scheme]);
67b24a90 186 } else {
c539e4dc
JM
187 if (RQ.length < 4) {
188 RFB.updateState('failed', "Invalid security frame");
189 return;
190 }
191 RFB.auth_scheme = RQ.shift32();
65e27ddd 192 }
c4164bda
JM
193 RFB.updateState('Authentication',
194 "Authenticating using scheme: " + RFB.auth_scheme);
ef764d3b
JM
195 // Fall through
196
197 case 'Authentication' :
198 console.log("Security auth scheme: " + RFB.auth_scheme);
199 switch (RFB.auth_scheme) {
65e27ddd 200 case 0: // connection failed
ef764d3b
JM
201 if (RQ.length < 4) {
202 console.log(" waiting for auth reason bytes");
203 return;
204 }
c4164bda
JM
205 strlen = RQ.shift32();
206 reason = RQ.shiftStr(strlen);
207 RFB.updateState('failed',
208 "Disconnected: auth failure: " + reason);
65e27ddd
JM
209 return;
210 case 1: // no authentication
2cec49d4
KC
211 // RFB.send_array([RFB.shared]); // ClientInitialisation
212 RFB.updateState('SecurityResult');
65e27ddd
JM
213 break;
214 case 2: // VNC authentication
ef764d3b
JM
215 if (RQ.length < 16) {
216 console.log(" waiting for auth challenge bytes");
217 return;
218 }
c4164bda 219 challenge = RQ.shiftBytes(16);
753bde8f 220 //console.log("Password: " + RFB.password);
c4164bda
JM
221 //console.log("Challenge: " + challenge +
222 // " (" + challenge.length + ")");
223 response = RFB.DES(RFB.password, challenge);
224 //console.log("Response: " + response +
225 // " (" + response.length + ")");
753bde8f
JM
226
227 console.log("Sending DES encrypted auth response");
8580b989 228 RFB.send_array(response);
8759ea6f 229 RFB.updateState('SecurityResult');
65e27ddd 230 break;
484a4696 231 default:
c4164bda
JM
232 RFB.updateState('failed',
233 "Disconnected: unsupported auth scheme: " +
234 RFB.auth_scheme);
484a4696 235 return;
65e27ddd
JM
236 }
237 break;
238
64ab5c4d 239 case 'SecurityResult' :
c539e4dc
JM
240 if (RQ.length < 4) {
241 RFB.updateState('failed', "Invalid VNC auth response");
65e27ddd
JM
242 return;
243 }
c4164bda 244 switch (RQ.shift32()) {
65e27ddd 245 case 0: // OK
8759ea6f 246 RFB.updateState('ServerInitialisation', "Authentication OK");
65e27ddd
JM
247 break;
248 case 1: // failed
67b24a90 249 if (RFB.version >= 3.8) {
c4164bda
JM
250 reason_len = RQ.shift32();
251 reason = RQ.shiftStr(reason_len);
c539e4dc 252 RFB.updateState('failed', reason);
67b24a90 253 } else {
c539e4dc
JM
254 RFB.updateState('failed', "Authentication failed");
255 }
65e27ddd
JM
256 return;
257 case 2: // too-many
c4164bda
JM
258 RFB.updateState('failed',
259 "Disconnected: too many auth attempts");
65e27ddd
JM
260 return;
261 }
64ab5c4d 262 RFB.send_array([RFB.shared]); // ClientInitialisation
65e27ddd
JM
263 break;
264
265 case 'ServerInitialisation' :
5d8e7ec0 266 if (RQ.length < 24) {
c539e4dc 267 RFB.updateState('failed', "Invalid server initialisation");
65e27ddd
JM
268 return;
269 }
489d1676
JM
270
271 /* Screen size */
5d8e7ec0
JM
272 RFB.fb_width = RQ.shift16();
273 RFB.fb_height = RQ.shift16();
489d1676 274
489d1676 275 /* PIXEL_FORMAT */
c4164bda
JM
276 bpp = RQ.shift8();
277 depth = RQ.shift8();
278 big_endian = RQ.shift8();
279 true_color = RQ.shift8();
489d1676 280
753bde8f
JM
281 console.log("Screen: " + RFB.fb_width + "x" + RFB.fb_height +
282 ", bpp: " + bpp + ", depth: " + depth +
283 ", big_endian: " + big_endian +
284 ", true_color: " + true_color);
489d1676
JM
285
286 /* Connection name/title */
5d8e7ec0 287 RQ.shiftStr(12);
c4164bda 288 name_length = RQ.shift32();
5d8e7ec0 289 RFB.fb_name = RQ.shiftStr(name_length);
489d1676 290
ded9dfae 291 Canvas.init('VNC_canvas', RFB.fb_width, RFB.fb_height,
8cf20615
JM
292 RFB.keyDown, RFB.keyUp,
293 RFB.mouseDown, RFB.mouseUp, RFB.mouseMove);
64ab5c4d 294
c4164bda
JM
295 response = RFB.pixelFormat();
296 response = response.concat(RFB.encodings());
297 response = response.concat(RFB.fbUpdateRequest(0));
298 RFB.send_array(response);
9f4af5a7 299
d064769c 300 /* Start pushing/polling */
8cf20615 301 RFB.checkEvents.delay(RFB.check_rate);
489d1676 302
8759ea6f 303 RFB.updateState('normal', "Connected to: " + RFB.fb_name);
65e27ddd
JM
304 break;
305 }
753bde8f 306 //console.log("<< init_msg");
64ab5c4d 307},
65e27ddd 308
28a5f293
JM
309
310/* Normal RFB/VNC server messages */
311normal_msg: function () {
312 //console.log(">> normal_msg");
c4164bda 313
56ec48be
JM
314 var RQ = RFB.RQ, FBU = RFB.FBU,
315 ret = true, msg_type, num_colours, msg;
316
1098b5bf 317 if (FBU.rects > 0) {
c4164bda
JM
318 msg_type = 0;
319 } else if (RFB.cuttext !== 'none') {
320 msg_type = 3;
28a5f293 321 } else {
c4164bda 322 msg_type = RQ.shift8();
28a5f293
JM
323 }
324 switch (msg_type) {
325 case 0: // FramebufferUpdate
c4164bda 326 if (FBU.rects === 0) {
5d8e7ec0
JM
327 if (RQ.length < 3) {
328 RQ.unshift(msg_type);
28a5f293 329 console.log(" waiting for FBU header bytes");
5d8e7ec0 330 return false;
28a5f293 331 }
5d8e7ec0
JM
332 RQ.shift8();
333 FBU.rects = RQ.shift16();
28a5f293 334 //console.log("FramebufferUpdate, rects:" + FBU.rects);
753bde8f 335 RFB.timing.cur_fbu = 0;
28a5f293
JM
336 FBU.bytes = 0;
337 }
338
5d8e7ec0 339 while ((FBU.rects > 0) && (RQ.length >= FBU.bytes)) {
c4164bda 340 if (FBU.bytes === 0) {
5d8e7ec0 341 if (RQ.length < 12) {
28a5f293 342 console.log(" waiting for rect header bytes");
5d8e7ec0 343 return false;
28a5f293
JM
344 }
345 /* New FramebufferUpdate */
5d8e7ec0
JM
346 FBU.x = RQ.shift16();
347 FBU.y = RQ.shift16();
348 FBU.width = RQ.shift16();
349 FBU.height = RQ.shift16();
350 FBU.encoding = parseInt(RQ.shift32(), 10);
1098b5bf
JM
351
352 // Debug:
353 /*
c4164bda
JM
354 msg = "FramebufferUpdate rects:" + FBU.rects +
355 " encoding:" + FBU.encoding
28a5f293 356 switch (FBU.encoding) {
1098b5bf
JM
357 case 0: msg += "(RAW)"; break;
358 case 1: msg += "(COPY-RECT)"; break;
359 case 2: msg += "(RRE)"; break;
360 case 5: msg += "(HEXTILE " + FBU.tiles + " tiles)"; break;
28a5f293 361 default:
c4164bda
JM
362 RFB.updateState('failed',
363 "Disconnected: unsupported encoding " +
364 FBU.encoding);
5d8e7ec0 365 return false;
28a5f293 366 }
5d8e7ec0 367 msg += ", RQ.length: " + RQ.length
1098b5bf
JM
368 console.log(msg);
369 */
28a5f293
JM
370 }
371
c4164bda 372 RFB.timing.last_fbu = (new Date()).getTime();
1098b5bf 373 switch (FBU.encoding) {
5d8e7ec0
JM
374 case 0: ret = RFB.display_raw(); break; // Raw
375 case 1: ret = RFB.display_copy_rect(); break; // Copy-Rect
376 case 2: ret = RFB.display_rre(); break; // RRE
377 case 5: ret = RFB.display_hextile(); break; // hextile
28a5f293 378 }
c4164bda
JM
379 RFB.timing.cur_fbu += ((new Date()).getTime() -
380 RFB.timing.last_fbu);
381 if (FBU.rects === 0) {
382 if ((FBU.width === RFB.fb_width) &&
383 (FBU.height === RFB.fb_height)) {
753bde8f
JM
384 RFB.timing.full_fbu_total += RFB.timing.cur_fbu;
385 RFB.timing.full_fbu_cnt += 1;
c4164bda
JM
386 console.log("Timing of full FBU, cur: " +
387 RFB.timing.cur_fbu + ", total: " +
388 RFB.timing.full_fbu_total + ", cnt: " +
389 RFB.timing.full_fbu_cnt + ", avg: " +
390 (RFB.timing.full_fbu_total /
391 RFB.timing.full_fbu_cnt));
753bde8f
JM
392 }
393 }
c4164bda 394 if (RFB.state !== "normal") { return true; }
28a5f293
JM
395 }
396
28a5f293
JM
397 break;
398 case 1: // SetColourMapEntries
399 console.log("SetColourMapEntries (unsupported)");
5d8e7ec0
JM
400 RQ.shift8(); // Padding
401 RQ.shift16(); // First colour
c4164bda 402 num_colours = RQ.shift16();
5d8e7ec0 403 RQ.shiftBytes(num_colours * 6);
28a5f293
JM
404 break;
405 case 2: // Bell
406 console.log("Bell (unsupported)");
407 break;
408 case 3: // ServerCutText
409 console.log("ServerCutText");
5d8e7ec0 410 console.log("RQ:" + RQ.slice(0,20));
c4164bda 411 if (RFB.cuttext === 'none') {
30059bdf
JM
412 RFB.cuttext = 'header';
413 }
c4164bda 414 if (RFB.cuttext === 'header') {
5d8e7ec0 415 if (RQ.length < 7) {
30059bdf 416 console.log("waiting for ServerCutText header");
5d8e7ec0 417 return false;
30059bdf 418 }
5d8e7ec0
JM
419 RQ.shiftBytes(3); // Padding
420 RFB.ct_length = RQ.shift32();
30059bdf
JM
421 }
422 RFB.cuttext = 'bytes';
5d8e7ec0 423 if (RQ.length < RFB.ct_length) {
30059bdf 424 console.log("waiting for ServerCutText bytes");
5d8e7ec0 425 return false;
30059bdf 426 }
5d8e7ec0 427 RFB.clipboardCopyTo(RQ.shiftStr(RFB.ct_length));
30059bdf 428 RFB.cuttext = 'none';
28a5f293
JM
429 break;
430 default:
c4164bda
JM
431 RFB.updateState('failed',
432 "Disconnected: illegal server message type " + msg_type);
5d8e7ec0 433 console.log("RQ.slice(0,30):" + RQ.slice(0,30));
28a5f293
JM
434 break;
435 }
436 //console.log("<< normal_msg");
5d8e7ec0 437 return ret;
28a5f293
JM
438},
439
440
441/*
442 * FramebufferUpdate encodings
443 */
444
ed7e776d 445display_raw: function () {
1098b5bf 446 //console.log(">> display_raw");
c4164bda 447
56ec48be 448 var RQ = RFB.RQ, FBU = RFB.FBU, cur_y, cur_height;
c4164bda
JM
449
450 if (FBU.lines === 0) {
1098b5bf
JM
451 FBU.lines = FBU.height;
452 }
453 FBU.bytes = FBU.width * RFB.fb_Bpp; // At least a line
5d8e7ec0 454 if (RQ.length < FBU.bytes) {
c4164bda
JM
455 //console.log(" waiting for " +
456 // (FBU.bytes - RQ.length) + " RAW bytes");
1098b5bf
JM
457 return;
458 }
c4164bda
JM
459 cur_y = FBU.y + (FBU.height - FBU.lines);
460 cur_height = Math.min(FBU.lines,
461 Math.floor(RQ.length/(FBU.width * RFB.fb_Bpp)));
5d8e7ec0
JM
462 Canvas.rgbxImage(FBU.x, cur_y, FBU.width, cur_height, RQ);
463 RQ.shiftBytes(FBU.width * cur_height * RFB.fb_Bpp);
1098b5bf
JM
464 FBU.lines -= cur_height;
465
466 if (FBU.lines > 0) {
467 FBU.bytes = FBU.width * RFB.fb_Bpp; // At least another line
468 } else {
469 FBU.rects --;
470 FBU.bytes = 0;
471 }
ed7e776d
JM
472},
473
474display_copy_rect: function () {
1098b5bf 475 //console.log(">> display_copy_rect");
c4164bda 476
56ec48be 477 var RQ = RFB.RQ, FBU = RFB.FBU, old_x, old_y;
c4164bda 478
5d8e7ec0 479 if (RQ.length < 4) {
c4164bda
JM
480 //console.log(" waiting for " +
481 // (FBU.bytes - RQ.length) + " COPY-RECT bytes");
1098b5bf
JM
482 return;
483 }
c4164bda
JM
484 old_x = RQ.shift16();
485 old_y = RQ.shift16();
ed7e776d
JM
486 Canvas.copyImage(old_x, old_y, FBU.x, FBU.y, FBU.width, FBU.height);
487 FBU.rects --;
1098b5bf 488 FBU.bytes = 0;
ed7e776d
JM
489},
490
491display_rre: function () {
56ec48be
JM
492 //console.log(">> display_rre (" + RFB.RQ.length + " bytes)");
493 var RQ = RFB.RQ, FBU = RFB.FBU, color, x, y, width, height, chunk;
c4164bda 494 if (FBU.subrects === 0) {
5d8e7ec0 495 if (RQ.length < 4 + RFB.fb_Bpp) {
c4164bda
JM
496 //console.log(" waiting for " +
497 // (4 + RFB.fb_Bpp - RQ.length) + " RRE bytes");
1098b5bf
JM
498 return;
499 }
5d8e7ec0 500 FBU.subrects = RQ.shift32();
c4164bda 501 color = RQ.shiftBytes(RFB.fb_Bpp); // Background
48ebcdb1 502 Canvas.fillRect(FBU.x, FBU.y, FBU.width, FBU.height, color);
f7618085 503 }
5d8e7ec0 504 while ((FBU.subrects > 0) && (RQ.length >= (RFB.fb_Bpp + 8))) {
c4164bda
JM
505 color = RQ.shiftBytes(RFB.fb_Bpp);
506 x = RQ.shift16();
507 y = RQ.shift16();
508 width = RQ.shift16();
509 height = RQ.shift16();
48ebcdb1 510 Canvas.fillRect(FBU.x + x, FBU.y + y, width, height, color);
1098b5bf 511 FBU.subrects --;
ed7e776d 512 }
c4164bda
JM
513 //console.log(" display_rre: rects: " + FBU.rects +
514 // ", FBU.subrects: " + FBU.subrects);
ed7e776d
JM
515
516 if (FBU.subrects > 0) {
c4164bda 517 chunk = Math.min(RFB.rre_chunk, FBU.subrects);
f7618085 518 FBU.bytes = (RFB.fb_Bpp + 8) * chunk;
ed7e776d 519 } else {
6dab56f9 520 FBU.rects --;
1098b5bf 521 FBU.bytes = 0;
ed7e776d 522 }
b7ec5487 523 //console.log("<< display_rre, FBU.bytes: " + FBU.bytes);
ed7e776d
JM
524},
525
0f628064 526display_hextile: function() {
8759ea6f 527 //console.log(">> display_hextile");
56ec48be
JM
528 var RQ = RFB.RQ, FBU = RFB.FBU,
529 subencoding, subrects, idx, tile, color, cur_tile,
530 tile_x, x, w, tile_y, y, h, xy, s, sx, sy, wh, sw, sh;
b7ec5487 531
c4164bda 532 if (FBU.tiles === 0) {
1098b5bf
JM
533 FBU.tiles_x = Math.ceil(FBU.width/16);
534 FBU.tiles_y = Math.ceil(FBU.height/16);
535 FBU.total_tiles = FBU.tiles_x * FBU.tiles_y;
536 FBU.tiles = FBU.total_tiles;
537 }
538
5d8e7ec0 539 /* FBU.bytes comes in as 1, RQ.length at least 1 */
1098b5bf
JM
540 while (FBU.tiles > 0) {
541 FBU.bytes = 1;
5d8e7ec0 542 if (RQ.length < FBU.bytes) {
1098b5bf
JM
543 console.log(" waiting for HEXTILE subencoding byte");
544 return;
545 }
5d8e7ec0 546 subencoding = RQ[0]; // Peek
1098b5bf 547 if (subencoding > 30) { // Raw
c4164bda
JM
548 RFB.updateState('failed',
549 "Disconnected: illegal hextile subencoding " + subencoding);
5d8e7ec0 550 console.log("RQ.slice(0,30):" + RQ.slice(0,30));
1098b5bf
JM
551 return;
552 }
553 subrects = 0;
9f4af5a7
JM
554 cur_tile = FBU.total_tiles - FBU.tiles;
555 tile_x = cur_tile % FBU.tiles_x;
556 tile_y = Math.floor(cur_tile / FBU.tiles_x);
557 x = FBU.x + tile_x * 16;
558 y = FBU.y + tile_y * 16;
c4164bda
JM
559 w = Math.min(16, (FBU.x + FBU.width) - x);
560 h = Math.min(16, (FBU.y + FBU.height) - y);
b7ec5487 561
1098b5bf
JM
562 /* Figure out how much we are expecting */
563 if (subencoding & 0x01) { // Raw
564 //console.log(" Raw subencoding");
565 FBU.bytes += w * h * RFB.fb_Bpp;
566 } else {
567 if (subencoding & 0x02) { // Background
568 FBU.bytes += RFB.fb_Bpp;
569 }
570 if (subencoding & 0x04) { // Foreground
571 FBU.bytes += RFB.fb_Bpp;
572 }
573 if (subencoding & 0x08) { // AnySubrects
574 FBU.bytes++; // Since we aren't shifting it off
5d8e7ec0 575 if (RQ.length < FBU.bytes) {
1098b5bf
JM
576 /* Wait for subrects byte */
577 console.log(" waiting for hextile subrects header byte");
578 return;
b7ec5487 579 }
5d8e7ec0 580 subrects = RQ[FBU.bytes-1]; // Peek
1098b5bf
JM
581 if (subencoding & 0x10) { // SubrectsColoured
582 FBU.bytes += subrects * (RFB.fb_Bpp + 2);
583 } else {
584 FBU.bytes += subrects * 2;
b7ec5487
JM
585 }
586 }
587 }
588
c4164bda
JM
589 //console.log(" tile:" + cur_tile + "/" + (FBU.total_tiles - 1) +
590 // ", subencoding:" + subencoding +
591 // "(last: " + FBU.lastsubencoding + "), subrects:" +
592 // subrects + ", tile:" + tile_x + "," + tile_y +
593 // " [" + x + "," + y + "]@" + w + "x" + h +
594 // ", d.length:" + RQ.length + ", bytes:" + FBU.bytes +
595 // " last:" + RQ.slice(FBU.bytes-10, FBU.bytes) +
596 // " next:" + RQ.slice(FBU.bytes-1, FBU.bytes+10));
5d8e7ec0 597 if (RQ.length < FBU.bytes) {
c4164bda
JM
598 //console.log(" waiting for " +
599 // (FBU.bytes - RQ.length) + " hextile bytes");
b7ec5487
JM
600 return;
601 }
602
1098b5bf 603 /* We know the encoding and have a whole tile */
5d8e7ec0 604 FBU.subencoding = RQ.shift8();
1098b5bf 605 FBU.bytes--;
c4164bda 606 if (FBU.subencoding === 0) {
1098b5bf
JM
607 if (FBU.lastsubencoding & 0x01) {
608 /* Weird: ignore blanks after RAW */
609 console.log(" Ignoring blank after RAW");
610 continue;
611 }
612 Canvas.fillRect(x, y, w, h, FBU.background);
613 } else if (FBU.subencoding & 0x01) { // Raw
5d8e7ec0 614 Canvas.rgbxImage(x, y, w, h, RQ);
1098b5bf 615 } else {
c4164bda 616 idx = 0;
1098b5bf 617 if (FBU.subencoding & 0x02) { // Background
5d8e7ec0 618 FBU.background = RQ.slice(idx, idx + RFB.fb_Bpp);
1098b5bf 619 idx += RFB.fb_Bpp;
1098b5bf
JM
620 }
621 if (FBU.subencoding & 0x04) { // Foreground
5d8e7ec0 622 FBU.foreground = RQ.slice(idx, idx + RFB.fb_Bpp);
1098b5bf 623 idx += RFB.fb_Bpp;
1098b5bf 624 }
3875f847 625
c4164bda 626 tile = Canvas.getTile(x, y, w, h, FBU.background);
1098b5bf 627 if (FBU.subencoding & 0x08) { // AnySubrects
5d8e7ec0 628 subrects = RQ[idx];
1098b5bf 629 idx++;
c4164bda 630 for (s = 0; s < subrects; s ++) {
1098b5bf 631 if (FBU.subencoding & 0x10) { // SubrectsColoured
5d8e7ec0 632 color = RQ.slice(idx, idx + RFB.fb_Bpp);
1098b5bf
JM
633 idx += RFB.fb_Bpp;
634 } else {
635 color = FBU.foreground;
b7ec5487 636 }
5d8e7ec0 637 xy = RQ[idx];
1098b5bf 638 idx++;
3875f847
JM
639 sx = (xy >> 4);
640 sy = (xy & 0x0f);
1098b5bf 641
5d8e7ec0 642 wh = RQ[idx];
1098b5bf
JM
643 idx++;
644 sw = (wh >> 4) + 1;
645 sh = (wh & 0x0f) + 1;
646
3875f847 647 Canvas.setTile(tile, sx, sy, sw, sh, color);
b7ec5487
JM
648 }
649 }
3875f847 650 Canvas.putTile(tile);
b7ec5487 651 }
5d8e7ec0 652 RQ.shiftBytes(FBU.bytes);
1098b5bf
JM
653 FBU.lastsubencoding = FBU.subencoding;
654 FBU.bytes = 0;
655 FBU.tiles --;
0f628064
JM
656 }
657
c4164bda 658 if (FBU.tiles === 0) {
b7ec5487
JM
659 FBU.rects --;
660 }
661
8759ea6f 662 //console.log("<< display_hextile");
0f628064
JM
663},
664
ed7e776d 665
65e27ddd
JM
666
667/*
668 * Client message routines
669 */
670
9f4af5a7 671pixelFormat: function () {
753bde8f 672 //console.log(">> setPixelFormat");
9f4af5a7
JM
673 var arr;
674 arr = [0]; // msg-type
64ab5c4d
JM
675 arr.push8(0); // padding
676 arr.push8(0); // padding
677 arr.push8(0); // padding
678
cc0410a3 679 arr.push8(RFB.fb_Bpp * 8); // bits-per-pixel
64ab5c4d
JM
680 arr.push8(24); // depth
681 arr.push8(0); // little-endian
682 arr.push8(1); // true-color
683
684 arr.push16(255); // red-max
685 arr.push16(255); // green-max
686 arr.push16(255); // blue-max
9f4af5a7 687 arr.push8(0); // red-shift
64ab5c4d 688 arr.push8(8); // green-shift
9f4af5a7 689 arr.push8(16); // blue-shift
64ab5c4d 690
9f4af5a7
JM
691 arr.push8(0); // padding
692 arr.push8(0); // padding
693 arr.push8(0); // padding
753bde8f 694 //console.log("<< setPixelFormat");
9f4af5a7 695 return arr;
64ab5c4d
JM
696},
697
698fixColourMapEntries: function () {
699},
700
9f4af5a7 701encodings: function () {
753bde8f 702 //console.log(">> setEncodings");
9f4af5a7
JM
703 var arr;
704 arr = [2]; // msg-type
64ab5c4d 705 arr.push8(0); // padding
b7ec5487
JM
706
707 //arr.push16(3); // encoding count
708 arr.push16(4); // encoding count
709 arr.push32(5); // hextile encoding
710
ed7e776d 711 arr.push32(2); // RRE encoding
64ab5c4d
JM
712 arr.push32(1); // copy-rect encoding
713 arr.push32(0); // raw encoding
753bde8f 714 //console.log("<< setEncodings");
9f4af5a7 715 return arr;
64ab5c4d 716},
65e27ddd 717
64ab5c4d 718fbUpdateRequest: function (incremental, x, y, xw, yw) {
410960ba 719 //console.log(">> fbUpdateRequest");
c4164bda
JM
720 if (!x) { x = 0; }
721 if (!y) { y = 0; }
722 if (!xw) { xw = RFB.fb_width; }
723 if (!yw) { yw = RFB.fb_height; }
9f4af5a7
JM
724 var arr;
725 arr = [3]; // msg-type
64ab5c4d
JM
726 arr.push8(incremental);
727 arr.push16(x);
728 arr.push16(y);
729 arr.push16(xw);
730 arr.push16(yw);
b7ec5487 731 //console.log("<< fbUpdateRequest");
9f4af5a7 732 return arr;
64ab5c4d 733},
65e27ddd 734
d9cbdc7d 735keyEvent: function (keysym, down) {
410960ba 736 //console.log(">> keyEvent, keysym: " + keysym + ", down: " + down);
9f4af5a7
JM
737 var arr;
738 arr = [4]; // msg-type
64ab5c4d
JM
739 arr.push8(down);
740 arr.push16(0);
d9cbdc7d 741 arr.push32(keysym);
b7ec5487 742 //console.log("<< keyEvent");
d064769c 743 return arr;
64ab5c4d 744},
65e27ddd 745
d064769c 746pointerEvent: function (x, y) {
c4164bda
JM
747 //console.log(">> pointerEvent, x,y: " + x + "," + y +
748 // " , mask: " + RFB.mouse_buttonMask);
d064769c
JM
749 var arr;
750 arr = [5]; // msg-type
97bfe5ba 751 arr.push8(RFB.mouse_buttonMask);
d064769c
JM
752 arr.push16(x);
753 arr.push16(y);
410960ba 754 //console.log("<< pointerEvent");
d064769c 755 return arr;
64ab5c4d 756},
65e27ddd 757
30059bdf
JM
758clientCutText: function (text) {
759 console.log(">> clientCutText");
760 var arr;
761 arr = [6]; // msg-type
762 arr.push8(0); // padding
763 arr.push8(0); // padding
764 arr.push8(0); // padding
765 arr.push32(text.length);
766 arr.pushStr(text);
ded9dfae 767 console.log("<< clientCutText:" + arr);
30059bdf 768 return arr;
64ab5c4d
JM
769},
770
771
772/*
773 * Utility routines
774 */
775
07287cfd
JM
776recv_message: function(e) {
777 //console.log(">> recv_message");
07287cfd 778
753bde8f
JM
779 try {
780 if (RFB.use_seq) {
781 RFB.recv_message_reorder(e);
782 } else {
56ec48be 783 RFB.RQ = RFB.RQ.concat(Base64.decode(e.data, 0));
753bde8f
JM
784
785 RFB.handle_message();
786 }
c4164bda
JM
787 } catch (exc) {
788 console.log("recv_message, caught exception: " + exc);
789 if (typeof exc.name !== 'undefined') {
790 RFB.updateState('failed', exc.name + ": " + exc.message);
753bde8f 791 } else {
c4164bda 792 RFB.updateState('failed', exc);
753bde8f
JM
793 }
794 }
07287cfd
JM
795 //console.log("<< recv_message");
796},
797
798recv_message_reorder: function(e) {
799 //console.log(">> recv_message_reorder");
800
56ec48be 801 var RQ_reorder = RFB.RQ_reorder, offset, seq_num, i;
c4164bda
JM
802
803 offset = e.data.indexOf(":") + 1;
804 seq_num = parseInt(e.data.substr(0, offset-1), 10);
56ec48be
JM
805 if (RFB.RQ_seq_num === seq_num) {
806 RFB.RQ = RFB.RQ.concat(Base64.decode(e.data, offset));
807 RFB.RQ_seq_num++;
07287cfd 808 } else {
c4164bda 809 console.warn("sequence number mismatch: expected " +
56ec48be
JM
810 RFB.RQ_seq_num + ", got " + seq_num);
811 if (RFB.RQ_reorder.length > 20) {
07287cfd
JM
812 RFB.updateState('failed', "Re-order queue too long");
813 } else {
56ec48be 814 RFB.RQ_reorder = RFB.RQ_reorder.concat(e.data.substr(0));
c4164bda 815 i = 0;
56ec48be
JM
816 while (i < RFB.RQ_reorder.length) {
817 offset = RFB.RQ_reorder[i].indexOf(":") + 1;
818 seq_num = parseInt(RFB.RQ_reorder[i].substr(0, offset-1), 10);
c4164bda
JM
819 //console.log("Searching reorder list item " +
820 // i + ", seq_num " + seq_num);
56ec48be 821 if (seq_num === RFB.RQ_seq_num) {
07287cfd
JM
822 /* Remove it from reorder queue, decode it and
823 * add it to the receive queue */
824 console.log("Found re-ordered packet seq_num " + seq_num);
56ec48be
JM
825 RFB.RQ = RFB.RQ.concat(
826 Base64.decode(RFB.RQ_reorder.splice(i, 1)[0],
827 offset));
828 RFB.RQ_seq_num++;
07287cfd
JM
829 i = 0; // Start search again for next one
830 } else {
831 i++;
832 }
833 }
834
835 }
836 }
837
838 RFB.handle_message();
839 //console.log("<< recv_message_reorder");
840},
841
842handle_message: function () {
843 switch (RFB.state) {
844 case 'disconnected':
845 console.error("Got data while disconnected");
846 break;
07287cfd
JM
847 case 'failed':
848 console.log("Giving up!");
849 RFB.disconnect();
850 break;
851 case 'normal':
852 RFB.normal_msg();
853 /*
56ec48be 854 while (RFB.RQ.length > 0) {
c4164bda 855 if (RFB.normal_msg() && RFB.state === 'normal') {
07287cfd
JM
856 console.log("More to process");
857 } else {
858 break;
859 }
860 }
861 */
862 break;
863 default:
864 RFB.init_msg();
865 break;
866 }
867},
868
64ab5c4d 869send_string: function (str) {
b7ec5487 870 //console.log(">> send_string: " + str);
9f4af5a7 871 RFB.send_array(str.split('').map(
c4164bda 872 function (chr) { return chr.charCodeAt(0); } ) );
64ab5c4d
JM
873},
874
875send_array: function (arr) {
1098b5bf 876 //console.log(">> send_array: " + arr);
b5537b60 877 //console.log(">> send_array: " + Base64.encode(arr));
56ec48be 878 RFB.SQ = RFB.SQ + Base64.encode(arr);
c4164bda 879 if (RFB.ws.bufferedAmount === 0) {
56ec48be
JM
880 RFB.ws.send(RFB.SQ);
881 RFB.SQ = "";
5d8e7ec0
JM
882 } else {
883 console.log("Delaying send");
884 }
64ab5c4d
JM
885},
886
c539e4dc 887DES: function (password, challenge) {
c4164bda
JM
888 var i, passwd, response;
889 passwd = [];
890 response = challenge.slice();
891 for (i=0; i < password.length; i++) {
c539e4dc 892 passwd.push(password.charCodeAt(i));
532a9fd9 893 }
c539e4dc
JM
894
895 DES.setKeys(passwd);
896 DES.encrypt(response, 0, response, 0);
897 DES.encrypt(response, 8, response, 8);
898 return response;
532a9fd9
JM
899},
900
d064769c 901flushClient: function () {
97bfe5ba
JM
902 if (RFB.mouse_arr.length > 0) {
903 //RFB.send_array(RFB.mouse_arr.concat(RFB.fbUpdateRequest(1)));
c4164bda 904 RFB.send_array(RFB.mouse_arr);
5d8e7ec0
JM
905 setTimeout(function() {
906 RFB.send_array(RFB.fbUpdateRequest(1));
907 }, 50);
908
97bfe5ba 909 RFB.mouse_arr = [];
8cf20615 910 return true;
d064769c 911 } else {
8cf20615 912 return false;
d064769c
JM
913 }
914},
915
8cf20615 916checkEvents: function () {
c4164bda
JM
917 var now;
918 if (RFB.state === 'normal') {
8cf20615 919 if (! RFB.flushClient()) {
c4164bda 920 now = new Date().getTime();
8cf20615
JM
921 if (now > RFB.last_req + RFB.req_rate) {
922 RFB.last_req = now;
923 RFB.send_array(RFB.fbUpdateRequest(1));
924 }
925 }
64ab5c4d 926 }
5d8e7ec0 927 RFB.checkEvents.delay(RFB.check_rate);
64ab5c4d
JM
928},
929
c4164bda
JM
930keyX: function (e, down) {
931 var arr;
30059bdf
JM
932 if (RFB.clipboardFocus) {
933 return true;
934 }
64ab5c4d 935 e.stop();
c4164bda 936 arr = RFB.keyEvent(Canvas.getKeysym(e), down);
d064769c
JM
937 arr = arr.concat(RFB.fbUpdateRequest(1));
938 RFB.send_array(arr);
64ab5c4d
JM
939},
940
30059bdf
JM
941keyDown: function (e) {
942 //console.log(">> keyDown: " + Canvas.getKeysym(e));
c4164bda 943 RFB.keyX(e, 1);
30059bdf
JM
944},
945
64ab5c4d 946keyUp: function (e) {
1098b5bf 947 //console.log(">> keyUp: " + Canvas.getKeysym(e));
c4164bda 948 RFB.keyX(e, 0);
d064769c
JM
949},
950
951mouseDown: function(e) {
c4164bda
JM
952 var evt, x, y;
953 evt = e.event || window.event;
d064769c
JM
954 x = (evt.clientX - Canvas.c_x);
955 y = (evt.clientY - Canvas.c_y);
c4164bda
JM
956 //console.log(">> mouseDown " + evt.which + "/" + evt.button +
957 // " " + x + "," + y);
97bfe5ba
JM
958 RFB.mouse_buttonMask |= 1 << evt.button;
959 RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
d064769c
JM
960
961 RFB.flushClient();
64ab5c4d 962},
65e27ddd 963
d064769c 964mouseUp: function(e) {
c4164bda
JM
965 var evt, x, y;
966 evt = e.event || window.event;
d064769c
JM
967 x = (evt.clientX - Canvas.c_x);
968 y = (evt.clientY - Canvas.c_y);
c4164bda
JM
969 //console.log(">> mouseUp " + evt.which + "/" + evt.button +
970 // " " + x + "," + y);
97bfe5ba
JM
971 RFB.mouse_buttonMask ^= 1 << evt.button;
972 RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
d064769c
JM
973
974 RFB.flushClient();
975},
976
977mouseMove: function(e) {
c4164bda
JM
978 var evt, x, y;
979 evt = e.event || window.event;
8cf20615
JM
980 x = (evt.clientX - Canvas.c_x);
981 y = (evt.clientY - Canvas.c_y);
1098b5bf 982 //console.log('>> mouseMove ' + x + "," + y);
97bfe5ba 983 RFB.mouse_arr = RFB.mouse_arr.concat( RFB.pointerEvent(x, y) );
d064769c
JM
984},
985
30059bdf
JM
986clipboardCopyTo: function (text) {
987 console.log(">> clipboardCopyTo: " + text.substr(0,40) + "...");
db504ade 988 RFB.clipboard.value = text;
30059bdf
JM
989 console.log("<< clipboardCopyTo");
990},
991
992clipboardPasteFrom: function () {
c4164bda
JM
993 var text;
994 if (RFB.state !== "normal") { return; }
995 text = RFB.clipboard.value;
30059bdf
JM
996 console.log(">> clipboardPasteFrom: " + text.substr(0,40) + "...");
997 RFB.send_array(RFB.clientCutText(text));
998 console.log("<< clipboardPasteFrom");
999},
1000
1001clipboardClear: function () {
db504ade 1002 RFB.clipboard.value = '';
30059bdf
JM
1003 RFB.clipboardPasteFrom();
1004},
d064769c 1005
8759ea6f 1006updateState: function(state, statusMsg) {
c4164bda
JM
1007 var s, c, func, color, cmsg;
1008 s = RFB.statusLine;
1009 c = RFB.connectBtn;
1010 func = function(msg) { console.log(msg); };
8759ea6f
JM
1011 switch (state) {
1012 case 'failed':
c4164bda 1013 func = function(msg) { console.error(msg); };
8759ea6f 1014 c.disabled = true;
753bde8f 1015 color = "#880000";
8759ea6f
JM
1016 break;
1017 case 'normal':
1018 c.value = "Disconnect";
1019 c.onclick = RFB.disconnect;
1020 c.disabled = false;
753bde8f 1021 color = "#000000";
8759ea6f
JM
1022 break;
1023 case 'disconnected':
1024 c.value = "Connect";
c4164bda 1025 c.onclick = function () { RFB.connect(); };
db504ade 1026
8759ea6f 1027 c.disabled = false;
753bde8f 1028 color = "#000000";
8759ea6f
JM
1029 break;
1030 default:
c4164bda 1031 func = function(msg) { console.warn(msg); };
8759ea6f 1032 c.disabled = true;
753bde8f 1033 color = "#444400";
8759ea6f
JM
1034 break;
1035 }
1036
753bde8f
JM
1037 if ((RFB.state === 'failed') &&
1038 ((state === 'disconnected') || (state === 'closed'))) {
1039 // Leave the failed message
1040 return;
1041 }
1042
8759ea6f 1043 RFB.state = state;
753bde8f 1044 s.style.color = color;
c4164bda 1045 cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
8759ea6f 1046 func("New state '" + state + "'." + cmsg);
c4164bda 1047 if (typeof(statusMsg) !== 'undefined') {
8759ea6f
JM
1048 s.innerHTML = statusMsg;
1049 }
1050},
65e27ddd
JM
1051
1052/*
1053 * Setup routines
1054 */
1055
532a9fd9 1056init_ws: function () {
b7ec5487 1057 console.log(">> init_ws");
c4164bda 1058
db504ade
JM
1059 var uri = "";
1060 if (RFB.encrypt) {
1061 uri = "wss://";
1062 } else {
1063 uri = "ws://";
1064 }
1065 uri += RFB.host + ":" + RFB.port + "/?b64encode";
07287cfd
JM
1066 if (RFB.use_seq) {
1067 uri += "&seq_num";
1068 }
b7ec5487 1069 console.log("connecting to " + uri);
cc0410a3 1070 RFB.ws = new WebSocket(uri);
5d8e7ec0 1071
753bde8f 1072 RFB.ws.onmessage = RFB.recv_message;
cc0410a3 1073 RFB.ws.onopen = function(e) {
8759ea6f
JM
1074 console.log(">> WebSocket.onopen");
1075 RFB.updateState('ProtocolVersion', "Starting VNC handshake");
5d8e7ec0
JM
1076 RFB.sendID = setInterval(function() {
1077 /*
1078 * Send updates either at a rate of one update every 50ms,
1079 * or whatever slower rate the network can handle
1080 */
c4164bda 1081 if (RFB.ws.bufferedAmount === 0) {
56ec48be
JM
1082 if (RFB.SQ) {
1083 RFB.ws.send(RFB.SQ);
1084 RFB.SQ = "";
5d8e7ec0
JM
1085 }
1086 } else {
1087 console.log("Delaying send");
1088 }
1089 }, 50);
8759ea6f 1090 console.log("<< WebSocket.onopen");
65e27ddd 1091 };
cc0410a3 1092 RFB.ws.onclose = function(e) {
8759ea6f 1093 console.log(">> WebSocket.onclose");
5d8e7ec0 1094 clearInterval(RFB.sendID);
753bde8f 1095 RFB.updateState('disconnected', 'VNC disconnected');
8759ea6f 1096 console.log("<< WebSocket.onclose");
5d2c3864
JM
1097 };
1098 RFB.ws.onerror = function(e) {
8759ea6f
JM
1099 console.error(">> WebSocket.onerror");
1100 console.error(" " + e);
1101 console.error("<< WebSocket.onerror");
5d2c3864 1102 };
64ab5c4d 1103
b7ec5487 1104 console.log("<< init_ws");
64ab5c4d 1105},
65e27ddd 1106
5d8e7ec0
JM
1107init_vars: function () {
1108 /* Reset state */
56ec48be
JM
1109 RFB.cuttext = 'none';
1110 RFB.ct_length = 0;
1111 RFB.RQ = [];
1112 RFB.RQ_reorder = [];
1113 RFB.RQ_seq_num = 0;
1114 RFB.SQ = "";
1115 RFB.FBU.rects = 0;
1116 RFB.FBU.subrects = 0; // RRE and HEXTILE
1117 RFB.FBU.lines = 0; // RAW
1118 RFB.FBU.tiles = 0; // HEXTILE
97bfe5ba 1119 RFB.mouse_buttonmask = 0;
56ec48be 1120 RFB.mouse_arr = [];
5d8e7ec0
JM
1121},
1122
1123
db504ade 1124connect: function (host, port, password, encrypt) {
b7ec5487 1125 console.log(">> connect");
753bde8f 1126
c4164bda
JM
1127 RFB.host = (host !== undefined) ? host :
1128 $('VNC_host').value;
1129 RFB.port = (port !== undefined) ? port :
1130 $('VNC_port').value;
1131 RFB.password = (password !== undefined) ? password :
1132 $('VNC_password').value;
1133 RFB.encrypt = (encrypt !== undefined) ? encrypt :
1134 $('VNC_encrypt').checked;
5d2c3864 1135 if ((!RFB.host) || (!RFB.port)) {
db504ade 1136 alert("Must set host and port");
532a9fd9
JM
1137 return;
1138 }
30059bdf 1139
5d8e7ec0 1140 RFB.init_vars();
30059bdf 1141
c4164bda 1142 if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
cc0410a3 1143 RFB.ws.close();
65e27ddd 1144 }
532a9fd9 1145 RFB.init_ws();
30059bdf 1146
8759ea6f 1147 RFB.updateState('ProtocolVersion');
b7ec5487 1148 console.log("<< connect");
532a9fd9
JM
1149
1150},
65e27ddd 1151
532a9fd9 1152disconnect: function () {
b7ec5487 1153 console.log(">> disconnect");
c4164bda 1154 if ((RFB.ws) && (RFB.ws.readyState === WebSocket.OPEN)) {
8759ea6f 1155 RFB.updateState('closed');
c4164bda 1156 RFB.ws.onmessage = function (e) { return; };
cc0410a3 1157 RFB.ws.close();
532a9fd9
JM
1158 }
1159 if (Canvas.ctx) {
31af85b9
JM
1160 Canvas.stop();
1161 if (! /__debug__$/i.test(document.location.href)) {
1162 Canvas.clear();
1163 }
532a9fd9 1164 }
8759ea6f 1165
753bde8f 1166 RFB.updateState('disconnected', 'Disconnected');
b7ec5487 1167 console.log("<< disconnect");
97bfe5ba 1168},
65e27ddd 1169
ded9dfae
JM
1170/*
1171 * Load the controls
1172 */
1173load: function (target) {
753bde8f 1174 //console.log(">> load");
c4164bda 1175 var html, url;
ded9dfae 1176
c4164bda 1177 if (!target) { target = 'vnc'; }
ded9dfae
JM
1178
1179 /* Populate the 'vnc' div */
c4164bda 1180 html = "";
ded9dfae
JM
1181 if ($('VNC_controls') === null) {
1182 html += '<div id="VNC_controls">';
1183 html += ' <ul>';
1184 html += ' <li>Host: <input id="VNC_host"></li>';
1185 html += ' <li>Port: <input id="VNC_port"></li>';
c4164bda
JM
1186 html += ' <li>Password: <input id="VNC_password"';
1187 html += ' type="password"></li>';
1188 html += ' <li>Encrypt: <input id="VNC_encrypt"';
1189 html += ' type="checkbox"></li>';
ded9dfae 1190 html += ' <li><input id="VNC_connect_button" type="button"';
c4164bda 1191 html += ' value="Loading" disabled></li>';
ded9dfae
JM
1192 html += ' </ul>';
1193 html += '</div>';
1194 }
1195 if ($('VNC_screen') === null) {
1196 html += '<div id="VNC_screen">';
1197 html += '</div>';
1198 }
1199 if ($('VNC_clipboard') === null) {
1200 html += '<br><br>';
1201 html += '<div id="VNC_clipboard">';
1202 html += ' VNC Clipboard:';
c4164bda
JM
1203 html += ' <input id="VNC_clipboard_clear_button"';
1204 html += ' type="button" value="Clear">';
ded9dfae
JM
1205 html += ' <br>';
1206 html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5>';
1207 html += ' </textarea>';
1208 html += '</div>';
1209 }
1210 $(target).innerHTML = html;
1211
1212 html = "";
1213 if ($('VNC_status') === null) {
1214 html += '<div id="VNC_status">Loading</div>';
1215 }
1216 if ($('VNC_canvas') === null) {
1217 html += '<canvas id="VNC_canvas" width="640px" height="20px">';
1218 html += ' Canvas not supported.';
1219 html += '</canvas>';
1220 }
1221 $('VNC_screen').innerHTML += html;
1222
db504ade
JM
1223 /* DOM object references */
1224 RFB.statusLine = $('VNC_status');
1225 RFB.connectBtn = $('VNC_connect_button');
1226 RFB.clipboard = $('VNC_clipboard_text');
1227
ded9dfae
JM
1228 /* Add handlers */
1229 $('VNC_clipboard_clear_button').onclick = RFB.clipboardClear;
db504ade
JM
1230 RFB.clipboard.onchange = RFB.clipboardPasteFrom;
1231 RFB.clipboard.onfocus = function () { RFB.clipboardFocus = true; };
1232 RFB.clipboard.onblur = function () { RFB.clipboardFocus = false; };
ded9dfae
JM
1233
1234 /* Load web-socket-js if no builtin WebSocket support */
1235 if (VNC_native_ws) {
1236 console.log("Using native WebSockets");
1237 RFB.updateState('disconnected', 'Disconnected');
1238 } else {
1239 console.log("Using web-socket-js flash bridge");
1240 if ((! Browser.Plugins.Flash) ||
1241 (Browser.Plugins.Flash.version < 9)) {
1242 RFB.updateState('failed', "WebSockets or Adobe Flash is required");
c4164bda
JM
1243 } else if (location.href.substr(0, 7) === "file://") {
1244 RFB.updateState('failed',
1245 "'file://' URL is incompatible with Adobe Flash");
ded9dfae
JM
1246 } else {
1247 WebSocket.__swfLocation = "include/web-socket-js/WebSocketMain.swf";
1248 RFB.use_seq = true;
1249 RFB.updateState('disconnected', 'Disconnected');
1250 }
1251 }
1252
1253 /* Populate the controls if defaults are provided in the URL */
c4164bda
JM
1254 if (RFB.state === 'disconnected') {
1255 url = document.location.href;
1256 $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
1257 ['',''])[1];
1258 $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
1259 ['',''])[1];
1260 $('VNC_password').value = (url.match(/password=([^&#]*)/) ||
1261 ['',''])[1];
1262 $('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
1263 ['',''])[1];
ded9dfae
JM
1264 }
1265
753bde8f 1266 //console.log("<< load");
c4164bda 1267}
ded9dfae 1268
64ab5c4d 1269}; /* End of RFB */