345a9724dacadd16e3ffb8b8807a8d9ee5e4f8bd
[guacamole-common-js.git] / src / main / resources / guacamole.js
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is guacamole-common-js.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
40
41
42 /**
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.
46  * 
47  * @constructor
48  * @param {Guacamole.Tunnel} tunnel The tunnel to use to send and receive
49  *                                  Guacamole instructions.
50  */
51 Guacamole.Client = function(tunnel) {
52
53     var guac_client = this;
54
55     var STATE_IDLE          = 0;
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;
61
62     var currentState = STATE_IDLE;
63     
64     var currentTimestamp = 0;
65     var pingInterval = null;
66
67     var displayWidth = 0;
68     var displayHeight = 0;
69
70     // Create display
71     var display = document.createElement("div");
72     display.style.position = "relative";
73     display.style.width = displayWidth + "px";
74     display.style.height = displayHeight + "px";
75
76     // Create default layer
77     var default_layer_container = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
78
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";
84
85     // Create cursor layer
86     var cursor = new Guacamole.Client.LayerContainer(0, 0);
87     cursor.getLayer().setCompositeOperation(Guacamole.Layer.SRC);
88
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";
94
95     // Add default layer and cursor to display
96     display.appendChild(default_layer_container.getElement());
97     display.appendChild(cursor.getElement());
98
99     // Initially, only default layer exists
100     var layers =  [default_layer_container];
101
102     // No initial buffers
103     var buffers = [];
104
105     tunnel.onerror = function(message) {
106         if (guac_client.onerror)
107             guac_client.onerror(message);
108     };
109
110     function setState(state) {
111         if (state != currentState) {
112             currentState = state;
113             if (guac_client.onstatechange)
114                 guac_client.onstatechange(currentState);
115         }
116     }
117
118     function isConnected() {
119         return currentState == STATE_CONNECTED
120             || currentState == STATE_WAITING;
121     }
122
123     var cursorHotspotX = 0;
124     var cursorHotspotY = 0;
125
126     var cursorX = 0;
127     var cursorY = 0;
128
129     function moveCursor(x, y) {
130
131         var element = cursor.getElement();
132
133         // Update rect
134         element.style.left = (x - cursorHotspotX) + "px";
135         element.style.top  = (y - cursorHotspotY) + "px";
136
137         // Update stored position
138         cursorX = x;
139         cursorY = y;
140
141     }
142
143     guac_client.getDisplay = function() {
144         return display;
145     };
146
147     guac_client.sendKeyEvent = function(pressed, keysym) {
148         // Do not send requests if not connected
149         if (!isConnected())
150             return;
151
152         tunnel.sendMessage("key", keysym, pressed);
153     };
154
155     guac_client.sendMouseState = function(mouseState) {
156
157         // Do not send requests if not connected
158         if (!isConnected())
159             return;
160
161         // Update client-side cursor
162         moveCursor(
163             mouseState.x,
164             mouseState.y
165         );
166
167         // Build mask
168         var buttonMask = 0;
169         if (mouseState.left)   buttonMask |= 1;
170         if (mouseState.middle) buttonMask |= 2;
171         if (mouseState.right)  buttonMask |= 4;
172         if (mouseState.up)     buttonMask |= 8;
173         if (mouseState.down)   buttonMask |= 16;
174
175         // Send message
176         tunnel.sendMessage("mouse", mouseState.x, mouseState.y, buttonMask);
177     };
178
179     guac_client.setClipboard = function(data) {
180
181         // Do not send requests if not connected
182         if (!isConnected())
183             return;
184
185         tunnel.sendMessage("clipboard", data);
186     };
187
188     // Handlers
189     guac_client.onstatechange = null;
190     guac_client.onname = null;
191     guac_client.onerror = null;
192     guac_client.onclipboard = null;
193
194     // Layers
195     function getBufferLayer(index) {
196
197         index = -1 - index;
198         var buffer = buffers[index];
199
200         // Create buffer if necessary
201         if (buffer == null) {
202             buffer = new Guacamole.Layer(0, 0);
203             buffer.autosize = 1;
204             buffers[index] = buffer;
205         }
206
207         return buffer;
208
209     }
210
211     function getLayerContainer(index) {
212
213         var layer = layers[index];
214         if (layer == null) {
215
216             // Add new layer
217             layer = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
218             layers[index] = layer;
219
220             // Get and position layer
221             var layer_element = layer.getElement();
222             layer_element.style.position = "absolute";
223             layer_element.style.left = "0px";
224             layer_element.style.top = "0px";
225
226             // Add to default layer container
227             default_layer_container.getElement().appendChild(layer_element);
228
229         }
230
231         return layer;
232
233     }
234
235     function getLayer(index) {
236        
237         // If buffer, just get layer
238         if (index < 0)
239             return getBufferLayer(index);
240
241         // Otherwise, retrieve layer from layer container
242         return getLayerContainer(index).getLayer();
243
244     }
245
246     var instructionHandlers = {
247
248         "error": function(parameters) {
249             if (guac_client.onerror) guac_client.onerror(parameters[0]);
250             guac_client.disconnect();
251         },
252
253         "name": function(parameters) {
254             if (guac_client.onname) guac_client.onname(parameters[0]);
255         },
256
257         "clipboard": function(parameters) {
258             if (guac_client.onclipboard) guac_client.onclipboard(parameters[0]);
259         },
260
261         "size": function(parameters) {
262
263             var layer_index = parseInt(parameters[0]);
264             var width = parseInt(parameters[1]);
265             var height = parseInt(parameters[2]);
266
267             // Only valid for layers (buffers auto-resize)
268             if (layer_index >= 0) {
269
270                 // Resize layer
271                 var layer_container = getLayerContainer(layer_index);
272                 layer_container.resize(width, height);
273
274                 // If layer is default, resize display
275                 if (layer_index == 0) {
276
277                     displayWidth = width;
278                     displayHeight = height;
279
280                     // Update (set) display size
281                     display.style.width = displayWidth + "px";
282                     display.style.height = displayHeight + "px";
283
284                 }
285
286             } // end if layer (not buffer)
287
288         },
289
290         "move": function(parameters) {
291             
292             var layer_index = parseInt(parameters[0]);
293             var parent_index = parseInt(parameters[1]);
294             var x = parseInt(parameters[2]);
295             var y = parseInt(parameters[3]);
296             var z = parseInt(parameters[4]);
297
298             // Only valid for non-default layers
299             if (layer_index > 0 && parent_index >= 0) {
300
301                 // Get container element
302                 var layer_container = getLayerContainer(layer_index).getElement();
303                 var parent = getLayerContainer(parent_index).getElement();
304
305                 // Set parent if necessary
306                 if (!(layer_container.parentNode === parent))
307                     parent.appendChild(layer_container);
308
309                 // Move layer
310                 layer_container.style.left   = x + "px";
311                 layer_container.style.top    = y + "px";
312                 layer_container.style.zIndex = z;
313
314             }
315
316         },
317
318         "dispose": function(parameters) {
319             
320             var layer_index = parseInt(parameters[0]);
321
322             // If visible layer, remove from parent
323             if (layer_index > 0) {
324
325                 // Get container element
326                 var layer_container = getLayerContainer(layer_index).getElement();
327
328                 // Remove from parent
329                 layer_container.parentNode.removeChild(layer_container);
330
331                 // Delete reference
332                 delete layers[layer_index];
333
334             }
335
336             // If buffer, just delete reference
337             else if (layer_index < 0)
338                 delete buffers[-1 - layer_index];
339
340             // Attempting to dispose the root layer currently has no effect.
341
342         },
343
344         "png": function(parameters) {
345
346             var channelMask = parseInt(parameters[0]);
347             var layer = getLayer(parseInt(parameters[1]));
348             var x = parseInt(parameters[2]);
349             var y = parseInt(parameters[3]);
350             var data = parameters[4];
351
352             layer.setCompositeOperation(channelMask);
353
354             layer.draw(
355                 x,
356                 y,
357                 "data:image/png;base64," + data
358             );
359
360             // If received first update, no longer waiting.
361             if (currentState == STATE_WAITING)
362                 setState(STATE_CONNECTED);
363
364         },
365
366         "copy": function(parameters) {
367
368             var srcL = getLayer(parseInt(parameters[0]));
369             var srcX = parseInt(parameters[1]);
370             var srcY = parseInt(parameters[2]);
371             var srcWidth = parseInt(parameters[3]);
372             var srcHeight = parseInt(parameters[4]);
373             var channelMask = parseInt(parameters[5]);
374             var dstL = getLayer(parseInt(parameters[6]));
375             var dstX = parseInt(parameters[7]);
376             var dstY = parseInt(parameters[8]);
377
378             dstL.setCompositeOperation(channelMask);
379
380             dstL.copyRect(
381                 srcL,
382                 srcX,
383                 srcY,
384                 srcWidth, 
385                 srcHeight, 
386                 dstX,
387                 dstY 
388             );
389
390         },
391
392         "rect": function(parameters) {
393
394             var channelMask = parseInt(parameters[0]);
395             var layer = getLayer(parseInt(parameters[1]));
396             var x = parseInt(parameters[2]);
397             var y = parseInt(parameters[3]);
398             var w = parseInt(parameters[4]);
399             var h = parseInt(parameters[5]);
400             var r = parseInt(parameters[6]);
401             var g = parseInt(parameters[7]);
402             var b = parseInt(parameters[8]);
403             var a = parseInt(parameters[9]);
404
405             layer.setCompositeOperation(channelMask);
406
407             layer.drawRect(
408                 x, y, w, h,
409                 r, g, b, a
410             );
411
412         },
413
414         "clip": function(parameters) {
415
416             var layer = getLayer(parseInt(parameters[0]));
417             var x = parseInt(parameters[1]);
418             var y = parseInt(parameters[2]);
419             var w = parseInt(parameters[3]);
420             var h = parseInt(parameters[4]);
421
422             layer.clipRect(x, y, w, h);
423
424         },
425
426         "cursor": function(parameters) {
427
428             cursorHotspotX = parseInt(parameters[0]);
429             cursorHotspotY = parseInt(parameters[1]);
430             var srcL = getLayer(parseInt(parameters[2]));
431             var srcX = parseInt(parameters[3]);
432             var srcY = parseInt(parameters[4]);
433             var srcWidth = parseInt(parameters[5]);
434             var srcHeight = parseInt(parameters[6]);
435
436             // Reset cursor size
437             cursor.resize(srcWidth, srcHeight);
438
439             // Draw cursor to cursor layer
440             cursor.getLayer().copyRect(
441                 srcL,
442                 srcX,
443                 srcY,
444                 srcWidth, 
445                 srcHeight, 
446                 0,
447                 0 
448             );
449
450             // Update cursor position (hotspot may have changed)
451             moveCursor(cursorX, cursorY);
452
453         },
454
455         "sync": function(parameters) {
456
457             var timestamp = parameters[0];
458
459             // When all layers have finished rendering all instructions
460             // UP TO THIS POINT IN TIME, send sync response.
461
462             var layersToSync = 0;
463             function syncLayer() {
464
465                 layersToSync--;
466
467                 // Send sync response when layers are finished
468                 if (layersToSync == 0) {
469                     if (timestamp != currentTimestamp) {
470                         tunnel.sendMessage("sync", timestamp);
471                         currentTimestamp = timestamp;
472                     }
473                 }
474
475             }
476
477             // Count active, not-ready layers and install sync tracking hooks
478             for (var i=0; i<layers.length; i++) {
479
480                 var layer = layers[i].getLayer();
481                 if (layer && !layer.isReady()) {
482                     layersToSync++;
483                     layer.sync(syncLayer);
484                 }
485
486             }
487
488             // If all layers are ready, then we didn't install any hooks.
489             // Send sync message now,
490             if (layersToSync == 0) {
491                 if (timestamp != currentTimestamp) {
492                     tunnel.sendMessage("sync", timestamp);
493                     currentTimestamp = timestamp;
494                 }
495             }
496
497         }
498       
499     };
500
501
502     tunnel.oninstruction = function(opcode, parameters) {
503
504         var handler = instructionHandlers[opcode];
505         if (handler)
506             handler(parameters);
507
508     };
509
510
511     guac_client.disconnect = function() {
512
513         // Only attempt disconnection not disconnected.
514         if (currentState != STATE_DISCONNECTED
515                 && currentState != STATE_DISCONNECTING) {
516
517             setState(STATE_DISCONNECTING);
518
519             // Stop ping
520             if (pingInterval)
521                 window.clearInterval(pingInterval);
522
523             // Send disconnect message and disconnect
524             tunnel.sendMessage("disconnect");
525             tunnel.disconnect();
526             setState(STATE_DISCONNECTED);
527
528         }
529
530     };
531     
532     guac_client.connect = function(data) {
533
534         setState(STATE_CONNECTING);
535
536         try {
537             tunnel.connect(data);
538         }
539         catch (e) {
540             setState(STATE_IDLE);
541             throw e;
542         }
543
544         // Ping every 5 seconds (ensure connection alive)
545         pingInterval = window.setInterval(function() {
546             tunnel.sendMessage("sync", currentTimestamp);
547         }, 5000);
548
549         setState(STATE_WAITING);
550     };
551
552 };
553
554
555 /**
556  * Simple container for Guacamole.Layer, allowing layers to be easily
557  * repositioned and nested. This allows certain operations to be accelerated
558  * through DOM manipulation, rather than raster operations.
559  * 
560  * @constructor
561  * 
562  * @param {Number} width The width of the Layer, in pixels. The canvas element
563  *                       backing this Layer will be given this width.
564  *                       
565  * @param {Number} height The height of the Layer, in pixels. The canvas element
566  *                        backing this Layer will be given this height.
567  */
568 Guacamole.Client.LayerContainer = function(width, height) {
569
570     /**
571      * Reference to this LayerContainer.
572      * @private
573      */
574     var layer_container = this;
575
576     // Create layer with given size
577     var layer = new Guacamole.Layer(width, height);
578
579     // Set layer position
580     var canvas = layer.getCanvas();
581     canvas.style.position = "absolute";
582     canvas.style.left = "0px";
583     canvas.style.top = "0px";
584
585     // Create div with given size
586     var div = document.createElement("div");
587     div.appendChild(canvas);
588     div.style.width = width + "px";
589     div.style.height = height + "px";
590
591     /**
592      * Changes the size of this LayerContainer and the contained Layer to the
593      * given width and height.
594      * 
595      * @param {Number} width The new width to assign to this Layer.
596      * @param {Number} height The new height to assign to this Layer.
597      */
598     layer_container.resize = function(width, height) {
599
600         // Resize layer
601         layer.resize(width, height);
602
603         // Resize containing div
604         div.style.width = width + "px";
605         div.style.height = height + "px";
606
607     };
608   
609     /**
610      * Returns the Layer contained within this LayerContainer.
611      * @returns {Guacamole.Layer} The Layer contained within this LayerContainer.
612      */
613     layer_container.getLayer = function() {
614         return layer;
615     };
616
617     /**
618      * Returns the element containing the Layer within this LayerContainer.
619      * @returns {Element} The element containing the Layer within this LayerContainer.
620      */
621     layer_container.getElement = function() {
622         return div;
623     };
624
625 };