From eaf11cc12a1aa4b050a8a1e7ea3770d3d9c81e95 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:00:29 +0200 Subject: Imported Upstream version 1.0 --- Meter.c | 86 +++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 31 deletions(-) (limited to 'Meter.c') diff --git a/Meter.c b/Meter.c index 341be72..97b1dc4 100644 --- a/Meter.c +++ b/Meter.c @@ -1,11 +1,10 @@ /* htop - Meter.c -(C) 2004-2010 Hisham H. Muhammad +(C) 2004-2011 Hisham H. Muhammad Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#define _GNU_SOURCE #include "RichString.h" #include "Meter.h" #include "Object.h" @@ -23,6 +22,7 @@ in the source distribution for its full text. #include #ifndef USE_FUNKY_MODES +#include #define USE_FUNKY_MODES 1 #endif @@ -69,13 +69,20 @@ struct Meter_ { int mode; int param; Meter_Draw draw; - void* drawBuffer; + void* drawData; int h; ProcessList* pl; double* values; double total; }; +#ifdef USE_FUNKY_MODES +typedef struct GraphData_ { + time_t time; + double values[METER_BUFFER_LEN]; +} GraphData; +#endif + typedef enum { CUSTOM_METERMODE = 0, BAR_METERMODE, @@ -123,8 +130,13 @@ MeterType* Meter_types[] = { &TasksMeter, &UptimeMeter, &BatteryMeter, - &AllCPUsMeter, &HostnameMeter, + &AllCPUsMeter, + &AllCPUs2Meter, + &LeftCPUsMeter, + &RightCPUsMeter, + &LeftCPUs2Meter, + &RightCPUs2Meter, NULL }; @@ -140,20 +152,21 @@ Meter* Meter_new(ProcessList* pl, int param, MeterType* type) { this->values = calloc(sizeof(double), type->items); this->total = type->total; this->caption = strdup(type->caption); - Meter_setMode(this, type->mode); if (this->type->init) this->type->init(this); + Meter_setMode(this, type->mode); return this; } void Meter_delete(Object* cast) { + if (!cast) + return; Meter* this = (Meter*) cast; - assert (this != NULL); if (this->type->done) { this->type->done(this); } - if (this->drawBuffer) - free(this->drawBuffer); + if (this->drawData) + free(this->drawData); free(this->caption); free(this->values); free(this); @@ -186,9 +199,9 @@ void Meter_setMode(Meter* this, int modeIndex) { this->type->setMode(this, modeIndex); } else { assert(modeIndex >= 1); - if (this->drawBuffer) - free(this->drawBuffer); - this->drawBuffer = NULL; + if (this->drawData) + free(this->drawData); + this->drawData = NULL; MeterMode* mode = Meter_modes[modeIndex]; this->draw = mode->draw; @@ -255,16 +268,21 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { w--; x++; - char bar[w]; + + if (w < 1) { + attrset(CRT_colors[RESET_COLOR]); + return; + } + char bar[w + 1]; int blockSizes[10]; for (int i = 0; i < w; i++) bar[i] = ' '; - sprintf(bar + (w-strlen(buffer)), "%s", buffer); + const size_t barOffset = w - MIN((int)strlen(buffer), w); + snprintf(bar + barOffset, w - barOffset + 1, "%s", buffer); // First draw in the bar[] buffer... - double total = 0.0; int offset = 0; for (int i = 0; i < type->items; i++) { double value = this->values[i]; @@ -287,7 +305,6 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { } } offset = nextOffset; - total += this->values[i]; } // ...then print the buffer. @@ -328,23 +345,30 @@ static const char* GraphMeterMode_characters = "^`'-.,_~'`-.,_~'`-.,_"; static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { - if (!this->drawBuffer) this->drawBuffer = calloc(sizeof(double), METER_BUFFER_LEN); - double* drawBuffer = (double*) this->drawBuffer; - - for (int i = 0; i < METER_BUFFER_LEN - 1; i++) - drawBuffer[i] = drawBuffer[i+1]; - - MeterType* type = this->type; - char buffer[METER_BUFFER_LEN]; - type->setValues(this, buffer, METER_BUFFER_LEN - 1); + if (!this->drawData) this->drawData = calloc(sizeof(GraphData), 1); + GraphData* data = (GraphData*) this->drawData; + const int nValues = METER_BUFFER_LEN; + + time_t now = time(NULL); + if (now > data->time) { + data->time = now; - double value = 0.0; - for (int i = 0; i < type->items; i++) - value += this->values[i]; - value /= this->total; - drawBuffer[METER_BUFFER_LEN - 1] = value; - for (int i = METER_BUFFER_LEN - w, k = 0; i < METER_BUFFER_LEN; i++, k++) { - value = drawBuffer[i]; + for (int i = 0; i < nValues - 1; i++) + data->values[i] = data->values[i+1]; + + MeterType* type = this->type; + char buffer[nValues]; + type->setValues(this, buffer, nValues - 1); + + double value = 0.0; + for (int i = 0; i < type->items; i++) + value += this->values[i]; + value /= this->total; + data->values[nValues - 1] = value; + } + + for (int i = nValues - w, k = 0; i < nValues; i++, k++) { + double value = data->values[i]; DrawDot( CRT_colors[DEFAULT_COLOR], y, ' ' ); DrawDot( CRT_colors[DEFAULT_COLOR], y+1, ' ' ); DrawDot( CRT_colors[DEFAULT_COLOR], y+2, ' ' ); -- cgit v1.2.3