Header.c 4.84 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
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

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

/*{

typedef enum HeaderSide_ {
   LEFT_HEADER,
   RIGHT_HEADER
} HeaderSide;

typedef struct Header_ {
   TypedVector* leftMeters;
   TypedVector* rightMeters;
   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));
   this->leftMeters = TypedVector_new(METER_CLASS, true, DEFAULT_SIZE);
   this->rightMeters = TypedVector_new(METER_CLASS, true, DEFAULT_SIZE);
   this->margin = true;
   this->pl = pl;
   return this;
}

void Header_delete(Header* this) {
   TypedVector_delete(this->leftMeters);
   TypedVector_delete(this->rightMeters);
   free(this);
}

void Header_createMeter(Header* this, char* name, HeaderSide side) {
   TypedVector* meters = side == LEFT_HEADER
                       ? this->leftMeters
                       : this->rightMeters;

56
57
58
59
60
61
62
63
64
65
66
67
   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)) {
         TypedVector_add(meters, Meter_new(this->pl, param, *type));
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
68
69
70
   }
}

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

   Meter* meter = (Meter*) TypedVector_get(meters, i);
   Meter_setMode(meter, mode);
}

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

85
86
87
   Meter* meter = Meter_new(this->pl, param, type);
   TypedVector_add(meters, meter);
   return meter;
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}

int Header_size(Header* this, HeaderSide side) {
   TypedVector* meters = side == LEFT_HEADER
                       ? this->leftMeters
                       : this->rightMeters;

   return TypedVector_size(meters);
}

char* Header_readMeterName(Header* this, int i, HeaderSide side) {
   TypedVector* meters = side == LEFT_HEADER
                       ? this->leftMeters
                       : this->rightMeters;
   Meter* meter = (Meter*) TypedVector_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) {
Hisham Muhammad's avatar
Hisham Muhammad committed
116
117
118
119
120
121
122
123
124
   TypedVector* meters = side == LEFT_HEADER
                       ? this->leftMeters
                       : this->rightMeters;

   Meter* meter = (Meter*) TypedVector_get(meters, i);
   return meter->mode;
}

void Header_defaultMeters(Header* this) {
125
   TypedVector_add(this->leftMeters, Meter_new(this->pl, 0, &AllCPUsMeter));
126
127
128
129
130
   TypedVector_add(this->leftMeters, Meter_new(this->pl, 0, &MemoryMeter));
   TypedVector_add(this->leftMeters, Meter_new(this->pl, 0, &SwapMeter));
   TypedVector_add(this->rightMeters, Meter_new(this->pl, 0, &TasksMeter));
   TypedVector_add(this->rightMeters, Meter_new(this->pl, 0, &LoadAverageMeter));
   TypedVector_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
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
}

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);
   }
   for (int y = (pad / 2), i = 0; i < TypedVector_size(this->leftMeters); i++) {
      Meter* meter = (Meter*) TypedVector_get(this->leftMeters, i);
      meter->draw(meter, pad, y, COLS / 2 - (pad * 2 - 1) - 1);
      y += meter->h;
   }
   for (int y = (pad / 2), i = 0; i < TypedVector_size(this->rightMeters); i++) {
      Meter* meter = (Meter*) TypedVector_get(this->rightMeters, i);
      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;

   for (int i = 0; i < TypedVector_size(this->leftMeters); i++) {
      Meter* meter = (Meter*) TypedVector_get(this->leftMeters, i);
      leftHeight += meter->h;
   }
   for (int i = 0; i < TypedVector_size(this->rightMeters); i++) {
      Meter* meter = (Meter*) TypedVector_get(this->rightMeters, i);
      rightHeight += meter->h;
   }
   this->pad = pad;
   this->height = MAX(leftHeight, rightHeight);
   return this->height;
}