[Spice-devel] [PATCH] SPICE port basic implementation

Oliver Gutierrez ogutierrez at redhat.com
Mon Sep 26 11:02:54 UTC 2016


---
 enums.js        | 11 +++++++-
 main.js         |  5 +++-
 port.js         | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 spice.html      | 50 +++++++++++++++++++--------------
 spice_auto.html | 12 +++++++-
 spicemsg.js     | 24 ++++++++++++++--
 utils.js        |  7 +++++
 7 files changed, 168 insertions(+), 26 deletions(-)
 create mode 100644 port.js

diff --git a/enums.js b/enums.js
index 3ef36dc..b6e013c 100644
--- a/enums.js
+++ b/enums.js
@@ -166,6 +166,15 @@ var SPICE_MSG_PLAYBACK_VOLUME           = 105;
 var SPICE_MSG_PLAYBACK_MUTE             = 106;
 var SPICE_MSG_PLAYBACK_LATENCY          = 107;
 
+var SPICE_MSG_SPICEVMC_DATA             = 101;
+var SPICE_MSG_PORT_INIT                 = 201;
+var SPICE_MSG_PORT_EVENT                = 202;
+var SPICE_MSG_END_PORT                  = 203;
+
+var SPICE_MSGC_SPICEVMC_DATA            = 101;
+var SPICE_MSGC_PORT_EVENT               = 201;
+var SPICE_MSGC_END_PORT                 = 202;
+
 var SPICE_PLAYBACK_CAP_CELT_0_5_1       = 0;
 var SPICE_PLAYBACK_CAP_VOLUME           = 1;
 var SPICE_PLAYBACK_CAP_LATENCY          = 2;
@@ -264,7 +273,7 @@ var SPICE_MOUSE_BUTTON_MASK_LEFT = (1 << 0),
     SPICE_MOUSE_BUTTON_MASK_MIDDLE = (1 << 1),
     SPICE_MOUSE_BUTTON_MASK_RIGHT = (1 << 2),
     SPICE_MOUSE_BUTTON_MASK_MASK = 0x7;
-    
+
 var SPICE_MOUSE_BUTTON_INVALID  = 0;
 var SPICE_MOUSE_BUTTON_LEFT     = 1;
 var SPICE_MOUSE_BUTTON_MIDDLE   = 2;
diff --git a/main.js b/main.js
index afe69bf..2d8a1ff 100644
--- a/main.js
+++ b/main.js
@@ -22,7 +22,7 @@
 **  SpiceMainConn
 **      This is the master Javascript class for establishing and
 **  managing a connection to a Spice Server.
-**  
+**
 **      Invocation:  You must pass an object with properties as follows:
 **          uri         (required)  Uri of a WebSocket listener that is
 **                                  connected to a spice server.
@@ -59,6 +59,7 @@ function SpiceMainConn()
     this.file_xfer_tasks = {};
     this.file_xfer_task_id = 0;
     this.file_xfer_read_queue = [];
+    this.ports = [];
 }
 
 SpiceMainConn.prototype = Object.create(SpiceConn.prototype);
@@ -154,6 +155,8 @@ SpiceMainConn.prototype.process_channel_message = function(msg)
                 this.cursor = new SpiceCursorConn(conn);
             else if (chans.channels[i].type == SPICE_CHANNEL_PLAYBACK)
                 this.cursor = new SpicePlaybackConn(conn);
