[xdg-desktop-portal-gnome/gbsneto/window-restore: 2/6] shellintrospect: Use GPtrArray to hold windows




commit e6c901a4f1445c240fda420e788d7da6a7bc517c
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Thu Aug 4 22:06:28 2022 -0300

    shellintrospect: Use GPtrArray to hold windows
    
    GPtrArray is generally nicer to use, so let's use it.

 src/screencast.c       |  7 ++++---
 src/screencastwidget.c |  7 +++----
 src/shellintrospect.c  | 23 +++++++++++------------
 src/shellintrospect.h  |  2 +-
 4 files changed, 19 insertions(+), 20 deletions(-)
---
diff --git a/src/screencast.c b/src/screencast.c
index 4f76a3f..5cbe9cc 100644
--- a/src/screencast.c
+++ b/src/screencast.c
@@ -436,16 +436,17 @@ find_best_window_by_app_id_and_title (const char *app_id,
                                       const char *title)
 {
   ShellIntrospect *shell_introspect = shell_introspect_get ();
+  GPtrArray *windows;
   Window *best_match;
   glong best_match_distance;
-  GList *l;
 
   best_match = NULL;
   best_match_distance = G_MAXLONG;
 
-  for (l = shell_introspect_get_windows (shell_introspect); l; l = l->next)
+  windows = shell_introspect_get_windows (shell_introspect);
+  for (size_t i = 0; windows && i < windows->len; i++)
     {
-      Window *window = l->data;
+      Window *window = g_ptr_array_index (windows, i);
       glong distance;
 
       if (g_strcmp0 (window_get_app_id (window), app_id) != 0)
diff --git a/src/screencastwidget.c b/src/screencastwidget.c
index 0525644..ff33097 100644
--- a/src/screencastwidget.c
+++ b/src/screencastwidget.c
@@ -189,8 +189,7 @@ update_windows_list (ScreenCastWidget *widget)
   GtkListBox *window_list = GTK_LIST_BOX (widget->window_list);
   GtkWidget *toplevel;
   GtkWidget *child;
-  GList *windows;
-  GList *l;
+  GPtrArray *windows;
 
   while ((child = gtk_widget_get_first_child (GTK_WIDGET (window_list))) != NULL)
     gtk_list_box_remove (window_list, child);
@@ -200,9 +199,9 @@ update_windows_list (ScreenCastWidget *widget)
     return;
 
   windows = shell_introspect_get_windows (widget->shell_introspect);
-  for (l = windows; l; l = l->next)
+  for (size_t i = 0; windows && i < windows->len; i++)
     {
-      Window *window = l->data;
+      Window *window = g_ptr_array_index (windows, i);
       GtkWidget *window_widget;
 
       if (should_skip_window (window, GTK_WINDOW (toplevel)))
diff --git a/src/shellintrospect.c b/src/shellintrospect.c
index 5c56956..5bbc076 100644
--- a/src/shellintrospect.c
+++ b/src/shellintrospect.c
@@ -40,7 +40,7 @@ struct _ShellIntrospect
 
   unsigned int version;
 
-  GList *windows;
+  GPtrArray *windows;
 
   int num_listeners;
 
@@ -76,15 +76,14 @@ get_windows_cb (GObject *source_object,
                 gpointer user_data)
 {
   ShellIntrospect *shell_introspect = user_data;
+  g_autoptr(GPtrArray) windows = NULL;
   g_autoptr(GVariant) windows_variant = NULL;
   g_autoptr(GError) error = NULL;
   GVariantIter iter;
   uint64_t id;
   GVariant *params = NULL;
-  GList *windows = NULL;
 
-  g_list_free_full (shell_introspect->windows, (GDestroyNotify) window_free);
-  shell_introspect->windows = NULL;
+  g_clear_pointer (&shell_introspect->windows, g_ptr_array_unref);
 
   if (!org_gnome_shell_introspect_call_get_windows_finish (shell_introspect->proxy,
                                                            &windows_variant,
@@ -96,6 +95,10 @@ get_windows_cb (GObject *source_object,
     }
 
   g_variant_iter_init (&iter, windows_variant);
+
+  windows = g_ptr_array_new_full (g_variant_iter_n_children (&iter),
+                                  (GDestroyNotify) window_free);
+
   while (g_variant_iter_loop (&iter, "{t@a{sv}}", &id, &params))
     {
       char *app_id = NULL;
@@ -113,12 +116,12 @@ get_windows_cb (GObject *source_object,
         .title = title,
         .app_id = app_id
       };
-      windows = g_list_prepend (windows, window);
+      g_ptr_array_add (windows, window);
 
       g_clear_pointer (&params, g_variant_unref);
     }
 
-  shell_introspect->windows = windows;
+  shell_introspect->windows = g_steal_pointer (&windows);
   g_signal_emit (shell_introspect, signals[WINDOWS_CHANGED], 0);
 }
 
@@ -264,7 +267,7 @@ shell_introspect_get (void)
   return shell_introspect;
 }
 
-GList *
+GPtrArray *
 shell_introspect_get_windows (ShellIntrospect *shell_introspect)
 {
   return shell_introspect->windows;
@@ -286,11 +289,7 @@ shell_introspect_unref_listeners (ShellIntrospect *shell_introspect)
 
   shell_introspect->num_listeners--;
   if (shell_introspect->num_listeners == 0)
-    {
-      g_list_free_full (shell_introspect->windows,
-                        (GDestroyNotify) window_free);
-      shell_introspect->windows = NULL;
-    }
+    g_clear_pointer (&shell_introspect->windows, g_ptr_array_unref);
 }
 
 const char *
diff --git a/src/shellintrospect.h b/src/shellintrospect.h
index 0900b66..a783d55 100644
--- a/src/shellintrospect.h
+++ b/src/shellintrospect.h
@@ -40,7 +40,7 @@ const char * window_get_title (Window *window);
 
 const uint64_t window_get_id (Window *window);
 
-GList * shell_introspect_get_windows (ShellIntrospect *shell_introspect);
+GPtrArray * shell_introspect_get_windows (ShellIntrospect *shell_introspect);
 
 gboolean shell_introspect_are_animations_enabled (ShellIntrospect *introspect,
                                                   gboolean        *enable_animations);


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