[virt-tools-list] [PATCH] timed-revealer: Inherit from GtkEventBox

Fabiano Fidêncio fidencio at redhat.com
Tue Jun 28 14:18:30 UTC 2016


As suggested by Christophe, inheritting from GtkEventBox instead of
having one instance of it as a member can help us to get rid of
virt_viewer_timed_revealer_get_overlay_widget() and also makes more
natural that we take ownership of the GtkToolbar floating ref.

This change also fixes the crash pointed by Pavel:
 #0  0x00007ffff3e92c9d in g_type_check_instance_is_fundamentally_a () at /lib64/libgobject-2.0.so.0
 #1  0x00007ffff3e722a5 in g_object_unref () at /lib64/libgobject-2.0.so.0
 #2  0x000000000041ebe3 in virt_viewer_timed_revealer_dispose  (object=0x1127320) at virt-viewer-timed-revealer.c:128
 #3  0x00007ffff3e723b6 in g_object_unref () at  /lib64/libgobject-2.0.so.0
 #4  0x000000000041c040 in virt_viewer_window_dispose  (object=0x981f70) at virt-viewer-window.c:191
 #5  0x00007ffff3e723b6 in g_object_unref () at  /lib64/libgobject-2.0.so.0
 #6  0x0000000000413a58 in virt_viewer_app_display_removed (nth=<optimized out>, self=0x680330) at virt-viewer-app.c:989
 #7  0x0000000000413a58 in virt_viewer_app_display_removed (session=<optimized out>, display=<optimized out>, self=0x680330) at virt-viewer-app.c:1000
 #8  0x00007ffff3e705e0 in g_cclosure_marshal_VOID__OBJECTv () at /lib64/libgobject-2.0.so.0  #9  0x00007ffff3e6d784 in _g_closure_invoke_va () at  /lib64/libgobject-2.0.so.0
 #10 0x00007ffff3e88cd9 in g_signal_emit_valist () at /lib64/libgobject-2.0.so.0
 #11 0x00007ffff3e897eb in g_signal_emit_by_name () at /lib64/libgobject-2.0.so.0
 #12 0x0000000000418973 in virt_viewer_session_remove_display (session=0x9c6de0, display=0x961a90) at virt-viewer-session.c:463
 #13 0x0000000000420934 in destroy_display (data=<optimized out>) at virt-viewer-session-spice.c:851
 #14 0x00007ffff3b6d0eb in g_ptr_array_foreach () at /lib64/libglib-2.0.so.0
 #15 0x00007ffff3b6d180 in ptr_array_free () at /lib64/libglib-2.0.so.0
 #16 0x000000000042072a in virt_viewer_session_spice_clear_displays (self=0x9c6de0) at virt-viewer-session-spice.c:94
 #17 0x000000000042240d in virt_viewer_session_spice_close (session=<optimized out>) at virt-viewer-session-spice.c:459
 #18 0x0000000000414be5 in virt_viewer_app_quit (self=self at entry=0x680330) at virt-viewer-app.c:285
 #19 0x0000000000415500 in virt_viewer_app_maybe_quit (self=0x680330, window=window at entry=0x981a90) at virt-viewer-app.c:481
 #20 0x000000000041c4ad in virt_viewer_window_delete (src=<optimized out>, dummy=<optimized out>, self=0x981a90) at virt-viewer-window.c:771
 #21 0x00007ffff61807f1 in _gtk_marshal_BOOLEAN__BOXEDv () at /lib64/libgtk-3.so.0
 #22 0x00007ffff3e6d784 in _g_closure_invoke_va () at /lib64/libgobject-2.0.so.0
 #23 0x00007ffff3e887b3 in g_signal_emit_valist () at /lib64/libgobject-2.0.so.0
 #24 0x00007ffff3e8933f in g_signal_emit () at /lib64/libgobject-2.0.so.0
 #25 0x00007ffff62dde6c in gtk_widget_event_internal () at /lib64/libgtk-3.so.0
 #26 0x00007ffff617f5ef in gtk_main_do_event () at /lib64/libgtk-3.so.0
 #27 0x00007ffff5c7dd25 in _gdk_event_emit () at /lib64/libgdk-3.so.0
 #28 0x00007ffff5cae672 in gdk_event_source_dispatch () at /lib64/libgdk-3.so.0
 #29 0x00007ffff3b9895a in g_main_context_dispatch () at /lib64/libglib-2.0.so.0
 #30 0x00007ffff3b98d10 in g_main_context_iterate.isra () at /lib64/libglib-2.0.so.0
 #31 0x00007ffff3b98dbc in g_main_context_iteration () at /lib64/libglib-2.0.so.0
 #32 0x00007ffff41643cd in g_application_run () at /lib64/libgio-2.0.so.0
 #33 0x000000000040fc1a in main (argc=3, argv=0x7fffffffdec8) at virt-viewer-main.c:41

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 src/virt-viewer-timed-revealer.c | 45 +++++++++++++++++++---------------------
 src/virt-viewer-timed-revealer.h |  7 ++-----
 src/virt-viewer-window.c         |  5 ++---
 3 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/src/virt-viewer-timed-revealer.c b/src/virt-viewer-timed-revealer.c
