[gnome-system-monitor] Allow for more than 4 unique colors for CPU graph
- From: Chris KÃhl <chriskuehl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-system-monitor] Allow for more than 4 unique colors for CPU graph
- Date: Mon, 9 Jan 2012 00:14:20 +0000 (UTC)
commit f13ddd3aa0015076961b7764b9c0f7eef79cd3b9
Author: Chris KÃhl <chrisk openismus com>
Date: Sun Jan 8 22:12:50 2012 +0100
Allow for more than 4 unique colors for CPU graph
This patch changes the GSettings key from being hard-coded as
cpu-color-n to an array with the GVariant type 'a(us)'; an array of
structures containing an unsigned int (cpu index) and a string (hex
color). Using this type as opposed to a simple 'as' allows one to
easily see which color is associated with each CPU. This becomes more
important as the number of CPUs grows.
Note that this patch does not change the issue that one can only
change the color of CPUs 0-3 in the GUI. One must use a tool such as
dcong-editor to edit CPUs 4-n.
https://bugzilla.gnome.org/show_bug.cgi?id=632188
src/callbacks.cpp | 43 ++++++++++++-----
src/org.gnome.gnome-system-monitor.gschema.xml.in | 29 ++---------
src/procman.cpp | 54 ++++++++++++--------
3 files changed, 68 insertions(+), 58 deletions(-)
---
diff --git a/src/callbacks.cpp b/src/callbacks.cpp
index b8c1c14..5608241 100644
--- a/src/callbacks.cpp
+++ b/src/callbacks.cpp
@@ -236,6 +236,37 @@ cb_end_process_button_pressed (GtkButton *button, gpointer data)
kill_process_helper(static_cast<ProcData*>(data), SIGTERM);
}
+void
+cb_cpu_color_changed (GSMColorButton *cp, gpointer data)
+{
+ guint cpu_i = GPOINTER_TO_UINT (data);
+ GSettings *settings = g_settings_new (GSM_GSETTINGS_SCHEMA);
+
+ /* Get current values */
+ GVariant *cpu_colors_var = g_settings_get_value(settings, "cpu-colors");
+ gsize children_n = g_variant_n_children(cpu_colors_var);
+
+ /* Create builder to contruct new setting with updated value for cpu i */
+ GVariantBuilder builder;
+ g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
+
+ for (guint i = 0; i < children_n; i++) {
+ if(cpu_i == i) {
+ gchar color[24];
+ GdkColor button_color;
+ gsm_color_button_get_color(cp, &button_color);
+ g_snprintf(color, sizeof(color), "#%04x%04x%04x",
+ button_color.red, button_color.green, button_color.blue);
+ g_variant_builder_add(&builder, "(us)", i, color);
+ } else {
+ g_variant_builder_add_value(&builder,
+ g_variant_get_child_value(cpu_colors_var, i));
+ }
+ }
+
+ /* Just set the value and let the changed::cpu-colors signal callback do the rest. */
+ g_settings_set_value(settings, "cpu-colors", g_variant_builder_end(&builder));
+}
static void change_settings_color(GSettings *settings, const char *key,
GSMColorButton *cp)
@@ -249,18 +280,6 @@ static void change_settings_color(GSettings *settings, const char *key,
}
void
-cb_cpu_color_changed (GSMColorButton *cp, gpointer data)
-{
- char key[80];
- gint i = GPOINTER_TO_INT (data);
- GSettings *settings = g_settings_new (GSM_GSETTINGS_SCHEMA);
-
- g_snprintf(key, sizeof key, "cpu-color%d", i%4);
-
- change_settings_color(settings, key, cp);
-}
-
-void
cb_mem_color_changed (GSMColorButton *cp, gpointer data)
{
ProcData * const procdata = static_cast<ProcData*>(data);
diff --git a/src/org.gnome.gnome-system-monitor.gschema.xml.in b/src/org.gnome.gnome-system-monitor.gschema.xml.in
index 129b5c2..6533681 100644
--- a/src/org.gnome.gnome-system-monitor.gschema.xml.in
+++ b/src/org.gnome.gnome-system-monitor.gschema.xml.in
@@ -105,32 +105,13 @@
</_description>
</key>
- <key name="cpu-color0" type="s">
- <default>'#FF6E00'
+ <key name="cpu-colors" type="a(us)">
+ <default>[(0,'#FF6E00'),(1,'#CB0C29'),(2,'#49A835'),(3,'#2D7DB3')]
</default>
- <_summary>Default graph CPU color
- </_summary>
- </key>
-
- <key name="cpu-color1" type="s">
- <default>'#CB0C29'
- </default>
- <_summary>Default graph CPU color
- </_summary>
- </key>
-
- <key name="cpu-color2" type="s">
- <default>'#49A835'
- </default>
- <_summary>Default graph CPU color
- </_summary>
- </key>
-
- <key name="cpu-color3" type="s">
- <default>'#2D7DB3'
- </default>
- <_summary>Default graph CPU color
+ <_summary>CPU colors
</_summary>
+ <_description>Each entry is in the format (CPU#, Hexadecimal color value)
+ </_description>
</key>
<key name="mem-color" type="s">
diff --git a/src/procman.cpp b/src/procman.cpp
index fc53088..07b6963 100644
--- a/src/procman.cpp
+++ b/src/procman.cpp
@@ -174,22 +174,43 @@ timeouts_changed_cb (GSettings *settings, const gchar *key, gpointer data)
}
static void
+apply_cpu_color_settings(GSettings *settings, ProcData * const procdata)
+{
+ GVariant *cpu_colors_var = g_settings_get_value(settings, "cpu-colors");
+ gsize n = g_variant_n_children(cpu_colors_var);
+
+ guint cpu_i;
+ gchar *color;
+ for (guint i = 0; i < static_cast<guint>(procdata->config.num_cpus); i++) {
+ if(i <= n) {
+ g_variant_get_child(cpu_colors_var, i, "(us)", &cpu_i, &color);
+ }
+ if (!color)
+ color = g_strdup ("#f25915e815e8");
+
+ gdk_color_parse(color, &procdata->config.cpu_color[i]);
+ g_free (color);
+ }
+}
+
+static void
color_changed_cb (GSettings *settings, const gchar *key, gpointer data)
{
ProcData * const procdata = static_cast<ProcData*>(data);
- const gchar *color = g_settings_get_string (settings, key);
- if (g_str_has_prefix (key, "cpu-color")) {
+ if (g_str_equal (key, "cpu-colors")) {
+ apply_cpu_color_settings(settings, procdata);
for (int i = 0; i < procdata->config.num_cpus; i++) {
- string cpu_key = make_string(g_strdup_printf("cpu-color%d", i%4));
- if (cpu_key == key) {
- gdk_color_parse (color, &procdata->config.cpu_color[i]);
- procdata->cpu_graph->colors.at(i) = procdata->config.cpu_color[i];
+ if(!gdk_color_equal(&procdata->cpu_graph->colors[i], &procdata->config.cpu_color[i])) {
+ procdata->cpu_graph->colors[i] = procdata->config.cpu_color[i];
break;
}
}
+ return;
}
- else if (g_str_equal (key, "mem-color")) {
+
+ const gchar *color = g_settings_get_string (settings, key);
+ if (g_str_equal (key, "mem-color")) {
gdk_color_parse (color, &procdata->config.mem_color);
procdata->mem_graph->colors.at(0) = procdata->config.mem_color;
}
@@ -268,7 +289,7 @@ procman_data_new (GSettings *settings)
g_signal_connect (G_OBJECT(settings), "changed::view-as", G_CALLBACK(view_as_changed_cb),pd);
pd->config.current_tab = g_settings_get_int (settings, "current-tab");
- /* Determinie number of cpus since libgtop doesn't really tell you*/
+ /* Determine number of cpus since libgtop doesn't really tell you*/
pd->config.num_cpus = 0;
glibtop_get_cpu (&cpu);
pd->frequency = cpu.frequency;
@@ -280,21 +301,10 @@ procman_data_new (GSettings *settings)
if (pd->config.num_cpus == 0)
pd->config.num_cpus = 1;
- for (int i = 0; i < pd->config.num_cpus; i++) {
- gchar *key;
- key = g_strdup_printf ("cpu-color%d", i%4);
-
- color = g_settings_get_string (settings, key);
- if (!color)
- color = g_strdup ("#f25915e815e8");
+ apply_cpu_color_settings(settings, pd);
+ g_signal_connect (G_OBJECT(settings), "changed::cpu-colors",
+ G_CALLBACK(color_changed_cb), pd);
- detail_string = std::string("changed::") + std::string(key);
- g_signal_connect (G_OBJECT(settings), detail_string.c_str(),
- G_CALLBACK(color_changed_cb), pd);
- gdk_color_parse(color, &pd->config.cpu_color[i]);
- g_free (color);
- g_free (key);
- }
color = g_settings_get_string (settings, "mem-color");
if (!color)
color = g_strdup ("#000000ff0082");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]