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 // Position default layer
80 var default_layer_container_element = default_layer_container.getElement();
81 default_layer_container_element.style.position = "absolute";
82 default_layer_container_element.style.left = "0px";
83 default_layer_container_element.style.top = "0px";
85 // Create cursor layer
86 var cursor = new Guacamole.Client.LayerContainer(0, 0);
87 cursor.getLayer().setChannelMask(Guacamole.Layer.SRC);
89 // Position cursor layer
90 var cursor_element = cursor.getElement();
91 cursor_element.style.position = "absolute";
92 cursor_element.style.left = "0px";
93 cursor_element.style.top = "0px";
95 // Add default layer and cursor to display
96 display.appendChild(default_layer_container.getElement());
97 display.appendChild(cursor.getElement());
99 // Initially, only default layer exists
100 var layers = [default_layer_container];
102 // No initial buffers
105 tunnel.onerror = function(message) {
106 if (guac_client.onerror)
107 guac_client.onerror(message);
110 function setState(state) {
111 if (state != currentState) {
112 currentState = state;
113 if (guac_client.onstatechange)
114 guac_client.onstatechange(currentState);
118 function isConnected() {
119 return currentState == STATE_CONNECTED
120 || currentState == STATE_WAITING;
123 var cursorHotspotX = 0;
124 var cursorHotspotY = 0;
126 function moveCursor(x, y) {
128 var element = cursor.getElement();
131 element.style.left = (x - cursorHotspotX) + "px";
132 element.style.top = (y - cursorHotspotY) + "px";
136 guac_client.getDisplay = function() {
140 guac_client.sendKeyEvent = function(pressed, keysym) {
141 // Do not send requests if not connected
145 tunnel.sendMessage("key", keysym, pressed);
148 guac_client.sendMouseState = function(mouseState) {
150 // Do not send requests if not connected
154 // Update client-side cursor
162 if (mouseState.left) buttonMask |= 1;
163 if (mouseState.middle) buttonMask |= 2;
164 if (mouseState.right) buttonMask |= 4;
165 if (mouseState.up) buttonMask |= 8;
166 if (mouseState.down) buttonMask |= 16;
169 tunnel.sendMessage("mouse", mouseState.x, mouseState.y, buttonMask);
172 guac_client.setClipboard = function(data) {
174 // Do not send requests if not connected
178 tunnel.sendMessage("clipboard", data);
182 guac_client.onstatechange = null;
183 guac_client.onname = null;
184 guac_client.onerror = null;
185 guac_client.onclipboard = null;
188 function getBufferLayer(index) {
191 var buffer = buffers[index];
193 // Create buffer if necessary
194 if (buffer == null) {
195 buffer = new Guacamole.Layer(0, 0);
197 buffers[index] = buffer;
204 function getLayerContainer(index) {
206 var layer = layers[index];
210 layer = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
211 layers[index] = layer;
213 // Get and position layer
214 var layer_element = layer.getElement();
215 layer_element.style.position = "absolute";
216 layer_element.style.left = "0px";
217 layer_element.style.top = "0px";
219 // Add to default layer container
220 default_layer_container.getElement().appendChild(layer_element);
228 function getLayer(index) {
230 // If buffer, just get layer
232 return getBufferLayer(index);
234 // Otherwise, retrieve layer from layer container
235 return getLayerContainer(index).getLayer();
239 var instructionHandlers = {
241 "error": function(parameters) {
242 if (guac_client.onerror) guac_client.onerror(parameters[0]);
243 guac_client.disconnect();
246 "name": function(parameters) {
247 if (guac_client.onname) guac_client.onname(parameters[0]);
250 "clipboard": function(parameters) {
251 if (guac_client.onclipboard) guac_client.onclipboard(parameters[0]);
254 "size": function(parameters) {
256 var layer_index = parseInt(parameters[0]);
257 var width = parseInt(parameters[1]);
258 var height = parseInt(parameters[2]);
260 // Only valid for layers (buffers auto-resize)
261 if (layer_index >= 0) {
264 var layer_container = getLayerContainer(layer_index);
265 layer_container.resize(width, height);
267 // If layer is default, resize display
268 if (layer_index == 0) {
270 displayWidth = width;
271 displayHeight = height;
273 // Update (set) display size
274 display.style.width = displayWidth + "px";
275 display.style.height = displayHeight + "px";
279 } // end if layer (not buffer)
283 "move": function(parameters) {
285 var layer_index = parseInt(parameters[0]);
286 var parent_index = parseInt(parameters[1]);
287 var x = parseInt(parameters[2]);
288 var y = parseInt(parameters[3]);
289 var z = parseInt(parameters[4]);
291 // Only valid for non-default layers
292 if (layer_index > 0 && parent_index >= 0) {
294 // Get container element
295 var layer_container = getLayerContainer(layer_index).getElement();
296 var parent = getLayerContainer(parent_index).getElement();
298 // Set parent if necessary
299 if (!(layer_container.parentNode === parent))
300 parent.appendChild(layer_container);
303 layer_container.style.left = x + "px";
304 layer_container.style.top = y + "px";
305 layer_container.style.zIndex = z;
311 "png": function(parameters) {
313 var channelMask = parseInt(parameters[0]);
314 var layer = getLayer(parseInt(parameters[1]));
315 var x = parseInt(parameters[2]);
316 var y = parseInt(parameters[3]);
317 var data = parameters[4];
319 layer.setChannelMask(channelMask);
324 "data:image/png;base64," + data
327 // If received first update, no longer waiting.
328 if (currentState == STATE_WAITING)
329 setState(STATE_CONNECTED);
333 "copy": function(parameters) {
335 var srcL = getLayer(parseInt(parameters[0]));
336 var srcX = parseInt(parameters[1]);
337 var srcY = parseInt(parameters[2]);
338 var srcWidth = parseInt(parameters[3]);
339 var srcHeight = parseInt(parameters[4]);
340 var channelMask = parseInt(parameters[5]);
341 var dstL = getLayer(parseInt(parameters[6]));
342 var dstX = parseInt(parameters[7]);
343 var dstY = parseInt(parameters[8]);
345 dstL.setChannelMask(channelMask);
359 "rect": function(parameters) {
361 var channelMask = parseInt(parameters[0]);
362 var layer = getLayer(parseInt(parameters[1]));
363 var x = parseInt(parameters[2]);
364 var y = parseInt(parameters[3]);
365 var w = parseInt(parameters[4]);
366 var h = parseInt(parameters[5]);
367 var r = parseInt(parameters[6]);
368 var g = parseInt(parameters[7]);
369 var b = parseInt(parameters[8]);
370 var a = parseInt(parameters[9]);
372 layer.setChannelMask(channelMask);
381 "clip": function(parameters) {
383 var layer = getLayer(parseInt(parameters[0]));
384 var x = parseInt(parameters[1]);
385 var y = parseInt(parameters[2]);
386 var w = parseInt(parameters[3]);
387 var h = parseInt(parameters[4]);
389 layer.clipRect(x, y, w, h);
393 "cursor": function(parameters) {
395 cursorHotspotX = parseInt(parameters[0]);
396 cursorHotspotY = parseInt(parameters[1]);
397 var srcL = getLayer(parseInt(parameters[2]));
398 var srcX = parseInt(parameters[3]);
399 var srcY = parseInt(parameters[4]);
400 var srcWidth = parseInt(parameters[5]);
401 var srcHeight = parseInt(parameters[6]);
404 cursor.resize(srcWidth, srcHeight);
406 // Draw cursor to cursor layer
407 cursor.getLayer().copyRect(
417 // FIXME: Update cursor position when hotspot changes
421 "sync": function(parameters) {
423 var timestamp = parameters[0];
425 // When all layers have finished rendering all instructions
426 // UP TO THIS POINT IN TIME, send sync response.
428 var layersToSync = 0;
429 function syncLayer() {
433 // Send sync response when layers are finished
434 if (layersToSync == 0) {
435 if (timestamp != currentTimestamp) {
436 tunnel.sendMessage("sync", timestamp);
437 currentTimestamp = timestamp;
443 // Count active, not-ready layers and install sync tracking hooks
444 for (var i=0; i<layers.length; i++) {
446 var layer = layers[i].getLayer();
447 if (layer && !layer.isReady()) {
449 layer.sync(syncLayer);
454 // If all layers are ready, then we didn't install any hooks.
455 // Send sync message now,
456 if (layersToSync == 0) {
457 if (timestamp != currentTimestamp) {
458 tunnel.sendMessage("sync", timestamp);
459 currentTimestamp = timestamp;
468 tunnel.oninstruction = function(opcode, parameters) {
470 var handler = instructionHandlers[opcode];
477 guac_client.disconnect = function() {
479 // Only attempt disconnection not disconnected.
480 if (currentState != STATE_DISCONNECTED
481 && currentState != STATE_DISCONNECTING) {
483 setState(STATE_DISCONNECTING);
487 window.clearInterval(pingInterval);
489 // Send disconnect message and disconnect
490 tunnel.sendMessage("disconnect");
492 setState(STATE_DISCONNECTED);
498 guac_client.connect = function(data) {
500 setState(STATE_CONNECTING);
503 tunnel.connect(data);
506 setState(STATE_IDLE);
510 // Ping every 5 seconds (ensure connection alive)
511 pingInterval = window.setInterval(function() {
512 tunnel.sendMessage("sync", currentTimestamp);
515 setState(STATE_WAITING);
522 * Simple container for Guacamole.Layer, allowing layers to be easily
523 * repositioned and nested. This allows certain operations to be accelerated
524 * through DOM manipulation, rather than raster operations.
528 * @param {Number} width The width of the Layer, in pixels. The canvas element
529 * backing this Layer will be given this width.
531 * @param {Number} height The height of the Layer, in pixels. The canvas element
532 * backing this Layer will be given this height.
534 Guacamole.Client.LayerContainer = function(width, height) {
537 * Reference to this LayerContainer.
540 var layer_container = this;
542 // Create layer with given size
543 var layer = new Guacamole.Layer(width, height);
545 // Set layer position
546 var canvas = layer.getCanvas();
547 canvas.style.position = "absolute";
548 canvas.style.left = "0px";
549 canvas.style.top = "0px";
551 // Create div with given size
552 var div = document.createElement("div");
553 div.appendChild(canvas);
554 div.style.width = width + "px";
555 div.style.height = height + "px";
558 * Changes the size of this LayerContainer and the contained Layer to the
559 * given width and height.
561 * @param {Number} width The new width to assign to this Layer.
562 * @param {Number} height The new height to assign to this Layer.
564 layer_container.resize = function(width, height) {
567 layer.resize(width, height);
569 // Resize containing div
570 div.style.width = width + "px";
571 div.style.height = height + "px";
576 * Returns the Layer contained within this LayerContainer.
577 * @returns {Guacamole.Layer} The Layer contained within this LayerContainer.
579 layer_container.getLayer = function() {
584 * Returns the element containing the Layer within this LayerContainer.
585 * @returns {Element} The element containing the Layer within this LayerContainer.
587 layer_container.getElement = function() {