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
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Header.h"
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
2014-11-27 21:18:14 +00:00
|
|
|
#include "Platform.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "ProvideCurses.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
Header* Header_new(struct ProcessList_* pl, Settings* settings, int nrColumns) {
|
2016-02-02 14:53:02 +00:00
|
|
|
Header* this = xCalloc(1, sizeof(Header));
|
|
|
|
this->columns = xCalloc(nrColumns, sizeof(Vector*));
|
2015-01-23 05:08:21 +00:00
|
|
|
this->settings = settings;
|
|
|
|
this->pl = pl;
|
2015-01-22 01:27:31 +00:00
|
|
|
this->nrColumns = nrColumns;
|
|
|
|
Header_forEachColumn(this, i) {
|
|
|
|
this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header_delete(Header* this) {
|
2015-01-22 01:27:31 +00:00
|
|
|
Header_forEachColumn(this, i) {
|
|
|
|
Vector_delete(this->columns[i]);
|
|
|
|
}
|
|
|
|
free(this->columns);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:08:21 +00:00
|
|
|
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);
|
2015-03-17 02:01:21 +00:00
|
|
|
if (colSettings->modes[i] != 0) {
|
|
|
|
Header_setMode(this, i, colSettings->modes[i], col);
|
|
|
|
}
|
2015-01-23 05:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Header_calculateHeight(this);
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:01:21 +00:00
|
|
|
void Header_writeBackToSettings(const Header* this) {
|
|
|
|
Header_forEachColumn(this, col) {
|
|
|
|
MeterColumnSettings* colSettings = &this->settings->columns[col];
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-03-17 02:01:21 +00:00
|
|
|
String_freeArray(colSettings->names);
|
|
|
|
free(colSettings->modes);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-03-17 02:01:21 +00:00
|
|
|
Vector* vec = this->columns[col];
|
|
|
|
int len = Vector_size(vec);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2016-02-02 14:53:02 +00:00
|
|
|
colSettings->names = xCalloc(len+1, sizeof(char*));
|
|
|
|
colSettings->modes = xCalloc(len, sizeof(int));
|
2015-06-11 23:35:12 +00:00
|
|
|
colSettings->len = len;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-03-17 02:01:21 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(vec, i);
|
2016-02-02 14:53:02 +00:00
|
|
|
char* name = xCalloc(64, sizeof(char));
|
2015-03-17 02:01:21 +00:00
|
|
|
if (meter->param) {
|
2017-07-27 19:07:50 +00:00
|
|
|
xSnprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param);
|
2015-03-17 02:01:21 +00:00
|
|
|
} else {
|
2017-07-27 19:07:50 +00:00
|
|
|
xSnprintf(name, 63, "%s", As_Meter(meter)->name);
|
2015-03-17 02:01:21 +00:00
|
|
|
}
|
|
|
|
colSettings->names[i] = name;
|
|
|
|
colSettings->modes[i] = meter->mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
MeterModeId Header_addMeterByName(Header* this, char* name, int column) {
|
|
|
|
Vector* meters = this->columns[column];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
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';
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
MeterModeId mode = TEXT_METERMODE;
|
2020-10-05 11:19:50 +00:00
|
|
|
for (const MeterClass* const* type = Platform_meterTypes; *type; type++) {
|
2006-04-10 20:40:38 +00:00
|
|
|
if (String_eq(name, (*type)->name)) {
|
2015-01-22 01:27:31 +00:00
|
|
|
Meter* meter = Meter_new(this->pl, param, *type);
|
|
|
|
Vector_add(meters, meter);
|
|
|
|
mode = meter->mode;
|
2006-04-10 20:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2018-11-11 02:03:33 +00:00
|
|
|
if (paren)
|
|
|
|
*paren = '(';
|
2015-01-22 01:27:31 +00:00
|
|
|
return mode;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
void Header_setMode(Header* this, int i, MeterModeId mode, int column) {
|
|
|
|
Vector* meters = this->columns[column];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, int param, int column) {
|
2015-01-22 01:27:31 +00:00
|
|
|
Vector* meters = this->columns[column];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
int Header_size(Header* this, int column) {
|
|
|
|
Vector* meters = this->columns[column];
|
2006-05-30 13:45:40 +00:00
|
|
|
return Vector_size(meters);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
char* Header_readMeterName(Header* this, int i, int column) {
|
|
|
|
Vector* meters = this->columns[column];
|
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;
|
2016-02-02 14:53:02 +00:00
|
|
|
char* name = xMalloc(len);
|
2020-08-20 19:31:50 +00:00
|
|
|
memcpy(name, Meter_name(meter), nameLen);
|
2006-04-10 20:40:38 +00:00
|
|
|
name[nameLen] = '\0';
|
|
|
|
if (meter->param)
|
2017-07-27 19:07:50 +00:00
|
|
|
xSnprintf(name + nameLen, len - nameLen, "(%d)", meter->param);
|
2006-04-10 20:40:38 +00:00
|
|
|
|
|
|
|
return name;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
MeterModeId Header_readMeterMode(Header* this, int i, int column) {
|
|
|
|
Vector* meters = this->columns[column];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
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) {
|
2015-01-22 01:27:31 +00:00
|
|
|
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);
|
|
|
|
}
|
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);
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
int width = COLS / this->nrColumns - (pad * this->nrColumns - 1) - 1;
|
|
|
|
int x = pad;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
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;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Header_calculateHeight(Header* this) {
|
2015-01-23 05:08:21 +00:00
|
|
|
int pad = this->settings->headerMargin ? 2 : 0;
|
2015-01-22 01:27:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-09-09 06:56:04 +00:00
|
|
|
maxHeight = MAXIMUM(maxHeight, height);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
this->height = maxHeight;
|
2015-01-23 05:08:21 +00:00
|
|
|
this->pad = pad;
|
2015-01-22 01:27:31 +00:00
|
|
|
return maxHeight;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|