[Xfce4-commits] <xfce4-settings:master> Add a wrapper structure for the supported modes per output

Nick Schermer noreply at xfce.org
Sat Aug 28 23:04:27 CEST 2010


Updating branch refs/heads/master
         to 2943f1733d08a754c26782e899e09f16b7b47366 (commit)
       from d2cedfa030128a51d5e289d71bd9ef378c4e4e33 (commit)

commit 2943f1733d08a754c26782e899e09f16b7b47366
Author: Lionel Le Folgoc <mrpouit at gmail.com>
Date:   Sat Jun 19 19:49:49 2010 +0200

    Add a wrapper structure for the supported modes per output
    
    Otherwise, one has to walk every time all outputs from randr->resources to get
    the ModeInfo for one output mode (XRROutputInfo only contains RRModes…).

 dialogs/display-settings/main.c       |   56 ++++++-----------
 dialogs/display-settings/xfce-randr.c |  107 ++++++++++++++++++++++++++-------
 dialogs/display-settings/xfce-randr.h |   19 +++++-
 3 files changed, 121 insertions(+), 61 deletions(-)

diff --git a/dialogs/display-settings/main.c b/dialogs/display-settings/main.c
index 4e9d1fe..c6b431f 100644
--- a/dialogs/display-settings/main.c
+++ b/dialogs/display-settings/main.c
@@ -432,13 +432,12 @@ display_setting_modes_changed (GtkComboBox *combobox,
 static void
 display_setting_modes_populate (GtkBuilder *builder)
 {
-    GtkTreeModel  *model;
-    GObject       *combobox;
-    gint           m, n;
-    gchar         *name;
-    GtkTreeIter    iter;
-    XRRModeInfo  *mode_info;
-    gfloat        rate;
+    GtkTreeModel *model;
+    GObject      *combobox;
+    gint          n;
+    gchar        *name;
+    GtkTreeIter   iter;
+    XfceRRMode   *modes;
 
     /* get the combo box store and clear it */
     combobox = gtk_builder_get_object (builder, "randr-mode");
@@ -457,36 +456,21 @@ display_setting_modes_populate (GtkBuilder *builder)
             gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
 
         /* walk all supported modes */
-        for (m = 0; m < XFCE_RANDR_OUTPUT_INFO (xfce_randr)->nmode; ++m)
+        modes = XFCE_RANDR_SUPPORTED_MODES (xfce_randr);
+        for (n = 0; n < XFCE_RANDR_OUTPUT_INFO (xfce_randr)->nmode; ++n)
         {
-            /* walk all the modes */
-            for (n = 0; n < xfce_randr->resources->nmode; ++n)
-            {
-                /* get the mode info */
-                mode_info = &xfce_randr->resources->modes[n];
+            /* insert the mode */
+            name = g_strdup_printf (_("%dx%d @ %.1f Hz"), modes[n].width,
+                                    modes[n].height, modes[n].rate);
+            gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+            gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+                                COLUMN_COMBO_NAME, name,
+                                COLUMN_COMBO_VALUE, modes[n].id, -1);
+            g_free (name);
 
-                /* check if this mode is supported by the output */
-                if (XFCE_RANDR_OUTPUT_INFO (xfce_randr)->modes[m] == mode_info->id)
-                {
-                    /* calculate the refresh rate */
-                    rate = (gfloat) mode_info->dotClock / ((gfloat) mode_info->hTotal * (gfloat) mode_info->vTotal);
-
-                    /* insert the mode */
-                    name = g_strdup_printf (_("%s @ %.1f Hz"), mode_info->name, rate);
-                    gtk_list_store_append (GTK_LIST_STORE (model), &iter);
-                    gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-                                        COLUMN_COMBO_NAME, name,
-                                        COLUMN_COMBO_VALUE, mode_info->id, -1);
-                    g_free (name);
-
-                    /* select the active mode */
-                    if (mode_info->id == XFCE_RANDR_MODE (xfce_randr))
-                        gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
-
-                    /* finished */
-                    break;
-                }
-            }
+            /* select the active mode */
+            if (modes[n].id == XFCE_RANDR_MODE (xfce_randr))
+                gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
         }
     }
     else
