[Xfce4-commits] <xfce4-settings:xrandr-display-settings> Only store connected outputs

Jérôme Guelfucci noreply at xfce.org
Fri Jun 25 23:58:01 CEST 2010


Updating branch refs/heads/xrandr-display-settings
         to 576a0241156e2063406aacb0ea85e5d82f26b57c (commit)
       from 906545a1b55fe46edc1546c83e2192c9a97199d9 (commit)

commit 576a0241156e2063406aacb0ea85e5d82f26b57c
Author: Lionel Le Folgoc <mrpouit at gmail.com>
Date:   Fri Jun 25 20:42:28 2010 +0200

    Only store connected outputs
    
    This will avoid lots of loops to get the number of connected outputs, and
    repeated tests to check that an output is connected before using it.

 dialogs/display-settings/main.c       |  113 +++++---------------
 dialogs/display-settings/xfce-randr.c |  188 +++++++++++++++-----------------
 dialogs/display-settings/xfce-randr.h |   12 ++-
 3 files changed, 122 insertions(+), 191 deletions(-)

diff --git a/dialogs/display-settings/main.c b/dialogs/display-settings/main.c
index 3e10ff0..b390b71 100644
--- a/dialogs/display-settings/main.c
+++ b/dialogs/display-settings/main.c
@@ -137,29 +137,6 @@ display_setting_combo_box_get_value (GtkComboBox *combobox,
 
 
 
-static gint
-display_setting_get_n_active_outputs (void)
-{
-    gint noutput;
-
-    noutput = 0;
-
-    if (xfce_randr)
-    {
-        gint i;
-
-        for (i = 0; i < xfce_randr->resources->noutput; i++)
-        {
-            if (xfce_randr->status[i] != XFCE_OUTPUT_STATUS_NONE)
-                noutput++;
-        }
-    }
-
-    return noutput;
-}
-
-
-
 #ifdef HAS_RANDR_ONE_POINT_TWO
 static void
 display_setting_reflections_changed (GtkComboBox *combobox,
@@ -528,7 +505,7 @@ display_setting_output_toggled (GtkToggleButton *togglebutton,
     if (!xfce_randr)
         return;
 
-    if (display_setting_get_n_active_outputs () > 1)
+    if (xfce_randr->noutput > 1)
     {
         GObject *radio;
         radio = gtk_builder_get_object (builder, "randr-on");
@@ -560,7 +537,7 @@ display_setting_output_status_populate (GtkBuilder *builder)
     if (!xfce_randr)
         return;
 
-    if (display_setting_get_n_active_outputs () <= 1)
+    if (xfce_randr->noutput <= 1)
         return;
 
     radio_on = gtk_builder_get_object (builder, "randr-on");
@@ -619,6 +596,9 @@ display_settings_treeview_selection_changed (GtkTreeSelection *selection,
 static void
 display_settings_treeview_populate (GtkBuilder *builder)
 {
+#ifdef HAS_RANDR_ONE_POINT_TWO
+    guint             m;
+#endif
     gint              n;
     GtkListStore     *store;
     GObject          *treeview;
@@ -650,39 +630,35 @@ display_settings_treeview_populate (GtkBuilder *builder)
 #ifdef HAS_RANDR_ONE_POINT_TWO
     if (xfce_randr)
     {
-        /* walk all the outputs */
-        for (n = 0; n < xfce_randr->resources->noutput; n++)
+        /* walk all the connected outputs */
+        for (m = 0; m < xfce_randr->noutput; ++m)
         {
-            /* only show screen that are primary or secondary */
-            if (xfce_randr->status[n] == XFCE_OUTPUT_STATUS_NONE)
-                continue;
-
             /* get a friendly name for the output */
             name = xfce_randr_friendly_name (xfce_randr,
-                                             xfce_randr->resources->outputs[n],
-                                             xfce_randr->output_info[n]->name);
+                                             xfce_randr->resources->outputs[m],
+                                             xfce_randr->output_info[m]->name);
 
-            if (xfce_randr->mode[n] == None && lucent_display_icon == NULL)
+            if (xfce_randr->mode[m] == None && lucent_display_icon == NULL)
                 lucent_display_icon =
                     exo_gdk_pixbuf_lucent (display_icon, 50);
 
             /* insert the output in the store */
             gtk_list_store_append (store, &iter);
-            if (xfce_randr->mode[n] == None)
+            if (xfce_randr->mode[m] == None)
                 gtk_list_store_set (store, &iter,
                                     COLUMN_OUTPUT_NAME, name,
                                     COLUMN_OUTPUT_ICON, lucent_display_icon,
-                                    COLUMN_OUTPUT_ID, n, -1);
+                                    COLUMN_OUTPUT_ID, m, -1);
             else
                 gtk_list_store_set (store, &iter,
                                     COLUMN_OUTPUT_NAME, name,
                                     COLUMN_OUTPUT_ICON, display_icon,
-                                    COLUMN_OUTPUT_ID, n, -1);
+                                    COLUMN_OUTPUT_ID, m, -1);
 
             g_free (name);
 
             /* select active output */
-            if (n == xfce_randr->active_output)
+            if (m == xfce_randr->active_output)
                 gtk_tree_selection_select_iter (selection, &iter);
         }
     }
@@ -813,7 +789,7 @@ display_settings_dialog_new (GtkBuilder *builder)
     if (xfce_randr != NULL)
     {
         radio = gtk_builder_get_object (builder, "randr-on");
-        if (display_setting_get_n_active_outputs () > 1)
+        if (xfce_randr->noutput > 1)
         {
             gtk_widget_show (GTK_WIDGET (radio));
             g_signal_connect (G_OBJECT (radio), "toggled", G_CALLBACK (display_setting_output_toggled), builder);
@@ -824,7 +800,7 @@ display_settings_dialog_new (GtkBuilder *builder)
         }
 
         radio = gtk_builder_get_object (builder, "randr-off");
-        if (display_setting_get_n_active_outputs () > 1)
+        if (xfce_randr->noutput > 1)
         {
             gtk_widget_show (GTK_WIDGET (radio));
             g_signal_connect (G_OBJECT (radio), "toggled", G_CALLBACK (display_setting_output_toggled), builder);
@@ -902,27 +878,14 @@ display_settings_minimal_dialog_response (GtkDialog  *dialog,
     gboolean    use_first_screen;
     gboolean    use_second_screen;
     gboolean    use_both;
-    gint        first, second;
+    guint       first, second;
     gint        m, n, found;
 
     if (response_id == 1)
     {
-        /* OK */
-
-        first = second = -1;
-
-        for (n = 0; n < xfce_randr->resources->noutput; n++)
-        {
-            if (xfce_randr->status[n] != XFCE_OUTPUT_STATUS_NONE)
-            {
-                if (first < 0)
-                    first = n;
-                else if (second < 0)
-                    second = n;
-                else
-                    break;
-            }
-        }
+        /* TODO: handle correctly more than 2 outputs? */
+        first = 0;
+        second = 1;
 
         first_screen_radio = gtk_builder_get_object (builder, "radiobutton1");
         second_screen_radio = gtk_builder_get_object (builder, "radiobutton2");
@@ -999,7 +962,7 @@ main (gint argc, gchar **argv)
     GdkDisplay *display;
     gboolean    succeeded = TRUE;
     gint        event_base, error_base;
-    guint       ui_ret;
+    guint       first, second;
 
     /* setup translation domain */
     xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
@@ -1157,39 +1120,17 @@ main (gint argc, gchar **argv)
         }
         else
         {
-            gint n;
-            gint first, second;
-
-            if (xfce_randr->resources->noutput < 2 || xfce_randr == NULL)
+            if (xfce_randr->noutput < 2 || xfce_randr == NULL)
                 goto err1;
 
-            first = second = -1;
-
-            for (n = 0; n < xfce_randr->resources->noutput; n++)
-            {
-                if (xfce_randr->status[n] != XFCE_OUTPUT_STATUS_NONE)
-                {
-                    if (first < 0)
-                        first = n;
-                    else if (second < 0)
-                        second = n;
-                    else
-                        break;
-                }
-            }
-
-            if (first < 0 || second < 0)
-                goto err1;
+            /* TODO: handle correctly more than 2 outputs? */
+            first = 0;
+            second = 1;
 
             builder = gtk_builder_new ();
 
-            ui_ret =
-                gtk_builder_add_from_string (builder,
-                                             minimal_display_dialog_ui,
-                                             minimal_display_dialog_ui_length,
-                                             &error);
-
-            if (ui_ret != 0)
+            if (gtk_builder_add_from_string (builder, minimal_display_dialog_ui,
+                                             minimal_display_dialog_ui_length, &error) != 0)
             {
                 GObject    *first_screen_radio;
                 GObject    *second_screen_radio;
diff --git a/dialogs/display-settings/xfce-randr.c b/dialogs/display-settings/xfce-randr.c
index c855bc6..ae87c30 100644
--- a/dialogs/display-settings/xfce-randr.c
+++ b/dialogs/display-settings/xfce-randr.c
@@ -40,24 +40,21 @@ static void
 xfce_randr_list_clone_modes (XfceRandr *randr)
 {
     GArray *clone_modes;
-    gint    l, m, n, candidate, found;
-    guint   i;
+    gint    l, n, candidate, found;
+    guint   m;
 
-    clone_modes = g_array_new (FALSE, FALSE, sizeof (RRMode));
+    clone_modes = g_array_new (TRUE, FALSE, sizeof (RRMode));
 
     /* walk all available modes */
-    for (n = 0; n < randr->resources->nmode; n++)
+    for (n = 0; n < randr->resources->nmode; ++n)
     {
         candidate = TRUE;
-        /* walk all active outputs */
-        for (m = 0; m < randr->resources->noutput; m++)
+        /* walk all connected outputs */
+        for (m = 0; m < randr->noutput; ++m)
         {
-            if (randr->status[m] == XFCE_OUTPUT_STATUS_NONE)
-                continue;
-
             found = FALSE;
             /* walk supported modes from this output */
-            for (l = 0; l < randr->output_info[m]->nmode; l++)
+            for (l = 0; l < randr->output_info[m]->nmode; ++l)
             {
                 if (randr->resources->modes[n].id == randr->output_info[m]->modes[l])
                 {
@@ -76,12 +73,7 @@ xfce_randr_list_clone_modes (XfceRandr *randr)
     }
 
     /* return a "normal" array (last value -> None) */
-    randr->clone_modes = g_new0 (RRMode, clone_modes->len + 1);
-
-    for (i = 0; i < clone_modes->len; i++)
-        randr->clone_modes[i] = g_array_index (clone_modes, RRMode, i);
-
-    g_array_free (clone_modes, TRUE);
+    randr->clone_modes = (RRMode *) g_array_free (clone_modes, FALSE);
 }
 
 
@@ -89,28 +81,17 @@ xfce_randr_list_clone_modes (XfceRandr *randr)
 static Rotation
 xfce_randr_get_safe_rotations (XfceRandr *randr,
                                Display   *xdisplay,
-                               GdkWindow *root_window,
-                               gint       num_output)
+                               guint      num_output)
 {
-    XRRScreenConfiguration *screen_config;
-    XRRCrtcInfo            *crtc_info;
-    gint                    n;
-    Rotation                dummy, rot;
+    XRRCrtcInfo *crtc_info;
+    Rotation     rot;
+    gint         n;
 
-    g_return_val_if_fail (num_output >= 0
-                          && num_output < randr->resources->noutput,
-                          RR_Rotate_0);
-
-    if (randr->output_info[num_output]->ncrtc < 1)
-    {
-        screen_config = XRRGetScreenInfo (xdisplay, GDK_WINDOW_XID (root_window));
-        rot = XRRConfigRotations (screen_config, &dummy);
-        XRRFreeScreenConfigInfo (screen_config);
-        return rot;
-    }
+    g_return_val_if_fail (num_output < randr->noutput, RR_Rotate_0);
+    g_return_val_if_fail (randr->output_info[num_output]->ncrtc > 0, RR_Rotate_0);
 
     rot = XFCE_RANDR_ROTATIONS_MASK | XFCE_RANDR_REFLECTIONS_MASK;
-    for (n = 0; n < randr->output_info[num_output]->ncrtc; n++)
+    for (n = 0; n < randr->output_info[num_output]->ncrtc; ++n)
     {
         crtc_info = XRRGetCrtcInfo (xdisplay, randr->resources,
                                     randr->output_info[num_output]->crtcs[n]);
@@ -167,76 +148,88 @@ xfce_randr_populate (XfceRandr *randr,
                      Display   *xdisplay,
                      GdkWindow *root_window)
 {
-    XRRCrtcInfo            *crtc_info;
-    gint                    n;
+    GPtrArray     *outputs;
+    XRROutputInfo *output_info;
+    XRRCrtcInfo   *crtc_info;
+    gint           n;
+    guint          m;
 
     g_return_val_if_fail (randr != NULL, FALSE);
     g_return_val_if_fail (randr->resources != NULL, FALSE);
 
-    /* allocate space for the settings */
-    randr->mode = g_new0 (RRMode, randr->resources->noutput);
-    randr->modes = g_new0 (XfceRRMode *, randr->resources->noutput);
-    randr->rotation = g_new0 (Rotation, randr->resources->noutput);
-    randr->rotations = g_new0 (Rotation, randr->resources->noutput);
-    randr->position = g_new0 (XfceOutputPosition, randr->resources->noutput);
-    randr->status = g_new0 (XfceOutputStatus, randr->resources->noutput);
-    randr->output_info = g_new0 (XRROutputInfo *, randr->resources->noutput);
+    /* prepare the temporary cache */
+    outputs = g_ptr_array_new_with_free_func ((GDestroyNotify) XRRFreeOutputInfo);
 
     /* walk the outputs */
-    for (n = 0; n < randr->resources->noutput; n++)
+    for (n = 0; n < randr->resources->noutput; ++n)
     {
-        /* reset */
-        randr->status[n] = XFCE_OUTPUT_STATUS_NONE;
-
         /* get the output info */
-        randr->output_info[n] = XRRGetOutputInfo (xdisplay, randr->resources, randr->resources->outputs[n]);
+        output_info = XRRGetOutputInfo (xdisplay, randr->resources,
+                                        randr->resources->outputs[n]);
 
-        /* check if the device is really a randr 1.2 device */
-        if (n == 0 && strcmp (randr->output_info[n]->name, "default") == 0)
+        /* forget about disconnected outputs */
+        if (output_info->connection != RR_Connected)
         {
-            /* make sure we don't free the not yet allocated outputs */
-            for (n++; n < randr->resources->noutput; n++)
-            {
-                randr->output_info[n] = NULL;
-                randr->modes[n] = NULL;
-            }
+            XRRFreeOutputInfo (output_info);
+            continue;
+        }
 
+        /* cache it */
+        g_ptr_array_add (outputs, output_info);
+
+        /* check if the device is really a randr 1.2 device */
+        if (n == 0 && strcmp (output_info->name, "default") == 0)
+        {
+            /* free the cache */
+            g_ptr_array_unref (outputs);
             return FALSE;
         }
+    }
+
+    /* migrate the temporary cache */
+    randr->noutput = outputs->len;
+    randr->output_info = (XRROutputInfo **) g_ptr_array_free (outputs, FALSE);
 
+    /* allocate final space for the settings */
+    randr->mode = g_new0 (RRMode, randr->noutput);
+    randr->modes = g_new0 (XfceRRMode *, randr->noutput);
+    randr->rotation = g_new0 (Rotation, randr->noutput);
+    randr->rotations = g_new0 (Rotation, randr->noutput);
+    randr->position = g_new0 (XfceOutputPosition, randr->noutput);
+    randr->status = g_new0 (XfceOutputStatus, randr->noutput);
+
+    /* walk the connected outputs */
+    for (m = 0; m < randr->noutput; ++m)
+    {
         /* fill in supported modes */
-        randr->modes[n] = xfce_randr_list_supported_modes (randr->resources, randr->output_info[n]);
+        randr->modes[m] = xfce_randr_list_supported_modes (randr->resources, randr->output_info[m]);
 
-        /* do not query disconnected outputs */
-        if (randr->output_info[n]->connection == RR_Connected)
-        {
 #ifdef HAS_RANDR_ONE_POINT_THREE
-            /* find the primary screen if supported */
-            if (randr->has_1_3 && XRRGetOutputPrimary (xdisplay, GDK_WINDOW_XID (root_window)) == randr->resources->outputs[n])
-                randr->status[n] = XFCE_OUTPUT_STATUS_PRIMARY;
-            else
+        /* find the primary screen if supported */
+        if (randr->has_1_3 && XRRGetOutputPrimary (xdisplay, GDK_WINDOW_XID (root_window)) == randr->resources->outputs[m])
+            randr->status[m] = XFCE_OUTPUT_STATUS_PRIMARY;
+        else
 #endif
-                randr->status[n] = XFCE_OUTPUT_STATUS_SECONDARY;
+            randr->status[m] = XFCE_OUTPUT_STATUS_SECONDARY;
 
-            if (randr->output_info[n]->crtc != None)
-            {
-                crtc_info = XRRGetCrtcInfo (xdisplay, randr->resources,
-                                            randr->output_info[n]->crtc);
-                randr->mode[n] = crtc_info->mode;
-                randr->rotation[n] = crtc_info->rotation;
-                randr->rotations[n] = crtc_info->rotations;
-                randr->position[n].x = crtc_info->x;
-                randr->position[n].y = crtc_info->y;
-                XRRFreeCrtcInfo (crtc_info);
-                continue;
-            }
+        if (randr->output_info[m]->crtc != None)
+        {
+            crtc_info = XRRGetCrtcInfo (xdisplay, randr->resources,
+                                        randr->output_info[m]->crtc);
+            randr->mode[m] = crtc_info->mode;
+            randr->rotation[m] = crtc_info->rotation;
+            randr->rotations[m] = crtc_info->rotations;
+            randr->position[m].x = crtc_info->x;
+            randr->position[m].y = crtc_info->y;
+            XRRFreeCrtcInfo (crtc_info);
+        }
+        else
+        {
+            /* output disabled */
+            randr->mode[m] = None;
+            randr->rotation[m] = RR_Rotate_0;
+            randr->rotations[m] = xfce_randr_get_safe_rotations (randr, xdisplay, m);
         }
-
-        /* output either disabled or disconnected */
-        randr->mode[n] = None;
-        randr->rotation[n] = RR_Rotate_0;
-        randr->rotations[n] = xfce_randr_get_safe_rotations (randr, xdisplay,
-                                                             root_window, n);
     }
 
     /* clone modes: same RRModes present for all outputs */
@@ -309,10 +302,10 @@ xfce_randr_new (GdkDisplay  *display,
 static void
 xfce_randr_cleanup (XfceRandr *randr)
 {
-    gint n;
+    guint n;
 
     /* free the output/mode info cache */
-    for (n = 0; n < randr->resources->noutput; n++)
+    for (n = 0; n < randr->noutput; ++n)
     {
         if (G_LIKELY (randr->output_info[n]))
             XRRFreeOutputInfo (randr->output_info[n]);
@@ -382,7 +375,7 @@ static void
 xfce_randr_save_device (XfceRandr     *randr,
                         const gchar   *scheme,
                         XfconfChannel *channel,
-                        gint           output,
+                        guint          output,
                         const gchar   *distinct)
 {
     gchar        property[512];
@@ -482,7 +475,7 @@ xfce_randr_save (XfceRandr     *randr,
                  XfconfChannel *channel)
 {
     gchar        property[512];
-    gint         n, num_outputs = 0;
+    guint        n;
 
     g_return_if_fail (XFCONF_IS_CHANNEL (channel));
 
@@ -490,20 +483,16 @@ xfce_randr_save (XfceRandr     *randr,
     g_snprintf (property, sizeof (property), "/%s/Layout", scheme);
     xfconf_channel_set_string (channel, property, "Outputs");
 
-    /* parse all outputs */
-    for (n = 0; n < randr->resources->noutput; n++)
+    /* save connected outputs */
+    for (n = 0; n < randr->noutput; ++n)
     {
-        /* do not save disconnected ones */
-        if (randr->status[n] != XFCE_OUTPUT_STATUS_NONE)
-        {
-            g_snprintf (property, sizeof (property), "Output%d", num_outputs++);
-            xfce_randr_save_device (randr, scheme, channel, n, property);
-        }
+        g_snprintf (property, sizeof (property), "Output%u", n);
+        xfce_randr_save_device (randr, scheme, channel, n, property);
     }
 
     /* store the number of outputs saved */
     g_snprintf (property, sizeof (property), "/%s/NumOutputs", scheme);
-    xfconf_channel_set_int (channel, property, num_outputs);
+    xfconf_channel_set_int (channel, property, randr->noutput);
 
     /* tell the helper to apply this theme */
     xfconf_channel_set_string (channel, "/Schemes/Apply", scheme);
@@ -606,14 +595,13 @@ xfce_randr_friendly_name (XfceRandr   *randr,
 
 XfceRRMode *
 xfce_randr_find_mode_by_id (XfceRandr *randr,
-                            gint       output,
+                            guint      output,
                             RRMode     id)
 {
     gint n;
 
     g_return_val_if_fail (randr != NULL, NULL);
-    g_return_val_if_fail (output >= 0 && output < randr->resources->noutput,
-                          NULL);
+    g_return_val_if_fail (output < randr->noutput, NULL);
 
     if (id == None)
         return NULL;
diff --git a/dialogs/display-settings/xfce-randr.h b/dialogs/display-settings/xfce-randr.h
index d01a017..28c3732 100644
--- a/dialogs/display-settings/xfce-randr.h
+++ b/dialogs/display-settings/xfce-randr.h
@@ -55,7 +55,6 @@ typedef enum   _XfceOutputStatus   XfceOutputStatus;
 
 enum _XfceOutputStatus
 {
-    XFCE_OUTPUT_STATUS_NONE,
     XFCE_OUTPUT_STATUS_PRIMARY,
     XFCE_OUTPUT_STATUS_SECONDARY
 };
@@ -89,16 +88,19 @@ struct _XfceRandr
     XRRScreenResources  *resources;
 
     /* the active selected layout */
-    gint                 active_output;
+    guint                active_output;
+
+    /* number of connected outputs */
+    guint                noutput;
 
     /* cache for the output/mode info */
     XRROutputInfo      **output_info;
     XfceRRMode         **modes;
 
-    /* modes common to all enabled outputs */
+    /* modes common to all connected outputs */
     RRMode              *clone_modes;
 
-    /* selected settings for all outputs */
+    /* selected settings for all connected outputs */
     RRMode              *mode;
     Rotation            *rotation;
     Rotation            *rotations;
@@ -128,7 +130,7 @@ gchar      *xfce_randr_friendly_name   (XfceRandr     *randr,
                                         const gchar   *name);
 
 XfceRRMode *xfce_randr_find_mode_by_id (XfceRandr     *randr,
-                                        gint           output,
+                                        guint          output,
                                         RRMode         id);
 
 #endif /* !HAS_RANDR_ONE_POINT_TWO */



More information about the Xfce4-commits mailing list