index 9124dad..f9f00ce 100644
--- a/src/virt-viewer-timed-revealer.c
+++ b/src/virt-viewer-timed-revealer.c
@@ -25,7 +25,7 @@
 
 #include "virt-viewer-timed-revealer.h"
 
-G_DEFINE_TYPE (VirtViewerTimedRevealer, virt_viewer_timed_revealer, G_TYPE_OBJECT)
+G_DEFINE_TYPE (VirtViewerTimedRevealer, virt_viewer_timed_revealer, GTK_TYPE_EVENT_BOX)
 
 #define VIRT_VIEWER_TIMED_REVEALER_GET_PRIVATE(obj) \
     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), VIRT_VIEWER_TYPE_TIMED_REVEALER, VirtViewerTimedRevealerPrivate))
@@ -36,7 +36,6 @@ struct _VirtViewerTimedRevealerPrivate
     guint timeout_id;
 
     GtkWidget *revealer;
-    GtkWidget *evBox;
 };
 
 static void
@@ -76,9 +75,9 @@ virt_viewer_timed_revealer_schedule_unreveal_timeout(VirtViewerTimedRevealer *se
 }
 
 static gboolean
-virt_viewer_timed_revealer_enter_leave_notify(GtkWidget *evBox,
+virt_viewer_timed_revealer_enter_leave_notify(VirtViewerTimedRevealer *self,
                                               GdkEventCrossing *event,
-                                              VirtViewerTimedRevealer *self)
+                                              gpointer user_data G_GNUC_UNUSED)
 {
     VirtViewerTimedRevealerPrivate *priv = self->priv;
     GdkDevice *device;
@@ -92,7 +91,7 @@ virt_viewer_timed_revealer_enter_leave_notify(GtkWidget *evBox,
     device = gdk_event_get_device((GdkEvent *)event);
 
     gdk_window_get_device_position(event->window, device, &x, &y, 0);
-    gtk_widget_get_allocation(evBox, &allocation);
+    gtk_widget_get_allocation(GTK_WIDGET(self), &allocation);
 
     entered = !!(x >= 0 && y >= 0 && x < allocation.width && y < allocation.height);
 
@@ -125,8 +124,7 @@ virt_viewer_timed_revealer_dispose(GObject *object)
     VirtViewerTimedRevealer *self = VIRT_VIEWER_TIMED_REVEALER(object);
     VirtViewerTimedRevealerPrivate *priv = self->priv;
 
-    g_clear_object(&priv->evBox);
-    g_clear_object(&priv->revealer);
+    priv->revealer = NULL;
 
     if (priv->timeout_id) {
         g_source_remove(priv->timeout_id);
@@ -153,6 +151,14 @@ virt_viewer_timed_revealer_new(GtkWidget *toolbar)
     VirtViewerTimedRevealer *self;
     VirtViewerTimedRevealerPrivate *priv;
 
+    /*
+     * Make sure gtk_container_add() is not going to steal a reference
+     * the caller thinks it owns
+     */
+    if (g_object_is_floating(toolbar)) {
+        g_object_ref_sink(toolbar);
+    }
+
     self = g_object_new(VIRT_VIEWER_TYPE_TIMED_REVEALER, NULL);
 
     priv = self->priv;
@@ -170,20 +176,19 @@ virt_viewer_timed_revealer_new(GtkWidget *toolbar)
      * the hidden toolbar.
      */
 
-    priv->evBox = gtk_event_box_new();
-    gtk_container_add(GTK_CONTAINER(priv->evBox), priv->revealer);
-    gtk_widget_set_halign(priv->evBox, GTK_ALIGN_CENTER);
-    gtk_widget_set_valign(priv->evBox, GTK_ALIGN_START);
-    gtk_widget_show_all(priv->evBox);
+    gtk_container_add(GTK_CONTAINER(self), priv->revealer);
+    gtk_widget_set_halign(GTK_WIDGET(self), GTK_ALIGN_CENTER);
+    gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_START);
+    gtk_widget_show_all(GTK_WIDGET(self));
 
-    g_signal_connect(priv->evBox,
+    g_signal_connect(self,
                      "enter-notify-event",
                      G_CALLBACK(virt_viewer_timed_revealer_enter_leave_notify),
-                     self);
-    g_signal_connect(priv->evBox,
+                     NULL);
+    g_signal_connect(self,
                      "leave-notify-event",
                      G_CALLBACK(virt_viewer_timed_revealer_enter_leave_notify),
-                     self);
+                     NULL);
 
     return self;
 }
@@ -203,11 +208,3 @@ virt_viewer_timed_revealer_force_reveal(VirtViewerTimedRevealer *self,
     gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer), fullscreen);
     virt_viewer_timed_revealer_schedule_unreveal_timeout(self, 2000);
 }
