aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/stats/statistics.c
blob: 09eec75baa725f10a393dfa9de2bd538d61a5c2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
     This file is part of GNUnet
     (C) 2004, 2005 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.

     Portions of this code were adopted from the
     gnome-system-monitor v2.0.5, (C) 2001 Kevin Vandersloot
*/

#include "platform.h"
#include "gnunetgtk_common.h"
#include <GNUnet/gnunet_stats_lib.h>
#include <GNUnet/gnunet_getoption_lib.h>
#include "functions.h"

#define MAX_COLOR 4
#define GNOME_PAD_SMALL 2
#define FRAME_WIDTH 0


typedef struct {
  gint type;
  guint count;
  guint speed;
  guint draw_width, draw_height;
  guint num_points; 
  guint allocated;  
  GdkColor *colors;
  gfloat **data, **odata;
  guint data_size;
  gint colors_allocated;
  GtkWidget *main_widget;
  GtkWidget *disp;
  GtkWidget *label;
  GdkPixmap *pixmap;
  GdkGC *gc;
  int timer_index;  
  gboolean draw;
  int statIdx;
} LoadGraph;

typedef struct {
  GdkColor        bg_color;
  GdkColor        frame_color;
  GdkColor        mem_color[MAX_COLOR];
} ProcConfig;

/**
 * Redraws the backing pixmap for the load graph and updates the window 
 */
static void load_graph_draw(LoadGraph *g) {
  guint i;
  guint j;
  gint dely;
  float delx;
  
  if (!g->disp->window)
    return;
  
  g->draw_width = g->disp->allocation.width;
  g->draw_height = g->disp->allocation.height;
  
  if (!g->pixmap)
    g->pixmap = gdk_pixmap_new (g->disp->window,
				g->draw_width, g->draw_height,
				gtk_widget_get_visual (g->disp)->depth);
  
  /* Create GC if necessary. */
  if (!g->gc) {
    g->gc = gdk_gc_new (g->disp->window);
    gdk_gc_copy (g->gc, g->disp->style->white_gc);
  }
  
  /* Allocate colors. */
  if (!g->colors_allocated) {
    GdkColormap *colormap;
    
    colormap = gdk_window_get_colormap (g->disp->window);
    for (i=0;i<2+g->count;i++) 
      gdk_color_alloc (colormap, &(g->colors [i]));    
    
    g->colors_allocated = 1;
  }
  /* Erase Rectangle */
  gdk_gc_set_foreground (g->gc, &(g->colors [0]));
  gdk_draw_rectangle (g->pixmap,
		      g->gc,
		      TRUE, 0, 0,
		      g->disp->allocation.width,
		      g->disp->allocation.height);
  
  /* draw frame */
  gdk_gc_set_foreground (g->gc, &(g->colors [1]));
  gdk_draw_rectangle (g->pixmap,
		      g->gc,
		      FALSE, 0, 0,
		      g->draw_width,
		      g->disp->allocation.height);
  
  dely = g->draw_height / 5;
  for (i = 1; i <5; i++) {
    gint y1 = g->draw_height + 1 - i * dely;
    gdk_draw_line (g->pixmap, g->gc,
		   0, y1, g->draw_width, y1);
  }
  
  gdk_gc_set_line_attributes(g->gc,
			     2, 
			     GDK_LINE_SOLID, 
			     GDK_CAP_ROUND, 
			     GDK_JOIN_MITER);
  delx = (float)g->draw_width / ( g->num_points - 1);
  
  for (j=0;j<g->count;j++) {
    gdk_gc_set_foreground (g->gc, &(g->colors [j + 2]));
    for (i = 0; i < g->num_points - 1; i++) {    
      gint x1 = i * delx;
      gint x2 = (i + 1) * delx;
      gint y1 = g->data[i][j] * g->draw_height - 1;
      gint y2 = g->data[i+1][j] * g->draw_height - 1;
      
      if ((g->data[i][j] != -1) && (g->data[i+1][j] != -1)) {
	if (stats[g->statIdx].fill == NO) {
	  gdk_draw_line(g->pixmap, g->gc,
			g->draw_width - x2, 
			g->draw_height - y2,
			g->draw_width - x1,
			g->draw_height - y1);
	} else {
	  GdkPoint points[4];
	  
	  points[0].x = g->draw_width - x2;
	  points[0].y = g->draw_height - y2;
	  points[1].x = g->draw_width - x1;
	  points[1].y = g->draw_height - y1;
	  points[2].x = g->draw_width - x1;
	  points[3].x = g->draw_width - x2;
	  if (j == 0) {
	    points[2].y = g->draw_height;
	    points[3].y = g->draw_height;
	  } else {
	    gint ly1 = g->data[i][j-1] * g->draw_height - 1;
	    gint ly2 = g->data[i+1][j-1] * g->draw_height - 1;
	    points[2].y = g->draw_height - ly1;
	    points[3].y = g->draw_height - ly2;
	  }
	  gdk_draw_polygon(g->pixmap,
			   g->gc,
			   1,
			   points,
			   4);  
	}
      }
    }
  }

  gdk_gc_set_line_attributes (g->gc, 1, 
			      GDK_LINE_SOLID,
			      GDK_CAP_ROUND,
			      GDK_JOIN_MITER );
  
  gdk_draw_pixmap (g->disp->window,
		   g->disp->style->fg_gc [GTK_WIDGET_STATE(g->disp)],
		   g->pixmap,
		   0, 0,
		   0, 0,
		   g->disp->allocation.width,
		   g->disp->allocation.height);
}


