~ubuntu-branches/ubuntu/dapper/htop/dapper

« back to all changes in this revision

Viewing changes to UptimeMeter.c

  • Committer: Bazaar Package Importer
  • Author(s): Bartosz Fenski
  • Date: 2004-11-27 10:10:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041127101017-vwknw2ipfvxchno4
Tags: 0.5-1
* New upstream version.
  - fixes problem with wrongly displayed CPU bar (Closes: #283212)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
htop
 
3
(C) 2004 Hisham H. Muhammad
 
4
Released under the GNU GPL, see the COPYING file
 
5
in the source distribution for its full text.
 
6
*/
 
7
 
 
8
#include "UptimeMeter.h"
 
9
#include "Meter.h"
 
10
 
 
11
#include "ProcessList.h"
 
12
 
 
13
#include "CRT.h"
 
14
 
 
15
#include "debug.h"
 
16
 
 
17
/*{
 
18
 
 
19
typedef struct UptimeMeter_ UptimeMeter;
 
20
 
 
21
struct UptimeMeter_ {
 
22
   Meter super;
 
23
   ProcessList* pl;
 
24
   int seconds;
 
25
   int minutes;
 
26
   int hours;
 
27
   int days;
 
28
};
 
29
 
 
30
}*/
 
31
 
 
32
UptimeMeter* UptimeMeter_new() {
 
33
   UptimeMeter* this = malloc(sizeof(UptimeMeter));
 
34
   Meter_init((Meter*)this, String_copy("Uptime"), String_copy("Uptime: "), 1);
 
35
   ((Meter*)this)->attributes[0] = CRT_colors[UPTIME];
 
36
   ((Object*)this)->display = UptimeMeter_display;
 
37
   ((Meter*)this)->setValues = UptimeMeter_setValues;
 
38
   Meter_setMode((Meter*)this, TEXT);
 
39
   ((Meter*)this)->total = 100.0;
 
40
   return this;
 
41
}
 
42
 
 
43
void UptimeMeter_setValues(Meter* cast) {
 
44
   UptimeMeter* this = (UptimeMeter*)cast;
 
45
   double uptime;
 
46
   FILE* fd = fopen(PROCDIR "/uptime", "r");
 
47
   fscanf(fd, "%lf", &uptime);
 
48
   fclose(fd);
 
49
   int totalseconds = (int) ceil(uptime);
 
50
   this->seconds = totalseconds % 60;
 
51
   this->minutes = (totalseconds-this->seconds) % 3600 / 60;
 
52
   this->hours = (totalseconds-this->seconds-(this->minutes*60)) % 86400 / 3600;
 
53
   this->days = (totalseconds-this->seconds-(this->minutes*60)-(this->hours*3600)) / 86400;
 
54
   cast->values[0] = this->days;
 
55
   if (this->days > cast->total) {
 
56
      cast->total = this->days;
 
57
   }
 
58
   snprintf(cast->displayBuffer.c, 14, "%d", this->days);
 
59
}
 
60
 
 
61
void UptimeMeter_display(Object* cast, RichString* out) {
 
62
   UptimeMeter* this = (UptimeMeter*)cast;
 
63
   char buffer[20];
 
64
   RichString_prune(out);
 
65
   if (this->days > 100) {
 
66
      sprintf(buffer, "%d days, ", this->days);
 
67
      RichString_write(out, CRT_colors[LARGE_NUMBER], buffer);
 
68
   } else if (this->days > 1) {
 
69
      sprintf(buffer, "%d days, ", this->days);
 
70
      RichString_write(out, CRT_colors[UPTIME], buffer);
 
71
   } else if (this->days == 1) {
 
72
      sprintf(buffer, "%d day, ", this->days);
 
73
      RichString_write(out, CRT_colors[UPTIME], buffer);
 
74
   }
 
75
   sprintf(buffer, "%02d:%02d:%02d ", this->hours, this->minutes, this->seconds);
 
76
   RichString_append(out, CRT_colors[UPTIME], buffer);
 
77
}