[Xfce4-commits] <xfce4-cpugraph-plugin:master> Implement 4819: support for tracking only one cpu

Florian Rivoal noreply at xfce.org
Tue Jun 8 16:42:01 CEST 2010


Updating branch refs/heads/master
         to 4c2642e04ab6b079ed651884a9bc961ebd0abbd6 (commit)
       from 2029bcfecfd60ec1acb1d6de011d6cee54709aff (commit)

commit 4c2642e04ab6b079ed651884a9bc961ebd0abbd6
Author: Florian Rivoal <frivoal at gmail.com>
Date:   Sat Jun 5 00:26:10 2010 +0900

    Implement 4819: support for tracking only one cpu
    
    It is now possible to track only one cpu or core in a particular
    cpugraph, even on systems that have many cpus or cores.
    
    The os dependent part still fetches data for all cores, but that data is
    only used to display information about one of them. This helps keep the
    design simple.

 panel-plugin/cpu.c        |   51 +++++++++++++++++++++++++++++++++++---------
 panel-plugin/cpu.h        |    2 +
 panel-plugin/os.c         |    2 +-
 panel-plugin/properties.c |   24 +++++++++++++++++++++
 panel-plugin/settings.c   |    4 +++
 5 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index 8b07674..0a85e49 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -87,14 +87,21 @@ static CPUGraph * create_gui( XfcePanelPlugin * plugin )
 	return base;
 }
 
