Fix cursor - copyRect() has been renamed to copy().
[guacamole-common-js.git] / src / main / resources / guacamole.js
index e126348..d4a52ba 100644 (file)
@@ -20,6 +20,7 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
+ * Matt Hortman
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -76,8 +77,21 @@ Guacamole.Client = function(tunnel) {
     // Create default layer
     var default_layer_container = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
 
+    // Position default layer
+    var default_layer_container_element = default_layer_container.getElement();
+    default_layer_container_element.style.position = "absolute";
+    default_layer_container_element.style.left = "0px";
+    default_layer_container_element.style.top  = "0px";
+
     // Create cursor layer
-    var cursor = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
+    var cursor = new Guacamole.Client.LayerContainer(0, 0);
+    cursor.getLayer().setChannelMask(Guacamole.Layer.SRC);
+
+    // Position cursor layer
+    var cursor_element = cursor.getElement();
+    cursor_element.style.position = "absolute";
+    cursor_element.style.left = "0px";
+    cursor_element.style.top  = "0px";
 
     // Add default layer and cursor to display
     display.appendChild(default_layer_container.getElement());
@@ -107,36 +121,23 @@ Guacamole.Client = function(tunnel) {
             || currentState == STATE_WAITING;
     }
 
-    var cursorImage = null;
     var cursorHotspotX = 0;
     var cursorHotspotY = 0;
 
-    var cursorRectX = 0;
-    var cursorRectY = 0;
-    var cursorRectW = 0;
-    var cursorRectH = 0;
-
-    var cursorHidden = 0;
+    var cursorX = 0;
+    var cursorY = 0;
 
-    function redrawCursor(x, y) {
-
-        // Hide hardware cursor
-        if (cursorHidden == 0) {
-            display.className += " guac-hide-cursor";
-            cursorHidden = 1;
-        }
+    function moveCursor(x, y) {
 
-        // Erase old cursor
-        cursor.clearRect(cursorRectX, cursorRectY, cursorRectW, cursorRectH);
+        var element = cursor.getElement();
 
         // Update rect
-        cursorRectX = x - cursorHotspotX;
-        cursorRectY = y - cursorHotspotY;
-        cursorRectW = cursorImage.width;
-        cursorRectH = cursorImage.height;
+        element.style.left = (x - cursorHotspotX) + "px";
+        element.style.top  = (y - cursorHotspotY) + "px";
 
-        // Draw new cursor
-        cursor.drawImage(cursorRectX, cursorRectY, cursorImage);
+        // Update stored position
+        cursorX = x;
+        cursorY = y;
 
     }
 
@@ -158,13 +159,11 @@ Guacamole.Client = function(tunnel) {
         if (!isConnected())
             return;
 
-        // Draw client-side cursor
-        if (cursorImage != null) {
-            redrawCursor(
-                mouseState.x,
-                mouseState.y
-            );
-        }
+        // Update client-side cursor
+        moveCursor(
+            mouseState.x,
+            mouseState.y
+        );
 
         // Build mask
         var buttonMask = 0;
@@ -283,10 +282,6 @@ Guacamole.Client = function(tunnel) {
                     display.style.width = displayWidth + "px";
                     display.style.height = displayHeight + "px";
 
-                    // Set cursor layer width/height
-                    if (cursor != null)
-                        cursor.resize(displayWidth, displayHeight);
-
                 }
 
             } // end if layer (not buffer)
@@ -321,6 +316,32 @@ Guacamole.Client = function(tunnel) {
 
         },
 
+        "dispose": function(parameters) {
+            
+            var layer_index = parseInt(parameters[0]);
+
+            // If visible layer, remove from parent
+            if (layer_index > 0) {
+
+                // Get container element
+                var layer_container = getLayerContainer(layer_index).getElement();
+
+                // Remove from parent
+                layer_container.parentNode.removeChild(layer_container);
+
+                // Delete reference
+                delete layers[layer_index];
+
+            }
+
+            // If buffer, just delete reference
+            else if (layer_index < 0)
+                delete buffers[-1 - layer_index];
+
+            // Attempting to dispose the root layer currently has no effect.
+
+        },
+
         "png": function(parameters) {
 
             var channelMask = parseInt(parameters[0]);
@@ -357,7 +378,7 @@ Guacamole.Client = function(tunnel) {
 
             dstL.setChannelMask(channelMask);
 
-            dstL.copyRect(
+            dstL.copy(
                 srcL,
                 srcX,
                 srcY,
@@ -369,29 +390,32 @@ Guacamole.Client = function(tunnel) {
 
         },
 
-        "rect": function(parameters) {
-
-            var channelMask = parseInt(parameters[0]);
-            var layer = getLayer(parseInt(parameters[1]));
-            var x = parseInt(parameters[2]);
-            var y = parseInt(parameters[3]);
-            var w = parseInt(parameters[4]);
-            var h = parseInt(parameters[5]);
-            var r = parseInt(parameters[6]);
-            var g = parseInt(parameters[7]);
-            var b = parseInt(parameters[8]);
-            var a = parseInt(parameters[9]);
+        "transfer": function(parameters) {
 
-            layer.setChannelMask(channelMask);
+            var srcL = getLayer(parseInt(parameters[0]));
+            var srcX = parseInt(parameters[1]);
+            var srcY = parseInt(parameters[2]);
+            var srcWidth = parseInt(parameters[3]);
+            var srcHeight = parseInt(parameters[4]);
+            var transferFunction = Guacamole.Client.DefaultTransferFunction[parameters[5]];
+            var dstL = getLayer(parseInt(parameters[6]));
+            var dstX = parseInt(parameters[7]);
+            var dstY = parseInt(parameters[8]);
 
-            layer.drawRect(
-                x, y, w, h,
-                r, g, b, a
+            dstL.transfer(
+                srcL,
+                srcX,
+                srcY,
+                srcWidth, 
+                srcHeight, 
+                dstX,
+                dstY,
+                transferFunction
             );
 
         },
 
-        "clip": function(parameters) {
+        "rect": function(parameters) {
 
             var layer = getLayer(parseInt(parameters[0]));
             var x = parseInt(parameters[1]);
@@ -399,30 +423,59 @@ Guacamole.Client = function(tunnel) {
             var w = parseInt(parameters[3]);
             var h = parseInt(parameters[4]);
 
-            layer.clipRect(x, y, w, h);
+            layer.rect(x, y, w, h);
 
         },
 
-        "cursor": function(parameters) {
+        "clip": function(parameters) {
+
+            var layer = getLayer(parseInt(parameters[0]));
+
+            layer.clip();
+
+        },
+
+        "cfill": function(parameters) {
+
+            var channelMask = parseInt(parameters[0]);
+            var layer = getLayer(parseInt(parameters[1]));
+            var r = parseInt(parameters[2]);
+            var g = parseInt(parameters[3]);
+            var b = parseInt(parameters[4]);
+            var a = parseInt(parameters[5]);
+
+            layer.setChannelMask(channelMask);
 
-            var x = parseInt(parameters[0]);
-            var y = parseInt(parameters[1]);
-            var data = parameters[2];
+            layer.fillColor(r, g, b, a);
 
-            // Start cursor image load
-            var image = new Image();
-            image.onload = function() {
-                cursorImage = image;
+        },
 
-                var cursorX = cursorRectX + cursorHotspotX;
-                var cursorY = cursorRectY + cursorHotspotY;
+        "cursor": function(parameters) {
 
-                cursorHotspotX = x;
-                cursorHotspotY = y;
+            cursorHotspotX = parseInt(parameters[0]);
+            cursorHotspotY = parseInt(parameters[1]);
+            var srcL = getLayer(parseInt(parameters[2]));
+            var srcX = parseInt(parameters[3]);
+            var srcY = parseInt(parameters[4]);
+            var srcWidth = parseInt(parameters[5]);
+            var srcHeight = parseInt(parameters[6]);
 
-                redrawCursor(cursorX, cursorY);
-            };
-            image.src = "data:image/png;base64," + data
+            // Reset cursor size
+            cursor.resize(srcWidth, srcHeight);
+
+            // Draw cursor to cursor layer
+            cursor.getLayer().copy(
+                srcL,
+                srcX,
+                srcY,
+                srcWidth, 
+                srcHeight, 
+                0,
+                0 
+            );
+
+            // Update cursor position (hotspot may have changed)
+            moveCursor(cursorX, cursorY);
 
         },
 
@@ -596,4 +649,120 @@ Guacamole.Client.LayerContainer = function(width, height) {
         return div;
     };
 
-};
\ No newline at end of file
+};
+
+/**
+ * Map of all Guacamole binary raster operations to transfer functions.
+ * @private
+ */
+Guacamole.Client.DefaultTransferFunction = {
+
+    /* BLACK */
+    0x0: function (src, dst) {
+        dst.red = dst.green = dst.blue = 0x00;
+    },
+
+    /* WHITE */
+    0xF: function (src, dst) {
+        dst.red = dst.green = dst.blue = 0xFF;
+    },
+
+    /* SRC */
+    0x3: function (src, dst) {
+        dst.red   = src.red;
+        dst.green = src.green;
+        dst.blue  = src.blue;
+        dst.alpha = src.alpha;
+    },
+
+    /* DEST (no-op) */
+    0x5: function (src, dst) {
+        // Do nothing
+    },
+
+    /* Invert SRC */
+    0xC: function (src, dst) {
+        dst.red   = 0xFF & ~src.red;
+        dst.green = 0xFF & ~src.green;
+        dst.blue  = 0xFF & ~src.blue;
+        dst.alpha =  src.alpha;
+    },
+    
+    /* Invert DEST */
+    0xA: function (src, dst) {
+        dst.red   = 0xFF & ~dst.red;
+        dst.green = 0xFF & ~dst.green;
+        dst.blue  = 0xFF & ~dst.blue;
+    },
+
+    /* AND */
+    0x1: function (src, dst) {
+        dst.red   =  ( src.red   &  dst.red);
+        dst.green =  ( src.green &  dst.green);
+        dst.blue  =  ( src.blue  &  dst.blue);
+    },
+
+    /* NAND */
+    0xE: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   &  dst.red);
+        dst.green = 0xFF & ~( src.green &  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  &  dst.blue);
+    },
+
+    /* OR */
+    0x7: function (src, dst) {
+        dst.red   =  ( src.red   |  dst.red);
+        dst.green =  ( src.green |  dst.green);
+        dst.blue  =  ( src.blue  |  dst.blue);
+    },
+
+    /* NOR */
+    0x8: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   |  dst.red);
+        dst.green = 0xFF & ~( src.green |  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  |  dst.blue);
+    },
+
+    /* XOR */
+    0x6: function (src, dst) {
+        dst.red   =  ( src.red   ^  dst.red);
+        dst.green =  ( src.green ^  dst.green);
+        dst.blue  =  ( src.blue  ^  dst.blue);
+    },
+
+    /* XNOR */
+    0x9: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   ^  dst.red);
+        dst.green = 0xFF & ~( src.green ^  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  ^  dst.blue);
+    },
+
+    /* AND inverted source */
+    0x4: function (src, dst) {
+        dst.red   =  0xFF & (~src.red   &  dst.red);
+        dst.green =  0xFF & (~src.green &  dst.green);
+        dst.blue  =  0xFF & (~src.blue  &  dst.blue);
+    },
+
+    /* OR inverted source */
+    0xD: function (src, dst) {
+        dst.red   =  0xFF & (~src.red   |  dst.red);
+        dst.green =  0xFF & (~src.green |  dst.green);
+        dst.blue  =  0xFF & (~src.blue  |  dst.blue);
+    },
+
+    /* AND inverted destination */
+    0x2: function (src, dst) {
+        dst.red   =  0xFF & ( src.red   & ~dst.red);
+        dst.green =  0xFF & ( src.green & ~dst.green);
+        dst.blue  =  0xFF & ( src.blue  & ~dst.blue);
+    },
+
+    /* OR inverted destination */
+    0xB: function (src, dst) {
+        dst.red   =  0xFF & ( src.red   | ~dst.red);
+        dst.green =  0xFF & ( src.green | ~dst.green);
+        dst.blue  =  0xFF & ( src.blue  | ~dst.blue);
+    }
+
+};