[Xfce4-commits] <xfce4-settings:jeromeg/clipboard-daemon> Use the Xfce prefix and make the indentation consistent.

Jérôme Guelfucci noreply at xfce.org
Thu Nov 19 00:48:08 CET 2009


Updating branch refs/heads/jeromeg/clipboard-daemon
         to 9a1098f6346ccfcd92513554c47011a707fce469 (commit)
       from 82e945d7df5b1491b8e6b362189911fad1dbf284 (commit)

commit 9a1098f6346ccfcd92513554c47011a707fce469
Author: Jérôme Guelfucci <jeromeg at xfce.org>
Date:   Wed Nov 18 00:36:16 2009 +0100

    Use the Xfce prefix and make the indentation consistent.

 xfce4-settings-helper/Makefile.am              |    6 +-
 xfce4-settings-helper/gsd-clipboard-manager.c  |  347 ----------------------
 xfce4-settings-helper/gsd-clipboard-manager.h  |   57 ----
 xfce4-settings-helper/main.c                   |   38 ++--
 xfce4-settings-helper/xfce-clipboard-manager.c |  366 ++++++++++++++++++++++++
 xfce4-settings-helper/xfce-clipboard-manager.h |   57 ++++
 6 files changed, 445 insertions(+), 426 deletions(-)

diff --git a/xfce4-settings-helper/Makefile.am b/xfce4-settings-helper/Makefile.am
index 2dc53d1..dc23081 100644
--- a/xfce4-settings-helper/Makefile.am
+++ b/xfce4-settings-helper/Makefile.am
@@ -18,8 +18,6 @@ xfce4_settings_helper_SOURCES = \
 	main.c \
 	accessibility.c \
 	accessibility.h \
-	gsd-clipboard-manager.c \
-	gsd-clipboard-manager.h \
 	keyboards.c \
 	keyboards.h \
 	keyboard-shortcuts.c \
@@ -29,7 +27,9 @@ xfce4_settings_helper_SOURCES = \
 	pointers.c \
 	pointers.h \
 	workspaces.c \
-	workspaces.h
+	workspaces.h \
+	xfce-clipboard-manager.c \
+	xfce-clipboard-manager.h
 
 xfce4_settings_helper_CFLAGS = \
 	-I$(top_builddir) \
