[Spice-devel,spice-gtk,v2,1/2] clipboard: Add fixup_clipboard_text helper

Submitted by Christophe Fergeau on Aug. 31, 2016, 12:55 p.m.

Details

Message ID 1472648117-27941-1-git-send-email-cfergeau@redhat.com
State Accepted
Commit b0a2ff4f205e39f87e9b54d658a27d415ee3d055
Headers show
Series "Series without cover letter" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Christophe Fergeau Aug. 31, 2016, 12:55 p.m.
This makes clipboard_received_cb a bit shorter, and will be useful
in the next commit.
---
 src/spice-gtk-session.c | 69 +++++++++++++++++++++++++++++++------------------
 1 file changed, 44 insertions(+), 25 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
index 6cdae87..fb7630d 100644
--- a/src/spice-gtk-session.c
+++ b/src/spice-gtk-session.c
@@ -910,6 +910,49 @@  static gboolean check_clipboard_size_limits(SpiceGtkSession *session,
     return TRUE;
 }
 
+/* This will convert line endings if needed (between Windows/Unix conventions),
+ * and will make sure 'len' does not take into account any trailing \0 as this could
+ * cause some confusion guest side.
+ * The 'len' argument will be modified by this function to the length of the modified
+ * string
+ */
+static char *fixup_clipboard_text(SpiceGtkSession *self, const char *text, int *len)
+{
+    char *conv = NULL;
+    int new_len = *len;
+
+
+    if (spice_main_agent_test_capability(self->priv->main, VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
+        GError *err = NULL;
+
+        conv = spice_unix2dos(text, *len, &err);
+        if (err) {
+            g_warning("Failed to convert text line ending: %s", err->message);
+            g_clear_error(&err);
+            return NULL;
+        }
+
+        new_len = strlen(conv);
+    } else {
+        /* On Windows, with some versions of gtk+, GtkSelectionData::length
+         * will include the final '\0'. When a string with this trailing '\0'
+         * is pasted in some linux applications, it will be pasted as <NIL> or
+         * as an invisible character, which is unwanted. Ensure the length we
+         * send to the agent does not include any trailing '\0'
+         * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=734670
+         */
+        new_len = strlen(text);
+    }
+
+    if (!check_clipboard_size_limits(self, new_len)) {
+        g_free(conv);
+        return NULL;
+    }
+
+    *len = new_len;
+    return conv;
+}
+
 static void clipboard_received_cb(GtkClipboard *clipboard,
                                   GtkSelectionData *selection_data,
                                   gpointer user_data)
@@ -959,31 +1002,7 @@  static void clipboard_received_cb(GtkClipboard *clipboard,
 
     /* gtk+ internal utf8 newline is always LF, even on windows */
     if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
-        if (spice_main_agent_test_capability(s->main, VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
-            GError *err = NULL;
-
-            conv = spice_unix2dos((gchar*)data, len, &err);
-            if (err) {
-                g_warning("Failed to convert text line ending: %s", err->message);
-                g_clear_error(&err);
-                return;
-            }
-
-            len = strlen(conv);
-        } else {
-            /* On Windows, with some versions of gtk+, GtkSelectionData::length
-             * will include the final '\0'. When a string with this trailing '\0'
-             * is pasted in some linux applications, it will be pasted as <NIL> or
-             * as an invisible character, which is unwanted. Ensure the length we
-             * send to the agent does not include any trailing '\0'
-             * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=734670
-             */
-            len = strlen((const char *)data);
-        }
-        if (!check_clipboard_size_limits(self, len)) {
-            g_free(conv);
-            return;
-        }
+        conv = fixup_clipboard_text(self, (gchar *)data, &len);
     }
 
     spice_main_clipboard_selection_notify(s->main, selection, type,

Comments

Ack both patches

Pavel

On Wed, 2016-08-31 at 14:55 +0200, Christophe Fergeau wrote:
> This makes clipboard_received_cb a bit shorter, and will be useful
> in the next commit.
> ---
>  src/spice-gtk-session.c | 69 +++++++++++++++++++++++++++++++-----------------
> -
>  1 file changed, 44 insertions(+), 25 deletions(-)
> 
> diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
> index 6cdae87..fb7630d 100644
> --- a/src/spice-gtk-session.c
> +++ b/src/spice-gtk-session.c
> @@ -910,6 +910,49 @@ static gboolean
> check_clipboard_size_limits(SpiceGtkSession *session,
>      return TRUE;
>  }
>  
> +/* This will convert line endings if needed (between Windows/Unix
> conventions),
> + * and will make sure 'len' does not take into account any trailing \0 as
> this could
> + * cause some confusion guest side.
> + * The 'len' argument will be modified by this function to the length of the
> modified
> + * string
> + */
> +static char *fixup_clipboard_text(SpiceGtkSession *self, const char *text,
> int *len)
> +{
> +    char *conv = NULL;
> +    int new_len = *len;
> +
> +
> +    if (spice_main_agent_test_capability(self->priv->main,
> VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
> +        GError *err = NULL;
> +
> +        conv = spice_unix2dos(text, *len, &err);
> +        if (err) {
> +            g_warning("Failed to convert text line ending: %s", err-
> >message);
> +            g_clear_error(&err);
> +            return NULL;
> +        }
> +
> +        new_len = strlen(conv);
> +    } else {
> +        /* On Windows, with some versions of gtk+, GtkSelectionData::length
> +         * will include the final '\0'. When a string with this trailing '\0'
> +         * is pasted in some linux applications, it will be pasted as <NIL>
> or
> +         * as an invisible character, which is unwanted. Ensure the length we
> +         * send to the agent does not include any trailing '\0'
> +         * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=734670
> +         */
> +        new_len = strlen(text);
> +    }
> +
> +    if (!check_clipboard_size_limits(self, new_len)) {
> +        g_free(conv);
> +        return NULL;
> +    }
> +
> +    *len = new_len;
> +    return conv;
> +}
> +
>  static void clipboard_received_cb(GtkClipboard *clipboard,
>                                    GtkSelectionData *selection_data,
>                                    gpointer user_data)
> @@ -959,31 +1002,7 @@ static void clipboard_received_cb(GtkClipboard
> *clipboard,
>  
>      /* gtk+ internal utf8 newline is always LF, even on windows */
>      if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
> -        if (spice_main_agent_test_capability(s->main,
> VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
> -            GError *err = NULL;
> -
> -            conv = spice_unix2dos((gchar*)data, len, &err);
> -            if (err) {
> -                g_warning("Failed to convert text line ending: %s", err-
> >message);
> -                g_clear_error(&err);
> -                return;
> -            }
> -
> -            len = strlen(conv);
> -        } else {
> -            /* On Windows, with some versions of gtk+,
> GtkSelectionData::length
> -             * will include the final '\0'. When a string with this trailing
> '\0'
> -             * is pasted in some linux applications, it will be pasted as
> <NIL> or
> -             * as an invisible character, which is unwanted. Ensure the
> length we
> -             * send to the agent does not include any trailing '\0'
> -             * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=73
> 4670
> -             */
> -            len = strlen((const char *)data);
> -        }
> -        if (!check_clipboard_size_limits(self, len)) {
> -            g_free(conv);
> -            return;
> -        }
> +        conv = fixup_clipboard_text(self, (gchar *)data, &len);
>      }
>  
>      spice_main_clipboard_selection_notify(s->main, selection, type,
>
On 08/31/2016 03:55 PM, Christophe Fergeau wrote:
> This makes clipboard_received_cb a bit shorter, and will be useful
> in the next commit.
> ---
>  src/spice-gtk-session.c | 69 +++++++++++++++++++++++++++++++------------------
>  1 file changed, 44 insertions(+), 25 deletions(-)
>
> diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
> index 6cdae87..fb7630d 100644
> --- a/src/spice-gtk-session.c
> +++ b/src/spice-gtk-session.c
> @@ -910,6 +910,49 @@ static gboolean check_clipboard_size_limits(SpiceGtkSession *session,
>      return TRUE;
>  }
>
> +/* This will convert line endings if needed (between Windows/Unix conventions),
> + * and will make sure 'len' does not take into account any trailing \0 as this could
> + * cause some confusion guest side.
> + * The 'len' argument will be modified by this function to the length of the modified
> + * string
> + */
> +static char *fixup_clipboard_text(SpiceGtkSession *self, const char *text, int *len)
> +{
> +    char *conv = NULL;
> +    int new_len = *len;
> +
> +
> +    if (spice_main_agent_test_capability(self->priv->main, VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
> +        GError *err = NULL;
> +
> +        conv = spice_unix2dos(text, *len, &err);
> +        if (err) {
> +            g_warning("Failed to convert text line ending: %s", err->message);
> +            g_clear_error(&err);
> +            return NULL;

Note that this return NULL and the below Windows
case return NULL are treated the same after
this patch and differently in the original code.

> +        }
> +
> +        new_len = strlen(conv);
> +    } else {
> +        /* On Windows, with some versions of gtk+, GtkSelectionData::length
> +         * will include the final '\0'. When a string with this trailing '\0'
> +         * is pasted in some linux applications, it will be pasted as <NIL> or
> +         * as an invisible character, which is unwanted. Ensure the length we
> +         * send to the agent does not include any trailing '\0'
> +         * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=734670
> +         */
> +        new_len = strlen(text);
> +    }
> +
> +    if (!check_clipboard_size_limits(self, new_len)) {
> +        g_free(conv);
> +        return NULL;

same here.

> +    }
> +
> +    *len = new_len;
> +    return conv;
> +}
> +
>  static void clipboard_received_cb(GtkClipboard *clipboard,
>                                    GtkSelectionData *selection_data,
>                                    gpointer user_data)
> @@ -959,31 +1002,7 @@ static void clipboard_received_cb(GtkClipboard *clipboard,
>
>      /* gtk+ internal utf8 newline is always LF, even on windows */
>      if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
> -        if (spice_main_agent_test_capability(s->main, VD_AGENT_CAP_GUEST_LINEEND_CRLF)) {
> -            GError *err = NULL;
> -
> -            conv = spice_unix2dos((gchar*)data, len, &err);
> -            if (err) {
> -                g_warning("Failed to convert text line ending: %s", err->message);
> -                g_clear_error(&err);
> -                return;
> -            }
> -
> -            len = strlen(conv);
> -        } else {
> -            /* On Windows, with some versions of gtk+, GtkSelectionData::length
> -             * will include the final '\0'. When a string with this trailing '\0'
> -             * is pasted in some linux applications, it will be pasted as <NIL> or
> -             * as an invisible character, which is unwanted. Ensure the length we
> -             * send to the agent does not include any trailing '\0'
> -             * This is gtk+ bug https://bugzilla.gnome.org/show_bug.cgi?id=734670
> -             */
> -            len = strlen((const char *)data);
> -        }
> -        if (!check_clipboard_size_limits(self, len)) {
> -            g_free(conv);
> -            return;
> -        }
> +        conv = fixup_clipboard_text(self, (gchar *)data, &len);
>      }

If an error occur, conv=NULL but selection_notify is called.

>      spice_main_clipboard_selection_notify(s->main, selection, type,
>

Uri.
On Thu, Sep 01, 2016 at 12:23:12PM +0300, Uri Lublin wrote:
> If an error occur, conv=NULL but selection_notify is called.
> 

Arg yes, and I noticed your email after pushing /o\

Christophe