From ff9409b1737627857eb47f64f536a3f66b6a09a4 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 11 Apr 2016 13:01:07 +0200 Subject: Imported Upstream version 2.0.0 --- ScreenManager.c | 258 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 176 insertions(+), 82 deletions(-) (limited to 'ScreenManager.c') diff --git a/ScreenManager.c b/ScreenManager.c index 646ee2f..a33d342 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -6,9 +6,10 @@ in the source distribution for its full text. */ #include "ScreenManager.h" +#include "ProcessList.h" -#include "Panel.h" #include "Object.h" +#include "CRT.h" #include #include @@ -19,6 +20,8 @@ in the source distribution for its full text. #include "FunctionBar.h" #include "Vector.h" #include "Header.h" +#include "Settings.h" +#include "Panel.h" typedef enum Orientation_ { VERTICAL, @@ -32,30 +35,27 @@ typedef struct ScreenManager_ { int y2; Orientation orientation; Vector* panels; - Vector* fuBars; int panelCount; - const FunctionBar* fuBar; const Header* header; - time_t lastScan; + const Settings* settings; bool owner; bool allowFocusChange; } ScreenManager; }*/ -ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, bool owner) { +ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) { ScreenManager* this; - this = malloc(sizeof(ScreenManager)); + this = xMalloc(sizeof(ScreenManager)); this->x1 = x1; this->y1 = y1; this->x2 = x2; this->y2 = y2; - this->fuBar = NULL; this->orientation = orientation; this->panels = Vector_new(Class(Panel), owner, DEFAULT_SIZE); - this->fuBars = Vector_new(Class(FunctionBar), true, DEFAULT_SIZE); this->panelCount = 0; this->header = header; + this->settings = settings; this->owner = owner; this->allowFocusChange = true; return this; @@ -63,7 +63,6 @@ ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation ori void ScreenManager_delete(ScreenManager* this) { Vector_delete(this->panels); - Vector_delete(this->fuBars); free(this); } @@ -71,27 +70,23 @@ inline int ScreenManager_size(ScreenManager* this) { return this->panelCount; } -void ScreenManager_add(ScreenManager* this, Panel* item, FunctionBar* fuBar, int size) { +void ScreenManager_add(ScreenManager* this, Panel* item, int size) { if (this->orientation == HORIZONTAL) { 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, LINES-this->y1+this->y2); + Panel_resize(item, size, height); } else { - Panel_resize(item, COLS-this->x1+this->x2-lastX, LINES-this->y1+this->y2); + Panel_resize(item, COLS-this->x1+this->x2-lastX, height); } Panel_move(item, lastX, this->y1); } // TODO: VERTICAL Vector_add(this->panels, item); - if (fuBar) - Vector_add(this->fuBars, fuBar); - else - Vector_add(this->fuBars, FunctionBar_new(NULL, NULL, NULL)); - if (!this->fuBar && fuBar) this->fuBar = fuBar; item->needsRedraw = true; this->panelCount++; } @@ -99,8 +94,6 @@ void ScreenManager_add(ScreenManager* this, Panel* item, FunctionBar* fuBar, int Panel* ScreenManager_remove(ScreenManager* this, int idx) { assert(this->panelCount > idx); Panel* panel = (Panel*) Vector_remove(this->panels, idx); - Vector_remove(this->fuBars, idx); - this->fuBar = NULL; this->panelCount--; return panel; } @@ -111,93 +104,191 @@ void ScreenManager_resize(ScreenManager* this, int x1, int y1, int x2, int y2) { this->x2 = x2; this->y2 = y2; int panels = this->panelCount; - 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); + 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); 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); - Panel_move(panel, lastX, y1); + // TODO: VERTICAL +} + +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->treeView) { + 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) { + 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(Panel* panel) { + FunctionBar_draw(panel->currentBar, NULL); + return panel; } void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { bool quit = false; int focus = 0; - Panel* panelFocus = (Panel*) Vector_get(this->panels, focus); - if (this->fuBar) - FunctionBar_draw(this->fuBar, NULL); - - this->lastScan = 0; + Panel* panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); + + double oldTime = 0.0; + + int ch = ERR; + int closeTimeout = 0; + + bool timedOut = true; + bool redraw = true; + bool rescan = false; + int sortTimeout = 0; + int resetSortTimeout = 5; - int ch = 0; while (!quit) { - int panels = this->panelCount; if (this->header) { - time_t now = time(NULL); - if (now > this->lastScan) { - ProcessList_scan(this->header->pl); - ProcessList_sort(this->header->pl); - this->lastScan = now; - } - Header_draw(this->header); - ProcessList_rebuildPanel(this->header->pl, false, false, false, false, NULL); + checkRecalculation(this, &oldTime, &sortTimeout, &redraw, &rescan, &timedOut); } - for (int i = 0; i < panels; i++) { - Panel* panel = (Panel*) Vector_get(this->panels, i); - Panel_draw(panel, i == focus); - if (i < panels) { - if (this->orientation == HORIZONTAL) { - mvvline(panel->y, panel->x+panel->w, ' ', panel->h+1); - } - } + + if (redraw) { + ScreenManager_drawPanels(this, focus); } - FunctionBar* bar = (FunctionBar*) Vector_get(this->fuBars, focus); - if (bar) - this->fuBar = bar; - if (this->fuBar) - FunctionBar_draw(this->fuBar, NULL); + int prevCh = ch; ch = getch(); - + + HandlerResult result = IGNORED; if (ch == KEY_MOUSE) { + ch = ERR; MEVENT mevent; int ok = getmouse(&mevent); if (ok == OK) { - if (mevent.y == LINES - 1) { - ch = FunctionBar_synthesizeEvent(this->fuBar, 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 && - mevent.y > panel->y && mevent.y <= panel->y+panel->h && - (this->allowFocusChange || panelFocus == panel) ) { - focus = i; - panelFocus = panel; - Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); - break; + 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; + } else if (mevent.y > panel->y && mevent.y <= panel->y+panel->h) { + ch = KEY_MOUSE; + if (panel == panelFocus || this->allowFocusChange) { + focus = i; + panelFocus = setCurrentPanel(panel); + Object* oldSelection = Panel_getSelected(panel); + Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); + if (Panel_getSelected(panel) == oldSelection) { + ch = KEY_RECLICK; + } + } + break; + } + } } } + #if NCURSES_MOUSE_VERSION > 1 + } else if (mevent.bstate & BUTTON4_PRESSED) { + ch = KEY_WHEELUP; + } else if (mevent.bstate & BUTTON5_PRESSED) { + ch = KEY_WHEELDOWN; + #endif } } } - - if (Panel_eventHandlerFn(panelFocus)) { - HandlerResult result = Panel_eventHandler(panelFocus, ch); - if (result == HANDLED) { - continue; - } else if (result == BREAK_LOOP) { - quit = true; - continue; + if (ch == ERR) { + sortTimeout--; + if (prevCh == ch && !timedOut) { + closeTimeout++; + if (closeTimeout == 100) { + break; + } + } else + closeTimeout = 0; + redraw = false; + continue; + } + else if (ch == 27) { + int ch2 = getch(); + if (ch2 != ERR) { + switch(ch2) + { + case 'h': + ch = KEY_LEFT; + break; + case 'j': + ch = KEY_DOWN; + break; + case 'k': + ch = KEY_UP; + break; + case 'l': + ch = KEY_RIGHT; + break; + default: + ungetch(ch2); + break; + } } } + redraw = true; + 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) { - case ERR: - continue; case KEY_RESIZE: { ScreenManager_resize(this, this->x1, this->y1, this->x2, this->y2); @@ -210,7 +301,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { tryLeft: if (focus > 0) focus--; - panelFocus = (Panel*) Vector_get(this->panels, focus); + panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); if (Panel_size(panelFocus) == 0 && focus > 0) goto tryLeft; break; @@ -222,7 +313,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { tryRight: if (focus < this->panelCount - 1) focus++; - panelFocus = (Panel*) Vector_get(this->panels, focus); + panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus)); if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1) goto tryRight; break; @@ -232,11 +323,14 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { quit = true; continue; default: + sortTimeout = resetSortTimeout; Panel_onKey(panelFocus, ch); break; } } - *lastFocus = panelFocus; - *lastKey = ch; + if (lastFocus) + *lastFocus = panelFocus; + if (lastKey) + *lastKey = ch; } -- cgit v1.2.3