gnome-system-monitor r2316 - trunk/src



Author: bdejean
Date: Thu Feb 21 20:05:54 2008
New Revision: 2316
URL: http://svn.gnome.org/viewvc/gnome-system-monitor?rev=2316&view=rev

Log:
LoadGraph is now a real C++ object.
I'm going to gradually rework it to make it copyable (to be used in
netspeed applet maybe).


Modified:
   trunk/src/interface.cpp
   trunk/src/load-graph.cpp
   trunk/src/load-graph.h

Modified: trunk/src/interface.cpp
==============================================================================
--- trunk/src/interface.cpp	(original)
+++ trunk/src/interface.cpp	Thu Feb 21 20:05:54 2008
@@ -260,7 +260,7 @@
 	cpu_graph_box = gtk_vbox_new (FALSE, 6);
 	gtk_box_pack_start (GTK_BOX (cpu_box), cpu_graph_box, TRUE, TRUE, 0);
 
-	cpu_graph = load_graph_new (LOAD_GRAPH_CPU, procdata);
+	cpu_graph = new LoadGraph(LOAD_GRAPH_CPU);
 	gtk_box_pack_start (GTK_BOX (cpu_graph_box),
 			    load_graph_get_widget(cpu_graph),
 			    TRUE,
@@ -339,7 +339,7 @@
 	gtk_box_pack_start (GTK_BOX (mem_box), mem_graph_box, TRUE, TRUE, 0);
 
 
-	mem_graph = load_graph_new (LOAD_GRAPH_MEM, procdata);
+	mem_graph = new LoadGraph(LOAD_GRAPH_MEM);
 	gtk_box_pack_start (GTK_BOX (mem_graph_box),
 			    load_graph_get_widget(mem_graph),
 			    TRUE,
@@ -424,7 +424,7 @@
 	net_graph_box = gtk_vbox_new (FALSE, 6);
 	gtk_box_pack_start (GTK_BOX (net_box), net_graph_box, TRUE, TRUE, 0);
 
-	net_graph = load_graph_new (LOAD_GRAPH_NET, procdata);
+	net_graph = new LoadGraph(LOAD_GRAPH_NET);
 	gtk_box_pack_start (GTK_BOX (net_graph_box),
 			    load_graph_get_widget(net_graph),
 			    TRUE,

Modified: trunk/src/load-graph.cpp
==============================================================================
--- trunk/src/load-graph.cpp	(original)
+++ trunk/src/load-graph.cpp	Thu Feb 21 20:05:54 2008
@@ -30,73 +30,6 @@
 #include "util.h"
 #include "gsm_color_button.h"
 
-#define NUM_POINTS 60
-#define GRAPH_MIN_HEIGHT 40
-
-enum {
-	CPU_TOTAL,
-	CPU_USED,
-	N_CPU_STATES
-};
-
-struct LoadGraph {
-
-
-	unsigned num_bars() const;
-
-	double fontsize;
-	double rmargin;
-	double indent;
-
-	guint n;
-	gint type;
-	guint speed;
-	guint draw_width, draw_height;
-	guint render_counter;
-	guint frames_per_unit;
-	guint graph_dely;
-	guint real_draw_height;
-	double graph_delx;
-	guint graph_buffer_offset;
-
-	GdkColor *colors;
-
-	gfloat* data_block;
-	gfloat* data[NUM_POINTS];
-
-	GtkWidget *main_widget;
-	GtkWidget *disp;
-
-	cairo_surface_t *buffer;
-	cairo_surface_t *graph_buffer;
-	cairo_surface_t *background_buffer;
-
-	guint timer_index;
-
-	gboolean draw;
-
-	LoadGraphLabels labels;
-	GtkWidget *mem_color_picker;
-	GtkWidget *swap_color_picker;
-
-	/* union { */
-		struct {
-			guint now; /* 0 -> current, 1 -> last
-				    now ^ 1 each time */
-			/* times[now], times[now ^ 1] is last */
-			guint64 times[2][GLIBTOP_NCPU][N_CPU_STATES];
-		} cpu;
-
-		struct {
-			guint64 last_in, last_out;
-			GTimeVal time;
-			unsigned int max;
-			unsigned values[NUM_POINTS];
-			size_t cur;
-		} net;
-	/* }; */
-};
-
 
 
 unsigned LoadGraph::num_bars() const
@@ -140,7 +73,7 @@
 	num_bars = g->num_bars();
 	g->graph_dely = (g->draw_height - 15) / num_bars; /* round to int to avoid AA blur */
 	g->real_draw_height = g->graph_dely * num_bars;
-	g->graph_delx = (g->draw_width - 2.0 - g->rmargin - g->indent) / (NUM_POINTS - 3);
+	g->graph_delx = (g->draw_width - 2.0 - g->rmargin - g->indent) / (LoadGraph::NUM_POINTS - 3);
 	g->graph_buffer_offset = (int) (1.5 * g->graph_delx) + FRAME_WIDTH ;
 
 	cr = cairo_create (g->buffer);
@@ -203,7 +136,7 @@
 
 	cairo_set_dash (tmp_cr, dash, 2, 1.5);
 
-	const unsigned total_seconds = g->speed * (NUM_POINTS - 2) / 1000;
+	const unsigned total_seconds = g->speed * (LoadGraph::NUM_POINTS - 2) / 1000;
 
 	for (unsigned int i = 0; i < 7; i++) {
 		double x = (i) * (g->draw_width - g->rmargin - g->indent) / 6;
@@ -260,7 +193,7 @@
 				       (1.0f - g->data[0][j]) * g->real_draw_height);
 			gdk_cairo_set_source_color (tmp_cr, &(g->colors [j]));
 
-			for (i = 1; i < NUM_POINTS; ++i) {
+			for (i = 1; i < LoadGraph::NUM_POINTS; ++i) {
 				if (g->data[i][j] == -1.0f)
 					continue;
 
@@ -310,7 +243,7 @@
 	g->draw_height = widget->allocation.height - 2 * FRAME_WIDTH;
 
 	// FIXME:
-	// g->frames_per_unit = g->draw_width/(NUM_POINTS);
+	// g->frames_per_unit = g->draw_width/(LoadGraph::NUM_POINTS);
 	// knock FRAMES down to 5 until cairo gets faster
 	g->frames_per_unit = 5;
 
@@ -473,7 +406,7 @@
 
 	unsigned dmax = std::max(din, dout);
 	g->net.values[g->net.cur] = dmax;
-	g->net.cur = (g->net.cur + 1) % NUM_POINTS;
+	g->net.cur = (g->net.cur + 1) % LoadGraph::NUM_POINTS;
 
 	unsigned new_max;
 	// both way, new_max is the greatest value
@@ -481,7 +414,7 @@
 		new_max = dmax;
 	else
 		new_max = *std::max_element(&g->net.values[0],
-					    &g->net.values[NUM_POINTS]);
+					    &g->net.values[LoadGraph::NUM_POINTS]);
 
 	//
 	// Round network maximum
@@ -532,7 +465,7 @@
 
 	const float scale = 1.0f * g->net.max / new_max;
 
-	for (size_t i = 0; i < NUM_POINTS; i++) {
+	for (size_t i = 0; i < LoadGraph::NUM_POINTS; i++) {
 		if (g->data[i][0] >= 0.0f) {
 			g->data[i][0] *= scale;
 			g->data[i][1] *= scale;
@@ -624,7 +557,7 @@
 	LoadGraph * const g = static_cast<LoadGraph*>(user_data);
 
 	if (g->render_counter == 0) {
-		std::rotate(&g->data[0], &g->data[NUM_POINTS - 1], &g->data[NUM_POINTS]);
+		std::rotate(&g->data[0], &g->data[LoadGraph::NUM_POINTS - 1], &g->data[LoadGraph::NUM_POINTS]);
 
 		switch (g->type) {
 		case LOAD_GRAPH_CPU:
@@ -652,54 +585,60 @@
 	return TRUE;
 }
 
-static void
-load_graph_unalloc (LoadGraph *g)
-{
-	g_free(g->data_block);
 
-	if (g->buffer) {
-		cairo_surface_destroy (g->buffer);
-		g->buffer = NULL;
-	}
-}
 
-static void
-load_graph_alloc (LoadGraph *g)
+LoadGraph::~LoadGraph()
 {
-	guint i, j;
+  load_graph_stop(this);
 
-	/* Allocate data in a contiguous block */
-	g->data_block = g_new(float, g->n * NUM_POINTS);
+  if (this->timer_index)
+    g_source_remove(this->timer_index);
 
-	for (i = 0; i < NUM_POINTS; ++i) {
-		g->data[i] = g->data_block + i * g->n;
-		for (j = 0; j < g->n; ++j)
-			g->data[i][j] = -1.0f;
-	}
+  if (this->buffer) {
+    cairo_surface_destroy(this->buffer);
+    this->buffer = NULL;
+  }
 }
 
+
+
 static gboolean
 load_graph_destroy (GtkWidget *widget, gpointer data_ptr)
 {
 	LoadGraph * const g = static_cast<LoadGraph*>(data_ptr);
 
-	load_graph_stop (g);
-
-	if (g->timer_index)
-		g_source_remove (g->timer_index);
-
-	load_graph_unalloc(g);
+	delete g;
 
 	return FALSE;
 }
 
-LoadGraph *
-load_graph_new (gint type, ProcData *procdata)
-{
-	LoadGraph *g;
-	guint i = 0;
 
-	g = g_new0 (LoadGraph, 1);
+LoadGraph::LoadGraph(guint type)
+  : fontsize(0.0),
+    rmargin(0.0),
+    indent(0.0),
+    n(0),
+    type(0),
+    speed(0),
+    draw_width(0),
+    draw_height(0),
+    render_counter(0),
+    frames_per_unit(0),
+    graph_dely(0),
+    real_draw_height(0),
+    graph_delx(0.0),
+    graph_buffer_offset(0),
+    main_widget(NULL),
+    disp(NULL),
+    buffer(NULL),
+    graph_buffer(NULL),
+    background_buffer(NULL),
+    timer_index(0),
+    draw(FALSE),
+    mem_color_picker(NULL),
+    swap_color_picker(NULL)
+{
+	LoadGraph * const g = this;
 
 	g->frames_per_unit = 1;  // this will be changed but needs initialising
 	g->fontsize = 8.0;
@@ -709,12 +648,13 @@
 	g->type = type;
 	switch (type) {
 	case LOAD_GRAPH_CPU:
-		if (procdata->config.num_cpus == 0)
+		memset(&this->cpu, 0, sizeof g->cpu);
+		if (ProcData::get_instance()->config.num_cpus == 0)
 			g->n = 1;
 		else
-			g->n = procdata->config.num_cpus;
+			g->n = ProcData::get_instance()->config.num_cpus;
 
-		for(i = 0; i < G_N_ELEMENTS(g->labels.cpu); ++i)
+		for(guint i = 0; i < G_N_ELEMENTS(g->labels.cpu); ++i)
 			g->labels.cpu[i] = gtk_label_new(NULL);
 
 		break;
@@ -726,6 +666,7 @@
 		break;
 
 	case LOAD_GRAPH_NET:
+		memset(&this->net, 0, sizeof g->net);
 		g->n = 2;
 		g->net.max = 1;
 		g->labels.net_in = gtk_label_new(NULL);
@@ -735,26 +676,26 @@
 		break;
 	}
 
-	g->speed  = procdata->config.graph_update_interval;
+	g->speed  = ProcData::get_instance()->config.graph_update_interval;
 
-	g->colors = g_new0 (GdkColor, g->n);
+	g->colors.resize(g->n);
 
 	switch (type) {
 	case LOAD_GRAPH_CPU:
-		memcpy(g->colors, procdata->config.cpu_color,
+		memcpy(&g->colors[0], ProcData::get_instance()->config.cpu_color,
 		       g->n * sizeof g->colors[0]);
 		break;
 	case LOAD_GRAPH_MEM:
-		g->colors[0] = procdata->config.mem_color;
-		g->colors[1] = procdata->config.swap_color;
+		g->colors[0] = ProcData::get_instance()->config.mem_color;
+		g->colors[1] = ProcData::get_instance()->config.swap_color;
 		g->mem_color_picker = gsm_color_button_new (&g->colors[0], 
 							    GSMCP_TYPE_PIE);
 		g->swap_color_picker = gsm_color_button_new (&g->colors[1], 
 							     GSMCP_TYPE_PIE);
 		break;
 	case LOAD_GRAPH_NET:
-		g->colors[0] = procdata->config.net_in_color;
-		g->colors[1] = procdata->config.net_out_color;
+		g->colors[0] = ProcData::get_instance()->config.net_in_color;
+		g->colors[1] = ProcData::get_instance()->config.net_out_color;
 		break;
 	}
 
@@ -765,7 +706,7 @@
 	g->draw = FALSE;
 
 	g->main_widget = gtk_vbox_new (FALSE, FALSE);
-	gtk_widget_set_size_request(g->main_widget, -1, GRAPH_MIN_HEIGHT);
+	gtk_widget_set_size_request(g->main_widget, -1, LoadGraph::GRAPH_MIN_HEIGHT);
 	gtk_widget_show (g->main_widget);
 
 	g->disp = gtk_drawing_area_new ();
@@ -781,14 +722,18 @@
 
 	gtk_box_pack_start (GTK_BOX (g->main_widget), g->disp, TRUE, TRUE, 0);
 
-	load_graph_alloc (g);
+
+	/* Allocate data in a contiguous block */
+	g->data_block = std::vector<float>(g->n * LoadGraph::NUM_POINTS, -1.0f);
+
+	for (guint i = 0; i < LoadGraph::NUM_POINTS; ++i)
+	  g->data[i] = &g->data_block[0] + i * g->n;
 
 	gtk_widget_show_all (g->main_widget);
 
 	load_graph_start(g);
 	load_graph_stop(g);
-	
-	return g;
+
 }
 
 void
@@ -839,7 +784,7 @@
 GdkColor*
 load_graph_get_colors (LoadGraph *g)
 {
-	return g->colors;
+	return &g->colors[0];
 }
 
 

Modified: trunk/src/load-graph.h
==============================================================================
--- trunk/src/load-graph.h	(original)
+++ trunk/src/load-graph.h	Thu Feb 21 20:05:54 2008
@@ -4,11 +4,6 @@
 #include <glib/gtypes.h>
 #include <glibtop/cpu.h>
 
-struct LoadGraph;
-typedef struct _LoadGraphLabels LoadGraphLabels;
-
-#include "procman.h"
-
 enum
 {
 	LOAD_GRAPH_CPU,
@@ -17,7 +12,14 @@
 };
 
 
-struct _LoadGraphLabels
+enum {
+	CPU_TOTAL,
+	CPU_USED,
+	N_CPU_STATES
+};
+
+
+struct LoadGraphLabels
 {
 	GtkWidget *cpu[GLIBTOP_NCPU];
 	GtkWidget *memory;
@@ -29,9 +31,71 @@
 };
 
 
-/* Create new load graph. */
-LoadGraph *
-load_graph_new (gint type, ProcData *procdata);
+
+struct LoadGraph {
+
+	static const unsigned NUM_POINTS = 60;
+	static const unsigned GRAPH_MIN_HEIGHT = 40;
+
+	LoadGraph(guint type);
+	~LoadGraph();
+
+	unsigned num_bars() const;
+
+	double fontsize;
+	double rmargin;
+	double indent;
+
+	guint n;
+	gint type;
+	guint speed;
+	guint draw_width, draw_height;
+	guint render_counter;
+	guint frames_per_unit;
+	guint graph_dely;
+	guint real_draw_height;
+	double graph_delx;
+	guint graph_buffer_offset;
+
+	std::vector<GdkColor> colors;
+
+	std::vector<float> data_block;
+	gfloat* data[NUM_POINTS];
+
+	GtkWidget *main_widget;
+	GtkWidget *disp;
+
+	cairo_surface_t *buffer;
+	cairo_surface_t *graph_buffer;
+	cairo_surface_t *background_buffer;
+
+	guint timer_index;
+
+	gboolean draw;
+
+	LoadGraphLabels labels;
+	GtkWidget *mem_color_picker;
+	GtkWidget *swap_color_picker;
+
+	/* union { */
+		struct {
+			guint now; /* 0 -> current, 1 -> last
+				    now ^ 1 each time */
+			/* times[now], times[now ^ 1] is last */
+			guint64 times[2][GLIBTOP_NCPU][N_CPU_STATES];
+		} cpu;
+
+		struct {
+			guint64 last_in, last_out;
+			GTimeVal time;
+			unsigned int max;
+			unsigned values[NUM_POINTS];
+			size_t cur;
+		} net;
+	/* }; */
+};
+
+
 
 /* Force a drawing update */
 void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]