+            else if (chans.channels[i].type == SPICE_CHANNEL_PORT)
+                this.ports.push(new SpicePortConn(conn));
             else
             {
                 if (! ("extra_channels" in this))
diff --git a/port.js b/port.js
new file mode 100644
index 0000000..ee22073
--- /dev/null
+++ b/port.js
@@ -0,0 +1,85 @@
+"use strict";
+/*
+   Copyright (C) 2016 by Oliver Gutierrez <ogutsua at gmail.com>
+                         Miroslav Chodil <mchodil at redhat.com>
+
+   This file is part of spice-html5.
+
+   spice-html5 is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   spice-html5 is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with spice-html5.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*----------------------------------------------------------------------------
+**  SpicePortConn
+**      Drive the Spice Port Channel
+**--------------------------------------------------------------------------*/
+function SpicePortConn()
+{
+    DEBUG > 0 && console.log('SPICE port: created SPICE port channel. Args:', arguments);
+    SpiceConn.apply(this, arguments);
+    this.port_name = null;
+}
+
+SpicePortConn.prototype = Object.create(SpiceConn.prototype);
+
+SpicePortConn.prototype.process_channel_message = function(msg)
+{
+    if (msg.type == SPICE_MSG_PORT_INIT)
+    {
+        if (this.port_name === null)
+        {
+            var m = new SpiceMsgPortInit(msg.data);
+            this.portName = arraybuffer_to_str(new Uint8Array(m.name));
+            this.portOpened = m.opened
+            DEBUG > 0 && console.log('SPICE port: Port', this.portName, 'initialized');
+            return true;
+        }
+
+        DEBUG > 0 && console.log('SPICE port: Port', this.port_name, 'is already initialized.');
+    }
+    else if (msg.type == SPICE_MSG_PORT_EVENT)
+    {
+        DEBUG > 0 && console.log('SPICE port: Port event received for', this.portName, msg);
+        var event = new CustomEvent('spice-port-event', {
+            detail: {
+                channel: this,
+                spiceEvent: new Uint8Array(msg.data)
+            },
+            bubbles: true,
+            cancelable: true
+        });
+
+        window.dispatchEvent(event);
+        return true;
+    }
+    else if (msg.type == SPICE_MSG_SPICEVMC_DATA)
+    {
+        DEBUG > 0 && console.log('SPICE port: Data received in port', this.portName, msg);
+        var event = new CustomEvent('spice-port-data', {
+            detail: {
+                channel: this,
+                data: msg.data
+            },
+            bubbles: true,
+            cancelable: true
+        });
+        window.dispatchEvent(event);
+        return true;
+    }
+    else
+    {
+        DEBUG > 0 && console.log('SPICE port: SPICE message type not recognized:', msg)
+    }
+
+    return false;
+};
diff --git a/spice.html b/spice.html
index f2f9ed0..f736c59 100644
--- a/spice.html
+++ b/spice.html
@@ -28,26 +28,27 @@
     <head>
 
         <title>Spice Javascript client</title>
-        <script src="spicearraybuffer.js"></script> 
-        <script src="enums.js"></script> 
-        <script src="atKeynames.js"></script> 
-        <script src="utils.js"></script> 
-        <script src="png.js"></script> 
-        <script src="lz.js"></script> 
-        <script src="quic.js"></script> 
-        <script src="bitmap.js"></script> 
-        <script src="spicedataview.js"></script> 
-        <script src="spicetype.js"></script> 
-        <script src="spicemsg.js"></script> 
-        <script src="wire.js"></script> 
-        <script src="spiceconn.js"></script> 
-        <script src="display.js"></script> 
-        <script src="main.js"></script> 
-        <script src="inputs.js"></script> 
+        <script src="spicearraybuffer.js"></script>
+        <script src="enums.js"></script>
+        <script src="atKeynames.js"></script>
+        <script src="utils.js"></script>
+        <script src="png.js"></script>
+        <script src="lz.js"></script>
+        <script src="quic.js"></script>
+        <script src="bitmap.js"></script>
+        <script src="spicedataview.js"></script>
+        <script src="spicetype.js"></script>
+        <script src="spicemsg.js"></script>
+        <script src="wire.js"></script>
+        <script src="spiceconn.js"></script>
+        <script src="display.js"></script>
+        <script src="port.js"></script>
+        <script src="main.js"></script>
+        <script src="inputs.js"></script>
         <script src="webm.js"></script>
         <script src="playback.js"></script>
         <script src="simulatecursor.js"></script>
-        <script src="cursor.js"></script> 
+        <script src="cursor.js"></script>
         <script src="thirdparty/jsbn.js"></script>
         <script src="thirdparty/rsa.js"></script>
         <script src="thirdparty/prng4.js"></script>
@@ -71,8 +72,8 @@
             {
                 var host, port, password, scheme = "ws://", uri;
 
-                host = document.getElementById("host").value; 
-                port = document.getElementById("port").value; 
+                host = document.getElementById("host").value;
+                port = document.getElementById("port").value;
                 password = document.getElementById("password").value;
 
 
@@ -92,7 +93,7 @@
 
                 try
                 {
-                    sc = new SpiceMainConn({uri: uri, screen_id: "spice-screen", dump_id: "debug-div", 
+                    sc = new SpiceMainConn({uri: uri, screen_id: "spice-screen", dump_id: "debug-div",
                                 message_id: "message-div", password: password, onerror: spice_error, onagent: agent_connected });
                 }
                 catch (e)
@@ -142,6 +143,15 @@
                 }
             }
 
+            window.addEventListener('spice-port-data', function(event) {
+                var msg_text = arraybuffer_to_str(new Uint8Array(event.detail.data));
+                console.log('SPICE port', event.detail.channel.portName, 'message text:', msg_text);
+            });
+
+            window.addEventListener('spice-port-event', function(event) {
+                console.log('SPICE port', event.detail.channel.portName, 'event data:', event.detail.spiceEvent);
+            });
+
         </script>
 
     </head>
diff --git a/spice_auto.html b/spice_auto.html
index 9aae118..304655c 100644
--- a/spice_auto.html
+++ b/spice_auto.html
@@ -28,7 +28,7 @@
     <head>
 
         <title>Spice Javascript client</title>
-        <script src="spicearraybuffer.js"></script> 
+        <script src="spicearraybuffer.js"></script>
         <script src="enums.js"></script>
         <script src="atKeynames.js"></script>
         <script src="utils.js"></script>
@@ -42,6 +42,7 @@
         <script src="wire.js"></script>
         <script src="spiceconn.js"></script>
         <script src="display.js"></script>
+        <script src="port.js"></script>
         <script src="main.js"></script>
         <script src="inputs.js"></script>
         <script src="webm.js"></script>
@@ -182,6 +183,15 @@
                 }
             }
 
