2 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
3 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
4 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
5 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
6 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
7 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
8 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
9 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255
12 function base64_8(str, index) {
14 (base64_val[str.charCodeAt(index)]) +
15 (base64_val[str.charCodeAt(index+1)] << 6);
19 function base64_16(str, index) {
21 (base64_val[str.charCodeAt(index)]) +
22 (base64_val[str.charCodeAt(index+1)] << 6) +
23 (base64_val[str.charCodeAt(index+2)] << 12);
27 function base64_16s(str, index) {
28 var v = base64_16(str, index);
35 function base64_24(str, index) {
37 (base64_val[str.charCodeAt(index)]) +
38 (base64_val[str.charCodeAt(index+1)] << 6) +
39 (base64_val[str.charCodeAt(index+2)] << 12) +
40 (base64_val[str.charCodeAt(index+3)] << 18);
44 function base64_32(str, index) {
46 (base64_val[str.charCodeAt(index)]) +
47 (base64_val[str.charCodeAt(index+1)] << 6) +
48 (base64_val[str.charCodeAt(index+2)] << 12) +
49 (base64_val[str.charCodeAt(index+3)] << 18) +
50 (base64_val[str.charCodeAt(index+4)] << 24) +
51 (base64_val[str.charCodeAt(index+5)] << 30);
57 try { return new XMLHttpRequest(); } catch(e) {}
58 try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
59 try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
60 try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
61 try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
69 var window_with_mouse = 0;
71 var outstanding_commands = new Array();
72 var input_socket = null;
74 function initContext(canvas, x, y, id)
76 canvas.surface_id = id;
77 canvas.style["position"] = "absolute";
78 canvas.style["left"] = x + "px";
79 canvas.style["top"] = y + "px";
80 canvas.style["display"] = "none";
81 var context = canvas.getContext("2d");
82 context.globalCompositeOperation = "src-over";
83 context.fillRect(0, 0, canvas.width, canvas.height);
84 document.body.appendChild(canvas);
89 function handleCommands(cmd_obj)
91 var cmd = cmd_obj.data;
94 while (i < cmd.length) {
95 var command = cmd[i++];
96 last_serial = base64_32(cmd, i);
99 /* create new surface */
101 var id = base64_16(cmd, i);
103 var x = base64_16(cmd, i);
105 var y = base64_16(cmd, i);
107 var w = base64_16(cmd, i);
109 var h = base64_16(cmd, i);
111 var surface = document.createElement("canvas");
114 surfaces[id] = initContext(surface, x, y, id);
119 var id = base64_16(cmd, i);
121 surfaces[id].canvas.style["display"] = "inline";
126 var id = base64_16(cmd, i);
128 surfaces[id].canvas.style["display"] = "inline";
133 var id = base64_16(cmd, i);
135 var canvas = surfaces[id].canvas;
137 canvas.parentNode.removeChild(canvas);
143 var id = base64_16(cmd, i);
145 var x = base64_16(cmd, i);
147 var y = base64_16(cmd, i);
149 surfaces[id].canvas.style["left"] = x + "px";
150 surfaces[id].canvas.style["top"] = y + "px";
153 /* resize a surface */
155 var id = base64_16(cmd, i);
157 var w = base64_16(cmd, i);
159 var h = base64_16(cmd, i);
161 surfaces[id].canvas.width = w;
162 surfaces[id].canvas.height = h;
165 /* put image data surface */
167 var id = base64_16(cmd, i);
169 var x = base64_16(cmd, i);
171 var y = base64_16(cmd, i);
173 var size = base64_32(cmd, i);
175 var url = cmd.slice(i, i + size);
177 var img = new Image();
180 surfaces[id].drawImage(img, x, y);
183 img.onload = function() { surfaces[id].drawImage(img, x, y); handleOutstanding(); };
191 var id = base64_16(cmd, i);
194 var nrects = base64_16(cmd, i);
197 var context = surfaces[id];
204 for (var r = 0; r < nrects; r++) {
205 var x = base64_16(cmd, i);
207 var y = base64_16(cmd, i);
209 var w = base64_16(cmd, i);
211 var h = base64_16(cmd, i);
213 context.rect(x, y, w, h);
234 var dx = base64_16s(cmd, i);
236 var dy = base64_16s(cmd, i);
239 context.drawImage(context.canvas,
240 minx - dx, miny - dy, maxx - minx, maxy - miny,
241 minx, miny, maxx - minx, maxy - miny);
246 case 'q': // Query pointer
247 var id = base64_16(cmd, i);
250 var pos = getPositionsFromAbsCoord(last_x, last_y, id);
252 send_input ("q", [pos.root_x, pos.root_y, pos.win_x, pos.win_x, window_with_mouse]);
256 alert("Unknown op " + command);
262 function handleOutstanding()
264 while (outstanding_commands.length > 0) {
265 var cmd = outstanding_commands.shift();
266 if (!handleCommands(cmd)) {
267 outstanding_commands.unshift(cmd);
273 function handleLoad(event)
276 cmd_obj.data = event.target.responseText;
279 outstanding_commands.push(cmd_obj);
280 if (outstanding_commands.length == 1) {
285 function get_surface_id(ev) {
286 var id = ev.target.surface_id;
292 function send_input(cmd, args)
294 if (input_socket != null) {
295 input_socket.send(cmd + ([last_serial].concat(args)).join(","));
299 function get_document_coordinates(element)
301 var res = new Object();
302 res.x = element.offsetLeft;
303 res.y = element.offsetTop;
305 var offsetParent = element.offsetParent;
306 while (offsetParent != null) {
307 res.x += offsetParent.offsetLeft;
308 res.y += offsetParent.offsetTop;
309 offsetParent = offsetParent.offsetParent;
314 function getPositionsFromAbsCoord(absX, absY, relativeId) {
321 if (relativeId != 0) {
322 var pos = get_document_coordinates(surfaces[relativeId].canvas);
323 res.win_x = res.win_x - pos.x;
324 res.win_y = res.win_y - pos.y;
330 function getPositionsFromEvent(ev, relativeId) {
331 var res = getPositionsFromAbsCoord(ev.pageX, ev.pageY, relativeId);
339 function on_mouse_move (ev) {
340 var id = get_surface_id(ev);
341 var pos = getPositionsFromEvent(ev, id);
342 send_input ("m", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
345 function on_mouse_over (ev) {
346 var id = get_surface_id(ev);
347 var pos = getPositionsFromEvent(ev, id);
348 window_with_mouse = id;
349 if (window_with_mouse != 0) {
350 send_input ("e", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
354 function on_mouse_out (ev) {
355 var id = get_surface_id(ev);
356 var pos = getPositionsFromEvent(ev, id);
359 send_input ("l", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp]);
361 window_with_mouse = 0;
364 function on_mouse_down (ev) {
365 var id = get_surface_id(ev);
366 var pos = getPositionsFromEvent(ev, id);
367 send_input ("b", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, ev.button]);
370 function on_mouse_up (ev) {
371 var id = get_surface_id(ev);
372 var pos = getPositionsFromEvent(ev, id);
373 send_input ("B", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, ev.button]);
376 var last_key_down = 0;
377 function on_key_down (ev) {
378 var key_code = ev.keyCode;
379 if (key_code != last_key_down) {
380 send_input ("k", [key_code, ev.timeStamp]);
381 last_key_down = key_code;
385 function on_key_up (ev) {
386 var key_code = ev.keyCode;
387 send_input ("K", [key_code, ev.timeStamp]);
391 function cancel_event(ev)
393 ev = ev ? ev : window.event;
394 if (ev.stopPropagation)
395 ev.stopPropagation();
396 if (ev.preventDefault)
398 ev.cancelBubble = true;
400 ev.returnValue = false;
404 function on_mouse_wheel(ev)
406 ev = ev ? ev : window.event;
408 var id = get_surface_id(ev);
409 var pos = getPositionsFromEvent(ev, id);
411 var offset = ev.detail ? ev.detail : ev.wheelDelta;
415 send_input ("s", [id, pos.root_x, pos.root_y, pos.win_x, pos.win_y, ev.timeStamp, dir]);
417 return cancel_event(ev);
422 var xhr = createXHR();
424 if (typeof xhr.multipart == 'undefined') {
425 alert("Sorry, this example only works in browsers that support multipart.");
429 xhr.multipart = true;
430 xhr.open("GET", "/output", true);
431 xhr.onload = handleLoad;
435 if ("WebSocket" in window) {
436 var loc = window.location.toString().replace("http:", "ws:");
437 loc = loc.substr(0, loc.lastIndexOf('/')) + "/input";
438 var ws = new WebSocket(loc, "broadway");
439 ws.onopen = function() {
442 ws.onclose = function() {
446 alert("WebSocket not supported, input will not work!");
448 document.oncontextmenu = function () { return false; };
449 document.onmousemove = on_mouse_move;
450 document.onmouseover = on_mouse_over;
451 document.onmouseout = on_mouse_out;
452 document.onmousedown = on_mouse_down;
453 document.onmouseup = on_mouse_up;
454 document.onkeydown = on_key_down;
455 document.onkeyup = on_key_up;
457 if (document.addEventListener) {
458 document.addEventListener('DOMMouseScroll', on_mouse_wheel, false);
459 document.addEventListener('mousewheel', on_mouse_wheel, false);
460 } else if (document.attachEvent) {
461 element.attachEvent("onmousewheel", on_mouse_wheel);