File: userbox.cc

package info (click to toggle)
gps 0.9.4-1.woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 964 kB
  • ctags: 928
  • sloc: cpp: 10,822; sh: 381; makefile: 307; ansic: 227; perl: 17
file content (677 lines) | stat: -rw-r--r-- 16,187 bytes parent folder | download
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* $Id: userbox.cc,v 1.10 2001/02/23 13:12:54 bergo Exp $ */

/*

    GPS - Graphical Process Statistics
    Copyright (C) 1999-2000 Felipe Paulo Guazzi Bergo
    bergo@seul.org

    This program 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 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <gtk/gtk.h>
#include "transient.h"

#include "polling.h"
#include "userbox.h"
#include "userinfo.h"
#include "importglobals.h"

GtkWidget *ubox=NULL;
GtkWidget *da;
GdkPixmap *ucanvas=NULL;

GdkCursor *point,*hand;

GList *dcpu=NULL,*dmem=NULL;
GList *ulinks=NULL;

gint to_ubox=-1;

void pop_user_box(GtkWidget *gw,gpointer data) {
  GtkWidget *v1,*h1,*b1,*hs;
  char title[512],bu[128];

  if (netcritical)
    return;

  if (ubox!=NULL)
    return;

  point=gdk_cursor_new(GDK_LEFT_PTR);
  hand=gdk_cursor_new(GDK_HAND2);

  ubox=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(ubox),GTK_WIN_POS_CENTER);
  gtk_widget_realize(ubox);
  gtk_window_set_policy(GTK_WINDOW(ubox),TRUE,TRUE,TRUE);
  gethostname(bu,256);
  sprintf(title,"gPS: User CPU and Memory Demand (%s)",bu);
  gtk_window_set_title(GTK_WINDOW(ubox),title);
  gtk_container_set_border_width(GTK_CONTAINER(ubox),4);

  v1=gtk_vbox_new(FALSE,2);
  gtk_container_add(GTK_CONTAINER(ubox),v1);

  da=gtk_drawing_area_new();
  gtk_widget_set_events(da,GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK|
			GDK_POINTER_MOTION_MASK);
  gtk_drawing_area_size(GTK_DRAWING_AREA(da),500,300);

  gtk_box_pack_start(GTK_BOX(v1),da,TRUE,TRUE,2);

  hs=gtk_hseparator_new();
  gtk_box_pack_start(GTK_BOX(v1),hs,FALSE,FALSE,4);

  h1=gtk_hbox_new(FALSE,2);
  gtk_box_pack_start(GTK_BOX(v1),h1,FALSE,TRUE,2);

  b1=gtk_button_new_with_label("   Dismiss   ");
  gtk_box_pack_end(GTK_BOX(h1),b1,FALSE,FALSE,2);

  gtk_signal_connect(GTK_OBJECT(b1),"clicked",
		     GTK_SIGNAL_FUNC(close_user_box),NULL);
  gtk_signal_connect (GTK_OBJECT (ubox), "destroy",
		      GTK_SIGNAL_FUNC (destroy_user_box), NULL);

  gtk_signal_connect (GTK_OBJECT (da), "expose_event",
		      (GtkSignalFunc) ubox_expose_event, NULL);
  gtk_signal_connect (GTK_OBJECT (da), "configure_event",
		      (GtkSignalFunc) ubox_configure_event, NULL);
  gtk_signal_connect (GTK_OBJECT (da), "button_press_event",
		      (GtkSignalFunc) ubox_bpress_event, NULL);
  gtk_signal_connect (GTK_OBJECT (da), "motion_notify_event",
		      (GtkSignalFunc) ubox_motion_event, NULL);

  gtk_widget_show(v1);
  gtk_widget_show(da);
  gtk_widget_show(h1);
  gtk_widget_show(hs);
  gtk_widget_show(b1);

  gtk_widget_show(ubox);

  refresh_ubox(NULL);
  to_ubox=gtk_timeout_add(1000,refresh_ubox,NULL);
}

void close_user_box(GtkWidget *w, gpointer data) {
  gtk_widget_destroy(ubox);
  ubox=NULL;
}

void destroy_user_box(GtkWidget *w,gpointer data) {
  int i;
  if (to_ubox>=0)
    gtk_timeout_remove(to_ubox);
  to_ubox=-1;

  gdk_pixmap_unref(ucanvas); ucanvas=NULL;
  gdk_cursor_destroy(point);
  gdk_cursor_destroy(hand);
  ubox_reset_lists();
  ubox_reset_links();
  ubox=NULL;
}

// drawing

gboolean ubox_configure_event(GtkWidget *widget,GdkEventConfigure *ce,
			    gpointer data) {
  GdkGC *mygc;
  int x,y;
  int b,cy,h,lw,gb,gh,gsy;
  GList *pt;
  UserBoxMemTuple *p1;
  UserBoxCpuTuple *p2;
  GdkFont *fnt;
  float pct,gpct;
  char zb[16];
  UserLink *ul;
  char *lastgr;
  static char *placeholder="aaa";
  
  ubox_reset_links();

  if (ucanvas!=NULL)
    gdk_pixmap_unref(ucanvas);

  mygc=gdk_gc_new(widget->window);
  
  ucanvas=gdk_pixmap_new(widget->window,x=widget->allocation.width,
			  y=widget->allocation.height,-1);
  gdk_draw_rectangle(ucanvas,widget->style->black_gc,TRUE,0,0,x,y);

  fnt=gdk_font_load("-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");

  if (dmem!=NULL) {
    cy=20;
    b=0;

    lastgr=placeholder;
    gb=0;
    gsy=20;
    gh=0;
    gpct=0.0;

    for(pt=dmem;pt!=NULL;pt=g_list_next(pt)) {
      p1=(UserBoxMemTuple *)(pt->data);
      h=(int)(((float)(y-40))*(pct=ubox_mem_fraction(p1->membytes)));

      if (g_list_next(pt)==NULL)
	h=(y-20)-cy;
     
      // group
      if (strcmp(p1->groupname,lastgr)!=0) {
	gb=(++gb)%2;

	// paint label of last one
	if (gh>12) {
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
	  gdk_draw_string(ucanvas,fnt,mygc,
			  x/2+48-3-gdk_string_width(fnt,lastgr),
			  gsy+gh/2+5,lastgr);
	  gdk_rgb_gc_set_foreground(mygc,0);
	  sprintf(zb,"%d%%",(int)rint(gpct*100.0));
	  gdk_draw_string(ucanvas,fnt,mygc,
			  x/2+48+24-gdk_string_width(fnt,zb)/2,
			  gsy+gh/2+5,zb);
	}

	gsy+=gh;
	gh=0;
	gpct=0.0;
      }

      gh+=h;
      gpct+=pct;
      lastgr=p1->groupname;

      if (gb%2)
	gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_a&0x00ffff);
      else
	gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_b&0x00ffff);

      gdk_draw_rectangle(ucanvas,mygc,TRUE,x/2+48,cy,48,h);

      // individual
      if (b%2)
	gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_a);
      else
	gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_b);

      gdk_draw_rectangle(ucanvas,mygc,TRUE,96+x/2,cy,48,h);
      b=(++b)%2;

      if (h>12) {
	if (b%2)
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_c);
	else
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_mem_d);

	sprintf(zb,"%d%%",(int)rint(pct*100.0));
	gdk_draw_string(ucanvas,fnt,mygc,
			x/2+96+(48-gdk_string_width(fnt,zb))/2,
			cy+h/2+5,zb);
      }

      if (h>12) {
	gdk_rgb_gc_set_foreground(mygc,c_ubox_uline);
	gdk_draw_line(ucanvas,mygc,x/2+144+24,cy+h/2+6+3,
		      x/2+144+24+(lw=gdk_string_width(fnt,p1->username)),
		      cy+h/2+6+3);
	gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
	gdk_draw_line(ucanvas,mygc,x/2+144,cy+h/2,x/2+144+16,cy+h/2);
	gdk_draw_string(ucanvas,fnt,mygc,x/2+144+24,cy+h/2+6,p1->username);

	ul=new UserLink();
	strcpy(ul->username,p1->username);
	ul->x=x/2+144+24;
	ul->y=cy+h/2+6-12;
	ul->w=lw;
	ul->h=15;
	ulinks=g_list_append(ulinks,(gpointer)ul);
      }

      cy+=h;
    }

    // paint last one
    if (gh>12) {
      gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
      gdk_draw_string(ucanvas,fnt,mygc,
		      x/2+48-3-gdk_string_width(fnt,lastgr),
		      gsy+gh/2+5,lastgr);
      gdk_rgb_gc_set_foreground(mygc,0);
      sprintf(zb,"%d%%",(int)rint(gpct*100.0));
      gdk_draw_string(ucanvas,fnt,mygc,
		      x/2+48+24-gdk_string_width(fnt,zb)/2,
		      gsy+gh/2+5,zb);
    }

    gdk_rgb_gc_set_foreground(mygc,c_ubox_border);
    gdk_draw_rectangle(ucanvas,mygc,FALSE,x/2+48,20,96,y-41);
  }

  if (dcpu!=NULL) {
    cy=20;
    b=0;

    lastgr=placeholder;
    gb=0;
    gsy=20;
    gh=0;
    gpct=0.0;

    for(pt=dcpu;pt!=NULL;pt=g_list_next(pt)) {
      p2=(UserBoxCpuTuple *)(pt->data);
      h=(int)(((float)(y-40))*(pct=ubox_cpu_fraction(p2->cpuusage)));

      if (g_list_next(pt)==NULL)
	h=(y-20)-cy;

      // group
      if (strcmp(p2->groupname,lastgr)!=0) { 
	gb=(++gb)%2;

	// paint label of last one
	if (gh>12) {
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
	  gdk_draw_string(ucanvas,fnt,mygc,
			  15+48-3-gdk_string_width(fnt,lastgr),
			  gsy+gh/2+5,lastgr);
	  gdk_rgb_gc_set_foreground(mygc,0);
	  sprintf(zb,"%d%%",(int)rint(gpct*100.0));
	  gdk_draw_string(ucanvas,fnt,mygc,
			  15+48+24-gdk_string_width(fnt,zb)/2,
			  gsy+gh/2+5,zb);
	}
	gsy+=gh;
	gh=0;
	gpct=0.0;
      }

      gh+=h;
      gpct+=pct;

      if (gb%2)
	gdk_rgb_gc_set_foreground(mygc,0xff8050);
      else
	gdk_rgb_gc_set_foreground(mygc,0xffff80);

      gdk_draw_rectangle(ucanvas,mygc,TRUE,15+48,cy,48,h);
      lastgr=p2->groupname;

      // individual
      if (b%2)
	gdk_rgb_gc_set_foreground(mygc,c_ubox_cpu_a);
      else
	gdk_rgb_gc_set_foreground(mygc,c_ubox_cpu_b);

      gdk_draw_rectangle(ucanvas,mygc,TRUE,15+96,cy,48,h);
      b=(++b)%2;

      if (h>12) {
	if (b%2)
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_cpu_c);
	else
	  gdk_rgb_gc_set_foreground(mygc,c_ubox_cpu_d);
	sprintf(zb,"%d%%",(int)rint(pct*100.0));
	gdk_draw_string(ucanvas,fnt,mygc,
			15+96+(48-gdk_string_width(fnt,zb))/2,
			cy+h/2+5,zb);
      }

      if (h>12) {
	gdk_rgb_gc_set_foreground(mygc,c_ubox_uline);
	gdk_draw_line(ucanvas,mygc,15+144+24,cy+h/2+6+3,
		      15+144+24+gdk_string_width(fnt,p2->username),
		      cy+h/2+6+3);
	gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
	gdk_draw_line(ucanvas,mygc,15+144,cy+h/2,15+144+16,cy+h/2);
	gdk_draw_string(ucanvas,fnt,mygc,15+144+24,cy+h/2+6,p2->username);

	ul=new UserLink();
	strcpy(ul->username,p2->username);
	ul->x=15+144+24;
	ul->y=cy+h/2+6-12;
	ul->w=lw;
	ul->h=15;
	ulinks=g_list_append(ulinks,(gpointer)ul);
      }

      cy+=h;
    }

    // last one
    if (gh>12) {
      gdk_rgb_gc_set_foreground(mygc,c_ubox_link);
      gdk_draw_string(ucanvas,fnt,mygc,
		      15+48-3-gdk_string_width(fnt,lastgr),
		      gsy+gh/2+5,lastgr);
      gdk_rgb_gc_set_foreground(mygc,0);
      sprintf(zb,"%d%%",(int)rint(gpct*100.0));
      gdk_draw_string(ucanvas,fnt,mygc,
		      15+48+24-gdk_string_width(fnt,zb)/2,
		      gsy+gh/2+5,zb);
    }

    gdk_rgb_gc_set_foreground(mygc,c_ubox_border);
    gdk_draw_rectangle(ucanvas,mygc,FALSE,15+48,20,96,y-41);
  }

  gdk_rgb_gc_set_foreground(mygc,c_ubox_label);
  gdk_draw_string(ucanvas,fnt,mygc,15+96-gdk_string_width(fnt,"%CPU")/2,
		  y-5,"%CPU");
  gdk_draw_string(ucanvas,fnt,mygc,1+15+96-gdk_string_width(fnt,"%CPU")/2,
		  y-5,"%CPU");

  gdk_draw_string(ucanvas,fnt,mygc,15+48-gdk_string_width(fnt,"groups"),
		  y-5,"groups");
  gdk_draw_string(ucanvas,fnt,mygc,15+144,
		  y-5,"users");

  gdk_draw_string(ucanvas,fnt,mygc,x/2+96-gdk_string_width(fnt,"Memory")/2,
		  y-5,"Memory");
  gdk_draw_string(ucanvas,fnt,mygc,1+x/2+96-gdk_string_width(fnt,"Memory")/2,
		  y-5,"Memory");

  gdk_draw_string(ucanvas,fnt,mygc,x/2+48-gdk_string_width(fnt,"groups"),
		  y-5,"groups");
  gdk_draw_string(ucanvas,fnt,mygc,x/2+144,
		  y-5,"users");

  gdk_font_unref(fnt);
  gdk_gc_destroy(mygc);
  return FALSE;
}

gboolean ubox_expose_event(GtkWidget *widget,GdkEventExpose *ee,
			    gpointer data) {
  gdk_draw_pixmap(widget->window,
		  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		  ucanvas,
		  ee->area.x, ee->area.y,
		  ee->area.x, ee->area.y,
		  ee->area.width, ee->area.height);
  return FALSE;
}

/* update */

