From e1f40a2431f58e95437b2b326cf3a288fc11c76b Mon Sep 17 00:00:00 2001
From: Victor Toso <me@victortoso.com>
Date: Fri, 10 Feb 2017 15:28:13 +0100
Subject: [PATCH spice-gtk 1/2] gtk-session: Use GWeakRef
To: spice-devel@lists.freedesktop.org
The custom WeakRef structure was introduced in 9baba9fd89 (2012) to
fix rhbz#743773. Since glib 2.32, GWeakRef was introduced and it
behaves similarly to our WeakRef.
Moving to GWeakRef to remove some code which exists in glib.
Note that I'm keeping two utility functions:
- get_weak_ref(gpointer object): which returns a newly allocated
GWeakRef, initialized with @object;
- free_weak_ref(gpointer pdata): which frees the GWeakRef and returns
the original object or NULL. It also takes care of an extra
reference to the object.
Signed-off-by: Victor Toso <victortoso@redhat.com>
Acked-by: Christophe Fergeau <cfergeau@redhat.com>
(cherry picked from commit 12f5d3391d6093b53c5b3f0f18182a859576a4b7)
---
src/spice-gtk-session.c | 52 +++++++++++++++++++------------------------------
1 file changed, 20 insertions(+), 32 deletions(-)
diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
index 3e0ec74..6424596 100644
--- a/src/spice-gtk-session.c
+++ b/src/spice-gtk-session.c
@@ -572,31 +572,26 @@ static const struct {
}
};
-typedef struct _WeakRef {
- GObject *object;
-} WeakRef;
-
-static void weak_notify_cb(WeakRef *weakref, GObject *object)
-{
- weakref->object = NULL;
-}
-
-static WeakRef* weak_ref(GObject *object)
+static GWeakRef* get_weak_ref(gpointer object)
{
- WeakRef *weakref = g_new(WeakRef, 1);
-
- g_object_weak_ref(object, (GWeakNotify)weak_notify_cb, weakref);
- weakref->object = object;
-
+ GWeakRef *weakref = g_new(GWeakRef, 1);
+ g_weak_ref_init(weakref, object);
return weakref;
}
-static void weak_unref(WeakRef* weakref)
+static gpointer free_weak_ref(gpointer data)
{
- if (weakref->object)
- g_object_weak_unref(weakref->object, (GWeakNotify)weak_notify_cb, weakref);
+ GWeakRef *weakref = data;
+ gpointer object = g_weak_ref_get(weakref);
+ g_weak_ref_clear(weakref);
g_free(weakref);
+ if (object != NULL) {
+ /* The main reference still exists as object is not NULL, so we can
+ * remove the strong reference given by g_weak_ref_get */
+ g_object_unref(object);
+ }
+ return object;
}
static void clipboard_get_targets(GtkClipboard *clipboard,
@@ -604,9 +599,7 @@ static void clipboard_get_targets(GtkClipboard *clipboard,
gint n_atoms,
gpointer user_data)
{
- WeakRef *weakref = user_data;
- SpiceGtkSession *self = (SpiceGtkSession*)weakref->object;
- weak_unref(weakref);
+ SpiceGtkSession *self = free_weak_ref(user_data);
if (self == NULL)
return;
@@ -703,7 +696,7 @@ static void clipboard_owner_change(GtkClipboard *clipboard,
s->clip_hasdata[selection] = TRUE;
if (s->auto_clipboard_enable && !read_only(self))
gtk_clipboard_request_targets(clipboard, clipboard_get_targets,
- weak_ref(G_OBJECT(self)));
+ get_weak_ref(self));
break;
default:
s->clip_hasdata[selection] = FALSE;
@@ -934,14 +927,11 @@ static void clipboard_received_text_cb(GtkClipboard *clipboard,
const gchar *text,
gpointer user_data)
{
- WeakRef *weakref = user_data;
- SpiceGtkSession *self = (SpiceGtkSession*)weakref->object;
+ SpiceGtkSession *self = free_weak_ref(user_data);
char *conv = NULL;
int len = 0;
int selection;
- weak_unref(weakref);
-
if (self == NULL)
return;
@@ -977,9 +967,7 @@ static void clipboard_received_cb(GtkClipboard *clipboard,
GtkSelectionData *selection_data,
gpointer user_data)
{
- WeakRef *weakref = user_data;
- SpiceGtkSession *self = (SpiceGtkSession*)weakref->object;
- weak_unref(weakref);
+ SpiceGtkSession *self = free_weak_ref(user_data);
if (self == NULL)
return;
@@ -1050,7 +1038,7 @@ static gboolean clipboard_request(SpiceMainChannel *main, guint selection,
if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
gtk_clipboard_request_text(cb, clipboard_received_text_cb,
- weak_ref(G_OBJECT(self)));
+ get_weak_ref(self));
} else {
for (m = 0; m < SPICE_N_ELEMENTS(atom2agent); m++) {
if (atom2agent[m].vdagent == type)
@@ -1061,7 +1049,7 @@ static gboolean clipboard_request(SpiceMainChannel *main, guint selection,
atom = gdk_atom_intern_static_string(atom2agent[m].xatom);
gtk_clipboard_request_contents(cb, atom, clipboard_received_cb,
- weak_ref(G_OBJECT(self)));
+ get_weak_ref(self));
}
return TRUE;
@@ -1234,7 +1222,7 @@ void spice_gtk_session_copy_to_guest(SpiceGtkSession *self)
if (s->clip_hasdata[selection] && !s->clip_grabbed[selection]) {
gtk_clipboard_request_targets(s->clipboard, clipboard_get_targets,
- weak_ref(G_OBJECT(self)));
+ get_weak_ref(self));
}
}
--
2.12.0