Commit 00b324bf authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Changes in object model: separate class objects to store vtable. Also, nicer...

Changes in object model: separate class objects to store vtable. Also, nicer UTF-8 display of big numbers.
parent 2a73405c
...@@ -13,6 +13,6 @@ in the source distribution for its full text. ...@@ -13,6 +13,6 @@ in the source distribution for its full text.
extern int UptimeMeter_attributes[]; extern int UptimeMeter_attributes[];
extern MeterType UptimeMeter; extern MeterClass UptimeMeter_class;
#endif #endif
...@@ -23,17 +23,16 @@ in the source distribution for its full text. ...@@ -23,17 +23,16 @@ in the source distribution for its full text.
typedef struct Vector_ { typedef struct Vector_ {
Object **array; Object **array;
Object_Compare compare; ObjectClass* type;
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
char* type;
bool owner; bool owner;
} Vector; } Vector;
}*/ }*/
Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare) { Vector* Vector_new(ObjectClass* type, bool owner, int size) {
Vector* this; Vector* this;
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)
...@@ -45,7 +44,6 @@ Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare) { ...@@ -45,7 +44,6 @@ Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare) {
this->items = 0; this->items = 0;
this->type = type; this->type = type;
this->owner = owner; this->owner = owner;
this->compare = compare;
return this; return this;
} }
...@@ -53,7 +51,7 @@ void Vector_delete(Vector* this) { ...@@ -53,7 +51,7 @@ void Vector_delete(Vector* this) {
if (this->owner) { if (this->owner) {
for (int i = 0; i < this->items; i++) for (int i = 0; i < this->items; i++)
if (this->array[i]) if (this->array[i])
(this->array[i])->delete(this->array[i]); Object_delete(this->array[i]);
} }
free(this->array); free(this->array);
free(this); free(this);
...@@ -65,7 +63,7 @@ static inline bool Vector_isConsistent(Vector* this) { ...@@ -65,7 +63,7 @@ static inline bool Vector_isConsistent(Vector* this) {
assert(this->items <= this->arraySize); assert(this->items <= this->arraySize);
if (this->owner) { if (this->owner) {
for (int i = 0; i < this->items; i++) for (int i = 0; i < this->items; i++)
if (this->array[i] && this->array[i]->class != this->type) if (this->array[i] && !Object_isA(this->array[i], this->type))
return false; return false;
return true; return true;
} else { } else {
...@@ -92,7 +90,7 @@ void Vector_prune(Vector* this) { ...@@ -92,7 +90,7 @@ void Vector_prune(Vector* this) {
if (this->owner) { if (this->owner) {
for (i = 0; i < this->items; i++) for (i = 0; i < this->items; i++)
if (this->array[i]) { if (this->array[i]) {
(this->array[i])->delete(this->array[i]); Object_delete(this->array[i]);
//this->array[i] = NULL; //this->array[i] = NULL;
} }
} }
...@@ -164,18 +162,16 @@ static void insertionSort(Object** array, int left, int right, Object_Compare co ...@@ -164,18 +162,16 @@ static void insertionSort(Object** array, int left, int right, Object_Compare co
} }
void Vector_quickSort(Vector* this) { void Vector_quickSort(Vector* this) {
assert(this->compare); assert(this->type->compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Object_Compare compare = this->compare; quickSort(this->array, 0, this->items - 1, this->type->compare);
quickSort(this->array, 0, this->items - 1, compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }
void Vector_insertionSort(Vector* this) { void Vector_insertionSort(Vector* this) {
assert(this->compare); assert(this->type->compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Object_Compare compare = this->compare; insertionSort(this->array, 0, this->items - 1, this->type->compare);
insertionSort(this->array, 0, this->items - 1, compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }
...@@ -196,8 +192,7 @@ void Vector_insert(Vector* this, int idx, void* data_) { ...@@ -196,8 +192,7 @@ void Vector_insert(Vector* this, int idx, void* data_) {
Object* data = data_; Object* data = data_;
assert(idx >= 0); assert(idx >= 0);
assert(idx <= this->items); assert(idx <= this->items);
assert(data_); assert(Object_isA(data, this->type));
assert(data->class == this->type);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Vector_checkArraySize(this); Vector_checkArraySize(this);
...@@ -226,7 +221,7 @@ Object* Vector_take(Vector* this, int idx) { ...@@ -226,7 +221,7 @@ Object* Vector_take(Vector* this, int idx) {
Object* Vector_remove(Vector* this, int idx) { Object* Vector_remove(Vector* this, int idx) {
Object* removed = Vector_take(this, idx); Object* removed = Vector_take(this, idx);
if (this->owner) { if (this->owner) {
removed->delete(removed); Object_delete(removed);
return NULL; return NULL;
} else } else
return removed; return removed;
...@@ -253,9 +248,9 @@ void Vector_moveDown(Vector* this, int idx) { ...@@ -253,9 +248,9 @@ void Vector_moveDown(Vector* this, int idx) {
} }
void Vector_set(Vector* this, int idx, void* data_) { void Vector_set(Vector* this, int idx, void* data_) {
assert(idx >= 0);
assert(((Object*)data_)->class == this->type);
Object* data = data_; Object* data = data_;
assert(idx >= 0);
assert(Object_isA((Object*)data, this->type));
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Vector_checkArraySize(this); Vector_checkArraySize(this);
...@@ -266,7 +261,7 @@ void Vector_set(Vector* this, int idx, void* data_) { ...@@ -266,7 +261,7 @@ void Vector_set(Vector* this, int idx, void* data_) {
Object* removed = this->array[idx]; Object* removed = this->array[idx];
assert (removed != NULL); assert (removed != NULL);
if (this->owner) { if (this->owner) {
removed->delete(removed); Object_delete(removed);
} }
} }
} }
...@@ -309,8 +304,8 @@ static void Vector_merge(Vector* this, Vector* v2) { ...@@ -309,8 +304,8 @@ static void Vector_merge(Vector* this, Vector* v2) {
*/ */
void Vector_add(Vector* this, void* data_) { void Vector_add(Vector* this, void* data_) {
assert(data_ && ((Object*)data_)->class == this->type);
Object* data = data_; Object* data = data_;
assert(Object_isA((Object*)data, this->type));
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i = this->items; int i = this->items;
Vector_set(this, this->items, data); Vector_set(this, this->items, data);
...@@ -319,9 +314,9 @@ void Vector_add(Vector* this, void* data_) { ...@@ -319,9 +314,9 @@ void Vector_add(Vector* this, void* data_) {
} }
inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) { inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) {
assert(((Object*)search_)->class == this->type);
assert(this->compare);
Object* search = search_; Object* search = search_;
assert(Object_isA((Object*)search, this->type));
assert(compare);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
for (int i = 0; i < this->items; i++) { for (int i = 0; i < this->items; i++) {
Object* o = (Object*)this->array[i]; Object* o = (Object*)this->array[i];
......
...@@ -19,16 +19,15 @@ in the source distribution for its full text. ...@@ -19,16 +19,15 @@ in the source distribution for its full text.
typedef struct Vector_ { typedef struct Vector_ {
Object **array; Object **array;
Object_Compare compare; ObjectClass* type;
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
char* type;
bool owner; bool owner;
} Vector; } Vector;
Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare); Vector* Vector_new(ObjectClass* type, bool owner, int size);
void Vector_delete(Vector* this); void Vector_delete(Vector* this);
......
...@@ -210,8 +210,6 @@ static bool changePriority(Panel* panel, int delta) { ...@@ -210,8 +210,6 @@ static bool changePriority(Panel* panel, int delta) {
static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const char** keyLabels, FunctionBar* prevBar, Header* header) { static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const char** keyLabels, FunctionBar* prevBar, Header* header) {
const char* fuKeys[] = {"Enter", "Esc", NULL}; const char* fuKeys[] = {"Enter", "Esc", NULL};
int fuEvents[] = {13, 27}; int fuEvents[] = {13, 27};
if (!list->eventHandler)
Panel_setEventHandler(list, Panel_selectByTyping);
ScreenManager* scr = ScreenManager_new(0, y, 0, -1, HORIZONTAL, header, false); ScreenManager* scr = ScreenManager_new(0, y, 0, -1, HORIZONTAL, header, false);
scr->allowFocusChange = false; scr->allowFocusChange = false;
ScreenManager_add(scr, list, FunctionBar_new(keyLabels, fuKeys, fuEvents), x - 1); ScreenManager_add(scr, list, FunctionBar_new(keyLabels, fuKeys, fuEvents), x - 1);
...@@ -275,7 +273,6 @@ int main(int argc, char** argv) { ...@@ -275,7 +273,6 @@ int main(int argc, char** argv) {
bool userOnly = false; bool userOnly = false;
uid_t userId = 0; uid_t userId = 0;
int usecolors = 1; int usecolors = 1;
TreeType treeType = TREE_TYPE_AUTO;
char *argCopy; char *argCopy;
char *pid; char *pid;
Hashtable *pidWhiteList = NULL; Hashtable *pidWhiteList = NULL;
...@@ -378,11 +375,23 @@ int main(int argc, char** argv) { ...@@ -378,11 +375,23 @@ int main(int argc, char** argv) {
bool doRecalculate = false; bool doRecalculate = false;
Settings* settings; Settings* settings;
Panel* killPanel = NULL;
ProcessList* pl = NULL; ProcessList* pl = NULL;
UsersTable* ut = UsersTable_new(); UsersTable* ut = UsersTable_new();
#ifdef HAVE_LIBNCURSESW
char *locale = setlocale(LC_ALL, NULL);
if (locale == NULL || locale[0] == '\0')
locale = setlocale(LC_CTYPE, NULL);
if (locale != NULL &&
(strstr(locale, "UTF-8") ||
strstr(locale, "utf-8") ||
strstr(locale, "UTF8") ||
strstr(locale, "utf8")))
CRT_utf8 = true;
else
CRT_utf8 = false;
#endif
pl = ProcessList_new(ut, pidWhiteList); pl = ProcessList_new(ut, pidWhiteList);
Process_getMaxPid(); Process_getMaxPid();
...@@ -396,36 +405,9 @@ int main(int argc, char** argv) { ...@@ -396,36 +405,9 @@ int main(int argc, char** argv) {
if (!usecolors) if (!usecolors)
settings->colorScheme = COLORSCHEME_MONOCHROME; settings->colorScheme = COLORSCHEME_MONOCHROME;
if (treeType == TREE_TYPE_AUTO) {
#ifdef HAVE_LIBNCURSESW
char *locale = setlocale(LC_ALL, NULL);
if (locale == NULL || locale[0] == '\0')
locale = setlocale(LC_CTYPE, NULL);
if (locale != NULL &&
(strstr(locale, "UTF-8") ||
strstr(locale, "utf-8") ||
strstr(locale, "UTF8") ||
strstr(locale, "utf8")))
treeType = TREE_TYPE_UTF8;
else
treeType = TREE_TYPE_ASCII;
#else
treeType = TREE_TYPE_ASCII;
#endif
}
switch (treeType) {
default:
case TREE_TYPE_ASCII:
pl->treeStr = ProcessList_treeStrAscii;
break;
case TREE_TYPE_UTF8:
pl->treeStr = ProcessList_treeStrUtf8;
break;
}
CRT_init(settings->delay, settings->colorScheme); CRT_init(settings->delay, settings->colorScheme);
Panel* panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, PROCESS_CLASS, false, NULL); Panel* panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, false, &Process_class);
ProcessList_setPanel(pl, panel); ProcessList_setPanel(pl, panel);
if (sortKey > 0) { if (sortKey > 0) {
...@@ -457,14 +439,18 @@ int main(int argc, char** argv) { ...@@ -457,14 +439,18 @@ int main(int argc, char** argv) {
int ch = ERR; int ch = ERR;
int closeTimeout = 0; int closeTimeout = 0;
bool idle = false;
while (!quit) { while (!quit) {
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000); newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
recalculate = (newTime - oldTime > CRT_delay); recalculate = (newTime - oldTime > settings->delay);
Process* p = (Process*)Panel_getSelected(panel); Process* p = (Process*)Panel_getSelected(panel);
int following = (follow && p) ? p->pid : -1; int following = (follow && p) ? p->pid : -1;
if (recalculate) if (recalculate) {
Header_draw(header);
oldTime = newTime; oldTime = newTime;
}
if (doRefresh) { if (doRefresh) {
if (recalculate || doRecalculate) { if (recalculate || doRecalculate) {
ProcessList_scan(pl); ProcessList_scan(pl);
...@@ -475,12 +461,13 @@ int main(int argc, char** argv) { ...@@ -475,12 +461,13 @@ int main(int argc, char** argv) {
refreshTimeout = 1; refreshTimeout = 1;
} }
ProcessList_rebuildPanel(pl, true, following, userOnly, userId, IncSet_filter(inc)); ProcessList_rebuildPanel(pl, true, following, userOnly, userId, IncSet_filter(inc));
idle = false;
} }
doRefresh = true; doRefresh = true;
Header_draw(header); if (!idle)
Panel_draw(panel, true); Panel_draw(panel, true);
int prev = ch; int prev = ch;
if (inc->active) if (inc->active)
move(LINES-1, CRT_cursorX); move(LINES-1, CRT_cursorX);
...@@ -496,8 +483,10 @@ int main(int argc, char** argv) { ...@@ -496,8 +483,10 @@ int main(int argc, char** argv) {
} }
} else } else
closeTimeout = 0; closeTimeout = 0;
idle = true;
continue; continue;
} }
idle = false;
if (ch == KEY_MOUSE) { if (ch == KEY_MOUSE) {
MEVENT mevent; MEVENT mevent;
...@@ -655,7 +644,7 @@ int main(int argc, char** argv) { ...@@ -655,7 +644,7 @@ int main(int argc, char** argv) {
} }
case 'u': case 'u':
{ {
Panel* usersPanel = Panel_new(0, 0, 0, 0, LISTITEM_CLASS, true, ListItem_compare); Panel* usersPanel = Panel_new(0, 0, 0, 0, true, Class(ListItem));
Panel_setHeader(usersPanel, "Show processes of:"); Panel_setHeader(usersPanel, "Show processes of:");
UsersTable_foreach(ut, addUserToVector, usersPanel); UsersTable_foreach(ut, addUserToVector, usersPanel);
Vector_insertionSort(usersPanel->items); Vector_insertionSort(usersPanel->items);
...@@ -687,12 +676,9 @@ int main(int argc, char** argv) { ...@@ -687,12 +676,9 @@ int main(int argc, char** argv) {
case KEY_F(9): case KEY_F(9):
case 'k': case 'k':
{ {
if (!killPanel) { Panel* signalsPanel = (Panel*) SignalsPanel_new();
killPanel = (Panel*) SignalsPanel_new(0, 0, 0, 0);
}
SignalsPanel_reset((SignalsPanel*) killPanel);
const char* fuFunctions[] = {"Send ", "Cancel ", NULL}; const char* fuFunctions[] = {"Send ", "Cancel ", NULL};
ListItem* sgn = (ListItem*) pickFromVector(panel, killPanel, 15, headerHeight, fuFunctions, defaultBar, header); ListItem* sgn = (ListItem*) pickFromVector(panel, signalsPanel, 15, headerHeight, fuFunctions, defaultBar, header);
if (sgn) { if (sgn) {
if (sgn->key != 0) { if (sgn->key != 0) {
Panel_setHeader(panel, "Sending..."); Panel_setHeader(panel, "Sending...");
...@@ -703,6 +689,7 @@ int main(int argc, char** argv) { ...@@ -703,6 +689,7 @@ int main(int argc, char** argv) {
} }
} }
ProcessList_printHeader(pl, Panel_getHeader(panel)); ProcessList_printHeader(pl, Panel_getHeader(panel));
Panel_delete((Object*)signalsPanel);
refreshTimeout = 0; refreshTimeout = 0;
break; break;
} }
...@@ -744,7 +731,7 @@ int main(int argc, char** argv) { ...@@ -744,7 +731,7 @@ int main(int argc, char** argv) {
case '.': case '.':
case KEY_F(6): case KEY_F(6):
{ {
Panel* sortPanel = Panel_new(0, 0, 0, 0, LISTITEM_CLASS, true, ListItem_compare); Panel* sortPanel = Panel_new(0, 0, 0, 0, true, Class(ListItem));
Panel_setHeader(sortPanel, "Sort by"); Panel_setHeader(sortPanel, "Sort by");
const char* fuFunctions[] = {"Sort ", "Cancel ", NULL}; const char* fuFunctions[] = {"Sort ", "Cancel ", NULL};
ProcessField* fields = pl->fields; ProcessField* fields = pl->fields;
...@@ -762,7 +749,7 @@ int main(int argc, char** argv) { ...@@ -762,7 +749,7 @@ int main(int argc, char** argv) {
} else { } else {
ProcessList_printHeader(pl, Panel_getHeader(panel)); ProcessList_printHeader(pl, Panel_getHeader(panel));
} }
((Object*)sortPanel)->delete((Object*)sortPanel); Object_delete(sortPanel);
refreshTimeout = 0; refreshTimeout = 0;
break; break;
} }
...@@ -858,8 +845,6 @@ int main(int argc, char** argv) { ...@@ -858,8 +845,6 @@ int main(int argc, char** argv) {
IncSet_delete(inc); IncSet_delete(inc);
FunctionBar_delete((Object*)defaultBar); FunctionBar_delete((Object*)defaultBar);
Panel_delete((Object*)panel); Panel_delete((Object*)panel);
if (killPanel)
((Object*)killPanel)->delete((Object*)killPanel);
UsersTable_delete(ut); UsersTable_delete(ut);
Settings_delete(settings); Settings_delete(settings);
if(pidWhiteList) { if(pidWhiteList) {
......
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