[libwnck/wip/muktupavels/wnck-handle-3: 3/5] class-group: move class_group_hash to WnckHandle




commit 736953c5ec07273f75ffcb3154ddcf837d1465fc
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Tue Dec 28 13:14:44 2021 +0200

    class-group: move class_group_hash to WnckHandle

 libwnck/class-group.c         | 43 ++++++++++----------------
 libwnck/private.h             |  1 -
 libwnck/util.c                | 18 -----------
 libwnck/wnck-handle-private.h | 12 +++++++-
 libwnck/wnck-handle.c         | 71 +++++++++++++++++++++++++++++++++++++++----
 5 files changed, 92 insertions(+), 53 deletions(-)
---
diff --git a/libwnck/class-group.c b/libwnck/class-group.c
index f4afc319..2d4f8a34 100644
--- a/libwnck/class-group.c
+++ b/libwnck/class-group.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include "class-group.h"
 #include "window.h"
+#include "wnck-handle-private.h"
 #include "private.h"
 
 /**
@@ -64,9 +65,6 @@ struct _WnckClassGroupPrivate {
 
 G_DEFINE_TYPE_WITH_PRIVATE (WnckClassGroup, wnck_class_group, G_TYPE_OBJECT);
 
-/* Hash table that maps res_class strings -> WnckClassGroup instances */
-static GHashTable *class_group_hash = NULL;
-
 static void wnck_class_group_finalize    (GObject             *object);
 
 enum {
@@ -77,16 +75,6 @@ enum {
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-void
-_wnck_class_group_shutdown_all (void)
-{
-  if (class_group_hash != NULL)
-    {
-      g_hash_table_destroy (class_group_hash);
-      class_group_hash = NULL;
-    }
-}
-
 static void
 wnck_class_group_class_init (WnckClassGroupClass *class)
 {
@@ -214,10 +202,7 @@ wnck_class_group_finalize (GObject *object)
 WnckClassGroup *
 wnck_class_group_get (const char *id)
 {
-  if (!class_group_hash)
-    return NULL;
-  else
-    return g_hash_table_lookup (class_group_hash, id ? id : "");
+  return _wnck_handle_get_class_group (_wnck_get_handle (), id);
 }
 
 /**
@@ -236,23 +221,24 @@ WnckClassGroup *
 _wnck_class_group_create (WnckScreen *screen,
                           const char *res_class)
 {
+  WnckHandle *handle;
   WnckClassGroup *class_group;
 
-  if (class_group_hash == NULL)
-    class_group_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                              NULL, g_object_unref);
+  handle = _wnck_screen_get_handle (screen);
+  class_group = _wnck_handle_get_class_group (handle, res_class);
 
-  g_return_val_if_fail (g_hash_table_lookup (class_group_hash, res_class ? res_class : "") == NULL,
-                       NULL);
+  g_return_val_if_fail (class_group == NULL, NULL);
 
   class_group = g_object_new (WNCK_TYPE_CLASS_GROUP, NULL);
   class_group->priv->screen = screen;
 
   class_group->priv->res_class = g_strdup (res_class ? res_class : "");
 
-  g_hash_table_insert (class_group_hash,
-                       class_group->priv->res_class, class_group);
-  /* Hash now owns one ref, caller gets none */
+  _wnck_handle_insert_class_group (handle,
+                                   class_group->priv->res_class,
+                                   class_group);
+
+  /* Handle now owns one ref, caller gets none */
 
   return class_group;
 }
@@ -266,11 +252,14 @@ _wnck_class_group_create (WnckScreen *screen,
 void
 _wnck_class_group_destroy (WnckClassGroup *class_group)
 {
+  WnckHandle *handle;
+
   g_return_if_fail (WNCK_IS_CLASS_GROUP (class_group));
 
-  g_hash_table_remove (class_group_hash, class_group->priv->res_class);
+  handle = _wnck_screen_get_handle (class_group->priv->screen);
+  _wnck_handle_remove_class_group (handle, class_group->priv->res_class);
 
-  /* Removing from hash also removes the only ref WnckClassGroup had */
+  /* Removing from handle also removes the only ref WnckClassGroup had */
 }
 
 static const char *
diff --git a/libwnck/private.h b/libwnck/private.h
index d37c328b..372e1cb7 100644
--- a/libwnck/private.h
+++ b/libwnck/private.h
@@ -106,7 +106,6 @@ void             _wnck_class_group_add_window    (WnckClassGroup *class_group,
                                                   WnckWindow     *window);
 void             _wnck_class_group_remove_window (WnckClassGroup *class_group,
                                                   WnckWindow     *window);
-void             _wnck_class_group_shutdown_all  (void);
 
 void _wnck_workspace_update_name (WnckWorkspace *workspace,
                                   const char    *name);
diff --git a/libwnck/util.c b/libwnck/util.c
index e908a1c5..3760bc38 100644
--- a/libwnck/util.c
+++ b/libwnck/util.c
@@ -833,24 +833,6 @@ _wnck_get_default_display (void)
 void
 wnck_shutdown (void)
 {
-  /* Warning: this is hacky :-)
-   *
-   * Shutting down all WnckScreen objects will automatically unreference (and
-   * finalize) all WnckWindow objects, but not the WnckClassGroup and
-   * WnckApplication objects.
-   * Therefore we need to manually shut down all WnckClassGroup and
-   * WnckApplication objects first, since they reference the WnckScreen they're
-   * on.
-   * On the other side, shutting down the WnckScreen objects will results in
-   * all WnckWindow objects getting unreferenced and finalized, and must
-   * actually be done before shutting down global WnckWindow structures
-   * (because the WnckScreen has a list of WnckWindow that will get mis-used
-   * otherwise). */
-  _wnck_class_group_shutdown_all ();
-  _wnck_application_shutdown_all ();
-  _wnck_screen_shutdown_all ();
-  _wnck_window_shutdown_all ();
-
   g_clear_object (&wnck_handle);
 
 #ifdef HAVE_XRES
diff --git a/libwnck/wnck-handle-private.h b/libwnck/wnck-handle-private.h
index f277661e..caa41eee 100644
--- a/libwnck/wnck-handle-private.h
+++ b/libwnck/wnck-handle-private.h
@@ -18,6 +18,7 @@
 #ifndef WNCK_HANDLE_PRIVATE_H
 #define WNCK_HANDLE_PRIVATE_H
 
+#include "class-group.h"
 #include "util.h"
 
 G_BEGIN_DECLS
@@ -33,12 +34,21 @@ void            _wnck_handle_set_default_icon_size      (WnckHandle     *self,
 
 gsize           _wnck_handle_get_default_icon_size      (WnckHandle     *self);
 
-
 void            _wnck_handle_set_default_mini_icon_size (WnckHandle     *self,
                                                          gsize           icon_size);
 
 gsize           _wnck_handle_get_default_mini_icon_size (WnckHandle     *self);
 
+void            _wnck_handle_insert_class_group         (WnckHandle      *self,
+                                                         const char      *id,
+                                                         WnckClassGroup  *class_group);
+
+void            _wnck_handle_remove_class_group         (WnckHandle      *self,
+                                                         const char      *id);
+
+WnckClassGroup *_wnck_handle_get_class_group            (WnckHandle      *self,
+                                                         const char      *id);
+
 G_END_DECLS
 
 #endif
diff --git a/libwnck/wnck-handle.c b/libwnck/wnck-handle.c
index 296b9494..eb14f657 100644
--- a/libwnck/wnck-handle.c
+++ b/libwnck/wnck-handle.c
@@ -31,12 +31,14 @@ G_DECLARE_FINAL_TYPE (WnckHandle, wnck_handle, WNCK, HANDLE, GObject)
 
 struct _WnckHandle
 {
-  GObject        parent;
+  GObject         parent;
 
-  WnckClientType client_type;
+  WnckClientType  client_type;
 
-  gsize          default_icon_size;
-  gsize          default_mini_icon_size;
+  gsize           default_icon_size;
+  gsize           default_mini_icon_size;
+
+  GHashTable     *class_group_hash;
 };
 
 enum
@@ -146,6 +148,31 @@ wnck_handle_finalize (GObject *object)
 
   gdk_window_remove_filter (NULL, filter_func, self);
 
+  /* Warning: this is hacky :-)
+   *
+   * Shutting down all WnckScreen objects will automatically unreference
+   * (and finalize) all WnckWindow objects, but not the WnckClassGroup and
+   * WnckApplication objects.
+   * Therefore we need to manually shut down all WnckClassGroup and
+   * WnckApplication objects first, since they reference the WnckScreen they
+   * are on.
+   * On the other side, shutting down the WnckScreen objects will results in
+   * all WnckWindow objects getting unreferenced and finalized, and must
+   * actually be done before shutting down global WnckWindow structures
+   * (because the WnckScreen has a list of WnckWindow that will get mis-used
+   * otherwise).
+   */
+
+  if (self->class_group_hash != NULL)
+    {
+      g_hash_table_destroy (self->class_group_hash);
+      self->class_group_hash = NULL;
+    }
+
+  _wnck_application_shutdown_all ();
+  _wnck_screen_shutdown_all ();
+  _wnck_window_shutdown_all ();
+
   G_OBJECT_CLASS (wnck_handle_parent_class)->finalize (object);
 }
 
@@ -230,11 +257,16 @@ wnck_handle_init (WnckHandle *self)
   self->default_icon_size = WNCK_DEFAULT_ICON_SIZE;
   self->default_mini_icon_size = WNCK_DEFAULT_MINI_ICON_SIZE;
 
+  self->class_group_hash = g_hash_table_new_full (g_str_hash,
+                                                  g_str_equal,
+                                                  NULL,
+                                                  g_object_unref);
+
   gdk_window_add_filter (NULL, filter_func, self);
 }
 
-WnckHandle
-*_wnck_handle_new (WnckClientType client_type)
+WnckHandle *
+_wnck_handle_new (WnckClientType client_type)
 {
   return g_object_new (WNCK_TYPE_HANDLE,
                        "client-type", client_type,
@@ -272,3 +304,30 @@ _wnck_handle_get_default_mini_icon_size (WnckHandle *self)
 {
   return self->default_mini_icon_size;
 }
+
+
+void
+_wnck_handle_insert_class_group (WnckHandle     *self,
+                                 const char     *id,
+                                 WnckClassGroup *class_group)
+{
+  g_return_if_fail (id != NULL);
+
+  g_hash_table_insert (self->class_group_hash, (gpointer) id, class_group);
+}
+
+void
+_wnck_handle_remove_class_group (WnckHandle *self,
+                                 const char *id)
+{
+  g_hash_table_remove (self->class_group_hash, id);
+}
+
+WnckClassGroup *
+_wnck_handle_get_class_group (WnckHandle *self,
+                              const char *id)
+{
+  g_return_val_if_fail (WNCK_IS_HANDLE (self), NULL);
+
+  return g_hash_table_lookup (self->class_group_hash, id ? id : "");
+}


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