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
in the source distribution for its full text.
*/
#include "ProcessList.h"
#include "Meter.h"
typedef enum HeaderSide_ {
LEFT_HEADER,
RIGHT_HEADER
} HeaderSide;
#include "Vector.h"
typedef struct Header_ {
Vector* leftMeters;
Vector* rightMeters;
ProcessList* pl;
Vector** columns;
struct ProcessList_* pl;
int height;
int pad;
int nrColumns;
bool margin;
} Header;
......@@ -31,21 +26,25 @@ typedef struct Header_ {
#define MAX(a,b) ((a)>(b)?(a):(b))
#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_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);
......
......@@ -22,6 +22,7 @@ typedef struct ListItem_ {
Object super;
char* value;
int key;
bool moving;
} ListItem;
}*/
......@@ -33,14 +34,19 @@ static void ListItem_delete(Object* cast) {
}
static void ListItem_display(Object* cast, RichString* out) {
ListItem* this = (ListItem*)cast;
ListItem* const this = (ListItem*)cast;
assert (this != NULL);
/*
int len = strlen(this->value)+1;
char buffer[len+1];
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 = {
......@@ -53,6 +59,7 @@ ListItem* ListItem_new(const char* value, int key) {
ListItem* this = AllocThis(ListItem);
this->value = strdup(value);
this->key = key;
this->moving = false;
return this;
}
......
......@@ -15,6 +15,7 @@ typedef struct ListItem_ {
Object super;
char* value;
int key;
bool moving;
} ListItem;
......
......@@ -16,7 +16,7 @@ htop_CFLAGS = -pedantic -Wall -Wextra -std=c99 -rdynamic -D_XOPEN_SOURCE_EXTENDE
AM_CPPFLAGS = -DNDEBUG
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 \
LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.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
myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.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 \
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 \
......
......@@ -8,7 +8,7 @@ in the source distribution for its full text.
#include "MemoryMeter.h"
#include "CRT.h"
#include "ProcessList.h"
#include "Platform.h"
#include <stdlib.h>
#include <string.h>
......@@ -25,15 +25,8 @@ int MemoryMeter_attributes[] = {
};
static void MemoryMeter_setValues(Meter* this, char* buffer, int size) {
long int usedMem = this->pl->usedMem;
long int buffersMem = this->pl->buffersMem;
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);
Platform_setMemoryValues(this);
snprintf(buffer, size, "%ld/%ldMB", (long int) this->values[0] / 1024, (long int) this->total / 1024);
}
static void MemoryMeter_display(Object* cast, RichString* out) {
......
......@@ -21,11 +21,12 @@ in the source distribution for its full text.
#include <assert.h>
#include <sys/time.h>
#define METER_BUFFER_LEN 128
#define METER_BUFFER_LEN 256
/*{
#include "ListItem.h"
#include "ProcessList.h"
#include <sys/time.h>
typedef struct Meter_ Meter;
......@@ -77,7 +78,7 @@ struct Meter_ {
int param;
void* drawData;
int h;
ProcessList* pl;
struct ProcessList_* pl;
double* values;
double total;
};
......@@ -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));
Object_setClass(this, type);
this->h = 1;
......@@ -302,19 +303,23 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
/* ---------- 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] = {
GRAPH_1, GRAPH_1, GRAPH_1,
GRAPH_2, GRAPH_2, GRAPH_2,
GRAPH_3, GRAPH_3, GRAPH_3,
GRAPH_4, GRAPH_4, GRAPH_4,
GRAPH_5, GRAPH_5, GRAPH_6,
GRAPH_7, GRAPH_7, GRAPH_7,
GRAPH_8, GRAPH_8, GRAPH_9
static const char* GraphMeterMode_dotsAscii[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 const char* GraphMeterMode_characters = "^`'-.,_~'`-.,_~'`-.,_";
static const char* (*GraphMeterMode_dots)[5];
static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
......@@ -322,6 +327,18 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
GraphData* data = (GraphData*) this->drawData;
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;
gettimeofday(&now, NULL);
if (!timercmp(&now, &(data->time), <)) {
......@@ -342,17 +359,25 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
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, ' ' );
double threshold = 1.00;
for (int j = 0; j < 21; j++, threshold -= 0.05)
if (value >= threshold) {
DrawDot(CRT_colors[GraphMeterMode_colors[j]], y+(j/7.0), GraphMeterMode_characters[j]);
break;
for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) {
const double dot = (1.0 / 16);
int v1 = data->values[i] / dot;
int v2 = data->values[i+1] / dot;
if (v1 == 0) v1 = 1;
if (v2 == 0) v2 = 1;
int level = 12;
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]);
......@@ -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) {
if (CRT_utf8) {
for (int i = 0; i < 3; i++)
mvaddstr(y+i, x, LEDMeterMode_digitsUtf8[i][n]);
} else {
for (int i = 0; i < 3; i++)
mvaddstr(y+i, x, LEDMeterMode_digitsAscii[i][n]);
}
mvaddstr(y+i, x, LEDMeterMode_digits[i][n]);
}
static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
(void) w;
if (CRT_utf8) {
LEDMeterMode_digits = LEDMeterMode_digitsUtf8;
} else {
LEDMeterMode_digits = LEDMeterMode_digitsAscii;
}
char buffer[METER_BUFFER_LEN];
Meter_setValues(this, buffer, METER_BUFFER_LEN - 1);
......@@ -423,7 +452,7 @@ static MeterMode TextMeterMode = {
static MeterMode GraphMeterMode = {
.uiName = "Graph",
.h = 3,
.h = 4,
.draw = GraphMeterMode_draw,
};
......
......@@ -9,10 +9,11 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#define METER_BUFFER_LEN 128
#define METER_BUFFER_LEN 256
#include "ListItem.h"
#include "ProcessList.h"
#include <sys/time.h>
typedef struct Meter_ Meter;
......@@ -64,7 +65,7 @@ struct Meter_ {
int param;
void* drawData;
int h;
ProcessList* pl;
struct ProcessList_* pl;
double* values;
double total;
};
......@@ -99,7 +100,7 @@ typedef struct GraphData_ {
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);
......@@ -115,8 +116,6 @@ ListItem* Meter_toListItem(Meter* this);
/* ---------- GraphMeterMode ---------- */
#define DrawDot(a,y,c) do { attrset(a); mvaddch(y, x+k, c); } while(0)
/* ---------- LEDMeterMode ---------- */
extern MeterMode* Meter_modes[];
......
......@@ -21,6 +21,7 @@ typedef struct MetersPanel_ {
Settings* settings;
Vector* meters;
ScreenManager* scr;
bool moving;
} MetersPanel;
}*/
......@@ -42,6 +43,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
case 0x0a:
case 0x0d:
case KEY_ENTER:
{
this->moving = !(this->moving);
((ListItem*)Panel_getSelected(super))->moving = this->moving;
result = HANDLED;
break;
}
case ' ':
case KEY_F(4):
case 't':
{
......@@ -53,6 +61,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
result = HANDLED;
break;
}
case KEY_UP:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
case KEY_F(7):
case '[':
case '-':
......@@ -62,6 +77,13 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
result = HANDLED;
break;
}
case KEY_DOWN:
{
if (!this->moving) {
break;
}
/* else fallthrough */
}
case KEY_F(8):
case ']':
case '+':
......@@ -83,7 +105,7 @@ static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) {
}
}
if (result == HANDLED) {
Header* header = this->settings->header;
Header* header = (Header*) this->scr->header;
this->settings->changed = true;
Header_calculateHeight(header);
Header_draw(header);
......@@ -108,6 +130,7 @@ MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* met
this->settings = settings;
this->meters = meters;
this->scr = scr;
this->moving = false;
Panel_setHeader(super, header);
for (int i = 0; i < Vector_size(meters); i++) {
Meter* meter = (Meter*) Vector_get(meters, i);
......
......@@ -19,6 +19,7 @@ typedef struct MetersPanel_ {
Settings* settings;
Vector* meters;
ScreenManager* scr;
bool moving;
} MetersPanel;
......
......@@ -28,9 +28,11 @@ in the source distribution for its full text.
typedef struct Panel_ Panel;
typedef enum HandlerResult_ {
HANDLED,
IGNORED,
BREAK_LOOP
HANDLED = 0x00,
IGNORED = 0x01,
BREAK_LOOP = 0x02,
REFRESH = 0x04,
RECALCULATE = 0x08,
} HandlerResult;
#define EVENT_SETSELECTED -1
......@@ -54,7 +56,7 @@ struct Panel_ {
Vector* items;
int selected;
int oldSelected;
char* eventHandlerBuffer;
void* eventHandlerState;
int scrollV;
short scrollH;
bool needsRedraw;
......@@ -102,7 +104,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, ObjectClass* type, bool
this->y = y;
this->w = w;
this->h = h;
this->eventHandlerBuffer = NULL;
this->eventHandlerState = NULL;
this->items = Vector_new(type, owner, DEFAULT_SIZE);
this->scrollV = 0;
this->scrollH = 0;
......@@ -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) {
assert (this != NULL);
free(this->eventHandlerBuffer);
free(this->eventHandlerState);
Vector_delete(this->items);
RichString_end(this->header);
}
......@@ -246,30 +248,11 @@ void Panel_setSelected(Panel* this, int selected) {
void Panel_draw(Panel* this, bool focus) {
assert (this != NULL);
int itemCount = Vector_size(this->items);
int size = Vector_size(this->items);
int scrollH = this->scrollH;
int y = this->y; int x = this->x;
int first = this->scrollV;
if (itemCount > this->h && first > itemCount - 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 y = this->y;
int x = this->x;
int h = this->h;
int headerLen = RichString_sizeVal(this->header);
if (headerLen > 0) {
......@@ -286,13 +269,33 @@ void Panel_draw(Panel* this, bool focus) {
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
? CRT_colors[PANEL_HIGHLIGHT_FOCUS]
: CRT_colors[PANEL_HIGHLIGHT_UNFOCUS];
if (this->needsRedraw) {
for(int i = first, j = 0; j < this->h && i < last; i++, j++) {
int line = 0;
for(int i = first; line < h && i < upTo; i++) {
Object* itemObj = Vector_get(this->items, i);
assert(itemObj); if(!itemObj) continue;
RichString_begin(item);
......@@ -304,15 +307,18 @@ void Panel_draw(Panel* this, bool focus) {
attrset(highlight);
RichString_setAttr(&item, highlight);
}
mvhline(y + j, x, ' ', this->w);
mvhline(y + line, x, ' ', this->w);
if (amt > 0)
RichString_printoffnVal(item, y+j, x, scrollH, amt);
RichString_printoffnVal(item, y + line, x, scrollH, amt);
if (selected)
attrset(CRT_colors[RESET_COLOR]);
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;
} else {
......@@ -325,15 +331,15 @@ void Panel_draw(Panel* this, bool focus) {
RichString_begin(new);
Object_display(newObj, &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)
RichString_printoffnVal(old, y+this->oldSelected - this->scrollV, x,
RichString_printoffnVal(old, y+this->oldSelected - first, x,
scrollH, MIN(oldLen - scrollH, this->w));
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);
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));
attrset(CRT_colors[RESET_COLOR]);
RichString_end(new);
......@@ -345,38 +351,26 @@ void Panel_draw(Panel* this, bool focus) {
bool Panel_onKey(Panel* this, int key) {
assert (this != NULL);
int size = Vector_size(this->items);
switch (key) {
case KEY_DOWN:
case KEY_CTRLN:
if (this->selected + 1 < Vector_size(this->items))
this->selected++;
return true;
break;
case KEY_UP:
case KEY_CTRLP:
if (this->selected > 0)
this->selected--;
return true;
break;
#ifdef KEY_C_DOWN
case KEY_C_DOWN:
if (this->selected + 1 < Vector_size(this->items)) {
this->selected++;
if (this->scrollV < Vector_size(this->items) - this->h) {
this->scrollV++;
this->needsRedraw = true;
}
}
return true;
break;
#endif
#ifdef KEY_C_UP
case KEY_C_UP:
if (this->selected > 0) {
this->selected--;
if (this->scrollV > 0) {
this->scrollV--;
this->needsRedraw = true;
}
}
return true;
break;
#endif
case KEY_LEFT:
case KEY_CTRLB:
......@@ -384,71 +378,74 @@ bool Panel_onKey(Panel* this, int key) {
this->scrollH -= CRT_scrollHAmount;
this->needsRedraw = true;
}
return true;
break;
case KEY_RIGHT:
case KEY_CTRLF:
this->scrollH += CRT_scrollHAmount;
this->needsRedraw = true;
return true;
break;
case KEY_PPAGE:
this->selected -= (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;
return true;
break;
case KEY_NPAGE:
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);
if (this->scrollV >= MAX(0, size - this->h))
this->scrollV = MAX(0, size - this->h - 1);
this->needsRedraw = true;
return true;
break;
case KEY_HOME:
this->selected = 0;
return true;
break;
case KEY_END:
this->selected = Vector_size(this->items) - 1;
return true;
}
this->selected = size - 1;
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 true;
}
HandlerResult Panel_selectByTyping(Panel* this, int ch) {
int size = Panel_size(this);
if (!this->eventHandlerBuffer)
this->eventHandlerBuffer = calloc(100, 1);
if (!this->eventHandlerState)
this->eventHandlerState = calloc(100, 1);
char* buffer = this->eventHandlerState;
if (isalnum(ch)) {
int len = strlen(this->eventHandlerBuffer);
int len = strlen(buffer);
if (len < 99) {
this->eventHandlerBuffer[len] = ch;
this->eventHandlerBuffer[len+1] = '\0';
buffer[len] = ch;
buffer[len+1] = '\0';
}
for (int try = 0; try < 2; try++) {
len = strlen(this->eventHandlerBuffer);
len = strlen(buffer);
for (int i = 0; i < size; i++) {
char* cur = ((ListItem*) Panel_get(this, i))->value;
while (*cur == ' ') cur++;
if (strncasecmp(cur, this->eventHandlerBuffer, len) == 0) {
if (strncasecmp(cur, buffer, len) == 0) {
Panel_setSelected(this, i);
return HANDLED;
}
}
this->eventHandlerBuffer[0] = ch;
this->eventHandlerBuffer[1] = '\0';
// if current word did not match,
// retry considering the character the start of a new word.
buffer[0] = ch;
buffer[1] = '\0';
}
return HANDLED;
} else if (ch != ERR) {
this->eventHandlerBuffer[0] = '\0';
buffer[0] = '\0';
}
if (ch == 13) {
return BREAK_LOOP;
......
......@@ -17,9 +17,11 @@ in the source distribution for its full text.
typedef struct Panel_ Panel;
typedef enum HandlerResult_ {
HANDLED,
IGNORED,
BREAK_LOOP
HANDLED = 0x00,
IGNORED = 0x01,
BREAK_LOOP = 0x02,
REFRESH = 0x04,
RECALCULATE = 0x08,
} HandlerResult;
#define EVENT_SETSELECTED -1
......@@ -43,7 +45,7 @@ struct Panel_ {
Vector* items;
int selected;
int oldSelected;
char* eventHandlerBuffer;
void* eventHandlerState;
int scrollV;
short scrollH;
bool needsRedraw;
......
......@@ -7,7 +7,7 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessList.h"
#include "Settings.h"
#include "CRT.h"
#include "String.h"
#include "RichString.h"
......@@ -24,14 +24,9 @@ in the source distribution for its full text.
#include <string.h>
#include <stdbool.h>
#include <pwd.h>
#include <sched.h>
#include <time.h>
#include <assert.h>
#ifdef HAVE_LIBHWLOC
#include <hwloc/linux.h>
#endif
// On Linux, this works only with glibc 2.1+. On earlier versions
// the behavior is similar to have a hardcoded page size.
#ifndef PAGE_SIZE
......@@ -41,7 +36,6 @@ in the source distribution for its full text.
/*{
#include "Object.h"
#include "Affinity.h"
#include <sys/types.h>
......@@ -88,12 +82,10 @@ typedef enum ProcessField_ {
LAST_PROCESSFIELD
} ProcessField;
struct ProcessList_;
typedef struct Process_ {
Object super;
struct ProcessList_ *pl;
struct Settings_* settings;
pid_t pid;
char* comm;
......@@ -133,10 +125,10 @@ typedef struct Process_ {
unsigned long long io_read_bytes;
unsigned long long io_write_bytes;
unsigned long long io_cancelled_write_bytes;
double io_rate_read_bps;
unsigned long long io_rate_read_time;
double io_rate_write_bps;
unsigned long long io_rate_write_time;
double io_rate_read_bps;
double io_rate_write_bps;
#endif
int processor;
......@@ -192,92 +184,99 @@ typedef struct Process_ {
} Process;
}*/
typedef struct ProcessFieldData_ {
const char* name;
const char* title;
const char* description;
int flags;
} ProcessFieldData;
const char *Process_fieldNames[] = {
"", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION",
"TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT",
"UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE",
"STARTTIME", "VSIZE", "RSS", "RLIM", "STARTCODE", "ENDCODE", "STARTSTACK",
"KSTKESP", "KSTKEIP", "SIGNAL", "BLOCKED", "SIGIGNORE", "SIGCATCH", "WCHAN",
"NSWAP", "CNSWAP", "EXIT_SIGNAL", "PROCESSOR", "M_SIZE", "M_RESIDENT", "M_SHARE",
"M_TRS", "M_DRS", "M_LRS", "M_DT", "ST_UID", "PERCENT_CPU", "PERCENT_MEM",
"USER", "TIME", "NLWP", "TGID",
#ifdef HAVE_OPENVZ
"CTID", "VPID",
#endif
#ifdef HAVE_VSERVER
"VXID",
#endif
#ifdef HAVE_TASKSTATS
"RCHAR", "WCHAR", "SYSCR", "SYSCW", "RBYTES", "WBYTES", "CNCLWB",
"IO_READ_RATE", "IO_WRITE_RATE", "IO_RATE",
#endif
#ifdef HAVE_CGROUP
"CGROUP",
#endif
#ifdef HAVE_OOM
"OOM",
#endif
"IO_PRIORITY",
"*** report bug! ***"
};
void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
const int Process_fieldFlags[] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
#ifdef HAVE_OPENVZ
PROCESS_FLAG_OPENVZ, PROCESS_FLAG_OPENVZ,
#endif
#ifdef HAVE_VSERVER
PROCESS_FLAG_VSERVER,
#endif
#ifdef HAVE_TASKSTATS
PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO,
PROCESS_FLAG_IO, PROCESS_FLAG_IO, PROCESS_FLAG_IO,
#endif
#ifdef HAVE_CGROUP
PROCESS_FLAG_CGROUP,
#endif
#ifdef HAVE_OOM
0,
#endif
PROCESS_FLAG_IOPRIO
};
}*/
const char *Process_fieldTitles[] = {
"", " PID ", "Command ", "S ", " PPID ", " PGRP ", " SESN ",
" TTY ", " TPGID ", "- ", " MINFLT ", " CMINFLT ", " MAJFLT ", " CMAJFLT ",
" UTIME+ ", " STIME+ ", " CUTIME+ ", " CSTIME+ ", "PRI ", " NI ", "- ",
"START ", "- ", "- ", "- ", "- ", "- ", "- ",
"- ", "- ", "- ", "- ", "- ", "- ", "- ",
"- ", "- ", "- ", "CPU ", " VIRT ", " RES ", " SHR ",
" CODE ", " DATA ", " LIB ", " DIRTY ", " UID ", "CPU% ", "MEM% ",
"USER ", " TIME+ ", "NLWP ", " TGID ",
ProcessFieldData Process_fields[] = {
{ .name = "", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, },
{ .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
{ .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, },
{ .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, },
{ .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, },
{ .name = "SESSION", .title = " SESN ", .description = "Process's session ID", .flags = 0, },
{ .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, },
{ .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, },
{ .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, },
{ .name = "CMINFLT", .title = " CMINFLT ", .description = "Children processes' minor faults", .flags = 0, },
{ .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, },
{ .name = "CMAJFLT", .title = " CMAJFLT ", .description = "Children processes' major faults", .flags = 0, },
{ .name = "UTIME", .title = " UTIME+ ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, },
{ .name = "STIME", .title = " STIME+ ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, },
{ .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, },
{ .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, },
{ .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
{ .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
{ .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
{ .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "RSS", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
{ .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
{ .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, },
{ .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, },
{ .name = "M_SHARE", .title = " SHR ", .description = "Size of the process's shared pages", .flags = 0, },
{ .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, },
{ .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, },
{ .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, },
{ .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, },
{ .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, },
{ .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, },
{ .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, },
{ .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
{ .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, },
{ .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
{ .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, },
#ifdef HAVE_OPENVZ
" CTID ", " VPID ",
{ .name = "CTID", .title = " CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_OPENVZ, },
{ .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_OPENVZ, },
#endif
#ifdef HAVE_VSERVER
" VXID ",
{ .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_VSERVER, },
#endif
#ifdef HAVE_TASKSTATS
" RD_CHAR ", " WR_CHAR ", " RD_SYSC ", " WR_SYSC ", " IO_RBYTES ", " IO_WBYTES ", " IO_CANCEL ",
" IORR ", " IOWR ", " IORW ",
{ .name = "RCHAR", .title = " RD_CHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, },
{ .name = "WCHAR", .title = " WR_CHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, },
{ .name = "SYSCR", .title = " RD_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "SYSCW", .title = " WR_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "RBYTES", .title = " IO_RBYTES ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "WBYTES", .title = " IO_WBYTES ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "CNCLWB", .title = " IO_CANCEL ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, },
{ .name = "IO_READ_RATE", .title = " DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, },
{ .name = "IO_RATE", .title = " DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, },
#endif
#ifdef HAVE_CGROUP
" CGROUP ",
{ .name = "CGROUP", .title = " CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_CGROUP, },
#endif
#ifdef HAVE_OOM
" OOM ",
{ .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = 0, },
#endif
"IO ",
"*** report bug! ***"
{ .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_IOPRIO, },
{ .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
};
static int Process_getuid = -1;
......@@ -289,32 +288,32 @@ void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
if (maxPid > 99999) {
Process_fieldTitles[PID] = " PID ";
Process_fieldTitles[PPID] = " PPID ";
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
#ifdef HAVE_OPENVZ
Process_fieldTitles[VPID] = " VPID ";
Process_fields[VPID].title = " VPID ";
#endif
Process_fieldTitles[TPGID] = " TPGID ";
Process_fieldTitles[TGID] = " TGID ";
Process_fieldTitles[PGRP] = " PGRP ";
Process_fieldTitles[SESSION] = " SESN ";
Process_fields[TPGID].title = " TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
#ifdef HAVE_OOM
Process_fieldTitles[OOM] = " OOM ";
Process_fields[OOM].title = " OOM ";
#endif
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fieldTitles[PID] = " PID ";
Process_fieldTitles[PPID] = " PPID ";
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
#ifdef HAVE_OPENVZ
Process_fieldTitles[VPID] = " VPID ";
Process_fields[VPID].title = " VPID ";
#endif
Process_fieldTitles[TPGID] = "TPGID ";
Process_fieldTitles[TGID] = " TGID ";
Process_fieldTitles[PGRP] = " PGRP ";
Process_fieldTitles[SESSION] = " SESN ";
Process_fields[TPGID].title = "TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
#ifdef HAVE_OOM
Process_fieldTitles[OOM] = " OOM ";
Process_fields[OOM].title = " OOM ";
#endif
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
......@@ -436,7 +435,7 @@ static void Process_printTime(RichString* str, unsigned long long t) {
static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) {
int start = RichString_size(str);
RichString_append(str, attr, this->comm);
if (this->pl->highlightBaseName) {
if (this->settings->highlightBaseName) {
int finish = RichString_size(str) - 1;
if (this->basenameOffset != -1)
finish = (start + this->basenameOffset) - 1;
......@@ -455,27 +454,35 @@ static inline void Process_writeCommand(Process* this, int attr, int baseattr, R
}
}
static inline void Process_outputRate(RichString* str, int attr, char* buffer, int n, double rate, int coloring) {
rate = rate / 1024;
if (rate < 0.01)
snprintf(buffer, n, " 0 ");
else if (rate <= 10)
snprintf(buffer, n, "%5.2f ", rate);
else if (rate <= 100)
snprintf(buffer, n, "%5.1f ", rate);
else {
Process_humanNumber(str, rate, coloring);
return;
static inline void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring) {
int largeNumberColor = CRT_colors[LARGE_NUMBER];
int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
int processColor = CRT_colors[PROCESS];
if (!coloring) {
largeNumberColor = CRT_colors[PROCESS];
processMegabytesColor = CRT_colors[PROCESS];
}
if (rate < ONE_K) {
int len = snprintf(buffer, n, "%7.2f B/s ", rate);
RichString_appendn(str, processColor, buffer, len);
} else if (rate < ONE_K * ONE_K) {
int len = snprintf(buffer, n, "%7.2f K/s ", rate / ONE_K);
RichString_appendn(str, processColor, buffer, len);
} else if (rate < ONE_K * ONE_K * ONE_K) {
int len = snprintf(buffer, n, "%7.2f M/s ", rate / ONE_K / ONE_K);
RichString_appendn(str, processMegabytesColor, buffer, len);
} else {
int len = snprintf(buffer, n, "%7.2f G/s ", rate / ONE_K / ONE_K / ONE_K);
RichString_appendn(str, largeNumberColor, buffer, len);
}
RichString_append(str, attr, buffer);
}
static void Process_writeField(Process* this, RichString* str, ProcessField field) {
void Process_writeDefaultField(Process* this, RichString* str, ProcessField field) {
char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
int baseattr = CRT_colors[PROCESS_BASENAME];
int n = sizeof(buffer) - 1;
bool coloring = this->pl->highlightMegabytes;
bool coloring = this->settings->highlightMegabytes;
switch (field) {
case PID: snprintf(buffer, n, Process_pidFormat, this->pid); break;
......@@ -489,24 +496,21 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel
case CMINFLT: Process_colorNumber(str, this->cminflt, coloring); return;
case MAJFLT: Process_colorNumber(str, this->majflt, coloring); return;
case CMAJFLT: Process_colorNumber(str, this->cmajflt, coloring); return;
case PROCESSOR: snprintf(buffer, n, "%3d ", ProcessList_cpuId(this->pl, this->processor)); break;
case PROCESSOR: snprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break;
case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break;
case COMM: {
if (this->pl->highlightThreads && Process_isThread(this)) {
if (this->settings->highlightThreads && Process_isThread(this)) {
attr = CRT_colors[PROCESS_THREAD];
baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
}
if (!this->pl->treeView || this->indent == 0) {
if (!this->settings->treeView || this->indent == 0) {
Process_writeCommand(this, attr, baseattr, str);
return;
} else {
char* buf = buffer;
int maxIndent = 0;
const char **treeStr = this->pl->treeStr;
bool lastItem = (this->indent < 0);
int indent = (this->indent < 0 ? -this->indent : this->indent);
if (treeStr == NULL)
treeStr = ProcessList_treeStrAscii;
for (int i = 0; i < 32; i++)
if (indent & (1 << i))
......@@ -514,14 +518,14 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel
for (int i = 0; i < maxIndent - 1; i++) {
int written;
if (indent & (1 << i))
written = snprintf(buf, n, "%s ", treeStr[TREE_STR_VERT]);
written = snprintf(buf, n, "%s ", CRT_treeStr[TREE_STR_VERT]);
else
written = snprintf(buf, n, " ");
buf += written;
n -= written;
}
const char* draw = treeStr[lastItem ? (this->pl->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE];
snprintf(buf, n, "%s%s ", draw, this->showChildren ? treeStr[TREE_STR_SHUT] : treeStr[TREE_STR_OPEN] );
const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE];
snprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] );
RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
Process_writeCommand(this, attr, baseattr, str);
return;
......@@ -614,9 +618,9 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel
case RBYTES: Process_colorNumber(str, this->io_read_bytes, coloring); return;
case WBYTES: Process_colorNumber(str, this->io_write_bytes, coloring); return;
case CNCLWB: Process_colorNumber(str, this->io_cancelled_write_bytes, coloring); return;
case IO_READ_RATE: Process_outputRate(str, attr, buffer, n, this->io_rate_read_bps, coloring); return;
case IO_WRITE_RATE: Process_outputRate(str, attr, buffer, n, this->io_rate_write_bps, coloring); return;
case IO_RATE: Process_outputRate(str, attr, buffer, n, this->io_rate_read_bps + this->io_rate_write_bps, coloring); return;
case IO_READ_RATE: Process_outputRate(str, buffer, n, this->io_rate_read_bps, coloring); return;
case IO_WRITE_RATE: Process_outputRate(str, buffer, n, this->io_rate_write_bps, coloring); return;
case IO_RATE: Process_outputRate(str, buffer, n, this->io_rate_read_bps + this->io_rate_write_bps, coloring); return;
#endif
#ifdef HAVE_CGROUP
case CGROUP: snprintf(buffer, n, "%-10s ", this->cgroup); break;
......@@ -632,11 +636,11 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel
static void Process_display(Object* cast, RichString* out) {
Process* this = (Process*) cast;
ProcessField* fields = this->pl->fields;
ProcessField* fields = this->settings->fields;
RichString_prune(out);
for (int i = 0; fields[i]; i++)
Process_writeField(this, out, fields[i]);
if (this->pl->shadowOtherUsers && (int)this->st_uid != Process_getuid)
if (this->settings->shadowOtherUsers && (int)this->st_uid != Process_getuid)
RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
if (this->tag == true)
RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
......@@ -660,11 +664,11 @@ ObjectClass Process_class = {
.compare = Process_compare
};
Process* Process_new(struct ProcessList_ *pl) {
Process* Process_new(struct Settings_* settings) {
Process* this = calloc(1, sizeof(Process));
Object_setClass(this, Class(Process));
this->pid = 0;
this->pl = pl;
this->settings = settings;
this->tag = false;
this->showChildren = true;
this->show = true;
......@@ -698,65 +702,6 @@ bool Process_changePriorityBy(Process* this, size_t delta) {
return Process_setPriority(this, this->nice + delta);
}
#ifdef HAVE_LIBHWLOC
Affinity* Process_getAffinity(Process* this) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
bool ok = (hwloc_linux_get_tid_cpubind(this->pl->topology, this->pid, cpuset) == 0);
Affinity* affinity = NULL;
if (ok) {
affinity = Affinity_new();
if (hwloc_bitmap_last(cpuset) == -1) {
for (int i = 0; i < this->pl->cpuCount; i++) {
Affinity_add(affinity, i);
}
} else {
unsigned int id;
hwloc_bitmap_foreach_begin(id, cpuset);
Affinity_add(affinity, id);
hwloc_bitmap_foreach_end();
}
}
hwloc_bitmap_free(cpuset);
return affinity;
}
bool Process_setAffinity(Process* this, Affinity* affinity) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
for (int i = 0; i < affinity->used; i++) {
hwloc_bitmap_set(cpuset, affinity->cpus[i]);
}
bool ok = (hwloc_linux_set_tid_cpubind(this->pl->topology, this->pid, cpuset) == 0);
hwloc_bitmap_free(cpuset);
return ok;
}
#elif HAVE_NATIVE_AFFINITY
Affinity* Process_getAffinity(Process* this) {
cpu_set_t cpuset;
bool ok = (sched_getaffinity(this->pid, sizeof(cpu_set_t), &cpuset) == 0);
if (!ok) return NULL;
Affinity* affinity = Affinity_new();
for (int i = 0; i < this->pl->cpuCount; i++) {
if (CPU_ISSET(i, &cpuset))
Affinity_add(affinity, i);
}
return affinity;
}
bool Process_setAffinity(Process* this, Affinity* affinity) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (int i = 0; i < affinity->used; i++) {
CPU_SET(affinity->cpus[i], &cpuset);
}
bool ok = (sched_setaffinity(this->pid, sizeof(unsigned long), &cpuset) == 0);
return ok;
}
#endif
void Process_sendSignal(Process* this, size_t sgn) {
kill(this->pid, (int) sgn);
}
......@@ -767,10 +712,10 @@ long Process_pidCompare(const void* v1, const void* v2) {
return (p1->pid - p2->pid);
}
long Process_compare(const void* v1, const void* v2) {
long Process_defaultCompare(const void* v1, const void* v2) {
Process *p1, *p2;
ProcessList *pl = ((Process*)v1)->pl;
if (pl->direction == 1) {
Settings *settings = ((Process*)v1)->settings;
if (settings->direction == 1) {
p1 = (Process*)v1;
p2 = (Process*)v2;
} else {
......@@ -778,7 +723,7 @@ long Process_compare(const void* v1, const void* v2) {
p1 = (Process*)v2;
}
long long diff;
switch (pl->sortKey) {
switch (settings->sortKey) {
case PID:
return (p1->pid - p2->pid);
case PPID:
......
......@@ -9,9 +9,6 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#ifdef HAVE_LIBHWLOC
#endif
// On Linux, this works only with glibc 2.1+. On earlier versions
// the behavior is similar to have a hardcoded page size.
#ifndef PAGE_SIZE
......@@ -20,7 +17,6 @@ in the source distribution for its full text.
#define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K )
#include "Object.h"
#include "Affinity.h"
#include <sys/types.h>
......@@ -67,12 +63,10 @@ typedef enum ProcessField_ {
LAST_PROCESSFIELD
} ProcessField;
struct ProcessList_;
typedef struct Process_ {
Object super;
struct ProcessList_ *pl;
struct Settings_* settings;
pid_t pid;
char* comm;
......@@ -112,10 +106,10 @@ typedef struct Process_ {
unsigned long long io_read_bytes;
unsigned long long io_write_bytes;
unsigned long long io_cancelled_write_bytes;
double io_rate_read_bps;
unsigned long long io_rate_read_time;
double io_rate_write_bps;
unsigned long long io_rate_write_time;
double io_rate_read_bps;
double io_rate_write_bps;
#endif
int processor;
......@@ -171,12 +165,18 @@ typedef struct 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();
......@@ -189,11 +189,13 @@ void Process_setupColumnWidths();
#define ONE_DECIMAL_M (ONE_DECIMAL_K * 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);
extern ObjectClass Process_class;
Process* Process_new(struct ProcessList_ *pl);
Process* Process_new(struct Settings_* settings);
void Process_toggleTag(Process* this);
......@@ -201,24 +203,10 @@ bool Process_setPriority(Process* this, int priority);
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);
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
......@@ -19,6 +19,7 @@ in the source distribution for its full text.
#include "UsersTable.h"
#include "Panel.h"
#include "Process.h"
#include "Settings.h"
#ifndef MAX_NAME
#define MAX_NAME 128
......@@ -28,51 +29,9 @@ in the source distribution for its full text.
#define MAX_READ 2048
#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_ {
const char **treeStr;
Settings* settings;
Vector* processes;
Vector* processes2;
Hashtable* processTable;
......@@ -84,90 +43,33 @@ typedef struct ProcessList_ {
const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
#ifdef HAVE_LIBHWLOC
hwloc_topology_t topology;
bool topologyOk;
#endif
CPUData* cpus;
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;
int cpuCount;
} 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_scan(ProcessList* pl);
}*/
static ProcessField defaultHeaders[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
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) {
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
this->processes = Vector_new(Class(Process), true, DEFAULT_SIZE);
this->processTable = Hashtable_new(140, false);
this->usersTable = usersTable;
this->pidWhiteList = pidWhiteList;
this->userId = userId;
// tree-view auxiliary buffers
this->processes2 = Vector_new(Class(Process), true, DEFAULT_SIZE);
// set later by platform-specific code
this->cpuCount = 0;
this->cpus = NULL;
#ifdef HAVE_LIBHWLOC
this->topologyOk = false;
......@@ -180,35 +82,8 @@ ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtab
}
#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;
if (CRT_utf8)
this->treeStr = CRT_utf8 ? ProcessList_treeStrUtf8 : ProcessList_treeStrAscii;
return this;
}
......@@ -216,27 +91,19 @@ void ProcessList_done(ProcessList* this) {
Hashtable_delete(this->processTable);
Vector_delete(this->processes);
Vector_delete(this->processes2);
free(this->cpus);
free(this->fields);
}
void ProcessList_setPanel(ProcessList* 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) {
RichString_prune(header);
ProcessField* fields = this->fields;
ProcessField* fields = this->settings->fields;
for (int i = 0; fields[i]; i++) {
const char* field = Process_fieldTitles[fields[i]];
if (!this->treeView && this->sortKey == fields[i])
const char* field = Process_fields[fields[i]].title;
if (!field) field = "- ";
if (!this->settings->treeView && this->settings->sortKey == fields[i])
RichString_append(header, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field);
else
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
}
void ProcessList_sort(ProcessList* this) {
if (!this->treeView) {
if (!this->settings->treeView) {
Vector_insertionSort(this->processes);
} else {
// Save settings
int direction = this->direction;
int sortKey = this->sortKey;
int direction = this->settings->direction;
int sortKey = this->settings->sortKey;
// Sort by PID
this->sortKey = PID;
this->direction = 1;
this->settings->sortKey = PID;
this->settings->direction = 1;
Vector_quickSort(this->processes);
// Restore settings
this->sortKey = sortKey;
this->direction = direction;
this->settings->sortKey = sortKey;
this->settings->direction = direction;
// Take PID 1 as root and add to the new listing
int vsize = Vector_size(this->processes);
Process* init = (Process*) (Vector_take(this->processes, 0));
......@@ -351,10 +218,12 @@ void ProcessList_sort(ProcessList* this) {
ProcessField ProcessList_keyAt(ProcessList* this, int at) {
int x = 0;
ProcessField* fields = this->fields;
ProcessField* fields = this->settings->fields;
ProcessField field;
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) {
return field;
}
......@@ -371,17 +240,12 @@ void ProcessList_expandTree(ProcessList* this) {
}
}
void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, const char* incFilter) {
if (!flags) {
following = this->following;
incFilter = this->incFilter;
} else {
this->following = following;
this->incFilter = incFilter;
}
void ProcessList_rebuildPanel(ProcessList* this) {
int following = this->following;
const char* incFilter = this->incFilter;
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;
Panel_prune(this->panel);
......@@ -392,14 +256,14 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, cons
Process* p = ProcessList_get(this, i);
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)))
|| (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
hidden = true;
if (!hidden) {
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);
this->panel->scrollV = currScrollV;
}
......
......@@ -14,6 +14,7 @@ in the source distribution for its full text.
#include "UsersTable.h"
#include "Panel.h"
#include "Process.h"
#include "Settings.h"
#ifndef MAX_NAME
#define MAX_NAME 128
......@@ -23,51 +24,9 @@ in the source distribution for its full text.
#define MAX_READ 2048
#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_ {
const char **treeStr;
Settings* settings;
Vector* processes;
Vector* processes2;
Hashtable* processTable;
......@@ -79,67 +38,26 @@ typedef struct ProcessList_ {
const char* incFilter;
Hashtable* pidWhiteList;
int cpuCount;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
#ifdef HAVE_LIBHWLOC
hwloc_topology_t topology;
bool topologyOk;
#endif
CPUData* cpus;
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;
int cpuCount;
} 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_scan(ProcessList* pl);
extern const char *ProcessList_treeStrAscii[TREE_STR_COUNT];
extern const char *ProcessList_treeStrUtf8[TREE_STR_COUNT];
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList);
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
void ProcessList_done(ProcessList* this);
void ProcessList_setPanel(ProcessList* this, Panel* panel);
void ProcessList_invertSortOrder(ProcessList* this);
void ProcessList_printHeader(ProcessList* this, RichString* header);
void ProcessList_add(ProcessList* this, Process* p);
......@@ -156,7 +74,7 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at);
void ProcessList_expandTree(ProcessList* this);
void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, const char* incFilter);
void ProcessList_rebuildPanel(ProcessList* this);
#endif
......@@ -6,8 +6,8 @@ in the source distribution for its full text.
*/
#include "ScreenManager.h"
#include "ProcessList.h"
#include "Panel.h"
#include "Object.h"
#include <assert.h>
......@@ -19,6 +19,8 @@ in the source distribution for its full text.
#include "FunctionBar.h"
#include "Vector.h"
#include "Header.h"
#include "Settings.h"
#include "Panel.h"
typedef enum Orientation_ {
VERTICAL,
......@@ -36,14 +38,14 @@ typedef struct ScreenManager_ {
int panelCount;
const FunctionBar* fuBar;
const Header* header;
time_t lastScan;
const Settings* settings;
bool owner;
bool allowFocusChange;
} 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;
this = malloc(sizeof(ScreenManager));
this->x1 = x1;
......@@ -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->panelCount = 0;
this->header = header;
this->settings = settings;
this->owner = owner;
this->allowFocusChange = true;
return this;
......@@ -131,21 +134,49 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL);
this->lastScan = 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;
int ch = 0;
while (!quit) {
int panels = this->panelCount;
if (this->header) {
time_t now = time(NULL);
if (now > this->lastScan) {
gettimeofday(&tv, NULL);
double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
timeToRecalculate = (newTime - oldTime > this->settings->delay);
if (newTime < oldTime) timeToRecalculate = true; // clock was adjusted?
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);
this->lastScan = now;
sortTimeout = 1;
}
Header_draw(this->header);
ProcessList_rebuildPanel(this->header->pl, false, false, NULL);
//this->header->pl->incFilter = IncSet_filter(inc);
ProcessList_rebuildPanel(this->header->pl);
drawPanel = true;
}
doRefresh = true;
}
if (drawPanel) {
for (int i = 0; i < panels; i++) {
Panel* panel = (Panel*) Vector_get(this->panels, i);
Panel_draw(panel, i == focus);
......@@ -155,12 +186,15 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
}
}
}
}
FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus);
if (bar)
this->fuBar = bar;
if (this->fuBar)
FunctionBar_draw(this->fuBar, NULL);
int prevCh = ch;
ch = getch();
if (ch == KEY_MOUSE) {
......@@ -187,17 +221,36 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
if (Panel_eventHandlerFn(panelFocus)) {
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;
} else if (result == BREAK_LOOP) {
} else if (result & BREAK_LOOP) {
quit = true;
continue;
}
}
switch (ch) {
case ERR:
if (ch == ERR) {
if (prevCh == ch && !timeToRecalculate) {
closeTimeout++;
if (closeTimeout == 100) {
break;
}
} else
closeTimeout = 0;
drawPanel = false;
continue;
}
drawPanel = true;
switch (ch) {
case KEY_RESIZE:
{
ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2);
......@@ -237,6 +290,8 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
}
}
if (lastFocus)
*lastFocus = panelFocus;
if (lastKey)
*lastKey = ch;
}
......@@ -12,6 +12,8 @@ in the source distribution for its full text.
#include "FunctionBar.h"
#include "Vector.h"
#include "Header.h"
#include "Settings.h"
#include "Panel.h"
typedef enum Orientation_ {
VERTICAL,
......@@ -29,13 +31,13 @@ typedef struct ScreenManager_ {
int panelCount;
const FunctionBar* fuBar;
const Header* header;
time_t lastScan;
const Settings* settings;
bool owner;
bool allowFocusChange;
} 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);
......
......@@ -18,64 +18,138 @@ in the source distribution for its full text.
#define DEFAULT_DELAY 15
/*{
#include "ProcessList.h"
#include "Header.h"
#include "Process.h"
#include <stdbool.h>
typedef struct {
int len;
char** names;
int* modes;
} MeterColumnSettings;
typedef struct Settings_ {
char* userSettings;
ProcessList* pl;
Header* header;
char* filename;
MeterColumnSettings columns[2];
ProcessField* fields;
int flags;
int colorScheme;
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;
} 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) {
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);
}
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);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
free(trim);
for (int i = 0; ids[i]; i++) {
Header_createMeter(this->header, ids[i], side);
}
String_freeArray(ids);
this->columns[column].names = 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);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
free(trim);
int len = 0;
for (int i = 0; ids[i]; i++) {
int mode = atoi(ids[i]);
Header_setMode(this->header, i, mode, side);
len++;
}
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);
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) {
Header_createMeter(header, "LeftCPUs2", LEFT_HEADER);
Header_createMeter(header, "RightCPUs2", RIGHT_HEADER);
this->columns[0].names[0] = strdup("LeftCPUs2");
this->columns[1].names[r++] = strdup("RightCPUs2");
} else if (cpuCount > 4) {
Header_createMeter(header, "LeftCPUs", LEFT_HEADER);
Header_createMeter(header, "RightCPUs", RIGHT_HEADER);
this->columns[0].names[0] = strdup("LeftCPUs");
this->columns[1].names[r++] = strdup("RightCPUs");
} else {
Header_createMeter(header, "AllCPUs", LEFT_HEADER);
this->columns[0].names[0] = strdup("AllCPUs");
}
Header_createMeter(header, "Memory", LEFT_HEADER);
Header_createMeter(header, "Swap", LEFT_HEADER);
Header_createMeter(header, "Tasks", RIGHT_HEADER);
Header_createMeter(header, "LoadAverage", RIGHT_HEADER);
Header_createMeter(header, "Uptime", RIGHT_HEADER);
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++;
}
}
fields[j] = (ProcessField) NULL;
String_freeArray(ids);
}
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;
}
if (String_eq(option[0], "fields")) {
char* trim = String_trim(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);
readFields(this->fields, &(this->flags), option[1]);
} else if (String_eq(option[0], "sort_key")) {
// 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")) {
this->pl->direction = atoi(option[1]);
this->direction = atoi(option[1]);
} 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")) {
this->pl->hideThreads = atoi(option[1]);
this->hideThreads = atoi(option[1]);
} 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")) {
this->pl->hideUserlandThreads = atoi(option[1]);
this->hideUserlandThreads = atoi(option[1]);
} 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")) {
this->pl->showThreadNames = atoi(option[1]);
this->showThreadNames = atoi(option[1]);
} 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")) {
this->pl->highlightMegabytes = atoi(option[1]);
this->highlightMegabytes = atoi(option[1]);
} 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")) {
this->header->margin = atoi(option[1]);
this->headerMargin = atoi(option[1]);
} else if (String_eq(option[0], "expand_system_time")) {
// Compatibility option.
this->pl->detailedCPUTime = atoi(option[1]);
this->detailedCPUTime = atoi(option[1]);
} 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")) {
this->pl->countCPUsFromZero = atoi(option[1]);
this->countCPUsFromZero = atoi(option[1]);
} 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")) {
this->pl->accountGuestInCPUMeter = atoi(option[1]);
this->accountGuestInCPUMeter = atoi(option[1]);
} else if (String_eq(option[0], "delay")) {
this->delay = atoi(option[1]);
} else if (String_eq(option[0], "color_scheme")) {
......@@ -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 > 5) this->colorScheme = 5;
} else if (String_eq(option[0], "left_meters")) {
Settings_readMeters(this, option[1], LEFT_HEADER);
Settings_readMeters(this, option[1], 0);
readMeters = true;
} else if (String_eq(option[0], "right_meters")) {
Settings_readMeters(this, option[1], RIGHT_HEADER);
Settings_readMeters(this, option[1], 1);
readMeters = true;
} else if (String_eq(option[0], "left_meter_modes")) {
Settings_readMeterModes(this, option[1], LEFT_HEADER);
Settings_readMeterModes(this, option[1], 0);
readMeters = true;
} else if (String_eq(option[0], "right_meter_modes")) {
Settings_readMeterModes(this, option[1], RIGHT_HEADER);
Settings_readMeterModes(this, option[1], 1);
readMeters = true;
}
String_freeArray(option);
}
fclose(fd);
if (!readMeters) {
Settings_defaultMeters(this->header, cpuCount);
Settings_defaultMeters(this, cpuCount);
}
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) {
// TODO: implement File object and make
// file I/O object-oriented.
FILE* fd;
fd = fopen(this->userSettings, "w");
fd = fopen(this->filename, "w");
if (fd == NULL) {
return false;
}
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, "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");
writeFields(fd, this->fields, "fields");
// This "-1" is for compatibility with the older enum format.
fprintf(fd, "sort_key=%d\n", (int) this->pl->sortKey-1);
fprintf(fd, "sort_direction=%d\n", (int) this->pl->direction);
fprintf(fd, "hide_threads=%d\n", (int) this->pl->hideThreads);
fprintf(fd, "hide_kernel_threads=%d\n", (int) this->pl->hideKernelThreads);
fprintf(fd, "hide_userland_threads=%d\n", (int) this->pl->hideUserlandThreads);
fprintf(fd, "shadow_other_users=%d\n", (int) this->pl->shadowOtherUsers);
fprintf(fd, "show_thread_names=%d\n", (int) this->pl->showThreadNames);
fprintf(fd, "highlight_base_name=%d\n", (int) this->pl->highlightBaseName);
fprintf(fd, "highlight_megabytes=%d\n", (int) this->pl->highlightMegabytes);
fprintf(fd, "highlight_threads=%d\n", (int) this->pl->highlightThreads);
fprintf(fd, "tree_view=%d\n", (int) this->pl->treeView);
fprintf(fd, "header_margin=%d\n", (int) this->header->margin);
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->pl->detailedCPUTime);
fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->pl->countCPUsFromZero);
fprintf(fd, "update_process_names=%d\n", (int) this->pl->updateProcessNames);
fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->pl->accountGuestInCPUMeter);
fprintf(fd, "sort_key=%d\n", (int) this->sortKey-1);
fprintf(fd, "sort_direction=%d\n", (int) this->direction);
fprintf(fd, "hide_threads=%d\n", (int) this->hideThreads);
fprintf(fd, "hide_kernel_threads=%d\n", (int) this->hideKernelThreads);
fprintf(fd, "hide_userland_threads=%d\n", (int) this->hideUserlandThreads);
fprintf(fd, "shadow_other_users=%d\n", (int) this->shadowOtherUsers);
fprintf(fd, "show_thread_names=%d\n", (int) this->showThreadNames);
fprintf(fd, "highlight_base_name=%d\n", (int) this->highlightBaseName);
fprintf(fd, "highlight_megabytes=%d\n", (int) this->highlightMegabytes);
fprintf(fd, "highlight_threads=%d\n", (int) this->highlightThreads);
fprintf(fd, "tree_view=%d\n", (int) this->treeView);
fprintf(fd, "header_margin=%d\n", (int) this->headerMargin);
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime);
fprintf(fd, "cpu_count_from_zero=%d\n", (int) this->countCPUsFromZero);
fprintf(fd, "update_process_names=%d\n", (int) this->updateProcessNames);
fprintf(fd, "account_guest_in_cpu_meter=%d\n", (int) this->accountGuestInCPUMeter);
fprintf(fd, "color_scheme=%d\n", (int) this->colorScheme);
fprintf(fd, "delay=%d\n", (int) this->delay);
fprintf(fd, "left_meters=");
for (int i = 0; i < Header_size(this->header, LEFT_HEADER); i++) {
char* name = Header_readMeterName(this->header, i, LEFT_HEADER);
fprintf(fd, "%s ", name);
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");
fprintf(fd, "left_meters="); writeMeters(this, fd, 0);
fprintf(fd, "left_meter_modes="); writeMeterModes(this, fd, 0);
fprintf(fd, "right_meters="); writeMeters(this, fd, 1);
fprintf(fd, "right_meter_modes="); writeMeterModes(this, fd, 1);
fclose(fd);
return true;
}
Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
Settings* this = malloc(sizeof(Settings));
this->pl = pl;
this->header = header;
Settings* Settings_new(int cpuCount) {
Settings* this = calloc(1, sizeof(Settings));
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* rcfile = getenv("HTOPRC");
if (rcfile) {
this->userSettings = strdup(rcfile);
this->filename = strdup(rcfile);
} else {
const char* home = getenv("HOME");
if (!home) home = "";
......@@ -251,11 +331,11 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
char* configDir = NULL;
char* htopDir = NULL;
if (xdgConfigHome) {
this->userSettings = String_cat(xdgConfigHome, "/htop/htoprc");
this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
configDir = strdup(xdgConfigHome);
htopDir = String_cat(xdgConfigHome, "/htop");
} else {
this->userSettings = String_cat(home, "/.config/htop/htoprc");
this->filename = String_cat(home, "/.config/htop/htoprc");
configDir = String_cat(home, "/.config");
htopDir = String_cat(home, "/.config/htop");
}
......@@ -276,7 +356,7 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
this->colorScheme = 0;
this->changed = false;
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 (legacyDotfile) {
// Transition to new location and delete old configuration file
......@@ -290,12 +370,19 @@ Settings* Settings_new(ProcessList* pl, Header* header, int cpuCount) {
ok = Settings_read(this, systemSettings, cpuCount);
free(systemSettings);
if (!ok) {
Settings_defaultMeters(this->header, cpuCount);
pl->hideKernelThreads = true;
pl->highlightMegabytes = true;
pl->highlightThreads = false;
Settings_defaultMeters(this, cpuCount);
this->hideKernelThreads = true;
this->highlightMegabytes = true;
this->highlightThreads = false;
}
}
free(legacyDotfile);
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.
#define DEFAULT_DELAY 15
#include "ProcessList.h"
#include "Header.h"
#include "Process.h"
#include <stdbool.h>
typedef struct {
int len;
char** names;
int* modes;
} MeterColumnSettings;
typedef struct Settings_ {
char* userSettings;
ProcessList* pl;
Header* header;
char* filename;
MeterColumnSettings columns[2];
ProcessField* fields;
int flags;
int colorScheme;
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;
} Settings;
#ifndef Settings_cpuId
#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromZero ? (cpu) : (cpu)+1)
#endif
void Settings_delete(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
......@@ -8,7 +8,7 @@ in the source distribution for its full text.
#include "SwapMeter.h"
#include "CRT.h"
#include "ProcessList.h"
#include "Platform.h"
#include <stdlib.h>
#include <string.h>
......@@ -34,11 +34,8 @@ static void SwapMeter_humanNumber(char* buffer, const long int* value) {
}
static void SwapMeter_setValues(Meter* this, char* buffer, int len) {
long int usedSwap = this->pl->usedSwap;
this->total = this->pl->totalSwap;
this->values[0] = usedSwap;
snprintf(buffer, len, "%ld/%ldMB", (long int) usedSwap / MEGABYTE, (long int) this->total / MEGABYTE);
Platform_setSwapValues(this);
snprintf(buffer, len, "%ld/%ldMB", (long int) this->values[0] / MEGABYTE, (long int) this->total / MEGABYTE);
}
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