gint refresh_ubox(gpointer data) {
  GList *pd;
  GList *pr,*ps;
  UserBoxCpuTuple *ubct,*p1;
  UserBoxMemTuple *ubmt,*p2;
  ProcessListItem *pli;
  int ac;

  if (ubox==NULL)
    return FALSE;

  list_poller->poll();
  pd=g_list_copy(list_poller->process_list);

  ubox_reset_lists();

  for(pr=pd;pr!=NULL;pr=g_list_next(pr)) {
    
    pli=(ProcessListItem *)(pr->data);

    // cpu
    ac=0;
    for(ps=dcpu;ps!=NULL;ps=g_list_next(ps)) {
      p1=(UserBoxCpuTuple *)(ps->data);
      if (!strcmp(p1->username,pli->owner)) {
	ac=1;
	p1->cpuusage+=atof(strtok(pli->cpu,"%\n"));
      }	
    }
    
    if (!ac) {
      ubct=new UserBoxCpuTuple();
      strcpy(ubct->username,pli->owner);
      query_group(ubct->groupname,ubct->username,64);
      ubct->cpuusage=atof(strtok(pli->cpu,"%\n"));
      dcpu=g_list_append(dcpu,(gpointer)(ubct));
    }

    // mem
    ac=0;
    for(ps=dmem;ps!=NULL;ps=g_list_next(ps)) {
      p2=(UserBoxMemTuple *)(ps->data);
      if (!strcmp(p2->username,pli->owner)) {
	ac=1;
	p2->membytes+=gps_memvalue(pli->size);
      }	
    }
    
    if (!ac) {
      ubmt=new UserBoxMemTuple();
      strcpy(ubmt->username,pli->owner);
      query_group(ubmt->groupname,ubmt->username,64);
      ubmt->membytes=gps_memvalue(pli->size);
      dmem=g_list_append(dmem,(gpointer)(ubmt));
    }

  }

  /* sort everybody */

  dcpu=g_list_sort(dcpu,(GCompareFunc)ubox_cpu_cmp);
  dmem=g_list_sort(dmem,(GCompareFunc)ubox_mem_cmp);

  // now sort for group name
  dcpu=g_list_sort(dcpu,(GCompareFunc)ubox_cpu_cmp_gr);
  dmem=g_list_sort(dmem,(GCompareFunc)ubox_mem_cmp_gr);

  ubox_configure_event(da,NULL,NULL);
  gtk_widget_queue_draw(da);
  g_list_free(pd);
  return TRUE;
}

