Blob Blame History Raw
From 8517b0284bfed75f82e9692b8543edaf42ba8a92 Mon Sep 17 00:00:00 2001
From: David King <dking@redhat.com>
Date: Wed, 18 Dec 2013 14:51:51 +0000
Subject: [PATCH] Display more than 64 CPUs correctly

Add a scrolled window for the GtkGrid containing the CPU color
selectors, and randomize the color for each CPU, if it is not set in the
configuration.

Adapted from an earlier patch by Cosimo Cecchi in commit
d5e323b5e2cfd47b47809f09d7dfee307f2eb378.

https://bugzilla.redhat.com/show_bug.cgi?id=1040501
---
 src/interface.cpp   |  12 +++++--
 src/procman-app.cpp |   2 +-
 src/util.cpp        | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/util.h          |   2 ++
 4 files changed, 114 insertions(+), 4 deletions(-)

diff --git a/src/interface.cpp b/src/interface.cpp
index c116cf5..720bcb2 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -169,7 +169,7 @@ create_sys_view (ProcmanApp *app, GtkBuilder * builder)
     GtkWidget *picker_alignment;
     LoadGraph *cpu_graph, *mem_graph, *net_graph;
 
-    gint i;
+    gint i, rows_n;
     gchar *title_text;
     gchar *label_text;
     gchar *title_template;
@@ -188,8 +188,16 @@ create_sys_view (ProcmanApp *app, GtkBuilder * builder)
                         TRUE,
                         0);
 
-    GtkWidget* cpu_table = GTK_WIDGET (gtk_builder_get_object (builder, "cpu_table"));
     gint cols = 4;
+    rows_n = std::max(app->config.num_cpus / cols, 1);
+
+    GtkWidget* cpu_table = GTK_WIDGET (gtk_builder_get_object (builder, "cpu_table"));
+    if (rows_n > 2) {
+        GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
+        gtk_widget_reparent(cpu_table, scrolled_window);
+        gtk_box_pack_end (GTK_BOX (cpu_graph_box), scrolled_window, TRUE, TRUE, 0);
+    }
+
     for (i=0;i<app->config.num_cpus; i++) {
         GtkWidget *temp_hbox;
 
diff --git a/src/procman-app.cpp b/src/procman-app.cpp
index 6ce08b9..83f6975 100644
--- a/src/procman-app.cpp
+++ b/src/procman-app.cpp
@@ -174,7 +174,7 @@ apply_cpu_color_settings(GSettings *settings, gpointer data)
             g_variant_builder_add_value ( &builder, child);
             g_variant_unref (child);
         } else {
-            color = g_strdup ("#f25915e815e8");
+            color = procman::generate_random_color();
             g_variant_builder_add(&builder, "(us)", i, color);
         }
         gdk_rgba_parse(&app->config.cpu_color[i], color);
diff --git a/src/util.cpp b/src/util.cpp
index f06745b..2af00db 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -365,7 +365,85 @@ procman_debug_real(const char *file, int line, const char *func,
     g_free(msg);
 }
 
-
+/* taken from gtk+/gtk/gtkstyle.c */
+static void
+hls_to_rgb (gdouble *h,
+            gdouble *l,
+            gdouble *s)
+{
+  gdouble hue;
+  gdouble lightness;
+  gdouble saturation;
+  gdouble m1, m2;
+  gdouble r, g, b;
+
+  lightness = *l;
+  saturation = *s;
+
+  if (lightness <= 0.5)
+    m2 = lightness * (1 + saturation);
+  else
+    m2 = lightness + saturation - lightness * saturation;
+  m1 = 2 * lightness - m2;
+
+  if (saturation == 0)
+    {
+      *h = lightness;
+      *l = lightness;
+      *s = lightness;
+    }
+  else
+    {
+      hue = *h + 120;
+      while (hue > 360)
+        hue -= 360;
+      while (hue < 0)
+        hue += 360;
+
+      if (hue < 60)
+        r = m1 + (m2 - m1) * hue / 60;
+      else if (hue < 180)
+        r = m2;
+      else if (hue < 240)
+        r = m1 + (m2 - m1) * (240 - hue) / 60;
+      else
+        r = m1;
+
+      hue = *h;
+      while (hue > 360)
+        hue -= 360;
+      while (hue < 0)
+        hue += 360;
+
+      if (hue < 60)
+        g = m1 + (m2 - m1) * hue / 60;
+      else if (hue < 180)
+        g = m2;
+      else if (hue < 240)
+        g = m1 + (m2 - m1) * (240 - hue) / 60;
+      else
+        g = m1;
+
+      hue = *h - 120;
+      while (hue > 360)
+        hue -= 360;
+      while (hue < 0)
+        hue += 360;
+
+      if (hue < 60)
+        b = m1 + (m2 - m1) * hue / 60;
+      else if (hue < 180)
+        b = m2;
+      else if (hue < 240)
+        b = m1 + (m2 - m1) * (240 - hue) / 60;
+      else
+        b = m1;
+
+      *h = r;
+      *l = g;
+      *s = b;
+    }
+}
 
 namespace procman
 {
@@ -649,6 +727,28 @@ namespace procman
         return procman::format_rate(rate, max_rate, ProcmanApp::get()->config.network_in_bits);
     }
 
+
+    gchar *generate_random_color (void)
+    {
+        GdkColor color;
+        gdouble h, l, s;
+
+        h = g_random_int_range(0, 255);
+        /* Clip luma, as we don't want colors too light (unreadable on a white
+         * bg. */
+        l = g_random_int_range(0, 224);
+        s = g_random_int_range(0, 255);
+
+        hls_to_rgb(&h, &l, &s);
+
+        color.red = h * 255;
+        color.green = l * 255;
+        color.blue = s * 255;
+
+        return gdk_color_to_string(&color);
+    }
+
+
 }
 
 
diff --git a/src/util.h b/src/util.h
index f55f821..43d0001 100644
--- a/src/util.h
+++ b/src/util.h
@@ -165,6 +165,8 @@ namespace procman
 
     std::string format_network(guint64 rate, guint64 max_rate = 0);
     std::string format_network_rate(guint64 rate, guint64 max_rate = 0);
+
+    gchar *generate_random_color (void);
 }
 
 
-- 
1.8.3.1