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-12-25 15:42:35 +00:00
|
|
|
#include <math.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"
|
2021-06-23 07:44:56 +00:00
|
|
|
#include "CPUMeter.h"
|
|
|
|
#include "DynamicMeter.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
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
Header* Header_new(ProcessList* pl, Settings* settings, HeaderLayout hLayout) {
|
2016-02-02 14:53:02 +00:00
|
|
|
Header* this = xCalloc(1, sizeof(Header));
|
2020-12-25 15:42:35 +00:00
|
|
|
this->columns = xMallocArray(HeaderLayout_getColumns(hLayout), sizeof(Vector*));
|
2015-01-23 05:08:21 +00:00
|
|
|
this->settings = settings;
|
|
|
|
this->pl = pl;
|
2020-12-25 15:42:35 +00:00
|
|
|
this->headerLayout = hLayout;
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
Header_forEachColumn(this, i) {
|
|
|
|
this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
|
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
|
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]);
|
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
free(this->columns);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
void Header_setLayout(Header* this, HeaderLayout hLayout) {
|
|
|
|
size_t oldColumns = HeaderLayout_getColumns(this->headerLayout);
|
|
|
|
size_t newColumns = HeaderLayout_getColumns(hLayout);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
this->headerLayout = hLayout;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
if (newColumns == oldColumns)
|
|
|
|
return;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
if (newColumns > oldColumns) {
|
|
|
|
this->columns = xReallocArray(this->columns, newColumns, sizeof(Vector*));
|
|
|
|
for (size_t i = oldColumns; i < newColumns; i++)
|
|
|
|
this->columns[i] = Vector_new(Class(Meter), true, DEFAULT_SIZE);
|
|
|
|
} else {
|
|
|
|
// move meters from to-be-deleted columns into last one
|
|
|
|
for (size_t i = newColumns; i < oldColumns; i++) {
|
|
|
|
for (int j = this->columns[i]->items - 1; j >= 0; j--) {
|
|
|
|
Vector_add(this->columns[newColumns - 1], Vector_take(this->columns[i], j));
|
2015-03-17 02:01:21 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
Vector_delete(this->columns[i]);
|
2015-03-17 02:01:21 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
this->columns = xReallocArray(this->columns, newColumns, sizeof(Vector*));
|
2015-03-17 02:01:21 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
|
|
|
|
Header_calculateHeight(this);
|
2015-03-17 02:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
static void Header_addMeterByName(Header* this, const char* name, MeterModeId mode, unsigned int column) {
|
|
|
|
assert(column < HeaderLayout_getColumns(this->headerLayout));
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
Vector* meters = this->columns[column];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
const char* paren = strchr(name, '(');
|
2021-02-17 16:38:35 +00:00
|
|
|
unsigned int param = 0;
|
2020-12-25 15:42:35 +00:00
|
|
|
size_t nameLen;
|
2006-04-10 20:40:38 +00:00
|
|
|
if (paren) {
|
2021-06-23 07:44:56 +00:00
|
|
|
int ok = sscanf(paren, "(%10u)", ¶m); // CPUMeter
|
|
|
|
if (!ok) {
|
2020-12-25 15:42:35 +00:00
|
|
|
char* end, dynamic[32] = {0};
|
2021-07-12 06:03:29 +00:00
|
|
|
if (sscanf(paren, "(%30s)", dynamic)) { // DynamicMeter
|
|
|
|
if ((end = strrchr(dynamic, ')')) == NULL)
|
2021-08-23 12:50:46 +00:00
|
|
|
return; // htoprc parse failure
|
2021-07-12 06:03:29 +00:00
|
|
|
*end = '\0';
|
2021-07-12 06:51:19 +00:00
|
|
|
if (!DynamicMeter_search(this->pl->dynamicMeters, dynamic, ¶m))
|
2021-08-23 12:50:46 +00:00
|
|
|
return; // name lookup failure
|
2020-12-25 15:42:35 +00:00
|
|
|
} else {
|
|
|
|
param = 0;
|
2021-07-12 06:03:29 +00:00
|
|
|
}
|
2021-06-23 07:44:56 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
nameLen = paren - name;
|
|
|
|
} else {
|
|
|
|
nameLen = strlen(name);
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
for (const MeterClass* const* type = Platform_meterTypes; *type; type++) {
|
2020-12-25 15:42:35 +00:00
|
|
|
if (0 == strncmp(name, (*type)->name, nameLen) && (*type)->name[nameLen] == '\0') {
|
2015-01-22 01:27:31 +00:00
|
|
|
Meter* meter = Meter_new(this->pl, param, *type);
|
2020-12-25 15:42:35 +00:00
|
|
|
if (mode != 0) {
|
|
|
|
Meter_setMode(meter, mode);
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
Vector_add(meters, meter);
|
2006-04-10 20:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Header_populateFromSettings(Header* this) {
|
|
|
|
Header_setLayout(this, this->settings->hLayout);
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
Header_forEachColumn(this, col) {
|
|
|
|
const MeterColumnSetting* colSettings = &this->settings->hColumns[col];
|
|
|
|
Vector_prune(this->columns[col]);
|
|
|
|
for (size_t i = 0; i < colSettings->len; i++) {
|
|
|
|
Header_addMeterByName(this, colSettings->names[i], colSettings->modes[i], col);
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
Header_calculateHeight(this);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
void Header_writeBackToSettings(const Header* this) {
|
|
|
|
Settings_setHeaderLayout(this->settings, this->headerLayout);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
Header_forEachColumn(this, col) {
|
|
|
|
MeterColumnSetting* colSettings = &this->settings->hColumns[col];
|
|
|
|
|
|
|
|
if (colSettings->names) {
|
|
|
|
for (size_t j = 0; j < colSettings->len; j++)
|
|
|
|
free(colSettings->names[j]);
|
|
|
|
free(colSettings->names);
|
|
|
|
}
|
|
|
|
free(colSettings->modes);
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
const Vector* vec = this->columns[col];
|
|
|
|
int len = Vector_size(vec);
|
|
|
|
|
|
|
|
colSettings->names = len ? xCalloc(len, sizeof(char*)) : NULL;
|
|
|
|
colSettings->modes = len ? xCalloc(len, sizeof(int)) : NULL;
|
|
|
|
colSettings->len = len;
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
const Meter* meter = (Meter*) Vector_get(vec, i);
|
|
|
|
char* name;
|
|
|
|
if (meter->param && As_Meter(meter) == &DynamicMeter_class) {
|
|
|
|
const char* dynamic = DynamicMeter_lookup(this->pl->dynamicMeters, meter->param);
|
|
|
|
xAsprintf(&name, "%s(%s)", As_Meter(meter)->name, dynamic);
|
|
|
|
} else if (meter->param && As_Meter(meter) == &CPUMeter_class) {
|
|
|
|
xAsprintf(&name, "%s(%u)", As_Meter(meter)->name, meter->param);
|
|
|
|
} else {
|
|
|
|
xAsprintf(&name, "%s", As_Meter(meter)->name);
|
|
|
|
}
|
|
|
|
colSettings->names[i] = name;
|
|
|
|
colSettings->modes[i] = meter->mode;
|
|
|
|
}
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
Meter* Header_addMeterByClass(Header* this, const MeterClass* type, unsigned int param, unsigned int column) {
|
|
|
|
assert(column < HeaderLayout_getColumns(this->headerLayout));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Meter_initFn(meter)) {
|
2015-01-22 01:27:31 +00:00
|
|
|
Meter_init(meter);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
}
|
2011-03-22 20:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header_draw(const Header* this) {
|
2021-01-18 14:23:23 +00:00
|
|
|
const int height = this->height;
|
|
|
|
const int pad = this->pad;
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
mvhline(y, 0, ' ', COLS);
|
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
const int width = COLS - pad;
|
2015-01-22 01:27:31 +00:00
|
|
|
int x = pad;
|
2020-12-25 15:42:35 +00:00
|
|
|
float roundingLoss = 0.0f;
|
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];
|
2020-12-25 15:42:35 +00:00
|
|
|
float colWidth = (float)width * HeaderLayout_layouts[this->headerLayout].widths[col] / 100.0f;
|
|
|
|
|
|
|
|
roundingLoss += colWidth - floorf(colWidth);
|
|
|
|
if (roundingLoss >= 1.0f) {
|
|
|
|
colWidth += 1.0f;
|
|
|
|
roundingLoss -= 1.0f;
|
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
for (int y = (pad / 2), i = 0; i < Vector_size(meters); i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
2021-01-18 14:23:23 +00:00
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
float actualWidth = colWidth;
|
|
|
|
if (meter->mode == TEXT_METERMODE) {
|
|
|
|
for (int j = 1; j < meter->columnWidthCount; j++) {
|
|
|
|
actualWidth += (float)width * HeaderLayout_layouts[this->headerLayout].widths[col + j] / 100.0f;
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 14:23:23 +00:00
|
|
|
|
2021-08-10 19:15:16 +00:00
|
|
|
assert(meter->draw);
|
2020-12-25 15:42:35 +00:00
|
|
|
meter->draw(meter, x, y, floorf(actualWidth));
|
2015-01-22 01:27:31 +00:00
|
|
|
y += meter->h;
|
|
|
|
}
|
2020-12-25 15:42:35 +00:00
|
|
|
|
|
|
|
x += floorf(colWidth);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:38:49 +00:00
|
|
|
void Header_updateData(Header* this) {
|
|
|
|
Header_forEachColumn(this, col) {
|
|
|
|
Vector* meters = this->columns[col];
|
|
|
|
int items = Vector_size(meters);
|
|
|
|
for (int i = 0; i < items; i++) {
|
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
|
|
|
Meter_updateValues(meter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:23:23 +00:00
|
|
|
/*
|
|
|
|
* Calculate how many columns the current meter is allowed to span,
|
|
|
|
* by counting how many columns to the right are empty or contain a BlankMeter.
|
|
|
|
* Returns the number of columns to span, i.e. if the direct neighbor is occupied 1.
|
|
|
|
*/
|
2020-12-25 15:42:35 +00:00
|
|
|
static int calcColumnWidthCount(const Header* this, const Meter* curMeter, const int pad, const unsigned int curColumn, const int curHeight) {
|
|
|
|
for (size_t i = curColumn + 1; i < HeaderLayout_getColumns(this->headerLayout); i++) {
|
2021-01-18 14:23:23 +00:00
|
|
|
const Vector* meters = this->columns[i];
|
|
|
|
|
|
|
|
int height = pad;
|
|
|
|
for (int j = 0; j < Vector_size(meters); j++) {
|
|
|
|
const Meter* meter = (const Meter*) Vector_get(meters, j);
|
|
|
|
|
|
|
|
if (height >= curHeight + curMeter->h)
|
|
|
|
break;
|
|
|
|
|
|
|
|
height += meter->h;
|
|
|
|
if (height <= curHeight)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Object_isA((const Object*) meter, (const ObjectClass*) &BlankMeter_class))
|
|
|
|
return i - curColumn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 15:42:35 +00:00
|
|
|
return HeaderLayout_getColumns(this->headerLayout) - curColumn;
|
2021-01-18 14:23:23 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
int Header_calculateHeight(Header* this) {
|
2021-01-18 14:23:23 +00:00
|
|
|
const int pad = this->settings->headerMargin ? 2 : 0;
|
2015-01-22 01:27:31 +00:00
|
|
|
int maxHeight = pad;
|
|
|
|
|
|
|
|
Header_forEachColumn(this, col) {
|
2021-01-05 22:42:55 +00:00
|
|
|
const Vector* meters = this->columns[col];
|
2015-01-22 01:27:31 +00:00
|
|
|
int height = pad;
|
|
|
|
for (int i = 0; i < Vector_size(meters); i++) {
|
2021-01-18 14:23:23 +00:00
|
|
|
Meter* meter = (Meter*) Vector_get(meters, i);
|
|
|
|
meter->columnWidthCount = calcColumnWidthCount(this, meter, pad, col, height);
|
2015-01-22 01:27:31 +00:00
|
|
|
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
|
|
|
}
|