diff --git a/xfce4-settings-helper/gsd-clipboard-manager.c b/xfce4-settings-helper/gsd-clipboard-manager.c
deleted file mode 100644
index 3d4a21e..0000000
--- a/xfce4-settings-helper/gsd-clipboard-manager.c
+++ /dev/null
@@ -1,347 +0,0 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
- *
- * Copyright (C) 2007 Matthias Clasen
- * Copyright (C) 2007 Anders Carlsson
- * Copyright (C) 2007 Rodrigo Moya
- * Copyright (C) 2007 William Jon McCann <mccann at jhu.edu>
- * Copyright (C) 2009 Mike Massonnet <mmassonnet at xfce.org>
- *
- * 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.
- */
-
-#include "config.h"
-
-#include <glib.h>
-#include <gdk/gdkx.h>
-#include <gtk/gtk.h>
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-
-#include "gsd-clipboard-manager.h"
-
-#define GSD_CLIPBOARD_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_CLIPBOARD_MANAGER, GsdClipboardManagerPrivate))
-
-struct GsdClipboardManagerPrivate
-{
-        GtkClipboard *default_clipboard;
-        GtkClipboard *primary_clipboard;
-
-        GSList       *default_cache;
-        gchar        *primary_cache;
-
-        gboolean      internal_change;
-
-        GtkWidget    *window;
-};
-
-static void     gsd_clipboard_manager_class_init  (GsdClipboardManagerClass *klass);
-static void     gsd_clipboard_manager_init        (GsdClipboardManager      *clipboard_manager);
-
-G_DEFINE_TYPE (GsdClipboardManager, gsd_clipboard_manager, G_TYPE_OBJECT)
-
-static gpointer manager_object = NULL;
-
-
-Atom XA_CLIPBOARD_MANAGER;
-Atom XA_MANAGER;
-
-static void
-init_atoms (Display *display)
-{
-        static int _init_atoms = 0;
-
-        if (_init_atoms > 0) {
-                return;
-        }
-
-        XA_CLIPBOARD_MANAGER = XInternAtom (display, "CLIPBOARD_MANAGER", False);
-        XA_MANAGER = XInternAtom (display, "MANAGER", False);
-
-        _init_atoms = 1;
-}
-
-
-static void
-default_clipboard_store (GsdClipboardManager *manager)
-{
-        GtkSelectionData *selection_data;
-        GdkAtom          *atoms;
-        gint              n_atoms;
-        gint              i;
-
-        if (!gtk_clipboard_wait_for_targets (manager->priv->default_clipboard, &atoms, &n_atoms)) {
-                return;
-        }
-
-        if (manager->priv->default_cache != NULL) {
-                g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
-                g_slist_free (manager->priv->default_cache);
-                manager->priv->default_cache = NULL;
-        }
-
-        for (i = 0; i < n_atoms; i++) {
-                if (atoms[i] == gdk_atom_intern_static_string ("TARGETS")
-                    || atoms[i] == gdk_atom_intern_static_string ("MULTIPLE")
-                    || atoms[i] == gdk_atom_intern_static_string ("DELETE")
-                    || atoms[i] == gdk_atom_intern_static_string ("INSERT_PROPERTY")
-                    || atoms[i] == gdk_atom_intern_static_string ("INSERT_SELECTION")
-                    || atoms[i] == gdk_atom_intern_static_string ("PIXMAP")) {
-                        continue;
-                }
-
-                selection_data = gtk_clipboard_wait_for_contents (manager->priv->default_clipboard, atoms[i]);
-                if (selection_data == NULL) {
-                        continue;
-                }
-
-                manager->priv->default_cache = g_slist_prepend (manager->priv->default_cache, selection_data);
-        }
-}
-
-static void
-default_clipboard_get_func (GtkClipboard *clipboard,
-                            GtkSelectionData *selection_data,
-                            guint info,
-                            GsdClipboardManager *manager)
-{
-        GSList           *list;
-        GtkSelectionData *selection_data_cache = NULL;
-
-        list = manager->priv->default_cache;
-        for (; list->next != NULL; list = list->next) {
-                selection_data_cache = list->data;
-                if (selection_data->target == selection_data_cache->target) {
-                        break;
-                }
-                selection_data_cache = NULL;
-        }
-        if (selection_data_cache == NULL) {
-                return;
-        }
-        gtk_selection_data_set (selection_data, selection_data->target,
-                                selection_data_cache->format,
-                                selection_data_cache->data,
-                                selection_data_cache->length);
-}
-
-static void
-default_clipboard_clear_func (GtkClipboard *clipboard,
-                              GsdClipboardManager *manager)
-{
-        return;
-}
-
-
-static void
-default_clipboard_restore (GsdClipboardManager *manager)
-{
-        GtkTargetList    *target_list;
-        GtkTargetEntry   *targets;
-        gint              n_targets;
-        GtkSelectionData *sdata;
-        GSList           *list;
-
-        target_list = gtk_target_list_new (NULL, 0);
-        list = manager->priv->default_cache;
-        for (; list->next != NULL; list = list->next) {
-                sdata = list->data;
-                gtk_target_list_add (target_list, sdata->target, 0, 0);
-        }
-        targets = gtk_target_table_new_from_list (target_list, &n_targets);
-
-        gtk_clipboard_set_with_data (manager->priv->default_clipboard,
-                                     targets, n_targets,
-                                     (GtkClipboardGetFunc)default_clipboard_get_func,
-                                     (GtkClipboardClearFunc)default_clipboard_clear_func,
-                                     manager);
-}
-
-static void
-default_clipboard_owner_change (GsdClipboardManager *manager,
-                                GdkEventOwnerChange *event)
-{
-        if (event->send_event == TRUE) {
-                return;
-        }
-
-        if (event->owner != 0) {
-                if (manager->priv->internal_change) {
-                        manager->priv->internal_change = FALSE;
-                        return;
-                }
-                default_clipboard_store (manager);
-        }
-        else {
-                /* This 'bug' happens with Mozilla applications, it means that
-                 * we restored the clipboard (we own it now) but somehow we are
-                 * being noticed twice about that fact where first the owner is
-                 * 0 (which is when we must restore) but then again where the
-                 * owner is ourself (which is what normally only happens and
-                 * also that means that we have to store the clipboard content
-                 * e.g. owner is not 0). By the second time we would store
-                 * ourself back with an empty clipboard... solution is to jump
-                 * over the first time and don't try to restore empty data. */
-                if (manager->priv->internal_change) {
-                        return;
-                }
-
-                manager->priv->internal_change = TRUE;
-                default_clipboard_restore (manager);
-        }
-}
-
-static void
-primary_clipboard_owner_change (GsdClipboardManager *manager,
-                                GdkEventOwnerChange *event)
-{
-        gchar *text;
-
-        if (event->send_event == TRUE) {
-                return;
-        }
-
-        if (event->owner != 0) {
-                text = gtk_clipboard_wait_for_text (manager->priv->primary_clipboard);
-                if (text != NULL) {
-                        g_free (manager->priv->primary_cache);
-                        manager->priv->primary_cache = text;
-                }
-        }
-        else {
-                if (manager->priv->primary_cache != NULL) {
-                        gtk_clipboard_set_text (manager->priv->primary_clipboard,
-                                                manager->priv->primary_cache,
-                                                -1);
-                }
-        }
-}
-
-static gboolean
-start_clipboard_idle_cb (GsdClipboardManager *manager)
-{
-        XClientMessageEvent     xev;
-        gboolean                ownership;
-        Display                *display;
-        Window                  window;
-        Time                    timestamp;
-
-        display = GDK_DISPLAY ();
-        init_atoms (display);
-
-        /* Check if there is a clipboard manager running */
-        if (gdk_display_supports_clipboard_persistence (gdk_display_get_default ())) {
-                g_warning ("Clipboard manager is already running.");
-                return FALSE;
-        }
-
-        manager->priv->window = gtk_invisible_new ();
-        gtk_widget_realize (manager->priv->window);
-
-        window = GDK_WINDOW_XID (manager->priv->window->window);
-        timestamp = GDK_CURRENT_TIME;
-
-        XSelectInput (display, window, PropertyChangeMask);
-        XSetSelectionOwner (display, XA_CLIPBOARD_MANAGER, window, timestamp);
-
-        g_signal_connect_swapped (manager->priv->default_clipboard, "owner-change",
-                                  G_CALLBACK (default_clipboard_owner_change), manager);
-        g_signal_connect_swapped (manager->priv->primary_clipboard, "owner-change",
-                                  G_CALLBACK (primary_clipboard_owner_change), manager);
-
-        /* Check to see if we managed to claim the selection. If not,
-         * we treat it as if we got it then immediately lost it
-         */
-        if (XGetSelectionOwner (display, XA_CLIPBOARD_MANAGER) == window) {
-                xev.type = ClientMessage;
-                xev.window = DefaultRootWindow (display);
-                xev.message_type = XA_MANAGER;
-                xev.format = 32;
-                xev.data.l[0] = timestamp;
-                xev.data.l[1] = XA_CLIPBOARD_MANAGER;
-                xev.data.l[2] = window;
-                xev.data.l[3] = 0;      /* manager specific data */
-                xev.data.l[4] = 0;      /* manager specific data */
-
-                XSendEvent (display, DefaultRootWindow (display), False,
-                            StructureNotifyMask, (XEvent *)&xev);
-        } else {
-                gsd_clipboard_manager_stop (manager);
-        }
-
-        return FALSE;
-}
-
-gboolean
-gsd_clipboard_manager_start (GsdClipboardManager *manager,
-                             GError             **error)
-{
-        g_idle_add ((GSourceFunc) start_clipboard_idle_cb, manager);
-        return TRUE;
-}
-
-void
-gsd_clipboard_manager_stop (GsdClipboardManager *manager)
-{
-        g_debug ("Stopping clipboard manager");
-
-        g_signal_handlers_disconnect_by_func (manager->priv->default_clipboard,
-                                              default_clipboard_owner_change, manager);
-        g_signal_handlers_disconnect_by_func (manager->priv->primary_clipboard,
-                                              primary_clipboard_owner_change, manager);
-        gtk_widget_destroy (manager->priv->window);
-
-        if (manager->priv->default_cache != NULL) {
-                g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
-                g_slist_free (manager->priv->default_cache);
-                manager->priv->default_cache = NULL;
-        }
-        if (manager->priv->primary_cache != NULL) {
-                g_free (manager->priv->primary_cache);
-        }
-}
-
-static void
-gsd_clipboard_manager_class_init (GsdClipboardManagerClass *klass)
-{
-        GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-        g_type_class_add_private (klass, sizeof (GsdClipboardManagerPrivate));
-}
-
-static void
-gsd_clipboard_manager_init (GsdClipboardManager *manager)
-{
-        manager->priv = GSD_CLIPBOARD_MANAGER_GET_PRIVATE (manager);
-
-        manager->priv->default_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
-        manager->priv->primary_clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
-
-        manager->priv->default_cache = NULL;
-        manager->priv->primary_cache = NULL;
-}
-
-GsdClipboardManager *
-gsd_clipboard_manager_new (void)
-{
-        if (manager_object != NULL) {
-                g_object_ref (manager_object);
-        } else {
-                manager_object = g_object_new (GSD_TYPE_CLIPBOARD_MANAGER, NULL);
-                g_object_add_weak_pointer (manager_object,
-                                           (gpointer *) &manager_object);
-        }
-
-        return GSD_CLIPBOARD_MANAGER (manager_object);
-}
diff --git a/xfce4-settings-helper/gsd-clipboard-manager.h b/xfce4-settings-helper/gsd-clipboard-manager.h
deleted file mode 100644
index bf8381f..0000000
--- a/xfce4-settings-helper/gsd-clipboard-manager.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
- *
- * Copyright (C) 2007 William Jon McCann <mccann at jhu.edu>
- *
- * 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.
- *
- */
-
-#ifndef __GSD_CLIPBOARD_MANAGER_H
-#define __GSD_CLIPBOARD_MANAGER_H
-
-#include <glib-object.h>
-
-G_BEGIN_DECLS
-
-#define GSD_TYPE_CLIPBOARD_MANAGER         (gsd_clipboard_manager_get_type ())
-#define GSD_CLIPBOARD_MANAGER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_CLIPBOARD_MANAGER, GsdClipboardManager))
-#define GSD_CLIPBOARD_MANAGER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_CLIPBOARD_MANAGER, GsdClipboardManagerClass))
-#define GSD_IS_CLIPBOARD_MANAGER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_CLIPBOARD_MANAGER))
-#define GSD_IS_CLIPBOARD_MANAGER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_CLIPBOARD_MANAGER))
-#define GSD_CLIPBOARD_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_CLIPBOARD_MANAGER, GsdClipboardManagerClass))
-
-typedef struct GsdClipboardManagerPrivate GsdClipboardManagerPrivate;
-
-typedef struct
-{
-        GObject                     parent;
-        GsdClipboardManagerPrivate *priv;
-} GsdClipboardManager;
-
-typedef struct
-{
-        GObjectClass   parent_class;
-} GsdClipboardManagerClass;
-
-GType                   gsd_clipboard_manager_get_type            (void);
-
-GsdClipboardManager *   gsd_clipboard_manager_new                 (void);
-gboolean                gsd_clipboard_manager_start               (GsdClipboardManager  *manager,
-                                                                   GError              **error);
-void                    gsd_clipboard_manager_stop                (GsdClipboardManager  *manager);
-
-G_END_DECLS
-
-#endif /* __GSD_CLIPBOARD_MANAGER_H */
diff --git a/xfce4-settings-helper/main.c b/xfce4-settings-helper/main.c
index 1a37e12..a45fdf1 100644
--- a/xfce4-settings-helper/main.c
+++ b/xfce4-settings-helper/main.c
@@ -51,11 +51,11 @@
 
 #include "accessibility.h"
 #include "pointers.h"
