Use MainPanel type in State struct

The State struct holds a pointer to the main process panel.
Use the distinct MainPanel type, to improve maintainability regrading
its usage.
This avoids usages of down-casts from Panel to MainPanel, only up-casts
from MainPanel to Panel are now required.
This commit is contained in:
Christian Göttsche 2021-02-05 14:12:49 +01:00
parent fd1ad863dc
commit fd4e6b432b
5 changed files with 48 additions and 46 deletions

View File

@ -41,18 +41,18 @@ in the source distribution for its full text.
Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) { Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) {
Panel* panel = st->panel; MainPanel* mainPanel = st->mainPanel;
Header* header = st->header; Header* header = st->header;
int y = panel->y; int y = ((Panel*)mainPanel)->y;
ScreenManager* scr = ScreenManager_new(header, st->settings, st, false); ScreenManager* scr = ScreenManager_new(header, st->settings, st, false);
scr->allowFocusChange = false; scr->allowFocusChange = false;
ScreenManager_add(scr, list, x - 1); ScreenManager_add(scr, list, x - 1);
ScreenManager_add(scr, panel, -1); ScreenManager_add(scr, (Panel*)mainPanel, -1);
Panel* panelFocus; Panel* panelFocus;
int ch; int ch;
bool unfollow = false; bool unfollow = false;
int pid = followProcess ? MainPanel_selectedPid((MainPanel*)panel) : -1; int pid = followProcess ? MainPanel_selectedPid(mainPanel) : -1;
if (followProcess && header->pl->following == -1) { if (followProcess && header->pl->following == -1) {
header->pl->following = pid; header->pl->following = pid;
unfollow = true; unfollow = true;
@ -62,11 +62,11 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess)
header->pl->following = -1; header->pl->following = -1;
} }
ScreenManager_delete(scr); ScreenManager_delete(scr);
Panel_move(panel, 0, y); Panel_move((Panel*)mainPanel, 0, y);
Panel_resize(panel, COLS, LINES - y - 1); Panel_resize((Panel*)mainPanel, COLS, LINES - y - 1);
if (panelFocus == list && ch == 13) { if (panelFocus == list && ch == 13) {
if (followProcess) { if (followProcess) {
const Process* selected = (const Process*)Panel_getSelected(panel); const Process* selected = (const Process*)Panel_getSelected((Panel*)mainPanel);
if (selected && selected->pid == pid) if (selected && selected->pid == pid)
return Panel_getSelected(list); return Panel_getSelected(list);
@ -235,25 +235,25 @@ static Htop_Reaction actionToggleTreeView(State* st) {
} }
static Htop_Reaction actionIncFilter(State* st) { static Htop_Reaction actionIncFilter(State* st) {
IncSet* inc = ((MainPanel*)st->panel)->inc; IncSet* inc = (st->mainPanel)->inc;
IncSet_activate(inc, INC_FILTER, st->panel); IncSet_activate(inc, INC_FILTER, (Panel*)st->mainPanel);
st->pl->incFilter = IncSet_filter(inc); st->pl->incFilter = IncSet_filter(inc);
return HTOP_REFRESH | HTOP_KEEP_FOLLOWING; return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
} }
static Htop_Reaction actionIncSearch(State* st) { static Htop_Reaction actionIncSearch(State* st) {
IncSet_reset(((MainPanel*)st->panel)->inc, INC_SEARCH); IncSet_reset(st->mainPanel->inc, INC_SEARCH);
IncSet_activate(((MainPanel*)st->panel)->inc, INC_SEARCH, st->panel); IncSet_activate(st->mainPanel->inc, INC_SEARCH, (Panel*)st->mainPanel);
return HTOP_REFRESH | HTOP_KEEP_FOLLOWING; return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
} }
static Htop_Reaction actionHigherPriority(State* st) { static Htop_Reaction actionHigherPriority(State* st) {
bool changed = changePriority((MainPanel*)st->panel, -1); bool changed = changePriority(st->mainPanel, -1);
return changed ? HTOP_REFRESH : HTOP_OK; return changed ? HTOP_REFRESH : HTOP_OK;
} }
static Htop_Reaction actionLowerPriority(State* st) { static Htop_Reaction actionLowerPriority(State* st) {
bool changed = changePriority((MainPanel*)st->panel, 1); bool changed = changePriority(st->mainPanel, 1);
return changed ? HTOP_REFRESH : HTOP_OK; return changed ? HTOP_REFRESH : HTOP_OK;
} }
@ -265,7 +265,7 @@ static Htop_Reaction actionInvertSortOrder(State* st) {
} }
static Htop_Reaction actionExpandOrCollapse(State* st) { static Htop_Reaction actionExpandOrCollapse(State* st) {
bool changed = expandCollapse(st->panel); bool changed = expandCollapse((Panel*)st->mainPanel);
return changed ? HTOP_RECALCULATE : HTOP_OK; return changed ? HTOP_RECALCULATE : HTOP_OK;
} }
@ -273,7 +273,7 @@ static Htop_Reaction actionCollapseIntoParent(State* st) {
if (!st->settings->treeView) { if (!st->settings->treeView) {
return HTOP_OK; return HTOP_OK;
} }
bool changed = collapseIntoParent(st->panel); bool changed = collapseIntoParent((Panel*)st->mainPanel);
return changed ? HTOP_RECALCULATE : HTOP_OK; return changed ? HTOP_RECALCULATE : HTOP_OK;
} }
@ -290,9 +290,7 @@ static Htop_Reaction actionSetAffinity(State* st) {
return HTOP_OK; return HTOP_OK;
#if (defined(HAVE_LIBHWLOC) || defined(HAVE_LINUX_AFFINITY)) #if (defined(HAVE_LIBHWLOC) || defined(HAVE_LINUX_AFFINITY))
Panel* panel = st->panel; const Process* p = (const Process*) Panel_getSelected((Panel*)st->mainPanel);
const Process* p = (const Process*) Panel_getSelected(panel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
@ -308,7 +306,7 @@ static Htop_Reaction actionSetAffinity(State* st) {
const void* set = Action_pickFromVector(st, affinityPanel, width, true); const void* set = Action_pickFromVector(st, affinityPanel, width, true);
if (set) { if (set) {
Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, st->pl); Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, st->pl);
bool ok = MainPanel_foreachProcess((MainPanel*)panel, Affinity_set, (Arg) { .v = affinity2 }, NULL); bool ok = MainPanel_foreachProcess(st->mainPanel, Affinity_set, (Arg) { .v = affinity2 }, NULL);
if (!ok) if (!ok)
beep(); beep();
Affinity_delete(affinity2); Affinity_delete(affinity2);
@ -323,10 +321,10 @@ static Htop_Reaction actionKill(State* st) {
const ListItem* sgn = (ListItem*) Action_pickFromVector(st, signalsPanel, 15, true); const ListItem* sgn = (ListItem*) Action_pickFromVector(st, signalsPanel, 15, true);
if (sgn) { if (sgn) {
if (sgn->key != 0) { if (sgn->key != 0) {
Panel_setHeader(st->panel, "Sending..."); Panel_setHeader((Panel*)st->mainPanel, "Sending...");
Panel_draw(st->panel, false, true, true, State_hideFunctionBar(st)); Panel_draw((Panel*)st->mainPanel, false, true, true, State_hideFunctionBar(st));
refresh(); refresh();
MainPanel_foreachProcess((MainPanel*)st->panel, Process_sendSignal, (Arg) { .i = sgn->key }, NULL); MainPanel_foreachProcess(st->mainPanel, Process_sendSignal, (Arg) { .i = sgn->key }, NULL);
napms(500); napms(500);
} }
} }
@ -354,21 +352,21 @@ static Htop_Reaction actionFilterByUser(State* st) {
} }
Htop_Reaction Action_follow(State* st) { Htop_Reaction Action_follow(State* st) {
st->pl->following = MainPanel_selectedPid((MainPanel*)st->panel); st->pl->following = MainPanel_selectedPid(st->mainPanel);
Panel_setSelectionColor(st->panel, PANEL_SELECTION_FOLLOW); Panel_setSelectionColor((Panel*)st->mainPanel, PANEL_SELECTION_FOLLOW);
return HTOP_KEEP_FOLLOWING; return HTOP_KEEP_FOLLOWING;
} }
static Htop_Reaction actionSetup(State* st) { static Htop_Reaction actionSetup(State* st) {
Action_runSetup(st); Action_runSetup(st);
int headerHeight = Header_calculateHeight(st->header); int headerHeight = Header_calculateHeight(st->header);
Panel_move(st->panel, 0, headerHeight); Panel_move((Panel*)st->mainPanel, 0, headerHeight);
Panel_resize(st->panel, COLS, LINES-headerHeight-1); Panel_resize((Panel*)st->mainPanel, COLS, LINES-headerHeight-1);
return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
} }
static Htop_Reaction actionLsof(State* st) { static Htop_Reaction actionLsof(State* st) {
const Process* p = (Process*) Panel_getSelected(st->panel); const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
@ -381,7 +379,7 @@ static Htop_Reaction actionLsof(State* st) {
} }
static Htop_Reaction actionShowLocks(State* st) { static Htop_Reaction actionShowLocks(State* st) {
const Process* p = (Process*) Panel_getSelected(st->panel); const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) return HTOP_OK; if (!p) return HTOP_OK;
ProcessLocksScreen* pls = ProcessLocksScreen_new(p); ProcessLocksScreen* pls = ProcessLocksScreen_new(p);
InfoScreen_run((InfoScreen*)pls); InfoScreen_run((InfoScreen*)pls);
@ -392,7 +390,7 @@ static Htop_Reaction actionShowLocks(State* st) {
} }
static Htop_Reaction actionStrace(State* st) { static Htop_Reaction actionStrace(State* st) {
const Process* p = (Process*) Panel_getSelected(st->panel); const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
@ -408,12 +406,12 @@ static Htop_Reaction actionStrace(State* st) {
} }
static Htop_Reaction actionTag(State* st) { static Htop_Reaction actionTag(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel); Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
Process_toggleTag(p); Process_toggleTag(p);
Panel_onKey(st->panel, KEY_DOWN); Panel_onKey((Panel*)st->mainPanel, KEY_DOWN);
return HTOP_OK; return HTOP_OK;
} }
@ -582,24 +580,24 @@ static Htop_Reaction actionHelp(State* st) {
} }
static Htop_Reaction actionUntagAll(State* st) { static Htop_Reaction actionUntagAll(State* st) {
for (int i = 0; i < Panel_size(st->panel); i++) { for (int i = 0; i < Panel_size((Panel*)st->mainPanel); i++) {
Process* p = (Process*) Panel_get(st->panel, i); Process* p = (Process*) Panel_get((Panel*)st->mainPanel, i);
p->tag = false; p->tag = false;
} }
return HTOP_REFRESH; return HTOP_REFRESH;
} }
static Htop_Reaction actionTagAllChildren(State* st) { static Htop_Reaction actionTagAllChildren(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel); Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
tagAllChildren(st->panel, p); tagAllChildren((Panel*)st->mainPanel, p);
return HTOP_OK; return HTOP_OK;
} }
static Htop_Reaction actionShowEnvScreen(State* st) { static Htop_Reaction actionShowEnvScreen(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel); Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
@ -612,7 +610,7 @@ static Htop_Reaction actionShowEnvScreen(State* st) {
} }
static Htop_Reaction actionShowCommandScreen(State* st) { static Htop_Reaction actionShowCommandScreen(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel); Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;

View File

@ -20,6 +20,7 @@ in the source distribution for its full text.
#include "Settings.h" #include "Settings.h"
#include "UsersTable.h" #include "UsersTable.h"
typedef enum { typedef enum {
HTOP_OK = 0x00, HTOP_OK = 0x00,
HTOP_REFRESH = 0x01, HTOP_REFRESH = 0x01,
@ -31,11 +32,13 @@ typedef enum {
HTOP_UPDATE_PANELHDR = 0x41, // implies HTOP_REFRESH HTOP_UPDATE_PANELHDR = 0x41, // implies HTOP_REFRESH
} Htop_Reaction; } Htop_Reaction;
struct MainPanel_;
typedef struct State_ { typedef struct State_ {
Settings* settings; Settings* settings;
UsersTable* ut; UsersTable* ut;
ProcessList* pl; ProcessList* pl;
Panel* panel; struct MainPanel_* mainPanel;
Header* header; Header* header;
bool pauseProcessUpdate; bool pauseProcessUpdate;
bool hideProcessSelection; bool hideProcessSelection;

View File

@ -123,7 +123,11 @@ static void ScreenManager_drawPanels(ScreenManager* this, int focus, bool force_
const int nPanels = this->panelCount; const int nPanels = this->panelCount;
for (int i = 0; i < nPanels; i++) { for (int i = 0; i < nPanels; i++) {
Panel* panel = (Panel*) Vector_get(this->panels, i); Panel* panel = (Panel*) Vector_get(this->panels, i);
Panel_draw(panel, force_redraw, i == focus, !((panel == this->state->panel) && this->state->hideProcessSelection), State_hideFunctionBar(this->state)); Panel_draw(panel,
force_redraw,
i == focus,
panel != (Panel*)this->state->mainPanel || !this->state->hideProcessSelection,
State_hideFunctionBar(this->state));
mvvline(panel->y, panel->x + panel->w, ' ', panel->h + (State_hideFunctionBar(this->state) ? 1 : 0)); mvvline(panel->y, panel->x + panel->w, ' ', panel->h + (State_hideFunctionBar(this->state) ? 1 : 0));
} }
} }

5
htop.c
View File

@ -294,9 +294,8 @@ static void millisleep(unsigned long millisec) {
} }
static void setCommFilter(State* state, char** commFilter) { static void setCommFilter(State* state, char** commFilter) {
MainPanel* panel = (MainPanel*)state->panel;
ProcessList* pl = state->pl; ProcessList* pl = state->pl;
IncSet* inc = panel->inc; IncSet* inc = state->mainPanel->inc;
IncSet_setFilter(inc, *commFilter); IncSet_setFilter(inc, *commFilter);
pl->incFilter = IncSet_filter(inc); pl->incFilter = IncSet_filter(inc);
@ -453,7 +452,7 @@ int main(int argc, char** argv) {
.settings = settings, .settings = settings,
.ut = ut, .ut = ut,
.pl = pl, .pl = pl,
.panel = (Panel*) panel, .mainPanel = panel,
.header = header, .header = header,
.pauseProcessUpdate = false, .pauseProcessUpdate = false,
.hideProcessSelection = false, .hideProcessSelection = false,

View File

@ -130,9 +130,7 @@ void Platform_done(void) {
} }
static Htop_Reaction Platform_actionSetIOPriority(State* st) { static Htop_Reaction Platform_actionSetIOPriority(State* st) {
Panel* panel = st->panel; const LinuxProcess* p = (const LinuxProcess*) Panel_getSelected((Panel*)st->mainPanel);
const LinuxProcess* p = (const LinuxProcess*) Panel_getSelected(panel);
if (!p) if (!p)
return HTOP_OK; return HTOP_OK;
@ -141,7 +139,7 @@ static Htop_Reaction Platform_actionSetIOPriority(State* st) {
const void* set = Action_pickFromVector(st, ioprioPanel, 21, true); const void* set = Action_pickFromVector(st, ioprioPanel, 21, true);
if (set) { if (set) {
IOPriority ioprio2 = IOPriorityPanel_getIOPriority(ioprioPanel); IOPriority ioprio2 = IOPriorityPanel_getIOPriority(ioprioPanel);
bool ok = MainPanel_foreachProcess((MainPanel*)panel, LinuxProcess_setIOPriority, (Arg) { .i = ioprio2 }, NULL); bool ok = MainPanel_foreachProcess(st->mainPanel, LinuxProcess_setIOPriority, (Arg) { .i = ioprio2 }, NULL);
if (!ok) { if (!ok) {
beep(); beep();
} }