2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - Header.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2006-03-04 18:16:49 +00:00
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Header.h"
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "CRT.h"
|
|
|
|
#include "String.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "Platform.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <assert.h>
|
2011-03-22 20:37:08 +00:00
|
|
|
#include <time.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
/*{
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "Meter.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
typedef enum HeaderSide_ {
|
|
|
|
LEFT_HEADER,
|
|
|
|
RIGHT_HEADER
|
|
|
|
} HeaderSide;
|
|
|
|
|
|
|
|
typedef struct Header_ {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* leftMeters;
|
|
|
|
Vector* rightMeters;
|
2006-03-04 18:16:49 +00:00
|
|
|
ProcessList* pl;
|
|
|
|
int height;
|
|
|
|
int pad;
|
2014-02-27 19:35:22 +00:00
|
|
|
bool margin;
|
2006-03-04 18:16:49 +00:00
|
|
|
} Header;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Header* Header_new(ProcessList* pl) {
|
2014-01-16 20:51:16 +00:00
|
|
|
Header* this = calloc(1, sizeof(Header));
|
2012-12-05 15:12:20 +00:00
|
|
|
this->leftMeters = Vector_new(Class(Meter), true, DEFAULT_SIZE);
|
|
|
|
this->rightMeters = Vector_new(Class(Meter), true, DEFAULT_SIZE);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->margin = true;
|
|
|
|
this->pl = pl;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header_delete(Header* this) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_delete(this->leftMeters);
|
|
|
|
Vector_delete(this->rightMeters);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header_createMeter(Header* this, char* name, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
char* paren = strchr(name, '(');
|
|
|
|
int param = 0;
|
|
|
|
if (paren) {
|
2014-04-21 22:18:08 +00:00
|
|
|
int ok = sscanf(paren, "(%10d)", ¶m);
|
2006-04-10 20:40:38 +00:00
|
|
|
if (!ok) param = 0;
|
|
|
|
*paren = '\0';
|
|
|
|
}
|
2014-11-27 21:18:14 +00:00
|
|
|
for (MeterClass** type = Platform_meterTypes; *type; type++) {
|
2006-04-10 20:40:38 +00:00
|
|
|
if (String_eq(name, (*type)->name)) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_add(meters, Meter_new(this->pl, param, *type));
|
2006-04-10 20:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
void Header_setMode(Header* this, int i, MeterModeId mode, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
|
|
|
|
2007-11-26 22:06:25 +00:00
|
|
|
if (i >= Vector_size(meters))
|
|
|
|
return;
|
2006-05-30 13:45:40 +00:00
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
Meter_setMode(meter, mode);
|
|
|
|
}
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter* Header_addMeter(Header* this, MeterClass* type, int param, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
Meter* meter = Meter_new(this->pl, param, type);
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_add(meters, meter);
|
2006-04-10 20:40:38 +00:00
|
|
|
return meter;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Header_size(Header* this, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
return Vector_size(meters);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* Header_readMeterName(Header* this, int i, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
2006-05-30 13:45:40 +00:00
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
int nameLen = strlen(Meter_name(meter));
|
2006-04-10 20:40:38 +00:00
|
|
|
int len = nameLen + 100;
|
|
|
|
char* name = malloc(len);
|
2012-12-05 15:12:20 +00:00
|
|
|
strncpy(name, Meter_name(meter), nameLen);
|
2006-04-10 20:40:38 +00:00
|
|
|
name[nameLen] = '\0';
|
|
|
|
if (meter->param)
|
|
|
|
snprintf(name + nameLen, len - nameLen, "(%d)", meter->param);
|
|
|
|
|
|
|
|
return name;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
MeterModeId Header_readMeterMode(Header* this, int i, HeaderSide side) {
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* meters = side == LEFT_HEADER
|
2006-03-04 18:16:49 +00:00
|
|
|
? this->leftMeters
|
|
|
|
: this->rightMeters;
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
return meter->mode;
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
void Header_reinit(Header* this) {
|
|
|
|
for (int i = 0; i < Vector_size(this->leftMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Meter_initFn(meter))
|
|
|
|
Meter_init(meter);
|
2011-03-22 20:37:08 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < Vector_size(this->rightMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Meter_initFn(meter))
|
|
|
|
Meter_init(meter);
|
2011-03-22 20:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header_draw(const Header* this) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int height = this->height;
|
|
|
|
int pad = this->pad;
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
mvhline(y, 0, ' ', COLS);
|
|
|
|
}
|
2006-05-30 13:45:40 +00:00
|
|
|
for (int y = (pad / 2), i = 0; i < Vector_size(this->leftMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
meter->draw(meter, pad, y, COLS / 2 - (pad * 2 - 1) - 1);
|
|
|
|
y += meter->h;
|
|
|
|
}
|
2006-05-30 13:45:40 +00:00
|
|
|
for (int y = (pad / 2), i = 0; i < Vector_size(this->rightMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
|
|
|
|
2006-05-30 13:45:40 +00:00
|
|
|
for (int i = 0; i < Vector_size(this->leftMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->leftMeters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
leftHeight += meter->h;
|
|
|
|
}
|
2006-05-30 13:45:40 +00:00
|
|
|
for (int i = 0; i < Vector_size(this->rightMeters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(this->rightMeters, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
rightHeight += meter->h;
|
|
|
|
}
|
|
|
|
this->pad = pad;
|
|
|
|
this->height = MAX(leftHeight, rightHeight);
|
|
|
|
return this->height;
|
|
|
|
}
|