+            window.addEventListener('spice-port-data', function(event) {
+                var msg_text = arraybuffer_to_str(new Uint8Array(event.detail.data));
+                console.log('SPICE port', event.detail.channel.portName, 'message text:', msg_text);
+            });
+
+            window.addEventListener('spice-port-event', function(event) {
+                console.log('SPICE port', event.detail.channel.portName, 'event data:', event.detail.spiceEvent);
+            });
+
             connect();
         </script>
 
diff --git a/spicemsg.js b/spicemsg.js
index db6625a..3619996 100644
--- a/spicemsg.js
+++ b/spicemsg.js
@@ -21,7 +21,7 @@
 /*----------------------------------------------------------------------------
 **  Spice messages
 **      This file contains classes for passing messages to and from
-**  a spice server.  This file should arguably be generated from 
+**  a spice server.  This file should arguably be generated from
 **  spice.proto, but it was instead put together by hand.
 **--------------------------------------------------------------------------*/
 function SpiceLinkHeader(a, at)
@@ -63,7 +63,7 @@ SpiceLinkHeader.prototype =
         dv.setUint32(at, this.size, true); at += 4;
     },
     buffer_size: function()
-    { 
+    {
         return 16;
     },
 }
@@ -938,7 +938,7 @@ function SpiceMsgcMousePosition(sc, e)
         this.x = e.clientX - sc.display.surfaces[sc.display.primary_surface].canvas.offsetLeft + scrollLeft;
         this.y = e.clientY - sc.display.surfaces[sc.display.primary_surface].canvas.offsetTop + scrollTop;
         sc.mousex = this.x;
-        sc.mousey = this.y; 
+        sc.mousey = this.y;
     }
     else
     {
@@ -1278,3 +1278,21 @@ SpiceMsgDisplayInvalList.prototype =
         }
     },
 }
+
+function SpiceMsgPortInit(a, at)
+{
+    this.from_buffer(a,at);
+};
+
+SpiceMsgPortInit.prototype =
+{
+    from_buffer: function (a, at)
+    {
+        at = at || 0;
+        var dv = new SpiceDataView(a);
+        var namesize = dv.getUint32(at, true); at += 4;
+        var offset = dv.getUint32(at, true); at += 4;
+        this.opened = dv.getUint8(at, true); at += 1;
+        this.name = a.slice(offset, offset + namesize - 1);
+    }
+}
diff --git a/utils.js b/utils.js
index 9093a24..a22d0ae 100644
--- a/utils.js
+++ b/utils.js
@@ -100,6 +100,13 @@ function hexdump_buffer(a)
 }
 
 /*----------------------------------------------------------------------------
+**  Convert arraybuffer to string
+**--------------------------------------------------------------------------*/
+function arraybuffer_to_str(buf) {
+  return String.fromCharCode.apply(null, new Uint16Array(buf));
+}
+
+/*----------------------------------------------------------------------------
 ** Converting keycodes to AT scancodes is very hard.
 ** luckly there are some resources on the web and in the Xorg driver that help
 ** us figure out what browser dependent keycodes match to what scancodes.
-- 
2.9.3



More information about the Spice-devel mailing list