[mutter/wip/carlosg/selection-bugfixes] x11: Map x11 (UTF8_)STRING requests to text/plain mimetypes



commit d9b1bfe5ab3fc175a5e29ec6ae82176aa8afa5dd
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Jan 13 14:18:00 2020 +0100

    x11: Map x11 (UTF8_)STRING requests to text/plain mimetypes
    
    Make the x11 selection proxy map UTF8_STRING and STRING to proper
    mimetypes, as the selection source (wayland or memory) might not
    offer those for backwards compatibility.
    
    Closes: https://gitlab.gnome.org/GNOME/gnome-shell/issues/1355
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/1005

 src/x11/meta-x11-selection.c | 59 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 9 deletions(-)
---
diff --git a/src/x11/meta-x11-selection.c b/src/x11/meta-x11-selection.c
index 5332e9d54..17b0c1e29 100644
--- a/src/x11/meta-x11-selection.c
+++ b/src/x11/meta-x11-selection.c
@@ -27,6 +27,9 @@
 #include "x11/meta-x11-selection-output-stream-private.h"
 #include "x11/meta-x11-selection-private.h"
 
+#define UTF8_STRING_MIMETYPE "text/plain;charset=utf-8"
+#define STRING_MIMETYPE "text/plain"
+
 static gboolean
 atom_to_selection_type (Display           *xdisplay,
                         Atom               selection,
@@ -142,6 +145,45 @@ transfer_cb (MetaSelection *selection,
   g_object_unref (output);
 }
 
+static char *
+meta_x11_selection_find_target (MetaX11Display     *x11_display,
+                                MetaSelection      *selection,
+                                MetaSelectionType   selection_type,
+                                Atom                selection_atom)
+{
+  GList* mimetypes = NULL;
+  const gchar *atom_name;
+  char *retval;
+
+  mimetypes = meta_selection_get_mimetypes (selection, selection_type);
+  atom_name = gdk_x11_get_xatom_name (selection_atom);
+
+  if (g_list_find_custom (mimetypes, atom_name, (GCompareFunc) g_strcmp0))
+    {
+      retval = g_strdup (atom_name);
+    }
+  else if (strcmp (atom_name, "UTF8_STRING") == 0 &&
+           g_list_find_custom (mimetypes, UTF8_STRING_MIMETYPE,
+                               (GCompareFunc) g_strcmp0))
+    {
+      retval = g_strdup (UTF8_STRING_MIMETYPE);
+    }
+  else if (strcmp (atom_name, "STRING") == 0 &&
+           g_list_find_custom (mimetypes, STRING_MIMETYPE,
+                               (GCompareFunc) g_strcmp0))
+    {
+      retval = g_strdup (STRING_MIMETYPE);
+    }
+  else
+    {
+      retval = NULL;
+    }
+
+  g_list_free_full (mimetypes, g_free);
+
+  return retval;
+}
+
 static gboolean
 meta_x11_selection_handle_selection_request (MetaX11Display *x11_display,
                                              XEvent         *xevent)
@@ -198,15 +240,12 @@ meta_x11_selection_handle_selection_request (MetaX11Display *x11_display,
     }
   else
     {
-      gboolean has_target;
+      g_autofree char *target = NULL;
 
-      mimetypes = meta_selection_get_mimetypes (selection, selection_type);
-      has_target = g_list_find_custom (mimetypes,
-                                       gdk_x11_get_xatom_name (event->target),
-                                       (GCompareFunc) g_strcmp0) != NULL;
-      g_list_free_full (mimetypes, g_free);
+      target = meta_x11_selection_find_target (x11_display, selection,
+                                               selection_type, event->target);
 
-      if (has_target)
+      if (target != NULL)
         {
           output = meta_x11_selection_output_stream_new (x11_display,
                                                          event->requestor,
@@ -218,7 +257,7 @@ meta_x11_selection_handle_selection_request (MetaX11Display *x11_display,
 
           meta_selection_transfer_async (selection,
                                          selection_type,
-                                         gdk_x11_get_xatom_name (event->target),
+                                         target,
                                          -1,
                                          output,
                                          NULL,
@@ -227,7 +266,9 @@ meta_x11_selection_handle_selection_request (MetaX11Display *x11_display,
           return TRUE;
         }
       else
-        send_selection_notify (event, FALSE);
+        {
+          send_selection_notify (event, FALSE);
+        }
     }
 
   return FALSE;


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