Header.c 4.81 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-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Header.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
#include "CRT.h"
#include "String.h"
12
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13

Hisham Muhammad's avatar
Hisham Muhammad committed
14
#include <assert.h>
15
#include <time.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
#include <string.h>
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
18
19

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
20
21
#include "ProcessList.h"
#include "Meter.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
25
26
27
28

typedef enum HeaderSide_ {
   LEFT_HEADER,
   RIGHT_HEADER
} HeaderSide;

typedef struct Header_ {
29
30
   Vector* leftMeters;
   Vector* rightMeters;
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33
   ProcessList* pl;
   int height;
   int pad;
Hisham Muhammad's avatar
Hisham Muhammad committed
34
   bool margin;
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37
38
39
40
41
42
43
} Header;

}*/

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

Header* Header_new(ProcessList* pl) {
44
   Header* this = calloc(1, sizeof(Header));
45
46
   this->leftMeters = Vector_new(Class(Meter), true, DEFAULT_SIZE);
   this->rightMeters = Vector_new(Class(Meter), true, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49
50
51
52
   this->margin = true;
   this->pl = pl;
   return this;
}

void Header_delete(Header* this) {
53
54
   Vector_delete(this->leftMeters);
   Vector_delete(this->rightMeters);
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
   free(this);
}

void Header_createMeter(Header* this, char* name, HeaderSide side) {
59
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
                       ? this->leftMeters
                       : this->rightMeters;

63
64
65
   char* paren = strchr(name, '(');
   int param = 0;
   if (paren) {
Hisham Muhammad's avatar
Hisham Muhammad committed
66
      int ok = sscanf(paren, "(%10d)", &param);
67
68
69
      if (!ok) param = 0;
      *paren = '\0';
   }
70
   for (MeterClass** type = Platform_meterTypes; *type; type++) {
71
      if (String_eq(name, (*type)->name)) {
72
         Vector_add(meters, Meter_new(this->pl, param, *type));
73
74
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
75
76
77
   }
}

78
void Header_setMode(Header* this, int i, MeterModeId mode, HeaderSide side) {
79
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
80
81
82
                       ? this->leftMeters
                       : this->rightMeters;

83
84
   if (i >= Vector_size(meters))
      return;
85
   Meter* meter = (Meter*) Vector_get(meters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
86
87
88
   Meter_setMode(meter, mode);
}

89
Meter* Header_addMeter(Header* this, MeterClass* type, int param, HeaderSide side) {
90
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
91
92
93
                       ? this->leftMeters
                       : this->rightMeters;

94
   Meter* meter = Meter_new(this->pl, param, type);
95
   Vector_add(meters, meter);
96
   return meter;
Hisham Muhammad's avatar
Hisham Muhammad committed
97
98
99
}

int Header_size(Header* this, HeaderSide side) {
100
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
101
102
103
                       ? this->leftMeters
                       : this->rightMeters;

104
   return Vector_size(meters);
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
}

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

113
   int nameLen = strlen(Meter_name(meter));
114
115
   int len = nameLen + 100;
   char* name = malloc(len);
116
   strncpy(name, Meter_name(meter), nameLen);
117
118
119
120
121
   name[nameLen] = '\0';
   if (meter->param)
      snprintf(name + nameLen, len - nameLen, "(%d)", meter->param);

   return name;
Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
}

124
MeterModeId Header_readMeterMode(Header* this, int i, HeaderSide side) {
125
   Vector* meters = side == LEFT_HEADER
Hisham Muhammad's avatar
Hisham Muhammad committed
126
127
128
                       ? this->leftMeters
                       : this->rightMeters;

129
   Meter* meter = (Meter*) Vector_get(meters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
130
131
132
   return meter->mode;
}

133
134
135
void Header_reinit(Header* this) {
   for (int i = 0; i < Vector_size(this->leftMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
136
137
      if (Meter_initFn(meter))
         Meter_init(meter);
138
139
140
   }
   for (int i = 0; i < Vector_size(this->rightMeters); i++) {
      Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
141
142
      if (Meter_initFn(meter))
         Meter_init(meter);
143
144
145
146
   }
}

void Header_draw(const Header* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
147
148
149
150
151
152
   int height = this->height;
   int pad = this->pad;
   attrset(CRT_colors[RESET_COLOR]);
   for (int y = 0; y < height; y++) {
      mvhline(y, 0, ' ', COLS);
   }
153
154
   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
155
156
157
      meter->draw(meter, pad, y, COLS / 2 - (pad * 2 - 1) - 1);
      y += meter->h;
   }
158
159
   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
160
161
162
163
164
165
166
167
168
169
      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;

170
171
   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
172
173
      leftHeight += meter->h;
   }
174
175
   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
176
177
178
179
180
181
      rightHeight += meter->h;
   }
   this->pad = pad;
   this->height = MAX(leftHeight, rightHeight);
   return this->height;
}