long gps_memvalue(char *s) {
  char t[128];
  long v;

  strcpy(t,s);
  v=atol(strtok(t,"KMGTP"));
  
  switch(s[strlen(s)-1]) {
  case 'K': v<<=10; break;
  case 'M': v<<=20; break;
  case 'G': v<<=30; break;
  }
  return v;
}

void query_group(char *dest,char *user,int maxc) {
  struct passwd *pws;
  struct group  *grs;
  pws=getpwnam(user);
  if (pws==NULL) { strncpy(dest,"(ENOMEM)",maxc); return; }
  grs=getgrgid(pws->pw_gid);
  if (grs==NULL) { strncpy(dest,"(ENOMEM)",maxc); return; }
  strncpy(dest,grs->gr_name,maxc);
}

gint ubox_mem_cmp(const gpointer a,const gpointer b) {
  UserBoxMemTuple *p1,*p2;
  p1=(UserBoxMemTuple *)a;
  p2=(UserBoxMemTuple *)b;
  if ((p1->membytes)>(p2->membytes))
    return 1;
  if ((p1->membytes)<(p2->membytes))
    return -1;
  return 0;
}

gint ubox_cpu_cmp(const gpointer a,const gpointer b) {
  UserBoxCpuTuple *p1,*p2;
  p1=(UserBoxCpuTuple *)a;
  p2=(UserBoxCpuTuple *)b;
  if ((p1->cpuusage)>(p2->cpuusage))
    return 1;
  if ((p1->cpuusage)<(p2->cpuusage))
    return -1;
  return 0;
}