-#include "gsd-clipboard-manager.h"
 #include "keyboards.h"
 #include "keyboard-layout.h"
 #include "keyboard-shortcuts.h"
 #include "workspaces.h"
+#include "xfce-clipboard-manager.h"
 
 #ifdef HAVE_XRANDR
 #include "displays.h"
@@ -209,22 +209,22 @@ xfce_settings_helper_acquire_selection (gboolean force)
 gint
 main (gint argc, gchar **argv)
 {
-    GsdClipboardManager *clipboard_daemon;
-    GError              *error = NULL;
-    GOptionContext      *context;
-    gboolean             in_session;
-    GObject             *pointer_helper;
-    GObject             *keyboards_helper;
-    GObject             *accessibility_helper;
-    GObject             *shortcuts_helper;
-    GObject             *keyboard_layout_helper;
+    XfceClipboardManager *clipboard_daemon;
+    GError               *error = NULL;
+    GOptionContext       *context;
+    gboolean              in_session;
+    GObject              *pointer_helper;
+    GObject              *keyboards_helper;
+    GObject              *accessibility_helper;
+    GObject              *shortcuts_helper;
+    GObject              *keyboard_layout_helper;
 #ifdef HAVE_XRANDR
-    GObject             *displays_helper;
+    GObject              *displays_helper;
 #endif
-    GObject             *workspaces_helper;
-    pid_t                pid;
-    guint                i;
-    const gint           signums[] = { SIGHUP, SIGINT, SIGQUIT, SIGTERM };
+    GObject              *workspaces_helper;
+    pid_t                 pid;
+    guint                 i;
+    const gint            signums[] = { SIGHUP, SIGINT, SIGQUIT, SIGTERM };
 
     /* setup translation domain */
     xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
@@ -288,7 +288,7 @@ main (gint argc, gchar **argv)
         g_printerr ("Failed to connect to session manager: %s\n", error->message);
         g_error_free (error);
     }
