2015-03-16 02:03:26 +00:00
|
|
|
/*
|
|
|
|
htop - ColumnsPanel.c
|
|
|
|
(C) 2004-2015 Hisham H. Muhammad
|
2020-08-19 23:35:24 +00:00
|
|
|
(C) 2020 Red Hat, Inc. All Rights Reserved.
|
2021-09-22 09:33:00 +00:00
|
|
|
Released under the GNU GPLv2+, see the COPYING file
|
2015-03-16 02:03:26 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MainPanel.h"
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <ctype.h>
|
2015-03-16 02:03:26 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "CRT.h"
|
|
|
|
#include "FunctionBar.h"
|
|
|
|
#include "Platform.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "Settings.h"
|
|
|
|
#include "XUtils.h"
|
|
|
|
|
|
|
|
|
2021-01-21 19:27:37 +00:00
|
|
|
static const char* const MainFunctions[] = {"Help ", "Setup ", "Search", "Filter", "Tree ", "SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL};
|
|
|
|
static const char* const MainFunctions_ro[] = {"Help ", "Setup ", "Search", "Filter", "Tree ", "SortBy", " ", " ", " ", "Quit ", NULL};
|
2015-03-23 18:26:56 +00:00
|
|
|
|
2022-03-25 13:51:14 +00:00
|
|
|
void MainPanel_updateLabels(MainPanel* this, bool list, bool filter) {
|
2015-03-23 20:04:53 +00:00
|
|
|
FunctionBar* bar = MainPanel_getFunctionBar(this);
|
2022-03-25 13:51:14 +00:00
|
|
|
FunctionBar_setLabel(bar, KEY_F(5), list ? "List " : "Tree ");
|
|
|
|
FunctionBar_setLabel(bar, KEY_F(4), filter ? "FILTER" : "Filter");
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:50:06 +00:00
|
|
|
static void MainPanel_pidSearch(MainPanel* this, int ch) {
|
2015-03-16 02:03:26 +00:00
|
|
|
Panel* super = (Panel*) this;
|
2020-10-31 22:28:02 +00:00
|
|
|
pid_t pid = ch - 48 + this->pidSearch;
|
2015-03-16 02:03:26 +00:00
|
|
|
for (int i = 0; i < Panel_size(super); i++) {
|
2021-01-05 22:42:55 +00:00
|
|
|
const Process* p = (const Process*) Panel_get(super, i);
|
2015-03-16 02:03:26 +00:00
|
|
|
if (p && p->pid == pid) {
|
|
|
|
Panel_setSelected(super, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->pidSearch = pid * 10;
|
|
|
|
if (this->pidSearch > 10000000) {
|
|
|
|
this->pidSearch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 22:01:30 +00:00
|
|
|
static const char* MainPanel_getValue(Panel* this, int i) {
|
|
|
|
const Process* p = (const Process*) Panel_get(this, i);
|
|
|
|
return Process_getCommand(p);
|
|
|
|
}
|
|
|
|
|
2015-03-16 02:03:26 +00:00
|
|
|
static HandlerResult MainPanel_eventHandler(Panel* super, int ch) {
|
|
|
|
MainPanel* this = (MainPanel*) super;
|
|
|
|
|
|
|
|
HandlerResult result = IGNORED;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2015-03-16 02:03:26 +00:00
|
|
|
Htop_Reaction reaction = HTOP_OK;
|
|
|
|
|
2021-01-02 22:49:04 +00:00
|
|
|
/* Let supervising ScreenManager handle resize */
|
|
|
|
if (ch == KEY_RESIZE)
|
|
|
|
return IGNORED;
|
|
|
|
|
2021-09-28 07:17:19 +00:00
|
|
|
/* reset on every normal key */
|
|
|
|
bool needReset = ch != ERR;
|
|
|
|
#ifdef HAVE_GETMOUSE
|
|
|
|
/* except mouse events while mouse support is disabled */
|
|
|
|
if (!(ch != KEY_MOUSE || this->state->settings->enableMouse))
|
|
|
|
needReset = false;
|
|
|
|
#endif
|
|
|
|
if (needReset)
|
2020-11-23 15:23:18 +00:00
|
|
|
this->state->hideProcessSelection = false;
|
|
|
|
|
2021-08-31 05:38:52 +00:00
|
|
|
Settings* settings = this->state->settings;
|
|
|
|
ScreenSettings* ss = settings->ss;
|
|
|
|
|
2015-03-25 02:12:43 +00:00
|
|
|
if (EVENT_IS_HEADER_CLICK(ch)) {
|
|
|
|
int x = EVENT_HEADER_CLICK_GET_X(ch);
|
2020-11-04 16:46:11 +00:00
|
|
|
const ProcessList* pl = this->state->pl;
|
2015-03-25 02:12:43 +00:00
|
|
|
int hx = super->scrollH + x + 1;
|
|
|
|
ProcessField field = ProcessList_keyAt(pl, hx);
|
2021-08-31 05:38:52 +00:00
|
|
|
if (ss->treeView && ss->treeViewAlwaysByPID) {
|
|
|
|
ss->treeView = false;
|
|
|
|
ss->direction = 1;
|
2020-12-18 14:03:31 +00:00
|
|
|
reaction |= Action_setSortKey(settings, field);
|
2021-08-31 05:38:52 +00:00
|
|
|
} else if (field == ScreenSettings_getActiveSortKey(ss)) {
|
|
|
|
ScreenSettings_invertSortOrder(ss);
|
2015-03-25 02:12:43 +00:00
|
|
|
} else {
|
|
|
|
reaction |= Action_setSortKey(settings, field);
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
reaction |= HTOP_RECALCULATE | HTOP_REDRAW_BAR | HTOP_SAVE_SETTINGS;
|
2015-03-25 02:12:43 +00:00
|
|
|
result = HANDLED;
|
2021-08-31 05:38:52 +00:00
|
|
|
} else if (EVENT_IS_SCREEN_TAB_CLICK(ch)) {
|
|
|
|
int x = EVENT_SCREEN_TAB_GET_X(ch);
|
|
|
|
reaction |= Action_setScreenTab(settings, x);
|
|
|
|
result = HANDLED;
|
2015-03-25 02:12:43 +00:00
|
|
|
} else if (ch != ERR && this->inc->active) {
|
2021-01-05 22:01:30 +00:00
|
|
|
bool filterChanged = IncSet_handleKey(this->inc, ch, super, MainPanel_getValue, NULL);
|
2015-03-23 20:04:53 +00:00
|
|
|
if (filterChanged) {
|
|
|
|
this->state->pl->incFilter = IncSet_filter(this->inc);
|
2015-03-23 01:39:33 +00:00
|
|
|
reaction = HTOP_REFRESH | HTOP_REDRAW_BAR;
|
|
|
|
}
|
2016-07-28 14:37:44 +00:00
|
|
|
if (this->inc->found) {
|
|
|
|
reaction |= Action_follow(this->state);
|
|
|
|
reaction |= HTOP_KEEP_FOLLOWING;
|
|
|
|
}
|
2015-03-23 20:04:53 +00:00
|
|
|
result = HANDLED;
|
|
|
|
} else if (ch == 27) {
|
2020-11-23 15:23:18 +00:00
|
|
|
this->state->hideProcessSelection = true;
|
2015-03-23 20:04:53 +00:00
|
|
|
return HANDLED;
|
2016-01-03 18:31:44 +00:00
|
|
|
} else if (ch != ERR && ch > 0 && ch < KEY_MAX && this->keys[ch]) {
|
2015-03-16 02:03:26 +00:00
|
|
|
reaction |= (this->keys[ch])(this->state);
|
|
|
|
result = HANDLED;
|
2020-09-11 17:27:56 +00:00
|
|
|
} else if (0 < ch && ch < 255 && isdigit((unsigned char)ch)) {
|
2015-03-16 02:03:26 +00:00
|
|
|
MainPanel_pidSearch(this, ch);
|
|
|
|
} else {
|
|
|
|
if (ch != ERR) {
|
|
|
|
this->pidSearch = 0;
|
2015-04-09 00:25:31 +00:00
|
|
|
} else {
|
|
|
|
reaction |= HTOP_KEEP_FOLLOWING;
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reaction & HTOP_REDRAW_BAR) {
|
2022-03-25 13:51:14 +00:00
|
|
|
MainPanel_updateLabels(this, settings->ss->treeView, this->state->pl->incFilter);
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
2021-08-21 18:48:45 +00:00
|
|
|
if (reaction & HTOP_RESIZE) {
|
|
|
|
result |= RESIZE;
|
|
|
|
}
|
2015-03-16 02:03:26 +00:00
|
|
|
if (reaction & HTOP_UPDATE_PANELHDR) {
|
2020-12-22 16:12:38 +00:00
|
|
|
result |= REDRAW;
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
|
|
|
if (reaction & HTOP_REFRESH) {
|
2020-12-16 20:46:11 +00:00
|
|
|
result |= REFRESH;
|
2019-10-31 16:39:12 +00:00
|
|
|
}
|
2015-03-16 02:03:26 +00:00
|
|
|
if (reaction & HTOP_RECALCULATE) {
|
2015-03-22 05:50:40 +00:00
|
|
|
result |= RESCAN;
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
|
|
|
if (reaction & HTOP_SAVE_SETTINGS) {
|
|
|
|
this->state->settings->changed = true;
|
|
|
|
}
|
|
|
|
if (reaction & HTOP_QUIT) {
|
|
|
|
return BREAK_LOOP;
|
|
|
|
}
|
|
|
|
if (!(reaction & HTOP_KEEP_FOLLOWING)) {
|
|
|
|
this->state->pl->following = -1;
|
2020-12-16 20:46:11 +00:00
|
|
|
Panel_setSelectionColor(super, PANEL_SELECTION_FOCUS);
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainPanel_selectedPid(MainPanel* this) {
|
2021-01-05 22:42:55 +00:00
|
|
|
const Process* p = (const Process*) Panel_getSelected((Panel*)this);
|
2015-03-16 02:03:26 +00:00
|
|
|
if (p) {
|
|
|
|
return p->pid;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-18 13:38:49 +00:00
|
|
|
bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, Arg arg, bool* wasAnyTagged) {
|
2015-03-16 02:03:26 +00:00
|
|
|
Panel* super = (Panel*) this;
|
|
|
|
bool ok = true;
|
|
|
|
bool anyTagged = false;
|
|
|
|
for (int i = 0; i < Panel_size(super); i++) {
|
|
|
|
Process* p = (Process*) Panel_get(super, i);
|
|
|
|
if (p->tag) {
|
|
|
|
ok = fn(p, arg) && ok;
|
|
|
|
anyTagged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!anyTagged) {
|
|
|
|
Process* p = (Process*) Panel_getSelected(super);
|
2020-10-31 22:28:02 +00:00
|
|
|
if (p) {
|
|
|
|
ok &= fn(p, arg);
|
|
|
|
}
|
2015-03-16 02:03:26 +00:00
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2015-03-16 02:03:26 +00:00
|
|
|
if (wasAnyTagged)
|
|
|
|
*wasAnyTagged = anyTagged;
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2015-03-16 02:03:26 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:26:14 +00:00
|
|
|
static void MainPanel_drawFunctionBar(Panel* super, bool hideFunctionBar) {
|
2020-12-16 20:46:11 +00:00
|
|
|
MainPanel* this = (MainPanel*) super;
|
2020-12-28 22:26:14 +00:00
|
|
|
|
|
|
|
// Do not hide active search and filter bar.
|
|
|
|
if (hideFunctionBar && !this->inc->active)
|
|
|
|
return;
|
|
|
|
|
2021-08-31 05:38:52 +00:00
|
|
|
IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
|
2020-12-16 20:46:11 +00:00
|
|
|
if (this->state->pauseProcessUpdate) {
|
|
|
|
FunctionBar_append("PAUSED", CRT_colors[PAUSED]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:12:38 +00:00
|
|
|
static void MainPanel_printHeader(Panel* super) {
|
|
|
|
MainPanel* this = (MainPanel*) super;
|
|
|
|
ProcessList_printHeader(this->state->pl, &super->header);
|
|
|
|
}
|
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
const PanelClass MainPanel_class = {
|
2015-03-16 02:03:26 +00:00
|
|
|
.super = {
|
|
|
|
.extends = Class(Panel),
|
|
|
|
.delete = MainPanel_delete
|
|
|
|
},
|
2020-12-16 20:46:11 +00:00
|
|
|
.eventHandler = MainPanel_eventHandler,
|
2020-12-22 16:12:38 +00:00
|
|
|
.drawFunctionBar = MainPanel_drawFunctionBar,
|
|
|
|
.printHeader = MainPanel_printHeader
|
2015-03-16 02:03:26 +00:00
|
|
|
};
|
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
MainPanel* MainPanel_new() {
|
2015-03-16 02:03:26 +00:00
|
|
|
MainPanel* this = AllocThis(MainPanel);
|
2021-01-21 19:27:37 +00:00
|
|
|
Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(Settings_isReadonly() ? MainFunctions_ro : MainFunctions, NULL, NULL));
|
2016-02-02 14:53:02 +00:00
|
|
|
this->keys = xCalloc(KEY_MAX, sizeof(Htop_Action));
|
2015-03-23 20:04:53 +00:00
|
|
|
this->inc = IncSet_new(MainPanel_getFunctionBar(this));
|
2015-03-16 02:03:26 +00:00
|
|
|
|
|
|
|
Action_setBindings(this->keys);
|
|
|
|
Platform_setBindings(this->keys);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainPanel_setState(MainPanel* this, State* state) {
|
|
|
|
this->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainPanel_delete(Object* object) {
|
|
|
|
Panel* super = (Panel*) object;
|
|
|
|
MainPanel* this = (MainPanel*) object;
|
|
|
|
Panel_done(super);
|
2015-03-23 01:39:33 +00:00
|
|
|
IncSet_delete(this->inc);
|
2015-03-16 02:03:26 +00:00
|
|
|
free(this->keys);
|
|
|
|
free(this);
|
|
|
|
}
|