gint ubox_mem_cmp_gr(const gpointer a,const gpointer b) {
  UserBoxMemTuple *p1,*p2;
  p1=(UserBoxMemTuple *)a;
  p2=(UserBoxMemTuple *)b;
  return(strcmp(p1->groupname,p2->groupname));
}

gint ubox_cpu_cmp_gr(const gpointer a,const gpointer b) {
  UserBoxCpuTuple *p1,*p2;
  p1=(UserBoxCpuTuple *)a;
  p2=(UserBoxCpuTuple *)b;
  return(strcmp(p1->groupname,p2->groupname));
}

void ubox_reset_lists() {
  GList *pt;
  if (dcpu!=NULL) {
    for(pt=dcpu;pt!=NULL;pt=g_list_next(pt))
      delete( (UserBoxCpuTuple *)(pt->data) );
    g_list_free(dcpu);
    dcpu=NULL;
  }
  if (dmem!=NULL) {
    for(pt=dmem;pt!=NULL;pt=g_list_next(pt))
      delete( (UserBoxMemTuple *)(pt->data) );
    g_list_free(dmem);
    dmem=NULL;
  }
}

void ubox_reset_links() {
  GList *pt;
  if (ulinks!=NULL) {
    for(pt=ulinks;pt!=NULL;pt=g_list_next(pt))
      delete( (UserLink *)(pt->data) );
    g_list_free(ulinks);
    ulinks=NULL;
  }
}

