[gtk/wip/otte/monitors: 4/5] x11: Use a GListStore for the monitors



commit bbabe71b2dbfbdc0d02a0a30b3b75c6253dda494
Author: Benjamin Otte <otte redhat com>
Date:   Tue May 12 19:46:40 2020 +0200

    x11: Use a GListStore for the monitors

 gdk/x11/gdkdisplay-x11.c | 37 ++++++++++++++++-------
 gdk/x11/gdkdisplay-x11.h |  2 +-
 gdk/x11/gdkscreen-x11.c  | 78 +++++++++++++++++++++++++++++-------------------
 gdk/x11/gdkx11display.h  |  2 +-
 4 files changed, 75 insertions(+), 44 deletions(-)
---
diff --git a/gdk/x11/gdkdisplay-x11.c b/gdk/x11/gdkdisplay-x11.c
index 6f90f0de78..16001a7e4a 100644
--- a/gdk/x11/gdkdisplay-x11.c
+++ b/gdk/x11/gdkdisplay-x11.c
@@ -209,9 +209,9 @@ G_DEFINE_TYPE_WITH_CODE (GdkX11Display, gdk_x11_display, GDK_TYPE_DISPLAY,
                                                 gdk_x11_display_event_translator_init))
 
 static void
-gdk_x11_display_init (GdkX11Display *display)
+gdk_x11_display_init (GdkX11Display *self)
 {
-  display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
+  self->monitors = g_list_store_new (GDK_TYPE_MONITOR);
 }
 
 static void
@@ -1950,7 +1950,8 @@ gdk_x11_display_finalize (GObject *object)
   g_object_unref (display_x11->screen);
   g_list_free_full (display_x11->screens, g_object_unref);
 
-  g_ptr_array_free (display_x11->monitors, TRUE);
+  g_list_store_remove_all (display_x11->monitors);
+  g_object_unref (display_x11->monitors);
 
   g_free (display_x11->startup_notification_id);
 
@@ -2899,7 +2900,7 @@ gdk_x11_display_get_n_monitors (GdkDisplay *display)
 {
   GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
 
-  return x11_display->monitors->len;
+  return g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors));
 }
 
 
@@ -2908,16 +2909,23 @@ gdk_x11_display_get_monitor (GdkDisplay *display,
                              int         monitor_num)
 {
   GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
+  GdkMonitor *monitor;
 
-  if (0 <= monitor_num && monitor_num < x11_display->monitors->len)
-    return (GdkMonitor *)x11_display->monitors->pdata[monitor_num];
+  if (monitor_num < 0)
+    return NULL;
+
+  monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), monitor_num);
+  if (monitor)
+    g_object_unref (monitor);
+  
+  return monitor;
 
   return NULL;
 }
 
 /**
  * gdk_x11_display_get_primary_monitor:
- * @display: a #GdkDisplay
+ * @self: a #GdkDisplay
  *
  * Gets the primary monitor for the display.
  *
@@ -2935,12 +2943,19 @@ gdk_x11_display_get_monitor (GdkDisplay *display,
 GdkMonitor *
 gdk_x11_display_get_primary_monitor (GdkDisplay *display)
 {
-  GdkX11Display *x11_display = GDK_X11_DISPLAY (display);
+  GdkX11Display *self = GDK_X11_DISPLAY (display);
+  GdkMonitor *monitor;
 
-  if (0 <= x11_display->primary_monitor && x11_display->primary_monitor < x11_display->monitors->len)
-    return x11_display->monitors->pdata[x11_display->primary_monitor];
+  if (0 <= self->primary_monitor)
+    return NULL;
 
-  return NULL;
+  monitor = g_list_model_get_item (G_LIST_MODEL (self->monitors), self->primary_monitor);
+  if (monitor == NULL)
+    return NULL;
+
+  /* because g_list_model_get_item() returns a ref */
+  g_object_unref (monitor);
+  return monitor;
 }
 
 int
diff --git a/gdk/x11/gdkdisplay-x11.h b/gdk/x11/gdkdisplay-x11.h
index fa688f0aca..e3b9305c2f 100644
--- a/gdk/x11/gdkdisplay-x11.h
+++ b/gdk/x11/gdkdisplay-x11.h
@@ -114,7 +114,7 @@ struct _GdkX11Display
   /* GdkCursor => XCursor */
   GHashTable *cursors;
 
-  GPtrArray *monitors;
+  GListStore *monitors;
   int primary_monitor;
 
   /* Startup notification */
