diff --git a/panel-plugin/xfce4-cpufreq-plugin.c b/panel-plugin/xfce4-cpufreq-plugin.c index e886121..8ffb4e7 100644 --- a/panel-plugin/xfce4-cpufreq-plugin.c +++ b/panel-plugin/xfce4-cpufreq-plugin.c @@ -401,6 +401,67 @@ cpufreq_current_cpu (void) +static void +cpufreq_update_pixmap (CpuInfo *cpu) +{ + GdkPixbuf *cur_pixmap; + gint freq_hash; + + gdouble delta = cpu->max_freq - cpu->min_freq; + + if (delta <= 0.0) + return; + + freq_hash = cpu->cur_freq >> 16; + + cur_pixmap = g_hash_table_lookup (cpuFreq->colored_pixmaps, + GINT_TO_POINTER (freq_hash)); + + if (!cur_pixmap) + { + guchar *pixels = NULL; + guint val; + gsize plength; + gsize pcomps; + gsize p; + + gdouble scale = (cpu->cur_freq - cpu->min_freq) / delta; + if (scale > 1.0) + scale = 1.0; + + val = (guchar) (255.0 * scale); + cur_pixmap = gdk_pixbuf_copy (cpuFreq->template_icon); + + if (!cur_pixmap) + return; + + pixels = gdk_pixbuf_get_pixels (cur_pixmap); + plength = gdk_pixbuf_get_byte_length (cur_pixmap); + pcomps = gdk_pixbuf_get_n_channels (cur_pixmap); + + for (p = 0; p < plength; p += pcomps) + { + gint delta1 = abs(pixels[p] - pixels[p+1]); + gint delta2 = abs(pixels[p] - pixels[p+2]); + if (delta1 < 10 && delta2 < 10) + pixels[p] = val; + } + + g_hash_table_insert (cpuFreq->colored_pixmaps, + GINT_TO_POINTER (freq_hash), cur_pixmap); + } + + gdk_pixbuf_copy_area (cur_pixmap, 0, 0, + gdk_pixbuf_get_width (cur_pixmap), + gdk_pixbuf_get_height (cur_pixmap), + gtk_image_get_pixbuf (GTK_IMAGE (cpuFreq->icon)), + 0, 0); + + g_signal_emit_by_name (cpuFreq->icon, "style-updated"); +} + + + gboolean cpufreq_update_plugin (gboolean reset_label_size) { @@ -418,6 +479,8 @@ cpufreq_update_plugin (gboolean reset_label_size) ret = cpufreq_update_label (cpu); + cpufreq_update_pixmap (cpu); + if (cpuFreq->layout_changed) { cpufreq_label_set_font (); @@ -512,7 +575,6 @@ cpufreq_update_icon (CpuFreqPlugin *cpufreq) if (cpufreq->options->show_icon) { - GdkPixbuf *buf, *scaled; gint icon_size; icon_size = cpuFreq->panel_size / cpuFreq->panel_rows; @@ -522,21 +584,26 @@ cpufreq_update_icon (CpuFreqPlugin *cpufreq) !cpufreq->options->show_label_governor)) icon_size -= 4; - buf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), - "xfce4-cpufreq-plugin", icon_size, 0, NULL); + if (cpufreq->template_icon) + g_object_unref (G_OBJECT (cpufreq->template_icon)); + + cpufreq->template_icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), + "xfce4-cpufreq-plugin", + icon_size, 0, NULL); - if (buf) + if (cpufreq->template_icon) { - scaled = gdk_pixbuf_scale_simple (buf, icon_size, icon_size, GDK_INTERP_BILINEAR); - cpufreq->icon = gtk_image_new_from_pixbuf (scaled); - g_object_unref (G_OBJECT (buf)); - g_object_unref (G_OBJECT (scaled)); + cpufreq->icon = gtk_image_new_from_pixbuf (cpufreq->template_icon); } else { cpufreq->icon = gtk_image_new_from_icon_name ("xfce4-cpufreq-plugin", GTK_ICON_SIZE_BUTTON); + cpufreq->template_icon = gtk_image_get_pixbuf (GTK_IMAGE (cpufreq->icon)); } + cpufreq->template_icon = gdk_pixbuf_copy (cpufreq->template_icon); + g_hash_table_remove_all (cpufreq->colored_pixmaps); + gtk_box_pack_start (GTK_BOX (cpufreq->box), cpufreq->icon, FALSE, FALSE, 0); gtk_widget_show (cpufreq->icon); } @@ -708,6 +775,9 @@ cpuinfo_free (CpuInfo *cpu) static void cpufreq_free (XfcePanelPlugin *plugin) { + g_object_unref (G_OBJECT (cpuFreq->template_icon)); + g_hash_table_unref (cpuFreq->colored_pixmaps); + if (cpuFreq->timeoutHandle) g_source_remove (cpuFreq->timeoutHandle); @@ -784,6 +854,9 @@ cpufreq_construct (XfcePanelPlugin *plugin) cpuFreq->label_max_width = -1; cpuFreq->cpus = g_ptr_array_new (); + cpuFreq->template_icon = NULL; + cpuFreq->colored_pixmaps = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref); + cpufreq_read_config (); cpuFreq->layout_changed = TRUE; diff --git a/panel-plugin/xfce4-cpufreq-plugin.h b/panel-plugin/xfce4-cpufreq-plugin.h index a6895e4..4a30167 100644 --- a/panel-plugin/xfce4-cpufreq-plugin.h +++ b/panel-plugin/xfce4-cpufreq-plugin.h @@ -84,8 +84,12 @@ typedef struct /* Intel P-State parameters */ IntelPState *intel_pstate; + /* Table with frequency color coded pixbufs */ + GHashTable *colored_pixmaps; + /* Widgets */ GtkWidget *button, *box, *icon, *label; + GdkPixbuf *template_icon; gboolean layout_changed; gint label_max_width;