2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - ScreenManager.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ScreenManager.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2015-08-19 21:55:24 +00:00
|
|
|
#include "CRT.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "FunctionBar.h"
|
2020-10-05 13:14:54 +00:00
|
|
|
#include "Object.h"
|
|
|
|
#include "ProcessList.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "XUtils.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2020-11-26 05:15:09 +00:00
|
|
|
ScreenManager* ScreenManager_new(Header* header, const Settings* settings, const State* state, bool owner) {
|
2006-03-04 18:16:49 +00:00
|
|
|
ScreenManager* this;
|
2016-02-02 14:53:02 +00:00
|
|
|
this = xMalloc(sizeof(ScreenManager));
|
2020-11-26 05:15:09 +00:00
|
|
|
this->x1 = 0;
|
|
|
|
this->y1 = header->height;
|
|
|
|
this->x2 = 0;
|
|
|
|
this->y2 = -1;
|
2012-12-05 15:12:20 +00:00
|
|
|
this->panels = Vector_new(Class(Panel), owner, DEFAULT_SIZE);
|
2011-12-01 12:31:57 +00:00
|
|
|
this->panelCount = 0;
|
2011-03-22 20:37:08 +00:00
|
|
|
this->header = header;
|
2015-01-22 01:27:31 +00:00
|
|
|
this->settings = settings;
|
2020-10-05 13:14:54 +00:00
|
|
|
this->state = state;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->owner = owner;
|
2011-12-01 12:31:57 +00:00
|
|
|
this->allowFocusChange = true;
|
2006-03-04 18:16:49 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenManager_delete(ScreenManager* this) {
|
2011-12-01 12:31:57 +00:00
|
|
|
Vector_delete(this->panels);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int ScreenManager_size(ScreenManager* this) {
|
2011-12-01 12:31:57 +00:00
|
|
|
return this->panelCount;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
void ScreenManager_add(ScreenManager* this, Panel* item, int size) {
|
2020-11-26 05:15:09 +00:00
|
|
|
int lastX = 0;
|
|
|
|
if (this->panelCount > 0) {
|
|
|
|
Panel* last = (Panel*) Vector_get(this->panels, this->panelCount - 1);
|
|
|
|
lastX = last->x + last->w + 1;
|
|
|
|
}
|
|
|
|
int height = LINES - this->y1 + this->y2;
|
|
|
|
if (size > 0) {
|
|
|
|
Panel_resize(item, size, height);
|
|
|
|
} else {
|
|
|
|
Panel_resize(item, COLS - this->x1 + this->x2 - lastX, height);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2020-11-26 05:15:09 +00:00
|
|
|
Panel_move(item, lastX, this->y1);
|
2011-12-01 12:31:57 +00:00
|
|
|
Vector_add(this->panels, item);
|
2006-03-04 18:16:49 +00:00
|
|
|
item->needsRedraw = true;
|
2011-12-01 12:31:57 +00:00
|
|
|
this->panelCount++;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
Panel* ScreenManager_remove(ScreenManager* this, int idx) {
|
2011-12-01 12:31:57 +00:00
|
|
|
assert(this->panelCount > idx);
|
|
|
|
Panel* panel = (Panel*) Vector_remove(this->panels, idx);
|
|
|
|
this->panelCount--;
|
2006-05-30 14:00:18 +00:00
|
|
|
return panel;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenManager_resize(ScreenManager* this, int x1, int y1, int x2, int y2) {
|
|
|
|
this->x1 = x1;
|
|
|
|
this->y1 = y1;
|
|
|
|
this->x2 = x2;
|
|
|
|
this->y2 = y2;
|
2011-12-01 12:31:57 +00:00
|
|
|
int panels = this->panelCount;
|
2020-11-26 05:15:09 +00:00
|
|
|
int lastX = 0;
|
|
|
|
for (int i = 0; i < panels - 1; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
|
|
|
Panel_resize(panel, panel->w, LINES - y1 + y2);
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_move(panel, lastX, y1);
|
2020-11-26 05:15:09 +00:00
|
|
|
lastX = panel->x + panel->w + 1;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2020-11-26 05:15:09 +00:00
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, panels - 1);
|
|
|
|
Panel_resize(panel, COLS - x1 + x2 - lastX, LINES - y1 + y2);
|
|
|
|
Panel_move(panel, lastX, y1);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
static void checkRecalculation(ScreenManager* this, double* oldTime, int* sortTimeout, bool* redraw, bool* rescan, bool* timedOut) {
|
2015-03-22 05:50:40 +00:00
|
|
|
ProcessList* pl = this->header->pl;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
|
2020-10-31 01:56:16 +00:00
|
|
|
|
2015-03-22 05:50:40 +00:00
|
|
|
*timedOut = (newTime - *oldTime > this->settings->delay);
|
2020-11-01 00:09:51 +00:00
|
|
|
*rescan |= *timedOut;
|
|
|
|
|
|
|
|
if (newTime < *oldTime) {
|
|
|
|
*rescan = true; // clock was adjusted?
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:03:37 +00:00
|
|
|
if (*rescan) {
|
2015-03-22 05:50:40 +00:00
|
|
|
*oldTime = newTime;
|
2020-10-13 14:03:37 +00:00
|
|
|
ProcessList_scan(pl, this->state->pauseProcessUpdate);
|
2020-12-09 10:43:53 +00:00
|
|
|
if (!this->state->pauseProcessUpdate && (*sortTimeout == 0 || this->settings->treeView)) {
|
2015-03-22 05:50:40 +00:00
|
|
|
ProcessList_sort(pl);
|
|
|
|
*sortTimeout = 1;
|
|
|
|
}
|
|
|
|
*redraw = true;
|
|
|
|
}
|
|
|
|
if (*redraw) {
|
|
|
|
ProcessList_rebuildPanel(pl);
|
|
|
|
Header_draw(this->header);
|
|
|
|
}
|
|
|
|
*rescan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ScreenManager_drawPanels(ScreenManager* this, int focus) {
|
2018-10-13 18:04:59 +00:00
|
|
|
const int nPanels = this->panelCount;
|
2015-03-22 05:50:40 +00:00
|
|
|
for (int i = 0; i < nPanels; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
2020-11-23 15:23:18 +00:00
|
|
|
Panel_draw(panel, i == focus, !((panel == this->state->panel) && this->state->hideProcessSelection));
|
2020-11-26 05:15:09 +00:00
|
|
|
mvvline(panel->y, panel->x + panel->w, ' ', panel->h + 1);
|
2015-03-22 05:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
static Panel* setCurrentPanel(const ScreenManager* this, Panel* panel) {
|
|
|
|
FunctionBar_draw(panel->currentBar);
|
2020-11-01 00:09:51 +00:00
|
|
|
if (panel == this->state->panel && this->state->pauseProcessUpdate) {
|
2020-10-05 13:14:54 +00:00
|
|
|
FunctionBar_append("PAUSED", CRT_colors[PAUSED]);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-10-05 13:14:54 +00:00
|
|
|
|
2015-03-23 20:04:53 +00:00
|
|
|
return panel;
|
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
|
2006-03-04 18:16:49 +00:00
|
|
|
bool quit = false;
|
|
|
|
int focus = 0;
|
2019-07-12 19:41:09 +00:00
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
Panel* panelFocus = setCurrentPanel(this, (Panel*) Vector_get(this->panels, focus));
|
2011-03-22 20:37:08 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
double oldTime = 0.0;
|
|
|
|
|
|
|
|
int ch = ERR;
|
|
|
|
int closeTimeout = 0;
|
|
|
|
|
2015-03-22 05:50:40 +00:00
|
|
|
bool timedOut = true;
|
|
|
|
bool redraw = true;
|
|
|
|
bool rescan = false;
|
2015-01-22 01:27:31 +00:00
|
|
|
int sortTimeout = 0;
|
|
|
|
int resetSortTimeout = 5;
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
while (!quit) {
|
2011-03-22 20:37:08 +00:00
|
|
|
if (this->header) {
|
2015-03-22 05:50:40 +00:00
|
|
|
checkRecalculation(this, &oldTime, &sortTimeout, &redraw, &rescan, &timedOut);
|
2011-03-22 20:37:08 +00:00
|
|
|
}
|
2019-07-12 19:41:09 +00:00
|
|
|
|
2015-03-22 05:50:40 +00:00
|
|
|
if (redraw) {
|
|
|
|
ScreenManager_drawPanels(this, focus);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
int prevCh = ch;
|
2016-05-19 19:09:47 +00:00
|
|
|
set_escdelay(25);
|
2006-03-04 18:16:49 +00:00
|
|
|
ch = getch();
|
2015-01-23 05:08:21 +00:00
|
|
|
|
2015-03-25 02:12:43 +00:00
|
|
|
HandlerResult result = IGNORED;
|
2019-07-12 19:41:09 +00:00
|
|
|
if (ch == KEY_MOUSE && this->settings->enableMouse) {
|
2015-03-25 02:12:43 +00:00
|
|
|
ch = ERR;
|
2006-03-04 18:16:49 +00:00
|
|
|
MEVENT mevent;
|
|
|
|
int ok = getmouse(&mevent);
|
|
|
|
if (ok == OK) {
|
2015-08-19 21:55:24 +00:00
|
|
|
if (mevent.bstate & BUTTON1_RELEASED) {
|
|
|
|
if (mevent.y == LINES - 1) {
|
|
|
|
ch = FunctionBar_synthesizeEvent(panelFocus->currentBar, mevent.x);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < this->panelCount; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
2020-10-31 22:28:02 +00:00
|
|
|
if (mevent.x >= panel->x && mevent.x <= panel->x + panel->w) {
|
2015-08-19 21:55:24 +00:00
|
|
|
if (mevent.y == panel->y) {
|
|
|
|
ch = EVENT_HEADER_CLICK(mevent.x - panel->x);
|
|
|
|
break;
|
2020-10-31 22:28:02 +00:00
|
|
|
} else if (mevent.y > panel->y && mevent.y <= panel->y + panel->h) {
|
2015-08-19 22:09:54 +00:00
|
|
|
ch = KEY_MOUSE;
|
2015-08-19 21:55:24 +00:00
|
|
|
if (panel == panelFocus || this->allowFocusChange) {
|
|
|
|
focus = i;
|
2020-10-05 13:14:54 +00:00
|
|
|
panelFocus = setCurrentPanel(this, panel);
|
2015-08-19 22:09:54 +00:00
|
|
|
Object* oldSelection = Panel_getSelected(panel);
|
2015-08-19 21:55:24 +00:00
|
|
|
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
|
2015-08-19 22:09:54 +00:00
|
|
|
if (Panel_getSelected(panel) == oldSelection) {
|
|
|
|
ch = KEY_RECLICK;
|
|
|
|
}
|
2015-08-19 21:55:24 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-03-25 02:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 21:55:24 +00:00
|
|
|
#if NCURSES_MOUSE_VERSION > 1
|
|
|
|
} else if (mevent.bstate & BUTTON4_PRESSED) {
|
|
|
|
ch = KEY_WHEELUP;
|
|
|
|
} else if (mevent.bstate & BUTTON5_PRESSED) {
|
|
|
|
ch = KEY_WHEELDOWN;
|
|
|
|
#endif
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 01:27:31 +00:00
|
|
|
if (ch == ERR) {
|
2015-01-23 05:08:21 +00:00
|
|
|
sortTimeout--;
|
2015-03-22 05:50:40 +00:00
|
|
|
if (prevCh == ch && !timedOut) {
|
2015-01-22 01:27:31 +00:00
|
|
|
closeTimeout++;
|
|
|
|
if (closeTimeout == 100) {
|
|
|
|
break;
|
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2015-01-22 01:27:31 +00:00
|
|
|
closeTimeout = 0;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2015-03-22 05:50:40 +00:00
|
|
|
redraw = false;
|
2006-03-04 18:16:49 +00:00
|
|
|
continue;
|
2015-01-22 01:27:31 +00:00
|
|
|
}
|
2016-02-19 15:38:02 +00:00
|
|
|
switch (ch) {
|
|
|
|
case KEY_ALT('H'): ch = KEY_LEFT; break;
|
|
|
|
case KEY_ALT('J'): ch = KEY_DOWN; break;
|
|
|
|
case KEY_ALT('K'): ch = KEY_UP; break;
|
|
|
|
case KEY_ALT('L'): ch = KEY_RIGHT; break;
|
2016-01-11 21:36:39 +00:00
|
|
|
}
|
2015-03-22 05:50:40 +00:00
|
|
|
redraw = true;
|
2015-03-25 02:12:43 +00:00
|
|
|
if (Panel_eventHandlerFn(panelFocus)) {
|
|
|
|
result = Panel_eventHandler(panelFocus, ch);
|
|
|
|
}
|
|
|
|
if (result & SYNTH_KEY) {
|
|
|
|
ch = result >> 16;
|
|
|
|
}
|
|
|
|
if (result & REDRAW) {
|
|
|
|
sortTimeout = 0;
|
|
|
|
}
|
|
|
|
if (result & RESCAN) {
|
|
|
|
rescan = true;
|
|
|
|
sortTimeout = 0;
|
|
|
|
}
|
|
|
|
if (result & HANDLED) {
|
|
|
|
continue;
|
|
|
|
} else if (result & BREAK_LOOP) {
|
|
|
|
quit = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-12 19:41:09 +00:00
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
switch (ch) {
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_RESIZE:
|
|
|
|
{
|
|
|
|
ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case KEY_LEFT:
|
2016-06-15 15:45:23 +00:00
|
|
|
case KEY_CTRL('B'):
|
|
|
|
if (this->panelCount < 2) {
|
|
|
|
goto defaultHandler;
|
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
|
|
|
|
if (!this->allowFocusChange) {
|
2011-12-01 12:31:57 +00:00
|
|
|
break;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tryLeft:
|
|
|
|
if (focus > 0) {
|
2006-03-04 18:16:49 +00:00
|
|
|
focus--;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
panelFocus = setCurrentPanel(this, (Panel*) Vector_get(this->panels, focus));
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Panel_size(panelFocus) == 0 && focus > 0) {
|
2006-03-04 18:16:49 +00:00
|
|
|
goto tryLeft;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
2016-06-15 15:45:23 +00:00
|
|
|
case KEY_CTRL('F'):
|
2006-03-04 18:16:49 +00:00
|
|
|
case 9:
|
2016-06-15 15:45:23 +00:00
|
|
|
if (this->panelCount < 2) {
|
|
|
|
goto defaultHandler;
|
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
if (!this->allowFocusChange) {
|
2011-12-01 12:31:57 +00:00
|
|
|
break;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tryRight:
|
|
|
|
if (focus < this->panelCount - 1) {
|
2006-03-04 18:16:49 +00:00
|
|
|
focus++;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
panelFocus = setCurrentPanel(this, (Panel*) Vector_get(this->panels, focus));
|
2020-11-01 00:09:51 +00:00
|
|
|
if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1) {
|
2006-03-04 18:16:49 +00:00
|
|
|
goto tryRight;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
case 27:
|
2020-11-21 15:59:38 +00:00
|
|
|
case 'q':
|
|
|
|
case KEY_F(10):
|
2006-03-04 18:16:49 +00:00
|
|
|
quit = true;
|
|
|
|
continue;
|
|
|
|
default:
|
2020-11-01 00:09:51 +00:00
|
|
|
defaultHandler:
|
2015-01-23 05:08:21 +00:00
|
|
|
sortTimeout = resetSortTimeout;
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_onKey(panelFocus, ch);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 00:09:51 +00:00
|
|
|
if (lastFocus) {
|
2015-01-22 01:27:31 +00:00
|
|
|
*lastFocus = panelFocus;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastKey) {
|
2015-01-22 01:27:31 +00:00
|
|
|
*lastKey = ch;
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|