2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacamole-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // Guacamole namespace
40 var Guacamole = Guacamole || {};
44 * Guacamole protocol client. Given a display element and {@link Guacamole.Tunnel},
45 * automatically handles incoming and outgoing Guacamole instructions via the
46 * provided tunnel, updating the display using one or more canvas elements.
49 * @param {Guacamole.Tunnel} tunnel The tunnel to use to send and receive
50 * Guacamole instructions.
52 Guacamole.Client = function(tunnel) {
54 var guac_client = this;
57 var STATE_CONNECTING = 1;
58 var STATE_WAITING = 2;
59 var STATE_CONNECTED = 3;
60 var STATE_DISCONNECTING = 4;
61 var STATE_DISCONNECTED = 5;
63 var currentState = STATE_IDLE;
65 var currentTimestamp = 0;
66 var pingInterval = null;
69 var displayHeight = 0;
72 var display = document.createElement("div");
73 display.style.position = "relative";
74 display.style.width = displayWidth + "px";
75 display.style.height = displayHeight + "px";
77 // Create default layer
78 var default_layer_container = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
80 // Position default layer
81 var default_layer_container_element = default_layer_container.getElement();
82 default_layer_container_element.style.position = "absolute";
83 default_layer_container_element.style.left = "0px";
84 default_layer_container_element.style.top = "0px";
86 // Create cursor layer
87 var cursor = new Guacamole.Client.LayerContainer(0, 0);
88 cursor.getLayer().setChannelMask(Guacamole.Layer.SRC);
90 // Position cursor layer
91 var cursor_element = cursor.getElement();
92 cursor_element.style.position = "absolute";
93 cursor_element.style.left = "0px";
94 cursor_element.style.top = "0px";
96 // Add default layer and cursor to display
97 display.appendChild(default_layer_container.getElement());
98 display.appendChild(cursor.getElement());
100 // Initially, only default layer exists
101 var layers = [default_layer_container];
103 // No initial buffers
106 tunnel.onerror = function(message) {
107 if (guac_client.onerror)
108 guac_client.onerror(message);
111 function setState(state) {
112 if (state != currentState) {
113 currentState = state;
114 if (guac_client.onstatechange)
115 guac_client.onstatechange(currentState);
119 function isConnected() {
120 return currentState == STATE_CONNECTED
121 || currentState == STATE_WAITING;
124 var cursorHotspotX = 0;
125 var cursorHotspotY = 0;
130 function moveCursor(x, y) {
132 var element = cursor.getElement();
135 element.style.left = (x - cursorHotspotX) + "px";
136 element.style.top = (y - cursorHotspotY) + "px";
138 // Update stored position
144 guac_client.getDisplay = function() {
148 guac_client.sendKeyEvent = function(pressed, keysym) {
149 // Do not send requests if not connected
153 tunnel.sendMessage("key", keysym, pressed);
156 guac_client.sendMouseState = function(mouseState) {
158 // Do not send requests if not connected
162 // Update client-side cursor
170 if (mouseState.left) buttonMask |= 1;
171 if (mouseState.middle) buttonMask |= 2;
172 if (mouseState.right) buttonMask |= 4;
173 if (mouseState.up) buttonMask |= 8;
174 if (mouseState.down) buttonMask |= 16;
177 tunnel.sendMessage("mouse", mouseState.x, mouseState.y, buttonMask);
180 guac_client.setClipboard = function(data) {
182 // Do not send requests if not connected
186 tunnel.sendMessage("clipboard", data);
190 guac_client.onstatechange = null;
191 guac_client.onname = null;
192 guac_client.onerror = null;
193 guac_client.onclipboard = null;
196 function getBufferLayer(index) {
199 var buffer = buffers[index];
201 // Create buffer if necessary
202 if (buffer == null) {
203 buffer = new Guacamole.Layer(0, 0);
205 buffers[index] = buffer;
212 function getLayerContainer(index) {
214 var layer = layers[index];
218 layer = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
219 layers[index] = layer;
221 // Get and position layer
222 var layer_element = layer.getElement();
223 layer_element.style.position = "absolute";
224 layer_element.style.left = "0px";
225 layer_element.style.top = "0px";
227 // Add to default layer container
228 default_layer_container.getElement().appendChild(layer_element);
236 function getLayer(index) {
238 // If buffer, just get layer
240 return getBufferLayer(index);
242 // Otherwise, retrieve layer from layer container
243 return getLayerContainer(index).getLayer();
247 var instructionHandlers = {
249 "error": function(parameters) {
250 if (guac_client.onerror) guac_client.onerror(parameters[0]);
251 guac_client.disconnect();
254 "name": function(parameters) {
255 if (guac_client.onname) guac_client.onname(parameters[0]);
258 "clipboard": function(parameters) {
259 if (guac_client.onclipboard) guac_client.onclipboard(parameters[0]);
262 "size": function(parameters) {
264 var layer_index = parseInt(parameters[0]);
265 var width = parseInt(parameters[1]);
266 var height = parseInt(parameters[2]);
268 // Only valid for layers (buffers auto-resize)
269 if (layer_index >= 0) {
272 var layer_container = getLayerContainer(layer_index);
273 layer_container.resize(width, height);
275 // If layer is default, resize display
276 if (layer_index == 0) {
278 displayWidth = width;
279 displayHeight = height;
281 // Update (set) display size
282 display.style.width = displayWidth + "px";
283 display.style.height = displayHeight + "px";
287 } // end if layer (not buffer)
291 "move": function(parameters) {
293 var layer_index = parseInt(parameters[0]);
294 var parent_index = parseInt(parameters[1]);
295 var x = parseInt(parameters[2]);
296 var y = parseInt(parameters[3]);
297 var z = parseInt(parameters[4]);
299 // Only valid for non-default layers
300 if (layer_index > 0 && parent_index >= 0) {
302 // Get container element
303 var layer_container = getLayerContainer(layer_index).getElement();
304 var parent = getLayerContainer(parent_index).getElement();
306 // Set parent if necessary
307 if (!(layer_container.parentNode === parent))
308 parent.appendChild(layer_container);
311 layer_container.style.left = x + "px";
312 layer_container.style.top = y + "px";
313 layer_container.style.zIndex = z;
319 "dispose": function(parameters) {
321 var layer_index = parseInt(parameters[0]);
323 // If visible layer, remove from parent
324 if (layer_index > 0) {
326 // Get container element
327 var layer_container = getLayerContainer(layer_index).getElement();
329 // Remove from parent
330 layer_container.parentNode.removeChild(layer_container);
333 delete layers[layer_index];
337 // If buffer, just delete reference
338 else if (layer_index < 0)
339 delete buffers[-1 - layer_index];
341 // Attempting to dispose the root layer currently has no effect.
345 "png": function(parameters) {
347 var channelMask = parseInt(parameters[0]);
348 var layer = getLayer(parseInt(parameters[1]));
349 var x = parseInt(parameters[2]);
350 var y = parseInt(parameters[3]);
351 var data = parameters[4];
353 layer.setChannelMask(channelMask);
358 "data:image/png;base64," + data
361 // If received first update, no longer waiting.
362 if (currentState == STATE_WAITING)
363 setState(STATE_CONNECTED);
367 "copy": function(parameters) {
369 var srcL = getLayer(parseInt(parameters[0]));
370 var srcX = parseInt(parameters[1]);
371 var srcY = parseInt(parameters[2]);
372 var srcWidth = parseInt(parameters[3]);
373 var srcHeight = parseInt(parameters[4]);
374 var channelMask = parseInt(parameters[5]);
375 var dstL = getLayer(parseInt(parameters[6]));
376 var dstX = parseInt(parameters[7]);
377 var dstY = parseInt(parameters[8]);
379 dstL.setChannelMask(channelMask);
393 "transfer": function(parameters) {
395 var srcL = getLayer(parseInt(parameters[0]));
396 var srcX = parseInt(parameters[1]);
397 var srcY = parseInt(parameters[2]);
398 var srcWidth = parseInt(parameters[3]);
399 var srcHeight = parseInt(parameters[4]);
400 var transferFunction = Guacamole.Client.DefaultTransferFunction[parameters[5]];
401 var dstL = getLayer(parseInt(parameters[6]));
402 var dstX = parseInt(parameters[7]);
403 var dstY = parseInt(parameters[8]);
418 "rect": function(parameters) {
420 var channelMask = parseInt(parameters[0]);
421 var layer = getLayer(parseInt(parameters[1]));
422 var x = parseInt(parameters[2]);
423 var y = parseInt(parameters[3]);
424 var w = parseInt(parameters[4]);
425 var h = parseInt(parameters[5]);
426 var r = parseInt(parameters[6]);
427 var g = parseInt(parameters[7]);
428 var b = parseInt(parameters[8]);
429 var a = parseInt(parameters[9]);
431 layer.setChannelMask(channelMask);
440 "clip": function(parameters) {
442 var layer = getLayer(parseInt(parameters[0]));
443 var x = parseInt(parameters[1]);
444 var y = parseInt(parameters[2]);
445 var w = parseInt(parameters[3]);
446 var h = parseInt(parameters[4]);
448 layer.clipRect(x, y, w, h);
452 "cursor": function(parameters) {
454 cursorHotspotX = parseInt(parameters[0]);
455 cursorHotspotY = parseInt(parameters[1]);
456 var srcL = getLayer(parseInt(parameters[2]));
457 var srcX = parseInt(parameters[3]);
458 var srcY = parseInt(parameters[4]);
459 var srcWidth = parseInt(parameters[5]);
460 var srcHeight = parseInt(parameters[6]);
463 cursor.resize(srcWidth, srcHeight);
465 // Draw cursor to cursor layer
466 cursor.getLayer().copyRect(
476 // Update cursor position (hotspot may have changed)
477 moveCursor(cursorX, cursorY);
481 "sync": function(parameters) {
483 var timestamp = parameters[0];
485 // When all layers have finished rendering all instructions
486 // UP TO THIS POINT IN TIME, send sync response.
488 var layersToSync = 0;
489 function syncLayer() {
493 // Send sync response when layers are finished
494 if (layersToSync == 0) {
495 if (timestamp != currentTimestamp) {
496 tunnel.sendMessage("sync", timestamp);
497 currentTimestamp = timestamp;
503 // Count active, not-ready layers and install sync tracking hooks
504 for (var i=0; i<layers.length; i++) {
506 var layer = layers[i].getLayer();
507 if (layer && !layer.isReady()) {
509 layer.sync(syncLayer);
514 // If all layers are ready, then we didn't install any hooks.
515 // Send sync message now,
516 if (layersToSync == 0) {
517 if (timestamp != currentTimestamp) {
518 tunnel.sendMessage("sync", timestamp);
519 currentTimestamp = timestamp;
528 tunnel.oninstruction = function(opcode, parameters) {
530 var handler = instructionHandlers[opcode];
537 guac_client.disconnect = function() {
539 // Only attempt disconnection not disconnected.
540 if (currentState != STATE_DISCONNECTED
541 && currentState != STATE_DISCONNECTING) {
543 setState(STATE_DISCONNECTING);
547 window.clearInterval(pingInterval);
549 // Send disconnect message and disconnect
550 tunnel.sendMessage("disconnect");
552 setState(STATE_DISCONNECTED);
558 guac_client.connect = function(data) {
560 setState(STATE_CONNECTING);
563 tunnel.connect(data);
566 setState(STATE_IDLE);
570 // Ping every 5 seconds (ensure connection alive)
571 pingInterval = window.setInterval(function() {
572 tunnel.sendMessage("sync", currentTimestamp);
575 setState(STATE_WAITING);
582 * Simple container for Guacamole.Layer, allowing layers to be easily
583 * repositioned and nested. This allows certain operations to be accelerated
584 * through DOM manipulation, rather than raster operations.
588 * @param {Number} width The width of the Layer, in pixels. The canvas element
589 * backing this Layer will be given this width.
591 * @param {Number} height The height of the Layer, in pixels. The canvas element
592 * backing this Layer will be given this height.
594 Guacamole.Client.LayerContainer = function(width, height) {
597 * Reference to this LayerContainer.
600 var layer_container = this;
602 // Create layer with given size
603 var layer = new Guacamole.Layer(width, height);
605 // Set layer position
606 var canvas = layer.getCanvas();
607 canvas.style.position = "absolute";
608 canvas.style.left = "0px";
609 canvas.style.top = "0px";
611 // Create div with given size
612 var div = document.createElement("div");
613 div.appendChild(canvas);
614 div.style.width = width + "px";
615 div.style.height = height + "px";
618 * Changes the size of this LayerContainer and the contained Layer to the
619 * given width and height.
621 * @param {Number} width The new width to assign to this Layer.
622 * @param {Number} height The new height to assign to this Layer.
624 layer_container.resize = function(width, height) {
627 layer.resize(width, height);
629 // Resize containing div
630 div.style.width = width + "px";
631 div.style.height = height + "px";
636 * Returns the Layer contained within this LayerContainer.
637 * @returns {Guacamole.Layer} The Layer contained within this LayerContainer.
639 layer_container.getLayer = function() {
644 * Returns the element containing the Layer within this LayerContainer.
645 * @returns {Element} The element containing the Layer within this LayerContainer.
647 layer_container.getElement = function() {
654 * Map of all Guacamole binary raster operations to transfer functions.
657 Guacamole.Client.DefaultTransferFunction = {
660 0x0: function (src, dst) {
661 dst.red = dst.green = dst.blue = 0x00;
665 0xF: function (src, dst) {
666 dst.red = dst.green = dst.blue = 0xFF;
670 0x3: function (src, dst) {
672 dst.green = src.green;
674 dst.alpha = src.alpha;
678 0x5: function (src, dst) {
683 0xC: function (src, dst) {
684 dst.red = 0xFF & ~src.red;
685 dst.green = 0xFF & ~src.green;
686 dst.blue = 0xFF & ~src.blue;
687 dst.alpha = src.alpha;
691 0xA: function (src, dst) {
692 dst.red = 0xFF & ~dst.red;
693 dst.green = 0xFF & ~dst.green;
694 dst.blue = 0xFF & ~dst.blue;
698 0x1: function (src, dst) {
699 dst.red = ( src.red & dst.red);
700 dst.green = ( src.green & dst.green);
701 dst.blue = ( src.blue & dst.blue);
705 0xE: function (src, dst) {
706 dst.red = 0xFF & ~( src.red & dst.red);
707 dst.green = 0xFF & ~( src.green & dst.green);
708 dst.blue = 0xFF & ~( src.blue & dst.blue);
712 0x7: function (src, dst) {
713 dst.red = ( src.red | dst.red);
714 dst.green = ( src.green | dst.green);
715 dst.blue = ( src.blue | dst.blue);
719 0x8: function (src, dst) {
720 dst.red = 0xFF & ~( src.red | dst.red);
721 dst.green = 0xFF & ~( src.green | dst.green);
722 dst.blue = 0xFF & ~( src.blue | dst.blue);
726 0x6: function (src, dst) {
727 dst.red = ( src.red ^ dst.red);
728 dst.green = ( src.green ^ dst.green);
729 dst.blue = ( src.blue ^ dst.blue);
733 0x9: function (src, dst) {
734 dst.red = 0xFF & ~( src.red ^ dst.red);
735 dst.green = 0xFF & ~( src.green ^ dst.green);
736 dst.blue = 0xFF & ~( src.blue ^ dst.blue);
739 /* AND inverted source */
740 0x4: function (src, dst) {
741 dst.red = 0xFF & (~src.red & dst.red);
742 dst.green = 0xFF & (~src.green & dst.green);
743 dst.blue = 0xFF & (~src.blue & dst.blue);
746 /* OR inverted source */
747 0xD: function (src, dst) {
748 dst.red = 0xFF & (~src.red | dst.red);
749 dst.green = 0xFF & (~src.green | dst.green);
750 dst.blue = 0xFF & (~src.blue | dst.blue);
753 /* AND inverted destination */
754 0x2: function (src, dst) {
755 dst.red = 0xFF & ( src.red & ~dst.red);
756 dst.green = 0xFF & ( src.green & ~dst.green);
757 dst.blue = 0xFF & ( src.blue & ~dst.blue);
760 /* OR inverted destination */
761 0xB: function (src, dst) {
762 dst.red = 0xFF & ( src.red | ~dst.red);
763 dst.green = 0xFF & ( src.green | ~dst.green);
764 dst.blue = 0xFF & ( src.blue | ~dst.blue);