+static guint nb_bars( CPUGraph * base )
+{
+	return base->tracked_core == 0 ? base->nr_cores : 1;
+}
 static void create_bars( CPUGraph *base )
 {
-	gint i;
-	base->bars = (GtkWidget **) g_malloc( sizeof( GtkWidget * ) * base->nr_cores );
+	guint i;
+	guint n;
+	n = nb_bars( base );
+	base->bars = (GtkWidget **) g_malloc( sizeof( GtkWidget * ) * n );
 
-	for(i=0; i<base->nr_cores; i++) {
+	for( i=0; i< n; i++ )
+	{
 		base->bars[i] = GTK_WIDGET(gtk_progress_bar_new());
-		gtk_box_pack_end( GTK_BOX(base->box), base->bars[i], FALSE, FALSE, 0);
+		gtk_box_pack_end( GTK_BOX(base->box), base->bars[i], FALSE, FALSE, 0 );
 		gtk_widget_show( base->bars[i] );
 	}
 }
@@ -127,10 +134,12 @@ static void shutdown( XfcePanelPlugin * plugin, CPUGraph * base )
 
 static void delete_bars( CPUGraph *base )
 {
-	gint i;
+	guint i;
+	guint n;
 	if( base->bars )
 	{
-		for( i=0; i < base->nr_cores; i++ )
+		n = nb_bars( base );
+		for( i=0; i < n; i++ )
 		{
 			gtk_widget_hide( base->bars[i] );
 			gtk_widget_destroy( base->bars[i] );
@@ -175,7 +184,8 @@ static gboolean size_cb( XfcePanelPlugin *plugin, guint size, CPUGraph *base )
 
 static void set_bars_size( CPUGraph *base, gint size, GtkOrientation orientation )
 {
-	gint i;
+	guint i;
+	guint n;
 	gint h, v;
 	if( orientation == GTK_ORIENTATION_HORIZONTAL )
 	{
@@ -187,7 +197,8 @@ static void set_bars_size( CPUGraph *base, gint size, GtkOrientation orientation
 		h = size;
 		v = BORDER;
 	}
-	for( i=0; i < base->nr_cores; i++ )
+	n = nb_bars( base );
+	for( i=0; i < n ; i++ )
 		gtk_widget_set_size_request( GTK_WIDGET(base->bars[i]), h, v );
 }
 
@@ -202,13 +213,15 @@ static void orientation_cb( XfcePanelPlugin * plugin, GtkOrientation orientation
 static void set_bars_orientation( CPUGraph *base, GtkOrientation orientation)
 {
 	GtkProgressBarOrientation barOrientation;
-	gint i; 
+	guint i; 
+	guint n; 
 	if( orientation == GTK_ORIENTATION_HORIZONTAL )
 		barOrientation = GTK_PROGRESS_BOTTOM_TO_TOP;
 	else
 		barOrientation = GTK_PROGRESS_LEFT_TO_RIGHT;
 
-	for( i=0; i<base->nr_cores; i++ )
+	n = nb_bars( base );
+	for( i=0; i < n; i++ )
 		gtk_progress_bar_set_orientation( GTK_PROGRESS_BAR( base->bars[i] ), barOrientation );	
 }
 
@@ -217,9 +230,15 @@ static gboolean update_cb( CPUGraph * base )
 	gint i, j, a, b, factor;
 	if( !read_cpu_data( base->cpu_data, base->nr_cores ) )
 		return TRUE;
+
+	if( base->tracked_core > base->nr_cores )
+		base->cpu_data[0].load = 0;
+	else if( base->tracked_core != 0 )
+		base->cpu_data[0].load = base->cpu_data[base->tracked_core].load;
+
 	if( base->has_bars )
 	{
-		if( base->nr_cores == 1 )
+		if( base->tracked_core != 0 || base->nr_cores == 1 )
 		{
 			gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(base->bars[0]),
 					(gdouble)base->cpu_data[0].load / CPU_SCALE
@@ -409,3 +428,13 @@ void set_color( CPUGraph *base, guint number, GdkColor color )
 		gtk_widget_modify_bg( base->draw_area, GTK_STATE_NORMAL, &base->colors[0] );
 	}
 }
+
+void set_tracked_core( CPUGraph *base, guint core )
+{
+	gboolean has_bars = base->has_bars;
+	if( has_bars)
+		set_bars( base, FALSE );
+	base->tracked_core = core;
+	if( has_bars)
+		set_bars( base, TRUE );
+}
diff --git a/panel-plugin/cpu.h b/panel-plugin/cpu.h
index d99e1a4..838a35b 100644
--- a/panel-plugin/cpu.h
+++ b/panel-plugin/cpu.h
@@ -35,6 +35,7 @@ typedef struct
 	gboolean in_terminal;
 	gboolean startup_notification;
 	GdkColor colors[4];
+	guint tracked_core;
 
 	/* Runtime data */
 	guint nr_cores;
@@ -56,4 +57,5 @@ void set_size( CPUGraph *base, guint width );
 void set_color_mode( CPUGraph *base, guint color_mode );
 void set_mode( CPUGraph *base, guint mode );
 void set_color( CPUGraph *base, guint number, GdkColor color );
+void set_tracked_core( CPUGraph *base, guint core );
 #endif /* !_XFCE_CPU_H_ */
diff --git a/panel-plugin/os.c b/panel-plugin/os.c
index 3abadac..125db97 100644
--- a/panel-plugin/os.c
+++ b/panel-plugin/os.c
@@ -61,7 +61,7 @@ guint detect_cpu_number()
 	return nb_lines > 1 ? nb_lines - 1 : 0;
 }
 
-gboolean read_cpu_data( CpuData *data, guint nb_cpu)
+gboolean read_cpu_data( CpuData *data, guint nb_cpu )
 {
 	FILE *fStat;
 	gchar cpuStr[PROCMAXLNLEN];
diff --git a/panel-plugin/properties.c b/panel-plugin/properties.c
index e726f13..19e045b 100644
--- a/panel-plugin/properties.c
+++ b/panel-plugin/properties.c
@@ -13,6 +13,7 @@ static void create_check_box( GtkBox *tab, GtkSizeGroup *sg, const gchar *name,
 static void create_drop_down( GtkBox *tab, GtkSizeGroup *sg, const gchar * name, const gchar **items, gsize nb_items, guint init, void (callback)( GtkComboBox *, CPUGraph * ), void * cb_data);
 
 static void setup_update_interval_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base );
+static void setup_tracked_core_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base );
 static void setup_size_option( GtkBox *vbox, GtkSizeGroup *sg, XfcePanelPlugin *plugin, CPUGraph *base );
 static void setup_command_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base );
 static void setup_color_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base, guint number, const gchar *name, GCallback cb );
@@ -36,6 +37,7 @@ static void change_bars( GtkToggleButton * button, CPUGraph * base );
 static void change_size( GtkSpinButton *sb, CPUGraph *base );
 static void change_time_scale( GtkToggleButton *button, CPUGraph *base );
 static void change_update( GtkComboBox *om, CPUGraph *base );
+static void change_core( GtkComboBox * combo, CPUGraph * base );
 
 void create_options( XfcePanelPlugin *plugin, CPUGraph *base )
 {
@@ -69,6 +71,7 @@ void create_options( XfcePanelPlugin *plugin, CPUGraph *base )
 
 	vbox = create_tab();
 	setup_update_interval_option( vbox, sg, base );
+	setup_tracked_core_option( vbox, sg, base );
 	setup_size_option( vbox, sg, plugin, base );
 	create_check_box( vbox, sg, _("Use non-linear time-scale"), base->non_linear, change_time_scale, base );
 	create_check_box( vbox, sg, _("Show frame"), base->has_frame, change_frame, base );
@@ -177,6 +180,22 @@ static void setup_update_interval_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGra
 	create_drop_down( vbox, sg, _("Update Interval:"), items, nb_items, base->update_interval, change_update, base);
 }
 
+static void setup_tracked_core_option( GtkBox *vbox, GtkSizeGroup *sg, CPUGraph *base )
+{
+	gsize nb_items = base->nr_cores + 1;
+	gchar *items[ nb_items ];
+	guint i;
+	items[0] = _("All");
+	for( i = 1; i < nb_items; i++ )
+	{
+		items[i] = g_malloc( g_snprintf( NULL, 0, "%u", i ) );
+		g_sprintf( items[i], "%u", i );
+	}
+	create_drop_down( vbox, sg, _("Tracked Core:"), (const gchar **)items, nb_items, base->tracked_core, change_core, base);
+	for( i = 1; i < nb_items; i++ )
+		g_free( items[i] );
+}
+
 static void setup_size_option( GtkBox *vbox, GtkSizeGroup *sg, XfcePanelPlugin *plugin, CPUGraph *base )
 {
 	GtkBox *hbox;
@@ -347,3 +366,8 @@ static void change_update( GtkComboBox * combo, CPUGraph * base )
 {
 	set_update_rate( base, gtk_combo_box_get_active( combo ) );
 }
+
+static void change_core( GtkComboBox * combo, CPUGraph * base )
+{
+	set_tracked_core( base, gtk_combo_box_get_active( combo ) );
+}
diff --git a/panel-plugin/settings.c b/panel-plugin/settings.c
index 16645b0..7835837 100644
--- a/panel-plugin/settings.c
+++ b/panel-plugin/settings.c
@@ -32,6 +32,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 	gboolean frame = TRUE;
 	gboolean border = TRUE;
 	gboolean bars = TRUE;
+	guint tracked_core = 0;
 
 	GdkColor foreground1;
 	GdkColor foreground2;
@@ -79,6 +80,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 			startup_notification = xfce_rc_read_int_entry( rc, "StartupNotification", startup_notification );
 			border = xfce_rc_read_int_entry( rc, "Border", border );
 			bars = xfce_rc_read_int_entry( rc, "Bars", bars );
+			tracked_core = xfce_rc_read_int_entry( rc, "TrackedCore", tracked_core );
 
 			if( (value = xfce_rc_read_entry( rc, "Foreground1", NULL )) )
 				gdk_color_parse( value, &foreground1 );
@@ -103,6 +105,7 @@ void read_settings( XfcePanelPlugin * plugin, CPUGraph * base )
 	set_in_terminal( base, in_terminal);
 	set_startup_notification( base, startup_notification );
 	set_border( base, border);
+	set_tracked_core( base, tracked_core );
 	set_bars( base, bars);
 	set_color( base, 1, foreground1 );
 	set_color( base, 2, foreground2 );
@@ -132,6 +135,7 @@ void write_settings( XfcePanelPlugin *plugin, CPUGraph *base )
 	xfce_rc_write_int_entry( rc, "Frame", base->has_frame );
 	xfce_rc_write_int_entry( rc, "Border", base->has_border );
 	xfce_rc_write_int_entry( rc, "Bars", base->has_bars );
+	xfce_rc_write_int_entry( rc, "TrackedCore", base->tracked_core );
 	xfce_rc_write_entry( rc, "Command", base->command ? base->command : "" );
 	xfce_rc_write_int_entry( rc, "InTerminal", base->in_terminal );
 	xfce_rc_write_int_entry( rc, "StartupNotification", base->startup_notification );



More information about the Xfce4-commits mailing list