htop/ScreenManager.c

394 lines
11 KiB
C
Raw Permalink Normal View History

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
2006-03-04 18:16:49 +00:00
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "ScreenManager.h"
#include "ProcessList.h"
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
#include "Object.h"
#include "CRT.h"
2011-12-26 21:35:57 +00:00
2006-03-04 18:16:49 +00:00
#include <assert.h>
#include <time.h>
2018-08-25 01:45:55 +00:00
#include <string.h>
2011-12-26 21:35:57 +00:00
#include <stdlib.h>
2006-03-04 18:16:49 +00:00
#include <stdbool.h>
/*{
2011-12-26 21:35:57 +00:00
#include "FunctionBar.h"
#include "Vector.h"
#include "Header.h"
#include "Settings.h"
#include "Panel.h"
2006-03-04 18:16:49 +00:00
typedef enum Orientation_ {
VERTICAL,
HORIZONTAL
} Orientation;
typedef struct ScreenManager_ {
int x1;
int y1;
int x2;
int y2;
Orientation orientation;
Vector* panels;
2018-08-25 01:45:55 +00:00
const char* name;
int panelCount;
const Header* header;
const Settings* settings;
2006-03-04 18:16:49 +00:00
bool owner;
bool allowFocusChange;
2006-03-04 18:16:49 +00:00
} ScreenManager;
}*/
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) {
2006-03-04 18:16:49 +00:00
ScreenManager* this;
2016-02-02 14:53:02 +00:00
this = xMalloc(sizeof(ScreenManager));
2006-03-04 18:16:49 +00:00
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
this->orientation = orientation;
this->panels = Vector_new(Class(Panel), owner, DEFAULT_SIZE);
this->panelCount = 0;
this->header = header;
this->settings = settings;
2006-03-04 18:16:49 +00:00
this->owner = owner;
this->allowFocusChange = true;
2006-03-04 18:16:49 +00:00
return this;
}
void ScreenManager_delete(ScreenManager* this) {
Vector_delete(this->panels);
2006-03-04 18:16:49 +00:00
free(this);
}
inline int ScreenManager_size(ScreenManager* this) {
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) {
ScreenManager_insert(this, item, size, Vector_size(this->panels));
}
void ScreenManager_insert(ScreenManager* this, Panel* item, int size, int idx) {
2006-03-04 18:16:49 +00:00
if (this->orientation == HORIZONTAL) {
int lastX = 0;
if (idx > 0) {
Panel* last = (Panel*) Vector_get(this->panels, idx - 1);
2006-03-04 18:16:49 +00:00
lastX = last->x + last->w + 1;
}
int height = LINES - this->y1 + this->y2;
if (size <= 0) {
size = COLS-this->x1+this->x2-lastX;
2006-03-04 18:16:49 +00:00
}
Panel_resize(item, size, height);
2006-05-30 13:47:28 +00:00
Panel_move(item, lastX, this->y1);
if (idx < this->panelCount) {
for (int i = idx + 1; i <= this->panelCount; i++) {
Panel* p = (Panel*) Vector_get(this->panels, i);
Panel_move(p, p->x + size, p->y);
}
}
2006-03-04 18:16:49 +00:00
}
// TODO: VERTICAL
Vector_insert(this->panels, idx, item);
2006-03-04 18:16:49 +00:00
item->needsRedraw = true;
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) {
assert(this->panelCount > idx);
int w = ((Panel*) Vector_get(this->panels, idx))->w;
Panel* panel = (Panel*) Vector_remove(this->panels, idx);
this->panelCount--;
if (idx < this->panelCount) {
for (int i = idx; i < this->panelCount; i++) {
Panel* p = (Panel*) Vector_get(this->panels, i);
Panel_move(p, p->x - w, p->y);
}
}
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;
int panels = this->panelCount;
if (this->orientation == HORIZONTAL) {
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);
Panel_move(panel, lastX, y1);
lastX = panel->x + panel->w + 1;
}
Panel* panel = (Panel*) Vector_get(this->panels, panels-1);
Panel_resize(panel, COLS-x1+x2-lastX, LINES-y1+y2);
2006-05-30 14:00:18 +00:00
Panel_move(panel, lastX, y1);
2006-03-04 18:16:49 +00:00
}
// TODO: VERTICAL
2006-03-04 18:16:49 +00:00
}
2015-03-22 05:50:40 +00:00
static void checkRecalculation(ScreenManager* this, double* oldTime, int* sortTimeout, bool* redraw, bool *rescan, bool *timedOut) {
ProcessList* pl = this->header->pl;
struct timeval tv;
gettimeofday(&tv, NULL);
double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
*timedOut = (newTime - *oldTime > this->settings->delay);
*rescan = *rescan || *timedOut;
if (newTime < *oldTime) *rescan = true; // clock was adjusted?
if (*rescan) {
*oldTime = newTime;
ProcessList_scan(pl);
if (*sortTimeout == 0 || this->settings->ss->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;
}
2018-08-25 01:45:55 +00:00
static inline bool drawTab(int* y, int* x, int l, const char* name, bool cur) {
attrset(CRT_colors[cur ? SCREENS_CUR_BORDER : SCREENS_OTH_BORDER]);
mvaddch(*y, *x, '[');
(*x)++;
if (*x >= l) return false;
int nameLen = strlen(name);
int n = MIN(l - *x, nameLen);
attrset(CRT_colors[cur ? SCREENS_CUR_TEXT : SCREENS_OTH_TEXT]);
mvaddnstr(*y, *x, name, n);
*x += n;
if (*x >= l) return false;
attrset(CRT_colors[cur ? SCREENS_CUR_BORDER : SCREENS_OTH_BORDER]);
mvaddch(*y, *x, ']');
*x += 2;
if (*x >= l) return false;
return true;
}
static void ScreenManager_drawScreenTabs(ScreenManager* this) {
ScreenSettings** screens = this->settings->screens;
int cur = this->settings->ssIndex;
int l = COLS;
Panel* panel = (Panel*) Vector_get(this->panels, 0);
int y = panel->y - 1;
int x = 2;
if (this->name) {
drawTab(&y, &x, l, this->name, true);
return;
}
for (int s = 0; screens[s]; s++) {
bool ok = drawTab(&y, &x, l, screens[s]->name, s == cur);
if (!ok) {
break;
}
}
attrset(CRT_colors[RESET_COLOR]);
}
2015-03-22 05:50:40 +00:00
static void ScreenManager_drawPanels(ScreenManager* this, int focus) {
2018-08-25 01:45:55 +00:00
if (this->settings->screenTabs) {
ScreenManager_drawScreenTabs(this);
}
2015-03-22 05:50:40 +00:00
int nPanels = this->panelCount;
for (int i = 0; i < nPanels; i++) {
Panel* panel = (Panel*) Vector_get(this->panels, i);
Panel_draw(panel, i == focus);
if (i < nPanels) {
if (this->orientation == HORIZONTAL) {
mvvline(panel->y, panel->x+panel->w, ' ', panel->h+1);
}
}
}
}
static Panel* setCurrentPanel(ScreenManager* this, int focus) {
Panel* panel = (Panel*) Vector_get(this->panels, focus);
FunctionBar_draw(panel->currentBar, NULL);
return panel;
}
2018-08-25 01:45:55 +00:00
void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey, char* name) {
2006-03-04 18:16:49 +00:00
bool quit = false;
int focus = 0;
Panel* panelFocus = setCurrentPanel(this, focus);
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;
int sortTimeout = 0;
int resetSortTimeout = 5;
2018-08-25 01:45:55 +00:00
this->name = name;
2006-03-04 18:16:49 +00:00
while (!quit) {
if (this->header) {
2015-03-22 05:50:40 +00:00
checkRecalculation(this, &oldTime, &sortTimeout, &redraw, &rescan, &timedOut);
}
2015-03-22 05:50:40 +00:00
if (redraw) {
ScreenManager_drawPanels(this, focus);
2006-03-04 18:16:49 +00:00
}
int prevCh = ch;
ch = Panel_getCh(panelFocus);
2015-03-25 02:12:43 +00:00
HandlerResult result = IGNORED;
2006-03-04 18:16:49 +00:00
if (ch == KEY_MOUSE) {
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) {
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);
if (mevent.x >= panel->x && mevent.x <= panel->x+panel->w) {
if (mevent.y == panel->y) {
ch = EVENT_HEADER_CLICK(mevent.x - panel->x);
break;
2018-08-25 01:45:55 +00:00
} else if (this->settings->screenTabs && mevent.y == panel->y - 1) {
ch = EVENT_SCREEN_TAB_CLICK(mevent.x);
break;
} else if (mevent.y > panel->y && mevent.y <= panel->y+panel->h) {
ch = KEY_MOUSE;
if (panel == panelFocus || this->allowFocusChange) {
focus = i;
panelFocus = setCurrentPanel(this, i);
Object* oldSelection = Panel_getSelected(panel);
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
if (Panel_getSelected(panel) == oldSelection) {
ch = KEY_RECLICK;
}
}
break;
2015-03-25 02:12:43 +00:00
}
}
2006-03-04 18:16:49 +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
}
}
}
if (ch == ERR) {
sortTimeout--;
2015-03-22 05:50:40 +00:00
if (prevCh == ch && !timedOut) {
closeTimeout++;
if (closeTimeout == 100) {
break;
}
} else
closeTimeout = 0;
2015-03-22 05:50:40 +00:00
redraw = false;
2006-03-04 18:16:49 +00:00
continue;
}
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;
}
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;
}
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:
case KEY_CTRL('B'):
if (this->panelCount < 2) {
goto defaultHandler;
}
if (!this->allowFocusChange)
break;
2006-03-04 18:16:49 +00:00
tryLeft:
if (focus > 0)
focus--;
panelFocus = setCurrentPanel(this, focus);
2009-06-02 04:51:23 +00:00
if (Panel_size(panelFocus) == 0 && focus > 0)
2006-03-04 18:16:49 +00:00
goto tryLeft;
break;
case KEY_RIGHT:
case KEY_CTRL('F'):
2006-03-04 18:16:49 +00:00
case 9:
if (this->panelCount < 2) {
goto defaultHandler;
}
if (!this->allowFocusChange)
break;
2006-03-04 18:16:49 +00:00
tryRight:
if (focus < this->panelCount - 1)
2006-03-04 18:16:49 +00:00
focus++;
panelFocus = setCurrentPanel(this, focus);
if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1)
2006-03-04 18:16:49 +00:00
goto tryRight;
break;
case KEY_F(10):
case 'q':
case 27:
quit = true;
continue;
default:
defaultHandler:
sortTimeout = resetSortTimeout;
2006-05-30 14:00:18 +00:00
Panel_onKey(panelFocus, ch);
2006-03-04 18:16:49 +00:00
break;
}
}
if (lastFocus)
*lastFocus = panelFocus;
if (lastKey)
*lastKey = ch;
2006-03-04 18:16:49 +00:00
}