2021-06-23 07:44:56 +00:00
|
|
|
/*
|
|
|
|
htop - DynamicMeter.c
|
|
|
|
(C) 2021 htop dev team
|
|
|
|
(C) 2021 Red Hat, Inc. All Rights Reserved.
|
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
2021-07-18 04:25:45 +00:00
|
|
|
|
2021-06-23 07:44:56 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include "DynamicMeter.h"
|
|
|
|
|
2021-07-18 04:25:45 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2021-06-23 07:44:56 +00:00
|
|
|
#include "CRT.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "Platform.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "RichString.h"
|
|
|
|
#include "XUtils.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const int DynamicMeter_attributes[] = {
|
|
|
|
DYNAMIC_GRAY,
|
|
|
|
DYNAMIC_DARKGRAY,
|
|
|
|
DYNAMIC_RED,
|
|
|
|
DYNAMIC_GREEN,
|
|
|
|
DYNAMIC_BLUE,
|
|
|
|
DYNAMIC_CYAN,
|
|
|
|
DYNAMIC_MAGENTA,
|
|
|
|
DYNAMIC_YELLOW,
|
|
|
|
DYNAMIC_WHITE
|
|
|
|
};
|
|
|
|
|
|
|
|
Hashtable* DynamicMeters_new(void) {
|
|
|
|
return Platform_dynamicMeters();
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int key;
|
|
|
|
const char* name;
|
2021-07-12 06:03:29 +00:00
|
|
|
bool found;
|
2021-06-23 07:44:56 +00:00
|
|
|
} DynamicIterator;
|
|
|
|
|
|
|
|
static void DynamicMeter_compare(ht_key_t key, void* value, void* data) {
|
|
|
|
const DynamicMeter* meter = (const DynamicMeter*)value;
|
|
|
|
DynamicIterator* iter = (DynamicIterator*)data;
|
2021-07-12 06:03:29 +00:00
|
|
|
if (String_eq(iter->name, meter->name)) {
|
|
|
|
iter->found = true;
|
2021-06-23 07:44:56 +00:00
|
|
|
iter->key = key;
|
2021-07-12 06:03:29 +00:00
|
|
|
}
|
2021-06-23 07:44:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 06:51:19 +00:00
|
|
|
bool DynamicMeter_search(Hashtable* dynamics, const char* name, unsigned int* key) {
|
2021-07-12 06:03:29 +00:00
|
|
|
DynamicIterator iter = { .key = 0, .name = name, .found = false };
|
2021-07-12 06:51:19 +00:00
|
|
|
if (dynamics)
|
|
|
|
Hashtable_foreach(dynamics, DynamicMeter_compare, &iter);
|
2021-07-12 09:11:39 +00:00
|
|
|
if (key)
|
|
|
|
*key = iter.key;
|
2021-07-12 06:03:29 +00:00
|
|
|
return iter.found;
|
2021-06-23 07:44:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 06:51:19 +00:00
|
|
|
const char* DynamicMeter_lookup(Hashtable* dynamics, unsigned int key) {
|
|
|
|
const DynamicMeter* meter = Hashtable_get(dynamics, key);
|
2021-06-23 07:44:56 +00:00
|
|
|
return meter ? meter->name : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DynamicMeter_init(Meter* meter) {
|
|
|
|
Platform_dynamicMeterInit(meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DynamicMeter_updateValues(Meter* meter) {
|
|
|
|
Platform_dynamicMeterUpdateValues(meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DynamicMeter_display(const Object* cast, RichString* out) {
|
|
|
|
const Meter* meter = (const Meter*)cast;
|
|
|
|
Platform_dynamicMeterDisplay(meter, out);
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:57:03 +00:00
|
|
|
static const char* DynamicMeter_getCaption(const Meter* this) {
|
|
|
|
const ProcessList* pl = this->pl;
|
|
|
|
const DynamicMeter* meter = Hashtable_get(pl->dynamicMeters, this->param);
|
|
|
|
if (meter)
|
|
|
|
return meter->caption ? meter->caption : meter->name;
|
|
|
|
return this->caption;
|
|
|
|
}
|
|
|
|
|
2021-06-23 07:44:56 +00:00
|
|
|
static void DynamicMeter_getUiName(const Meter* this, char* name, size_t length) {
|
|
|
|
const ProcessList* pl = this->pl;
|
|
|
|
const DynamicMeter* meter = Hashtable_get(pl->dynamicMeters, this->param);
|
|
|
|
if (meter) {
|
2021-07-07 06:57:03 +00:00
|
|
|
const char* uiName = meter->caption;
|
|
|
|
if (uiName) {
|
|
|
|
int len = strlen(uiName);
|
2021-07-14 17:18:27 +00:00
|
|
|
if (len > 2 && uiName[len - 2] == ':')
|
2021-07-07 06:57:03 +00:00
|
|
|
len -= 2;
|
|
|
|
xSnprintf(name, length, "%.*s", len, uiName);
|
|
|
|
} else {
|
|
|
|
xSnprintf(name, length, "%s", meter->name);
|
|
|
|
}
|
2021-06-23 07:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MeterClass DynamicMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = DynamicMeter_display
|
|
|
|
},
|
|
|
|
.init = DynamicMeter_init,
|
|
|
|
.updateValues = DynamicMeter_updateValues,
|
2021-07-07 06:57:03 +00:00
|
|
|
.getCaption = DynamicMeter_getCaption,
|
2021-06-23 07:44:56 +00:00
|
|
|
.getUiName = DynamicMeter_getUiName,
|
|
|
|
.defaultMode = TEXT_METERMODE,
|
|
|
|
.maxItems = 0,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = DynamicMeter_attributes,
|
|
|
|
.name = "Dynamic",
|
|
|
|
.uiName = "Dynamic",
|
|
|
|
.caption = "",
|
|
|
|
};
|