Commit 3383d8e5 authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Sorry about the mega-patch.

This is a work-in-progress, code is currently broken.
(Some actions, and notably, the header, are missing.)
parent 36b78328
...@@ -9,20 +9,15 @@ Released under the GNU GPL, see the COPYING file ...@@ -9,20 +9,15 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#include "ProcessList.h"
#include "Meter.h" #include "Meter.h"
#include "Vector.h"
typedef enum HeaderSide_ {
LEFT_HEADER,
RIGHT_HEADER
} HeaderSide;
typedef struct Header_ { typedef struct Header_ {
Vector* leftMeters; Vector** columns;
Vector* rightMeters; struct ProcessList_* pl;
ProcessList* pl;
int height; int height;
int pad; int pad;
int nrColumns;
bool margin; bool margin;
} Header; } Header;
...@@ -31,21 +26,25 @@ typedef struct Header_ { ...@@ -31,21 +26,25 @@ typedef struct Header_ {
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
Header* Header_new(ProcessList* pl); #ifndef Header_forEachColumn
#define Header_forEachColumn(this_, i_) for (int i_=0; i_ < this->nrColumns; i_++)
#endif
Header* Header_new(struct ProcessList_* pl, int nrColumns);
void Header_delete(Header* this); void Header_delete(Header* this);
void Header_createMeter(Header* this, char* name, HeaderSide side); MeterModeId Header_addMeterByName(Header* this, char* name, int column);
void Header_setMode(Header* this, int i, MeterModeId mode, HeaderSide side); void Header_setMode(Header* this, int i, MeterModeId mode, int column);
Meter* Header_addMeter(Header* this, MeterClass* type, int param, HeaderSide side); Meter* Header_addMeterByClass(Header* this, MeterClass* type, int param, int column);
int Header_size(Header* this, HeaderSide side); int Header_size(Header* this, int column);
char* Header_readMeterName(Header* this, int i, HeaderSide side); char* Header_readMeterName(Header* this, int i, int column);
MeterModeId Header_readMeterMode(Header* this, int i, HeaderSide side); MeterModeId Header_readMeterMode(Header* this, int i, int column);
void Header_reinit(Header* this); void Header_reinit(Header* this);
......
...@@ -22,6 +22,7 @@ typedef struct ListItem_ { ...@@ -22,6 +22,7 @@ typedef struct ListItem_ {
Object super; Object super;
char* value; char* value;
int key; int key;
bool moving;
} ListItem; } ListItem;
}*/ }*/
...@@ -33,14 +34,19 @@ static void ListItem_delete(Object* cast) { ...@@ -33,14 +34,19 @@ static void ListItem_delete(Object* cast) {
} }
static void ListItem_display(Object* cast, RichString* out) { static void ListItem_display(Object* cast, RichString* out) {
ListItem* this = (ListItem*)cast; ListItem* const this = (ListItem*)cast;
assert (this != NULL); assert (this != NULL);
/* /*
int len = strlen(this->value)+1; int len = strlen(this->value)+1;
char buffer[len+1]; char buffer[len+1];
snprintf(buffer, len, "%s", this->value); snprintf(buffer, len, "%s", this->value);
*/ */
RichString_write(out, CRT_colors[DEFAULT_COLOR], this->value/*buffer*/); if (this->moving) {
RichString_write(out, CRT_colors[DEFAULT_COLOR], "↕ ");
} else {
RichString_prune(out);
}
RichString_append(out, CRT_colors[DEFAULT_COLOR], this->value/*buffer*/);
} }
ObjectClass ListItem_class = { ObjectClass ListItem_class = {
...@@ -53,6 +59,7 @@ ListItem* ListItem_new(const char* value, int key) { ...@@ -53,6 +59,7 @@ ListItem* ListItem_new(const char* value, int key) {
ListItem* this = AllocThis(ListItem); ListItem* this = AllocThis(ListItem);
this->value = strdup(value); this->value = strdup(value);
this->key = key; this->key = key;
this->moving = false;
return this; return this;
} }
......
...@@ -15,6 +15,7 @@ typedef struct ListItem_ { ...@@ -15,6 +15,7 @@ typedef struct ListItem_ {
Object super; Object super;
char* value; char* value;
int key; int key;
bool moving;
} ListItem; } ListItem;
......
...@@ -16,7 +16,7 @@ htop_CFLAGS = -pedantic -Wall -Wextra -std=c99 -rdynamic -D_XOPEN_SOURCE_EXTENDE ...@@ -16,7 +16,7 @@ htop_CFLAGS = -pedantic -Wall -Wextra -std=c99 -rdynamic -D_XOPEN_SOURCE_EXTENDE
AM_CPPFLAGS = -DNDEBUG AM_CPPFLAGS = -DNDEBUG
myhtopsources = AvailableMetersPanel.c CategoriesPanel.c CheckItem.c \ myhtopsources = AvailableMetersPanel.c CategoriesPanel.c CheckItem.c \
ClockMeter.c ColorsPanel.c ColumnsPanel.c CPUMeter.c CRT.c \ ClockMeter.c ColorsPanel.c ColumnsPanel.c CPUMeter.c CRT.c MainPanel.c \
DisplayOptionsPanel.c FunctionBar.c Hashtable.c Header.c htop.c ListItem.c \ DisplayOptionsPanel.c FunctionBar.c Hashtable.c Header.c htop.c ListItem.c \
LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.c \ LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.c \
BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \ BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \
...@@ -26,7 +26,7 @@ HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c ...@@ -26,7 +26,7 @@ HostnameMeter.c OpenFilesScreen.c Affinity.c IncSet.c Action.c
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \ myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \
CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \ CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \
CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \ CPUMeter.h CRT.h MainPanel.h DisplayOptionsPanel.h FunctionBar.h \
Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.h \ Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.h \
BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \
ScreenManager.h Settings.h SignalsPanel.h String.h SwapMeter.h TasksMeter.h \ ScreenManager.h Settings.h SignalsPanel.h String.h SwapMeter.h TasksMeter.h \
......
...@@ -8,7 +8,7 @@ in the source distribution for its full text. ...@@ -8,7 +8,7 @@ in the source distribution for its full text.
#include "MemoryMeter.h" #include "MemoryMeter.h"
#include "CRT.h" #include "CRT.h"
#include "ProcessList.h" #include "Platform.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -25,15 +25,8 @@ int MemoryMeter_attributes[] = { ...@@ -25,15 +25,8 @@ int MemoryMeter_attributes[] = {
}; };
static void MemoryMeter_setValues(Meter* this, char* buffer, int size) { static void MemoryMeter_setValues(Meter* this, char* buffer, int size) {
long int usedMem = this->pl->usedMem; Platform_setMemoryValues(this);
long int buffersMem = this->pl->buffersMem; snprintf(buffer, size, "%ld/%ldMB", (long int) this->values[0] / 1024, (long int) this->total / 1024);
long int cachedMem = this->pl->cachedMem;
usedMem -= buffersMem + cachedMem;
this->total = this->pl->totalMem;
this->values[0] = usedMem;
this->values[1] = buffersMem;
this->values[2] = cachedMem;
snprintf(buffer, size, "%ld/%ldMB", (long int) usedMem / 1024, (long int) this->total / 1024);
} }
static void MemoryMeter_display(Object* cast, RichString* out) { static void MemoryMeter_display(Object* cast, RichString* out) {
......
...@@ -21,11 +21,12 @@ in the source distribution for its full text. ...@@ -21,11 +21,12 @@ in the source distribution for its full text.
#include <assert.h> #include <assert.h>
#include <sys/time.h> #include <sys/time.h>
#define METER_BUFFER_LEN 128 #define METER_BUFFER_LEN 256
/*{ /*{
#include "ListItem.h" #include "ListItem.h"
#include "ProcessList.h"
#include <sys/time.h>
typedef struct Meter_ Meter; typedef struct Meter_ Meter;
...@@ -77,7 +78,7 @@ struct Meter_ { ...@@ -77,7 +78,7 @@ struct Meter_ {
int param; int param;
void* drawData; void* drawData;
int h; int h;
ProcessList* pl; struct ProcessList_* pl;
double* values; double* values;
double total; double total;
}; };
...@@ -117,7 +118,7 @@ MeterClass Meter_class = { ...@@ -117,7 +118,7 @@ MeterClass Meter_class = {
} }
}; };
Meter* Meter_new(ProcessList* pl, int param, MeterClass* type) { Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
Meter* this = calloc(1, sizeof(Meter)); Meter* this = calloc(1, sizeof(Meter));
Object_setClass(this, type); Object_setClass(this, type);
this->h = 1; this->h = 1;
...@@ -302,25 +303,41 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { ...@@ -302,25 +303,41 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
/* ---------- GraphMeterMode ---------- */ /* ---------- GraphMeterMode ---------- */
#define DrawDot(a,y,c) do { attrset(a); mvaddch(y, x+k, c); } while(0) static const char* GraphMeterMode_dotsUtf8[5][5] = {
{ /*00*/"⠀", /*01*/"⢀", /*02*/"⢠", /*03*/"⢰", /*04*/ "⢸" },
{ /*10*/"⡀", /*11*/"⣀", /*12*/"⣠", /*13*/"⣰", /*14*/ "⣸" },
{ /*20*/"⡄", /*21*/"⣄", /*22*/"⣤", /*23*/"⣴", /*24*/ "⣼" },
{ /*30*/"⡆", /*31*/"⣆", /*32*/"⣦", /*33*/"⣶", /*34*/ "⣾" },
{ /*40*/"⡇", /*41*/"⣇", /*42*/"⣧", /*43*/"⣷", /*44*/ "⣿" },
};
static int GraphMeterMode_colors[21] = { static const char* GraphMeterMode_dotsAscii[5][5] = {
GRAPH_1, GRAPH_1, GRAPH_1, { /*00*/" ", /*01*/".", /*02*/".", /*03*/":", /*04*/ ":" },
GRAPH_2, GRAPH_2, GRAPH_2, { /*10*/"⡀", /*11*/".", /*12*/".", /*13*/":", /*14*/ ":" },
GRAPH_3, GRAPH_3, GRAPH_3, { /*20*/".", /*21*/".", /*22*/".", /*23*/":", /*24*/ ":" },
GRAPH_4, GRAPH_4, GRAPH_4, { /*30*/":", /*31*/":", /*32*/":", /*33*/":", /*34*/ ":" },
GRAPH_5, GRAPH_5, GRAPH_6, { /*40*/":", /*41*/":", /*42*/":", /*43*/":", /*44*/ ":" },
GRAPH_7, GRAPH_7, GRAPH_7,
GRAPH_8, GRAPH_8, GRAPH_9
}; };
static const char* GraphMeterMode_characters = "^`'-.,_~'`-.,_~'`-.,_"; static const char* (*GraphMeterMode_dots)[5];
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
if (!this->drawData) this->drawData = calloc(1, sizeof(GraphData)); if (!this->drawData) this->drawData = calloc(1, sizeof(GraphData));
GraphData* data = (GraphData*) this->drawData; GraphData* data = (GraphData*) this->drawData;
const int nValues = METER_BUFFER_LEN; const int nValues = METER_BUFFER_LEN;
if (CRT_utf8) {
GraphMeterMode_dots = GraphMeterMode_dotsUtf8;
} else {
GraphMeterMode_dots = GraphMeterMode_dotsAscii;
}
attrset(CRT_colors[METER_TEXT]);
int captionLen = 3;
mvaddnstr(y, x, this->caption, captionLen);
x += captionLen;
w -= captionLen;
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
...@@ -342,18 +359,26 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { ...@@ -342,18 +359,26 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
data->values[nValues - 1] = value; data->values[nValues - 1] = value;
} }
for (int i = nValues - w, k = 0; i < nValues; i++, k++) { for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) {
double value = data->values[i]; const double dot = (1.0 / 16);
DrawDot( CRT_colors[DEFAULT_COLOR], y, ' ' ); int v1 = data->values[i] / dot;
DrawDot( CRT_colors[DEFAULT_COLOR], y+1, ' ' ); int v2 = data->values[i+1] / dot;
DrawDot( CRT_colors[DEFAULT_COLOR], y+2, ' ' );
double threshold = 1.00; if (v1 == 0) v1 = 1;
for (int j = 0; j < 21; j++, threshold -= 0.05) if (v2 == 0) v2 = 1;
if (value >= threshold) {
DrawDot(CRT_colors[GraphMeterMode_colors[j]], y+(j/7.0), GraphMeterMode_characters[j]); int level = 12;
break; int colorIdx = GRAPH_1;
} for (int line = 0; line < 4; line++) {
int line1 = MIN(4, MAX(0, v1 - level));
int line2 = MIN(4, MAX(0, v2 - level));
attrset(CRT_colors[colorIdx]);
mvaddstr(y+line, x+k, GraphMeterMode_dots[line1][line2]);
colorIdx = GRAPH_2;
level -= 4;
}
} }
attrset(CRT_colors[RESET_COLOR]); attrset(CRT_colors[RESET_COLOR]);
} }
...@@ -372,18 +397,22 @@ static const char* LEDMeterMode_digitsUtf8[3][10] = { ...@@ -372,18 +397,22 @@ static const char* LEDMeterMode_digitsUtf8[3][10] = {
{ "└──┘"," ╵ ","└──╴","╶──┘"," ╵","╶──┘","└──┘"," ╵","└──┘"," ──┘"}, { "└──┘"," ╵ ","└──╴","╶──┘"," ╵","╶──┘","└──┘"," ╵","└──┘"," ──┘"},
}; };
static const char* (*LEDMeterMode_digits)[10];
static void LEDMeterMode_drawDigit(int x, int y, int n) { static void LEDMeterMode_drawDigit(int x, int y, int n) {
if (CRT_utf8) { for (int i = 0; i < 3; i++)
for (int i = 0; i < 3; i++) mvaddstr(y+i, x, LEDMeterMode_digits[i][n]);
mvaddstr(y+i, x, LEDMeterMode_digitsUtf8[i][n]);
} else {
for (int i = 0; i < 3; i++)
mvaddstr(y+i, x, LEDMeterMode_digitsAscii[i][n]);
}
} }
static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
(void) w; (void) w;
if (CRT_utf8) {
LEDMeterMode_digits = LEDMeterMode_digitsUtf8;
} else {
LEDMeterMode_digits = LEDMeterMode_digitsAscii;
}
char buffer[METER_BUFFER_LEN]; char buffer[METER_BUFFER_LEN];
Meter_setValues(this, buffer, METER_BUFFER_LEN - 1); Meter_setValues(this, buffer, METER_BUFFER_LEN - 1);
...@@ -423,7 +452,7 @@ static MeterMode TextMeterMode = { ...@@ -423,7 +452,7 @@ static MeterMode TextMeterMode = {
static MeterMode GraphMeterMode = { static MeterMode GraphMeterMode = {
.uiName = "Graph", .uiName = "Graph",
.h = 3, .h = 4,
.draw = GraphMeterMode_draw, .draw = GraphMeterMode_draw,
}; };
......
...@@ -9,10 +9,11 @@ Released under the GNU GPL, see the COPYING file ...@@ -9,10 +9,11 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#define METER_BUFFER_LEN 128 #define METER_BUFFER_LEN 256
#include "ListItem.h" #include "ListItem.h"
#include "ProcessList.h"
#include <sys/time.h>
typedef struct Meter_ Meter; typedef struct Meter_ Meter;
...@@ -64,7 +65,7 @@ struct Meter_ { ...@@ -64,7 +65,7 @@ struct Meter_ {
int param; int param;
void* drawData; void* drawData;
int h; int h;
ProcessList* pl; struct ProcessList_* pl;
double* values; double* values;
double total; double total;
}; };
...@@ -99,7 +100,7 @@ typedef struct GraphData_ { ...@@ -99,7 +100,7 @@ typedef struct GraphData_ {
extern MeterClass Meter_class; extern MeterClass Meter_class;
Meter* Meter_new(ProcessList* pl, int param, MeterClass* type); Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type);
void Meter_delete(Object* cast); void Meter_delete(Object* cast);
...@@ -115,8 +116,6 @@ ListItem* Meter_toListItem(Meter* this); ...@@ -115,8 +116,6 @@ ListItem* Meter_toListItem(Meter* this);
/* ---------- GraphMeterMode ---------- */ /* ---------- GraphMeterMode ---------- */
#define DrawDot(a,y,c) do { attrset(a); mvaddch(y, x+k, c); } while(0)
/* ---------- LEDMeterMode ---------- */ /* ---------- LEDMeterMode ---------- */
extern MeterMode* Meter_modes[]; extern MeterMode* Meter_modes[];
......
...@@ -21,6 +21,7 @@ typedef struct MetersPanel_ { ...@@ -21,6 +21,7 @@ typedef struct MetersPanel_ {
Settings* settings; Settings* settings;
Vector* meters; Vector* meters;
ScreenManager* scr; ScreenManager* scr;
bool moving;
} MetersPanel; } MetersPanel;
}*/ }*/
...@@ -42,6 +43,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) { ...@@ -42,6 +43,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
case 0x0a: case 0x0a:
case 0x0d: case 0x0d:
case KEY_ENTER: case KEY_ENTER:
{
this->moving = !(this->moving);
((ListItem*)Panel_getSelected(super))->moving = this->moving;
result = HANDLED;
break;
}
case ' ':
case KEY_F(4): case KEY_F(4):
case 't': case 't':
{ {
...@@ -53,6 +61,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) { ...@@ -53,6 +61,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
result = HANDLED; result = HANDLED;
break; break;
} }
case KEY_UP:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
case KEY_F(7): case KEY_F(7):
case '[': case '[':
case '-': case '-':
...@@ -62,6 +77,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) { ...@@ -62,6 +77,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
result = HANDLED; result = HANDLED;
break; break;
} }
case KEY_DOWN:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
case KEY_F(8): case KEY_F(8):
case ']': case ']':
case '+': case '+':
...@@ -83,7 +105,7 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) { ...@@ -83,7 +105,7 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
} }
} }
if (result == HANDLED) { if (result == HANDLED) {
Header* header = this->settings->header; Header* header = (Header*) this->scr->header;
this->settings->changed = true; this->settings->changed = true;
Header_calculateHeight(header); Header_calculateHeight(header);
Header_draw(header); Header_draw(header);
...@@ -108,6 +130,7 @@ MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* met ...@@ -108,6 +130,7 @@ MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* met
this->settings = settings; this->settings = settings;
this->meters = meters; this->meters = meters;
this->scr = scr; this->scr = scr;
this->moving = false;
Panel_setHeader(super, header); Panel_setHeader(super, header);
for (int i = 0; i < Vector_size(meters); i++) { for (int i = 0; i < Vector_size(meters); i++) {
Meter* meter = (Meter*) Vector_get(meters, i); Meter* meter = (Meter*) Vector_get(meters, i);
......
...@@ -19,6 +19,7 @@ typedef struct MetersPanel_ { ...@@ -19,6 +19,7 @@ typedef struct MetersPanel_ {
Settings* settings; Settings* settings;
Vector* meters; Vector* meters;
ScreenManager* scr; ScreenManager* scr;
bool moving;
} MetersPanel; } MetersPanel;
......
...@@ -28,9 +28,11 @@ in the source distribution for its full text. ...@@ -28,9 +28,11 @@ in the source distribution for its full text.
typedef struct Panel_ Panel; typedef struct Panel_ Panel;
typedef enum HandlerResult_ { typedef enum HandlerResult_ {
HANDLED, HANDLED = 0x00,
IGNORED, IGNORED = 0x01,
BREAK_LOOP BREAK_LOOP = 0x02,
REFRESH = 0x04,
RECALCULATE = 0x08,
} HandlerResult; } HandlerResult;
#define EVENT_SETSELECTED -1 #define EVENT_SETSELECTED -1
...@@ -54,7 +56,7 @@ struct Panel_ { ...@@ -54,7 +56,7 @@ struct Panel_ {
Vector* items; Vector* items;
int selected; int selected;
int oldSelected; int oldSelected;
char* eventHandlerBuffer; void* eventHandlerState;
int scrollV; int scrollV;
short scrollH; short scrollH;
bool needsRedraw; bool needsRedraw;
...@@ -102,7 +104,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool ...@@ -102,7 +104,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool
this->y = y; this->y = y;
this->w = w; this->w = w;
this->h = h; this->h = h;
this->eventHandlerBuffer = NULL; this->eventHandlerState = NULL;
this->items = Vector_new(type, owner, DEFAULT_SIZE); this->items = Vector_new(type, owner, DEFAULT_SIZE);
this->scrollV = 0; this->scrollV = 0;
this->scrollH = 0; this->scrollH = 0;
...@@ -114,7 +116,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool ...@@ -114,7 +116,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool
void Panel_done(Panel* this) { void Panel_done(Panel* this) {
assert (this != NULL); assert (this != NULL);
free(this->eventHandlerBuffer); free(this->eventHandlerState);
Vector_delete(this->items); Vector_delete(this->items);
RichString_end(this->header); RichString_end(this->header);
} }
...@@ -246,30 +248,11 @@ void Panel_setSelected(Panel* this, int selected) { ...@@ -246,30 +248,11 @@ void Panel_setSelected(Panel* this, int selected) {
void Panel_draw(Panel* this, bool focus) { void Panel_draw(Panel* this, bool focus) {
assert (this != NULL); assert (this != NULL);
int itemCount = Vector_size(this->items); int size = Vector_size(this->items);
int scrollH = this->scrollH; int scrollH = this->scrollH;
int y = this->y; int x = this->x; int y = this->y;
int first = this->scrollV; int x = this->x;
if (itemCount > this->h && first > itemCount - this->h) { int h = this->h;
first = itemCount - this->h;
this->scrollV = first;
}
int last = MIN(itemCount, first + MIN(itemCount, this->h));
if (this->selected < first) {
first = this->selected;
this->scrollV = first;
this->needsRedraw = true;
}
if (this->selected >= last) {
last = MIN(itemCount, this->selected + 1);
first = last - this->h;
this->scrollV = first;
this->needsRedraw = true;
}
if (first < 0)
first = 0;
if (last > itemCount)
last = itemCount;
int headerLen = RichString_sizeVal(this->header); int headerLen = RichString_sizeVal(this->header);
if (headerLen > 0) { if (headerLen > 0) {
...@@ -285,14 +268,34 @@ void Panel_draw(Panel* this, bool focus) { ...@@ -285,14 +268,34 @@ void Panel_draw(Panel* this, bool focus) {
attrset(CRT_colors[RESET_COLOR]); attrset(CRT_colors[RESET_COLOR]);
y++; y++;
} }
// ensure scroll area is on screen
if (this->scrollV < 0) {
this->scrollV = 0;
this->needsRedraw = true;
} else if (this->scrollV >= size) {
this->scrollV = MAX(size - 1, 0);
this->needsRedraw = true;
}
// ensure selection is on screen
if (this->selected < this->scrollV) {
this->scrollV = this->selected;
this->needsRedraw = true;
} else if (this->selected >= this->scrollV + h) {
this->scrollV = this->selected - h + 1;
this->needsRedraw = true;
}
int first = this->scrollV;
int upTo = MIN(first + h, size);
int highlight = focus int highlight = focus
? CRT_colors[PANEL_HIGHLIGHT_FOCUS] ? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
: CRT_colors[PANEL_HIGHLIGHT_UNFOCUS]; : CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];
if (this->needsRedraw) { if (this->needsRedraw) {
int line = 0;
for(int i = first, j = 0; j < this->h && i < last; i++, j++) { for(int i = first; line < h && i < upTo; i++) {
Object* itemObj = Vector_get(this->items, i); Object* itemObj = Vector_get(this->items, i);
assert(itemObj); if(!itemObj) continue; assert(itemObj); if(!itemObj) continue;
RichString_begin(item); RichString_begin(item);
...@@ -304,15 +307,18 @@ void Panel_draw(Panel* this, bool focus) { ...@@ -304,15 +307,18 @@ void Panel_draw(Panel* this, bool focus) {
attrset(highlight); attrset(highlight);
RichString_setAttr(&item, highlight); RichString_setAttr(&item, highlight);
} }
mvhline(y + j, x, ' ', this->w); mvhline(y + line, x, ' ', this->w);
if (amt > 0) if (amt > 0)
RichString_printoffnVal(item, y+j, x, scrollH, amt); RichString_printoffnVal(item, y + line, x, scrollH, amt);
if (selected) if (selected)
attrset(CRT_colors[RESET_COLOR]); attrset(CRT_colors[RESET_COLOR]);
RichString_end(item); RichString_end(item);
line++;
}
while (line < h) {
mvhline(y + line, x, ' ', this->w);
line++;
} }
for (int i = y + (last - first); i < y + this->h; i++)
mvhline(i, x+0, ' ', this->w);
this->needsRedraw = false; this->needsRedraw = false;
} else { } else {
...@@ -325,15 +331,15 @@ void Panel_draw(Panel* this, bool focus) { ...@@ -325,15 +331,15 @@ void Panel_draw(Panel* this, bool focus) {
RichString_begin(new); RichString_begin(new);
Object_display(newObj, &new); Object_display(newObj, &new);
int newLen = RichString_sizeVal(new); int newLen = RichString_sizeVal(new);
mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', this->w); mvhline(y+ this->oldSelected - first, x+0, ' ', this->w);
if (scrollH < oldLen) if (scrollH < oldLen)
RichString_printoffnVal(old, y+this->oldSelected - this->scrollV, x, RichString_printoffnVal(old, y+this->oldSelected - first, x,
scrollH, MIN(oldLen - scrollH, this->w)); scrollH, MIN(oldLen - scrollH, this->w));
attrset(highlight); attrset(highlight);
mvhline(y+this->selected - this->scrollV, x+0, ' ', this->w); mvhline(y+this->selected - first, x+0, ' ', this->w);
RichString_setAttr(&new, highlight); RichString_setAttr(&new, highlight);
if (scrollH < newLen) if (scrollH < newLen)
RichString_printoffnVal(new, y+this->selected - this->scrollV, x, RichString_printoffnVal(new, y+this->selected - first, x,
scrollH, MIN(newLen - scrollH, this->w)); scrollH, MIN(newLen - scrollH, this->w));
attrset(CRT_colors[RESET_COLOR]); attrset(CRT_colors[RESET_COLOR]);
RichString_end(new); RichString_end(new);
...@@ -345,38 +351,26 @@ void Panel_draw(Panel* this, bool focus) { ...@@ -345,38 +351,26 @@ void Panel_draw(Panel* this, bool focus) {
bool Panel_onKey(Panel* this, int key) { bool Panel_onKey(Panel* this, int key) {
assert (this != NULL); assert (this != NULL);
int size = Vector_size(this->items);
switch (key) { switch (key) {
case KEY_DOWN: case KEY_DOWN:
case KEY_CTRLN: case KEY_CTRLN:
if (this->selected + 1 < Vector_size(this->items)) this->selected++;
this->selected++; break;
return true;
case KEY_UP: case KEY_UP:
case KEY_CTRLP: case KEY_CTRLP:
if (this->selected > 0) this->selected--;
this->selected--; break;
return true;
#ifdef KEY_C_DOWN #ifdef KEY_C_DOWN
case KEY_C_DOWN: case KEY_C_DOWN:
if (this->selected + 1 < Vector_size(this->items)) { this->selected++;
this->selected++; break;
if (this->scrollV < Vector_size(this->items) - this->h) {
this->scrollV++;
this->needsRedraw = true;
}
}
return true;
#endif #endif
#ifdef KEY_C_UP #ifdef KEY_C_UP
case KEY_C_UP: case KEY_C_UP:
if (this->selected > 0) { this->selected--;
this->selected--; break;
if (this->scrollV > 0) {
this->scrollV--;
this->needsRedraw = true;
}
}
return true;
#endif #endif
case KEY_LEFT: case KEY_LEFT:
case KEY_CTRLB: case KEY_CTRLB:
...@@ -384,71 +378,74 @@ bool Panel_onKey(Panel* this, int key) { ...@@ -384,71 +378,74 @@ bool Panel_onKey(Panel* this, int key) {
this->scrollH -= CRT_scrollHAmount; this->scrollH -= CRT_scrollHAmount;
this->needsRedraw = true; this->needsRedraw = true;
} }
return true; break;
case KEY_RIGHT: case KEY_RIGHT:
case KEY_CTRLF: case KEY_CTRLF:
this->scrollH += CRT_scrollHAmount; this->scrollH += CRT_scrollHAmount;
this->needsRedraw = true; this->needsRedraw = true;
return true; break;
case KEY_PPAGE: case KEY_PPAGE:
this->selected -= (this->h - 1); this->selected -= (this->h - 1);
this->scrollV -= (this->h - 1); this->scrollV -= (this->h - 1);
if (this->selected < 0)
this->selected = 0;
if (this->scrollV < 0)
this->scrollV = 0;
this->needsRedraw = true; this->needsRedraw = true;
return true; break;
case KEY_NPAGE: case KEY_NPAGE:
this->selected += (this->h - 1); this->selected += (this->h - 1);
int size = Vector_size(this->items);
if (this->selected < 0)
this->selected = 0;
if (this->selected >= size)
this->selected = size - 1;
this->scrollV += (this->h - 1); this->scrollV += (this->h - 1);
if (this->scrollV >= MAX(0, size - this->h))
this->scrollV = MAX(0, size - this->h - 1);
this->needsRedraw = true; this->needsRedraw = true;
return true; break;
case KEY_HOME: case KEY_HOME:
this->selected = 0; this->selected = 0;
return true; break;
case KEY_END: case KEY_END:
this->selected = Vector_size(this->items) - 1; this->selected = size - 1;
return true; break;
default:
return false;
}
// ensure selection within bounds
if (this->selected < 0) {
this->selected = 0;
this->needsRedraw = true;
} else if (this->selected >= size) {
this->selected = size - 1;
this->needsRedraw = true;
} }
return false; return true;
} }
HandlerResult Panel_selectByTyping(Panel* this, int ch) { HandlerResult Panel_selectByTyping(Panel* this, int ch) {
int size = Panel_size(this); int size = Panel_size(this);
if (!this->eventHandlerBuffer) if (!this->eventHandlerState)
this->eventHandlerBuffer = calloc(100, 1); this->eventHandlerState = calloc(100, 1);
char* buffer = this->eventHandlerState;
if (isalnum(ch)) { if (isalnum(ch)) {
int len = strlen(this->eventHandlerBuffer); int len = strlen(buffer);
if (len < 99) { if (len < 99) {
this->eventHandlerBuffer[len] = ch; buffer[len] = ch;
this->eventHandlerBuffer[len+1] = '\0'; buffer[len+1] = '\0';
} }
for (int try = 0; try < 2; try++) { for (int try = 0; try < 2; try++) {
len = strlen(this->eventHandlerBuffer); len = strlen(buffer);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
char* cur = ((ListItem*) Panel_get(this, i))->value; char* cur = ((ListItem*) Panel_get(this, i))->value;
while (*cur == ' ') cur++; while (*cur == ' ') cur++;
if (strncasecmp(cur, this->eventHandlerBuffer, len) == 0) { if (strncasecmp(cur, buffer, len) == 0) {
Panel_setSelected(this, i); Panel_setSelected(this, i);
return HANDLED; return HANDLED;
} }
} }
this->eventHandlerBuffer[0] = ch; // if current word did not match,
this->eventHandlerBuffer[1] = '\0'; // retry considering the character the start of a new word.
buffer[0] = ch;
buffer[1] = '\0';
} }
return HANDLED; return HANDLED;
} else if (ch != ERR) { } else if (ch != ERR) {
this->eventHandlerBuffer[0] = '\0'; buffer[0] = '\0';
} }
if (ch == 13) { if (ch == 13) {
return BREAK_LOOP; return BREAK_LOOP;
......
...@@ -17,9 +17,11 @@ in the source distribution for its full text. ...@@ -17,9 +17,11 @@ in the source distribution for its full text.
typedef struct Panel_ Panel; typedef struct Panel_ Panel;
typedef enum HandlerResult_ { typedef enum HandlerResult_ {
HANDLED, HANDLED = 0x00,
IGNORED, IGNORED = 0x01,
BREAK_LOOP BREAK_LOOP = 0x02,
REFRESH = 0x04,
RECALCULATE = 0x08,
} HandlerResult; } HandlerResult;
#define EVENT_SETSELECTED -1 #define EVENT_SETSELECTED -1
...@@ -43,7 +45,7 @@ struct Panel_ { ...@@ -43,7 +45,7 @@ struct Panel_ {
Vector* items; Vector* items;
int selected; int selected;
int oldSelected; int oldSelected;
char* eventHandlerBuffer; void* eventHandlerState;
int scrollV; int scrollV;
short scrollH; short scrollH;
bool needsRedraw; bool needsRedraw;
......
This diff is collapsed.
...@@ -9,9 +9,6 @@ Released under the GNU GPL, see the COPYING file ...@@ -9,9 +9,6 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#ifdef HAVE_LIBHWLOC
#endif
// On Linux, this works only with glibc 2.1+. On earlier versions // On Linux, this works only with glibc 2.1+. On earlier versions
// the behavior is similar to have a hardcoded page size. // the behavior is similar to have a hardcoded page size.
#ifndef PAGE_SIZE #ifndef PAGE_SIZE
...@@ -20,7 +17,6 @@ in the source distribution for its full text. ...@@ -20,7 +17,6 @@ in the source distribution for its full text.
#define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K ) #define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K )
#include "Object.h" #include "Object.h"
#include "Affinity.h"
#include <sys/types.h> #include <sys/types.h>
...@@ -67,12 +63,10 @@ typedef enum ProcessField_ { ...@@ -67,12 +63,10 @@ typedef enum ProcessField_ {
LAST_PROCESSFIELD LAST_PROCESSFIELD
} ProcessField; } ProcessField;
struct ProcessList_;
typedef struct Process_ { typedef struct Process_ {
Object super; Object super;
struct ProcessList_ *pl; struct Settings_* settings;
pid_t pid; pid_t pid;
char* comm; char* comm;
...@@ -112,10 +106,10 @@ typedef struct Process_ { ...@@ -112,10 +106,10 @@ typedef struct Process_ {
unsigned long long io_read_bytes; unsigned long long io_read_bytes;
unsigned long long io_write_bytes; unsigned long long io_write_bytes;
unsigned long long io_cancelled_write_bytes; unsigned long long io_cancelled_write_bytes;
double io_rate_read_bps;
unsigned long long io_rate_read_time; unsigned long long io_rate_read_time;
double io_rate_write_bps;
unsigned long long io_rate_write_time; unsigned long long io_rate_write_time;
double io_rate_read_bps;
double io_rate_write_bps;
#endif #endif
int processor; int processor;
...@@ -171,12 +165,18 @@ typedef struct Process_ { ...@@ -171,12 +165,18 @@ typedef struct Process_ {
} Process; } Process;
typedef struct ProcessFieldData_ {
const char* name;
const char* title;
const char* description;
int flags;
} ProcessFieldData;
extern const char *Process_fieldNames[]; void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
extern const int Process_fieldFlags[];
extern const char *Process_fieldTitles[]; extern ProcessFieldData Process_fields[];
void Process_setupColumnWidths(); void Process_setupColumnWidths();
...@@ -189,11 +189,13 @@ void Process_setupColumnWidths(); ...@@ -189,11 +189,13 @@ void Process_setupColumnWidths();
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
void Process_writeDefaultField(Process* this, RichString* str, ProcessField field);
void Process_delete(Object* cast); void Process_delete(Object* cast);
extern ObjectClass Process_class; extern ObjectClass Process_class;
Process* Process_new(struct ProcessList_ *pl); Process* Process_new(struct Settings_* settings);
void Process_toggleTag(Process* this); void Process_toggleTag(Process* this);
...@@ -201,24 +203,10 @@ bool Process_setPriority(Process* this, int priority); ...@@ -201,24 +203,10 @@ bool Process_setPriority(Process* this, int priority);
bool Process_changePriorityBy(Process* this, size_t delta); bool Process_changePriorityBy(Process* this, size_t delta);
#ifdef HAVE_LIBHWLOC
Affinity* Process_getAffinity(Process* this);
bool Process_setAffinity(Process* this, Affinity* affinity);
#elif HAVE_NATIVE_AFFINITY
Affinity* Process_getAffinity(Process* this);
bool Process_setAffinity(Process* this, Affinity* affinity);
#endif
void Process_sendSignal(Process* this, size_t sgn); void Process_sendSignal(Process* this, size_t sgn);
long Process_pidCompare(const void* v1, const void* v2); long Process_pidCompare(const void* v1, const void* v2);
long Process_compare(const void* v1, const void* v2); long Process_defaultCompare(const void* v1, const void* v2);
#endif #endif
...@@ -19,6 +19,7 @@ in the source distribution for its full text. ...@@ -19,6 +19,7 @@ in the source distribution for its full text.
#include "UsersTable.h" #include "UsersTable.h"
#include "Panel.h" #include "Panel.h"
#include "Process.h" #include "Process.h"
#include "Settings.h"
#ifndef MAX_NAME #ifndef MAX_NAME
#define MAX_NAME 128 #define MAX_NAME 128
...@@ -28,51 +29,9 @@ in the source distribution for its full text. ...@@ -28,51 +29,9 @@ in the source distribution for its full text.
#define MAX_READ 2048 #define MAX_READ 2048
#endif #endif
#ifndef ProcessList_cpuId
#define ProcessList_cpuId(pl, cpu) ((pl)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif
typedef enum TreeStr_ {
TREE_STR_HORZ,
TREE_STR_VERT,
TREE_STR_RTEE,
TREE_STR_BEND,
TREE_STR_TEND,
TREE_STR_OPEN,
TREE_STR_SHUT,
TREE_STR_COUNT
} TreeStr;
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct ProcessList_ { typedef struct ProcessList_ {
const char **treeStr; Settings* settings;
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
Hashtable* processTable; Hashtable* processTable;
...@@ -84,90 +43,33 @@ typedef struct ProcessList_ { ...@@ -84,90 +43,33 @@ typedef struct ProcessList_ {
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList; Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
#ifdef HAVE_LIBHWLOC #ifdef HAVE_LIBHWLOC
hwloc_topology_t topology; hwloc_topology_t topology;
bool topologyOk; bool topologyOk;
#endif #endif
CPUData* cpus;
int cpuCount;
unsigned long long int totalMem;
unsigned long long int usedMem;
unsigned long long int freeMem;
unsigned long long int sharedMem;
unsigned long long int buffersMem;
unsigned long long int cachedMem;
unsigned long long int totalSwap;
unsigned long long int usedSwap;
unsigned long long int freeSwap;
int flags;
ProcessField* fields;
ProcessField sortKey;
int direction;
bool hideThreads;
bool shadowOtherUsers;
bool showThreadNames;
bool showingThreadNames;
bool hideKernelThreads;
bool hideUserlandThreads;
bool treeView;
bool highlightBaseName;
bool highlightMegabytes;
bool highlightThreads;
bool detailedCPUTime;
bool countCPUsFromZero;
bool updateProcessNames;
bool accountGuestInCPUMeter;
bool userOnly;
} ProcessList; } ProcessList;
ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList); ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList, uid_t userId);
void ProcessList_delete(ProcessList* pl); void ProcessList_delete(ProcessList* pl);
void ProcessList_scan(ProcessList* pl); void ProcessList_scan(ProcessList* pl);
}*/ }*/
static ProcessField defaultHeaders[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
const char *ProcessList_treeStrAscii[TREE_STR_COUNT] = {
"-", // TREE_STR_HORZ
"|", // TREE_STR_VERT
"`", // TREE_STR_RTEE
"`", // TREE_STR_BEND
",", // TREE_STR_TEND
"+", // TREE_STR_OPEN
"-", // TREE_STR_SHUT
};
const char *ProcessList_treeStrUtf8[TREE_STR_COUNT] = {
"\xe2\x94\x80", // TREE_STR_HORZ ─
"\xe2\x94\x82", // TREE_STR_VERT │
"\xe2\x94\x9c", // TREE_STR_RTEE ├
"\xe2\x94\x94", // TREE_STR_BEND └
"\xe2\x94\x8c", // TREE_STR_TEND ┌
"+", // TREE_STR_OPEN +
"\xe2\x94\x80", // TREE_STR_SHUT ─
};
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList) {
this->processes = Vector_new(Class(Process), true, DEFAULT_SIZE); this->processes = Vector_new(Class(Process), true, DEFAULT_SIZE);
this->processTable = Hashtable_new(140, false); this->processTable = Hashtable_new(140, false);
this->usersTable = usersTable; this->usersTable = usersTable;
this->pidWhiteList = pidWhiteList; this->pidWhiteList = pidWhiteList;
this->userId = userId;
// tree-view auxiliary buffers // tree-view auxiliary buffers
this->processes2 = Vector_new(Class(Process), true, DEFAULT_SIZE); this->processes2 = Vector_new(Class(Process), true, DEFAULT_SIZE);
// set later by platform-specific code // set later by platform-specific code
this->cpuCount = 0; this->cpuCount = 0;
this->cpus = NULL;
#ifdef HAVE_LIBHWLOC #ifdef HAVE_LIBHWLOC
this->topologyOk = false; this->topologyOk = false;
...@@ -180,35 +82,8 @@ ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtab ...@@ -180,35 +82,8 @@ ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtab
} }
#endif #endif
this->fields = calloc(LAST_PROCESSFIELD+1, sizeof(ProcessField));
// TODO: turn 'fields' into a Vector,
// (and ProcessFields into proper objects).
this->flags = 0;
for (int i = 0; defaultHeaders[i]; i++) {
this->fields[i] = defaultHeaders[i];
this->flags |= Process_fieldFlags[defaultHeaders[i]];
}
this->sortKey = PERCENT_CPU;
this->direction = 1;
this->hideThreads = false;
this->shadowOtherUsers = false;
this->showThreadNames = false;
this->showingThreadNames = false;
this->hideKernelThreads = false;
this->hideUserlandThreads = false;
this->treeView = false;
this->highlightBaseName = false;
this->highlightMegabytes = false;
this->detailedCPUTime = false;
this->countCPUsFromZero = false;
this->updateProcessNames = false;
this->treeStr = NULL;
this->following = -1; this->following = -1;
if (CRT_utf8)
this->treeStr = CRT_utf8 ? ProcessList_treeStrUtf8 : ProcessList_treeStrAscii;
return this; return this;
} }
...@@ -216,27 +91,19 @@ void ProcessList_done(ProcessList* this) { ...@@ -216,27 +91,19 @@ void ProcessList_done(ProcessList* this) {
Hashtable_delete(this->processTable); Hashtable_delete(this->processTable);
Vector_delete(this->processes); Vector_delete(this->processes);
Vector_delete(this->processes2); Vector_delete(this->processes2);
free(this->cpus);
free(this->fields);
} }
void ProcessList_setPanel(ProcessList* this, Panel* panel) { void ProcessList_setPanel(ProcessList* this, Panel* panel) {
this->panel = panel; this->panel = panel;
} }
void ProcessList_invertSortOrder(ProcessList* this) {
if (this->direction == 1)
this->direction = -1;
else
this->direction = 1;
}
void ProcessList_printHeader(ProcessList* this, RichString* header) { void ProcessList_printHeader(ProcessList* this, RichString* header) {
RichString_prune(header); RichString_prune(header);
ProcessField* fields = this->fields; ProcessField* fields = this->settings->fields;
for (int i = 0; fields[i]; i++) { for (int i = 0; fields[i]; i++) {
const char* field = Process_fieldTitles[fields[i]]; const char* field = Process_fields[fields[i]].title;
if (!this->treeView && this->sortKey == fields[i]) if (!field) field = "- ";
if (!this->settings->treeView && this->settings->sortKey == fields[i])
RichString_append(header, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field); RichString_append(header, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field);
else else
RichString_append(header, CRT_colors[PANEL_HEADER_FOCUS], field); RichString_append(header, CRT_colors[PANEL_HEADER_FOCUS], field);
...@@ -308,19 +175,19 @@ static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int i ...@@ -308,19 +175,19 @@ static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int i
} }
void ProcessList_sort(ProcessList* this) { void ProcessList_sort(ProcessList* this) {
if (!this->treeView) { if (!this->settings->treeView) {
Vector_insertionSort(this->processes); Vector_insertionSort(this->processes);
} else { } else {
// Save settings // Save settings
int direction = this->direction; int direction = this->settings->direction;
int sortKey = this->sortKey; int sortKey = this->settings->sortKey;
// Sort by PID // Sort by PID
this->sortKey = PID; this->settings->sortKey = PID;
this->direction = 1; this->settings->direction = 1;
Vector_quickSort(this->processes); Vector_quickSort(this->processes);
// Restore settings // Restore settings
this->sortKey = sortKey; this->settings->sortKey = sortKey;
this->direction = direction; this->settings->direction = direction;
// Take PID 1 as root and add to the new listing // Take PID 1 as root and add to the new listing
int vsize = Vector_size(this->processes); int vsize = Vector_size(this->processes);
Process* init = (Process*) (Vector_take(this->processes, 0)); Process* init = (Process*) (Vector_take(this->processes, 0));
...@@ -351,10 +218,12 @@ void ProcessList_sort(ProcessList* this) { ...@@ -351,10 +218,12 @@ void ProcessList_sort(ProcessList* this) {
ProcessField ProcessList_keyAt(ProcessList* this, int at) { ProcessField ProcessList_keyAt(ProcessList* this, int at) {
int x = 0; int x = 0;
ProcessField* fields = this->fields; ProcessField* fields = this->settings->fields;
ProcessField field; ProcessField field;
for (int i = 0; (field = fields[i]); i++) { for (int i = 0; (field = fields[i]); i++) {
int len = strlen(Process_fieldTitles[field]); const char* title = Process_fields[field].title;
if (!title) title = "- ";
int len = strlen(title);
if (at >= x && at <= x + len) { if (at >= x && at <= x + len) {
return field; return field;
} }
...@@ -371,17 +240,12 @@ void ProcessList_expandTree(ProcessList* this) { ...@@ -371,17 +240,12 @@ void ProcessList_expandTree(ProcessList* this) {
} }
} }
void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, const char* incFilter) { void ProcessList_rebuildPanel(ProcessList* this) {
if (!flags) { int following = this->following;
following = this->following; const char* incFilter = this->incFilter;
incFilter = this->incFilter;
} else {
this->following = following;
this->incFilter = incFilter;
}
int currPos = Panel_getSelectedIndex(this->panel); int currPos = Panel_getSelectedIndex(this->panel);
pid_t currPid = following != -1 ? following : 0; pid_t currPid = this->following != -1 ? this->following : 0;
int currScrollV = this->panel->scrollV; int currScrollV = this->panel->scrollV;
Panel_prune(this->panel); Panel_prune(this->panel);
...@@ -392,14 +256,14 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, cons ...@@ -392,14 +256,14 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, cons
Process* p = ProcessList_get(this, i); Process* p = ProcessList_get(this, i);
if ( (!p->show) if ( (!p->show)
|| (this->userOnly && (p->st_uid != this->userId)) || (this->userId != (uid_t) -1 && (p->st_uid != this->userId))
|| (incFilter && !(String_contains_i(p->comm, incFilter))) || (incFilter && !(String_contains_i(p->comm, incFilter)))
|| (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) ) || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
hidden = true; hidden = true;
if (!hidden) { if (!hidden) {
Panel_set(this->panel, idx, (Object*)p); Panel_set(this->panel, idx, (Object*)p);
if ((following == -1 && idx == currPos) || (following != -1 && p->pid == currPid)) { if ((this->following == -1 && idx == currPos) || (this->following != -1 && p->pid == currPid)) {
Panel_setSelected(this->panel, idx); Panel_setSelected(this->panel, idx);
this->panel->scrollV = currScrollV; this->panel->scrollV = currScrollV;
} }
......
...@@ -14,6 +14,7 @@ in the source distribution for its full text. ...@@ -14,6 +14,7 @@ in the source distribution for its full text.
#include "UsersTable.h" #include "UsersTable.h"
#include "Panel.h" #include "Panel.h"
#include "Process.h" #include "Process.h"
#include "Settings.h"
#ifndef MAX_NAME #ifndef MAX_NAME
#define MAX_NAME 128 #define MAX_NAME 128
...@@ -23,51 +24,9 @@ in the source distribution for its full text. ...@@ -23,51 +24,9 @@ in the source distribution for its full text.
#define MAX_READ 2048 #define MAX_READ 2048
#endif #endif
#ifndef ProcessList_cpuId
#define ProcessList_cpuId(pl, cpu) ((pl)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif
typedef enum TreeStr_ {
TREE_STR_HORZ,
TREE_STR_VERT,
TREE_STR_RTEE,
TREE_STR_BEND,
TREE_STR_TEND,
TREE_STR_OPEN,
TREE_STR_SHUT,
TREE_STR_COUNT
} TreeStr;
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct ProcessList_ { typedef struct ProcessList_ {
const char **treeStr; Settings* settings;
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
Hashtable* processTable; Hashtable* processTable;
...@@ -79,67 +38,26 @@ typedef struct ProcessList_ { ...@@ -79,67 +38,26 @@ typedef struct ProcessList_ {
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList; Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
#ifdef HAVE_LIBHWLOC #ifdef HAVE_LIBHWLOC
hwloc_topology_t topology; hwloc_topology_t topology;
bool topologyOk; bool topologyOk;
#endif #endif
CPUData* cpus;
int cpuCount;
unsigned long long int totalMem;
unsigned long long int usedMem;
unsigned long long int freeMem;
unsigned long long int sharedMem;
unsigned long long int buffersMem;
unsigned long long int cachedMem;
unsigned long long int totalSwap;
unsigned long long int usedSwap;
unsigned long long int freeSwap;
int flags;
ProcessField* fields;
ProcessField sortKey;
int direction;
bool hideThreads;
bool shadowOtherUsers;
bool showThreadNames;
bool showingThreadNames;
bool hideKernelThreads;
bool hideUserlandThreads;
bool treeView;
bool highlightBaseName;
bool highlightMegabytes;
bool highlightThreads;
bool detailedCPUTime;
bool countCPUsFromZero;
bool updateProcessNames;
bool accountGuestInCPUMeter;
bool userOnly;
} ProcessList; } ProcessList;
ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList); ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList, uid_t userId);
void ProcessList_delete(ProcessList* pl); void ProcessList_delete(ProcessList* pl);
void ProcessList_scan(ProcessList* pl); void ProcessList_scan(ProcessList* pl);
extern const char *ProcessList_treeStrAscii[TREE_STR_COUNT]; ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
extern const char *ProcessList_treeStrUtf8[TREE_STR_COUNT];
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList);
void ProcessList_done(ProcessList* this); void ProcessList_done(ProcessList* this);
void ProcessList_setPanel(ProcessList* this, Panel* panel); void ProcessList_setPanel(ProcessList* this, Panel* panel);
void ProcessList_invertSortOrder(ProcessList* this);
void ProcessList_printHeader(ProcessList* this, RichString* header); void ProcessList_printHeader(ProcessList* this, RichString* header);
void ProcessList_add(ProcessList* this, Process* p); void ProcessList_add(ProcessList* this, Process* p);
...@@ -156,7 +74,7 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at); ...@@ -156,7 +74,7 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at);
void ProcessList_expandTree(ProcessList* this); void ProcessList_expandTree(ProcessList* this);
void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, const char* incFilter); void ProcessList_rebuildPanel(ProcessList* this);
#endif #endif
...@@ -6,8 +6,8 @@ in the source distribution for its full text. ...@@ -6,8 +6,8 @@ in the source distribution for its full text.
*/ */
#include "ScreenManager.h" #include "ScreenManager.h"
#include "ProcessList.h"
#include "Panel.h"
#include "Object.h" #include "Object.h"
#include <assert.h> #include <assert.h>
...@@ -19,6 +19,8 @@ in the source distribution for its full text. ...@@ -19,6 +19,8 @@ in the source distribution for its full text.
#include "FunctionBar.h" #include "FunctionBar.h"
#include "Vector.h" #include "Vector.h"
#include "Header.h" #include "Header.h"
#include "Settings.h"
#include "Panel.h"
typedef enum Orientation_ { typedef enum Orientation_ {
VERTICAL, VERTICAL,
...@@ -36,14 +38,14 @@ typedef struct ScreenManager_ { ...@@ -36,14 +38,14 @@ typedef struct ScreenManager_ {
int panelCount; int panelCount;
const FunctionBar* fuBar; const FunctionBar* fuBar;
const Header* header; const Header* header;
time_t lastScan; const Settings* settings;
bool owner; bool owner;
bool allowFocusChange; bool allowFocusChange;
} ScreenManager; } ScreenManager;
}*/ }*/
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, bool owner) { ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) {
ScreenManager* this; ScreenManager* this;
this = malloc(sizeof(ScreenManager)); this = malloc(sizeof(ScreenManager));
this->x1 = x1; this->x1 = x1;
...@@ -56,6 +58,7 @@ ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation ori ...@@ -56,6 +58,7 @@ ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation ori
this->fuBars = Vector_new(Class(FunctionBar), true, DEFAULT_SIZE); this->fuBars = Vector_new(Class(FunctionBar), true, DEFAULT_SIZE);
this->panelCount = 0; this->panelCount = 0;
this->header = header; this->header = header;
this->settings = settings;
this->owner = owner; this->owner = owner;
this->allowFocusChange = true; this->allowFocusChange = true;
return this; return this;
...@@ -130,37 +133,68 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { ...@@ -130,37 +133,68 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
Panel* panelFocus = (Panel*) Vector_get(this->panels, focus); Panel* panelFocus = (Panel*) Vector_get(this->panels, focus);
if (this->fuBar) if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL); FunctionBar_draw(this->fuBar, NULL);
this->lastScan = 0;
int ch = 0; struct timeval tv;
double oldTime = 0.0;
int ch = ERR;
int closeTimeout = 0;
bool drawPanel = true;
bool timeToRecalculate = true;
bool doRefresh = true;
bool forceRecalculate = false;
int sortTimeout = 0;
int resetSortTimeout = 5;
while (!quit) { while (!quit) {
int panels = this->panelCount; int panels = this->panelCount;
if (this->header) { if (this->header) {
time_t now = time(NULL); gettimeofday(&tv, NULL);
if (now > this->lastScan) { double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
ProcessList_scan(this->header->pl); timeToRecalculate = (newTime - oldTime > this->settings->delay);
ProcessList_sort(this->header->pl); if (newTime < oldTime) timeToRecalculate = true; // clock was adjusted?
this->lastScan = now;
if (timeToRecalculate) {
Header_draw(this->header);
oldTime = newTime;
}
if (doRefresh) {
if (timeToRecalculate || forceRecalculate) {
ProcessList_scan(this->header->pl);
forceRecalculate = false;
}
if (sortTimeout == 0 || this->settings->treeView) {
ProcessList_sort(this->header->pl);
sortTimeout = 1;
}
//this->header->pl->incFilter = IncSet_filter(inc);
ProcessList_rebuildPanel(this->header->pl);
drawPanel = true;
} }
Header_draw(this->header); doRefresh = true;
ProcessList_rebuildPanel(this->header->pl, false, false, NULL);
} }
for (int i = 0; i < panels; i++) {
Panel* panel = (Panel*) Vector_get(this->panels, i); if (drawPanel) {
Panel_draw(panel, i == focus); for (int i = 0; i < panels; i++) {
if (i < panels) { Panel* panel = (Panel*) Vector_get(this->panels, i);
if (this->orientation == HORIZONTAL) { Panel_draw(panel, i == focus);
mvvline(panel->y, panel->x+panel->w, ' ', panel->h+1); if (i < panels) {
if (this->orientation == HORIZONTAL) {
mvvline(panel->y, panel->x+panel->w, ' ', panel->h+1);
}
} }
} }
} }
FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus); FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus);
if (bar) if (bar)
this->fuBar = bar; this->fuBar = bar;
if (this->fuBar) if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL); FunctionBar_draw(this->fuBar, NULL);
int prevCh = ch;
ch = getch(); ch = getch();
if (ch == KEY_MOUSE) { if (ch == KEY_MOUSE) {
...@@ -187,17 +221,36 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { ...@@ -187,17 +221,36 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
if (Panel_eventHandlerFn(panelFocus)) { if (Panel_eventHandlerFn(panelFocus)) {
HandlerResult result = Panel_eventHandler(panelFocus, ch); HandlerResult result = Panel_eventHandler(panelFocus, ch);
if (result == HANDLED) { if (result & REFRESH) {
doRefresh = true;
sortTimeout = 0;
}
if (result & RECALCULATE) {
forceRecalculate = true;
sortTimeout = 0;
}
if (result & HANDLED) {
continue; continue;
} else if (result == BREAK_LOOP) { } else if (result & BREAK_LOOP) {
quit = true; quit = true;
continue; continue;
} }
} }
switch (ch) { if (ch == ERR) {
case ERR: if (prevCh == ch && !timeToRecalculate) {
closeTimeout++;
if (closeTimeout == 100) {
break;
}
} else
closeTimeout = 0;
drawPanel = false;
continue; continue;
}
drawPanel = true;
switch (ch) {
case KEY_RESIZE: case KEY_RESIZE:
{ {
ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2); ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2);
...@@ -237,6 +290,8 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { ...@@ -237,6 +290,8 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
} }
} }
*lastFocus = panelFocus; if (lastFocus)
*lastKey = ch; *lastFocus = panelFocus;
if (lastKey)
*lastKey = ch;
} }
...@@ -12,6 +12,8 @@ in the source distribution for its full text. ...@@ -12,6 +12,8 @@ in the source distribution for its full text.
#include "FunctionBar.h" #include "FunctionBar.h"
#include "Vector.h" #include "Vector.h"
#include "Header.h" #include "Header.h"
#include "Settings.h"
#include "Panel.h"
typedef enum Orientation_ { typedef enum Orientation_ {
VERTICAL, VERTICAL,
...@@ -29,13 +31,13 @@ typedef struct ScreenManager_ { ...@@ -29,13 +31,13 @@ typedef struct ScreenManager_ {
int panelCount; int panelCount;
const FunctionBar* fuBar; const FunctionBar* fuBar;
const Header* header; const Header* header;
time_t lastScan; const Settings* settings;
bool owner; bool owner;
bool allowFocusChange; bool allowFocusChange;
} ScreenManager; } ScreenManager;
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, bool owner); ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner);
void ScreenManager_delete(ScreenManager* this); void ScreenManager_delete(ScreenManager* this);
......
...@@ -18,64 +18,138 @@ in the source distribution for its full text. ...@@ -18,64 +18,138 @@ in the source distribution for its full text.
#define DEFAULT_DELAY 15 #define DEFAULT_DELAY 15
/*{ /*{
#include "ProcessList.h" #include "Process.h"
#include "Header.h"
#include <stdbool.h> #include <stdbool.h>
typedef struct {
int len;
char** names;
int* modes;
} MeterColumnSettings;
typedef struct Settings_ { typedef struct Settings_ {
char* userSettings; char* filename;
ProcessList* pl;
Header* header; MeterColumnSettings columns[2];
ProcessField* fields;
int flags;
int colorScheme; int colorScheme;
int delay; int delay;
int direction;
ProcessField sortKey;
bool countCPUsFromZero;
bool detailedCPUTime;
bool treeView;
bool hideThreads;
bool shadowOtherUsers;
bool showThreadNames;
bool hideKernelThreads;
bool hideUserlandThreads;
bool highlightBaseName;
bool highlightMegabytes;
bool highlightThreads;
bool updateProcessNames;
bool accountGuestInCPUMeter;
bool headerMargin;
bool changed; bool changed;
} Settings; } Settings;
#ifndef Settings_cpuId
#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif
}*/ }*/
static ProcessField defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
//static ProcessField defaultIoFields[] = { PID, IO_PRIORITY, USER, IO_READ_RATE, IO_WRITE_RATE, IO_RATE, COMM, 0 };
void Settings_delete(Settings* this) { void Settings_delete(Settings* this) {
free(this->userSettings); free(this->filename);
free(this->fields);
for (unsigned int i = 0; i < (sizeof(this->columns)/sizeof(MeterColumnSettings)); i++) {
String_freeArray(this->columns[i].names);
free(this->columns[i].modes);
}
free(this); free(this);
} }
static void Settings_readMeters(Settings* this, char* line, HeaderSide side) { static void Settings_readMeters(Settings* this, char* line, int column) {
char* trim = String_trim(line); char* trim = String_trim(line);
int nIds; int nIds;
char** ids = String_split(trim, ' ', &nIds); char** ids = String_split(trim, ' ', &nIds);
free(trim); free(trim);
for (int i = 0; ids[i]; i++) { this->columns[column].names = ids;
Header_createMeter(this->header, ids[i], side);
}
String_freeArray(ids);
} }
static void Settings_readMeterModes(Settings* this, char* line, HeaderSide side) { static void Settings_readMeterModes(Settings* this, char* line, int column) {
char* trim = String_trim(line); char* trim = String_trim(line);
int nIds; int nIds;
char** ids = String_split(trim, ' ', &nIds); char** ids = String_split(trim, ' ', &nIds);
free(trim); free(trim);
int len = 0;
for (int i = 0; ids[i]; i++) { for (int i = 0; ids[i]; i++) {
int mode = atoi(ids[i]); len++;
Header_setMode(this->header, i, mode, side); }
this->columns[column].len = len;
int* modes = calloc(len, sizeof(int));
for (int i = 0; i < len; i++) {
modes[i] = atoi(ids[i]);
} }
String_freeArray(ids); String_freeArray(ids);
this->columns[column].modes = modes;
} }
static void Settings_defaultMeters(Header* header, int cpuCount) { static void Settings_defaultMeters(Settings* this, int cpuCount) {
int sizes[] = { 3, 3 };
if (cpuCount > 4) {
sizes[1]++;
}
for (int i = 0; i < 2; i++) {
this->columns[i].names = calloc(sizes[i], sizeof(char*));
this->columns[i].modes = calloc(sizes[i], sizeof(int));
}
int r = 0;
if (cpuCount > 8) { if (cpuCount > 8) {
Header_createMeter(header, "LeftCPUs2", LEFT_HEADER); this->columns[0].names[0] = strdup("LeftCPUs2");
Header_createMeter(header, "RightCPUs2", RIGHT_HEADER); this->columns[1].names[r++] = strdup("RightCPUs2");
} else if (cpuCount > 4) { } else if (cpuCount > 4) {
Header_createMeter(header, "LeftCPUs", LEFT_HEADER); this->columns[0].names[0] = strdup("LeftCPUs");
Header_createMeter(header, "RightCPUs", RIGHT_HEADER); this->columns[1].names[r++] = strdup("RightCPUs");
} else { } else {
Header_createMeter(header, "AllCPUs", LEFT_HEADER); this->columns[0].names[0] = strdup("AllCPUs");
}
this->columns[0].names[1] = strdup("Memory");
this->columns[0].names[2] = strdup("Swap");
this->columns[1].names[r++] = strdup("Tasks");
this->columns[1].names[r++] = strdup("LoadAverage");
this->columns[1].names[r++] = strdup("Uptime");
}
static void readFields(ProcessField* fields, int* flags, const char* line) {
char* trim = String_trim(line);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
free(trim);
int i, j;
*flags = 0;
for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) {
// This "+1" is for compatibility with the older enum format.
int id = atoi(ids[i]) + 1;
if (id > 0 && id < LAST_PROCESSFIELD) {
fields[j] = id;
*flags |= Process_fields[id].flags;
j++;
}
} }
Header_createMeter(header, "Memory", LEFT_HEADER); fields[j] = (ProcessField) NULL;
Header_createMeter(header, "Swap", LEFT_HEADER); String_freeArray(ids);
Header_createMeter(header, "Tasks", RIGHT_HEADER);
Header_createMeter(header, "LoadAverage", RIGHT_HEADER);
Header_createMeter(header, "Uptime", RIGHT_HEADER);
} }
static bool Settings_read(Settings* this, const char* fileName, int cpuCount) { static bool Settings_read(Settings* this, const char* fileName, int cpuCount) {
...@@ -94,59 +168,43 @@ static bool Settings_read(Settings* this, const char* fileName, int cpuCount) { ...@@ -94,59 +168,43 @@ static bool Settings_read(Settings* this, const char* fileName, int cpuCount) {
continue; continue;
} }
if (String_eq(option[0], "fields")) { if (String_eq(option[0], "fields")) {
char* trim = String_trim(option[1]); readFields(this->fields, &(this->flags), option[1]);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
free(trim);
int i, j;
this->pl->flags = 0;
for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) {
// This "+1" is for compatibility with the older enum format.
int id = atoi(ids[i]) + 1;
if (id > 0 && id < LAST_PROCESSFIELD) {
this->pl->fields[j] = id;
this->pl->flags |= Process_fieldFlags[id];
j++;
}
}
this->pl->fields[j] = (ProcessField) NULL;
String_freeArray(ids);
} else if (String_eq(option[0], "sort_key")) { } else if (String_eq(option[0], "sort_key")) {
// This "+1" is for compatibility with the older enum format. // This "+1" is for compatibility with the older enum format.
this->pl->sortKey = atoi(option[1]) + 1; this->sortKey = atoi(option[1]) + 1;
} else if (String_eq(option[0], "sort_direction")) { } else if (String_eq(option[0], "sort_direction")) {
this->pl->direction = atoi(option[1]); this->direction = atoi(option[1]);
} else if (String_eq(option[0], "tree_view")) { } else if (String_eq(option[0], "tree_view")) {
this->pl->treeView = atoi(option[1]); this->treeView = atoi(option[1]);
} else if (String_eq(option[0], "hide_threads")) { } else if (String_eq(option[0], "hide_threads")) {
this->pl->hideThreads = atoi(option[1]); this->hideThreads = atoi(option[1]);
} else if (String_eq(option[0], "hide_kernel_threads")) { } else if (String_eq(option[0], "hide_kernel_threads")) {
this->pl->hideKernelThreads = atoi(option[1]); this->hideKernelThreads = atoi(option[1]);
} else if (String_eq(option[0], "hide_userland_threads")) { } else if (String_eq(option[0], "hide_userland_threads")) {
this->pl->hideUserlandThreads = atoi(option[1]); this->hideUserlandThreads = atoi(option[1]);
} else if (String_eq(option[0], "shadow_other_users")) { } else if (String_eq(option[0], "shadow_other_users")) {
this->pl->shadowOtherUsers = atoi(option[1]); this->shadowOtherUsers = atoi(option[1]);
} else if (String_eq(option[0], "show_thread_names")) { } else if (String_eq(option[0], "show_thread_names")) {
this->pl->showThreadNames = atoi(option[1]); this->showThreadNames = atoi(option[1]);
} else if (String_eq(option[0], "highlight_base_name")) { } else if (String_eq(option[0], "highlight_base_name")) {
this->pl->highlightBaseName = atoi(option[1]); this->highlightBaseName = atoi(option[1]);
} else if (String_eq(option[0], "highlight_megabytes")) { } else if (String_eq(option[0], "highlight_megabytes")) {
this->pl->highlightMegabytes = atoi(option[1]); this->highlightMegabytes = atoi(option[1]);
} else if (String_eq(option[0], "highlight_threads")) { } else if (String_eq(option[0], "highlight_threads")) {
this->pl->highlightThreads = atoi(option[1]); this->highlightThreads = atoi(option[1]);
} else if (String_eq(option[0], "header_margin")) { } else if (String_eq(option[0], "header_margin")) {
this->header->margin = atoi(option[1]); this->headerMargin = atoi(option[1]);
} else if (String_eq(option[0], "expand_system_time")) { } else if (String_eq(option[0], "expand_system_time")) {
// Compatibility option. // Compatibility option.
this->pl->detailedCPUTime = atoi(option[1]); this->detailedCPUTime = atoi(option[1]);
} else if (String_eq(option[0], "detailed_cpu_time")) { } else if (String_eq(option[0], "detailed_cpu_time")) {
this->pl->detailedCPUTime = atoi(option[1]); this->detailedCPUTime = atoi(option[1]);
} else if (String_eq(option[0], "cpu_count_from_zero")) { } else if (String_eq(option[0], "cpu_count_from_zero")) {
this->pl->countCPUsFromZero = atoi(option[1]); this->countCPUsFromZero = atoi(option[1]);
} else if (String_eq(option[0], "update_process_names")) { } else if (String_eq(option[0], "update_process_names")) {
this->pl->updateProcessNames = atoi(option[1]); this->updateProcessNames = atoi(option[1]);
} else if (String_eq(option[0], "account_guest_in_cpu_meter")) { } else if (String_eq(option[0], "account_guest_in_cpu_meter")) {
this->pl->accountGuestInCPUMeter = atoi(option[1]); this->accountGuestInCPUMeter = atoi(option[1]);
} else if (String_eq(option[0], "delay")) { } else if (String_eq(option[0], "delay")) {
this->delay = atoi(option[1]); this->delay = atoi(option[1]);
} else if (String_eq(option[0], "color_scheme")) { } else if (String_eq(option[0], "color_scheme")) {
...@@ -154,96 +212,118 @@ static bool Settings_read(Settings* this, const char* fileName, int cpuCount) { ...@@ -154,96 +212,118 @@ static bool Settings_read(Settings* this, const char* fileName, int cpuCount) {
if (this->colorScheme < 0) this->colorScheme = 0; if (this->colorScheme < 0) this->colorScheme = 0;
if (this->colorScheme > 5) this->colorScheme = 5; if (this->colorScheme > 5) this->colorScheme = 5;
} else if (String_eq(option[0], "left_meters")) { } else if (String_eq(option[0], "left_meters")) {
Settings_readMeters(this, option[1], LEFT_HEADER); Settings_readMeters(this, option[1], 0);
readMeters = true; readMeters = true;
} else if (String_eq(option[0], "right_meters")) { } else if (String_eq(option[0], "right_meters")) {
Settings_readMeters(this, option[1], RIGHT_HEADER); Settings_readMeters(this, option[1], 1);
readMeters = true; readMeters = true;
} else if (String_eq(option[0], "left_meter_modes")) { } else if (String_eq(option[0], "left_meter_modes")) {
Settings_readMeterModes(this, option[1], LEFT_HEADER); Settings_readMeterModes(this, option[1], 0);
readMeters = true; readMeters = true;
} else if (String_eq(option[0], "right_meter_modes")) { } else if (String_eq(option[0], "right_meter_modes")) {
Settings_readMeterModes(this, option[1], RIGHT_HEADER); Settings_readMeterModes(this, option[1], 1);
readMeters = true; readMeters = true;
} }
String_freeArray(option); String_freeArray(option);
} }
fclose(fd); fclose(fd);
if (!readMeters) { if (!readMeters) {
Settings_defaultMeters(this->header, cpuCount); Settings_defaultMeters(this, cpuCount);
} }
return true; return true;
} }
static void writeFields(FILE* fd, ProcessField* fields, const char* name) {
fprintf(fd, "%s=", name);
for (int i = 0; fields[i]; i++) {
// This "-1" is for compatibility with the older enum format.
fprintf(fd, "%d ", (int) fields[i]-1);
}
fprintf(fd, "\n");
}
static void writeMeters(Settings* this, FILE* fd, int column) {
for (int i = 0; i < this->columns[column].len; i++) {
fprintf(fd, "%s ", this->columns[column].names[i]);
}
fprintf(fd, "\n");
}
static void writeMeterModes(Settings* this, FILE* fd, int column) {
for (int i = 0; i < this->columns[column].len; i++) {
fprintf(fd, "%d ", this->columns[column].modes[i]);
}
fprintf(fd, "\n");
}
bool Settings_write(Settings* this) { bool Settings_write(Settings* this) {
// TODO: implement File object and make
// file I/O object-oriented.
FILE* fd; FILE* fd;
fd = fopen(this->userSettings, "w"); fd = fopen(this->filename, "w");
if (fd == NULL) { if (fd == NULL) {
return false; return false;
} }
fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n"); fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n");
fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n"); fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n");
fprintf(fd, "fields="); writeFields(fd, this->fields, "fields");
for (int i = 0; this->pl->fields[i]; i++) {
// This "-1" is for compatibility with the older enum format.
fprintf(fd, "%d ", (int) this->pl->fields[i]-1);
}
fprintf(fd, "\n");
// This "-1" is for compatibility with the older enum format. // This "-1" is for compatibility with the older enum format.
fprintf(fd, "sort_key=%d\n", (int) this->pl->sortKey-1); fprintf(fd, "sort_key=%d\n", (int) this->sortKey-1);
fprintf(fd, "sort_direction=%d\n", (int) this->pl->direction); fprintf(fd, "sort_direction=%d\n", (int) this->direction);
fprintf(fd, "hide_threads=%d\n", (int) this->pl->hideThreads); fprintf(fd, "hide_threads=%d\n", (int) this->hideThreads);
fprintf(fd, "hide_kernel_threads=%d\n", (int) this->pl->hideKernelThreads); fprintf(fd, "hide_kernel_threads=%d\n", (int) this->hideKernelThreads);
fprintf(fd, "hide_userland_threads=%d\n", (int) this->pl->hideUserlandThreads); fprintf(fd, "hide_userland_threads=%d\n", (int) this->hideUserlandThreads);
fprintf(fd, "shadow_other_users=%d\n", (int) this->pl->shadowOtherUsers); fprintf(fd, "shadow_other_users=%d\n", (int) this->shadowOtherUsers);
fprintf(fd, "show_thread_names=%d\n", (int) this->pl->showThreadNames); fprintf(fd, "show_thread_names=%d\n", (int) this->showThreadNames);
fprintf(fd, "highlight_base_name=%d\n", (int) this->pl->highlightBaseName); fprintf(fd, "highlight_base_name=%d\n", (int) this->highlightBaseName);
fprintf(fd, "highlight_megabytes=%d\n", (int) this->pl->highlightMegabytes); fprintf(fd, "highlight_megabytes=%d\n", (int) this->highlightMegabytes);
fprintf(fd, "highlight_threads=%d\n", (int) this->pl->highlightThreads); fprintf(fd, "highlight_threads=%d\n", (int) this->highlightThreads);
fprintf(fd, "tree_view=%d\n", (int) this->pl->treeView); fprintf(fd, "tree_view=%d\n", (int) this->treeView);
fprintf(fd, "header_margin=%d\n", (int) this->header->margin); fprintf(fd, "header_margin=%d\n", (int) this->headerMargin);
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->pl->detailedCPUTime); fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime);
fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->pl->countCPUsFromZero); fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->countCPUsFromZero);
fprintf(fd, "update_process_names=%d\n", (int) this->pl->updateProcessNames); fprintf(fd, "update_process_names=%d\n", (int) this->updateProcessNames);
fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->pl->accountGuestInCPUMeter); fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->accountGuestInCPUMeter);
fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme); fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme);
fprintf(fd, "delay=%d\n", (int) this->delay); fprintf(fd, "delay=%d\n", (int) this->delay);
fprintf(fd, "left_meters="); fprintf(fd, "left_meters="); writeMeters(this, fd, 0);
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++) { fprintf(fd, "left_meter_modes="); writeMeterModes(this, fd, 0);
char* name = Header_readMeterName(this->header, i, LEFT_HEADER); fprintf(fd, "right_meters="); writeMeters(this, fd, 1);
fprintf(fd, "%s ", name); fprintf(fd, "right_meter_modes="); writeMeterModes(this, fd, 1);
free(name);
}
fprintf(fd, "\n");
fprintf(fd, "left_meter_modes=");
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++)
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, LEFT_HEADER));
fprintf(fd, "\n");
fprintf(fd, "right_meters=");
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++) {
char* name = Header_readMeterName(this->header, i, RIGHT_HEADER);
fprintf(fd, "%s ", name);
free(name);
}
fprintf(fd, "\n");
fprintf(fd, "right_meter_modes=");
for (int i = 0; i < Header_size(this->header, RIGHT_HEADER); i++)
fprintf(fd, "%d ", Header_readMeterMode(this->header, i, RIGHT_HEADER));
fprintf(fd, "\n");
fclose(fd); fclose(fd);
return true; return true;
} }
Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) { Settings* Settings_new(int cpuCount) {
Settings* this = malloc(sizeof(Settings));
this->pl = pl; Settings* this = calloc(1, sizeof(Settings));
this->header = header;
this->sortKey = PERCENT_CPU;
this->direction = 1;
this->hideThreads = false;
this->shadowOtherUsers = false;
this->showThreadNames = false;
this->hideKernelThreads = false;
this->hideUserlandThreads = false;
this->treeView = false;
this->highlightBaseName = false;
this->highlightMegabytes = false;
this->detailedCPUTime = false;
this->countCPUsFromZero = false;
this->updateProcessNames = false;
this->fields = calloc(LAST_PROCESSFIELD+1, sizeof(ProcessField));
// TODO: turn 'fields' into a Vector,
// (and ProcessFields into proper objects).
this->flags = 0;
ProcessField* defaults = defaultFields;
for (int i = 0; defaults[i]; i++) {
this->fields[i] = defaults[i];
this->flags |= Process_fields[defaults[i]].flags;
}
char* legacyDotfile = NULL; char* legacyDotfile = NULL;
char* rcfile = getenv("HTOPRC"); char* rcfile = getenv("HTOPRC");
if (rcfile) { if (rcfile) {
this->userSettings = strdup(rcfile); this->filename = strdup(rcfile);
} else { } else {
const char* home = getenv("HOME"); const char* home = getenv("HOME");
if (!home) home = ""; if (!home) home = "";
...@@ -251,11 +331,11 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) { ...@@ -251,11 +331,11 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
char* configDir = NULL; char* configDir = NULL;
char* htopDir = NULL; char* htopDir = NULL;
if (xdgConfigHome) { if (xdgConfigHome) {
this->userSettings = String_cat(xdgConfigHome, "/htop/htoprc"); this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
configDir = strdup(xdgConfigHome); configDir = strdup(xdgConfigHome);
htopDir = String_cat(xdgConfigHome, "/htop"); htopDir = String_cat(xdgConfigHome, "/htop");
} else { } else {
this->userSettings = String_cat(home, "/.config/htop/htoprc"); this->filename = String_cat(home, "/.config/htop/htoprc");
configDir = String_cat(home, "/.config"); configDir = String_cat(home, "/.config");
htopDir = String_cat(home, "/.config/htop"); htopDir = String_cat(home, "/.config/htop");
} }
...@@ -276,7 +356,7 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) { ...@@ -276,7 +356,7 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
this->colorScheme = 0; this->colorScheme = 0;
this->changed = false; this->changed = false;
this->delay = DEFAULT_DELAY; this->delay = DEFAULT_DELAY;
bool ok = Settings_read(this, legacyDotfile ? legacyDotfile : this->userSettings, cpuCount); bool ok = Settings_read(this, legacyDotfile ? legacyDotfile : this->filename, cpuCount);
if (ok) { if (ok) {
if (legacyDotfile) { if (legacyDotfile) {
// Transition to new location and delete old configuration file // Transition to new location and delete old configuration file
...@@ -290,12 +370,19 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) { ...@@ -290,12 +370,19 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
ok = Settings_read(this, systemSettings, cpuCount); ok = Settings_read(this, systemSettings, cpuCount);
free(systemSettings); free(systemSettings);
if (!ok) { if (!ok) {
Settings_defaultMeters(this->header, cpuCount); Settings_defaultMeters(this, cpuCount);
pl->hideKernelThreads = true; this->hideKernelThreads = true;
pl->highlightMegabytes = true; this->highlightMegabytes = true;
pl->highlightThreads = false; this->highlightThreads = false;
} }
} }
free(legacyDotfile); free(legacyDotfile);
return this; return this;
} }
void Settings_invertSortOrder(Settings* this) {
if (this->direction == 1)
this->direction = -1;
else
this->direction = 1;
}
...@@ -11,24 +11,57 @@ in the source distribution for its full text. ...@@ -11,24 +11,57 @@ in the source distribution for its full text.
#define DEFAULT_DELAY 15 #define DEFAULT_DELAY 15
#include "ProcessList.h" #include "Process.h"
#include "Header.h"
#include <stdbool.h> #include <stdbool.h>
typedef struct {
int len;
char** names;
int* modes;
} MeterColumnSettings;
typedef struct Settings_ { typedef struct Settings_ {
char* userSettings; char* filename;
ProcessList* pl;
Header* header; MeterColumnSettings columns[2];
ProcessField* fields;
int flags;
int colorScheme; int colorScheme;
int delay; int delay;
int direction;
ProcessField sortKey;
bool countCPUsFromZero;
bool detailedCPUTime;
bool treeView;
bool hideThreads;
bool shadowOtherUsers;
bool showThreadNames;
bool hideKernelThreads;
bool hideUserlandThreads;
bool highlightBaseName;
bool highlightMegabytes;
bool highlightThreads;
bool updateProcessNames;
bool accountGuestInCPUMeter;
bool headerMargin;
bool changed; bool changed;
} Settings; } Settings;
#ifndef Settings_cpuId
#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif
void Settings_delete(Settings* this); void Settings_delete(Settings* this);
bool Settings_write(Settings* this); bool Settings_write(Settings* this);
Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount); Settings* Settings_new(int cpuCount);
void Settings_invertSortOrder(Settings* this);
#endif #endif
...@@ -8,7 +8,7 @@ in the source distribution for its full text. ...@@ -8,7 +8,7 @@ in the source distribution for its full text.
#include "SwapMeter.h" #include "SwapMeter.h"
#include "CRT.h" #include "CRT.h"
#include "ProcessList.h" #include "Platform.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -34,11 +34,8 @@ static void SwapMeter_humanNumber(char* buffer, const long int* value) { ...@@ -34,11 +34,8 @@ static void SwapMeter_humanNumber(char* buffer, const long int* value) {
} }
static void SwapMeter_setValues(Meter* this, char* buffer, int len) { static void SwapMeter_setValues(Meter* this, char* buffer, int len) {
long int usedSwap = this->pl->usedSwap; Platform_setSwapValues(this);
this->total = this->pl->totalSwap; snprintf(buffer, len, "%ld/%ldMB", (long int) this->values[0] / MEGABYTE, (long int) this->total / MEGABYTE);
this->values[0] = usedSwap;
snprintf(buffer, len, "%ld/%ldMB", (long int) usedSwap / MEGABYTE, (long int) this->total / MEGABYTE);
} }
static void SwapMeter_display(Object* cast, RichString* out) { static void SwapMeter_display(Object* cast, RichString* out) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment