2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - Meter.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.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "Meter.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Macros.h"
|
|
|
|
#include "Object.h"
|
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "RichString.h"
|
2020-12-06 14:22:41 +00:00
|
|
|
#include "Settings.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2009-06-02 04:51:23 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2016-01-21 02:11:54 +00:00
|
|
|
#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterClass Meter_class = {
|
2012-12-05 15:12:20 +00:00
|
|
|
.super = {
|
|
|
|
.extends = Class(Object)
|
|
|
|
}
|
|
|
|
};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-10-21 19:25:50 +00:00
|
|
|
Meter* Meter_new(const struct ProcessList_* pl, int param, const MeterClass* type) {
|
2016-02-02 14:53:02 +00:00
|
|
|
Meter* this = xCalloc(1, sizeof(Meter));
|
2012-12-05 15:12:20 +00:00
|
|
|
Object_setClass(this, type);
|
2006-04-10 20:40:38 +00:00
|
|
|
this->h = 1;
|
|
|
|
this->param = param;
|
|
|
|
this->pl = pl;
|
2020-10-04 15:55:08 +00:00
|
|
|
this->curItems = type->maxItems;
|
2020-12-23 19:56:19 +00:00
|
|
|
this->curAttributes = NULL;
|
2020-10-05 10:49:01 +00:00
|
|
|
this->values = type->maxItems ? xCalloc(type->maxItems, sizeof(double)) : NULL;
|
2006-04-10 20:40:38 +00:00
|
|
|
this->total = type->total;
|
2016-02-02 14:53:02 +00:00
|
|
|
this->caption = xStrdup(type->caption);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Meter_initFn(this)) {
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter_init(this);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter_setMode(this, type->defaultMode);
|
2006-04-10 20:40:38 +00:00
|
|
|
return this;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 17:31:03 +00:00
|
|
|
int Meter_humanUnit(char* buffer, unsigned long int value, size_t size) {
|
2020-10-31 22:28:02 +00:00
|
|
|
const char* prefix = "KMGTPEZY";
|
2015-08-28 00:37:06 +00:00
|
|
|
unsigned long int powi = 1;
|
2020-11-24 16:49:41 +00:00
|
|
|
unsigned int powj = 1, precision = 2;
|
2015-06-12 07:50:55 +00:00
|
|
|
|
2020-10-31 19:55:36 +00:00
|
|
|
for (;;) {
|
2015-08-28 00:37:06 +00:00
|
|
|
if (value / 1024 < powi)
|
|
|
|
break;
|
2015-06-12 07:50:55 +00:00
|
|
|
|
2017-11-24 02:31:39 +00:00
|
|
|
if (prefix[1] == '\0')
|
2015-08-28 00:37:06 +00:00
|
|
|
break;
|
2015-06-12 07:50:55 +00:00
|
|
|
|
2015-08-28 00:37:06 +00:00
|
|
|
powi *= 1024;
|
|
|
|
++prefix;
|
|
|
|
}
|
2015-06-12 07:50:55 +00:00
|
|
|
|
2015-08-28 08:40:33 +00:00
|
|
|
if (*prefix == 'K')
|
|
|
|
precision = 0;
|
|
|
|
|
2015-08-28 00:37:06 +00:00
|
|
|
for (; precision > 0; precision--) {
|
|
|
|
powj *= 10;
|
|
|
|
if (value / powi < powj)
|
|
|
|
break;
|
|
|
|
}
|
2015-06-12 07:50:55 +00:00
|
|
|
|
2020-11-24 16:49:41 +00:00
|
|
|
return snprintf(buffer, size, "%.*f%c", precision, (double) value / powi, *prefix);
|
2015-06-12 07:50:55 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
void Meter_delete(Object* cast) {
|
2011-03-31 20:24:59 +00:00
|
|
|
if (!cast)
|
|
|
|
return;
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
Meter* this = (Meter*) cast;
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Meter_doneFn(this)) {
|
|
|
|
Meter_done(this);
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
2016-05-27 09:11:54 +00:00
|
|
|
free(this->drawData);
|
2006-04-10 20:40:38 +00:00
|
|
|
free(this->caption);
|
|
|
|
free(this->values);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void Meter_setCaption(Meter* this, const char* caption) {
|
2006-04-10 20:40:38 +00:00
|
|
|
free(this->caption);
|
2016-02-02 14:53:02 +00:00
|
|
|
this->caption = xStrdup(caption);
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 10:28:11 +00:00
|
|
|
static inline void Meter_displayBuffer(const Meter* this, const char* buffer, RichString* out) {
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Object_displayFn(this)) {
|
|
|
|
Object_display(this, out);
|
2006-04-10 20:40:38 +00:00
|
|
|
} else {
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_writeWide(out, CRT_colors[Meter_attributes(this)[0]], buffer);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Meter_setMode(Meter* this, int modeIndex) {
|
2020-11-01 00:09:51 +00:00
|
|
|
if (modeIndex > 0 && modeIndex == this->mode) {
|
2006-04-10 20:40:38 +00:00
|
|
|
return;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!modeIndex) {
|
2006-04-10 20:40:38 +00:00
|
|
|
modeIndex = 1;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
assert(modeIndex < LAST_METERMODE);
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Meter_defaultMode(this) == CUSTOM_METERMODE) {
|
|
|
|
this->draw = Meter_drawFn(this);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Meter_updateModeFn(this)) {
|
2012-12-05 15:12:20 +00:00
|
|
|
Meter_updateMode(this, modeIndex);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
} else {
|
2006-05-09 18:18:08 +00:00
|
|
|
assert(modeIndex >= 1);
|
2016-05-27 09:11:54 +00:00
|
|
|
free(this->drawData);
|
2011-03-22 20:37:08 +00:00
|
|
|
this->drawData = NULL;
|
2006-05-09 18:18:08 +00:00
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterMode* mode = Meter_modes[modeIndex];
|
2006-05-09 18:18:08 +00:00
|
|
|
this->draw = mode->draw;
|
|
|
|
this->h = mode->h;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
this->mode = modeIndex;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 02:01:21 +00:00
|
|
|
ListItem* Meter_toListItem(Meter* this, bool moving) {
|
2020-11-28 18:33:07 +00:00
|
|
|
char mode[20];
|
2020-11-01 00:09:51 +00:00
|
|
|
if (this->mode) {
|
2020-11-28 18:33:07 +00:00
|
|
|
xSnprintf(mode, sizeof(mode), " [%s]", Meter_modes[this->mode]->uiName);
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2006-04-10 20:40:38 +00:00
|
|
|
mode[0] = '\0';
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-11-28 18:33:07 +00:00
|
|
|
char number[10];
|
2020-11-01 00:09:51 +00:00
|
|
|
if (this->param > 0) {
|
2020-11-28 18:33:07 +00:00
|
|
|
xSnprintf(number, sizeof(number), " %d", this->param);
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2006-04-10 20:40:38 +00:00
|
|
|
number[0] = '\0';
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-11-28 18:33:07 +00:00
|
|
|
char buffer[50];
|
|
|
|
xSnprintf(buffer, sizeof(buffer), "%s%s%s", Meter_uiName(this), number, mode);
|
2015-03-17 02:01:21 +00:00
|
|
|
ListItem* li = ListItem_new(buffer, 0);
|
|
|
|
li->moving = moving;
|
|
|
|
return li;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
/* ---------- TextMeterMode ---------- */
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
|
2006-04-10 20:40:38 +00:00
|
|
|
char buffer[METER_BUFFER_LEN];
|
2020-11-24 18:34:27 +00:00
|
|
|
Meter_updateValues(this, buffer, sizeof(buffer));
|
2006-04-10 20:40:38 +00:00
|
|
|
|
|
|
|
attrset(CRT_colors[METER_TEXT]);
|
2020-12-15 16:25:25 +00:00
|
|
|
mvaddnstr(y, x, this->caption, w - 1);
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
int captionLen = strlen(this->caption);
|
|
|
|
x += captionLen;
|
2020-12-15 16:25:25 +00:00
|
|
|
w -= captionLen;
|
|
|
|
if (w <= 0)
|
|
|
|
return;
|
|
|
|
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_begin(out);
|
|
|
|
Meter_displayBuffer(this, buffer, &out);
|
2020-12-15 16:25:25 +00:00
|
|
|
RichString_printoffnVal(out, y, x, 0, w - 1);
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_end(out);
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------- BarMeterMode ---------- */
|
|
|
|
|
2016-07-22 06:58:29 +00:00
|
|
|
static const char BarMeterMode_characters[] = "|#*@$%&.";
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
|
2006-04-10 20:40:38 +00:00
|
|
|
char buffer[METER_BUFFER_LEN];
|
2020-11-24 18:34:27 +00:00
|
|
|
Meter_updateValues(this, buffer, sizeof(buffer));
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
w -= 2;
|
|
|
|
attrset(CRT_colors[METER_TEXT]);
|
2008-09-23 06:21:28 +00:00
|
|
|
int captionLen = 3;
|
|
|
|
mvaddnstr(y, x, this->caption, captionLen);
|
2006-03-04 18:16:49 +00:00
|
|
|
x += captionLen;
|
|
|
|
w -= captionLen;
|
|
|
|
attrset(CRT_colors[BAR_BORDER]);
|
|
|
|
mvaddch(y, x, '[');
|
2020-12-15 16:25:25 +00:00
|
|
|
mvaddch(y, x + MAXIMUM(w, 0), ']');
|
2020-12-04 15:11:21 +00:00
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
w--;
|
|
|
|
x++;
|
2011-08-26 20:55:09 +00:00
|
|
|
|
2020-12-04 15:11:21 +00:00
|
|
|
if (w < 1)
|
2011-08-26 20:55:09 +00:00
|
|
|
return;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-11-23 19:12:11 +00:00
|
|
|
// The text in the bar is right aligned;
|
2020-12-20 16:15:51 +00:00
|
|
|
// Pad with maximal spaces and then calculate needed starting position offset
|
2020-11-23 19:12:11 +00:00
|
|
|
RichString_begin(bar);
|
2020-12-19 23:28:10 +00:00
|
|
|
RichString_appendChr(&bar, ' ', w);
|
2020-12-04 13:44:57 +00:00
|
|
|
RichString_appendWide(&bar, 0, buffer);
|
2020-12-19 23:28:10 +00:00
|
|
|
int startPos = RichString_sizeVal(bar) - w;
|
|
|
|
if (startPos > w) {
|
|
|
|
// Text is too large for bar
|
2020-12-20 16:15:51 +00:00
|
|
|
// Truncate meter text at a space character
|
2020-12-19 23:28:10 +00:00
|
|
|
for (int pos = 2 * w; pos > w; pos--) {
|
|
|
|
if (RichString_getCharVal(bar, pos) == ' ') {
|
|
|
|
while (pos > w && RichString_getCharVal(bar, pos - 1) == ' ')
|
|
|
|
pos--;
|
|
|
|
startPos = pos - w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If still to large, print the start not the end
|
|
|
|
startPos = MINIMUM(startPos, w);
|
|
|
|
}
|
|
|
|
assert(startPos >= 0);
|
|
|
|
assert(startPos <= w);
|
|
|
|
assert(startPos + w <= RichString_sizeVal(bar));
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-11-23 19:12:11 +00:00
|
|
|
int blockSizes[10];
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
// First draw in the bar[] buffer...
|
|
|
|
int offset = 0;
|
2020-10-29 22:17:12 +00:00
|
|
|
for (uint8_t i = 0; i < this->curItems; i++) {
|
2006-03-04 18:16:49 +00:00
|
|
|
double value = this->values[i];
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
value = CLAMP(value, 0.0, this->total);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (value > 0) {
|
2020-10-31 22:28:02 +00:00
|
|
|
blockSizes[i] = ceil((value / this->total) * w);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
|
|
|
blockSizes[i] = 0;
|
|
|
|
}
|
|
|
|
int nextOffset = offset + blockSizes[i];
|
|
|
|
// (Control against invalid values)
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
nextOffset = CLAMP(nextOffset, 0, w);
|
2006-03-04 18:16:49 +00:00
|
|
|
for (int j = offset; j < nextOffset; j++)
|
2020-12-19 23:28:10 +00:00
|
|
|
if (RichString_getCharVal(bar, startPos + j) == ' ') {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (CRT_colorScheme == COLORSCHEME_MONOCHROME) {
|
2020-12-19 23:28:10 +00:00
|
|
|
RichString_setChar(&bar, startPos + j, BarMeterMode_characters[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
2020-12-19 23:28:10 +00:00
|
|
|
RichString_setChar(&bar, startPos + j, '|');
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
offset = nextOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...then print the buffer.
|
|
|
|
offset = 0;
|
2020-10-29 22:17:12 +00:00
|
|
|
for (uint8_t i = 0; i < this->curItems; i++) {
|
2020-12-23 19:56:19 +00:00
|
|
|
int attr = this->curAttributes ? this->curAttributes[i] : Meter_attributes(this)[i];
|
|
|
|
RichString_setAttrn(&bar, CRT_colors[attr], startPos + offset, startPos + offset + blockSizes[i] - 1);
|
2020-12-19 23:28:10 +00:00
|
|
|
RichString_printoffnVal(bar, y, x + offset, startPos + offset, MINIMUM(blockSizes[i], w - offset));
|
2006-03-04 18:16:49 +00:00
|
|
|
offset += blockSizes[i];
|
Introduce CLAMP macro. Unify all MIN(MAX(a,b),c) uses.
With the CLAMP macro replacing the combination of MIN and MAX, we will
have at least two advantages:
1. It's more obvious semantically.
2. There are no more mixes of confusing uses like MIN(MAX(a,b),c) and
MAX(MIN(a,b),c) and MIN(a,MAX(b,c)) appearing everywhere. We unify
the 'clamping' with a single macro.
Note that the behavior of this CLAMP macro is different from
the combination `MAX(low,MIN(x,high))`.
* This CLAMP macro expands to two comparisons instead of three from
MAX and MIN combination. In theory, this makes the code slightly
smaller, in case that (low) or (high) or both are computed at
runtime, so that compilers cannot optimize them. (The third
comparison will matter if (low)>(high); see below.)
* CLAMP has a side effect, that if (low)>(high) it will produce weird
results. Unlike MIN & MAX which will force either (low) or (high) to
win. No assertion of ((low)<=(high)) is done in this macro, for now.
This CLAMP macro is implemented like described in glib
<http://developer.gnome.org/glib/stable/glib-Standard-Macros.html>
and does not handle weird uses like CLAMP(a++, low++, high--) .
2016-01-15 12:26:01 +00:00
|
|
|
offset = CLAMP(offset, 0, w);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
if (offset < w) {
|
2020-12-19 23:28:10 +00:00
|
|
|
RichString_setAttrn(&bar, CRT_colors[BAR_SHADOW], startPos + offset, startPos + w - 1);
|
|
|
|
RichString_printoffnVal(bar, y, x + offset, startPos + offset, w - offset);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:12:11 +00:00
|
|
|
RichString_end(bar);
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
move(y, x + w + 1);
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
/* ---------- GraphMeterMode ---------- */
|
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
#ifdef HAVE_LIBNCURSESW
|
|
|
|
|
2015-07-16 11:05:48 +00:00
|
|
|
#define PIXPERROW_UTF8 4
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const GraphMeterMode_dotsUtf8[] = {
|
2015-08-25 15:40:36 +00:00
|
|
|
/*00*/" ", /*01*/"⢀", /*02*/"⢠", /*03*/"⢰", /*04*/ "⢸",
|
2015-07-16 11:05:48 +00:00
|
|
|
/*10*/"⡀", /*11*/"⣀", /*12*/"⣠", /*13*/"⣰", /*14*/ "⣸",
|
|
|
|
/*20*/"⡄", /*21*/"⣄", /*22*/"⣤", /*23*/"⣴", /*24*/ "⣼",
|
|
|
|
/*30*/"⡆", /*31*/"⣆", /*32*/"⣦", /*33*/"⣶", /*34*/ "⣾",
|
|
|
|
/*40*/"⡇", /*41*/"⣇", /*42*/"⣧", /*43*/"⣷", /*44*/ "⣿"
|
2015-01-22 01:27:31 +00:00
|
|
|
};
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-16 11:05:48 +00:00
|
|
|
#define PIXPERROW_ASCII 2
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const GraphMeterMode_dotsAscii[] = {
|
2015-07-16 11:05:48 +00:00
|
|
|
/*00*/" ", /*01*/".", /*02*/":",
|
|
|
|
/*10*/".", /*11*/".", /*12*/":",
|
|
|
|
/*20*/":", /*21*/":", /*22*/":"
|
2006-04-10 20:40:38 +00:00
|
|
|
};
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2020-11-01 00:09:51 +00:00
|
|
|
if (!this->drawData) {
|
|
|
|
this->drawData = xCalloc(1, sizeof(GraphData));
|
|
|
|
}
|
2020-10-02 14:27:57 +00:00
|
|
|
GraphData* data = this->drawData;
|
2011-03-22 20:37:08 +00:00
|
|
|
const int nValues = METER_BUFFER_LEN;
|
2015-01-22 01:27:31 +00:00
|
|
|
|
2021-01-09 13:31:07 +00:00
|
|
|
const char* const* GraphMeterMode_dots;
|
|
|
|
int GraphMeterMode_pixPerRow;
|
2015-07-17 12:33:34 +00:00
|
|
|
#ifdef HAVE_LIBNCURSESW
|
2015-01-22 01:27:31 +00:00
|
|
|
if (CRT_utf8) {
|
|
|
|
GraphMeterMode_dots = GraphMeterMode_dotsUtf8;
|
2015-08-20 04:27:07 +00:00
|
|
|
GraphMeterMode_pixPerRow = PIXPERROW_UTF8;
|
2015-07-17 12:33:34 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2015-01-22 01:27:31 +00:00
|
|
|
GraphMeterMode_dots = GraphMeterMode_dotsAscii;
|
2015-08-20 04:27:07 +00:00
|
|
|
GraphMeterMode_pixPerRow = PIXPERROW_ASCII;
|
2015-01-22 01:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attrset(CRT_colors[METER_TEXT]);
|
|
|
|
int captionLen = 3;
|
|
|
|
mvaddnstr(y, x, this->caption, captionLen);
|
|
|
|
x += captionLen;
|
|
|
|
w -= captionLen;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if (!timercmp(&now, &(data->time), <)) {
|
2020-11-21 20:40:08 +00:00
|
|
|
int globalDelay = this->pl->settings->delay;
|
|
|
|
struct timeval delay = { .tv_sec = globalDelay / 10, .tv_usec = (globalDelay - ((globalDelay / 10) * 10)) * 100000 };
|
2012-12-05 15:12:20 +00:00
|
|
|
timeradd(&now, &delay, &(data->time));
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
for (int i = 0; i < nValues - 1; i++)
|
2020-10-31 22:28:02 +00:00
|
|
|
data->values[i] = data->values[i + 1];
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-11-24 18:34:27 +00:00
|
|
|
char buffer[METER_BUFFER_LEN];
|
|
|
|
Meter_updateValues(this, buffer, sizeof(buffer));
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
double value = 0.0;
|
2020-10-29 22:17:12 +00:00
|
|
|
for (uint8_t i = 0; i < this->curItems; i++)
|
2011-03-22 20:37:08 +00:00
|
|
|
value += this->values[i];
|
|
|
|
value /= this->total;
|
|
|
|
data->values[nValues - 1] = value;
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
int i = nValues - (w * 2) + 2, k = 0;
|
2016-02-02 23:20:11 +00:00
|
|
|
if (i < 0) {
|
2020-10-31 22:28:02 +00:00
|
|
|
k = -i / 2;
|
2016-02-02 23:20:11 +00:00
|
|
|
i = 0;
|
|
|
|
}
|
2020-10-31 22:28:02 +00:00
|
|
|
for (; i < nValues - 1; i += 2, k++) {
|
2016-01-21 06:06:11 +00:00
|
|
|
int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT;
|
2016-07-22 04:44:16 +00:00
|
|
|
int v1 = CLAMP((int) lround(data->values[i] * pix), 1, pix);
|
2020-10-31 22:28:02 +00:00
|
|
|
int v2 = CLAMP((int) lround(data->values[i + 1] * pix), 1, pix);
|
2015-08-14 14:59:14 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
int colorIdx = GRAPH_1;
|
2016-01-21 02:11:54 +00:00
|
|
|
for (int line = 0; line < GRAPH_HEIGHT; line++) {
|
|
|
|
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
|
|
|
|
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
|
2015-08-14 14:59:14 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
attrset(CRT_colors[colorIdx]);
|
2020-10-31 22:28:02 +00:00
|
|
|
mvaddstr(y + line, x + k, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);
|
2015-01-22 01:27:31 +00:00
|
|
|
colorIdx = GRAPH_2;
|
|
|
|
}
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
}
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
/* ---------- LEDMeterMode ---------- */
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const LEDMeterMode_digitsAscii[] = {
|
2020-10-31 22:28:02 +00:00
|
|
|
" __ ", " ", " __ ", " __ ", " ", " __ ", " __ ", " __ ", " __ ", " __ ",
|
|
|
|
"| |", " |", " __|", " __|", "|__|", "|__ ", "|__ ", " |", "|__|", "|__|",
|
|
|
|
"|__|", " |", "|__ ", " __|", " |", " __|", "|__|", " |", "|__|", " __|"
|
2006-04-10 20:40:38 +00:00
|
|
|
};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
#ifdef HAVE_LIBNCURSESW
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const LEDMeterMode_digitsUtf8[] = {
|
2020-10-31 22:28:02 +00:00
|
|
|
"┌──┐", " ┐ ", "╶──┐", "╶──┐", "╷ ╷", "┌──╴", "┌──╴", "╶──┐", "┌──┐", "┌──┐",
|
|
|
|
"│ │", " │ ", "┌──┘", " ──┤", "└──┤", "└──┐", "├──┐", " │", "├──┤", "└──┤",
|
|
|
|
"└──┘", " ╵ ", "└──╴", "╶──┘", " ╵", "╶──┘", "└──┘", " ╵", "└──┘", " ──┘"
|
2012-12-05 15:12:20 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
#endif
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const* LEDMeterMode_digits;
|
2015-01-22 01:27:31 +00:00
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
static void LEDMeterMode_drawDigit(int x, int y, int n) {
|
2015-01-22 01:27:31 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2015-07-16 11:05:48 +00:00
|
|
|
mvaddstr(y+i, x, LEDMeterMode_digits[i * 10 + n]);
|
2006-04-10 20:40:38 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
|
2010-02-25 01:43:18 +00:00
|
|
|
(void) w;
|
2015-01-22 01:27:31 +00:00
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
#ifdef HAVE_LIBNCURSESW
|
|
|
|
if (CRT_utf8)
|
2015-01-22 01:27:31 +00:00
|
|
|
LEDMeterMode_digits = LEDMeterMode_digitsUtf8;
|
2015-07-17 12:33:34 +00:00
|
|
|
else
|
|
|
|
#endif
|
2015-01-22 01:27:31 +00:00
|
|
|
LEDMeterMode_digits = LEDMeterMode_digitsAscii;
|
|
|
|
|
2006-04-10 20:40:38 +00:00
|
|
|
char buffer[METER_BUFFER_LEN];
|
2020-11-24 18:34:27 +00:00
|
|
|
Meter_updateValues(this, buffer, sizeof(buffer));
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_begin(out);
|
|
|
|
Meter_displayBuffer(this, buffer, &out);
|
2006-04-10 20:40:38 +00:00
|
|
|
|
2015-07-17 12:33:34 +00:00
|
|
|
int yText =
|
|
|
|
#ifdef HAVE_LIBNCURSESW
|
2020-10-31 22:28:02 +00:00
|
|
|
CRT_utf8 ? y + 1 :
|
2015-07-17 12:33:34 +00:00
|
|
|
#endif
|
2020-10-31 22:28:02 +00:00
|
|
|
y + 2;
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[LED_COLOR]);
|
2012-12-05 15:12:20 +00:00
|
|
|
mvaddstr(yText, x, this->caption);
|
2006-03-04 18:16:49 +00:00
|
|
|
int xx = x + strlen(this->caption);
|
2010-11-22 12:40:20 +00:00
|
|
|
int len = RichString_sizeVal(out);
|
|
|
|
for (int i = 0; i < len; i++) {
|
2020-11-24 16:52:05 +00:00
|
|
|
int c = RichString_getCharVal(out, i);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (c >= '0' && c <= '9') {
|
2020-10-31 22:28:02 +00:00
|
|
|
LEDMeterMode_drawDigit(xx, y, c - 48);
|
2006-04-10 20:40:38 +00:00
|
|
|
xx += 4;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
2012-12-05 15:12:20 +00:00
|
|
|
mvaddch(yText, xx, c);
|
2006-03-04 18:16:49 +00:00
|
|
|
xx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
2010-11-22 12:40:20 +00:00
|
|
|
RichString_end(out);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 08:58:38 +00:00
|
|
|
static MeterMode BarMeterMode = {
|
|
|
|
.uiName = "Bar",
|
|
|
|
.h = 1,
|
|
|
|
.draw = BarMeterMode_draw,
|
|
|
|
};
|
|
|
|
|
|
|
|
static MeterMode TextMeterMode = {
|
|
|
|
.uiName = "Text",
|
|
|
|
.h = 1,
|
|
|
|
.draw = TextMeterMode_draw,
|
|
|
|
};
|
|
|
|
|
|
|
|
static MeterMode GraphMeterMode = {
|
|
|
|
.uiName = "Graph",
|
2016-01-21 02:11:54 +00:00
|
|
|
.h = GRAPH_HEIGHT,
|
2008-03-09 08:58:38 +00:00
|
|
|
.draw = GraphMeterMode_draw,
|
|
|
|
};
|
|
|
|
|
|
|
|
static MeterMode LEDMeterMode = {
|
|
|
|
.uiName = "LED",
|
|
|
|
.h = 3,
|
|
|
|
.draw = LEDMeterMode_draw,
|
|
|
|
};
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterMode* const Meter_modes[] = {
|
2008-03-09 08:58:38 +00:00
|
|
|
NULL,
|
|
|
|
&BarMeterMode,
|
|
|
|
&TextMeterMode,
|
|
|
|
&GraphMeterMode,
|
|
|
|
&LEDMeterMode,
|
|
|
|
NULL
|
|
|
|
};
|
2014-02-27 19:35:22 +00:00
|
|
|
|
|
|
|
/* Blank meter */
|
|
|
|
|
2020-11-24 17:31:03 +00:00
|
|
|
static void BlankMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
|
2020-08-18 09:46:10 +00:00
|
|
|
if (size > 0) {
|
|
|
|
*buffer = 0;
|
|
|
|
}
|
2014-02-27 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 10:28:11 +00:00
|
|
|
static void BlankMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
|
2014-02-27 19:35:22 +00:00
|
|
|
RichString_prune(out);
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:23:07 +00:00
|
|
|
static const int BlankMeter_attributes[] = {
|
2014-02-27 19:35:22 +00:00
|
|
|
DEFAULT_COLOR
|
|
|
|
};
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const MeterClass BlankMeter_class = {
|
2014-02-27 19:35:22 +00:00
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete,
|
|
|
|
.display = BlankMeter_display,
|
|
|
|
},
|
2016-05-04 05:39:26 +00:00
|
|
|
.updateValues = BlankMeter_updateValues,
|
2014-02-27 19:35:22 +00:00
|
|
|
.defaultMode = TEXT_METERMODE,
|
2016-03-11 02:54:34 +00:00
|
|
|
.maxItems = 0,
|
2014-02-27 19:35:22 +00:00
|
|
|
.total = 100.0,
|
|
|
|
.attributes = BlankMeter_attributes,
|
|
|
|
.name = "Blank",
|
|
|
|
.uiName = "Blank",
|
|
|
|
.caption = ""
|
|
|
|
};
|