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"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
#include "Panel.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "Object.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <assert.h>
|
2011-03-22 20:37:08 +00:00
|
|
|
#include <time.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"
|
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;
|
2011-12-01 12:31:57 +00:00
|
|
|
Vector* panels;
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector* fuBars;
|
2011-12-01 12:31:57 +00:00
|
|
|
int panelCount;
|
2011-03-22 20:37:08 +00:00
|
|
|
const FunctionBar* fuBar;
|
|
|
|
const Header* header;
|
|
|
|
time_t lastScan;
|
2006-03-04 18:16:49 +00:00
|
|
|
bool owner;
|
2011-12-01 12:31:57 +00:00
|
|
|
bool allowFocusChange;
|
2006-03-04 18:16:49 +00:00
|
|
|
} ScreenManager;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, bool owner) {
|
2006-03-04 18:16:49 +00:00
|
|
|
ScreenManager* this;
|
|
|
|
this = malloc(sizeof(ScreenManager));
|
|
|
|
this->x1 = x1;
|
|
|
|
this->y1 = y1;
|
|
|
|
this->x2 = x2;
|
|
|
|
this->y2 = y2;
|
|
|
|
this->fuBar = NULL;
|
|
|
|
this->orientation = orientation;
|
2012-12-05 15:12:20 +00:00
|
|
|
this->panels = Vector_new(Class(Panel), owner, DEFAULT_SIZE);
|
|
|
|
this->fuBars = Vector_new(Class(FunctionBar), true, DEFAULT_SIZE);
|
2011-12-01 12:31:57 +00:00
|
|
|
this->panelCount = 0;
|
2011-03-22 20:37:08 +00:00
|
|
|
this->header = header;
|
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-05-30 13:45:40 +00:00
|
|
|
Vector_delete(this->fuBars);
|
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
|
|
|
}
|
|
|
|
|
2006-05-30 13:47:28 +00:00
|
|
|
void ScreenManager_add(ScreenManager* this, Panel* item, FunctionBar* fuBar, int size) {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->orientation == HORIZONTAL) {
|
|
|
|
int lastX = 0;
|
2011-12-01 12:31:57 +00:00
|
|
|
if (this->panelCount > 0) {
|
|
|
|
Panel* last = (Panel*) Vector_get(this->panels, this->panelCount - 1);
|
2006-03-04 18:16:49 +00:00
|
|
|
lastX = last->x + last->w + 1;
|
|
|
|
}
|
|
|
|
if (size > 0) {
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel_resize(item, size, LINES-this->y1+this->y2);
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel_resize(item, COLS-this->x1+this->x2-lastX, LINES-this->y1+this->y2);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel_move(item, lastX, this->y1);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
// TODO: VERTICAL
|
2011-12-01 12:31:57 +00:00
|
|
|
Vector_add(this->panels, item);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (fuBar)
|
2006-05-30 13:45:40 +00:00
|
|
|
Vector_add(this->fuBars, fuBar);
|
2006-03-04 18:16:49 +00:00
|
|
|
else
|
2009-06-02 04:51:23 +00:00
|
|
|
Vector_add(this->fuBars, FunctionBar_new(NULL, NULL, NULL));
|
2006-03-04 18:16:49 +00:00
|
|
|
if (!this->fuBar && fuBar) this->fuBar = fuBar;
|
|
|
|
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);
|
2010-02-25 01:43:18 +00:00
|
|
|
Vector_remove(this->fuBars, idx);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->fuBar = NULL;
|
2011-12-01 12:31:57 +00:00
|
|
|
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;
|
2006-03-04 18:16:49 +00:00
|
|
|
int lastX = 0;
|
2011-12-01 12:31:57 +00:00
|
|
|
for (int i = 0; i < panels - 1; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_resize(panel, panel->w, LINES-y1+y2);
|
|
|
|
Panel_move(panel, lastX, y1);
|
|
|
|
lastX = panel->x + panel->w + 1;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2011-12-01 12:31:57 +00:00
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, panels-1);
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_resize(panel, COLS-x1+x2-lastX, LINES-y1+y2);
|
|
|
|
Panel_move(panel, lastX, y1);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2011-03-22 20:37:08 +00:00
|
|
|
|
2011-12-01 12:31:57 +00:00
|
|
|
Panel* panelFocus = (Panel*) Vector_get(this->panels, focus);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->fuBar)
|
|
|
|
FunctionBar_draw(this->fuBar, NULL);
|
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
this->lastScan = 0;
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
int ch = 0;
|
2006-03-04 18:16:49 +00:00
|
|
|
while (!quit) {
|
2011-12-01 12:31:57 +00:00
|
|
|
int panels = this->panelCount;
|
2011-03-22 20:37:08 +00:00
|
|
|
if (this->header) {
|
|
|
|
time_t now = time(NULL);
|
|
|
|
if (now > this->lastScan) {
|
|
|
|
ProcessList_scan(this->header->pl);
|
2011-12-01 12:31:57 +00:00
|
|
|
ProcessList_sort(this->header->pl);
|
2011-03-22 20:37:08 +00:00
|
|
|
this->lastScan = now;
|
|
|
|
}
|
|
|
|
Header_draw(this->header);
|
2014-11-20 01:17:52 +00:00
|
|
|
ProcessList_rebuildPanel(this->header->pl, false, false, NULL);
|
2011-03-22 20:37:08 +00:00
|
|
|
}
|
2011-12-01 12:31:57 +00:00
|
|
|
for (int i = 0; i < panels; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_draw(panel, i == focus);
|
2011-12-01 12:31:57 +00:00
|
|
|
if (i < panels) {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (this->orientation == HORIZONTAL) {
|
2006-05-30 14:00:18 +00:00
|
|
|
mvvline(panel->y, panel->x+panel->w, ' ', panel->h+1);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-30 13:45:40 +00:00
|
|
|
FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (bar)
|
|
|
|
this->fuBar = bar;
|
|
|
|
if (this->fuBar)
|
|
|
|
FunctionBar_draw(this->fuBar, NULL);
|
|
|
|
|
|
|
|
ch = getch();
|
|
|
|
|
|
|
|
if (ch == KEY_MOUSE) {
|
|
|
|
MEVENT mevent;
|
|
|
|
int ok = getmouse(&mevent);
|
|
|
|
if (ok == OK) {
|
|
|
|
if (mevent.y == LINES - 1) {
|
|
|
|
ch = FunctionBar_synthesizeEvent(this->fuBar, mevent.x);
|
|
|
|
} else {
|
2011-12-01 12:31:57 +00:00
|
|
|
for (int i = 0; i < this->panelCount; i++) {
|
|
|
|
Panel* panel = (Panel*) Vector_get(this->panels, i);
|
2006-05-30 14:00:18 +00:00
|
|
|
if (mevent.x > panel->x && mevent.x <= panel->x+panel->w &&
|
2013-02-26 20:24:27 +00:00
|
|
|
mevent.y > panel->y && mevent.y <= panel->y+panel->h &&
|
|
|
|
(this->allowFocusChange || panelFocus == panel) ) {
|
2006-03-04 18:16:49 +00:00
|
|
|
focus = i;
|
2006-05-30 14:00:18 +00:00
|
|
|
panelFocus = panel;
|
|
|
|
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
if (Panel_eventHandlerFn(panelFocus)) {
|
|
|
|
HandlerResult result = Panel_eventHandler(panelFocus, ch);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (result == HANDLED) {
|
|
|
|
continue;
|
|
|
|
} else if (result == BREAK_LOOP) {
|
|
|
|
quit = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case ERR:
|
|
|
|
continue;
|
|
|
|
case KEY_RESIZE:
|
|
|
|
{
|
|
|
|
ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case KEY_LEFT:
|
2010-03-03 21:13:33 +00:00
|
|
|
case KEY_CTRLB:
|
2011-12-01 12:31:57 +00:00
|
|
|
if (!this->allowFocusChange)
|
|
|
|
break;
|
2006-03-04 18:16:49 +00:00
|
|
|
tryLeft:
|
|
|
|
if (focus > 0)
|
|
|
|
focus--;
|
2011-12-01 12:31:57 +00:00
|
|
|
panelFocus = (Panel*) Vector_get(this->panels, 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:
|
2010-03-03 21:13:33 +00:00
|
|
|
case KEY_CTRLF:
|
2006-03-04 18:16:49 +00:00
|
|
|
case 9:
|
2011-12-01 12:31:57 +00:00
|
|
|
if (!this->allowFocusChange)
|
|
|
|
break;
|
2006-03-04 18:16:49 +00:00
|
|
|
tryRight:
|
2011-12-01 12:31:57 +00:00
|
|
|
if (focus < this->panelCount - 1)
|
2006-03-04 18:16:49 +00:00
|
|
|
focus++;
|
2011-12-01 12:31:57 +00:00
|
|
|
panelFocus = (Panel*) Vector_get(this->panels, 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:
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_onKey(panelFocus, ch);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-30 14:00:18 +00:00
|
|
|
*lastFocus = panelFocus;
|
2006-03-04 18:16:49 +00:00
|
|
|
*lastKey = ch;
|
|
|
|
}
|