[Spice-devel] [PATCH] Simplify sorting of monitor configuration

Jonathon Jongsma jjongsma at redhat.com
Wed Oct 2 10:08:04 PDT 2013


Instead of sorting the entire array of monitor config structs and then iterating
both lists to find the resulting index, just sort an array of indices.
---
 gtk/channel-main.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/gtk/channel-main.c b/gtk/channel-main.c
index b342e97..c449e86 100644
--- a/gtk/channel-main.c
+++ b/gtk/channel-main.c
@@ -1002,47 +1002,44 @@ static void agent_msg_queue_many(SpiceMainChannel *channel, int type, const void
 
 static int monitors_cmp(const void *p1, const void *p2, gpointer user_data)
 {
-    const VDAgentMonConfig *m1 = p1;
-    const VDAgentMonConfig *m2 = p2;
+    const VDAgentMonConfig *monitors = user_data;
+    guint i = *(guint*)p1;
+    guint j = *(guint*)p2;
+    const VDAgentMonConfig *m1 = &monitors[i];
+    const VDAgentMonConfig *m2 = &monitors[j];
     double d1 = sqrt(m1->x * m1->x + m1->y * m1->y);
     double d2 = sqrt(m2->x * m2->x + m2->y * m2->y);
     int diff = d1 - d2;
 
-    return diff == 0 ? (char*)p1 - (char*)p2 : diff;
+    return diff == 0 ? i - j : diff;
 }
 
 static void monitors_align(VDAgentMonConfig *monitors, int nmonitors)
 {
     gint i, j, x = 0;
-    guint32 used = 0;
-    VDAgentMonConfig *sorted_monitors;
+    guint *sorted_monitors;
 
     if (nmonitors == 0)
         return;
 
     /* sort by distance from origin */
-    sorted_monitors = g_memdup(monitors, nmonitors * sizeof(VDAgentMonConfig));
-    g_qsort_with_data(sorted_monitors, nmonitors, sizeof(VDAgentMonConfig), monitors_cmp, NULL);
+    sorted_monitors = g_new0(guint, nmonitors);
+    for (i = 0; i < nmonitors; i++)
+        sorted_monitors[i] = i;
+    g_qsort_with_data(sorted_monitors, nmonitors, sizeof(guint), monitors_cmp, monitors);
 
     /* super-KISS ltr alignment, feel free to improve */
     for (i = 0; i < nmonitors; i++) {
-        /* Find where this monitor is in the sorted order */
-        for (j = 0; j < nmonitors; j++) {
-            /* Avoid using the same entry twice, this happens with older
-               virt-viewer versions which always set x and y to 0 */
-            if (used & (1 << j))
-                continue;
-            if (memcmp(&monitors[j], &sorted_monitors[i],
-                       sizeof(VDAgentMonConfig)) == 0)
-                break;
-        }
-        used |= 1 << j;
+        j = sorted_monitors[i];
+        g_assert(j < nmonitors);
         monitors[j].x = x;
         monitors[j].y = 0;
         x += monitors[j].width;
         if (monitors[j].width || monitors[j].height)
-            SPICE_DEBUG("#%d +%d+%d-%dx%d", j, monitors[j].x, monitors[j].y,
-                        monitors[j].width, monitors[j].height);
+            SPICE_DEBUG("monitor config: #%d %dx%d+%d+%d @ %d bpp", j,
+                        monitors[j].width, monitors[j].height,
+                        monitors[j].x, monitors[j].y,
+                        monitors[j].depth);
     }
     g_free(sorted_monitors);
 }
-- 
1.8.3.1



More information about the Spice-devel mailing list