Header.c 5.71 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
#include "CRT.h"
David Hunt's avatar
David Hunt committed
11
#include "StringUtils.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
#include "Meter.h"
21
#include "Settings.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
22
#include "Vector.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
23
24

typedef struct Header_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
25
   Vector** columns;
26
   Settings* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
   struct ProcessList_* pl;
   int nrColumns;
29
30
   int pad;
   int height;
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33
34
35
36
37
38
} Header;

}*/

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

Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
42
#ifndef Header_forEachColumn
#define Header_forEachColumn(this_, i_) for (int i_=0; i_ < this->nrColumns; i_++)
#endif

43
Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) {
44
   Header* this = calloc(1, sizeof(Header));
Hisham Muhammad's avatar
Hisham Muhammad committed
45
   this->columns = calloc(nrColumns, sizeof(Vector*));
46
47
   this->settings = settings;
   this->pl = pl;
Hisham Muhammad's avatar
Hisham Muhammad committed
48
49
50
51
   this->nrColumns = nrColumns;
   Header_forEachColumn(this, i) {
      this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53
54
55
   return this;
}

void Header_delete(Header* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
56
57
58
59
   Header_forEachColumn(this, i) {
      Vector_delete(this->columns[i]);
   }
   free(this->columns);
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
   free(this);
}

63
64
65
66
67
void Header_populateFromSettings(Header* this) {
   Header_forEachColumn(this, col) {
      MeterColumnSettings* colSettings = &this->settings->columns[col];
      for (int i = 0; i < colSettings->len; i++) {
         Header_addMeterByName(this, colSettings->names[i], col);
68
69
70
         if (colSettings->modes[i] != 0) {
            Header_setMode(this, i, colSettings->modes[i], col);
         }
71
72
73
74
75
      }
   }
   Header_calculateHeight(this);
}

76
77
78
79
80
81
82
83
84
85
86
87
void Header_writeBackToSettings(const Header* this) {
   Header_forEachColumn(this, col) {
      MeterColumnSettings* colSettings = &this->settings->columns[col];
      
      String_freeArray(colSettings->names);
      free(colSettings->modes);
      
      Vector* vec = this->columns[col];
      int len = Vector_size(vec);
      
      colSettings->names = calloc(len+1, sizeof(char*));
      colSettings->modes = calloc(len, sizeof(int));
88
      colSettings->len = len;
89
90
91
      
      for (int i = 0; i < len; i++) {
         Meter* meter = (Meter*) Vector_get(vec, i);
Christian Hesse's avatar
Christian Hesse committed
92
         char* name = calloc(64, sizeof(char));
93
94
95
96
97
98
99
100
101
102
103
         if (meter->param) {
            snprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param);
         } else {
            snprintf(name, 63, "%s", As_Meter(meter)->name);
         }
         colSettings->names[i] = name;
         colSettings->modes[i] = meter->mode;
      }
   }
}