-
-GtkWidget *
-virt_viewer_timed_revealer_get_overlay_widget(VirtViewerTimedRevealer *self)
-{
-    g_return_val_if_fail(VIRT_VIEWER_IS_TIMED_REVEALER(self), NULL);
-
-    return self->priv->evBox;
-}
diff --git a/src/virt-viewer-timed-revealer.h b/src/virt-viewer-timed-revealer.h
index 6720a63..2305952 100644
--- a/src/virt-viewer-timed-revealer.h
+++ b/src/virt-viewer-timed-revealer.h
@@ -49,12 +49,12 @@ G_BEGIN_DECLS
 typedef struct _VirtViewerTimedRevealerPrivate VirtViewerTimedRevealerPrivate;
 
 typedef struct {
-    GObject parent;
+    GtkEventBox parent;
     VirtViewerTimedRevealerPrivate *priv;
 } VirtViewerTimedRevealer;
 
 typedef struct {
-    GObjectClass parent_class;
+    GtkEventBoxClass parent_class;
 } VirtViewerTimedRevealerClass;
 
 GType virt_viewer_timed_revealer_get_type (void);
@@ -66,9 +66,6 @@ void
 virt_viewer_timed_revealer_force_reveal(VirtViewerTimedRevealer *self,
                                         gboolean fullscreen);
 
-GtkWidget *
-virt_viewer_timed_revealer_get_overlay_widget(VirtViewerTimedRevealer *self);
-
 G_END_DECLS
 
 #endif /* _VIRT_VIEWER_TIMED_REVEALER_H */
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 1ebb423..c51d76e 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -1070,7 +1070,7 @@ virt_viewer_window_toolbar_setup(VirtViewerWindow *self)
     GtkWidget *overlay;
     VirtViewerWindowPrivate *priv = self->priv;
 
-    priv->toolbar = g_object_ref(gtk_toolbar_new());
+    priv->toolbar = gtk_toolbar_new();
     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(priv->toolbar), FALSE);
     gtk_widget_set_no_show_all(priv->toolbar, TRUE);
     gtk_toolbar_set_style(GTK_TOOLBAR(priv->toolbar), GTK_TOOLBAR_BOTH_HORIZ);
@@ -1115,8 +1115,7 @@ virt_viewer_window_toolbar_setup(VirtViewerWindow *self)
 
     priv->revealer = virt_viewer_timed_revealer_new(priv->toolbar);
     overlay = GTK_WIDGET(gtk_builder_get_object(priv->builder, "viewer-overlay"));
-    gtk_overlay_add_overlay(GTK_OVERLAY(overlay),
-                            virt_viewer_timed_revealer_get_overlay_widget(priv->revealer));
+    gtk_overlay_add_overlay(GTK_OVERLAY(overlay), g_object_ref_sink(priv->revealer));
 }
 
 VirtViewerNotebook*
-- 
2.7.4




More information about the virt-tools-list mailing list