/* Updates the load graph when the timeout expires */
static gboolean load_graph_update(gpointer ptr) {
  LoadGraph *g = ptr;
  guint i;  
  guint j;

  for (i=0;i<g->num_points;i++)
    memcpy(g->odata[i], 
	   g->data[i],
	   g->data_size * g->count); 
  stats[g->statIdx].getData(stats[g->statIdx].get_closure,
			    g->data);
  for (i=0;i<g->num_points-1;i++)
    for (j=0;j<g->count;j++)
      g->data[i+1][j] = g->odata[i][j];  
  if (g->draw)
    load_graph_draw (g);  
  return TRUE;
}

static void load_graph_unalloc(LoadGraph *g) {
  int i;
  if (!g->allocated)
    return;
  for (i = 0; i < g->num_points; i++) {
    FREE(g->data[i]);
    FREE(g->odata[i]);
  }
  FREE(g->data);
  FREE(g->odata);
  g->data = g->odata = NULL;
  if (g->pixmap) {
    gdk_pixmap_unref(g->pixmap);
    g->pixmap = NULL;
  }
  g->allocated = FALSE;
}

static void load_graph_alloc (LoadGraph *g) {
  int i;
  int j;

  if (g->allocated)
    return;
  
  g->data = MALLOC(sizeof(gfloat *) * g->num_points);
  g->odata = MALLOC(sizeof(gfloat*) * g->num_points);
  g->data_size = sizeof (gfloat);  
  for (i = 0; i < g->num_points; i++) {
    g->data[i] = MALLOC(g->data_size * g->count);
    g->odata[i] = MALLOC(g->data_size * g->count);
  }  
  for (i=0;i<g->num_points;i++) 
    for (j=0;j<g->count;j++)
      g->data[i][j] = -1;
  g->allocated = TRUE;
}

static gint load_graph_configure(GtkWidget *widget, 
				 GdkEventConfigure *event,
				 gpointer data_ptr) {
  LoadGraph *c = (LoadGraph *) data_ptr;
  
  if (c->pixmap) {
    gdk_pixmap_unref (c->pixmap);
    c->pixmap = NULL;
  }
  
  if (!c->pixmap)
    c->pixmap = gdk_pixmap_new(widget->window,
			       widget->allocation.width,
			       widget->allocation.height,
			       gtk_widget_get_visual (c->disp)->depth);
  gdk_draw_rectangle(c->pixmap,
		     widget->style->black_gc,
		     TRUE, 0,0,
		     widget->allocation.width,
		     widget->allocation.height);
  gdk_draw_pixmap(widget->window,
		  c->disp->style->fg_gc[GTK_WIDGET_STATE(widget)],
		  c->pixmap,
		  0, 0,
		  0, 0,
		  c->disp->allocation.width,
		  c->disp->allocation.height);  

  load_graph_draw(c); 
  return TRUE;
}

static gint load_graph_expose(GtkWidget *widget,
			      GdkEventExpose *event,
			      gpointer data_ptr) {
  LoadGraph *g = (LoadGraph *) data_ptr;
  
  gdk_draw_pixmap(widget->window,
		  widget->style->fg_gc [GTK_WIDGET_STATE(widget)],
		  g->pixmap,
		  event->area.x, event->area.y,
		  event->area.x, event->area.y,
		  event->area.width, event->area.height);
  return FALSE;
}

static void load_graph_stop (LoadGraph *g) {
  if (g->timer_index != -1) {
    gtk_timeout_remove(g->timer_index);
    g->timer_index = -1;
  }
  if (!g)
    return;
  g->draw = FALSE;
}