float ubox_mem_fraction(long value) {
  long tt;
  float a,b;
  GList *pt;
  UserBoxMemTuple *p1;
  if (dmem==NULL) return 0.0;
  
  tt=0;
  for(pt=dmem;pt!=NULL;pt=g_list_next(pt)) {
    p1=(UserBoxMemTuple *)(pt->data);
    tt+=p1->membytes;
  }

  a=(float)tt;
  if (a==0.0) a=1.0;
  b=(float)value;

  a=b/a;
  return(a);
}

float ubox_cpu_fraction(float value) {
  float tt;
  float b;
  GList *pt;
  UserBoxCpuTuple *p1;
  if (dcpu==NULL) return 0.0;
  
  tt=0.0;
  for(pt=dcpu;pt!=NULL;pt=g_list_next(pt)) {
    p1=(UserBoxCpuTuple *)(pt->data);
    tt+=p1->cpuusage;
  }

  if (tt==0.0) tt=1.0;

  b=value/tt;
  return(b);
}

/// USER LINK

int UserLink::hit(int a,int b) {
  if ((a>=x)&&(a<(x+w))&&(b>=y)&&(b<(y+h)))
    return 1;
  else
    return 0;
}

gboolean ubox_bpress_event(GtkWidget *widget,GdkEventButton *be,
			   gpointer data) {
  GList *pt;
  UserLink *ul;
  int x,y;

  if ((be==NULL)||(ulinks==NULL))
    return FALSE;

  x=(int)(be->x);
  y=(int)(be->y);

  for(pt=ulinks;pt!=NULL;pt=g_list_next(pt)) {
    ul=(UserLink *)(pt->data);
    if (ul->hit(x,y)) {
      uinfo_pop(ul->username);
      break;
    }
  }

  return FALSE;
}

gboolean ubox_motion_event(GtkWidget *widget,GdkEventMotion *gem,
			   gpointer data) {
  GList *pt;
  UserLink *ul;
  int x,y;

  if ((gem==NULL)||(ulinks==NULL))
    return FALSE;

  x=(int)(gem->x);
  y=(int)(gem->y);

  for(pt=ulinks;pt!=NULL;pt=g_list_next(pt)) {
    ul=(UserLink *)(pt->data);
    if (ul->hit(x,y)) {
      gdk_window_set_cursor(da->window,hand);
      return FALSE;
    }
  }
  gdk_window_set_cursor(da->window,point);
  return FALSE;
}