2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - FunctionBar.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.
|
|
|
|
*/
|
|
|
|
|
2020-10-03 19:20:43 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "FunctionBar.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "Macros.h"
|
|
|
|
#include "ProvideCurses.h"
|
|
|
|
#include "XUtils.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const FunctionBar_FKeys[] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", NULL};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const FunctionBar_FLabels[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", NULL};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2009-06-02 04:51:23 +00:00
|
|
|
static int FunctionBar_FEvents[] = {KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10)};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const FunctionBar_EnterEscKeys[] = {"Enter", "Esc", NULL};
|
|
|
|
static const int FunctionBar_EnterEscEvents[] = {13, 27};
|
2015-03-23 22:24:34 +00:00
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
static int currentLen = 0;
|
|
|
|
|
2015-03-23 22:24:34 +00:00
|
|
|
FunctionBar* FunctionBar_newEnterEsc(const char* enter, const char* esc) {
|
|
|
|
const char* functions[] = {enter, esc, NULL};
|
|
|
|
return FunctionBar_new(functions, FunctionBar_EnterEscKeys, FunctionBar_EnterEscEvents);
|
|
|
|
}
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
FunctionBar* FunctionBar_new(const char* const* functions, const char* const* keys, const int* events) {
|
2016-02-02 14:53:02 +00:00
|
|
|
FunctionBar* this = xCalloc(1, sizeof(FunctionBar));
|
|
|
|
this->functions = xCalloc(16, sizeof(char*));
|
2014-04-09 21:02:50 +00:00
|
|
|
if (!functions) {
|
|
|
|
functions = FunctionBar_FLabels;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 15 && functions[i]; i++) {
|
2016-02-02 14:53:02 +00:00
|
|
|
this->functions[i] = xStrdup(functions[i]);
|
2014-04-09 21:02:50 +00:00
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
if (keys && events) {
|
2019-10-31 16:39:12 +00:00
|
|
|
this->staticData = false;
|
2020-10-04 12:30:35 +00:00
|
|
|
this->keys.keys = xCalloc(15, sizeof(char*));
|
2016-02-02 14:53:02 +00:00
|
|
|
this->events = xCalloc(15, sizeof(int));
|
2010-03-26 23:03:13 +00:00
|
|
|
int i = 0;
|
|
|
|
while (i < 15 && functions[i]) {
|
2020-10-04 12:30:35 +00:00
|
|
|
this->keys.keys[i] = xStrdup(keys[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->events[i] = events[i];
|
2010-03-26 23:03:13 +00:00
|
|
|
i++;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2010-03-26 23:03:13 +00:00
|
|
|
this->size = i;
|
2006-03-04 18:16:49 +00:00
|
|
|
} else {
|
|
|
|
this->staticData = true;
|
2020-10-04 12:30:35 +00:00
|
|
|
this->keys.constKeys = FunctionBar_FKeys;
|
2006-03-04 18:16:49 +00:00
|
|
|
this->events = FunctionBar_FEvents;
|
2020-10-05 13:14:54 +00:00
|
|
|
this->size = ARRAYSIZE(FunctionBar_FEvents);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
void FunctionBar_delete(FunctionBar* this) {
|
2014-04-09 21:02:50 +00:00
|
|
|
for (int i = 0; i < 15 && this->functions[i]; i++) {
|
|
|
|
free(this->functions[i]);
|
|
|
|
}
|
|
|
|
free(this->functions);
|
2006-03-04 18:16:49 +00:00
|
|
|
if (!this->staticData) {
|
|
|
|
for (int i = 0; i < this->size; i++) {
|
2020-10-04 12:30:35 +00:00
|
|
|
free(this->keys.keys[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2020-10-04 12:30:35 +00:00
|
|
|
free(this->keys.keys);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this->events);
|
|
|
|
}
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
void FunctionBar_setLabel(FunctionBar* this, int event, const char* text) {
|
2006-03-04 18:16:49 +00:00
|
|
|
for (int i = 0; i < this->size; i++) {
|
|
|
|
if (this->events[i] == event) {
|
|
|
|
free(this->functions[i]);
|
2016-02-02 14:53:02 +00:00
|
|
|
this->functions[i] = xStrdup(text);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
void FunctionBar_draw(const FunctionBar* this) {
|
|
|
|
FunctionBar_drawExtra(this, NULL, -1, false);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:14:54 +00:00
|
|
|
void FunctionBar_drawExtra(const FunctionBar* this, const char* buffer, int attr, bool setCursor) {
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[FUNCTION_BAR]);
|
2020-10-31 22:28:02 +00:00
|
|
|
mvhline(LINES - 1, 0, ' ', COLS);
|
2006-03-04 18:16:49 +00:00
|
|
|
int x = 0;
|
|
|
|
for (int i = 0; i < this->size; i++) {
|
|
|
|
attrset(CRT_colors[FUNCTION_KEY]);
|
2020-10-31 22:28:02 +00:00
|
|
|
mvaddstr(LINES - 1, x, this->keys.constKeys[i]);
|
2020-10-04 12:30:35 +00:00
|
|
|
x += strlen(this->keys.constKeys[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[FUNCTION_BAR]);
|
2020-10-31 22:28:02 +00:00
|
|
|
mvaddstr(LINES - 1, x, this->functions[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
x += strlen(this->functions[i]);
|
|
|
|
}
|
2020-10-05 13:14:54 +00:00
|
|
|
|
2011-09-08 01:10:58 +00:00
|
|
|
if (buffer) {
|
2020-11-01 00:09:51 +00:00
|
|
|
if (attr == -1) {
|
2020-10-05 13:14:54 +00:00
|
|
|
attrset(CRT_colors[FUNCTION_BAR]);
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2020-10-05 13:14:54 +00:00
|
|
|
attrset(attr);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-10-31 22:28:02 +00:00
|
|
|
mvaddstr(LINES - 1, x, buffer);
|
2020-10-05 13:14:54 +00:00
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
|
|
|
x += strlen(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setCursor) {
|
|
|
|
CRT_cursorX = x;
|
2011-09-08 01:10:58 +00:00
|
|
|
curs_set(1);
|
|
|
|
} else {
|
|
|
|
curs_set(0);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2020-10-05 13:14:54 +00:00
|
|
|
|
|
|
|
currentLen = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionBar_append(const char* buffer, int attr) {
|
2020-11-01 00:09:51 +00:00
|
|
|
if (attr == -1) {
|
2020-10-05 13:14:54 +00:00
|
|
|
attrset(CRT_colors[FUNCTION_BAR]);
|
2020-11-01 00:09:51 +00:00
|
|
|
} else {
|
2020-10-05 13:14:54 +00:00
|
|
|
attrset(attr);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2020-10-31 22:28:02 +00:00
|
|
|
mvaddstr(LINES - 1, currentLen, buffer);
|
2006-03-04 18:16:49 +00:00
|
|
|
attrset(CRT_colors[RESET_COLOR]);
|
2020-10-05 13:14:54 +00:00
|
|
|
|
|
|
|
currentLen += strlen(buffer);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 20:37:08 +00:00
|
|
|
int FunctionBar_synthesizeEvent(const FunctionBar* this, int pos) {
|
2006-03-04 18:16:49 +00:00
|
|
|
int x = 0;
|
|
|
|
for (int i = 0; i < this->size; i++) {
|
2020-10-04 12:30:35 +00:00
|
|
|
x += strlen(this->keys.constKeys[i]);
|
2006-03-04 18:16:49 +00:00
|
|
|
x += strlen(this->functions[i]);
|
|
|
|
if (pos < x) {
|
|
|
|
return this->events[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR;
|
|
|
|
}
|