diff --git a/gdk/x11/gdkscreen-x11.c b/gdk/x11/gdkscreen-x11.c
index 5419c72bb2..0f24eb4a01 100644
--- a/gdk/x11/gdkscreen-x11.c
+++ b/gdk/x11/gdkscreen-x11.c
@@ -135,12 +135,16 @@ gdk_x11_screen_get_monitor_output (GdkX11Screen *x11_screen,
 {
   GdkX11Display *x11_display = GDK_X11_DISPLAY (x11_screen->display);
   GdkX11Monitor *monitor;
+  XID output;
 
   g_return_val_if_fail (monitor_num >= 0, None);
-  g_return_val_if_fail (monitor_num < x11_display->monitors->len, None);
+  g_return_val_if_fail (monitor_num < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)), None);
 
-  monitor = x11_display->monitors->pdata[monitor_num];
-  return monitor->output;
+  monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), monitor_num);
+  output = monitor->output;
+  g_object_unref (monitor);
+
+  return output;
 }
 
 static int
@@ -386,9 +390,10 @@ find_monitor_by_output (GdkX11Display *x11_display, XID output)
 {
   int i;
 
-  for (i = 0; i < x11_display->monitors->len; i++)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
+      g_object_unref (monitor);
       if (monitor->output == output)
         return monitor;
     }
@@ -439,11 +444,12 @@ init_randr15 (GdkX11Screen *x11_screen, gboolean *changed)
   if (!rr_monitors)
     return FALSE;
 
-  for (i = 0; i < x11_display->monitors->len; i++)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       monitor->add = FALSE;
       monitor->remove = TRUE;
+      g_object_unref (monitor);
     }
 
   for (i = 0; i < num_rr_monitors; i++)
@@ -503,7 +509,7 @@ init_randr15 (GdkX11Screen *x11_screen, gboolean *changed)
                                   NULL);
           monitor->output = output;
           monitor->add = TRUE;
-          g_ptr_array_add (x11_display->monitors, monitor);
+          g_list_store_append (x11_display->monitors, monitor);
         }
 
       /* Fetch minimal manufacturer information (PNP ID) from EDID */
@@ -591,9 +597,9 @@ init_randr15 (GdkX11Screen *x11_screen, gboolean *changed)
   XRRFreeMonitors (rr_monitors);
   XRRFreeScreenResources (resources);
 
-  for (i = x11_display->monitors->len - 1; i >= 0; i--)
+  for (i = g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)) - 1; i >= 0; i--)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       if (monitor->add)
         {
           gdk_display_monitor_added (display, GDK_MONITOR (monitor));
@@ -602,18 +608,20 @@ init_randr15 (GdkX11Screen *x11_screen, gboolean *changed)
       else if (monitor->remove)
         {
           g_object_ref (monitor);
-          g_ptr_array_remove (x11_display->monitors, monitor);
+          g_list_store_remove (x11_display->monitors, i);
           gdk_display_monitor_removed (display, GDK_MONITOR (monitor));
           g_object_unref (monitor);
           *changed = TRUE;
         }
+      g_object_unref (monitor);
     }
 
   old_primary = x11_display->primary_monitor;
   x11_display->primary_monitor = 0;
-  for (i = 0; i < x11_display->monitors->len; ++i)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
+      g_object_unref (monitor);
       if (monitor->output == primary_output)
         {
           x11_display->primary_monitor = i;
@@ -636,7 +644,7 @@ init_randr15 (GdkX11Screen *x11_screen, gboolean *changed)
   if (x11_display->primary_monitor != old_primary)
     *changed = TRUE;
 
-  return x11_display->monitors->len > 0;
+  return g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)) > 0;
 #endif
 
   return FALSE;
@@ -662,13 +670,15 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
   if (!resources)
     return FALSE;
 
-  for (i = 0; i < x11_display->monitors->len; i++)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       monitor->add = FALSE;
       monitor->remove = TRUE;
+      g_object_unref (monitor);
     }
 
+
   for (i = 0; i < resources->noutput; ++i)
     {
       RROutput output = resources->outputs[i];
@@ -712,7 +722,7 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
                                       NULL);
               monitor->output = output;
               monitor->add = TRUE;
-              g_ptr_array_add (x11_display->monitors, monitor);
+              g_list_store_append (x11_display->monitors, monitor);
             }
 
           gdk_monitor_get_geometry (GDK_MONITOR (monitor), &geometry);
@@ -758,9 +768,9 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
 
   /* Which usable multihead data is not returned in non RandR 1.2+ X driver? */
 