Hisham Muhammad's avatar
Hisham Muhammad committed
104
105
MeterModeId Header_addMeterByName(Header* this, char* name, int column) {
   Vector* meters = this->columns[column];
Hisham Muhammad's avatar
Hisham Muhammad committed
106

107
108
109
   char* paren = strchr(name, '(');
   int param = 0;
   if (paren) {
Hisham Muhammad's avatar
Hisham Muhammad committed
110
      int ok = sscanf(paren, "(%10d)", &param);
111
112
113
      if (!ok) param = 0;
      *paren = '\0';
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
114
   MeterModeId mode = TEXT_METERMODE;
115
   for (MeterClass** type = Platform_meterTypes; *type; type++) {
116
      if (String_eq(name, (*type)->name)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119
         Meter* meter = Meter_new(this->pl, param, *type);
         Vector_add(meters, meter);
         mode = meter->mode;
120
121
         break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
122
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
123
   return mode;
Hisham Muhammad's avatar
Hisham Muhammad committed
124
125
}

Hisham Muhammad's avatar
Hisham Muhammad committed
126
127
void Header_setMode(Header* this, int i, MeterModeId mode, int column) {
   Vector* meters = this->columns[column];
Hisham Muhammad's avatar
Hisham Muhammad committed
128

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

Hisham Muhammad's avatar
Hisham Muhammad committed
135
136
Meter* Header_addMeterByClass(Header* this, MeterClass* type, int param, int column) {
   Vector* meters = this->columns[column];
Hisham Muhammad's avatar
Hisham Muhammad committed
137

138
   Meter* meter = Meter_new(this->pl, param, type);
139
   Vector_add(meters, meter);
140
   return meter;
Hisham Muhammad's avatar
Hisham Muhammad committed
141
142
}

Hisham Muhammad's avatar
Hisham Muhammad committed
143
144
int Header_size(Header* this, int column) {
   Vector* meters = this->columns[column];
145
   return Vector_size(meters);
Hisham Muhammad's avatar
Hisham Muhammad committed
146
147
}

Hisham Muhammad's avatar
Hisham Muhammad committed
148
149
char* Header_readMeterName(Header* this, int i, int column) {
   Vector* meters = this->columns[column];
150
   Meter* meter = (Meter*) Vector_get(meters, i);
151

152
   int nameLen = strlen(Meter_name(meter));
153
154
   int len = nameLen + 100;
   char* name = malloc(len);
155
   strncpy(name, Meter_name(meter), nameLen);
156
157
158
159
160
   name[nameLen] = '\0';
   if (meter->param)
      snprintf(name + nameLen, len - nameLen, "(%d)", meter->param);

   return name;
Hisham Muhammad's avatar
Hisham Muhammad committed
161
162
}

Hisham Muhammad's avatar
Hisham Muhammad committed
163
164
MeterModeId Header_readMeterMode(Header* this, int i, int column) {
   Vector* meters = this->columns[column];
Hisham Muhammad's avatar
Hisham Muhammad committed
165

166
   Meter* meter = (Meter*) Vector_get(meters, i);
Hisham Muhammad's avatar
Hisham Muhammad committed
167
168
169
   return meter->mode;
}

170
void Header_reinit(Header* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
171
172
173
174
175
176
   Header_forEachColumn(this, col) {
      for (int i = 0; i < Vector_size(this->columns[col]); i++) {
         Meter* meter = (Meter*) Vector_get(this->columns[col], i);
         if (Meter_initFn(meter))
            Meter_init(meter);
      }
177
178
179
180
   }
}

void Header_draw(const Header* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
181
182
183
184
185
186
   int height = this->height;
   int pad = this->pad;
   attrset(CRT_colors[RESET_COLOR]);
   for (int y = 0; y < height; y++) {
      mvhline(y, 0, ' ', COLS);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
187
188
189
190
191
192
193
194
195
196
197
   int width = COLS / this->nrColumns - (pad * this->nrColumns - 1) - 1;
   int x = pad;
   
   Header_forEachColumn(this, col) {
      Vector* meters = this->columns[col];
      for (int y = (pad / 2), i = 0; i < Vector_size(meters); i++) {
         Meter* meter = (Meter*) Vector_get(meters, i);
         meter->draw(meter, x, y, width);
         y += meter->h;
      }
      x += width + pad;
Hisham Muhammad's avatar
Hisham Muhammad committed
198
199
200
201
   }
}

int Header_calculateHeight(Header* this) {
202
   int pad = this->settings->headerMargin ? 2 : 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
203
204
205
206
207
208
209
210
211
212
   int maxHeight = pad;

   Header_forEachColumn(this, col) {
      Vector* meters = this->columns[col];
      int height = pad;
      for (int i = 0; i < Vector_size(meters); i++) {
         Meter* meter = (Meter*) Vector_get(meters, i);
         height += meter->h;
      }
      maxHeight = MAX(maxHeight, height);
Hisham Muhammad's avatar
Hisham Muhammad committed
213
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
214
   this->height = maxHeight;
215
   this->pad = pad;
Hisham Muhammad's avatar
Hisham Muhammad committed
216
   return maxHeight;
Hisham Muhammad's avatar
Hisham Muhammad committed
217
}