[Spice-devel] [PATCH spice-html5 v2] cursor: Add support for mono cursor type

Tomáš Bohdálek tom.bohdalek at gmail.com
Thu Jun 1 11:01:10 UTC 2017


---
 cursor.js                | 18 ++++++++---
 png.js                   |  2 +-
 spice.html               |  1 +
 spice_auto.html          |  1 +
 thirdparty/monocursor.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 5 deletions(-)
 create mode 100644 thirdparty/monocursor.js

diff --git a/cursor.js b/cursor.js
index d3f4d55..1fb1ede 100644
--- a/cursor.js
+++ b/cursor.js
@@ -62,7 +62,8 @@ SpiceCursorConn.prototype.process_channel_message = function(msg)
         if (cursor_set.flags > 0)
             this.log_warn("FIXME: No support for cursor flags " + cursor_set.flags);
 
-        if (cursor_set.cursor.header.type != SPICE_CURSOR_TYPE_ALPHA)
+        if (cursor_set.cursor.header.type != SPICE_CURSOR_TYPE_ALPHA &&
+            cursor_set.cursor.header.type != SPICE_CURSOR_TYPE_MONO)
         {
             this.log_warn("FIXME: No support for cursor type " + cursor_set.cursor.header.type);
             return false;
@@ -117,9 +118,18 @@ SpiceCursorConn.prototype.process_channel_message = function(msg)
 
 SpiceCursorConn.prototype.set_cursor = function(cursor)
 {
-    var pngstr = create_rgba_png(cursor.header.height, cursor.header.width, cursor.data);
-    var curstr = 'url(data:image/png,' + pngstr + ') ' +
-        cursor.header.hot_spot_x + ' ' + cursor.header.hot_spot_y + ", default";
+    var pngstr = null;
+    if (cursor.header.type == SPICE_CURSOR_TYPE_ALPHA)
+    {
+        pngstr = create_rgba_png(cursor.header.height, cursor.header.width, cursor.data);
+    }
+    if (cursor.header.type == SPICE_CURSOR_TYPE_MONO)
+    {
+        pngstr = mono_cursor(cursor.header.height, cursor.header.width, cursor.data);
+    }
+
+    var curstr = 'url(' + pngstr + ') ' +
+    cursor.header.hot_spot_x + ' ' + cursor.header.hot_spot_y + ", default";
     var screen = document.getElementById(this.parent.screen_id);
     screen.style.cursor = 'auto';
     screen.style.cursor = curstr;
diff --git a/png.js b/png.js
index 6a26151..5b52ed5 100644
--- a/png.js
+++ b/png.js
@@ -252,5 +252,5 @@ function create_rgba_png(width, height, bytes)
     }
 
 
-    return "%89PNG%0D%0A%1A%0A" + str;
+    return "data:image/png,%89PNG%0D%0A%1A%0A" + str;
 }
diff --git a/spice.html b/spice.html
index d4c9962..8c43dcb 100644
--- a/spice.html
+++ b/spice.html
@@ -54,6 +54,7 @@
         <script src="thirdparty/prng4.js"></script>
         <script src="thirdparty/rng.js"></script>
         <script src="thirdparty/sha1.js"></script>
+        <script src="thirdparty/monocursor.js"></script>
         <script src="ticket.js"></script>
         <script src="resize.js"></script>
         <script src="filexfer.js"></script>
diff --git a/spice_auto.html b/spice_auto.html
index 2f04fc9..10f5508 100644
--- a/spice_auto.html
+++ b/spice_auto.html
@@ -54,6 +54,7 @@
         <script src="thirdparty/prng4.js"></script>
         <script src="thirdparty/rng.js"></script>
         <script src="thirdparty/sha1.js"></script>
+        <script src="thirdparty/monocursor.js"></script>
         <script src="ticket.js"></script>
         <script src="resize.js"></script>
         <script src="filexfer.js"></script>
diff --git a/thirdparty/monocursor.js b/thirdparty/monocursor.js
new file mode 100644
index 0000000..5df6da9
--- /dev/null
+++ b/thirdparty/monocursor.js
@@ -0,0 +1,79 @@
+/*  Downloaded 1/6/2017 from https://github.com/eyeos/spice-web-client/blob/master/lib/graphic.js by Tomáš Bohdálek.
+
+License reproduce here for completeness:
+
+The MIT License (MIT)
+Copyright (c) 2016 eyeOS S.L.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+function mono_cursor(height, width, data)
+{
+    var monoMask = [1, 2, 4, 8, 16, 32, 64, 128];
+    var bytes = new Uint8Array(data);
+    var length = bytes.length;
+    var half = length / 2;
+
+    var canvas = document.createElement("canvas");
+        canvas.setAttribute('width', width);
+        canvas.setAttribute('height', height);
+    var context = canvas.getContext("2d");
+    var result = context.createImageData(width, height);
+
+    var andMask = [];
+    var xorMask = [];
+
+    for (var i = 0; i < length; i++) {
+        var currentByte = bytes[i];
+        var bitsLeft = 8;
+
+        if (i >= half) {
+            while (bitsLeft--) {
+                var bit = (currentByte & monoMask[bitsLeft]) && true;
+                andMask.push(bit);
+            }
+        } else if (i < half) {
+            while (bitsLeft--) {
+                var bit = (currentByte & monoMask[bitsLeft]) && true;
+                xorMask.push(bit);
+            }
+        }
+    }
+
+    var pos = 0;
+    half = xorMask.length;
+
+    for (i = 0; i < half; i++) {
+        pos = i * 4;
+        if (!andMask[i] && !xorMask[i]) {
+            result.data[pos] = 0;
+            result.data[pos + 1] = 0;
+            result.data[pos + 2] = 0;
+            result.data[pos + 3] = 255;
+        } else if (!andMask[i] && xorMask[i]) {
+            result.data[pos] = 255;
+            result.data[pos + 1] = 255;
+            result.data[pos + 2] = 255;
+            result.data[pos + 3] = 0;
+        } else if (andMask[i] && !xorMask[i]) {
+            result.data[pos] = 255;
+            result.data[pos + 1] = 255;
+            result.data[pos + 2] = 255;
+            result.data[pos + 3] = 255;
+        } else if (andMask[i] && xorMask[i]) {
+            result.data[pos] = 0;
+            result.data[pos + 1] = 0;
+            result.data[pos + 2] = 0;
+            result.data[pos + 3] = 255;
+        }
+    }
+
+    context.putImageData(result, 0, 0);
+
+    return canvas.toDataURL("image/png");
+}
-- 
2.9.4



More information about the Spice-devel mailing list