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.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
43 * Guacamole protocol client. Given a display element and {@link Guacamole.Tunnel},
44 * automatically handles incoming and outgoing Guacamole instructions via the
45 * provided tunnel, updating the display using one or more canvas elements.
48 * @param {Guacamole.Tunnel} tunnel The tunnel to use to send and receive
49 * Guacamole instructions.
51 Guacamole.Client = function(tunnel) {
53 var guac_client = this;
56 var STATE_CONNECTING = 1;
57 var STATE_WAITING = 2;
58 var STATE_CONNECTED = 3;
59 var STATE_DISCONNECTING = 4;
60 var STATE_DISCONNECTED = 5;
62 var currentState = STATE_IDLE;
64 var currentTimestamp = 0;
65 var pingInterval = null;
68 var displayHeight = 0;
71 var display = document.createElement("div");
72 display.style.position = "relative";
73 display.style.width = displayWidth + "px";
74 display.style.height = displayHeight + "px";
76 // Create default layer
77 var default_layer_container = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
79 // Create cursor layer
80 var cursor = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
82 // Add default layer and cursor to display
83 display.appendChild(default_layer_container.getElement());
84 display.appendChild(cursor.getElement());
86 // Initially, only default layer exists
87 var layers = [default_layer_container];
92 tunnel.onerror = function(message) {
93 if (guac_client.onerror)
94 guac_client.onerror(message);
97 function setState(state) {
98 if (state != currentState) {
100 if (guac_client.onstatechange)
101 guac_client.onstatechange(currentState);
105 function isConnected() {
106 return currentState == STATE_CONNECTED
107 || currentState == STATE_WAITING;
110 var cursorImage = null;
111 var cursorHotspotX = 0;
112 var cursorHotspotY = 0;
119 var cursorHidden = 0;
121 function redrawCursor(x, y) {
123 // Hide hardware cursor
124 if (cursorHidden == 0) {
125 display.className += " guac-hide-cursor";
130 cursor.clearRect(cursorRectX, cursorRectY, cursorRectW, cursorRectH);
133 cursorRectX = x - cursorHotspotX;
134 cursorRectY = y - cursorHotspotY;
135 cursorRectW = cursorImage.width;
136 cursorRectH = cursorImage.height;
139 cursor.drawImage(cursorRectX, cursorRectY, cursorImage);
143 guac_client.getDisplay = function() {
147 guac_client.sendKeyEvent = function(pressed, keysym) {
148 // Do not send requests if not connected
152 tunnel.sendMessage("key", keysym, pressed);
155 guac_client.sendMouseState = function(mouseState) {
157 // Do not send requests if not connected
161 // Draw client-side cursor
162 if (cursorImage != null) {
171 if (mouseState.left) buttonMask |= 1;
172 if (mouseState.middle) buttonMask |= 2;
173 if (mouseState.right) buttonMask |= 4;
174 if (mouseState.up) buttonMask |= 8;
175 if (mouseState.down) buttonMask |= 16;
178 tunnel.sendMessage("mouse", mouseState.x, mouseState.y, buttonMask);
181 guac_client.setClipboard = function(data) {
183 // Do not send requests if not connected
187 tunnel.sendMessage("clipboard", data);
191 guac_client.onstatechange = null;
192 guac_client.onname = null;
193 guac_client.onerror = null;
194 guac_client.onclipboard = null;
197 function getBufferLayer(index) {
200 var buffer = buffers[index];
202 // Create buffer if necessary
203 if (buffer == null) {
204 buffer = new Guacamole.Layer(0, 0);
206 buffers[index] = buffer;
213 function getLayerContainer(index) {
215 var layer = layers[index];
219 layer = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
220 layers[index] = layer;
222 // Get and position layer
223 var layer_element = layer.getElement();
224 layer_element.style.position = "absolute";
225 layer_element.style.left = "0px";
226 layer_element.style.top = "0px";
228 // Add to default layer container
229 default_layer_container.getElement().appendChild(layer_element);
237 function getLayer(index) {
239 // If buffer, just get layer
241 return getBufferLayer(index);
243 // Otherwise, retrieve layer from layer container
244 return getLayerContainer(index).getLayer();
248 var instructionHandlers = {
250 "error": function(parameters) {
251 if (guac_client.onerror) guac_client.onerror(parameters[0]);
252 guac_client.disconnect();
255 "name": function(parameters) {
256 if (guac_client.onname) guac_client.onname(parameters[0]);
259 "clipboard": function(parameters) {
260 if (guac_client.onclipboard) guac_client.onclipboard(parameters[0]);
263 "size": function(parameters) {
265 var layer_index = parseInt(parameters[0]);
266 var width = parseInt(parameters[1]);
267 var height = parseInt(parameters[2]);
269 // Only valid for layers (buffers auto-resize)
270 if (layer_index >= 0) {
273 var layer_container = getLayerContainer(layer_index);
274 layer_container.resize(width, height);
276 // If layer is default, resize display
277 if (layer_index == 0) {
279 displayWidth = width;
280 displayHeight = height;
282 // Update (set) display size
283 display.style.width = displayWidth + "px";
284 display.style.height = displayHeight + "px";
286 // Set cursor layer width/height
288 cursor.resize(displayWidth, displayHeight);
292 } // end if layer (not buffer)
296 "move": function(parameters) {
298 var layer_index = parseInt(parameters[0]);
299 var parent_index = parseInt(parameters[1]);
300 var x = parseInt(parameters[2]);
301 var y = parseInt(parameters[3]);
302 var z = parseInt(parameters[4]);
304 // Only valid for non-default layers
305 if (layer_index > 0 && parent_index >= 0) {
307 // Get container element
308 var layer_container = getLayerContainer(layer_index).getElement();
309 var parent = getLayerContainer(parent_index).getElement();
311 // Set parent if necessary
312 if (!(layer_container.parentNode === parent))
313 parent.appendChild(layer_container);
316 layer_container.style.left = x + "px";
317 layer_container.style.top = y + "px";
318 layer_container.style.zIndex = z;
324 "png": function(parameters) {
326 var channelMask = parseInt(parameters[0]);
327 var layer = getLayer(parseInt(parameters[1]));
328 var x = parseInt(parameters[2]);
329 var y = parseInt(parameters[3]);
330 var data = parameters[4];
332 layer.setChannelMask(channelMask);
337 "data:image/png;base64," + data
340 // If received first update, no longer waiting.
341 if (currentState == STATE_WAITING)
342 setState(STATE_CONNECTED);
346 "copy": function(parameters) {
348 var srcL = getLayer(parseInt(parameters[0]));
349 var srcX = parseInt(parameters[1]);
350 var srcY = parseInt(parameters[2]);
351 var srcWidth = parseInt(parameters[3]);
352 var srcHeight = parseInt(parameters[4]);
353 var channelMask = parseInt(parameters[5]);
354 var dstL = getLayer(parseInt(parameters[6]));
355 var dstX = parseInt(parameters[7]);
356 var dstY = parseInt(parameters[8]);
358 dstL.setChannelMask(channelMask);
372 "rect": function(parameters) {
374 var channelMask = parseInt(parameters[0]);
375 var layer = getLayer(parseInt(parameters[1]));
376 var x = parseInt(parameters[2]);
377 var y = parseInt(parameters[3]);
378 var w = parseInt(parameters[4]);
379 var h = parseInt(parameters[5]);
380 var r = parseInt(parameters[6]);
381 var g = parseInt(parameters[7]);
382 var b = parseInt(parameters[8]);
383 var a = parseInt(parameters[9]);
385 layer.setChannelMask(channelMask);
394 "clip": function(parameters) {
396 var layer = getLayer(parseInt(parameters[0]));
397 var x = parseInt(parameters[1]);
398 var y = parseInt(parameters[2]);
399 var w = parseInt(parameters[3]);
400 var h = parseInt(parameters[4]);
402 layer.clipRect(x, y, w, h);
406 "cursor": function(parameters) {
408 var x = parseInt(parameters[0]);
409 var y = parseInt(parameters[1]);
410 var data = parameters[2];
412 // Start cursor image load
413 var image = new Image();
414 image.onload = function() {
417 var cursorX = cursorRectX + cursorHotspotX;
418 var cursorY = cursorRectY + cursorHotspotY;
423 redrawCursor(cursorX, cursorY);
425 image.src = "data:image/png;base64," + data
429 "sync": function(parameters) {
431 var timestamp = parameters[0];
433 // When all layers have finished rendering all instructions
434 // UP TO THIS POINT IN TIME, send sync response.
436 var layersToSync = 0;
437 function syncLayer() {
441 // Send sync response when layers are finished
442 if (layersToSync == 0) {
443 if (timestamp != currentTimestamp) {
444 tunnel.sendMessage("sync", timestamp);
445 currentTimestamp = timestamp;
451 // Count active, not-ready layers and install sync tracking hooks
452 for (var i=0; i<layers.length; i++) {
454 var layer = layers[i].getLayer();
455 if (layer && !layer.isReady()) {
457 layer.sync(syncLayer);
462 // If all layers are ready, then we didn't install any hooks.
463 // Send sync message now,
464 if (layersToSync == 0) {
465 if (timestamp != currentTimestamp) {
466 tunnel.sendMessage("sync", timestamp);
467 currentTimestamp = timestamp;
476 tunnel.oninstruction = function(opcode, parameters) {
478 var handler = instructionHandlers[opcode];
485 guac_client.disconnect = function() {
487 // Only attempt disconnection not disconnected.
488 if (currentState != STATE_DISCONNECTED
489 && currentState != STATE_DISCONNECTING) {
491 setState(STATE_DISCONNECTING);
495 window.clearInterval(pingInterval);
497 // Send disconnect message and disconnect
498 tunnel.sendMessage("disconnect");
500 setState(STATE_DISCONNECTED);
506 guac_client.connect = function(data) {
508 setState(STATE_CONNECTING);
511 tunnel.connect(data);
514 setState(STATE_IDLE);
518 // Ping every 5 seconds (ensure connection alive)
519 pingInterval = window.setInterval(function() {
520 tunnel.sendMessage("sync", currentTimestamp);
523 setState(STATE_WAITING);
530 * Simple container for Guacamole.Layer, allowing layers to be easily
531 * repositioned and nested. This allows certain operations to be accelerated
532 * through DOM manipulation, rather than raster operations.
536 * @param {Number} width The width of the Layer, in pixels. The canvas element
537 * backing this Layer will be given this width.
539 * @param {Number} height The height of the Layer, in pixels. The canvas element
540 * backing this Layer will be given this height.
542 Guacamole.Client.LayerContainer = function(width, height) {
545 * Reference to this LayerContainer.
548 var layer_container = this;
550 // Create layer with given size
551 var layer = new Guacamole.Layer(width, height);
553 // Set layer position
554 var canvas = layer.getCanvas();
555 canvas.style.position = "absolute";
556 canvas.style.left = "0px";
557 canvas.style.top = "0px";
559 // Create div with given size
560 var div = document.createElement("div");
561 div.appendChild(canvas);
562 div.style.width = width + "px";
563 div.style.height = height + "px";
566 * Changes the size of this LayerContainer and the contained Layer to the
567 * given width and height.
569 * @param {Number} width The new width to assign to this Layer.
570 * @param {Number} height The new height to assign to this Layer.
572 layer_container.resize = function(width, height) {
575 layer.resize(width, height);
577 // Resize containing div
578 div.style.width = width + "px";
579 div.style.height = height + "px";
584 * Returns the Layer contained within this LayerContainer.
585 * @returns {Guacamole.Layer} The Layer contained within this LayerContainer.
587 layer_container.getLayer = function() {
592 * Returns the element containing the Layer within this LayerContainer.
593 * @returns {Element} The element containing the Layer within this LayerContainer.
595 layer_container.getElement = function() {