-  for (i = x11_display->monitors->len - 1; i >= 0; i--)
+  for (i = g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)) - 1; i >= 0; i--)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       if (monitor->add)
         {
           gdk_display_monitor_added (display, GDK_MONITOR (monitor));
@@ -769,7 +779,7 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
       else if (monitor->remove)
         {
           g_object_ref (monitor);
-          g_ptr_array_remove (x11_display->monitors, monitor);
+          g_list_store_remove (x11_display->monitors, i);
           gdk_display_monitor_removed (display, GDK_MONITOR (monitor));
           g_object_unref (monitor);
           *changed = TRUE;
@@ -781,9 +791,10 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
   primary_output = XRRGetOutputPrimary (x11_screen->xdisplay,
                                         x11_screen->xroot_window);
 
-  for (i = 0; i < x11_display->monitors->len; ++i)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkX11Monitor *monitor = x11_display->monitors->pdata[i];
+      GdkX11Monitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
+      g_object_unref (monitor);
       if (monitor->output == primary_output)
         {
           x11_display->primary_monitor = i;
@@ -806,7 +817,7 @@ init_randr13 (GdkX11Screen *x11_screen, gboolean *changed)
   if (x11_display->primary_monitor != old_primary)
     *changed = TRUE;
 
-  return x11_display->monitors->len > 0;
+  return g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)) > 0;
 #endif
 
   return FALSE;
@@ -822,13 +833,15 @@ init_no_multihead (GdkX11Screen *x11_screen, gboolean *changed)
   int width, height;
   int i;
 
-  for (i = 0; i < x11_display->monitors->len; i++)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      monitor = x11_display->monitors->pdata[i];
+      monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       monitor->add = FALSE;
       monitor->remove = TRUE;
+      g_object_unref (monitor);
     }
 
+
   monitor = find_monitor_by_output (x11_display, 0);
   if (monitor)
     monitor->remove = FALSE;
@@ -839,7 +852,7 @@ init_no_multihead (GdkX11Screen *x11_screen, gboolean *changed)
                               NULL);
       monitor->output = 0;
       monitor->add = TRUE;
-      g_ptr_array_add (x11_display->monitors, monitor);
+      g_list_store_append (x11_display->monitors, monitor);
     }
 
   width_mm = WidthMMOfScreen (x11_screen->xscreen);
@@ -866,9 +879,9 @@ init_no_multihead (GdkX11Screen *x11_screen, gboolean *changed)
     *changed = TRUE;
   x11_display->primary_monitor = 0;
 
-  for (i = x11_display->monitors->len - 1; i >= 0; i--)
+  for (i = g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)) - 1; i >= 0; i--)
     {
-      monitor = x11_display->monitors->pdata[i];
+      monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
       if (monitor->add)
         {
           gdk_display_monitor_added (GDK_DISPLAY (x11_display), GDK_MONITOR (monitor));
@@ -877,11 +890,12 @@ init_no_multihead (GdkX11Screen *x11_screen, gboolean *changed)
       else if (monitor->remove)
         {
           g_object_ref (monitor);
-          g_ptr_array_remove (x11_display->monitors, monitor);
+          g_list_store_remove (x11_display->monitors, i);
           gdk_display_monitor_removed (GDK_DISPLAY (x11_display), GDK_MONITOR (monitor));
           g_object_unref (monitor);
           *changed = TRUE;
         }
+      g_object_unref (monitor);
     }
 }
 
@@ -958,11 +972,13 @@ _gdk_x11_screen_set_surface_scale (GdkX11Screen *x11_screen,
       _gdk_x11_surface_set_surface_scale (surface, scale);
     }
 
-  for (i = 0; i < x11_display->monitors->len; i++)
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (x11_display->monitors)); i++)
     {
-      GdkMonitor *monitor = GDK_MONITOR (x11_display->monitors->pdata[i]);
+      GdkMonitor *monitor = g_list_model_get_item (G_LIST_MODEL (x11_display->monitors), i);
 
       gdk_monitor_set_scale_factor (monitor, scale);
+
+      g_object_unref (monitor);
     }
 
   /* We re-read the monitor sizes so we can apply the new scale */
diff --git a/gdk/x11/gdkx11display.h b/gdk/x11/gdkx11display.h
index 9a24a4a035..676886d34a 100644
--- a/gdk/x11/gdkx11display.h
+++ b/gdk/x11/gdkx11display.h
@@ -106,7 +106,7 @@ GDK_AVAILABLE_IN_ALL
 GdkX11Screen *gdk_x11_display_get_screen (GdkDisplay *display);
 
 GDK_AVAILABLE_IN_ALL
-GdkMonitor * gdk_x11_display_get_primary_monitor (GdkDisplay *display);
+GdkMonitor * gdk_x11_display_get_primary_monitor (GdkDisplay *self);
 
 GDK_AVAILABLE_IN_ALL
 void        gdk_x11_display_grab              (GdkDisplay *display);


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