diff --git a/dialogs/display-settings/xfce-randr.c b/dialogs/display-settings/xfce-randr.c
index b2d4189..397bd45 100644
--- a/dialogs/display-settings/xfce-randr.c
+++ b/dialogs/display-settings/xfce-randr.c
@@ -37,7 +37,7 @@
 #ifdef HAS_RANDR_ONE_POINT_TWO
 
 static void
-xfce_randr_get_clone_modes (XfceRandr *randr)
+xfce_randr_list_clone_modes (XfceRandr *randr)
 {
     GArray *clone_modes;
     gint    l, m, n, candidate, found;
@@ -123,6 +123,70 @@ xfce_randr_get_safe_rotations (XfceRandr *randr,
 
 
 
+static XfceRRMode *
+xfce_randr_find_mode_by_id (XfceRandr *randr,
+                            gint       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);
+
+    if (id == None)
+        return NULL;
+
+    for (n = 0; n < randr->output_info[output]->nmode; ++n)
+    {
+        if (randr->modes[output][n].id == id)
+            return &randr->modes[output][n];
+    }
+
+    return NULL;
+}
+
+
+
+static XfceRRMode *
+xfce_randr_list_supported_modes (XRRScreenResources *resources,
+                                XRROutputInfo       *output_info)
+{
+    XfceRRMode *modes;
+    gint m, n;
+
+    g_return_val_if_fail (resources != NULL, NULL);
+    g_return_val_if_fail (output_info != NULL, NULL);
+
+    if (output_info->nmode == 0)
+        return NULL;
+
+    modes = g_new0 (XfceRRMode, output_info->nmode);
+
+    for (n = 0; n < output_info->nmode; ++n)
+    {
+        modes[n].id = output_info->modes[n];
+
+        /* we need to walk yet another list to get the mode info */
+        for (m = 0; m < resources->nmode; ++m)
+        {
+            if (output_info->modes[n] == resources->modes[m].id)
+            {
+                modes[n].width = resources->modes[m].width;
+                modes[n].height = resources->modes[m].height;
+                modes[n].rate = (gdouble) resources->modes[m].dotClock /
+                                ((gdouble) resources->modes[m].hTotal * (gdouble) resources->modes[m].vTotal);
+
+                break;
+            }
+        }
+    }
+
+    return modes;
+}
+
+
+
 static gboolean
 xfce_randr_populate (XfceRandr *randr,
                      Display   *xdisplay,
@@ -137,6 +201,7 @@ xfce_randr_populate (XfceRandr *randr,
     /* allocate space for the settings */
     randr->mode = g_new0 (RRMode, randr->resources->noutput);
     randr->preferred_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);
@@ -162,6 +227,9 @@ xfce_randr_populate (XfceRandr *randr,
             return FALSE;
         }
 
+        /* fill in supported modes */
+        randr->modes[n] = xfce_randr_list_supported_modes (randr->resources, randr->output_info[n]);
+
         /* do not query disconnected outputs */
         if (randr->output_info[n]->connection == RR_Connected)
         {
@@ -198,7 +266,7 @@ xfce_randr_populate (XfceRandr *randr,
     }
 
     /* clone modes: same RRModes present for all outputs */
-    xfce_randr_get_clone_modes (randr);
+    xfce_randr_list_clone_modes (randr);
 
     return TRUE;
 }
@@ -269,10 +337,14 @@ xfce_randr_cleanup (XfceRandr *randr)
 {
     gint n;
 
-    /* free the output info cache */
+    /* free the output/mode info cache */
     for (n = 0; n < randr->resources->noutput; n++)
+    {
         if (G_LIKELY (randr->output_info[n]))
             XRRFreeOutputInfo (randr->output_info[n]);
+        if (G_LIKELY (randr->modes[n]))
+            g_free (randr->modes[n]);
+    }
 
     /* free the screen resources */
     XRRFreeScreenResources (randr->resources);
@@ -280,6 +352,7 @@ xfce_randr_cleanup (XfceRandr *randr)
     /* free the settings */
     g_free (randr->clone_modes);
     g_free (randr->mode);
+    g_free (randr->modes);
     g_free (randr->preferred_mode);
     g_free (randr->rotation);
     g_free (randr->rotations);
@@ -340,25 +413,15 @@ xfce_randr_save_device (XfceRandr     *randr,
                         const gchar   *distinct)
 {
     gchar        property[512];
-    const gchar *resolution_name = NULL;
+    gchar       *resolution_name = NULL;
     const gchar *reflection_name = NULL;
-    gdouble      refresh_rate = 0.00;
-    XRRModeInfo *mode;
-    gint         n;
+    XfceRRMode  *mode;
     gint         degrees;
 
-    /* find the resolution name and refresh rate */
-    for (n = 0; n < randr->resources->nmode; n++)
-    {
-        if (randr->resources->modes[n].id == randr->mode[output])
-        {
-            mode = &randr->resources->modes[n];
-            resolution_name = mode->name;
-            refresh_rate = (gdouble) mode->dotClock / ((gdouble) mode->hTotal * (gdouble) mode->vTotal);
-
-            break;
-        }
-    }
+    /* find the resolution and refresh rate */
+    mode = xfce_randr_find_mode_by_id (randr, output, randr->mode[output]);
+    if (mode)
+        resolution_name = g_strdup_printf ("%dx%d", mode->width, mode->height);
 
     /* save the device name */
     g_snprintf (property, sizeof (property), "/%s/%s", scheme, distinct);
@@ -373,8 +436,8 @@ xfce_randr_save_device (XfceRandr     *randr,
 
     /* save the refresh rate */
     g_snprintf (property, sizeof (property), "/%s/%s/RefreshRate", scheme, distinct);
-    if (G_LIKELY (refresh_rate > 0.00))
-        xfconf_channel_set_double (channel, property, refresh_rate);
+    if (G_LIKELY (resolution_name != NULL))
+        xfconf_channel_set_double (channel, property, mode->rate);
     else
         xfconf_channel_reset_property (channel, property, FALSE);
 
@@ -434,6 +497,8 @@ xfce_randr_save_device (XfceRandr     *randr,
         g_snprintf (property, sizeof (property), "/%s/%s/Position/Y", scheme, distinct);
         xfconf_channel_set_int (channel, property, randr->position[output].y);
     }
+
+    g_free (resolution_name);
 }
 
 
diff --git a/dialogs/display-settings/xfce-randr.h b/dialogs/display-settings/xfce-randr.h
index 1ba6a49..3141799 100644
--- a/dialogs/display-settings/xfce-randr.h
+++ b/dialogs/display-settings/xfce-randr.h
@@ -27,6 +27,7 @@
 #define XFCE_RANDR_EVENT_BASE(randr)      (randr->event_base)
 #define XFCE_RANDR_MODE(randr)            (randr->mode[randr->active_output])
 #define XFCE_RANDR_PREFERRED_MODE(randr)  (randr->preferred_mode[randr->active_output])
+#define XFCE_RANDR_SUPPORTED_MODES(randr) (randr->modes[randr->active_output])
 #define XFCE_RANDR_ROTATION(randr)        (randr->rotation[randr->active_output])
 #define XFCE_RANDR_ROTATIONS(randr)       (randr->rotations[randr->active_output])
 #define XFCE_RANDR_OUTPUT_INFO(randr)     (randr->output_info[randr->active_output])
@@ -48,9 +49,10 @@
 #endif
 
 #ifdef HAS_RANDR_ONE_POINT_TWO
-typedef struct _XfceRandr                XfceRandr;
-typedef struct _XfceOutputPosition       XfceOutputPosition;
-typedef enum   _XfceOutputStatus         XfceOutputStatus;
+typedef struct _XfceRandr          XfceRandr;
+typedef struct _XfceOutputPosition XfceOutputPosition;
+typedef struct _XfceRRMode         XfceRRMode;
+typedef enum   _XfceOutputStatus   XfceOutputStatus;
 
 enum _XfceOutputStatus
 {
@@ -65,6 +67,14 @@ struct _XfceOutputPosition
     gint y;
 };
 
+struct _XfceRRMode
+{
+    RRMode  id;
+    guint   width;
+    guint   height;
+    gdouble rate;
+};
+
 struct _XfceRandr
 {
     /* xrandr 1.3 capable */
@@ -82,8 +92,9 @@ struct _XfceRandr
     /* the active selected layout */
     gint                 active_output;
 
-    /* cache for the output info */
+    /* cache for the output/mode info */
     XRROutputInfo      **output_info;
+    XfceRRMode         **modes;
 
     /* modes common to all enabled outputs */
     RRMode              *clone_modes;



More information about the Xfce4-commits mailing list