[mutter/wip/carlosg/clipboard-manager: 2/10] x11: Add X11 MetaSelectionSource implementation



commit 82b7e33dd79d2bd184d1363c2ef5049a17fb0938
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Nov 19 17:16:37 2018 +0100

    x11: Add X11 MetaSelectionSource implementation
    
    This object represents the selection ownership from an X11 client. The
    list of supported targets is queried upfront, so its initialization is
    asynchronous. Requests to read contents from the selection will hand
    a MetaX11SelectionInputStream.

 src/meson.build                             |   2 +
 src/x11/meta-selection-source-x11-private.h |  44 +++++
 src/x11/meta-selection-source-x11.c         | 239 ++++++++++++++++++++++++++++
 src/x11/meta-x11-display-private.h          |   1 +
 4 files changed, 286 insertions(+)
---
diff --git a/src/meson.build b/src/meson.build
index f5326ec2f..110b43259 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -379,6 +379,8 @@ mutter_sources = [
   'x11/group-props.h',
   'x11/iconcache.c',
   'x11/iconcache.h',
+  'x11/meta-selection-source-x11.c',
+  'x11/meta-selection-source-x11-private.h',
   'x11/meta-startup-notification-x11.c',
   'x11/meta-startup-notification-x11.h',
   'x11/meta-x11-display.c',
diff --git a/src/x11/meta-selection-source-x11-private.h b/src/x11/meta-selection-source-x11-private.h
new file mode 100644
index 000000000..a03d64b95
--- /dev/null
+++ b/src/x11/meta-selection-source-x11-private.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2018 Red Hat
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+#ifndef META_SELECTION_SOURCE_X11_H
+#define META_SELECTION_SOURCE_X11_H
+
+#include "core/meta-selection-source.h"
+#include "x11/meta-x11-display-private.h"
+
+#define META_TYPE_SELECTION_SOURCE_X11 (meta_selection_source_x11_get_type ())
+G_DECLARE_FINAL_TYPE (MetaSelectionSourceX11,
+                      meta_selection_source_x11,
+                      META, SELECTION_SOURCE_X11,
+                      MetaSelectionSource)
+
+void                  meta_selection_source_x11_new_async  (MetaX11Display      *display,
+                                                            Window               owner,
+                                                            uint32_t             timestamp,
+                                                            Atom                 xselection,
+                                                            GCancellable        *cancellable,
+                                                            GAsyncReadyCallback  callback,
+                                                            gpointer             user_data);
+MetaSelectionSource * meta_selection_source_x11_new_finish (GAsyncResult  *result,
+                                                            GError       **error);
+
+#endif /* META_SELECTION_SOURCE_X11_H */
diff --git a/src/x11/meta-selection-source-x11.c b/src/x11/meta-selection-source-x11.c
new file mode 100644
index 000000000..9d3ff032e
--- /dev/null
+++ b/src/x11/meta-selection-source-x11.c
@@ -0,0 +1,239 @@
+/*
+ * Copyright (C) 2018 Red Hat
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+#include "config.h"
+
+#include <gdk/gdkx.h>
+
+#include "x11/meta-x11-selection-input-stream-private.h"
+#include "x11/meta-selection-source-x11-private.h"
+
+#define MAX_MIMETYPE_SIZE 4096
+
+struct _MetaSelectionSourceX11
+{
+  MetaSelectionSource parent_instance;
+  MetaX11Display *x11_display;
+  GList *mimetypes;
+  Window owner;
+  Atom xselection;
+  uint32_t timestamp;
+};
+
+G_DEFINE_TYPE (MetaSelectionSourceX11, meta_selection_source_x11,
+               META_TYPE_SELECTION_SOURCE)
+
+static void
+stream_new_cb (GObject      *source,
+               GAsyncResult *res,
+               GTask        *task)
+{
+  GInputStream *stream;
+  GError *error = NULL;
+
+  stream = meta_x11_selection_input_stream_new_finish (res, NULL, NULL, &error);
+
+  if (stream)
+    g_task_return_pointer (task, stream, g_object_unref);
+  else
+    g_task_return_error (task, error);
+
+  g_object_unref (task);
+}
+
+static void
+meta_selection_source_x11_read_async (MetaSelectionSource *source,
+                                      const gchar         *mimetype,
+                                      GCancellable        *cancellable,
+                                      GAsyncReadyCallback  callback,
+                                      gpointer             user_data)
+{
+  MetaSelectionSourceX11 *source_x11 = META_SELECTION_SOURCE_X11 (source);
+  GTask *task;
+
+  task = g_task_new (source, cancellable, callback, user_data);
+  g_task_set_source_tag (task, meta_selection_source_x11_read_async);
+
+  meta_x11_selection_input_stream_new_async (source_x11->x11_display,
+                                             source_x11->x11_display->selection.window,
+                                             gdk_x11_get_xatom_name (source_x11->xselection),
+                                             mimetype,
+                                             source_x11->timestamp,
+                                             G_PRIORITY_DEFAULT,
+                                             cancellable,
+                                             (GAsyncReadyCallback) stream_new_cb,
+                                             task);
+}
+
+static GInputStream *
+meta_selection_source_x11_read_finish (MetaSelectionSource  *source,
+                                       GAsyncResult         *result,
+                                       GError              **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, source), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
+                        meta_selection_source_x11_read_async, NULL);
+
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+static GList *
+meta_selection_source_x11_get_mimetypes (MetaSelectionSource  *source)
+{
+  MetaSelectionSourceX11 *source_x11 = META_SELECTION_SOURCE_X11 (source);
+
+  return g_list_copy_deep (source_x11->mimetypes, (GCopyFunc) g_strdup, NULL);
+}
+
+
+static void
+meta_selection_source_x11_class_init (MetaSelectionSourceX11Class *klass)
+{
+  MetaSelectionSourceClass *source_class = META_SELECTION_SOURCE_CLASS (klass);
+
+  source_class->read_async = meta_selection_source_x11_read_async;
+  source_class->read_finish = meta_selection_source_x11_read_finish;
+  source_class->get_mimetypes = meta_selection_source_x11_get_mimetypes;
+}
+
+static void
+meta_selection_source_x11_init (MetaSelectionSourceX11 *source)
+{
+}
+
+static GList *
+atoms_to_mimetypes (MetaX11Display *display,
+                    GBytes         *bytes)
+{
+  GList *mimetypes = NULL;
+  const Atom *atoms;
+  gsize size;
+  guint i, n_atoms;
+
+  atoms = g_bytes_get_data (bytes, &size);
+  n_atoms = size / sizeof (Atom);
+
+  for (i = 0; i < n_atoms; i++)
+    {
+      const gchar *mimetype;
+
+      mimetype = gdk_x11_get_xatom_name (atoms[i]);
+      mimetypes = g_list_prepend (mimetypes, g_strdup (mimetype));
+    }
+
+  return mimetypes;
+}
+
+static void
+read_mimetypes_cb (GInputStream *stream,
+                   GAsyncResult *res,
+                   GTask        *task)
+{
+  MetaSelectionSourceX11 *source_x11 = g_task_get_task_data (task);
+  GError *error = NULL;
+  GBytes *bytes;
+
+  bytes = g_input_stream_read_bytes_finish (stream, res, &error);
+  if (error)
+    {
+      g_task_return_error (task, error);
+      g_object_unref (task);
+      return;
+    }
+
+  source_x11->mimetypes = atoms_to_mimetypes (source_x11->x11_display, bytes);
+  g_bytes_unref (bytes);
+
+  g_task_return_pointer (task,
+                         g_object_ref (g_task_get_task_data (task)),
+                         g_object_unref);
+  g_object_unref (task);
+}
+
+static void
+get_mimetypes_cb (GObject      *source,
+                  GAsyncResult *res,
+                  GTask        *task)
+{
+  GInputStream *stream;
+  GError *error = NULL;
+
+  stream = meta_x11_selection_input_stream_new_finish (res, NULL, NULL, &error);
+  if (error)
+    {
+      g_task_return_error (task, error);
+      g_object_unref (task);
+      return;
+    }
+
+  g_input_stream_read_bytes_async (stream,
+                                   MAX_MIMETYPE_SIZE,
+                                   G_PRIORITY_DEFAULT,
+                                   g_task_get_cancellable (task),
+                                   (GAsyncReadyCallback) read_mimetypes_cb,
+                                   task);
+}
+
+void
+meta_selection_source_x11_new_async (MetaX11Display      *x11_display,
+                                     Window               owner,
+                                     uint32_t             timestamp,
+                                     Atom                 xselection,
+                                     GCancellable        *cancellable,
+                                     GAsyncReadyCallback  callback,
+                                     gpointer             user_data)
+{
+  MetaSelectionSourceX11 *source;
+  GTask *task;
+
+  source = g_object_new (META_TYPE_SELECTION_SOURCE_X11, NULL);
+  source->x11_display = x11_display;
+  source->owner = owner;
+  source->timestamp = timestamp;
+  source->xselection = xselection;
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  g_task_set_source_tag (task, meta_selection_source_x11_new_async);
+  g_task_set_task_data (task, source, g_object_unref);
+
+  meta_x11_selection_input_stream_new_async (x11_display,
+                                             x11_display->selection.window,
+                                             gdk_x11_get_xatom_name (xselection),
+                                             "TARGETS",
+                                             timestamp,
+                                             G_PRIORITY_DEFAULT,
+                                             cancellable,
+                                             (GAsyncReadyCallback) get_mimetypes_cb,
+                                             task);
+}
+
+MetaSelectionSource *
+meta_selection_source_x11_new_finish (GAsyncResult  *result,
+                                      GError       **error)
+{
+  GTask *task = G_TASK (result);
+
+  g_return_val_if_fail (g_task_is_valid (task, NULL), NULL);
+  g_return_val_if_fail (g_task_get_source_tag (task) ==
+                        meta_selection_source_x11_new_async, NULL);
+
+  return g_task_propagate_pointer (task, error);
+}
diff --git a/src/x11/meta-x11-display-private.h b/src/x11/meta-x11-display-private.h
index 118157dd2..eb5969f4a 100644
--- a/src/x11/meta-x11-display-private.h
+++ b/src/x11/meta-x11-display-private.h
@@ -124,6 +124,7 @@ struct _MetaX11Display
   MetaUI *ui;
 
   struct {
+    Window window;
     GList *input_streams;
     GList *output_streams;
   } selection;


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