diff mbox

[6/6] vnc: Add a virtio-console-bus device to send / receive guest clipboard

Message ID 1254225888-17093-7-git-send-email-amit.shah@redhat.com
State Superseded
Headers show

Commit Message

Amit Shah Sept. 29, 2009, 12:04 p.m. UTC
This is a simple device on the virtio-console-bus that syncs with the
guest for clipboard activity.

A daemon is needed in the guest to send / receive the clipboard data.

This patch is just meant to show how to use the read/write api is
exposed for devices that ride on top of the virtio-console-bus.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 vnc.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)

Comments

Gerd Hoffmann Sept. 29, 2009, 6:13 p.m. UTC | #1
>   static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
>   {
> +    virtio_console_write(&virtcon_vnc->port, text, len);

Needs "if (virtcon_vnc)" ?

> +    if (virtcon_vnc) {
> +        virtio_console_open(&virtcon_vnc->port);
> +    }

Like it is done here?

There is no virtio_console_close() in this patch.  Does this work 
correctly after multiple connects + disconnects (with multiple vnc 
clients connected at the same time)?

I think when moving the buffering and host_connected state tracking into 
the port driver (patch 4/6) which is the only user of the facility you 
don't need these open/close calls at all.

cheers
   Gerd
Amit Shah Sept. 30, 2009, 4:50 a.m. UTC | #2
On (Tue) Sep 29 2009 [20:13:56], Gerd Hoffmann wrote:
>>   static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
>>   {
>> +    virtio_console_write(&virtcon_vnc->port, text, len);
>
> Needs "if (virtcon_vnc)" ?

Yes.

>> +    if (virtcon_vnc) {
>> +        virtio_console_open(&virtcon_vnc->port);
>> +    }
>
> Like it is done here?
>
> There is no virtio_console_close() in this patch.  Does this work  
> correctly after multiple connects + disconnects (with multiple vnc  
> clients connected at the same time)?

Yes, I actually don't propose this for inclusion. It's just a way to
show how the api works. I've only done some very basic testing with this
vnc patch (and found some bug in the vnc code that I reported about a
while back).

> I think when moving the buffering and host_connected state tracking into  
> the port driver (patch 4/6) which is the only user of the facility you  
> don't need these open/close calls at all.

True for vnc, but may not be for the future users.

		Amit
diff mbox

Patch

diff --git a/vnc.c b/vnc.c
index ff2d4a8..b6f9cce 100644
--- a/vnc.c
+++ b/vnc.c
@@ -29,6 +29,7 @@ 
 #include "qemu_socket.h"
 #include "qemu-timer.h"
 #include "acl.h"
+#include "hw/virtio-console.h"
 
 #define VNC_REFRESH_INTERVAL_BASE 30
 #define VNC_REFRESH_INTERVAL_INC  50
@@ -44,6 +45,11 @@ 
     } \
 }
 
+typedef struct VirtConVnc {
+    VirtConPort port;
+} VirtConVnc;
+
+static VirtConVnc *virtcon_vnc;
 
 static VncDisplay *vnc_display; /* needed for info vnc */
 static DisplayChangeListener *dcl;
@@ -659,6 +665,34 @@  static void send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
     }
 }
 
+static size_t vnc_clipboard_data_from_guest(VirtConPort *port,
+                                            const uint8_t *buf, size_t len)
+{
+    VncState *vs;
+    VncDisplay *vd;
+    DisplayState *ds;
+
+    if (!is_vnc_active())
+        return 0;
+
+    ds = vnc_display->ds;
+    vd = ds->opaque;
+
+    for (vs = vd->clients; vs; vs = vs->next) {
+        vnc_write_u8(vs, 3);  /* ServerCutText */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u32(vs, len);
+        while (len--) {
+            vnc_write_u8(vs, *(buf++));
+        }
+        vnc_flush(vs);
+    }
+    return len;
+}
+
+
 static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
 {
     /* send bitblit op to the vnc client */
@@ -1240,6 +1274,7 @@  uint32_t read_u32(uint8_t *data, size_t offset)
 
 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
 {
+    virtio_console_write(&virtcon_vnc->port, text, len);
 }
 
 static void check_pointer_type_change(VncState *vs, int absolute)
@@ -2197,6 +2232,10 @@  static void vnc_connect(VncDisplay *vd, int csock)
 
     vnc_init_timer(vd);
 
+    if (virtcon_vnc) {
+        virtio_console_open(&virtcon_vnc->port);
+    }
+
     /* vs might be free()ed here */
 }
 
@@ -2516,3 +2555,31 @@  int vnc_display_open(DisplayState *ds, const char *display)
     }
     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
 }
+
+static int vcon_vnc_initfn(VirtConDevice *dev)
+{
+    VirtConPort *port;
+    VirtConVnc *vnc;
+
+    port = DO_UPCAST(VirtConPort, dev, &dev->qdev);
+    vnc = DO_UPCAST(VirtConVnc, port, port);
+    virtcon_vnc = vnc;
+
+    port->name = qemu_strdup("org.qemu.clipboard");
+    /* We don't want to keep old data lingering if vnc is not connected */
+    port->flush_buffers = 1;
+    return 0;
+}
+
+static VirtConPortInfo virtcon_vnc_info = {
+    .qdev.name     = "virtconvnc",
+    .qdev.size     = sizeof(VirtConVnc),
+    .init          = vcon_vnc_initfn,
+    .have_data     = vnc_clipboard_data_from_guest,
+};
+
+static void virtcon_port_register(void)
+{
+    virtcon_port_qdev_register(&virtcon_vnc_info);
+}
+device_init(virtcon_port_register)