Header.c 4.69 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - Header.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2006 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Header.h"
9
#include "Meter.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
12
13
14
15
16
17
18
19
20
21

#include "debug.h"
#include <assert.h>

/*{

typedef enum HeaderSide_ {
   LEFT_HEADER,
   RIGHT_HEADER
} HeaderSide;

typedef struct Header_ {
22
23
   Vector* leftMeters;
   Vector* rightMeters;
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   ProcessList* pl;
   bool margin;
   int height;
   int pad;
} Header;

}*/

#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif

Header* Header_new(ProcessList* pl) {
   Header* this = malloc(sizeof(Header));
38
39
   this->leftMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE, NULL);
   this->rightMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE, NULL);
Hisham Muhammad's avatar
Hisham Muhammad committed
40
41
42
43
44
45
   this->margin = true;
   this->pl = pl;
   return this;
}

void Header_delete(Header* this) {
46
47
   Vector_delete(this->leftMeters);
   Vector_delete(this->rightMeters);
Hisham Muhammad's avatar
Hisham Muhammad committed
48
49
50
51
   free(this);
}

void Header_createMeter(Header* this, char* name, HeaderSide side) {
52
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
53
54
55
                       ? this->leftMeters
                       : this->rightMeters;

56
57
58
59
60
61
62
63
64
   char* paren = strchr(name, '(');
   int param = 0;
   if (paren) {
      int ok = sscanf(paren, "(%d)", &param);
      if (!ok) param = 0;
      *paren = '\0';
   }
   for (MeterType** type = Meter_types; *type; type++) {
      if (String_eq(name, (*type)->name)) {
65
         Vector_add(meters, Meter_new(this->pl, param, *type));
66
67
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
70
   }
}

71
void Header_setMode(Header* this, int i, MeterModeId mode, HeaderSide side) {
72
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
73
74
75
                       ? this->leftMeters
                       : this->rightMeters;

76
   Meter* meter = (Meter*) Vector_get(meters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
77
78
79
   Meter_setMode(meter, mode);
}

80
Meter* Header_addMeter(Header* this, MeterType* type, int param, HeaderSide side) {
81
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
                       ? this->leftMeters
                       : this->rightMeters;

85
   Meter* meter = Meter_new(this->pl, param, type);
86
   Vector_add(meters, meter);
87
   return meter;
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
}

int Header_size(Header* this, HeaderSide side) {
91
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
92
93
94
                       ? this->leftMeters
                       : this->rightMeters;

95
   return Vector_size(meters);
Hisham Muhammad's avatar
Hisham Muhammad committed
96
97
98
}

char* Header_readMeterName(Header* this, int i, HeaderSide side) {
99
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
100
101
                       ? this->leftMeters
                       : this->rightMeters;
102
   Meter* meter = (Meter*) Vector_get(meters, i);
103
104
105
106
107
108
109
110
111
112

   int nameLen = strlen(meter->type->name);
   int len = nameLen + 100;
   char* name = malloc(len);
   strncpy(name, meter->type->name, nameLen);
   name[nameLen] = '\0';
   if (meter->param)
      snprintf(name + nameLen, len - nameLen, "(%d)", meter->param);

   return name;
Hisham Muhammad's avatar
Hisham Muhammad committed
113
114
}

115
MeterModeId Header_readMeterMode(Header* this, int i, HeaderSide side) {
116
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119
                       ? this->leftMeters
                       : this->rightMeters;

120
   Meter* meter = (Meter*) Vector_get(meters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
124
   return meter->mode;
}

void Header_defaultMeters(Header* this) {
125
126
127
128
129
130
   Vector_add(this->leftMeters, Meter_new(this->pl, 0, &AllCPUsMeter));
   Vector_add(this->leftMeters, Meter_new(this->pl, 0, &MemoryMeter));
   Vector_add(this->leftMeters, Meter_new(this->pl, 0, &SwapMeter));
   Vector_add(this->rightMeters, Meter_new(this->pl, 0, &TasksMeter));
   Vector_add(this->rightMeters, Meter_new(this->pl, 0, &LoadAverageMeter));
   Vector_add(this->rightMeters, Meter_new(this->pl, 0, &UptimeMeter));
Hisham Muhammad's avatar
Hisham Muhammad committed
131
132
133
134
135
136
137
138
139
140
}

void Header_draw(Header* this) {
   int height = this->height;
   int pad = this->pad;
   
   attrset(CRT_colors[RESET_COLOR]);
   for (int y = 0; y < height; y++) {
      mvhline(y, 0, ' ', COLS);
   }
141
142
   for (int y = (pad / 2), i = 0; i < Vector_size(this->leftMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
143
144
145
      meter->draw(meter, pad, y, COLS / 2 - (pad * 2 - 1) - 1);
      y += meter->h;
   }
146
147
   for (int y = (pad / 2), i = 0; i < Vector_size(this->rightMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
148
149
150
151
152
153
154
155
156
157
      meter->draw(meter, COLS / 2 + pad, y, COLS / 2 - (pad * 2 - 1) - 1);
      y += meter->h;
   }
}

int Header_calculateHeight(Header* this) {
   int pad = this->margin ? 2 : 0;
   int leftHeight = pad;
   int rightHeight = pad;

158
159
   for (int i = 0; i < Vector_size(this->leftMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
160
161
      leftHeight += meter->h;
   }
162
163
   for (int i = 0; i < Vector_size(this->rightMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
164
165
166
167
168
169
      rightHeight += meter->h;
   }
   this->pad = pad;
   this->height = MAX(leftHeight, rightHeight);
   return this->height;
}