-    
+
     in_session = xfce_sm_client_is_resumed (sm_client);
     if (!xfce_settings_helper_acquire_selection (in_session))
     {
@@ -330,8 +330,8 @@ main (gint argc, gchar **argv)
     workspaces_helper = g_object_new (XFCE_TYPE_WORKSPACES_HELPER, NULL);
 
     /* Try to start the clipboard daemon */
-    clipboard_daemon = gsd_clipboard_manager_new ();
-    gsd_clipboard_manager_start (clipboard_daemon, NULL);
+    clipboard_daemon = xfce_clipboard_manager_new ();
+    xfce_clipboard_manager_start (clipboard_daemon, NULL);
 
     /* setup signal handlers to properly quit the main loop */
     if (xfce_posix_signal_handler_init (NULL))
@@ -355,7 +355,7 @@ main (gint argc, gchar **argv)
     g_object_unref (G_OBJECT (workspaces_helper));
 
     /* Stop the clipboard daemon */
-    gsd_clipboard_manager_stop (clipboard_daemon);
+    xfce_clipboard_manager_stop (clipboard_daemon);
 
     /* shutdown xfconf */
     xfconf_shutdown ();
diff --git a/xfce4-settings-helper/xfce-clipboard-manager.c b/xfce4-settings-helper/xfce-clipboard-manager.c
new file mode 100644
index 0000000..0432b25
--- /dev/null
+++ b/xfce4-settings-helper/xfce-clipboard-manager.c
@@ -0,0 +1,366 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Matthias Clasen
+ * Copyright (C) 2007 Anders Carlsson
+ * Copyright (C) 2007 Rodrigo Moya
+ * Copyright (C) 2007 William Jon McCann <mccann at jhu.edu>
+ * Copyright (C) 2009 Mike Massonnet <mmassonnet at xfce.org>
+ *
+ * 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.
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
+#include "xfce-clipboard-manager.h"
+
+#define XFCE_CLIPBOARD_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), XFCE_TYPE_CLIPBOARD_MANAGER, XfceClipboardManagerPrivate))
+
+struct XfceClipboardManagerPrivate
+{
+  GtkClipboard *default_clipboard;
+  GtkClipboard *primary_clipboard;
+
+  GSList       *default_cache;
+  gchar        *primary_cache;
+
+  gboolean      internal_change;
+
+  GtkWidget    *window;
+};
+
+static void xfce_clipboard_manager_class_init (XfceClipboardManagerClass *klass);
+static void xfce_clipboard_manager_init       (XfceClipboardManager      *clipboard_manager);
+
+G_DEFINE_TYPE (XfceClipboardManager, xfce_clipboard_manager, G_TYPE_OBJECT)
+
+static gpointer manager_object = NULL;
+
+
+Atom XA_CLIPBOARD_MANAGER;
+Atom XA_MANAGER;
+
+static void
+init_atoms (Display *display)
+{
+  static int _init_atoms = 0;
+
+  if (_init_atoms > 0)
+    return;
+
+  XA_CLIPBOARD_MANAGER = XInternAtom (display, "CLIPBOARD_MANAGER", False);
+  XA_MANAGER = XInternAtom (display, "MANAGER", False);
+
+  _init_atoms = 1;
+}
+
+
+static void
+default_clipboard_store (XfceClipboardManager *manager)
+{
+  GtkSelectionData *selection_data;
+  GdkAtom          *atoms;
+  gint              n_atoms;
+  gint              i;
+
+  if (!gtk_clipboard_wait_for_targets (manager->priv->default_clipboard, &atoms, &n_atoms))
+    return;
+
+  if (manager->priv->default_cache != NULL)
+    {
+      g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
+                       g_slist_free (manager->priv->default_cache);
+      manager->priv->default_cache = NULL;
+    }
+
+  for (i = 0; i < n_atoms; i++)
+    {
+      if (atoms[i] == gdk_atom_intern_static_string ("TARGETS") ||
+          atoms[i] == gdk_atom_intern_static_string ("MULTIPLE") ||
+          atoms[i] == gdk_atom_intern_static_string ("DELETE") ||
+          atoms[i] == gdk_atom_intern_static_string ("INSERT_PROPERTY") ||
+          atoms[i] == gdk_atom_intern_static_string ("INSERT_SELECTION") ||
+          atoms[i] == gdk_atom_intern_static_string ("PIXMAP"))
+        {
+          continue;
+        }
+
+      selection_data = gtk_clipboard_wait_for_contents (manager->priv->default_clipboard, atoms[i]);
+
+      if (selection_data == NULL)
+        return;
+
+      manager->priv->default_cache = g_slist_prepend (manager->priv->default_cache, selection_data);
+    }
+}
+
+static void
+default_clipboard_get_func (GtkClipboard *clipboard,
+                            GtkSelectionData *selection_data,
+                            guint info,
+                            XfceClipboardManager *manager)
+{
+  GSList           *list;
+  GtkSelectionData *selection_data_cache = NULL;
+
+  list = manager->priv->default_cache;
+
+  for (; list->next != NULL; list = list->next)
+    {
+      selection_data_cache = list->data;
+
+      if (selection_data->target == selection_data_cache->target)
+        break;
+
+      selection_data_cache = NULL;
+    }
+
+  if (selection_data_cache == NULL)
+    return;
+
+  gtk_selection_data_set (selection_data, selection_data->target,
+                          selection_data_cache->format,
+                          selection_data_cache->data,
+                          selection_data_cache->length);
+}
+
+static void
+default_clipboard_clear_func (GtkClipboard *clipboard,
+                              XfceClipboardManager *manager)
+{
+  return;
+}
+
+
+static void
+default_clipboard_restore (XfceClipboardManager *manager)
+{
+  GtkTargetList    *target_list;
+  GtkTargetEntry   *targets;
+  gint              n_targets;
+  GtkSelectionData *sdata;
+  GSList           *list;
+
+  target_list = gtk_target_list_new (NULL, 0);
+  list = manager->priv->default_cache;
+
+  for (; list->next != NULL; list = list->next)
+    {
+      sdata = list->data;
+      gtk_target_list_add (target_list, sdata->target, 0, 0);
+    }
+
+  targets = gtk_target_table_new_from_list (target_list, &n_targets);
+
+  gtk_clipboard_set_with_data (manager->priv->default_clipboard,
+                               targets, n_targets,
+                               (GtkClipboardGetFunc)default_clipboard_get_func,
+                               (GtkClipboardClearFunc)default_clipboard_clear_func,
+                               manager);
+}
+
+static void
+default_clipboard_owner_change (XfceClipboardManager *manager,
+                                GdkEventOwnerChange *event)
+{
+  if (event->send_event == TRUE)
+    return;
+
+  if (event->owner != 0)
+    {
+      if (manager->priv->internal_change)
+        {
+          manager->priv->internal_change = FALSE;
+          return;
+        }
+
+      default_clipboard_store (manager);
+    }
+  else
+    {
+      /* This 'bug' happens with Mozilla applications, it means that
+       * we restored the clipboard (we own it now) but somehow we are
+       * being noticed twice about that fact where first the owner is
+       * 0 (which is when we must restore) but then again where the
+       * owner is ourself (which is what normally only happens and
+       * also that means that we have to store the clipboard content
+       * e.g. owner is not 0). By the second time we would store
+       * ourself back with an empty clipboard... solution is to jump
+       * over the first time and don't try to restore empty data. */
+      if (manager->priv->internal_change)
+        return;
+
+      manager->priv->internal_change = TRUE;
+      default_clipboard_restore (manager);
+    }
+}
+
+static void
+primary_clipboard_owner_change (XfceClipboardManager *manager,
+                                GdkEventOwnerChange *event)
+{
+  gchar *text;
+
+  if (event->send_event == TRUE)
+    return;
+
+  if (event->owner != 0)
+    {
+      text = gtk_clipboard_wait_for_text (manager->priv->primary_clipboard);
+
+      if (text != NULL)
+        {
+          g_free (manager->priv->primary_cache);
+          manager->priv->primary_cache = text;
+        }
+    }
+  else
+    {
+      if (manager->priv->primary_cache != NULL)
+        gtk_clipboard_set_text (manager->priv->primary_clipboard,
+                                manager->priv->primary_cache,
+                                -1);
+    }
+}
+
+static gboolean
+start_clipboard_idle_cb (XfceClipboardManager *manager)
+{
+  XClientMessageEvent     xev;
+  gboolean                ownership;
+  Display                *display;
+  Window                  window;
+  Time                    timestamp;
+
+  display = GDK_DISPLAY ();
+  init_atoms (display);
+
+  /* Check if there is a clipboard manager running */
+  if (gdk_display_supports_clipboard_persistence (gdk_display_get_default ()))
+    {
+      g_warning ("Clipboard manager is already running.");
+      return FALSE;
+    }
+
+  manager->priv->window = gtk_invisible_new ();
+  gtk_widget_realize (manager->priv->window);
+
+  window = GDK_WINDOW_XID (manager->priv->window->window);
+  timestamp = GDK_CURRENT_TIME;
+
+  XSelectInput (display, window, PropertyChangeMask);
+  XSetSelectionOwner (display, XA_CLIPBOARD_MANAGER, window, timestamp);
+
+  g_signal_connect_swapped (manager->priv->default_clipboard, "owner-change",
+                            G_CALLBACK (default_clipboard_owner_change), manager);
+  g_signal_connect_swapped (manager->priv->primary_clipboard, "owner-change",
+                            G_CALLBACK (primary_clipboard_owner_change), manager);
+
+  /* Check to see if we managed to claim the selection. If not,
+   * we treat it as if we got it then immediately lost it
+   */
+  if (XGetSelectionOwner (display, XA_CLIPBOARD_MANAGER) == window)
+    {
+      xev.type = ClientMessage;
+      xev.window = DefaultRootWindow (display);
+      xev.message_type = XA_MANAGER;
+      xev.format = 32;
+      xev.data.l[0] = timestamp;
+      xev.data.l[1] = XA_CLIPBOARD_MANAGER;
+      xev.data.l[2] = window;
+      xev.data.l[3] = 0;      /* manager specific data */
+      xev.data.l[4] = 0;      /* manager specific data */
+
+      XSendEvent (display, DefaultRootWindow (display), False,
+                  StructureNotifyMask, (XEvent *)&xev);
+    }
+  else
+    {
+      xfce_clipboard_manager_stop (manager);
+    }
+
+  return FALSE;
+}
+
+gboolean
+xfce_clipboard_manager_start (XfceClipboardManager *manager,
+                              GError              **error)
+{
+  g_idle_add ((GSourceFunc) start_clipboard_idle_cb, manager);
+
+  return TRUE;
+}
+
+void
+xfce_clipboard_manager_stop (XfceClipboardManager *manager)
+{
+  g_debug ("Stopping clipboard manager");
+
+  g_signal_handlers_disconnect_by_func (manager->priv->default_clipboard,
+                                        default_clipboard_owner_change, manager);
+  g_signal_handlers_disconnect_by_func (manager->priv->primary_clipboard,
+                                        primary_clipboard_owner_change, manager);
+  gtk_widget_destroy (manager->priv->window);
+
+  if (manager->priv->default_cache != NULL)
+    {
+      g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
+      g_slist_free (manager->priv->default_cache);
+      manager->priv->default_cache = NULL;
+    }
+
+  if (manager->priv->primary_cache != NULL)
+    g_free (manager->priv->primary_cache);
+}
+
+static void
+xfce_clipboard_manager_class_init (XfceClipboardManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  g_type_class_add_private (klass, sizeof (XfceClipboardManagerPrivate));
+}
+
+static void
+xfce_clipboard_manager_init (XfceClipboardManager *manager)
+{
+  manager->priv = XFCE_CLIPBOARD_MANAGER_GET_PRIVATE (manager);
+
+  manager->priv->default_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+  manager->priv->primary_clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
+
+  manager->priv->default_cache = NULL;
+  manager->priv->primary_cache = NULL;
+}
+
+XfceClipboardManager *
+xfce_clipboard_manager_new (void)
+{
+  if (manager_object != NULL)
+    g_object_ref (manager_object);
+  else
+    {
+      manager_object = g_object_new (XFCE_TYPE_CLIPBOARD_MANAGER, NULL);
+      g_object_add_weak_pointer (manager_object,
+                                 (gpointer *) &manager_object);
+    }
+
+  return XFCE_CLIPBOARD_MANAGER (manager_object);
+}
diff --git a/xfce4-settings-helper/xfce-clipboard-manager.h b/xfce4-settings-helper/xfce-clipboard-manager.h
new file mode 100644
index 0000000..cac4906
--- /dev/null
+++ b/xfce4-settings-helper/xfce-clipboard-manager.h
@@ -0,0 +1,57 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 William Jon McCann <mccann at jhu.edu>
+ *
+ * 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.
+ *
+ */
+
+#ifndef __XFCE_CLIPBOARD_MANAGER_H
+#define __XFCE_CLIPBOARD_MANAGER_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define XFCE_TYPE_CLIPBOARD_MANAGER         (xfce_clipboard_manager_get_type ())
+#define XFCE_CLIPBOARD_MANAGER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), XFCE_TYPE_CLIPBOARD_MANAGER, XfceClipboardManager))
+#define XFCE_CLIPBOARD_MANAGER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), XFCE_TYPE_CLIPBOARD_MANAGER, XfceClipboardManagerClass))
+#define XFCE_IS_CLIPBOARD_MANAGER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), XFCE_TYPE_CLIPBOARD_MANAGER))
+#define XFCE_IS_CLIPBOARD_MANAGER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), XFCE_TYPE_CLIPBOARD_MANAGER))
+#define XFCE_CLIPBOARD_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), XFCE_TYPE_CLIPBOARD_MANAGER, XfceClipboardManagerClass))
+
+typedef struct XfceClipboardManagerPrivate XfceClipboardManagerPrivate;
+
+typedef struct
+{
+  GObject                      parent;
+  XfceClipboardManagerPrivate *priv;
+} XfceClipboardManager;
+
+typedef struct
+{
+  GObjectClass   parent_class;
+} XfceClipboardManagerClass;
+
+GType                 xfce_clipboard_manager_get_type            (void);
+
+XfceClipboardManager *xfce_clipboard_manager_new                 (void);
+gboolean              xfce_clipboard_manager_start               (XfceClipboardManager  *manager,
+                                                                  GError               **error);
+void                  xfce_clipboard_manager_stop                (XfceClipboardManager  *manager);
+
+G_END_DECLS
+
+#endif /* __XFCE_CLIPBOARD_MANAGER_H */



More information about the Xfce4-commits mailing list