[gtk+/gtk-3-20] wayland: Implement gdk_utf8_to_string_target



commit 694c1661b835c617e528dc3645cbb3ba53ee3b85
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jun 27 11:57:21 2016 +0200

    wayland: Implement gdk_utf8_to_string_target
    
    The sanitize_utf8() function has been copied from X11 so both
    backends behave the same. This allows interaction with older clients
    (mainly through Xwayland, and the STRING selection target) that
    request non-utf8 text.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=768082

 gdk/wayland/gdkselection-wayland.c |   60 +++++++++++++++++++++++++++++++++++-
 1 files changed, 59 insertions(+), 1 deletions(-)
---
diff --git a/gdk/wayland/gdkselection-wayland.c b/gdk/wayland/gdkselection-wayland.c
index d6555d5..cd4aeba 100644
--- a/gdk/wayland/gdkselection-wayland.c
+++ b/gdk/wayland/gdkselection-wayland.c
@@ -1385,11 +1385,69 @@ _gdk_wayland_display_text_property_to_utf8_list (GdkDisplay    *display,
   return nitems;
 }
 
+/* This function has been copied straight from the x11 backend */
+static gchar *
+sanitize_utf8 (const gchar *src,
+               gboolean return_latin1)
+{
+  gint len = strlen (src);
+  GString *result = g_string_sized_new (len);
+  const gchar *p = src;
+
+  while (*p)
+    {
+      if (*p == '\r')
+        {
+          p++;
+          if (*p == '\n')
+            p++;
+
+          g_string_append_c (result, '\n');
+        }
+      else
+        {
+          gunichar ch = g_utf8_get_char (p);
+
+          if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
+            {
+              if (return_latin1)
+                {
+                  if (ch <= 0xff)
+                    g_string_append_c (result, ch);
+                  else
+                    g_string_append_printf (result,
+                                            ch < 0x10000 ? "\\u%04x" : "\\U%08x",
+                                            ch);
+                }
+              else
+                {
+                  char buf[7];
+                  gint buflen;
+
+                  buflen = g_unichar_to_utf8 (ch, buf);
+                  g_string_append_len (result, buf, buflen);
+                }
+            }
+
+          p = g_utf8_next_char (p);
+        }
+    }
+
+  return g_string_free (result, FALSE);
+}
+
 gchar *
 _gdk_wayland_display_utf8_to_string_target (GdkDisplay  *display,
                                             const gchar *str)
 {
-  return NULL;
+  /* This is mainly needed when interfacing with old clients through
+   * Xwayland, the STRING target could be used, and passed as-is
+   * by the compositor.
+   *
+   * There's already some handling of this atom (aka "mimetype" in
+   * this backend) in common code, so we end up in this vfunc.
+   */
+  return sanitize_utf8 (str, TRUE);
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]