static void load_graph_destroy(GtkWidget *widget, 
			       gpointer data_ptr) {
  LoadGraph *g = (LoadGraph *) data_ptr;  
  load_graph_stop(g);  
  if (g->timer_index != -1)
    gtk_timeout_remove (g->timer_index);
  load_graph_unalloc(g);
  FREE(g->colors);
  FREE(g);
}

static LoadGraph * load_graph_new(int statIdx,
				  const ProcConfig * config) {
  LoadGraph *g;
  unsigned int i;

  if (stats[statIdx].count > MAX_COLOR) {
    BREAK();
    return NULL;
  }
  
  g = MALLOC(sizeof(LoadGraph));
  g->statIdx = statIdx;
  g->count = stats[statIdx].count;
  g->speed = UPDATE_INTERVAL / cronMILLIS;
  g->num_points = 600;
  g->colors = MALLOC(sizeof(GdkColor) * (2+g->count));
  g->colors[0] = config->bg_color;
  g->colors[1] = config->frame_color;  
  for (i=0;i<g->count;i++) 
    g->colors[2+i] = config->mem_color[i];
  g->timer_index = -1;
  g->draw = FALSE;
  g->main_widget = gtk_vbox_new (FALSE, FALSE);
  gtk_widget_show(g->main_widget);
  g->disp = gtk_drawing_area_new();
  gtk_widget_show (g->disp);
  gtk_signal_connect(GTK_OBJECT (g->disp),
		     "expose_event",
		     GTK_SIGNAL_FUNC (load_graph_expose), g);
  gtk_signal_connect(GTK_OBJECT(g->disp), 
		     "configure_event",
		     GTK_SIGNAL_FUNC (load_graph_configure), g);
  gtk_signal_connect(GTK_OBJECT(g->disp),
		     "destroy",
		     GTK_SIGNAL_FUNC (load_graph_destroy), g); 
  gtk_widget_add_events(g->disp, 
			GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
  gtk_box_pack_start(GTK_BOX(g->main_widget), 
		     g->disp, 
		     TRUE, 
		     TRUE,
		     0);
  load_graph_alloc(g);  
  gtk_widget_show_all(g->main_widget);  
  g->timer_index = gtk_timeout_add(g->speed,
				   &load_graph_update, g);
  
  return g;
}

static GtkWidget * create_sys_view(int statIdx,
				   const ProcConfig * config) {
  GtkWidget * mem_frame;
  LoadGraph * mem_graph;

  mem_graph = load_graph_new(statIdx,
			     config);
  if (mem_graph == NULL)
    return NULL; /* oops */
  mem_frame = gtk_frame_new(_(stats[statIdx].frameName));
  gtk_container_add(GTK_CONTAINER(mem_frame),
		    mem_graph->main_widget);
  gtk_container_set_border_width(GTK_CONTAINER(mem_graph->main_widget),
				 GNOME_PAD_SMALL);
  gtk_container_set_border_width(GTK_CONTAINER(mem_frame),
				 GNOME_PAD_SMALL);
  gtk_widget_show(mem_frame);
  if (mem_graph->timer_index == -1)
    mem_graph->timer_index
      = gtk_timeout_add(mem_graph->speed,
			&load_graph_update,
			mem_graph);
  mem_graph->draw = TRUE;
  return mem_frame;
}





void init_stats() {
  GtkWidget * sys_box;
  GtkWidget * label;
  GtkWidget * notebook;
  ProcConfig config;
  int i;

  init_functions(); 
  notebook
    = glade_xml_get_widget(getMainXML(),
			   "downloadNotebook");
  memset(&config,
	 0, 
	 sizeof(ProcConfig));
  gdk_color_parse("black",
		  &config.bg_color);
  gdk_color_parse("gray",
		  &config.frame_color);
  gdk_color_parse("red",
		  &config.mem_color[0]);
  gdk_color_parse("green",
		  &config.mem_color[1]);
  gdk_color_parse("yellow",
		  &config.mem_color[2]);
  gdk_color_parse("blue",
		  &config.mem_color[3]);
  GNUNET_ASSERT(MAX_COLOR == 4);
  i = -1;
  while (stats[++i].paneName != NULL) {
    sys_box = create_sys_view(i,
			      &config);
    if (sys_box == NULL) 
      continue; /* oops */
    
    label = gtk_label_new(gettext(stats[i].paneName));
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
			     sys_box,
			     label);
  } 
  gtk_widget_show(notebook);
}

void done_stats() {
  done_functions();
}

 
/* end of statistics.c */