Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCP-related changes for column handling; code cleanups #1182

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Action.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ static Htop_Reaction actionRedraw(ATTR_UNUSED State* st) {
return HTOP_REFRESH | HTOP_REDRAW_BAR;
}

static Htop_Reaction actionTogglePauseProcessUpdate(State* st) {
st->pauseProcessUpdate = !st->pauseProcessUpdate;
static Htop_Reaction actionTogglePauseUpdate(State* st) {
st->pauseUpdate = !st->pauseUpdate;
return HTOP_REFRESH | HTOP_REDRAW_BAR;
}

Expand Down Expand Up @@ -779,7 +779,7 @@ void Action_setBindings(Htop_Action* keys) {
keys['S'] = actionSetup;
keys['T'] = actionSortByTime;
keys['U'] = actionUntagAll;
keys['Z'] = actionTogglePauseProcessUpdate;
keys['Z'] = actionTogglePauseUpdate;
keys['['] = actionLowerPriority;
keys['\014'] = actionRedraw; // Ctrl+L
keys['\177'] = actionCollapseIntoParent;
Expand Down
8 changes: 5 additions & 3 deletions Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ in the source distribution for its full text.
#include "Panel.h"
#include "Process.h"
#include "ProcessList.h"
#include "GenericDataList.h"
#include "Settings.h"
#include "UsersTable.h"

Expand All @@ -39,15 +40,16 @@ typedef struct State_ {
Settings* settings;
UsersTable* ut;
ProcessList* pl;
GenericDataList* gl;
struct MainPanel_* mainPanel;
Header* header;
bool pauseProcessUpdate;
bool hideProcessSelection;
bool pauseUpdate;
bool hideSelection;
bool hideMeters;
} State;

static inline bool State_hideFunctionBar(const State* st) {
return st->settings->hideFunctionBar == 2 || (st->settings->hideFunctionBar == 1 && st->hideProcessSelection);
return st->settings->hideFunctionBar == 2 || (st->settings->hideFunctionBar == 1 && st->hideSelection);
}

typedef Htop_Reaction (*Htop_Action)(State* st);
Expand Down
16 changes: 14 additions & 2 deletions AvailableColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ in the source distribution for its full text.
#include "XUtils.h"


Panel* activeAvailableColumns;

static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL};

static void AvailableColumnsPanel_delete(Object* object) {
Panel* super = (Panel*) object;
AvailableColumnsPanel* this = (AvailableColumnsPanel*) object;
if (activeAvailableColumns == super)
activeAvailableColumns = NULL;
Panel_done(super);
free(this);
}
Expand Down Expand Up @@ -81,6 +85,9 @@ const PanelClass AvailableColumnsPanel_class = {

static void AvailableColumnsPanel_addDynamicColumn(ht_key_t key, void* value, void* data) {
const DynamicColumn* column = (const DynamicColumn*) value;
if (column->belongToDynamicScreen)
return;

Panel* super = (Panel*) data;
const char* title = column->caption ? column->caption : column->heading;
if (!title)
Expand All @@ -91,13 +98,13 @@ static void AvailableColumnsPanel_addDynamicColumn(ht_key_t key, void* value, vo
}

// Handle DynamicColumns entries in the AvailableColumnsPanel
static void AvailableColumnsPanel_addDynamicColumns(Panel* super, Hashtable* dynamicColumns) {
void AvailableColumnsPanel_addDynamicColumns(Panel* super, Hashtable* dynamicColumns) {
assert(dynamicColumns);
Hashtable_foreach(dynamicColumns, AvailableColumnsPanel_addDynamicColumn, super);
}

// Handle remaining Platform Meter entries in the AvailableColumnsPanel
static void AvailableColumnsPanel_addPlatformColumn(Panel* super) {
void AvailableColumnsPanel_addPlatformColumn(Panel* super) {
for (int i = 1; i < LAST_PROCESSFIELD; i++) {
if (i != COMM && Process_fields[i].description) {
char description[256];
Expand All @@ -117,6 +124,11 @@ AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns, Hashtable* dyna
AvailableColumnsPanel_addPlatformColumn(super);
AvailableColumnsPanel_addDynamicColumns(super, dynamicColumns);

activeAvailableColumns = super;
this->columns = columns;
return this;
}

Panel* AvailableColumnsPanel_get(void) {
return activeAvailableColumns;
}
6 changes: 6 additions & 0 deletions AvailableColumnsPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ extern const PanelClass AvailableColumnsPanel_class;

AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns, Hashtable* dynamicColumns);

void AvailableColumnsPanel_addPlatformColumn(Panel* super);

void AvailableColumnsPanel_addDynamicColumns(Panel* super, Hashtable* dynamicColumns);

Panel* AvailableColumnsPanel_get(void);

#endif
27 changes: 21 additions & 6 deletions CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ in the source distribution for its full text.
#include "CRT.h"
#include "DynamicColumn.h"
#include "DynamicMeter.h"
#include "DynamicScreen.h"
#include "GenericDataList.h"
#include "Hashtable.h"
#include "Header.h"
#include "IncSet.h"
Expand Down Expand Up @@ -328,11 +330,16 @@ int CommandLine_run(const char* name, int argc, char** argv) {
dc = Hashtable_new(0, true);

ProcessList* pl = ProcessList_new(ut, dm, dc, flags.pidMatchList, flags.userId);

Settings* settings = Settings_new(pl->activeCPUs, dc);

Hashtable* dt = DynamicScreens_new(settings);
GenericDataList* gl = GenericDataList_new();

pl->settings = settings;
if (gl)
gl->settings = settings;

Header* header = Header_new(pl, settings, 2);
Header* header = Header_new(pl, gl, settings, 2);

Header_populateFromSettings(header);

Expand Down Expand Up @@ -362,7 +369,7 @@ int CommandLine_run(const char* name, int argc, char** argv) {
CRT_init(settings, flags.allowUnicode);

MainPanel* panel = MainPanel_new();
ProcessList_setPanel(pl, (Panel*) panel);
MainPanel* genericDataPanel = MainPanel_new();

MainPanel_updateLabels(panel, settings->ss->treeView, flags.commFilter);

Expand All @@ -372,12 +379,18 @@ int CommandLine_run(const char* name, int argc, char** argv) {
.pl = pl,
.mainPanel = panel,
.header = header,
.pauseProcessUpdate = false,
.hideProcessSelection = false,
.pauseUpdate = false,
.hideSelection = false,
.hideMeters = false,
};

MainPanel_setState(panel, &state);
panel->state = &state;
genericDataPanel->state = &state;

ProcessList_setPanel(pl, (Panel*) panel);
if (gl)
GenericDataList_setPanel(gl, (Panel*) genericDataPanel);

if (flags.commFilter)
setCommFilter(&state, &(flags.commFilter));

Expand Down Expand Up @@ -405,6 +418,7 @@ int CommandLine_run(const char* name, int argc, char** argv) {

Header_delete(header);
ProcessList_delete(pl);
GenericDataList_delete(gl);

ScreenManager_delete(scr);
MetersPanel_cleanup();
Expand All @@ -420,6 +434,7 @@ int CommandLine_run(const char* name, int argc, char** argv) {
Settings_delete(settings);
DynamicColumns_delete(dc);
DynamicMeters_delete(dm);
DynamicScreens_delete(dt);

return 0;
}
12 changes: 7 additions & 5 deletions DynamicColumn.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#define DYNAMIC_DEFAULT_COLUMN_WIDTH -5

typedef struct DynamicColumn_ {
char name[32]; /* unique, internal-only name */
char* heading; /* displayed in main screen */
char* caption; /* displayed in setup menu (short name) */
char* description; /* displayed in setup menu (detail) */
int width; /* display width +/- for value alignment */
char name[32]; /* unique, internal-only name */
char* heading; /* displayed in main screen */
char* caption; /* displayed in setup menu (short name) */
char* description; /* displayed in setup menu (detail) */
int width; /* display width +/- for value alignment */
bool belongToDynamicScreen; /* belong to DynamicScreen or ProcessList screen? */
bool enabled; /* false == ignore this column */
} DynamicColumn;

Hashtable* DynamicColumns_new(void);
Expand Down
62 changes: 62 additions & 0 deletions DynamicScreen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
htop - DynamicScreen.c
(C) 2022 Sohaib Mohammed
(C) 2022 htop dev team
(C) 2022 Red Hat, Inc. All Rights Reserved.
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "DynamicScreen.h"

#include <stdbool.h>
#include <stddef.h>

#include "Hashtable.h"
#include "Platform.h"
#include "XUtils.h"


Hashtable* DynamicScreens_new(Settings* settings) {
return Platform_dynamicScreens(settings);
}

void DynamicScreens_delete(Hashtable* dynamics) {
if (dynamics) {
Platform_dynamicScreensDone(dynamics);
Hashtable_delete(dynamics);
}
}

typedef struct {
ht_key_t key;
const char* name;
bool found;
} DynamicIterator;

static void DynamicScreen_compare(ht_key_t key, void* value, void* data) {
const DynamicScreen* screen = (const DynamicScreen*)value;
DynamicIterator* iter = (DynamicIterator*)data;
if (String_eq(iter->name, screen->name)) {
iter->found = true;
iter->key = key;
}
}

bool DynamicScreen_search(Hashtable* dynamics, const char* name, ht_key_t* key) {
DynamicIterator iter = { .key = 0, .name = name, .found = false };
if (dynamics)
Hashtable_foreach(dynamics, DynamicScreen_compare, &iter);
if (key)
*key = iter.key;
return iter.found;
}

const char* DynamicScreen_lookup(Hashtable* dynamics, ht_key_t key) {
const DynamicScreen* screen = Hashtable_get(dynamics, key);
return screen ? screen->name : NULL;
}

void DynamicScreen_availableColumns(char* currentScreen) {
Platform_dynamicScreenAvailableColumns(currentScreen);
}
28 changes: 28 additions & 0 deletions DynamicScreen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef HEADER_DynamicScreen
#define HEADER_DynamicScreen

#include <stdbool.h>

#include "Hashtable.h"
#include "Settings.h"


typedef struct DynamicScreen_ {
char name[32]; /* unique name, cannot contain spaces */
char* caption;
char* fields;
char* sortKey;
int direction;
} DynamicScreen;

Hashtable* DynamicScreens_new(Settings* settings);

void DynamicScreens_delete(Hashtable* dynamics);

const char* DynamicScreen_lookup(Hashtable* dynamics, unsigned int key);

bool DynamicScreen_search(Hashtable* dynamics, const char* name, unsigned int* key);

void DynamicScreen_availableColumns(char* currentScreen);

#endif
69 changes: 69 additions & 0 deletions GenericData.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
htop - GenericData.c
(C) 2022 Sohaib Mohammed
(C) 2022 htop dev team
(C) 2022 Red Hat, Inc.
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include "GenericData.h"

#include <assert.h>

#include "CRT.h"
#include "Macros.h"
#include "Process.h"
#include "RichString.h"
#include "Settings.h"


void GenericData_init(GenericData* this, const Settings* settings) {
this->settings = settings;
}

void GenericData_done(ATTR_UNUSED GenericData* this) {
assert (this != NULL);
}

void GenericData_writeField(ATTR_UNUSED const GenericData* this, ATTR_UNUSED RichString* str, ATTR_UNUSED int field) {
return;
}

void GenericData_display(const Object* cast, RichString* out) {
const GenericData* this = (const GenericData*) cast;
const ProcessField* fields = this->settings->ss->fields;
for (int i = 0; fields[i]; i++)
As_GenericData(this)->writeField(this, out, i);
}

int GenericData_compare(const void* v1, const void* v2) {
const GenericData* g1 = (const GenericData*)v1;
const GenericData* g2 = (const GenericData*)v2;

const Settings* settings = g1->settings;
const ScreenSettings* ss = settings->ss;

ProcessField key = ScreenSettings_getActiveSortKey(ss);

int result = GenericData_compareByKey(g1, g2, key);

return (ScreenSettings_getActiveDirection(ss) == 1) ? result : -result;
}

int GenericData_compareByKey_Base(const GenericData* g1, const GenericData* g2, ATTR_UNUSED ProcessField key) {
// TODO
(void) g1;
(void) g2;

return 0;
}

const GenericDataClass GenericData_class = {
.super = {
.extends = Class(Object),
.display = GenericData_display,
.compare = GenericData_compare,
},
.writeField = GenericData_writeField,
};
Loading